intel_display.c 430 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646
  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 == -EDEADLK) {
  2069. /*
  2070. * -EDEADLK means there are no free fences
  2071. * no pending flips.
  2072. *
  2073. * This is propagated to atomic, but it uses
  2074. * -EDEADLK to force a locking recovery, so
  2075. * change the returned error to -EBUSY.
  2076. */
  2077. ret = -EBUSY;
  2078. goto err_unpin;
  2079. } else if (ret)
  2080. goto err_unpin;
  2081. i915_gem_object_pin_fence(obj);
  2082. dev_priv->mm.interruptible = true;
  2083. intel_runtime_pm_put(dev_priv);
  2084. return 0;
  2085. err_unpin:
  2086. i915_gem_object_unpin_from_display_plane(obj, &view);
  2087. err_interruptible:
  2088. dev_priv->mm.interruptible = true;
  2089. intel_runtime_pm_put(dev_priv);
  2090. return ret;
  2091. }
  2092. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2093. const struct drm_plane_state *plane_state)
  2094. {
  2095. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2096. struct i915_ggtt_view view;
  2097. int ret;
  2098. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2099. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2100. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2101. i915_gem_object_unpin_fence(obj);
  2102. i915_gem_object_unpin_from_display_plane(obj, &view);
  2103. }
  2104. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2105. * is assumed to be a power-of-two. */
  2106. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2107. int *x, int *y,
  2108. unsigned int tiling_mode,
  2109. unsigned int cpp,
  2110. unsigned int pitch)
  2111. {
  2112. if (tiling_mode != I915_TILING_NONE) {
  2113. unsigned int tile_rows, tiles;
  2114. tile_rows = *y / 8;
  2115. *y %= 8;
  2116. tiles = *x / (512/cpp);
  2117. *x %= 512/cpp;
  2118. return tile_rows * pitch * 8 + tiles * 4096;
  2119. } else {
  2120. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2121. unsigned int offset;
  2122. offset = *y * pitch + *x * cpp;
  2123. *y = (offset & alignment) / pitch;
  2124. *x = ((offset & alignment) - *y * pitch) / cpp;
  2125. return offset & ~alignment;
  2126. }
  2127. }
  2128. static int i9xx_format_to_fourcc(int format)
  2129. {
  2130. switch (format) {
  2131. case DISPPLANE_8BPP:
  2132. return DRM_FORMAT_C8;
  2133. case DISPPLANE_BGRX555:
  2134. return DRM_FORMAT_XRGB1555;
  2135. case DISPPLANE_BGRX565:
  2136. return DRM_FORMAT_RGB565;
  2137. default:
  2138. case DISPPLANE_BGRX888:
  2139. return DRM_FORMAT_XRGB8888;
  2140. case DISPPLANE_RGBX888:
  2141. return DRM_FORMAT_XBGR8888;
  2142. case DISPPLANE_BGRX101010:
  2143. return DRM_FORMAT_XRGB2101010;
  2144. case DISPPLANE_RGBX101010:
  2145. return DRM_FORMAT_XBGR2101010;
  2146. }
  2147. }
  2148. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2149. {
  2150. switch (format) {
  2151. case PLANE_CTL_FORMAT_RGB_565:
  2152. return DRM_FORMAT_RGB565;
  2153. default:
  2154. case PLANE_CTL_FORMAT_XRGB_8888:
  2155. if (rgb_order) {
  2156. if (alpha)
  2157. return DRM_FORMAT_ABGR8888;
  2158. else
  2159. return DRM_FORMAT_XBGR8888;
  2160. } else {
  2161. if (alpha)
  2162. return DRM_FORMAT_ARGB8888;
  2163. else
  2164. return DRM_FORMAT_XRGB8888;
  2165. }
  2166. case PLANE_CTL_FORMAT_XRGB_2101010:
  2167. if (rgb_order)
  2168. return DRM_FORMAT_XBGR2101010;
  2169. else
  2170. return DRM_FORMAT_XRGB2101010;
  2171. }
  2172. }
  2173. static bool
  2174. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2175. struct intel_initial_plane_config *plane_config)
  2176. {
  2177. struct drm_device *dev = crtc->base.dev;
  2178. struct drm_i915_gem_object *obj = NULL;
  2179. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2180. struct drm_framebuffer *fb = &plane_config->fb->base;
  2181. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2182. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2183. PAGE_SIZE);
  2184. size_aligned -= base_aligned;
  2185. if (plane_config->size == 0)
  2186. return false;
  2187. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2188. base_aligned,
  2189. base_aligned,
  2190. size_aligned);
  2191. if (!obj)
  2192. return false;
  2193. obj->tiling_mode = plane_config->tiling;
  2194. if (obj->tiling_mode == I915_TILING_X)
  2195. obj->stride = fb->pitches[0];
  2196. mode_cmd.pixel_format = fb->pixel_format;
  2197. mode_cmd.width = fb->width;
  2198. mode_cmd.height = fb->height;
  2199. mode_cmd.pitches[0] = fb->pitches[0];
  2200. mode_cmd.modifier[0] = fb->modifier[0];
  2201. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2202. mutex_lock(&dev->struct_mutex);
  2203. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2204. &mode_cmd, obj)) {
  2205. DRM_DEBUG_KMS("intel fb init failed\n");
  2206. goto out_unref_obj;
  2207. }
  2208. mutex_unlock(&dev->struct_mutex);
  2209. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2210. return true;
  2211. out_unref_obj:
  2212. drm_gem_object_unreference(&obj->base);
  2213. mutex_unlock(&dev->struct_mutex);
  2214. return false;
  2215. }
  2216. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2217. static void
  2218. update_state_fb(struct drm_plane *plane)
  2219. {
  2220. if (plane->fb == plane->state->fb)
  2221. return;
  2222. if (plane->state->fb)
  2223. drm_framebuffer_unreference(plane->state->fb);
  2224. plane->state->fb = plane->fb;
  2225. if (plane->state->fb)
  2226. drm_framebuffer_reference(plane->state->fb);
  2227. }
  2228. static void
  2229. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2230. struct intel_initial_plane_config *plane_config)
  2231. {
  2232. struct drm_device *dev = intel_crtc->base.dev;
  2233. struct drm_i915_private *dev_priv = dev->dev_private;
  2234. struct drm_crtc *c;
  2235. struct intel_crtc *i;
  2236. struct drm_i915_gem_object *obj;
  2237. struct drm_plane *primary = intel_crtc->base.primary;
  2238. struct drm_plane_state *plane_state = primary->state;
  2239. struct drm_framebuffer *fb;
  2240. if (!plane_config->fb)
  2241. return;
  2242. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2243. fb = &plane_config->fb->base;
  2244. goto valid_fb;
  2245. }
  2246. kfree(plane_config->fb);
  2247. /*
  2248. * Failed to alloc the obj, check to see if we should share
  2249. * an fb with another CRTC instead
  2250. */
  2251. for_each_crtc(dev, c) {
  2252. i = to_intel_crtc(c);
  2253. if (c == &intel_crtc->base)
  2254. continue;
  2255. if (!i->active)
  2256. continue;
  2257. fb = c->primary->fb;
  2258. if (!fb)
  2259. continue;
  2260. obj = intel_fb_obj(fb);
  2261. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2262. drm_framebuffer_reference(fb);
  2263. goto valid_fb;
  2264. }
  2265. }
  2266. return;
  2267. valid_fb:
  2268. plane_state->src_x = plane_state->src_y = 0;
  2269. plane_state->src_w = fb->width << 16;
  2270. plane_state->src_h = fb->height << 16;
  2271. plane_state->crtc_x = plane_state->src_y = 0;
  2272. plane_state->crtc_w = fb->width;
  2273. plane_state->crtc_h = fb->height;
  2274. obj = intel_fb_obj(fb);
  2275. if (obj->tiling_mode != I915_TILING_NONE)
  2276. dev_priv->preserve_bios_swizzle = true;
  2277. drm_framebuffer_reference(fb);
  2278. primary->fb = primary->state->fb = fb;
  2279. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2280. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2281. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2282. }
  2283. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2284. struct drm_framebuffer *fb,
  2285. int x, int y)
  2286. {
  2287. struct drm_device *dev = crtc->dev;
  2288. struct drm_i915_private *dev_priv = dev->dev_private;
  2289. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2290. struct drm_plane *primary = crtc->primary;
  2291. bool visible = to_intel_plane_state(primary->state)->visible;
  2292. struct drm_i915_gem_object *obj;
  2293. int plane = intel_crtc->plane;
  2294. unsigned long linear_offset;
  2295. u32 dspcntr;
  2296. u32 reg = DSPCNTR(plane);
  2297. int pixel_size;
  2298. if (!visible || !fb) {
  2299. I915_WRITE(reg, 0);
  2300. if (INTEL_INFO(dev)->gen >= 4)
  2301. I915_WRITE(DSPSURF(plane), 0);
  2302. else
  2303. I915_WRITE(DSPADDR(plane), 0);
  2304. POSTING_READ(reg);
  2305. return;
  2306. }
  2307. obj = intel_fb_obj(fb);
  2308. if (WARN_ON(obj == NULL))
  2309. return;
  2310. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2311. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2312. dspcntr |= DISPLAY_PLANE_ENABLE;
  2313. if (INTEL_INFO(dev)->gen < 4) {
  2314. if (intel_crtc->pipe == PIPE_B)
  2315. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2316. /* pipesrc and dspsize control the size that is scaled from,
  2317. * which should always be the user's requested size.
  2318. */
  2319. I915_WRITE(DSPSIZE(plane),
  2320. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2321. (intel_crtc->config->pipe_src_w - 1));
  2322. I915_WRITE(DSPPOS(plane), 0);
  2323. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2324. I915_WRITE(PRIMSIZE(plane),
  2325. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2326. (intel_crtc->config->pipe_src_w - 1));
  2327. I915_WRITE(PRIMPOS(plane), 0);
  2328. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2329. }
  2330. switch (fb->pixel_format) {
  2331. case DRM_FORMAT_C8:
  2332. dspcntr |= DISPPLANE_8BPP;
  2333. break;
  2334. case DRM_FORMAT_XRGB1555:
  2335. dspcntr |= DISPPLANE_BGRX555;
  2336. break;
  2337. case DRM_FORMAT_RGB565:
  2338. dspcntr |= DISPPLANE_BGRX565;
  2339. break;
  2340. case DRM_FORMAT_XRGB8888:
  2341. dspcntr |= DISPPLANE_BGRX888;
  2342. break;
  2343. case DRM_FORMAT_XBGR8888:
  2344. dspcntr |= DISPPLANE_RGBX888;
  2345. break;
  2346. case DRM_FORMAT_XRGB2101010:
  2347. dspcntr |= DISPPLANE_BGRX101010;
  2348. break;
  2349. case DRM_FORMAT_XBGR2101010:
  2350. dspcntr |= DISPPLANE_RGBX101010;
  2351. break;
  2352. default:
  2353. BUG();
  2354. }
  2355. if (INTEL_INFO(dev)->gen >= 4 &&
  2356. obj->tiling_mode != I915_TILING_NONE)
  2357. dspcntr |= DISPPLANE_TILED;
  2358. if (IS_G4X(dev))
  2359. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2360. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2361. if (INTEL_INFO(dev)->gen >= 4) {
  2362. intel_crtc->dspaddr_offset =
  2363. intel_gen4_compute_page_offset(dev_priv,
  2364. &x, &y, obj->tiling_mode,
  2365. pixel_size,
  2366. fb->pitches[0]);
  2367. linear_offset -= intel_crtc->dspaddr_offset;
  2368. } else {
  2369. intel_crtc->dspaddr_offset = linear_offset;
  2370. }
  2371. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2372. dspcntr |= DISPPLANE_ROTATE_180;
  2373. x += (intel_crtc->config->pipe_src_w - 1);
  2374. y += (intel_crtc->config->pipe_src_h - 1);
  2375. /* Finding the last pixel of the last line of the display
  2376. data and adding to linear_offset*/
  2377. linear_offset +=
  2378. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2379. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2380. }
  2381. I915_WRITE(reg, dspcntr);
  2382. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2383. if (INTEL_INFO(dev)->gen >= 4) {
  2384. I915_WRITE(DSPSURF(plane),
  2385. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2386. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2387. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2388. } else
  2389. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2390. POSTING_READ(reg);
  2391. }
  2392. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2393. struct drm_framebuffer *fb,
  2394. int x, int y)
  2395. {
  2396. struct drm_device *dev = crtc->dev;
  2397. struct drm_i915_private *dev_priv = dev->dev_private;
  2398. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2399. struct drm_plane *primary = crtc->primary;
  2400. bool visible = to_intel_plane_state(primary->state)->visible;
  2401. struct drm_i915_gem_object *obj;
  2402. int plane = intel_crtc->plane;
  2403. unsigned long linear_offset;
  2404. u32 dspcntr;
  2405. u32 reg = DSPCNTR(plane);
  2406. int pixel_size;
  2407. if (!visible || !fb) {
  2408. I915_WRITE(reg, 0);
  2409. I915_WRITE(DSPSURF(plane), 0);
  2410. POSTING_READ(reg);
  2411. return;
  2412. }
  2413. obj = intel_fb_obj(fb);
  2414. if (WARN_ON(obj == NULL))
  2415. return;
  2416. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2417. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2418. dspcntr |= DISPLAY_PLANE_ENABLE;
  2419. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2420. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2421. switch (fb->pixel_format) {
  2422. case DRM_FORMAT_C8:
  2423. dspcntr |= DISPPLANE_8BPP;
  2424. break;
  2425. case DRM_FORMAT_RGB565:
  2426. dspcntr |= DISPPLANE_BGRX565;
  2427. break;
  2428. case DRM_FORMAT_XRGB8888:
  2429. dspcntr |= DISPPLANE_BGRX888;
  2430. break;
  2431. case DRM_FORMAT_XBGR8888:
  2432. dspcntr |= DISPPLANE_RGBX888;
  2433. break;
  2434. case DRM_FORMAT_XRGB2101010:
  2435. dspcntr |= DISPPLANE_BGRX101010;
  2436. break;
  2437. case DRM_FORMAT_XBGR2101010:
  2438. dspcntr |= DISPPLANE_RGBX101010;
  2439. break;
  2440. default:
  2441. BUG();
  2442. }
  2443. if (obj->tiling_mode != I915_TILING_NONE)
  2444. dspcntr |= DISPPLANE_TILED;
  2445. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2446. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2447. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2448. intel_crtc->dspaddr_offset =
  2449. intel_gen4_compute_page_offset(dev_priv,
  2450. &x, &y, obj->tiling_mode,
  2451. pixel_size,
  2452. fb->pitches[0]);
  2453. linear_offset -= intel_crtc->dspaddr_offset;
  2454. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2455. dspcntr |= DISPPLANE_ROTATE_180;
  2456. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2457. x += (intel_crtc->config->pipe_src_w - 1);
  2458. y += (intel_crtc->config->pipe_src_h - 1);
  2459. /* Finding the last pixel of the last line of the display
  2460. data and adding to linear_offset*/
  2461. linear_offset +=
  2462. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2463. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2464. }
  2465. }
  2466. I915_WRITE(reg, dspcntr);
  2467. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2468. I915_WRITE(DSPSURF(plane),
  2469. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2470. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2471. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2472. } else {
  2473. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2474. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2475. }
  2476. POSTING_READ(reg);
  2477. }
  2478. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2479. uint32_t pixel_format)
  2480. {
  2481. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2482. /*
  2483. * The stride is either expressed as a multiple of 64 bytes
  2484. * chunks for linear buffers or in number of tiles for tiled
  2485. * buffers.
  2486. */
  2487. switch (fb_modifier) {
  2488. case DRM_FORMAT_MOD_NONE:
  2489. return 64;
  2490. case I915_FORMAT_MOD_X_TILED:
  2491. if (INTEL_INFO(dev)->gen == 2)
  2492. return 128;
  2493. return 512;
  2494. case I915_FORMAT_MOD_Y_TILED:
  2495. /* No need to check for old gens and Y tiling since this is
  2496. * about the display engine and those will be blocked before
  2497. * we get here.
  2498. */
  2499. return 128;
  2500. case I915_FORMAT_MOD_Yf_TILED:
  2501. if (bits_per_pixel == 8)
  2502. return 64;
  2503. else
  2504. return 128;
  2505. default:
  2506. MISSING_CASE(fb_modifier);
  2507. return 64;
  2508. }
  2509. }
  2510. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2511. struct drm_i915_gem_object *obj)
  2512. {
  2513. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2514. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2515. view = &i915_ggtt_view_rotated;
  2516. return i915_gem_obj_ggtt_offset_view(obj, view);
  2517. }
  2518. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2519. {
  2520. struct drm_device *dev = intel_crtc->base.dev;
  2521. struct drm_i915_private *dev_priv = dev->dev_private;
  2522. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2523. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2524. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2525. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2526. intel_crtc->base.base.id, intel_crtc->pipe, id);
  2527. }
  2528. /*
  2529. * This function detaches (aka. unbinds) unused scalers in hardware
  2530. */
  2531. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2532. {
  2533. struct intel_crtc_scaler_state *scaler_state;
  2534. int i;
  2535. scaler_state = &intel_crtc->config->scaler_state;
  2536. /* loop through and disable scalers that aren't in use */
  2537. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2538. if (!scaler_state->scalers[i].in_use)
  2539. skl_detach_scaler(intel_crtc, i);
  2540. }
  2541. }
  2542. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2543. {
  2544. switch (pixel_format) {
  2545. case DRM_FORMAT_C8:
  2546. return PLANE_CTL_FORMAT_INDEXED;
  2547. case DRM_FORMAT_RGB565:
  2548. return PLANE_CTL_FORMAT_RGB_565;
  2549. case DRM_FORMAT_XBGR8888:
  2550. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2551. case DRM_FORMAT_XRGB8888:
  2552. return PLANE_CTL_FORMAT_XRGB_8888;
  2553. /*
  2554. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2555. * to be already pre-multiplied. We need to add a knob (or a different
  2556. * DRM_FORMAT) for user-space to configure that.
  2557. */
  2558. case DRM_FORMAT_ABGR8888:
  2559. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2560. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2561. case DRM_FORMAT_ARGB8888:
  2562. return PLANE_CTL_FORMAT_XRGB_8888 |
  2563. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2564. case DRM_FORMAT_XRGB2101010:
  2565. return PLANE_CTL_FORMAT_XRGB_2101010;
  2566. case DRM_FORMAT_XBGR2101010:
  2567. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2568. case DRM_FORMAT_YUYV:
  2569. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2570. case DRM_FORMAT_YVYU:
  2571. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2572. case DRM_FORMAT_UYVY:
  2573. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2574. case DRM_FORMAT_VYUY:
  2575. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2576. default:
  2577. MISSING_CASE(pixel_format);
  2578. }
  2579. return 0;
  2580. }
  2581. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2582. {
  2583. switch (fb_modifier) {
  2584. case DRM_FORMAT_MOD_NONE:
  2585. break;
  2586. case I915_FORMAT_MOD_X_TILED:
  2587. return PLANE_CTL_TILED_X;
  2588. case I915_FORMAT_MOD_Y_TILED:
  2589. return PLANE_CTL_TILED_Y;
  2590. case I915_FORMAT_MOD_Yf_TILED:
  2591. return PLANE_CTL_TILED_YF;
  2592. default:
  2593. MISSING_CASE(fb_modifier);
  2594. }
  2595. return 0;
  2596. }
  2597. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2598. {
  2599. switch (rotation) {
  2600. case BIT(DRM_ROTATE_0):
  2601. break;
  2602. /*
  2603. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2604. * while i915 HW rotation is clockwise, thats why this swapping.
  2605. */
  2606. case BIT(DRM_ROTATE_90):
  2607. return PLANE_CTL_ROTATE_270;
  2608. case BIT(DRM_ROTATE_180):
  2609. return PLANE_CTL_ROTATE_180;
  2610. case BIT(DRM_ROTATE_270):
  2611. return PLANE_CTL_ROTATE_90;
  2612. default:
  2613. MISSING_CASE(rotation);
  2614. }
  2615. return 0;
  2616. }
  2617. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2618. struct drm_framebuffer *fb,
  2619. int x, int y)
  2620. {
  2621. struct drm_device *dev = crtc->dev;
  2622. struct drm_i915_private *dev_priv = dev->dev_private;
  2623. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2624. struct drm_plane *plane = crtc->primary;
  2625. bool visible = to_intel_plane_state(plane->state)->visible;
  2626. struct drm_i915_gem_object *obj;
  2627. int pipe = intel_crtc->pipe;
  2628. u32 plane_ctl, stride_div, stride;
  2629. u32 tile_height, plane_offset, plane_size;
  2630. unsigned int rotation;
  2631. int x_offset, y_offset;
  2632. unsigned long surf_addr;
  2633. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2634. struct intel_plane_state *plane_state;
  2635. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2636. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2637. int scaler_id = -1;
  2638. plane_state = to_intel_plane_state(plane->state);
  2639. if (!visible || !fb) {
  2640. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2641. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2642. POSTING_READ(PLANE_CTL(pipe, 0));
  2643. return;
  2644. }
  2645. plane_ctl = PLANE_CTL_ENABLE |
  2646. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2647. PLANE_CTL_PIPE_CSC_ENABLE;
  2648. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2649. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2650. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2651. rotation = plane->state->rotation;
  2652. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2653. obj = intel_fb_obj(fb);
  2654. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2655. fb->pixel_format);
  2656. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2657. /*
  2658. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2659. * update_plane helpers are called from legacy paths.
  2660. * Once full atomic crtc is available, below check can be avoided.
  2661. */
  2662. if (drm_rect_width(&plane_state->src)) {
  2663. scaler_id = plane_state->scaler_id;
  2664. src_x = plane_state->src.x1 >> 16;
  2665. src_y = plane_state->src.y1 >> 16;
  2666. src_w = drm_rect_width(&plane_state->src) >> 16;
  2667. src_h = drm_rect_height(&plane_state->src) >> 16;
  2668. dst_x = plane_state->dst.x1;
  2669. dst_y = plane_state->dst.y1;
  2670. dst_w = drm_rect_width(&plane_state->dst);
  2671. dst_h = drm_rect_height(&plane_state->dst);
  2672. WARN_ON(x != src_x || y != src_y);
  2673. } else {
  2674. src_w = intel_crtc->config->pipe_src_w;
  2675. src_h = intel_crtc->config->pipe_src_h;
  2676. }
  2677. if (intel_rotation_90_or_270(rotation)) {
  2678. /* stride = Surface height in tiles */
  2679. tile_height = intel_tile_height(dev, fb->pixel_format,
  2680. fb->modifier[0]);
  2681. stride = DIV_ROUND_UP(fb->height, tile_height);
  2682. x_offset = stride * tile_height - y - src_h;
  2683. y_offset = x;
  2684. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2685. } else {
  2686. stride = fb->pitches[0] / stride_div;
  2687. x_offset = x;
  2688. y_offset = y;
  2689. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2690. }
  2691. plane_offset = y_offset << 16 | x_offset;
  2692. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2693. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2694. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2695. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2696. if (scaler_id >= 0) {
  2697. uint32_t ps_ctrl = 0;
  2698. WARN_ON(!dst_w || !dst_h);
  2699. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2700. crtc_state->scaler_state.scalers[scaler_id].mode;
  2701. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2702. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2703. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2704. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2705. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2706. } else {
  2707. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2708. }
  2709. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2710. POSTING_READ(PLANE_SURF(pipe, 0));
  2711. }
  2712. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2713. static int
  2714. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2715. int x, int y, enum mode_set_atomic state)
  2716. {
  2717. struct drm_device *dev = crtc->dev;
  2718. struct drm_i915_private *dev_priv = dev->dev_private;
  2719. if (dev_priv->fbc.disable_fbc)
  2720. dev_priv->fbc.disable_fbc(dev_priv);
  2721. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2722. return 0;
  2723. }
  2724. static void intel_complete_page_flips(struct drm_device *dev)
  2725. {
  2726. struct drm_crtc *crtc;
  2727. for_each_crtc(dev, crtc) {
  2728. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2729. enum plane plane = intel_crtc->plane;
  2730. intel_prepare_page_flip(dev, plane);
  2731. intel_finish_page_flip_plane(dev, plane);
  2732. }
  2733. }
  2734. static void intel_update_primary_planes(struct drm_device *dev)
  2735. {
  2736. struct drm_i915_private *dev_priv = dev->dev_private;
  2737. struct drm_crtc *crtc;
  2738. for_each_crtc(dev, crtc) {
  2739. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2740. drm_modeset_lock(&crtc->mutex, NULL);
  2741. /*
  2742. * FIXME: Once we have proper support for primary planes (and
  2743. * disabling them without disabling the entire crtc) allow again
  2744. * a NULL crtc->primary->fb.
  2745. */
  2746. if (intel_crtc->active && crtc->primary->fb)
  2747. dev_priv->display.update_primary_plane(crtc,
  2748. crtc->primary->fb,
  2749. crtc->x,
  2750. crtc->y);
  2751. drm_modeset_unlock(&crtc->mutex);
  2752. }
  2753. }
  2754. void intel_prepare_reset(struct drm_device *dev)
  2755. {
  2756. /* no reset support for gen2 */
  2757. if (IS_GEN2(dev))
  2758. return;
  2759. /* reset doesn't touch the display */
  2760. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2761. return;
  2762. drm_modeset_lock_all(dev);
  2763. /*
  2764. * Disabling the crtcs gracefully seems nicer. Also the
  2765. * g33 docs say we should at least disable all the planes.
  2766. */
  2767. intel_display_suspend(dev);
  2768. }
  2769. void intel_finish_reset(struct drm_device *dev)
  2770. {
  2771. struct drm_i915_private *dev_priv = to_i915(dev);
  2772. /*
  2773. * Flips in the rings will be nuked by the reset,
  2774. * so complete all pending flips so that user space
  2775. * will get its events and not get stuck.
  2776. */
  2777. intel_complete_page_flips(dev);
  2778. /* no reset support for gen2 */
  2779. if (IS_GEN2(dev))
  2780. return;
  2781. /* reset doesn't touch the display */
  2782. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2783. /*
  2784. * Flips in the rings have been nuked by the reset,
  2785. * so update the base address of all primary
  2786. * planes to the the last fb to make sure we're
  2787. * showing the correct fb after a reset.
  2788. */
  2789. intel_update_primary_planes(dev);
  2790. return;
  2791. }
  2792. /*
  2793. * The display has been reset as well,
  2794. * so need a full re-initialization.
  2795. */
  2796. intel_runtime_pm_disable_interrupts(dev_priv);
  2797. intel_runtime_pm_enable_interrupts(dev_priv);
  2798. intel_modeset_init_hw(dev);
  2799. spin_lock_irq(&dev_priv->irq_lock);
  2800. if (dev_priv->display.hpd_irq_setup)
  2801. dev_priv->display.hpd_irq_setup(dev);
  2802. spin_unlock_irq(&dev_priv->irq_lock);
  2803. intel_display_resume(dev);
  2804. intel_hpd_init(dev_priv);
  2805. drm_modeset_unlock_all(dev);
  2806. }
  2807. static void
  2808. intel_finish_fb(struct drm_framebuffer *old_fb)
  2809. {
  2810. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2811. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2812. bool was_interruptible = dev_priv->mm.interruptible;
  2813. int ret;
  2814. /* Big Hammer, we also need to ensure that any pending
  2815. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2816. * current scanout is retired before unpinning the old
  2817. * framebuffer. Note that we rely on userspace rendering
  2818. * into the buffer attached to the pipe they are waiting
  2819. * on. If not, userspace generates a GPU hang with IPEHR
  2820. * point to the MI_WAIT_FOR_EVENT.
  2821. *
  2822. * This should only fail upon a hung GPU, in which case we
  2823. * can safely continue.
  2824. */
  2825. dev_priv->mm.interruptible = false;
  2826. ret = i915_gem_object_wait_rendering(obj, true);
  2827. dev_priv->mm.interruptible = was_interruptible;
  2828. WARN_ON(ret);
  2829. }
  2830. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2831. {
  2832. struct drm_device *dev = crtc->dev;
  2833. struct drm_i915_private *dev_priv = dev->dev_private;
  2834. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2835. bool pending;
  2836. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2837. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2838. return false;
  2839. spin_lock_irq(&dev->event_lock);
  2840. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2841. spin_unlock_irq(&dev->event_lock);
  2842. return pending;
  2843. }
  2844. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2845. {
  2846. struct drm_device *dev = crtc->base.dev;
  2847. struct drm_i915_private *dev_priv = dev->dev_private;
  2848. const struct drm_display_mode *adjusted_mode;
  2849. if (!i915.fastboot)
  2850. return;
  2851. /*
  2852. * Update pipe size and adjust fitter if needed: the reason for this is
  2853. * that in compute_mode_changes we check the native mode (not the pfit
  2854. * mode) to see if we can flip rather than do a full mode set. In the
  2855. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2856. * pfit state, we'll end up with a big fb scanned out into the wrong
  2857. * sized surface.
  2858. *
  2859. * To fix this properly, we need to hoist the checks up into
  2860. * compute_mode_changes (or above), check the actual pfit state and
  2861. * whether the platform allows pfit disable with pipe active, and only
  2862. * then update the pipesrc and pfit state, even on the flip path.
  2863. */
  2864. adjusted_mode = &crtc->config->base.adjusted_mode;
  2865. I915_WRITE(PIPESRC(crtc->pipe),
  2866. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2867. (adjusted_mode->crtc_vdisplay - 1));
  2868. if (!crtc->config->pch_pfit.enabled &&
  2869. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2870. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2871. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2872. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2873. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2874. }
  2875. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2876. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2877. }
  2878. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2879. {
  2880. struct drm_device *dev = crtc->dev;
  2881. struct drm_i915_private *dev_priv = dev->dev_private;
  2882. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2883. int pipe = intel_crtc->pipe;
  2884. u32 reg, temp;
  2885. /* enable normal train */
  2886. reg = FDI_TX_CTL(pipe);
  2887. temp = I915_READ(reg);
  2888. if (IS_IVYBRIDGE(dev)) {
  2889. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2890. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2891. } else {
  2892. temp &= ~FDI_LINK_TRAIN_NONE;
  2893. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2894. }
  2895. I915_WRITE(reg, temp);
  2896. reg = FDI_RX_CTL(pipe);
  2897. temp = I915_READ(reg);
  2898. if (HAS_PCH_CPT(dev)) {
  2899. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2900. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2901. } else {
  2902. temp &= ~FDI_LINK_TRAIN_NONE;
  2903. temp |= FDI_LINK_TRAIN_NONE;
  2904. }
  2905. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2906. /* wait one idle pattern time */
  2907. POSTING_READ(reg);
  2908. udelay(1000);
  2909. /* IVB wants error correction enabled */
  2910. if (IS_IVYBRIDGE(dev))
  2911. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2912. FDI_FE_ERRC_ENABLE);
  2913. }
  2914. /* The FDI link training functions for ILK/Ibexpeak. */
  2915. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2916. {
  2917. struct drm_device *dev = crtc->dev;
  2918. struct drm_i915_private *dev_priv = dev->dev_private;
  2919. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2920. int pipe = intel_crtc->pipe;
  2921. u32 reg, temp, tries;
  2922. /* FDI needs bits from pipe first */
  2923. assert_pipe_enabled(dev_priv, pipe);
  2924. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2925. for train result */
  2926. reg = FDI_RX_IMR(pipe);
  2927. temp = I915_READ(reg);
  2928. temp &= ~FDI_RX_SYMBOL_LOCK;
  2929. temp &= ~FDI_RX_BIT_LOCK;
  2930. I915_WRITE(reg, temp);
  2931. I915_READ(reg);
  2932. udelay(150);
  2933. /* enable CPU FDI TX and PCH FDI RX */
  2934. reg = FDI_TX_CTL(pipe);
  2935. temp = I915_READ(reg);
  2936. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2937. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2938. temp &= ~FDI_LINK_TRAIN_NONE;
  2939. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2940. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2941. reg = FDI_RX_CTL(pipe);
  2942. temp = I915_READ(reg);
  2943. temp &= ~FDI_LINK_TRAIN_NONE;
  2944. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2945. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2946. POSTING_READ(reg);
  2947. udelay(150);
  2948. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2949. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2950. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2951. FDI_RX_PHASE_SYNC_POINTER_EN);
  2952. reg = FDI_RX_IIR(pipe);
  2953. for (tries = 0; tries < 5; tries++) {
  2954. temp = I915_READ(reg);
  2955. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2956. if ((temp & FDI_RX_BIT_LOCK)) {
  2957. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2958. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2959. break;
  2960. }
  2961. }
  2962. if (tries == 5)
  2963. DRM_ERROR("FDI train 1 fail!\n");
  2964. /* Train 2 */
  2965. reg = FDI_TX_CTL(pipe);
  2966. temp = I915_READ(reg);
  2967. temp &= ~FDI_LINK_TRAIN_NONE;
  2968. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2969. I915_WRITE(reg, temp);
  2970. reg = FDI_RX_CTL(pipe);
  2971. temp = I915_READ(reg);
  2972. temp &= ~FDI_LINK_TRAIN_NONE;
  2973. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2974. I915_WRITE(reg, temp);
  2975. POSTING_READ(reg);
  2976. udelay(150);
  2977. reg = FDI_RX_IIR(pipe);
  2978. for (tries = 0; tries < 5; tries++) {
  2979. temp = I915_READ(reg);
  2980. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2981. if (temp & FDI_RX_SYMBOL_LOCK) {
  2982. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2983. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2984. break;
  2985. }
  2986. }
  2987. if (tries == 5)
  2988. DRM_ERROR("FDI train 2 fail!\n");
  2989. DRM_DEBUG_KMS("FDI train done\n");
  2990. }
  2991. static const int snb_b_fdi_train_param[] = {
  2992. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2993. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2994. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2995. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2996. };
  2997. /* The FDI link training functions for SNB/Cougarpoint. */
  2998. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2999. {
  3000. struct drm_device *dev = crtc->dev;
  3001. struct drm_i915_private *dev_priv = dev->dev_private;
  3002. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3003. int pipe = intel_crtc->pipe;
  3004. u32 reg, temp, i, retry;
  3005. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3006. for train result */
  3007. reg = FDI_RX_IMR(pipe);
  3008. temp = I915_READ(reg);
  3009. temp &= ~FDI_RX_SYMBOL_LOCK;
  3010. temp &= ~FDI_RX_BIT_LOCK;
  3011. I915_WRITE(reg, temp);
  3012. POSTING_READ(reg);
  3013. udelay(150);
  3014. /* enable CPU FDI TX and PCH FDI RX */
  3015. reg = FDI_TX_CTL(pipe);
  3016. temp = I915_READ(reg);
  3017. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3018. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3019. temp &= ~FDI_LINK_TRAIN_NONE;
  3020. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3021. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3022. /* SNB-B */
  3023. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3024. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3025. I915_WRITE(FDI_RX_MISC(pipe),
  3026. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3027. reg = FDI_RX_CTL(pipe);
  3028. temp = I915_READ(reg);
  3029. if (HAS_PCH_CPT(dev)) {
  3030. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3031. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3032. } else {
  3033. temp &= ~FDI_LINK_TRAIN_NONE;
  3034. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3035. }
  3036. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3037. POSTING_READ(reg);
  3038. udelay(150);
  3039. for (i = 0; i < 4; i++) {
  3040. reg = FDI_TX_CTL(pipe);
  3041. temp = I915_READ(reg);
  3042. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3043. temp |= snb_b_fdi_train_param[i];
  3044. I915_WRITE(reg, temp);
  3045. POSTING_READ(reg);
  3046. udelay(500);
  3047. for (retry = 0; retry < 5; retry++) {
  3048. reg = FDI_RX_IIR(pipe);
  3049. temp = I915_READ(reg);
  3050. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3051. if (temp & FDI_RX_BIT_LOCK) {
  3052. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3053. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3054. break;
  3055. }
  3056. udelay(50);
  3057. }
  3058. if (retry < 5)
  3059. break;
  3060. }
  3061. if (i == 4)
  3062. DRM_ERROR("FDI train 1 fail!\n");
  3063. /* Train 2 */
  3064. reg = FDI_TX_CTL(pipe);
  3065. temp = I915_READ(reg);
  3066. temp &= ~FDI_LINK_TRAIN_NONE;
  3067. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3068. if (IS_GEN6(dev)) {
  3069. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3070. /* SNB-B */
  3071. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3072. }
  3073. I915_WRITE(reg, temp);
  3074. reg = FDI_RX_CTL(pipe);
  3075. temp = I915_READ(reg);
  3076. if (HAS_PCH_CPT(dev)) {
  3077. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3078. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3079. } else {
  3080. temp &= ~FDI_LINK_TRAIN_NONE;
  3081. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3082. }
  3083. I915_WRITE(reg, temp);
  3084. POSTING_READ(reg);
  3085. udelay(150);
  3086. for (i = 0; i < 4; i++) {
  3087. reg = FDI_TX_CTL(pipe);
  3088. temp = I915_READ(reg);
  3089. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3090. temp |= snb_b_fdi_train_param[i];
  3091. I915_WRITE(reg, temp);
  3092. POSTING_READ(reg);
  3093. udelay(500);
  3094. for (retry = 0; retry < 5; retry++) {
  3095. reg = FDI_RX_IIR(pipe);
  3096. temp = I915_READ(reg);
  3097. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3098. if (temp & FDI_RX_SYMBOL_LOCK) {
  3099. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3100. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3101. break;
  3102. }
  3103. udelay(50);
  3104. }
  3105. if (retry < 5)
  3106. break;
  3107. }
  3108. if (i == 4)
  3109. DRM_ERROR("FDI train 2 fail!\n");
  3110. DRM_DEBUG_KMS("FDI train done.\n");
  3111. }
  3112. /* Manual link training for Ivy Bridge A0 parts */
  3113. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3114. {
  3115. struct drm_device *dev = crtc->dev;
  3116. struct drm_i915_private *dev_priv = dev->dev_private;
  3117. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3118. int pipe = intel_crtc->pipe;
  3119. u32 reg, temp, i, j;
  3120. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3121. for train result */
  3122. reg = FDI_RX_IMR(pipe);
  3123. temp = I915_READ(reg);
  3124. temp &= ~FDI_RX_SYMBOL_LOCK;
  3125. temp &= ~FDI_RX_BIT_LOCK;
  3126. I915_WRITE(reg, temp);
  3127. POSTING_READ(reg);
  3128. udelay(150);
  3129. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3130. I915_READ(FDI_RX_IIR(pipe)));
  3131. /* Try each vswing and preemphasis setting twice before moving on */
  3132. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3133. /* disable first in case we need to retry */
  3134. reg = FDI_TX_CTL(pipe);
  3135. temp = I915_READ(reg);
  3136. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3137. temp &= ~FDI_TX_ENABLE;
  3138. I915_WRITE(reg, temp);
  3139. reg = FDI_RX_CTL(pipe);
  3140. temp = I915_READ(reg);
  3141. temp &= ~FDI_LINK_TRAIN_AUTO;
  3142. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3143. temp &= ~FDI_RX_ENABLE;
  3144. I915_WRITE(reg, temp);
  3145. /* enable CPU FDI TX and PCH FDI RX */
  3146. reg = FDI_TX_CTL(pipe);
  3147. temp = I915_READ(reg);
  3148. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3149. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3150. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3151. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3152. temp |= snb_b_fdi_train_param[j/2];
  3153. temp |= FDI_COMPOSITE_SYNC;
  3154. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3155. I915_WRITE(FDI_RX_MISC(pipe),
  3156. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3157. reg = FDI_RX_CTL(pipe);
  3158. temp = I915_READ(reg);
  3159. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3160. temp |= FDI_COMPOSITE_SYNC;
  3161. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3162. POSTING_READ(reg);
  3163. udelay(1); /* should be 0.5us */
  3164. for (i = 0; i < 4; i++) {
  3165. reg = FDI_RX_IIR(pipe);
  3166. temp = I915_READ(reg);
  3167. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3168. if (temp & FDI_RX_BIT_LOCK ||
  3169. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3170. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3171. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3172. i);
  3173. break;
  3174. }
  3175. udelay(1); /* should be 0.5us */
  3176. }
  3177. if (i == 4) {
  3178. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3179. continue;
  3180. }
  3181. /* Train 2 */
  3182. reg = FDI_TX_CTL(pipe);
  3183. temp = I915_READ(reg);
  3184. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3185. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3186. I915_WRITE(reg, temp);
  3187. reg = FDI_RX_CTL(pipe);
  3188. temp = I915_READ(reg);
  3189. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3190. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3191. I915_WRITE(reg, temp);
  3192. POSTING_READ(reg);
  3193. udelay(2); /* should be 1.5us */
  3194. for (i = 0; i < 4; i++) {
  3195. reg = FDI_RX_IIR(pipe);
  3196. temp = I915_READ(reg);
  3197. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3198. if (temp & FDI_RX_SYMBOL_LOCK ||
  3199. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3200. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3201. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3202. i);
  3203. goto train_done;
  3204. }
  3205. udelay(2); /* should be 1.5us */
  3206. }
  3207. if (i == 4)
  3208. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3209. }
  3210. train_done:
  3211. DRM_DEBUG_KMS("FDI train done.\n");
  3212. }
  3213. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3214. {
  3215. struct drm_device *dev = intel_crtc->base.dev;
  3216. struct drm_i915_private *dev_priv = dev->dev_private;
  3217. int pipe = intel_crtc->pipe;
  3218. u32 reg, temp;
  3219. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3220. reg = FDI_RX_CTL(pipe);
  3221. temp = I915_READ(reg);
  3222. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3223. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3224. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3225. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3226. POSTING_READ(reg);
  3227. udelay(200);
  3228. /* Switch from Rawclk to PCDclk */
  3229. temp = I915_READ(reg);
  3230. I915_WRITE(reg, temp | FDI_PCDCLK);
  3231. POSTING_READ(reg);
  3232. udelay(200);
  3233. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3234. reg = FDI_TX_CTL(pipe);
  3235. temp = I915_READ(reg);
  3236. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3237. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3238. POSTING_READ(reg);
  3239. udelay(100);
  3240. }
  3241. }
  3242. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3243. {
  3244. struct drm_device *dev = intel_crtc->base.dev;
  3245. struct drm_i915_private *dev_priv = dev->dev_private;
  3246. int pipe = intel_crtc->pipe;
  3247. u32 reg, temp;
  3248. /* Switch from PCDclk to Rawclk */
  3249. reg = FDI_RX_CTL(pipe);
  3250. temp = I915_READ(reg);
  3251. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3252. /* Disable CPU FDI TX PLL */
  3253. reg = FDI_TX_CTL(pipe);
  3254. temp = I915_READ(reg);
  3255. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3256. POSTING_READ(reg);
  3257. udelay(100);
  3258. reg = FDI_RX_CTL(pipe);
  3259. temp = I915_READ(reg);
  3260. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3261. /* Wait for the clocks to turn off. */
  3262. POSTING_READ(reg);
  3263. udelay(100);
  3264. }
  3265. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3266. {
  3267. struct drm_device *dev = crtc->dev;
  3268. struct drm_i915_private *dev_priv = dev->dev_private;
  3269. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3270. int pipe = intel_crtc->pipe;
  3271. u32 reg, temp;
  3272. /* disable CPU FDI tx and PCH FDI rx */
  3273. reg = FDI_TX_CTL(pipe);
  3274. temp = I915_READ(reg);
  3275. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3276. POSTING_READ(reg);
  3277. reg = FDI_RX_CTL(pipe);
  3278. temp = I915_READ(reg);
  3279. temp &= ~(0x7 << 16);
  3280. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3281. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3282. POSTING_READ(reg);
  3283. udelay(100);
  3284. /* Ironlake workaround, disable clock pointer after downing FDI */
  3285. if (HAS_PCH_IBX(dev))
  3286. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3287. /* still set train pattern 1 */
  3288. reg = FDI_TX_CTL(pipe);
  3289. temp = I915_READ(reg);
  3290. temp &= ~FDI_LINK_TRAIN_NONE;
  3291. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3292. I915_WRITE(reg, temp);
  3293. reg = FDI_RX_CTL(pipe);
  3294. temp = I915_READ(reg);
  3295. if (HAS_PCH_CPT(dev)) {
  3296. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3297. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3298. } else {
  3299. temp &= ~FDI_LINK_TRAIN_NONE;
  3300. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3301. }
  3302. /* BPC in FDI rx is consistent with that in PIPECONF */
  3303. temp &= ~(0x07 << 16);
  3304. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3305. I915_WRITE(reg, temp);
  3306. POSTING_READ(reg);
  3307. udelay(100);
  3308. }
  3309. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3310. {
  3311. struct intel_crtc *crtc;
  3312. /* Note that we don't need to be called with mode_config.lock here
  3313. * as our list of CRTC objects is static for the lifetime of the
  3314. * device and so cannot disappear as we iterate. Similarly, we can
  3315. * happily treat the predicates as racy, atomic checks as userspace
  3316. * cannot claim and pin a new fb without at least acquring the
  3317. * struct_mutex and so serialising with us.
  3318. */
  3319. for_each_intel_crtc(dev, crtc) {
  3320. if (atomic_read(&crtc->unpin_work_count) == 0)
  3321. continue;
  3322. if (crtc->unpin_work)
  3323. intel_wait_for_vblank(dev, crtc->pipe);
  3324. return true;
  3325. }
  3326. return false;
  3327. }
  3328. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3329. {
  3330. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3331. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3332. /* ensure that the unpin work is consistent wrt ->pending. */
  3333. smp_rmb();
  3334. intel_crtc->unpin_work = NULL;
  3335. if (work->event)
  3336. drm_send_vblank_event(intel_crtc->base.dev,
  3337. intel_crtc->pipe,
  3338. work->event);
  3339. drm_crtc_vblank_put(&intel_crtc->base);
  3340. wake_up_all(&dev_priv->pending_flip_queue);
  3341. queue_work(dev_priv->wq, &work->work);
  3342. trace_i915_flip_complete(intel_crtc->plane,
  3343. work->pending_flip_obj);
  3344. }
  3345. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3346. {
  3347. struct drm_device *dev = crtc->dev;
  3348. struct drm_i915_private *dev_priv = dev->dev_private;
  3349. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3350. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3351. !intel_crtc_has_pending_flip(crtc),
  3352. 60*HZ) == 0)) {
  3353. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3354. spin_lock_irq(&dev->event_lock);
  3355. if (intel_crtc->unpin_work) {
  3356. WARN_ONCE(1, "Removing stuck page flip\n");
  3357. page_flip_completed(intel_crtc);
  3358. }
  3359. spin_unlock_irq(&dev->event_lock);
  3360. }
  3361. if (crtc->primary->fb) {
  3362. mutex_lock(&dev->struct_mutex);
  3363. intel_finish_fb(crtc->primary->fb);
  3364. mutex_unlock(&dev->struct_mutex);
  3365. }
  3366. }
  3367. /* Program iCLKIP clock to the desired frequency */
  3368. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3369. {
  3370. struct drm_device *dev = crtc->dev;
  3371. struct drm_i915_private *dev_priv = dev->dev_private;
  3372. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3373. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3374. u32 temp;
  3375. mutex_lock(&dev_priv->sb_lock);
  3376. /* It is necessary to ungate the pixclk gate prior to programming
  3377. * the divisors, and gate it back when it is done.
  3378. */
  3379. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3380. /* Disable SSCCTL */
  3381. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3382. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3383. SBI_SSCCTL_DISABLE,
  3384. SBI_ICLK);
  3385. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3386. if (clock == 20000) {
  3387. auxdiv = 1;
  3388. divsel = 0x41;
  3389. phaseinc = 0x20;
  3390. } else {
  3391. /* The iCLK virtual clock root frequency is in MHz,
  3392. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3393. * divisors, it is necessary to divide one by another, so we
  3394. * convert the virtual clock precision to KHz here for higher
  3395. * precision.
  3396. */
  3397. u32 iclk_virtual_root_freq = 172800 * 1000;
  3398. u32 iclk_pi_range = 64;
  3399. u32 desired_divisor, msb_divisor_value, pi_value;
  3400. desired_divisor = (iclk_virtual_root_freq / clock);
  3401. msb_divisor_value = desired_divisor / iclk_pi_range;
  3402. pi_value = desired_divisor % iclk_pi_range;
  3403. auxdiv = 0;
  3404. divsel = msb_divisor_value - 2;
  3405. phaseinc = pi_value;
  3406. }
  3407. /* This should not happen with any sane values */
  3408. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3409. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3410. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3411. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3412. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3413. clock,
  3414. auxdiv,
  3415. divsel,
  3416. phasedir,
  3417. phaseinc);
  3418. /* Program SSCDIVINTPHASE6 */
  3419. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3420. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3421. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3422. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3423. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3424. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3425. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3426. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3427. /* Program SSCAUXDIV */
  3428. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3429. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3430. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3431. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3432. /* Enable modulator and associated divider */
  3433. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3434. temp &= ~SBI_SSCCTL_DISABLE;
  3435. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3436. /* Wait for initialization time */
  3437. udelay(24);
  3438. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3439. mutex_unlock(&dev_priv->sb_lock);
  3440. }
  3441. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3442. enum pipe pch_transcoder)
  3443. {
  3444. struct drm_device *dev = crtc->base.dev;
  3445. struct drm_i915_private *dev_priv = dev->dev_private;
  3446. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3447. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3448. I915_READ(HTOTAL(cpu_transcoder)));
  3449. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3450. I915_READ(HBLANK(cpu_transcoder)));
  3451. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3452. I915_READ(HSYNC(cpu_transcoder)));
  3453. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3454. I915_READ(VTOTAL(cpu_transcoder)));
  3455. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3456. I915_READ(VBLANK(cpu_transcoder)));
  3457. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3458. I915_READ(VSYNC(cpu_transcoder)));
  3459. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3460. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3461. }
  3462. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3463. {
  3464. struct drm_i915_private *dev_priv = dev->dev_private;
  3465. uint32_t temp;
  3466. temp = I915_READ(SOUTH_CHICKEN1);
  3467. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3468. return;
  3469. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3470. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3471. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3472. if (enable)
  3473. temp |= FDI_BC_BIFURCATION_SELECT;
  3474. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3475. I915_WRITE(SOUTH_CHICKEN1, temp);
  3476. POSTING_READ(SOUTH_CHICKEN1);
  3477. }
  3478. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3479. {
  3480. struct drm_device *dev = intel_crtc->base.dev;
  3481. switch (intel_crtc->pipe) {
  3482. case PIPE_A:
  3483. break;
  3484. case PIPE_B:
  3485. if (intel_crtc->config->fdi_lanes > 2)
  3486. cpt_set_fdi_bc_bifurcation(dev, false);
  3487. else
  3488. cpt_set_fdi_bc_bifurcation(dev, true);
  3489. break;
  3490. case PIPE_C:
  3491. cpt_set_fdi_bc_bifurcation(dev, true);
  3492. break;
  3493. default:
  3494. BUG();
  3495. }
  3496. }
  3497. /*
  3498. * Enable PCH resources required for PCH ports:
  3499. * - PCH PLLs
  3500. * - FDI training & RX/TX
  3501. * - update transcoder timings
  3502. * - DP transcoding bits
  3503. * - transcoder
  3504. */
  3505. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3506. {
  3507. struct drm_device *dev = crtc->dev;
  3508. struct drm_i915_private *dev_priv = dev->dev_private;
  3509. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3510. int pipe = intel_crtc->pipe;
  3511. u32 reg, temp;
  3512. assert_pch_transcoder_disabled(dev_priv, pipe);
  3513. if (IS_IVYBRIDGE(dev))
  3514. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3515. /* Write the TU size bits before fdi link training, so that error
  3516. * detection works. */
  3517. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3518. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3519. /* For PCH output, training FDI link */
  3520. dev_priv->display.fdi_link_train(crtc);
  3521. /* We need to program the right clock selection before writing the pixel
  3522. * mutliplier into the DPLL. */
  3523. if (HAS_PCH_CPT(dev)) {
  3524. u32 sel;
  3525. temp = I915_READ(PCH_DPLL_SEL);
  3526. temp |= TRANS_DPLL_ENABLE(pipe);
  3527. sel = TRANS_DPLLB_SEL(pipe);
  3528. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3529. temp |= sel;
  3530. else
  3531. temp &= ~sel;
  3532. I915_WRITE(PCH_DPLL_SEL, temp);
  3533. }
  3534. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3535. * transcoder, and we actually should do this to not upset any PCH
  3536. * transcoder that already use the clock when we share it.
  3537. *
  3538. * Note that enable_shared_dpll tries to do the right thing, but
  3539. * get_shared_dpll unconditionally resets the pll - we need that to have
  3540. * the right LVDS enable sequence. */
  3541. intel_enable_shared_dpll(intel_crtc);
  3542. /* set transcoder timing, panel must allow it */
  3543. assert_panel_unlocked(dev_priv, pipe);
  3544. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3545. intel_fdi_normal_train(crtc);
  3546. /* For PCH DP, enable TRANS_DP_CTL */
  3547. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3548. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3549. reg = TRANS_DP_CTL(pipe);
  3550. temp = I915_READ(reg);
  3551. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3552. TRANS_DP_SYNC_MASK |
  3553. TRANS_DP_BPC_MASK);
  3554. temp |= TRANS_DP_OUTPUT_ENABLE;
  3555. temp |= bpc << 9; /* same format but at 11:9 */
  3556. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3557. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3558. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3559. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3560. switch (intel_trans_dp_port_sel(crtc)) {
  3561. case PCH_DP_B:
  3562. temp |= TRANS_DP_PORT_SEL_B;
  3563. break;
  3564. case PCH_DP_C:
  3565. temp |= TRANS_DP_PORT_SEL_C;
  3566. break;
  3567. case PCH_DP_D:
  3568. temp |= TRANS_DP_PORT_SEL_D;
  3569. break;
  3570. default:
  3571. BUG();
  3572. }
  3573. I915_WRITE(reg, temp);
  3574. }
  3575. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3576. }
  3577. static void lpt_pch_enable(struct drm_crtc *crtc)
  3578. {
  3579. struct drm_device *dev = crtc->dev;
  3580. struct drm_i915_private *dev_priv = dev->dev_private;
  3581. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3582. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3583. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3584. lpt_program_iclkip(crtc);
  3585. /* Set transcoder timing. */
  3586. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3587. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3588. }
  3589. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3590. struct intel_crtc_state *crtc_state)
  3591. {
  3592. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3593. struct intel_shared_dpll *pll;
  3594. struct intel_shared_dpll_config *shared_dpll;
  3595. enum intel_dpll_id i;
  3596. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3597. if (HAS_PCH_IBX(dev_priv->dev)) {
  3598. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3599. i = (enum intel_dpll_id) crtc->pipe;
  3600. pll = &dev_priv->shared_dplls[i];
  3601. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3602. crtc->base.base.id, pll->name);
  3603. WARN_ON(shared_dpll[i].crtc_mask);
  3604. goto found;
  3605. }
  3606. if (IS_BROXTON(dev_priv->dev)) {
  3607. /* PLL is attached to port in bxt */
  3608. struct intel_encoder *encoder;
  3609. struct intel_digital_port *intel_dig_port;
  3610. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3611. if (WARN_ON(!encoder))
  3612. return NULL;
  3613. intel_dig_port = enc_to_dig_port(&encoder->base);
  3614. /* 1:1 mapping between ports and PLLs */
  3615. i = (enum intel_dpll_id)intel_dig_port->port;
  3616. pll = &dev_priv->shared_dplls[i];
  3617. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3618. crtc->base.base.id, pll->name);
  3619. WARN_ON(shared_dpll[i].crtc_mask);
  3620. goto found;
  3621. }
  3622. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3623. pll = &dev_priv->shared_dplls[i];
  3624. /* Only want to check enabled timings first */
  3625. if (shared_dpll[i].crtc_mask == 0)
  3626. continue;
  3627. if (memcmp(&crtc_state->dpll_hw_state,
  3628. &shared_dpll[i].hw_state,
  3629. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3630. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3631. crtc->base.base.id, pll->name,
  3632. shared_dpll[i].crtc_mask,
  3633. pll->active);
  3634. goto found;
  3635. }
  3636. }
  3637. /* Ok no matching timings, maybe there's a free one? */
  3638. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3639. pll = &dev_priv->shared_dplls[i];
  3640. if (shared_dpll[i].crtc_mask == 0) {
  3641. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3642. crtc->base.base.id, pll->name);
  3643. goto found;
  3644. }
  3645. }
  3646. return NULL;
  3647. found:
  3648. if (shared_dpll[i].crtc_mask == 0)
  3649. shared_dpll[i].hw_state =
  3650. crtc_state->dpll_hw_state;
  3651. crtc_state->shared_dpll = i;
  3652. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3653. pipe_name(crtc->pipe));
  3654. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3655. return pll;
  3656. }
  3657. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3658. {
  3659. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3660. struct intel_shared_dpll_config *shared_dpll;
  3661. struct intel_shared_dpll *pll;
  3662. enum intel_dpll_id i;
  3663. if (!to_intel_atomic_state(state)->dpll_set)
  3664. return;
  3665. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3666. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3667. pll = &dev_priv->shared_dplls[i];
  3668. pll->config = shared_dpll[i];
  3669. }
  3670. }
  3671. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3672. {
  3673. struct drm_i915_private *dev_priv = dev->dev_private;
  3674. int dslreg = PIPEDSL(pipe);
  3675. u32 temp;
  3676. temp = I915_READ(dslreg);
  3677. udelay(500);
  3678. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3679. if (wait_for(I915_READ(dslreg) != temp, 5))
  3680. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3681. }
  3682. }
  3683. static int
  3684. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3685. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3686. int src_w, int src_h, int dst_w, int dst_h)
  3687. {
  3688. struct intel_crtc_scaler_state *scaler_state =
  3689. &crtc_state->scaler_state;
  3690. struct intel_crtc *intel_crtc =
  3691. to_intel_crtc(crtc_state->base.crtc);
  3692. int need_scaling;
  3693. need_scaling = intel_rotation_90_or_270(rotation) ?
  3694. (src_h != dst_w || src_w != dst_h):
  3695. (src_w != dst_w || src_h != dst_h);
  3696. /*
  3697. * if plane is being disabled or scaler is no more required or force detach
  3698. * - free scaler binded to this plane/crtc
  3699. * - in order to do this, update crtc->scaler_usage
  3700. *
  3701. * Here scaler state in crtc_state is set free so that
  3702. * scaler can be assigned to other user. Actual register
  3703. * update to free the scaler is done in plane/panel-fit programming.
  3704. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3705. */
  3706. if (force_detach || !need_scaling) {
  3707. if (*scaler_id >= 0) {
  3708. scaler_state->scaler_users &= ~(1 << scaler_user);
  3709. scaler_state->scalers[*scaler_id].in_use = 0;
  3710. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3711. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3712. intel_crtc->pipe, scaler_user, *scaler_id,
  3713. scaler_state->scaler_users);
  3714. *scaler_id = -1;
  3715. }
  3716. return 0;
  3717. }
  3718. /* range checks */
  3719. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3720. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3721. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3722. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3723. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3724. "size is out of scaler range\n",
  3725. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3726. return -EINVAL;
  3727. }
  3728. /* mark this plane as a scaler user in crtc_state */
  3729. scaler_state->scaler_users |= (1 << scaler_user);
  3730. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3731. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3732. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3733. scaler_state->scaler_users);
  3734. return 0;
  3735. }
  3736. /**
  3737. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3738. *
  3739. * @state: crtc's scaler state
  3740. *
  3741. * Return
  3742. * 0 - scaler_usage updated successfully
  3743. * error - requested scaling cannot be supported or other error condition
  3744. */
  3745. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3746. {
  3747. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3748. struct drm_display_mode *adjusted_mode =
  3749. &state->base.adjusted_mode;
  3750. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3751. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3752. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3753. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3754. state->pipe_src_w, state->pipe_src_h,
  3755. adjusted_mode->hdisplay, adjusted_mode->vdisplay);
  3756. }
  3757. /**
  3758. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3759. *
  3760. * @state: crtc's scaler state
  3761. * @plane_state: atomic plane state to update
  3762. *
  3763. * Return
  3764. * 0 - scaler_usage updated successfully
  3765. * error - requested scaling cannot be supported or other error condition
  3766. */
  3767. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3768. struct intel_plane_state *plane_state)
  3769. {
  3770. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3771. struct intel_plane *intel_plane =
  3772. to_intel_plane(plane_state->base.plane);
  3773. struct drm_framebuffer *fb = plane_state->base.fb;
  3774. int ret;
  3775. bool force_detach = !fb || !plane_state->visible;
  3776. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3777. intel_plane->base.base.id, intel_crtc->pipe,
  3778. drm_plane_index(&intel_plane->base));
  3779. ret = skl_update_scaler(crtc_state, force_detach,
  3780. drm_plane_index(&intel_plane->base),
  3781. &plane_state->scaler_id,
  3782. plane_state->base.rotation,
  3783. drm_rect_width(&plane_state->src) >> 16,
  3784. drm_rect_height(&plane_state->src) >> 16,
  3785. drm_rect_width(&plane_state->dst),
  3786. drm_rect_height(&plane_state->dst));
  3787. if (ret || plane_state->scaler_id < 0)
  3788. return ret;
  3789. /* check colorkey */
  3790. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3791. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3792. intel_plane->base.base.id);
  3793. return -EINVAL;
  3794. }
  3795. /* Check src format */
  3796. switch (fb->pixel_format) {
  3797. case DRM_FORMAT_RGB565:
  3798. case DRM_FORMAT_XBGR8888:
  3799. case DRM_FORMAT_XRGB8888:
  3800. case DRM_FORMAT_ABGR8888:
  3801. case DRM_FORMAT_ARGB8888:
  3802. case DRM_FORMAT_XRGB2101010:
  3803. case DRM_FORMAT_XBGR2101010:
  3804. case DRM_FORMAT_YUYV:
  3805. case DRM_FORMAT_YVYU:
  3806. case DRM_FORMAT_UYVY:
  3807. case DRM_FORMAT_VYUY:
  3808. break;
  3809. default:
  3810. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3811. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3812. return -EINVAL;
  3813. }
  3814. return 0;
  3815. }
  3816. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3817. {
  3818. int i;
  3819. for (i = 0; i < crtc->num_scalers; i++)
  3820. skl_detach_scaler(crtc, i);
  3821. }
  3822. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3823. {
  3824. struct drm_device *dev = crtc->base.dev;
  3825. struct drm_i915_private *dev_priv = dev->dev_private;
  3826. int pipe = crtc->pipe;
  3827. struct intel_crtc_scaler_state *scaler_state =
  3828. &crtc->config->scaler_state;
  3829. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3830. if (crtc->config->pch_pfit.enabled) {
  3831. int id;
  3832. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3833. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3834. return;
  3835. }
  3836. id = scaler_state->scaler_id;
  3837. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3838. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3839. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3840. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3841. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3842. }
  3843. }
  3844. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3845. {
  3846. struct drm_device *dev = crtc->base.dev;
  3847. struct drm_i915_private *dev_priv = dev->dev_private;
  3848. int pipe = crtc->pipe;
  3849. if (crtc->config->pch_pfit.enabled) {
  3850. /* Force use of hard-coded filter coefficients
  3851. * as some pre-programmed values are broken,
  3852. * e.g. x201.
  3853. */
  3854. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3855. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3856. PF_PIPE_SEL_IVB(pipe));
  3857. else
  3858. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3859. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3860. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3861. }
  3862. }
  3863. void hsw_enable_ips(struct intel_crtc *crtc)
  3864. {
  3865. struct drm_device *dev = crtc->base.dev;
  3866. struct drm_i915_private *dev_priv = dev->dev_private;
  3867. if (!crtc->config->ips_enabled)
  3868. return;
  3869. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3870. intel_wait_for_vblank(dev, crtc->pipe);
  3871. assert_plane_enabled(dev_priv, crtc->plane);
  3872. if (IS_BROADWELL(dev)) {
  3873. mutex_lock(&dev_priv->rps.hw_lock);
  3874. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3875. mutex_unlock(&dev_priv->rps.hw_lock);
  3876. /* Quoting Art Runyan: "its not safe to expect any particular
  3877. * value in IPS_CTL bit 31 after enabling IPS through the
  3878. * mailbox." Moreover, the mailbox may return a bogus state,
  3879. * so we need to just enable it and continue on.
  3880. */
  3881. } else {
  3882. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3883. /* The bit only becomes 1 in the next vblank, so this wait here
  3884. * is essentially intel_wait_for_vblank. If we don't have this
  3885. * and don't wait for vblanks until the end of crtc_enable, then
  3886. * the HW state readout code will complain that the expected
  3887. * IPS_CTL value is not the one we read. */
  3888. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3889. DRM_ERROR("Timed out waiting for IPS enable\n");
  3890. }
  3891. }
  3892. void hsw_disable_ips(struct intel_crtc *crtc)
  3893. {
  3894. struct drm_device *dev = crtc->base.dev;
  3895. struct drm_i915_private *dev_priv = dev->dev_private;
  3896. if (!crtc->config->ips_enabled)
  3897. return;
  3898. assert_plane_enabled(dev_priv, crtc->plane);
  3899. if (IS_BROADWELL(dev)) {
  3900. mutex_lock(&dev_priv->rps.hw_lock);
  3901. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3902. mutex_unlock(&dev_priv->rps.hw_lock);
  3903. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3904. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3905. DRM_ERROR("Timed out waiting for IPS disable\n");
  3906. } else {
  3907. I915_WRITE(IPS_CTL, 0);
  3908. POSTING_READ(IPS_CTL);
  3909. }
  3910. /* We need to wait for a vblank before we can disable the plane. */
  3911. intel_wait_for_vblank(dev, crtc->pipe);
  3912. }
  3913. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3914. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3915. {
  3916. struct drm_device *dev = crtc->dev;
  3917. struct drm_i915_private *dev_priv = dev->dev_private;
  3918. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3919. enum pipe pipe = intel_crtc->pipe;
  3920. int palreg = PALETTE(pipe);
  3921. int i;
  3922. bool reenable_ips = false;
  3923. /* The clocks have to be on to load the palette. */
  3924. if (!crtc->state->active)
  3925. return;
  3926. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3927. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3928. assert_dsi_pll_enabled(dev_priv);
  3929. else
  3930. assert_pll_enabled(dev_priv, pipe);
  3931. }
  3932. /* use legacy palette for Ironlake */
  3933. if (!HAS_GMCH_DISPLAY(dev))
  3934. palreg = LGC_PALETTE(pipe);
  3935. /* Workaround : Do not read or write the pipe palette/gamma data while
  3936. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3937. */
  3938. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3939. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3940. GAMMA_MODE_MODE_SPLIT)) {
  3941. hsw_disable_ips(intel_crtc);
  3942. reenable_ips = true;
  3943. }
  3944. for (i = 0; i < 256; i++) {
  3945. I915_WRITE(palreg + 4 * i,
  3946. (intel_crtc->lut_r[i] << 16) |
  3947. (intel_crtc->lut_g[i] << 8) |
  3948. intel_crtc->lut_b[i]);
  3949. }
  3950. if (reenable_ips)
  3951. hsw_enable_ips(intel_crtc);
  3952. }
  3953. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3954. {
  3955. if (intel_crtc->overlay) {
  3956. struct drm_device *dev = intel_crtc->base.dev;
  3957. struct drm_i915_private *dev_priv = dev->dev_private;
  3958. mutex_lock(&dev->struct_mutex);
  3959. dev_priv->mm.interruptible = false;
  3960. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3961. dev_priv->mm.interruptible = true;
  3962. mutex_unlock(&dev->struct_mutex);
  3963. }
  3964. /* Let userspace switch the overlay on again. In most cases userspace
  3965. * has to recompute where to put it anyway.
  3966. */
  3967. }
  3968. /**
  3969. * intel_post_enable_primary - Perform operations after enabling primary plane
  3970. * @crtc: the CRTC whose primary plane was just enabled
  3971. *
  3972. * Performs potentially sleeping operations that must be done after the primary
  3973. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3974. * called due to an explicit primary plane update, or due to an implicit
  3975. * re-enable that is caused when a sprite plane is updated to no longer
  3976. * completely hide the primary plane.
  3977. */
  3978. static void
  3979. intel_post_enable_primary(struct drm_crtc *crtc)
  3980. {
  3981. struct drm_device *dev = crtc->dev;
  3982. struct drm_i915_private *dev_priv = dev->dev_private;
  3983. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3984. int pipe = intel_crtc->pipe;
  3985. /*
  3986. * BDW signals flip done immediately if the plane
  3987. * is disabled, even if the plane enable is already
  3988. * armed to occur at the next vblank :(
  3989. */
  3990. if (IS_BROADWELL(dev))
  3991. intel_wait_for_vblank(dev, pipe);
  3992. /*
  3993. * FIXME IPS should be fine as long as one plane is
  3994. * enabled, but in practice it seems to have problems
  3995. * when going from primary only to sprite only and vice
  3996. * versa.
  3997. */
  3998. hsw_enable_ips(intel_crtc);
  3999. /*
  4000. * Gen2 reports pipe underruns whenever all planes are disabled.
  4001. * So don't enable underrun reporting before at least some planes
  4002. * are enabled.
  4003. * FIXME: Need to fix the logic to work when we turn off all planes
  4004. * but leave the pipe running.
  4005. */
  4006. if (IS_GEN2(dev))
  4007. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4008. /* Underruns don't raise interrupts, so check manually. */
  4009. if (HAS_GMCH_DISPLAY(dev))
  4010. i9xx_check_fifo_underruns(dev_priv);
  4011. }
  4012. /**
  4013. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4014. * @crtc: the CRTC whose primary plane is to be disabled
  4015. *
  4016. * Performs potentially sleeping operations that must be done before the
  4017. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4018. * be called due to an explicit primary plane update, or due to an implicit
  4019. * disable that is caused when a sprite plane completely hides the primary
  4020. * plane.
  4021. */
  4022. static void
  4023. intel_pre_disable_primary(struct drm_crtc *crtc)
  4024. {
  4025. struct drm_device *dev = crtc->dev;
  4026. struct drm_i915_private *dev_priv = dev->dev_private;
  4027. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4028. int pipe = intel_crtc->pipe;
  4029. /*
  4030. * Gen2 reports pipe underruns whenever all planes are disabled.
  4031. * So diasble underrun reporting before all the planes get disabled.
  4032. * FIXME: Need to fix the logic to work when we turn off all planes
  4033. * but leave the pipe running.
  4034. */
  4035. if (IS_GEN2(dev))
  4036. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4037. /*
  4038. * Vblank time updates from the shadow to live plane control register
  4039. * are blocked if the memory self-refresh mode is active at that
  4040. * moment. So to make sure the plane gets truly disabled, disable
  4041. * first the self-refresh mode. The self-refresh enable bit in turn
  4042. * will be checked/applied by the HW only at the next frame start
  4043. * event which is after the vblank start event, so we need to have a
  4044. * wait-for-vblank between disabling the plane and the pipe.
  4045. */
  4046. if (HAS_GMCH_DISPLAY(dev)) {
  4047. intel_set_memory_cxsr(dev_priv, false);
  4048. dev_priv->wm.vlv.cxsr = false;
  4049. intel_wait_for_vblank(dev, pipe);
  4050. }
  4051. /*
  4052. * FIXME IPS should be fine as long as one plane is
  4053. * enabled, but in practice it seems to have problems
  4054. * when going from primary only to sprite only and vice
  4055. * versa.
  4056. */
  4057. hsw_disable_ips(intel_crtc);
  4058. }
  4059. static void intel_post_plane_update(struct intel_crtc *crtc)
  4060. {
  4061. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4062. struct drm_device *dev = crtc->base.dev;
  4063. struct drm_i915_private *dev_priv = dev->dev_private;
  4064. struct drm_plane *plane;
  4065. if (atomic->wait_vblank)
  4066. intel_wait_for_vblank(dev, crtc->pipe);
  4067. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4068. if (atomic->disable_cxsr)
  4069. crtc->wm.cxsr_allowed = true;
  4070. if (crtc->atomic.update_wm_post)
  4071. intel_update_watermarks(&crtc->base);
  4072. if (atomic->update_fbc)
  4073. intel_fbc_update(dev_priv);
  4074. if (atomic->post_enable_primary)
  4075. intel_post_enable_primary(&crtc->base);
  4076. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4077. intel_update_sprite_watermarks(plane, &crtc->base,
  4078. 0, 0, 0, false, false);
  4079. memset(atomic, 0, sizeof(*atomic));
  4080. }
  4081. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4082. {
  4083. struct drm_device *dev = crtc->base.dev;
  4084. struct drm_i915_private *dev_priv = dev->dev_private;
  4085. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4086. struct drm_plane *p;
  4087. /* Track fb's for any planes being disabled */
  4088. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4089. struct intel_plane *plane = to_intel_plane(p);
  4090. mutex_lock(&dev->struct_mutex);
  4091. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
  4092. plane->frontbuffer_bit);
  4093. mutex_unlock(&dev->struct_mutex);
  4094. }
  4095. if (atomic->wait_for_flips)
  4096. intel_crtc_wait_for_pending_flips(&crtc->base);
  4097. if (atomic->disable_fbc)
  4098. intel_fbc_disable_crtc(crtc);
  4099. if (crtc->atomic.disable_ips)
  4100. hsw_disable_ips(crtc);
  4101. if (atomic->pre_disable_primary)
  4102. intel_pre_disable_primary(&crtc->base);
  4103. if (atomic->disable_cxsr) {
  4104. crtc->wm.cxsr_allowed = false;
  4105. intel_set_memory_cxsr(dev_priv, false);
  4106. }
  4107. }
  4108. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4109. {
  4110. struct drm_device *dev = crtc->dev;
  4111. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4112. struct drm_plane *p;
  4113. int pipe = intel_crtc->pipe;
  4114. intel_crtc_dpms_overlay_disable(intel_crtc);
  4115. drm_for_each_plane_mask(p, dev, plane_mask)
  4116. to_intel_plane(p)->disable_plane(p, crtc);
  4117. /*
  4118. * FIXME: Once we grow proper nuclear flip support out of this we need
  4119. * to compute the mask of flip planes precisely. For the time being
  4120. * consider this a flip to a NULL plane.
  4121. */
  4122. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4123. }
  4124. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4125. {
  4126. struct drm_device *dev = crtc->dev;
  4127. struct drm_i915_private *dev_priv = dev->dev_private;
  4128. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4129. struct intel_encoder *encoder;
  4130. int pipe = intel_crtc->pipe;
  4131. if (WARN_ON(intel_crtc->active))
  4132. return;
  4133. if (intel_crtc->config->has_pch_encoder)
  4134. intel_prepare_shared_dpll(intel_crtc);
  4135. if (intel_crtc->config->has_dp_encoder)
  4136. intel_dp_set_m_n(intel_crtc, M1_N1);
  4137. intel_set_pipe_timings(intel_crtc);
  4138. if (intel_crtc->config->has_pch_encoder) {
  4139. intel_cpu_transcoder_set_m_n(intel_crtc,
  4140. &intel_crtc->config->fdi_m_n, NULL);
  4141. }
  4142. ironlake_set_pipeconf(crtc);
  4143. intel_crtc->active = true;
  4144. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4145. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4146. for_each_encoder_on_crtc(dev, crtc, encoder)
  4147. if (encoder->pre_enable)
  4148. encoder->pre_enable(encoder);
  4149. if (intel_crtc->config->has_pch_encoder) {
  4150. /* Note: FDI PLL enabling _must_ be done before we enable the
  4151. * cpu pipes, hence this is separate from all the other fdi/pch
  4152. * enabling. */
  4153. ironlake_fdi_pll_enable(intel_crtc);
  4154. } else {
  4155. assert_fdi_tx_disabled(dev_priv, pipe);
  4156. assert_fdi_rx_disabled(dev_priv, pipe);
  4157. }
  4158. ironlake_pfit_enable(intel_crtc);
  4159. /*
  4160. * On ILK+ LUT must be loaded before the pipe is running but with
  4161. * clocks enabled
  4162. */
  4163. intel_crtc_load_lut(crtc);
  4164. intel_update_watermarks(crtc);
  4165. intel_enable_pipe(intel_crtc);
  4166. if (intel_crtc->config->has_pch_encoder)
  4167. ironlake_pch_enable(crtc);
  4168. assert_vblank_disabled(crtc);
  4169. drm_crtc_vblank_on(crtc);
  4170. for_each_encoder_on_crtc(dev, crtc, encoder)
  4171. encoder->enable(encoder);
  4172. if (HAS_PCH_CPT(dev))
  4173. cpt_verify_modeset(dev, intel_crtc->pipe);
  4174. }
  4175. /* IPS only exists on ULT machines and is tied to pipe A. */
  4176. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4177. {
  4178. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4179. }
  4180. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4181. {
  4182. struct drm_device *dev = crtc->dev;
  4183. struct drm_i915_private *dev_priv = dev->dev_private;
  4184. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4185. struct intel_encoder *encoder;
  4186. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4187. struct intel_crtc_state *pipe_config =
  4188. to_intel_crtc_state(crtc->state);
  4189. if (WARN_ON(intel_crtc->active))
  4190. return;
  4191. if (intel_crtc_to_shared_dpll(intel_crtc))
  4192. intel_enable_shared_dpll(intel_crtc);
  4193. if (intel_crtc->config->has_dp_encoder)
  4194. intel_dp_set_m_n(intel_crtc, M1_N1);
  4195. intel_set_pipe_timings(intel_crtc);
  4196. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4197. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4198. intel_crtc->config->pixel_multiplier - 1);
  4199. }
  4200. if (intel_crtc->config->has_pch_encoder) {
  4201. intel_cpu_transcoder_set_m_n(intel_crtc,
  4202. &intel_crtc->config->fdi_m_n, NULL);
  4203. }
  4204. haswell_set_pipeconf(crtc);
  4205. intel_set_pipe_csc(crtc);
  4206. intel_crtc->active = true;
  4207. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4208. for_each_encoder_on_crtc(dev, crtc, encoder)
  4209. if (encoder->pre_enable)
  4210. encoder->pre_enable(encoder);
  4211. if (intel_crtc->config->has_pch_encoder) {
  4212. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4213. true);
  4214. dev_priv->display.fdi_link_train(crtc);
  4215. }
  4216. intel_ddi_enable_pipe_clock(intel_crtc);
  4217. if (INTEL_INFO(dev)->gen == 9)
  4218. skylake_pfit_enable(intel_crtc);
  4219. else if (INTEL_INFO(dev)->gen < 9)
  4220. ironlake_pfit_enable(intel_crtc);
  4221. else
  4222. MISSING_CASE(INTEL_INFO(dev)->gen);
  4223. /*
  4224. * On ILK+ LUT must be loaded before the pipe is running but with
  4225. * clocks enabled
  4226. */
  4227. intel_crtc_load_lut(crtc);
  4228. intel_ddi_set_pipe_settings(crtc);
  4229. intel_ddi_enable_transcoder_func(crtc);
  4230. intel_update_watermarks(crtc);
  4231. intel_enable_pipe(intel_crtc);
  4232. if (intel_crtc->config->has_pch_encoder)
  4233. lpt_pch_enable(crtc);
  4234. if (intel_crtc->config->dp_encoder_is_mst)
  4235. intel_ddi_set_vc_payload_alloc(crtc, true);
  4236. assert_vblank_disabled(crtc);
  4237. drm_crtc_vblank_on(crtc);
  4238. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4239. encoder->enable(encoder);
  4240. intel_opregion_notify_encoder(encoder, true);
  4241. }
  4242. /* If we change the relative order between pipe/planes enabling, we need
  4243. * to change the workaround. */
  4244. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4245. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4246. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4247. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4248. }
  4249. }
  4250. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4251. {
  4252. struct drm_device *dev = crtc->base.dev;
  4253. struct drm_i915_private *dev_priv = dev->dev_private;
  4254. int pipe = crtc->pipe;
  4255. /* To avoid upsetting the power well on haswell only disable the pfit if
  4256. * it's in use. The hw state code will make sure we get this right. */
  4257. if (crtc->config->pch_pfit.enabled) {
  4258. I915_WRITE(PF_CTL(pipe), 0);
  4259. I915_WRITE(PF_WIN_POS(pipe), 0);
  4260. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4261. }
  4262. }
  4263. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4264. {
  4265. struct drm_device *dev = crtc->dev;
  4266. struct drm_i915_private *dev_priv = dev->dev_private;
  4267. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4268. struct intel_encoder *encoder;
  4269. int pipe = intel_crtc->pipe;
  4270. u32 reg, temp;
  4271. for_each_encoder_on_crtc(dev, crtc, encoder)
  4272. encoder->disable(encoder);
  4273. drm_crtc_vblank_off(crtc);
  4274. assert_vblank_disabled(crtc);
  4275. if (intel_crtc->config->has_pch_encoder)
  4276. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4277. intel_disable_pipe(intel_crtc);
  4278. ironlake_pfit_disable(intel_crtc);
  4279. if (intel_crtc->config->has_pch_encoder)
  4280. ironlake_fdi_disable(crtc);
  4281. for_each_encoder_on_crtc(dev, crtc, encoder)
  4282. if (encoder->post_disable)
  4283. encoder->post_disable(encoder);
  4284. if (intel_crtc->config->has_pch_encoder) {
  4285. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4286. if (HAS_PCH_CPT(dev)) {
  4287. /* disable TRANS_DP_CTL */
  4288. reg = TRANS_DP_CTL(pipe);
  4289. temp = I915_READ(reg);
  4290. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4291. TRANS_DP_PORT_SEL_MASK);
  4292. temp |= TRANS_DP_PORT_SEL_NONE;
  4293. I915_WRITE(reg, temp);
  4294. /* disable DPLL_SEL */
  4295. temp = I915_READ(PCH_DPLL_SEL);
  4296. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4297. I915_WRITE(PCH_DPLL_SEL, temp);
  4298. }
  4299. ironlake_fdi_pll_disable(intel_crtc);
  4300. }
  4301. intel_crtc->active = false;
  4302. intel_update_watermarks(crtc);
  4303. }
  4304. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4305. {
  4306. struct drm_device *dev = crtc->dev;
  4307. struct drm_i915_private *dev_priv = dev->dev_private;
  4308. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4309. struct intel_encoder *encoder;
  4310. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4311. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4312. intel_opregion_notify_encoder(encoder, false);
  4313. encoder->disable(encoder);
  4314. }
  4315. drm_crtc_vblank_off(crtc);
  4316. assert_vblank_disabled(crtc);
  4317. if (intel_crtc->config->has_pch_encoder)
  4318. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4319. false);
  4320. intel_disable_pipe(intel_crtc);
  4321. if (intel_crtc->config->dp_encoder_is_mst)
  4322. intel_ddi_set_vc_payload_alloc(crtc, false);
  4323. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4324. if (INTEL_INFO(dev)->gen == 9)
  4325. skylake_scaler_disable(intel_crtc);
  4326. else if (INTEL_INFO(dev)->gen < 9)
  4327. ironlake_pfit_disable(intel_crtc);
  4328. else
  4329. MISSING_CASE(INTEL_INFO(dev)->gen);
  4330. intel_ddi_disable_pipe_clock(intel_crtc);
  4331. if (intel_crtc->config->has_pch_encoder) {
  4332. lpt_disable_pch_transcoder(dev_priv);
  4333. intel_ddi_fdi_disable(crtc);
  4334. }
  4335. for_each_encoder_on_crtc(dev, crtc, encoder)
  4336. if (encoder->post_disable)
  4337. encoder->post_disable(encoder);
  4338. intel_crtc->active = false;
  4339. intel_update_watermarks(crtc);
  4340. }
  4341. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4342. {
  4343. struct drm_device *dev = crtc->base.dev;
  4344. struct drm_i915_private *dev_priv = dev->dev_private;
  4345. struct intel_crtc_state *pipe_config = crtc->config;
  4346. if (!pipe_config->gmch_pfit.control)
  4347. return;
  4348. /*
  4349. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4350. * according to register description and PRM.
  4351. */
  4352. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4353. assert_pipe_disabled(dev_priv, crtc->pipe);
  4354. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4355. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4356. /* Border color in case we don't scale up to the full screen. Black by
  4357. * default, change to something else for debugging. */
  4358. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4359. }
  4360. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4361. {
  4362. switch (port) {
  4363. case PORT_A:
  4364. case PORT_E:
  4365. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4366. case PORT_B:
  4367. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4368. case PORT_C:
  4369. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4370. case PORT_D:
  4371. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4372. default:
  4373. WARN_ON_ONCE(1);
  4374. return POWER_DOMAIN_PORT_OTHER;
  4375. }
  4376. }
  4377. #define for_each_power_domain(domain, mask) \
  4378. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4379. if ((1 << (domain)) & (mask))
  4380. enum intel_display_power_domain
  4381. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4382. {
  4383. struct drm_device *dev = intel_encoder->base.dev;
  4384. struct intel_digital_port *intel_dig_port;
  4385. switch (intel_encoder->type) {
  4386. case INTEL_OUTPUT_UNKNOWN:
  4387. /* Only DDI platforms should ever use this output type */
  4388. WARN_ON_ONCE(!HAS_DDI(dev));
  4389. case INTEL_OUTPUT_DISPLAYPORT:
  4390. case INTEL_OUTPUT_HDMI:
  4391. case INTEL_OUTPUT_EDP:
  4392. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4393. return port_to_power_domain(intel_dig_port->port);
  4394. case INTEL_OUTPUT_DP_MST:
  4395. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4396. return port_to_power_domain(intel_dig_port->port);
  4397. case INTEL_OUTPUT_ANALOG:
  4398. return POWER_DOMAIN_PORT_CRT;
  4399. case INTEL_OUTPUT_DSI:
  4400. return POWER_DOMAIN_PORT_DSI;
  4401. default:
  4402. return POWER_DOMAIN_PORT_OTHER;
  4403. }
  4404. }
  4405. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4406. {
  4407. struct drm_device *dev = crtc->dev;
  4408. struct intel_encoder *intel_encoder;
  4409. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4410. enum pipe pipe = intel_crtc->pipe;
  4411. unsigned long mask;
  4412. enum transcoder transcoder;
  4413. if (!crtc->state->active)
  4414. return 0;
  4415. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4416. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4417. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4418. if (intel_crtc->config->pch_pfit.enabled ||
  4419. intel_crtc->config->pch_pfit.force_thru)
  4420. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4421. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4422. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4423. return mask;
  4424. }
  4425. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4426. {
  4427. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4428. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4429. enum intel_display_power_domain domain;
  4430. unsigned long domains, new_domains, old_domains;
  4431. old_domains = intel_crtc->enabled_power_domains;
  4432. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4433. domains = new_domains & ~old_domains;
  4434. for_each_power_domain(domain, domains)
  4435. intel_display_power_get(dev_priv, domain);
  4436. return old_domains & ~new_domains;
  4437. }
  4438. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4439. unsigned long domains)
  4440. {
  4441. enum intel_display_power_domain domain;
  4442. for_each_power_domain(domain, domains)
  4443. intel_display_power_put(dev_priv, domain);
  4444. }
  4445. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4446. {
  4447. struct drm_device *dev = state->dev;
  4448. struct drm_i915_private *dev_priv = dev->dev_private;
  4449. unsigned long put_domains[I915_MAX_PIPES] = {};
  4450. struct drm_crtc_state *crtc_state;
  4451. struct drm_crtc *crtc;
  4452. int i;
  4453. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4454. if (needs_modeset(crtc->state))
  4455. put_domains[to_intel_crtc(crtc)->pipe] =
  4456. modeset_get_crtc_power_domains(crtc);
  4457. }
  4458. if (dev_priv->display.modeset_commit_cdclk) {
  4459. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4460. if (cdclk != dev_priv->cdclk_freq &&
  4461. !WARN_ON(!state->allow_modeset))
  4462. dev_priv->display.modeset_commit_cdclk(state);
  4463. }
  4464. for (i = 0; i < I915_MAX_PIPES; i++)
  4465. if (put_domains[i])
  4466. modeset_put_power_domains(dev_priv, put_domains[i]);
  4467. }
  4468. static void intel_update_max_cdclk(struct drm_device *dev)
  4469. {
  4470. struct drm_i915_private *dev_priv = dev->dev_private;
  4471. if (IS_SKYLAKE(dev)) {
  4472. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4473. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4474. dev_priv->max_cdclk_freq = 675000;
  4475. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4476. dev_priv->max_cdclk_freq = 540000;
  4477. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4478. dev_priv->max_cdclk_freq = 450000;
  4479. else
  4480. dev_priv->max_cdclk_freq = 337500;
  4481. } else if (IS_BROADWELL(dev)) {
  4482. /*
  4483. * FIXME with extra cooling we can allow
  4484. * 540 MHz for ULX and 675 Mhz for ULT.
  4485. * How can we know if extra cooling is
  4486. * available? PCI ID, VTB, something else?
  4487. */
  4488. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4489. dev_priv->max_cdclk_freq = 450000;
  4490. else if (IS_BDW_ULX(dev))
  4491. dev_priv->max_cdclk_freq = 450000;
  4492. else if (IS_BDW_ULT(dev))
  4493. dev_priv->max_cdclk_freq = 540000;
  4494. else
  4495. dev_priv->max_cdclk_freq = 675000;
  4496. } else if (IS_CHERRYVIEW(dev)) {
  4497. dev_priv->max_cdclk_freq = 320000;
  4498. } else if (IS_VALLEYVIEW(dev)) {
  4499. dev_priv->max_cdclk_freq = 400000;
  4500. } else {
  4501. /* otherwise assume cdclk is fixed */
  4502. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4503. }
  4504. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4505. dev_priv->max_cdclk_freq);
  4506. }
  4507. static void intel_update_cdclk(struct drm_device *dev)
  4508. {
  4509. struct drm_i915_private *dev_priv = dev->dev_private;
  4510. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4511. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4512. dev_priv->cdclk_freq);
  4513. /*
  4514. * Program the gmbus_freq based on the cdclk frequency.
  4515. * BSpec erroneously claims we should aim for 4MHz, but
  4516. * in fact 1MHz is the correct frequency.
  4517. */
  4518. if (IS_VALLEYVIEW(dev)) {
  4519. /*
  4520. * Program the gmbus_freq based on the cdclk frequency.
  4521. * BSpec erroneously claims we should aim for 4MHz, but
  4522. * in fact 1MHz is the correct frequency.
  4523. */
  4524. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4525. }
  4526. if (dev_priv->max_cdclk_freq == 0)
  4527. intel_update_max_cdclk(dev);
  4528. }
  4529. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4530. {
  4531. struct drm_i915_private *dev_priv = dev->dev_private;
  4532. uint32_t divider;
  4533. uint32_t ratio;
  4534. uint32_t current_freq;
  4535. int ret;
  4536. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4537. switch (frequency) {
  4538. case 144000:
  4539. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4540. ratio = BXT_DE_PLL_RATIO(60);
  4541. break;
  4542. case 288000:
  4543. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4544. ratio = BXT_DE_PLL_RATIO(60);
  4545. break;
  4546. case 384000:
  4547. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4548. ratio = BXT_DE_PLL_RATIO(60);
  4549. break;
  4550. case 576000:
  4551. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4552. ratio = BXT_DE_PLL_RATIO(60);
  4553. break;
  4554. case 624000:
  4555. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4556. ratio = BXT_DE_PLL_RATIO(65);
  4557. break;
  4558. case 19200:
  4559. /*
  4560. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4561. * to suppress GCC warning.
  4562. */
  4563. ratio = 0;
  4564. divider = 0;
  4565. break;
  4566. default:
  4567. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4568. return;
  4569. }
  4570. mutex_lock(&dev_priv->rps.hw_lock);
  4571. /* Inform power controller of upcoming frequency change */
  4572. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4573. 0x80000000);
  4574. mutex_unlock(&dev_priv->rps.hw_lock);
  4575. if (ret) {
  4576. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4577. ret, frequency);
  4578. return;
  4579. }
  4580. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4581. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4582. current_freq = current_freq * 500 + 1000;
  4583. /*
  4584. * DE PLL has to be disabled when
  4585. * - setting to 19.2MHz (bypass, PLL isn't used)
  4586. * - before setting to 624MHz (PLL needs toggling)
  4587. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4588. */
  4589. if (frequency == 19200 || frequency == 624000 ||
  4590. current_freq == 624000) {
  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),
  4594. 1))
  4595. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4596. }
  4597. if (frequency != 19200) {
  4598. uint32_t val;
  4599. val = I915_READ(BXT_DE_PLL_CTL);
  4600. val &= ~BXT_DE_PLL_RATIO_MASK;
  4601. val |= ratio;
  4602. I915_WRITE(BXT_DE_PLL_CTL, val);
  4603. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4604. /* Timeout 200us */
  4605. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4606. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4607. val = I915_READ(CDCLK_CTL);
  4608. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4609. val |= divider;
  4610. /*
  4611. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4612. * enable otherwise.
  4613. */
  4614. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4615. if (frequency >= 500000)
  4616. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4617. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4618. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4619. val |= (frequency - 1000) / 500;
  4620. I915_WRITE(CDCLK_CTL, val);
  4621. }
  4622. mutex_lock(&dev_priv->rps.hw_lock);
  4623. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4624. DIV_ROUND_UP(frequency, 25000));
  4625. mutex_unlock(&dev_priv->rps.hw_lock);
  4626. if (ret) {
  4627. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4628. ret, frequency);
  4629. return;
  4630. }
  4631. intel_update_cdclk(dev);
  4632. }
  4633. void broxton_init_cdclk(struct drm_device *dev)
  4634. {
  4635. struct drm_i915_private *dev_priv = dev->dev_private;
  4636. uint32_t val;
  4637. /*
  4638. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4639. * or else the reset will hang because there is no PCH to respond.
  4640. * Move the handshake programming to initialization sequence.
  4641. * Previously was left up to BIOS.
  4642. */
  4643. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4644. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4645. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4646. /* Enable PG1 for cdclk */
  4647. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4648. /* check if cd clock is enabled */
  4649. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4650. DRM_DEBUG_KMS("Display already initialized\n");
  4651. return;
  4652. }
  4653. /*
  4654. * FIXME:
  4655. * - The initial CDCLK needs to be read from VBT.
  4656. * Need to make this change after VBT has changes for BXT.
  4657. * - check if setting the max (or any) cdclk freq is really necessary
  4658. * here, it belongs to modeset time
  4659. */
  4660. broxton_set_cdclk(dev, 624000);
  4661. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4662. POSTING_READ(DBUF_CTL);
  4663. udelay(10);
  4664. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4665. DRM_ERROR("DBuf power enable timeout!\n");
  4666. }
  4667. void broxton_uninit_cdclk(struct drm_device *dev)
  4668. {
  4669. struct drm_i915_private *dev_priv = dev->dev_private;
  4670. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4671. POSTING_READ(DBUF_CTL);
  4672. udelay(10);
  4673. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4674. DRM_ERROR("DBuf power disable timeout!\n");
  4675. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4676. broxton_set_cdclk(dev, 19200);
  4677. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4678. }
  4679. static const struct skl_cdclk_entry {
  4680. unsigned int freq;
  4681. unsigned int vco;
  4682. } skl_cdclk_frequencies[] = {
  4683. { .freq = 308570, .vco = 8640 },
  4684. { .freq = 337500, .vco = 8100 },
  4685. { .freq = 432000, .vco = 8640 },
  4686. { .freq = 450000, .vco = 8100 },
  4687. { .freq = 540000, .vco = 8100 },
  4688. { .freq = 617140, .vco = 8640 },
  4689. { .freq = 675000, .vco = 8100 },
  4690. };
  4691. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4692. {
  4693. return (freq - 1000) / 500;
  4694. }
  4695. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4696. {
  4697. unsigned int i;
  4698. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4699. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4700. if (e->freq == freq)
  4701. return e->vco;
  4702. }
  4703. return 8100;
  4704. }
  4705. static void
  4706. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4707. {
  4708. unsigned int min_freq;
  4709. u32 val;
  4710. /* select the minimum CDCLK before enabling DPLL 0 */
  4711. val = I915_READ(CDCLK_CTL);
  4712. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4713. val |= CDCLK_FREQ_337_308;
  4714. if (required_vco == 8640)
  4715. min_freq = 308570;
  4716. else
  4717. min_freq = 337500;
  4718. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4719. I915_WRITE(CDCLK_CTL, val);
  4720. POSTING_READ(CDCLK_CTL);
  4721. /*
  4722. * We always enable DPLL0 with the lowest link rate possible, but still
  4723. * taking into account the VCO required to operate the eDP panel at the
  4724. * desired frequency. The usual DP link rates operate with a VCO of
  4725. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4726. * The modeset code is responsible for the selection of the exact link
  4727. * rate later on, with the constraint of choosing a frequency that
  4728. * works with required_vco.
  4729. */
  4730. val = I915_READ(DPLL_CTRL1);
  4731. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4732. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4733. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4734. if (required_vco == 8640)
  4735. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4736. SKL_DPLL0);
  4737. else
  4738. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4739. SKL_DPLL0);
  4740. I915_WRITE(DPLL_CTRL1, val);
  4741. POSTING_READ(DPLL_CTRL1);
  4742. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4743. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4744. DRM_ERROR("DPLL0 not locked\n");
  4745. }
  4746. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4747. {
  4748. int ret;
  4749. u32 val;
  4750. /* inform PCU we want to change CDCLK */
  4751. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4752. mutex_lock(&dev_priv->rps.hw_lock);
  4753. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4754. mutex_unlock(&dev_priv->rps.hw_lock);
  4755. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4756. }
  4757. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4758. {
  4759. unsigned int i;
  4760. for (i = 0; i < 15; i++) {
  4761. if (skl_cdclk_pcu_ready(dev_priv))
  4762. return true;
  4763. udelay(10);
  4764. }
  4765. return false;
  4766. }
  4767. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4768. {
  4769. struct drm_device *dev = dev_priv->dev;
  4770. u32 freq_select, pcu_ack;
  4771. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4772. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4773. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4774. return;
  4775. }
  4776. /* set CDCLK_CTL */
  4777. switch(freq) {
  4778. case 450000:
  4779. case 432000:
  4780. freq_select = CDCLK_FREQ_450_432;
  4781. pcu_ack = 1;
  4782. break;
  4783. case 540000:
  4784. freq_select = CDCLK_FREQ_540;
  4785. pcu_ack = 2;
  4786. break;
  4787. case 308570:
  4788. case 337500:
  4789. default:
  4790. freq_select = CDCLK_FREQ_337_308;
  4791. pcu_ack = 0;
  4792. break;
  4793. case 617140:
  4794. case 675000:
  4795. freq_select = CDCLK_FREQ_675_617;
  4796. pcu_ack = 3;
  4797. break;
  4798. }
  4799. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4800. POSTING_READ(CDCLK_CTL);
  4801. /* inform PCU of the change */
  4802. mutex_lock(&dev_priv->rps.hw_lock);
  4803. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4804. mutex_unlock(&dev_priv->rps.hw_lock);
  4805. intel_update_cdclk(dev);
  4806. }
  4807. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4808. {
  4809. /* disable DBUF power */
  4810. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4811. POSTING_READ(DBUF_CTL);
  4812. udelay(10);
  4813. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4814. DRM_ERROR("DBuf power disable timeout\n");
  4815. /* disable DPLL0 */
  4816. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4817. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4818. DRM_ERROR("Couldn't disable DPLL0\n");
  4819. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4820. }
  4821. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4822. {
  4823. u32 val;
  4824. unsigned int required_vco;
  4825. /* enable PCH reset handshake */
  4826. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4827. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4828. /* enable PG1 and Misc I/O */
  4829. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4830. /* DPLL0 already enabed !? */
  4831. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4832. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4833. return;
  4834. }
  4835. /* enable DPLL0 */
  4836. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4837. skl_dpll0_enable(dev_priv, required_vco);
  4838. /* set CDCLK to the frequency the BIOS chose */
  4839. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4840. /* enable DBUF power */
  4841. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4842. POSTING_READ(DBUF_CTL);
  4843. udelay(10);
  4844. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4845. DRM_ERROR("DBuf power enable timeout\n");
  4846. }
  4847. /* returns HPLL frequency in kHz */
  4848. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4849. {
  4850. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4851. /* Obtain SKU information */
  4852. mutex_lock(&dev_priv->sb_lock);
  4853. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4854. CCK_FUSE_HPLL_FREQ_MASK;
  4855. mutex_unlock(&dev_priv->sb_lock);
  4856. return vco_freq[hpll_freq] * 1000;
  4857. }
  4858. /* Adjust CDclk dividers to allow high res or save power if possible */
  4859. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4860. {
  4861. struct drm_i915_private *dev_priv = dev->dev_private;
  4862. u32 val, cmd;
  4863. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4864. != dev_priv->cdclk_freq);
  4865. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4866. cmd = 2;
  4867. else if (cdclk == 266667)
  4868. cmd = 1;
  4869. else
  4870. cmd = 0;
  4871. mutex_lock(&dev_priv->rps.hw_lock);
  4872. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4873. val &= ~DSPFREQGUAR_MASK;
  4874. val |= (cmd << DSPFREQGUAR_SHIFT);
  4875. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4876. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4877. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4878. 50)) {
  4879. DRM_ERROR("timed out waiting for CDclk change\n");
  4880. }
  4881. mutex_unlock(&dev_priv->rps.hw_lock);
  4882. mutex_lock(&dev_priv->sb_lock);
  4883. if (cdclk == 400000) {
  4884. u32 divider;
  4885. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4886. /* adjust cdclk divider */
  4887. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4888. val &= ~DISPLAY_FREQUENCY_VALUES;
  4889. val |= divider;
  4890. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4891. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4892. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4893. 50))
  4894. DRM_ERROR("timed out waiting for CDclk change\n");
  4895. }
  4896. /* adjust self-refresh exit latency value */
  4897. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4898. val &= ~0x7f;
  4899. /*
  4900. * For high bandwidth configs, we set a higher latency in the bunit
  4901. * so that the core display fetch happens in time to avoid underruns.
  4902. */
  4903. if (cdclk == 400000)
  4904. val |= 4500 / 250; /* 4.5 usec */
  4905. else
  4906. val |= 3000 / 250; /* 3.0 usec */
  4907. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4908. mutex_unlock(&dev_priv->sb_lock);
  4909. intel_update_cdclk(dev);
  4910. }
  4911. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4912. {
  4913. struct drm_i915_private *dev_priv = dev->dev_private;
  4914. u32 val, cmd;
  4915. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4916. != dev_priv->cdclk_freq);
  4917. switch (cdclk) {
  4918. case 333333:
  4919. case 320000:
  4920. case 266667:
  4921. case 200000:
  4922. break;
  4923. default:
  4924. MISSING_CASE(cdclk);
  4925. return;
  4926. }
  4927. /*
  4928. * Specs are full of misinformation, but testing on actual
  4929. * hardware has shown that we just need to write the desired
  4930. * CCK divider into the Punit register.
  4931. */
  4932. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4933. mutex_lock(&dev_priv->rps.hw_lock);
  4934. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4935. val &= ~DSPFREQGUAR_MASK_CHV;
  4936. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4937. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4938. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4939. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4940. 50)) {
  4941. DRM_ERROR("timed out waiting for CDclk change\n");
  4942. }
  4943. mutex_unlock(&dev_priv->rps.hw_lock);
  4944. intel_update_cdclk(dev);
  4945. }
  4946. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4947. int max_pixclk)
  4948. {
  4949. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4950. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4951. /*
  4952. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4953. * 200MHz
  4954. * 267MHz
  4955. * 320/333MHz (depends on HPLL freq)
  4956. * 400MHz (VLV only)
  4957. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4958. * of the lower bin and adjust if needed.
  4959. *
  4960. * We seem to get an unstable or solid color picture at 200MHz.
  4961. * Not sure what's wrong. For now use 200MHz only when all pipes
  4962. * are off.
  4963. */
  4964. if (!IS_CHERRYVIEW(dev_priv) &&
  4965. max_pixclk > freq_320*limit/100)
  4966. return 400000;
  4967. else if (max_pixclk > 266667*limit/100)
  4968. return freq_320;
  4969. else if (max_pixclk > 0)
  4970. return 266667;
  4971. else
  4972. return 200000;
  4973. }
  4974. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4975. int max_pixclk)
  4976. {
  4977. /*
  4978. * FIXME:
  4979. * - remove the guardband, it's not needed on BXT
  4980. * - set 19.2MHz bypass frequency if there are no active pipes
  4981. */
  4982. if (max_pixclk > 576000*9/10)
  4983. return 624000;
  4984. else if (max_pixclk > 384000*9/10)
  4985. return 576000;
  4986. else if (max_pixclk > 288000*9/10)
  4987. return 384000;
  4988. else if (max_pixclk > 144000*9/10)
  4989. return 288000;
  4990. else
  4991. return 144000;
  4992. }
  4993. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4994. * that's non-NULL, look at current state otherwise. */
  4995. static int intel_mode_max_pixclk(struct drm_device *dev,
  4996. struct drm_atomic_state *state)
  4997. {
  4998. struct intel_crtc *intel_crtc;
  4999. struct intel_crtc_state *crtc_state;
  5000. int max_pixclk = 0;
  5001. for_each_intel_crtc(dev, intel_crtc) {
  5002. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5003. if (IS_ERR(crtc_state))
  5004. return PTR_ERR(crtc_state);
  5005. if (!crtc_state->base.enable)
  5006. continue;
  5007. max_pixclk = max(max_pixclk,
  5008. crtc_state->base.adjusted_mode.crtc_clock);
  5009. }
  5010. return max_pixclk;
  5011. }
  5012. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5013. {
  5014. struct drm_device *dev = state->dev;
  5015. struct drm_i915_private *dev_priv = dev->dev_private;
  5016. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5017. if (max_pixclk < 0)
  5018. return max_pixclk;
  5019. to_intel_atomic_state(state)->cdclk =
  5020. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5021. return 0;
  5022. }
  5023. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5024. {
  5025. struct drm_device *dev = state->dev;
  5026. struct drm_i915_private *dev_priv = dev->dev_private;
  5027. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5028. if (max_pixclk < 0)
  5029. return max_pixclk;
  5030. to_intel_atomic_state(state)->cdclk =
  5031. broxton_calc_cdclk(dev_priv, max_pixclk);
  5032. return 0;
  5033. }
  5034. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5035. {
  5036. unsigned int credits, default_credits;
  5037. if (IS_CHERRYVIEW(dev_priv))
  5038. default_credits = PFI_CREDIT(12);
  5039. else
  5040. default_credits = PFI_CREDIT(8);
  5041. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5042. /* CHV suggested value is 31 or 63 */
  5043. if (IS_CHERRYVIEW(dev_priv))
  5044. credits = PFI_CREDIT_63;
  5045. else
  5046. credits = PFI_CREDIT(15);
  5047. } else {
  5048. credits = default_credits;
  5049. }
  5050. /*
  5051. * WA - write default credits before re-programming
  5052. * FIXME: should we also set the resend bit here?
  5053. */
  5054. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5055. default_credits);
  5056. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5057. credits | PFI_CREDIT_RESEND);
  5058. /*
  5059. * FIXME is this guaranteed to clear
  5060. * immediately or should we poll for it?
  5061. */
  5062. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5063. }
  5064. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5065. {
  5066. struct drm_device *dev = old_state->dev;
  5067. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5068. struct drm_i915_private *dev_priv = dev->dev_private;
  5069. /*
  5070. * FIXME: We can end up here with all power domains off, yet
  5071. * with a CDCLK frequency other than the minimum. To account
  5072. * for this take the PIPE-A power domain, which covers the HW
  5073. * blocks needed for the following programming. This can be
  5074. * removed once it's guaranteed that we get here either with
  5075. * the minimum CDCLK set, or the required power domains
  5076. * enabled.
  5077. */
  5078. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5079. if (IS_CHERRYVIEW(dev))
  5080. cherryview_set_cdclk(dev, req_cdclk);
  5081. else
  5082. valleyview_set_cdclk(dev, req_cdclk);
  5083. vlv_program_pfi_credits(dev_priv);
  5084. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5085. }
  5086. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5087. {
  5088. struct drm_device *dev = crtc->dev;
  5089. struct drm_i915_private *dev_priv = to_i915(dev);
  5090. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5091. struct intel_encoder *encoder;
  5092. int pipe = intel_crtc->pipe;
  5093. bool is_dsi;
  5094. if (WARN_ON(intel_crtc->active))
  5095. return;
  5096. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5097. if (!is_dsi) {
  5098. if (IS_CHERRYVIEW(dev))
  5099. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5100. else
  5101. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5102. }
  5103. if (intel_crtc->config->has_dp_encoder)
  5104. intel_dp_set_m_n(intel_crtc, M1_N1);
  5105. intel_set_pipe_timings(intel_crtc);
  5106. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5107. struct drm_i915_private *dev_priv = dev->dev_private;
  5108. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5109. I915_WRITE(CHV_CANVAS(pipe), 0);
  5110. }
  5111. i9xx_set_pipeconf(intel_crtc);
  5112. intel_crtc->active = true;
  5113. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5114. for_each_encoder_on_crtc(dev, crtc, encoder)
  5115. if (encoder->pre_pll_enable)
  5116. encoder->pre_pll_enable(encoder);
  5117. if (!is_dsi) {
  5118. if (IS_CHERRYVIEW(dev))
  5119. chv_enable_pll(intel_crtc, intel_crtc->config);
  5120. else
  5121. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5122. }
  5123. for_each_encoder_on_crtc(dev, crtc, encoder)
  5124. if (encoder->pre_enable)
  5125. encoder->pre_enable(encoder);
  5126. i9xx_pfit_enable(intel_crtc);
  5127. intel_crtc_load_lut(crtc);
  5128. intel_enable_pipe(intel_crtc);
  5129. assert_vblank_disabled(crtc);
  5130. drm_crtc_vblank_on(crtc);
  5131. for_each_encoder_on_crtc(dev, crtc, encoder)
  5132. encoder->enable(encoder);
  5133. }
  5134. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5135. {
  5136. struct drm_device *dev = crtc->base.dev;
  5137. struct drm_i915_private *dev_priv = dev->dev_private;
  5138. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5139. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5140. }
  5141. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5142. {
  5143. struct drm_device *dev = crtc->dev;
  5144. struct drm_i915_private *dev_priv = to_i915(dev);
  5145. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5146. struct intel_encoder *encoder;
  5147. int pipe = intel_crtc->pipe;
  5148. if (WARN_ON(intel_crtc->active))
  5149. return;
  5150. i9xx_set_pll_dividers(intel_crtc);
  5151. if (intel_crtc->config->has_dp_encoder)
  5152. intel_dp_set_m_n(intel_crtc, M1_N1);
  5153. intel_set_pipe_timings(intel_crtc);
  5154. i9xx_set_pipeconf(intel_crtc);
  5155. intel_crtc->active = true;
  5156. if (!IS_GEN2(dev))
  5157. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5158. for_each_encoder_on_crtc(dev, crtc, encoder)
  5159. if (encoder->pre_enable)
  5160. encoder->pre_enable(encoder);
  5161. i9xx_enable_pll(intel_crtc);
  5162. i9xx_pfit_enable(intel_crtc);
  5163. intel_crtc_load_lut(crtc);
  5164. intel_update_watermarks(crtc);
  5165. intel_enable_pipe(intel_crtc);
  5166. assert_vblank_disabled(crtc);
  5167. drm_crtc_vblank_on(crtc);
  5168. for_each_encoder_on_crtc(dev, crtc, encoder)
  5169. encoder->enable(encoder);
  5170. }
  5171. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5172. {
  5173. struct drm_device *dev = crtc->base.dev;
  5174. struct drm_i915_private *dev_priv = dev->dev_private;
  5175. if (!crtc->config->gmch_pfit.control)
  5176. return;
  5177. assert_pipe_disabled(dev_priv, crtc->pipe);
  5178. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5179. I915_READ(PFIT_CONTROL));
  5180. I915_WRITE(PFIT_CONTROL, 0);
  5181. }
  5182. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5183. {
  5184. struct drm_device *dev = crtc->dev;
  5185. struct drm_i915_private *dev_priv = dev->dev_private;
  5186. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5187. struct intel_encoder *encoder;
  5188. int pipe = intel_crtc->pipe;
  5189. /*
  5190. * On gen2 planes are double buffered but the pipe isn't, so we must
  5191. * wait for planes to fully turn off before disabling the pipe.
  5192. * We also need to wait on all gmch platforms because of the
  5193. * self-refresh mode constraint explained above.
  5194. */
  5195. intel_wait_for_vblank(dev, pipe);
  5196. for_each_encoder_on_crtc(dev, crtc, encoder)
  5197. encoder->disable(encoder);
  5198. drm_crtc_vblank_off(crtc);
  5199. assert_vblank_disabled(crtc);
  5200. intel_disable_pipe(intel_crtc);
  5201. i9xx_pfit_disable(intel_crtc);
  5202. for_each_encoder_on_crtc(dev, crtc, encoder)
  5203. if (encoder->post_disable)
  5204. encoder->post_disable(encoder);
  5205. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5206. if (IS_CHERRYVIEW(dev))
  5207. chv_disable_pll(dev_priv, pipe);
  5208. else if (IS_VALLEYVIEW(dev))
  5209. vlv_disable_pll(dev_priv, pipe);
  5210. else
  5211. i9xx_disable_pll(intel_crtc);
  5212. }
  5213. if (!IS_GEN2(dev))
  5214. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5215. intel_crtc->active = false;
  5216. intel_update_watermarks(crtc);
  5217. }
  5218. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5219. {
  5220. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5221. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5222. enum intel_display_power_domain domain;
  5223. unsigned long domains;
  5224. if (!intel_crtc->active)
  5225. return;
  5226. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5227. intel_crtc_wait_for_pending_flips(crtc);
  5228. intel_pre_disable_primary(crtc);
  5229. }
  5230. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5231. dev_priv->display.crtc_disable(crtc);
  5232. intel_disable_shared_dpll(intel_crtc);
  5233. domains = intel_crtc->enabled_power_domains;
  5234. for_each_power_domain(domain, domains)
  5235. intel_display_power_put(dev_priv, domain);
  5236. intel_crtc->enabled_power_domains = 0;
  5237. }
  5238. /*
  5239. * turn all crtc's off, but do not adjust state
  5240. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5241. */
  5242. int intel_display_suspend(struct drm_device *dev)
  5243. {
  5244. struct drm_mode_config *config = &dev->mode_config;
  5245. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5246. struct drm_atomic_state *state;
  5247. struct drm_crtc *crtc;
  5248. unsigned crtc_mask = 0;
  5249. int ret = 0;
  5250. if (WARN_ON(!ctx))
  5251. return 0;
  5252. lockdep_assert_held(&ctx->ww_ctx);
  5253. state = drm_atomic_state_alloc(dev);
  5254. if (WARN_ON(!state))
  5255. return -ENOMEM;
  5256. state->acquire_ctx = ctx;
  5257. state->allow_modeset = true;
  5258. for_each_crtc(dev, crtc) {
  5259. struct drm_crtc_state *crtc_state =
  5260. drm_atomic_get_crtc_state(state, crtc);
  5261. ret = PTR_ERR_OR_ZERO(crtc_state);
  5262. if (ret)
  5263. goto free;
  5264. if (!crtc_state->active)
  5265. continue;
  5266. crtc_state->active = false;
  5267. crtc_mask |= 1 << drm_crtc_index(crtc);
  5268. }
  5269. if (crtc_mask) {
  5270. ret = drm_atomic_commit(state);
  5271. if (!ret) {
  5272. for_each_crtc(dev, crtc)
  5273. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5274. crtc->state->active = true;
  5275. return ret;
  5276. }
  5277. }
  5278. free:
  5279. if (ret)
  5280. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5281. drm_atomic_state_free(state);
  5282. return ret;
  5283. }
  5284. void intel_encoder_destroy(struct drm_encoder *encoder)
  5285. {
  5286. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5287. drm_encoder_cleanup(encoder);
  5288. kfree(intel_encoder);
  5289. }
  5290. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5291. * internal consistency). */
  5292. static void intel_connector_check_state(struct intel_connector *connector)
  5293. {
  5294. struct drm_crtc *crtc = connector->base.state->crtc;
  5295. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5296. connector->base.base.id,
  5297. connector->base.name);
  5298. if (connector->get_hw_state(connector)) {
  5299. struct drm_encoder *encoder = &connector->encoder->base;
  5300. struct drm_connector_state *conn_state = connector->base.state;
  5301. I915_STATE_WARN(!crtc,
  5302. "connector enabled without attached crtc\n");
  5303. if (!crtc)
  5304. return;
  5305. I915_STATE_WARN(!crtc->state->active,
  5306. "connector is active, but attached crtc isn't\n");
  5307. if (!encoder)
  5308. return;
  5309. I915_STATE_WARN(conn_state->best_encoder != encoder,
  5310. "atomic encoder doesn't match attached encoder\n");
  5311. I915_STATE_WARN(conn_state->crtc != encoder->crtc,
  5312. "attached encoder crtc differs from connector crtc\n");
  5313. } else {
  5314. I915_STATE_WARN(crtc && crtc->state->active,
  5315. "attached crtc is active, but connector isn't\n");
  5316. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5317. "best encoder set without crtc!\n");
  5318. }
  5319. }
  5320. int intel_connector_init(struct intel_connector *connector)
  5321. {
  5322. struct drm_connector_state *connector_state;
  5323. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5324. if (!connector_state)
  5325. return -ENOMEM;
  5326. connector->base.state = connector_state;
  5327. return 0;
  5328. }
  5329. struct intel_connector *intel_connector_alloc(void)
  5330. {
  5331. struct intel_connector *connector;
  5332. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5333. if (!connector)
  5334. return NULL;
  5335. if (intel_connector_init(connector) < 0) {
  5336. kfree(connector);
  5337. return NULL;
  5338. }
  5339. return connector;
  5340. }
  5341. /* Simple connector->get_hw_state implementation for encoders that support only
  5342. * one connector and no cloning and hence the encoder state determines the state
  5343. * of the connector. */
  5344. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5345. {
  5346. enum pipe pipe = 0;
  5347. struct intel_encoder *encoder = connector->encoder;
  5348. return encoder->get_hw_state(encoder, &pipe);
  5349. }
  5350. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5351. {
  5352. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5353. return crtc_state->fdi_lanes;
  5354. return 0;
  5355. }
  5356. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5357. struct intel_crtc_state *pipe_config)
  5358. {
  5359. struct drm_atomic_state *state = pipe_config->base.state;
  5360. struct intel_crtc *other_crtc;
  5361. struct intel_crtc_state *other_crtc_state;
  5362. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5363. pipe_name(pipe), pipe_config->fdi_lanes);
  5364. if (pipe_config->fdi_lanes > 4) {
  5365. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5366. pipe_name(pipe), pipe_config->fdi_lanes);
  5367. return -EINVAL;
  5368. }
  5369. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5370. if (pipe_config->fdi_lanes > 2) {
  5371. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5372. pipe_config->fdi_lanes);
  5373. return -EINVAL;
  5374. } else {
  5375. return 0;
  5376. }
  5377. }
  5378. if (INTEL_INFO(dev)->num_pipes == 2)
  5379. return 0;
  5380. /* Ivybridge 3 pipe is really complicated */
  5381. switch (pipe) {
  5382. case PIPE_A:
  5383. return 0;
  5384. case PIPE_B:
  5385. if (pipe_config->fdi_lanes <= 2)
  5386. return 0;
  5387. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5388. other_crtc_state =
  5389. intel_atomic_get_crtc_state(state, other_crtc);
  5390. if (IS_ERR(other_crtc_state))
  5391. return PTR_ERR(other_crtc_state);
  5392. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5393. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5394. pipe_name(pipe), pipe_config->fdi_lanes);
  5395. return -EINVAL;
  5396. }
  5397. return 0;
  5398. case PIPE_C:
  5399. if (pipe_config->fdi_lanes > 2) {
  5400. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5401. pipe_name(pipe), pipe_config->fdi_lanes);
  5402. return -EINVAL;
  5403. }
  5404. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5405. other_crtc_state =
  5406. intel_atomic_get_crtc_state(state, other_crtc);
  5407. if (IS_ERR(other_crtc_state))
  5408. return PTR_ERR(other_crtc_state);
  5409. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5410. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5411. return -EINVAL;
  5412. }
  5413. return 0;
  5414. default:
  5415. BUG();
  5416. }
  5417. }
  5418. #define RETRY 1
  5419. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5420. struct intel_crtc_state *pipe_config)
  5421. {
  5422. struct drm_device *dev = intel_crtc->base.dev;
  5423. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5424. int lane, link_bw, fdi_dotclock, ret;
  5425. bool needs_recompute = false;
  5426. retry:
  5427. /* FDI is a binary signal running at ~2.7GHz, encoding
  5428. * each output octet as 10 bits. The actual frequency
  5429. * is stored as a divider into a 100MHz clock, and the
  5430. * mode pixel clock is stored in units of 1KHz.
  5431. * Hence the bw of each lane in terms of the mode signal
  5432. * is:
  5433. */
  5434. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5435. fdi_dotclock = adjusted_mode->crtc_clock;
  5436. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5437. pipe_config->pipe_bpp);
  5438. pipe_config->fdi_lanes = lane;
  5439. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5440. link_bw, &pipe_config->fdi_m_n);
  5441. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5442. intel_crtc->pipe, pipe_config);
  5443. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5444. pipe_config->pipe_bpp -= 2*3;
  5445. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5446. pipe_config->pipe_bpp);
  5447. needs_recompute = true;
  5448. pipe_config->bw_constrained = true;
  5449. goto retry;
  5450. }
  5451. if (needs_recompute)
  5452. return RETRY;
  5453. return ret;
  5454. }
  5455. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5456. struct intel_crtc_state *pipe_config)
  5457. {
  5458. if (pipe_config->pipe_bpp > 24)
  5459. return false;
  5460. /* HSW can handle pixel rate up to cdclk? */
  5461. if (IS_HASWELL(dev_priv->dev))
  5462. return true;
  5463. /*
  5464. * We compare against max which means we must take
  5465. * the increased cdclk requirement into account when
  5466. * calculating the new cdclk.
  5467. *
  5468. * Should measure whether using a lower cdclk w/o IPS
  5469. */
  5470. return ilk_pipe_pixel_rate(pipe_config) <=
  5471. dev_priv->max_cdclk_freq * 95 / 100;
  5472. }
  5473. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5474. struct intel_crtc_state *pipe_config)
  5475. {
  5476. struct drm_device *dev = crtc->base.dev;
  5477. struct drm_i915_private *dev_priv = dev->dev_private;
  5478. pipe_config->ips_enabled = i915.enable_ips &&
  5479. hsw_crtc_supports_ips(crtc) &&
  5480. pipe_config_supports_ips(dev_priv, pipe_config);
  5481. }
  5482. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5483. struct intel_crtc_state *pipe_config)
  5484. {
  5485. struct drm_device *dev = crtc->base.dev;
  5486. struct drm_i915_private *dev_priv = dev->dev_private;
  5487. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5488. /* FIXME should check pixel clock limits on all platforms */
  5489. if (INTEL_INFO(dev)->gen < 4) {
  5490. int clock_limit = dev_priv->max_cdclk_freq;
  5491. /*
  5492. * Enable pixel doubling when the dot clock
  5493. * is > 90% of the (display) core speed.
  5494. *
  5495. * GDG double wide on either pipe,
  5496. * otherwise pipe A only.
  5497. */
  5498. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5499. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5500. clock_limit *= 2;
  5501. pipe_config->double_wide = true;
  5502. }
  5503. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5504. return -EINVAL;
  5505. }
  5506. /*
  5507. * Pipe horizontal size must be even in:
  5508. * - DVO ganged mode
  5509. * - LVDS dual channel mode
  5510. * - Double wide pipe
  5511. */
  5512. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5513. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5514. pipe_config->pipe_src_w &= ~1;
  5515. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5516. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5517. */
  5518. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5519. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5520. return -EINVAL;
  5521. if (HAS_IPS(dev))
  5522. hsw_compute_ips_config(crtc, pipe_config);
  5523. if (pipe_config->has_pch_encoder)
  5524. return ironlake_fdi_compute_config(crtc, pipe_config);
  5525. return 0;
  5526. }
  5527. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5528. {
  5529. struct drm_i915_private *dev_priv = to_i915(dev);
  5530. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5531. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5532. uint32_t linkrate;
  5533. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5534. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5535. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5536. return 540000;
  5537. linkrate = (I915_READ(DPLL_CTRL1) &
  5538. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5539. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5540. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5541. /* vco 8640 */
  5542. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5543. case CDCLK_FREQ_450_432:
  5544. return 432000;
  5545. case CDCLK_FREQ_337_308:
  5546. return 308570;
  5547. case CDCLK_FREQ_675_617:
  5548. return 617140;
  5549. default:
  5550. WARN(1, "Unknown cd freq selection\n");
  5551. }
  5552. } else {
  5553. /* vco 8100 */
  5554. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5555. case CDCLK_FREQ_450_432:
  5556. return 450000;
  5557. case CDCLK_FREQ_337_308:
  5558. return 337500;
  5559. case CDCLK_FREQ_675_617:
  5560. return 675000;
  5561. default:
  5562. WARN(1, "Unknown cd freq selection\n");
  5563. }
  5564. }
  5565. /* error case, do as if DPLL0 isn't enabled */
  5566. return 24000;
  5567. }
  5568. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5569. {
  5570. struct drm_i915_private *dev_priv = to_i915(dev);
  5571. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5572. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5573. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5574. int cdclk;
  5575. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5576. return 19200;
  5577. cdclk = 19200 * pll_ratio / 2;
  5578. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5579. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5580. return cdclk; /* 576MHz or 624MHz */
  5581. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5582. return cdclk * 2 / 3; /* 384MHz */
  5583. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5584. return cdclk / 2; /* 288MHz */
  5585. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5586. return cdclk / 4; /* 144MHz */
  5587. }
  5588. /* error case, do as if DE PLL isn't enabled */
  5589. return 19200;
  5590. }
  5591. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5592. {
  5593. struct drm_i915_private *dev_priv = dev->dev_private;
  5594. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5595. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5596. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5597. return 800000;
  5598. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5599. return 450000;
  5600. else if (freq == LCPLL_CLK_FREQ_450)
  5601. return 450000;
  5602. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5603. return 540000;
  5604. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5605. return 337500;
  5606. else
  5607. return 675000;
  5608. }
  5609. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5610. {
  5611. struct drm_i915_private *dev_priv = dev->dev_private;
  5612. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5613. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5614. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5615. return 800000;
  5616. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5617. return 450000;
  5618. else if (freq == LCPLL_CLK_FREQ_450)
  5619. return 450000;
  5620. else if (IS_HSW_ULT(dev))
  5621. return 337500;
  5622. else
  5623. return 540000;
  5624. }
  5625. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5626. {
  5627. struct drm_i915_private *dev_priv = dev->dev_private;
  5628. u32 val;
  5629. int divider;
  5630. if (dev_priv->hpll_freq == 0)
  5631. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5632. mutex_lock(&dev_priv->sb_lock);
  5633. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5634. mutex_unlock(&dev_priv->sb_lock);
  5635. divider = val & DISPLAY_FREQUENCY_VALUES;
  5636. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5637. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5638. "cdclk change in progress\n");
  5639. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5640. }
  5641. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5642. {
  5643. return 450000;
  5644. }
  5645. static int i945_get_display_clock_speed(struct drm_device *dev)
  5646. {
  5647. return 400000;
  5648. }
  5649. static int i915_get_display_clock_speed(struct drm_device *dev)
  5650. {
  5651. return 333333;
  5652. }
  5653. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5654. {
  5655. return 200000;
  5656. }
  5657. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5658. {
  5659. u16 gcfgc = 0;
  5660. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5661. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5662. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5663. return 266667;
  5664. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5665. return 333333;
  5666. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5667. return 444444;
  5668. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5669. return 200000;
  5670. default:
  5671. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5672. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5673. return 133333;
  5674. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5675. return 166667;
  5676. }
  5677. }
  5678. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5679. {
  5680. u16 gcfgc = 0;
  5681. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5682. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5683. return 133333;
  5684. else {
  5685. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5686. case GC_DISPLAY_CLOCK_333_MHZ:
  5687. return 333333;
  5688. default:
  5689. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5690. return 190000;
  5691. }
  5692. }
  5693. }
  5694. static int i865_get_display_clock_speed(struct drm_device *dev)
  5695. {
  5696. return 266667;
  5697. }
  5698. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5699. {
  5700. u16 hpllcc = 0;
  5701. /*
  5702. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5703. * encoding is different :(
  5704. * FIXME is this the right way to detect 852GM/852GMV?
  5705. */
  5706. if (dev->pdev->revision == 0x1)
  5707. return 133333;
  5708. pci_bus_read_config_word(dev->pdev->bus,
  5709. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5710. /* Assume that the hardware is in the high speed state. This
  5711. * should be the default.
  5712. */
  5713. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5714. case GC_CLOCK_133_200:
  5715. case GC_CLOCK_133_200_2:
  5716. case GC_CLOCK_100_200:
  5717. return 200000;
  5718. case GC_CLOCK_166_250:
  5719. return 250000;
  5720. case GC_CLOCK_100_133:
  5721. return 133333;
  5722. case GC_CLOCK_133_266:
  5723. case GC_CLOCK_133_266_2:
  5724. case GC_CLOCK_166_266:
  5725. return 266667;
  5726. }
  5727. /* Shouldn't happen */
  5728. return 0;
  5729. }
  5730. static int i830_get_display_clock_speed(struct drm_device *dev)
  5731. {
  5732. return 133333;
  5733. }
  5734. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5735. {
  5736. struct drm_i915_private *dev_priv = dev->dev_private;
  5737. static const unsigned int blb_vco[8] = {
  5738. [0] = 3200000,
  5739. [1] = 4000000,
  5740. [2] = 5333333,
  5741. [3] = 4800000,
  5742. [4] = 6400000,
  5743. };
  5744. static const unsigned int pnv_vco[8] = {
  5745. [0] = 3200000,
  5746. [1] = 4000000,
  5747. [2] = 5333333,
  5748. [3] = 4800000,
  5749. [4] = 2666667,
  5750. };
  5751. static const unsigned int cl_vco[8] = {
  5752. [0] = 3200000,
  5753. [1] = 4000000,
  5754. [2] = 5333333,
  5755. [3] = 6400000,
  5756. [4] = 3333333,
  5757. [5] = 3566667,
  5758. [6] = 4266667,
  5759. };
  5760. static const unsigned int elk_vco[8] = {
  5761. [0] = 3200000,
  5762. [1] = 4000000,
  5763. [2] = 5333333,
  5764. [3] = 4800000,
  5765. };
  5766. static const unsigned int ctg_vco[8] = {
  5767. [0] = 3200000,
  5768. [1] = 4000000,
  5769. [2] = 5333333,
  5770. [3] = 6400000,
  5771. [4] = 2666667,
  5772. [5] = 4266667,
  5773. };
  5774. const unsigned int *vco_table;
  5775. unsigned int vco;
  5776. uint8_t tmp = 0;
  5777. /* FIXME other chipsets? */
  5778. if (IS_GM45(dev))
  5779. vco_table = ctg_vco;
  5780. else if (IS_G4X(dev))
  5781. vco_table = elk_vco;
  5782. else if (IS_CRESTLINE(dev))
  5783. vco_table = cl_vco;
  5784. else if (IS_PINEVIEW(dev))
  5785. vco_table = pnv_vco;
  5786. else if (IS_G33(dev))
  5787. vco_table = blb_vco;
  5788. else
  5789. return 0;
  5790. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5791. vco = vco_table[tmp & 0x7];
  5792. if (vco == 0)
  5793. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5794. else
  5795. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5796. return vco;
  5797. }
  5798. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5799. {
  5800. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5801. uint16_t tmp = 0;
  5802. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5803. cdclk_sel = (tmp >> 12) & 0x1;
  5804. switch (vco) {
  5805. case 2666667:
  5806. case 4000000:
  5807. case 5333333:
  5808. return cdclk_sel ? 333333 : 222222;
  5809. case 3200000:
  5810. return cdclk_sel ? 320000 : 228571;
  5811. default:
  5812. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5813. return 222222;
  5814. }
  5815. }
  5816. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5817. {
  5818. static const uint8_t div_3200[] = { 16, 10, 8 };
  5819. static const uint8_t div_4000[] = { 20, 12, 10 };
  5820. static const uint8_t div_5333[] = { 24, 16, 14 };
  5821. const uint8_t *div_table;
  5822. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5823. uint16_t tmp = 0;
  5824. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5825. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5826. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5827. goto fail;
  5828. switch (vco) {
  5829. case 3200000:
  5830. div_table = div_3200;
  5831. break;
  5832. case 4000000:
  5833. div_table = div_4000;
  5834. break;
  5835. case 5333333:
  5836. div_table = div_5333;
  5837. break;
  5838. default:
  5839. goto fail;
  5840. }
  5841. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5842. fail:
  5843. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5844. return 200000;
  5845. }
  5846. static int g33_get_display_clock_speed(struct drm_device *dev)
  5847. {
  5848. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5849. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5850. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5851. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5852. const uint8_t *div_table;
  5853. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5854. uint16_t tmp = 0;
  5855. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5856. cdclk_sel = (tmp >> 4) & 0x7;
  5857. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5858. goto fail;
  5859. switch (vco) {
  5860. case 3200000:
  5861. div_table = div_3200;
  5862. break;
  5863. case 4000000:
  5864. div_table = div_4000;
  5865. break;
  5866. case 4800000:
  5867. div_table = div_4800;
  5868. break;
  5869. case 5333333:
  5870. div_table = div_5333;
  5871. break;
  5872. default:
  5873. goto fail;
  5874. }
  5875. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5876. fail:
  5877. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5878. return 190476;
  5879. }
  5880. static void
  5881. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5882. {
  5883. while (*num > DATA_LINK_M_N_MASK ||
  5884. *den > DATA_LINK_M_N_MASK) {
  5885. *num >>= 1;
  5886. *den >>= 1;
  5887. }
  5888. }
  5889. static void compute_m_n(unsigned int m, unsigned int n,
  5890. uint32_t *ret_m, uint32_t *ret_n)
  5891. {
  5892. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5893. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5894. intel_reduce_m_n_ratio(ret_m, ret_n);
  5895. }
  5896. void
  5897. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5898. int pixel_clock, int link_clock,
  5899. struct intel_link_m_n *m_n)
  5900. {
  5901. m_n->tu = 64;
  5902. compute_m_n(bits_per_pixel * pixel_clock,
  5903. link_clock * nlanes * 8,
  5904. &m_n->gmch_m, &m_n->gmch_n);
  5905. compute_m_n(pixel_clock, link_clock,
  5906. &m_n->link_m, &m_n->link_n);
  5907. }
  5908. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5909. {
  5910. if (i915.panel_use_ssc >= 0)
  5911. return i915.panel_use_ssc != 0;
  5912. return dev_priv->vbt.lvds_use_ssc
  5913. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5914. }
  5915. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5916. int num_connectors)
  5917. {
  5918. struct drm_device *dev = crtc_state->base.crtc->dev;
  5919. struct drm_i915_private *dev_priv = dev->dev_private;
  5920. int refclk;
  5921. WARN_ON(!crtc_state->base.state);
  5922. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5923. refclk = 100000;
  5924. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5925. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5926. refclk = dev_priv->vbt.lvds_ssc_freq;
  5927. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5928. } else if (!IS_GEN2(dev)) {
  5929. refclk = 96000;
  5930. } else {
  5931. refclk = 48000;
  5932. }
  5933. return refclk;
  5934. }
  5935. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5936. {
  5937. return (1 << dpll->n) << 16 | dpll->m2;
  5938. }
  5939. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5940. {
  5941. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5942. }
  5943. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5944. struct intel_crtc_state *crtc_state,
  5945. intel_clock_t *reduced_clock)
  5946. {
  5947. struct drm_device *dev = crtc->base.dev;
  5948. u32 fp, fp2 = 0;
  5949. if (IS_PINEVIEW(dev)) {
  5950. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5951. if (reduced_clock)
  5952. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5953. } else {
  5954. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5955. if (reduced_clock)
  5956. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5957. }
  5958. crtc_state->dpll_hw_state.fp0 = fp;
  5959. crtc->lowfreq_avail = false;
  5960. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5961. reduced_clock) {
  5962. crtc_state->dpll_hw_state.fp1 = fp2;
  5963. crtc->lowfreq_avail = true;
  5964. } else {
  5965. crtc_state->dpll_hw_state.fp1 = fp;
  5966. }
  5967. }
  5968. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5969. pipe)
  5970. {
  5971. u32 reg_val;
  5972. /*
  5973. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5974. * and set it to a reasonable value instead.
  5975. */
  5976. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5977. reg_val &= 0xffffff00;
  5978. reg_val |= 0x00000030;
  5979. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5980. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5981. reg_val &= 0x8cffffff;
  5982. reg_val = 0x8c000000;
  5983. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5984. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5985. reg_val &= 0xffffff00;
  5986. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5987. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5988. reg_val &= 0x00ffffff;
  5989. reg_val |= 0xb0000000;
  5990. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5991. }
  5992. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5993. struct intel_link_m_n *m_n)
  5994. {
  5995. struct drm_device *dev = crtc->base.dev;
  5996. struct drm_i915_private *dev_priv = dev->dev_private;
  5997. int pipe = crtc->pipe;
  5998. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5999. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6000. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6001. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6002. }
  6003. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6004. struct intel_link_m_n *m_n,
  6005. struct intel_link_m_n *m2_n2)
  6006. {
  6007. struct drm_device *dev = crtc->base.dev;
  6008. struct drm_i915_private *dev_priv = dev->dev_private;
  6009. int pipe = crtc->pipe;
  6010. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6011. if (INTEL_INFO(dev)->gen >= 5) {
  6012. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6013. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6014. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6015. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6016. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6017. * for gen < 8) and if DRRS is supported (to make sure the
  6018. * registers are not unnecessarily accessed).
  6019. */
  6020. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6021. crtc->config->has_drrs) {
  6022. I915_WRITE(PIPE_DATA_M2(transcoder),
  6023. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6024. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6025. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6026. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6027. }
  6028. } else {
  6029. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6030. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6031. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6032. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6033. }
  6034. }
  6035. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6036. {
  6037. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6038. if (m_n == M1_N1) {
  6039. dp_m_n = &crtc->config->dp_m_n;
  6040. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6041. } else if (m_n == M2_N2) {
  6042. /*
  6043. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6044. * needs to be programmed into M1_N1.
  6045. */
  6046. dp_m_n = &crtc->config->dp_m2_n2;
  6047. } else {
  6048. DRM_ERROR("Unsupported divider value\n");
  6049. return;
  6050. }
  6051. if (crtc->config->has_pch_encoder)
  6052. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6053. else
  6054. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6055. }
  6056. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6057. struct intel_crtc_state *pipe_config)
  6058. {
  6059. u32 dpll, dpll_md;
  6060. /*
  6061. * Enable DPIO clock input. We should never disable the reference
  6062. * clock for pipe B, since VGA hotplug / manual detection depends
  6063. * on it.
  6064. */
  6065. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6066. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6067. /* We should never disable this, set it here for state tracking */
  6068. if (crtc->pipe == PIPE_B)
  6069. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6070. dpll |= DPLL_VCO_ENABLE;
  6071. pipe_config->dpll_hw_state.dpll = dpll;
  6072. dpll_md = (pipe_config->pixel_multiplier - 1)
  6073. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6074. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6075. }
  6076. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6077. const struct intel_crtc_state *pipe_config)
  6078. {
  6079. struct drm_device *dev = crtc->base.dev;
  6080. struct drm_i915_private *dev_priv = dev->dev_private;
  6081. int pipe = crtc->pipe;
  6082. u32 mdiv;
  6083. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6084. u32 coreclk, reg_val;
  6085. mutex_lock(&dev_priv->sb_lock);
  6086. bestn = pipe_config->dpll.n;
  6087. bestm1 = pipe_config->dpll.m1;
  6088. bestm2 = pipe_config->dpll.m2;
  6089. bestp1 = pipe_config->dpll.p1;
  6090. bestp2 = pipe_config->dpll.p2;
  6091. /* See eDP HDMI DPIO driver vbios notes doc */
  6092. /* PLL B needs special handling */
  6093. if (pipe == PIPE_B)
  6094. vlv_pllb_recal_opamp(dev_priv, pipe);
  6095. /* Set up Tx target for periodic Rcomp update */
  6096. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6097. /* Disable target IRef on PLL */
  6098. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6099. reg_val &= 0x00ffffff;
  6100. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6101. /* Disable fast lock */
  6102. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6103. /* Set idtafcrecal before PLL is enabled */
  6104. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6105. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6106. mdiv |= ((bestn << DPIO_N_SHIFT));
  6107. mdiv |= (1 << DPIO_K_SHIFT);
  6108. /*
  6109. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6110. * but we don't support that).
  6111. * Note: don't use the DAC post divider as it seems unstable.
  6112. */
  6113. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6114. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6115. mdiv |= DPIO_ENABLE_CALIBRATION;
  6116. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6117. /* Set HBR and RBR LPF coefficients */
  6118. if (pipe_config->port_clock == 162000 ||
  6119. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6120. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6121. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6122. 0x009f0003);
  6123. else
  6124. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6125. 0x00d0000f);
  6126. if (pipe_config->has_dp_encoder) {
  6127. /* Use SSC source */
  6128. if (pipe == PIPE_A)
  6129. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6130. 0x0df40000);
  6131. else
  6132. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6133. 0x0df70000);
  6134. } else { /* HDMI or VGA */
  6135. /* Use bend source */
  6136. if (pipe == PIPE_A)
  6137. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6138. 0x0df70000);
  6139. else
  6140. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6141. 0x0df40000);
  6142. }
  6143. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6144. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6145. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6146. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6147. coreclk |= 0x01000000;
  6148. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6149. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6150. mutex_unlock(&dev_priv->sb_lock);
  6151. }
  6152. static void chv_compute_dpll(struct intel_crtc *crtc,
  6153. struct intel_crtc_state *pipe_config)
  6154. {
  6155. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6156. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6157. DPLL_VCO_ENABLE;
  6158. if (crtc->pipe != PIPE_A)
  6159. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6160. pipe_config->dpll_hw_state.dpll_md =
  6161. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6162. }
  6163. static void chv_prepare_pll(struct intel_crtc *crtc,
  6164. const struct intel_crtc_state *pipe_config)
  6165. {
  6166. struct drm_device *dev = crtc->base.dev;
  6167. struct drm_i915_private *dev_priv = dev->dev_private;
  6168. int pipe = crtc->pipe;
  6169. int dpll_reg = DPLL(crtc->pipe);
  6170. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6171. u32 loopfilter, tribuf_calcntr;
  6172. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6173. u32 dpio_val;
  6174. int vco;
  6175. bestn = pipe_config->dpll.n;
  6176. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6177. bestm1 = pipe_config->dpll.m1;
  6178. bestm2 = pipe_config->dpll.m2 >> 22;
  6179. bestp1 = pipe_config->dpll.p1;
  6180. bestp2 = pipe_config->dpll.p2;
  6181. vco = pipe_config->dpll.vco;
  6182. dpio_val = 0;
  6183. loopfilter = 0;
  6184. /*
  6185. * Enable Refclk and SSC
  6186. */
  6187. I915_WRITE(dpll_reg,
  6188. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6189. mutex_lock(&dev_priv->sb_lock);
  6190. /* p1 and p2 divider */
  6191. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6192. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6193. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6194. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6195. 1 << DPIO_CHV_K_DIV_SHIFT);
  6196. /* Feedback post-divider - m2 */
  6197. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6198. /* Feedback refclk divider - n and m1 */
  6199. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6200. DPIO_CHV_M1_DIV_BY_2 |
  6201. 1 << DPIO_CHV_N_DIV_SHIFT);
  6202. /* M2 fraction division */
  6203. if (bestm2_frac)
  6204. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6205. /* M2 fraction division enable */
  6206. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6207. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6208. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6209. if (bestm2_frac)
  6210. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6211. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6212. /* Program digital lock detect threshold */
  6213. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6214. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6215. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6216. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6217. if (!bestm2_frac)
  6218. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6219. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6220. /* Loop filter */
  6221. if (vco == 5400000) {
  6222. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6223. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6224. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6225. tribuf_calcntr = 0x9;
  6226. } else if (vco <= 6200000) {
  6227. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6228. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6229. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6230. tribuf_calcntr = 0x9;
  6231. } else if (vco <= 6480000) {
  6232. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6233. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6234. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6235. tribuf_calcntr = 0x8;
  6236. } else {
  6237. /* Not supported. Apply the same limits as in the max case */
  6238. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6239. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6240. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6241. tribuf_calcntr = 0;
  6242. }
  6243. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6244. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6245. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6246. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6247. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6248. /* AFC Recal */
  6249. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6250. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6251. DPIO_AFC_RECAL);
  6252. mutex_unlock(&dev_priv->sb_lock);
  6253. }
  6254. /**
  6255. * vlv_force_pll_on - forcibly enable just the PLL
  6256. * @dev_priv: i915 private structure
  6257. * @pipe: pipe PLL to enable
  6258. * @dpll: PLL configuration
  6259. *
  6260. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6261. * in cases where we need the PLL enabled even when @pipe is not going to
  6262. * be enabled.
  6263. */
  6264. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6265. const struct dpll *dpll)
  6266. {
  6267. struct intel_crtc *crtc =
  6268. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6269. struct intel_crtc_state pipe_config = {
  6270. .base.crtc = &crtc->base,
  6271. .pixel_multiplier = 1,
  6272. .dpll = *dpll,
  6273. };
  6274. if (IS_CHERRYVIEW(dev)) {
  6275. chv_compute_dpll(crtc, &pipe_config);
  6276. chv_prepare_pll(crtc, &pipe_config);
  6277. chv_enable_pll(crtc, &pipe_config);
  6278. } else {
  6279. vlv_compute_dpll(crtc, &pipe_config);
  6280. vlv_prepare_pll(crtc, &pipe_config);
  6281. vlv_enable_pll(crtc, &pipe_config);
  6282. }
  6283. }
  6284. /**
  6285. * vlv_force_pll_off - forcibly disable just the PLL
  6286. * @dev_priv: i915 private structure
  6287. * @pipe: pipe PLL to disable
  6288. *
  6289. * Disable the PLL for @pipe. To be used in cases where we need
  6290. * the PLL enabled even when @pipe is not going to be enabled.
  6291. */
  6292. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6293. {
  6294. if (IS_CHERRYVIEW(dev))
  6295. chv_disable_pll(to_i915(dev), pipe);
  6296. else
  6297. vlv_disable_pll(to_i915(dev), pipe);
  6298. }
  6299. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6300. struct intel_crtc_state *crtc_state,
  6301. intel_clock_t *reduced_clock,
  6302. int num_connectors)
  6303. {
  6304. struct drm_device *dev = crtc->base.dev;
  6305. struct drm_i915_private *dev_priv = dev->dev_private;
  6306. u32 dpll;
  6307. bool is_sdvo;
  6308. struct dpll *clock = &crtc_state->dpll;
  6309. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6310. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6311. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6312. dpll = DPLL_VGA_MODE_DIS;
  6313. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6314. dpll |= DPLLB_MODE_LVDS;
  6315. else
  6316. dpll |= DPLLB_MODE_DAC_SERIAL;
  6317. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6318. dpll |= (crtc_state->pixel_multiplier - 1)
  6319. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6320. }
  6321. if (is_sdvo)
  6322. dpll |= DPLL_SDVO_HIGH_SPEED;
  6323. if (crtc_state->has_dp_encoder)
  6324. dpll |= DPLL_SDVO_HIGH_SPEED;
  6325. /* compute bitmask from p1 value */
  6326. if (IS_PINEVIEW(dev))
  6327. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6328. else {
  6329. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6330. if (IS_G4X(dev) && reduced_clock)
  6331. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6332. }
  6333. switch (clock->p2) {
  6334. case 5:
  6335. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6336. break;
  6337. case 7:
  6338. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6339. break;
  6340. case 10:
  6341. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6342. break;
  6343. case 14:
  6344. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6345. break;
  6346. }
  6347. if (INTEL_INFO(dev)->gen >= 4)
  6348. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6349. if (crtc_state->sdvo_tv_clock)
  6350. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6351. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6352. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6353. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6354. else
  6355. dpll |= PLL_REF_INPUT_DREFCLK;
  6356. dpll |= DPLL_VCO_ENABLE;
  6357. crtc_state->dpll_hw_state.dpll = dpll;
  6358. if (INTEL_INFO(dev)->gen >= 4) {
  6359. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6360. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6361. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6362. }
  6363. }
  6364. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6365. struct intel_crtc_state *crtc_state,
  6366. intel_clock_t *reduced_clock,
  6367. int num_connectors)
  6368. {
  6369. struct drm_device *dev = crtc->base.dev;
  6370. struct drm_i915_private *dev_priv = dev->dev_private;
  6371. u32 dpll;
  6372. struct dpll *clock = &crtc_state->dpll;
  6373. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6374. dpll = DPLL_VGA_MODE_DIS;
  6375. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6376. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6377. } else {
  6378. if (clock->p1 == 2)
  6379. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6380. else
  6381. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6382. if (clock->p2 == 4)
  6383. dpll |= PLL_P2_DIVIDE_BY_4;
  6384. }
  6385. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6386. dpll |= DPLL_DVO_2X_MODE;
  6387. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6388. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6389. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6390. else
  6391. dpll |= PLL_REF_INPUT_DREFCLK;
  6392. dpll |= DPLL_VCO_ENABLE;
  6393. crtc_state->dpll_hw_state.dpll = dpll;
  6394. }
  6395. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6396. {
  6397. struct drm_device *dev = intel_crtc->base.dev;
  6398. struct drm_i915_private *dev_priv = dev->dev_private;
  6399. enum pipe pipe = intel_crtc->pipe;
  6400. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6401. struct drm_display_mode *adjusted_mode =
  6402. &intel_crtc->config->base.adjusted_mode;
  6403. uint32_t crtc_vtotal, crtc_vblank_end;
  6404. int vsyncshift = 0;
  6405. /* We need to be careful not to changed the adjusted mode, for otherwise
  6406. * the hw state checker will get angry at the mismatch. */
  6407. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6408. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6409. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6410. /* the chip adds 2 halflines automatically */
  6411. crtc_vtotal -= 1;
  6412. crtc_vblank_end -= 1;
  6413. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6414. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6415. else
  6416. vsyncshift = adjusted_mode->crtc_hsync_start -
  6417. adjusted_mode->crtc_htotal / 2;
  6418. if (vsyncshift < 0)
  6419. vsyncshift += adjusted_mode->crtc_htotal;
  6420. }
  6421. if (INTEL_INFO(dev)->gen > 3)
  6422. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6423. I915_WRITE(HTOTAL(cpu_transcoder),
  6424. (adjusted_mode->crtc_hdisplay - 1) |
  6425. ((adjusted_mode->crtc_htotal - 1) << 16));
  6426. I915_WRITE(HBLANK(cpu_transcoder),
  6427. (adjusted_mode->crtc_hblank_start - 1) |
  6428. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6429. I915_WRITE(HSYNC(cpu_transcoder),
  6430. (adjusted_mode->crtc_hsync_start - 1) |
  6431. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6432. I915_WRITE(VTOTAL(cpu_transcoder),
  6433. (adjusted_mode->crtc_vdisplay - 1) |
  6434. ((crtc_vtotal - 1) << 16));
  6435. I915_WRITE(VBLANK(cpu_transcoder),
  6436. (adjusted_mode->crtc_vblank_start - 1) |
  6437. ((crtc_vblank_end - 1) << 16));
  6438. I915_WRITE(VSYNC(cpu_transcoder),
  6439. (adjusted_mode->crtc_vsync_start - 1) |
  6440. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6441. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6442. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6443. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6444. * bits. */
  6445. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6446. (pipe == PIPE_B || pipe == PIPE_C))
  6447. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6448. /* pipesrc controls the size that is scaled from, which should
  6449. * always be the user's requested size.
  6450. */
  6451. I915_WRITE(PIPESRC(pipe),
  6452. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6453. (intel_crtc->config->pipe_src_h - 1));
  6454. }
  6455. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6456. struct intel_crtc_state *pipe_config)
  6457. {
  6458. struct drm_device *dev = crtc->base.dev;
  6459. struct drm_i915_private *dev_priv = dev->dev_private;
  6460. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6461. uint32_t tmp;
  6462. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6463. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6464. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6465. tmp = I915_READ(HBLANK(cpu_transcoder));
  6466. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6467. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6468. tmp = I915_READ(HSYNC(cpu_transcoder));
  6469. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6470. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6471. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6472. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6473. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6474. tmp = I915_READ(VBLANK(cpu_transcoder));
  6475. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6476. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6477. tmp = I915_READ(VSYNC(cpu_transcoder));
  6478. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6479. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6480. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6481. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6482. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6483. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6484. }
  6485. tmp = I915_READ(PIPESRC(crtc->pipe));
  6486. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6487. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6488. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6489. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6490. }
  6491. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6492. struct intel_crtc_state *pipe_config)
  6493. {
  6494. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6495. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6496. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6497. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6498. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6499. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6500. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6501. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6502. mode->flags = pipe_config->base.adjusted_mode.flags;
  6503. mode->type = DRM_MODE_TYPE_DRIVER;
  6504. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6505. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6506. mode->hsync = drm_mode_hsync(mode);
  6507. mode->vrefresh = drm_mode_vrefresh(mode);
  6508. drm_mode_set_name(mode);
  6509. }
  6510. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6511. {
  6512. struct drm_device *dev = intel_crtc->base.dev;
  6513. struct drm_i915_private *dev_priv = dev->dev_private;
  6514. uint32_t pipeconf;
  6515. pipeconf = 0;
  6516. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6517. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6518. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6519. if (intel_crtc->config->double_wide)
  6520. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6521. /* only g4x and later have fancy bpc/dither controls */
  6522. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6523. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6524. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6525. pipeconf |= PIPECONF_DITHER_EN |
  6526. PIPECONF_DITHER_TYPE_SP;
  6527. switch (intel_crtc->config->pipe_bpp) {
  6528. case 18:
  6529. pipeconf |= PIPECONF_6BPC;
  6530. break;
  6531. case 24:
  6532. pipeconf |= PIPECONF_8BPC;
  6533. break;
  6534. case 30:
  6535. pipeconf |= PIPECONF_10BPC;
  6536. break;
  6537. default:
  6538. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6539. BUG();
  6540. }
  6541. }
  6542. if (HAS_PIPE_CXSR(dev)) {
  6543. if (intel_crtc->lowfreq_avail) {
  6544. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6545. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6546. } else {
  6547. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6548. }
  6549. }
  6550. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6551. if (INTEL_INFO(dev)->gen < 4 ||
  6552. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6553. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6554. else
  6555. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6556. } else
  6557. pipeconf |= PIPECONF_PROGRESSIVE;
  6558. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6559. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6560. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6561. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6562. }
  6563. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6564. struct intel_crtc_state *crtc_state)
  6565. {
  6566. struct drm_device *dev = crtc->base.dev;
  6567. struct drm_i915_private *dev_priv = dev->dev_private;
  6568. int refclk, num_connectors = 0;
  6569. intel_clock_t clock;
  6570. bool ok;
  6571. bool is_dsi = false;
  6572. struct intel_encoder *encoder;
  6573. const intel_limit_t *limit;
  6574. struct drm_atomic_state *state = crtc_state->base.state;
  6575. struct drm_connector *connector;
  6576. struct drm_connector_state *connector_state;
  6577. int i;
  6578. memset(&crtc_state->dpll_hw_state, 0,
  6579. sizeof(crtc_state->dpll_hw_state));
  6580. for_each_connector_in_state(state, connector, connector_state, i) {
  6581. if (connector_state->crtc != &crtc->base)
  6582. continue;
  6583. encoder = to_intel_encoder(connector_state->best_encoder);
  6584. switch (encoder->type) {
  6585. case INTEL_OUTPUT_DSI:
  6586. is_dsi = true;
  6587. break;
  6588. default:
  6589. break;
  6590. }
  6591. num_connectors++;
  6592. }
  6593. if (is_dsi)
  6594. return 0;
  6595. if (!crtc_state->clock_set) {
  6596. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6597. /*
  6598. * Returns a set of divisors for the desired target clock with
  6599. * the given refclk, or FALSE. The returned values represent
  6600. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6601. * 2) / p1 / p2.
  6602. */
  6603. limit = intel_limit(crtc_state, refclk);
  6604. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6605. crtc_state->port_clock,
  6606. refclk, NULL, &clock);
  6607. if (!ok) {
  6608. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6609. return -EINVAL;
  6610. }
  6611. /* Compat-code for transition, will disappear. */
  6612. crtc_state->dpll.n = clock.n;
  6613. crtc_state->dpll.m1 = clock.m1;
  6614. crtc_state->dpll.m2 = clock.m2;
  6615. crtc_state->dpll.p1 = clock.p1;
  6616. crtc_state->dpll.p2 = clock.p2;
  6617. }
  6618. if (IS_GEN2(dev)) {
  6619. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6620. num_connectors);
  6621. } else if (IS_CHERRYVIEW(dev)) {
  6622. chv_compute_dpll(crtc, crtc_state);
  6623. } else if (IS_VALLEYVIEW(dev)) {
  6624. vlv_compute_dpll(crtc, crtc_state);
  6625. } else {
  6626. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6627. num_connectors);
  6628. }
  6629. return 0;
  6630. }
  6631. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6632. struct intel_crtc_state *pipe_config)
  6633. {
  6634. struct drm_device *dev = crtc->base.dev;
  6635. struct drm_i915_private *dev_priv = dev->dev_private;
  6636. uint32_t tmp;
  6637. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6638. return;
  6639. tmp = I915_READ(PFIT_CONTROL);
  6640. if (!(tmp & PFIT_ENABLE))
  6641. return;
  6642. /* Check whether the pfit is attached to our pipe. */
  6643. if (INTEL_INFO(dev)->gen < 4) {
  6644. if (crtc->pipe != PIPE_B)
  6645. return;
  6646. } else {
  6647. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6648. return;
  6649. }
  6650. pipe_config->gmch_pfit.control = tmp;
  6651. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6652. if (INTEL_INFO(dev)->gen < 5)
  6653. pipe_config->gmch_pfit.lvds_border_bits =
  6654. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6655. }
  6656. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6657. struct intel_crtc_state *pipe_config)
  6658. {
  6659. struct drm_device *dev = crtc->base.dev;
  6660. struct drm_i915_private *dev_priv = dev->dev_private;
  6661. int pipe = pipe_config->cpu_transcoder;
  6662. intel_clock_t clock;
  6663. u32 mdiv;
  6664. int refclk = 100000;
  6665. /* In case of MIPI DPLL will not even be used */
  6666. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6667. return;
  6668. mutex_lock(&dev_priv->sb_lock);
  6669. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6670. mutex_unlock(&dev_priv->sb_lock);
  6671. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6672. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6673. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6674. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6675. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6676. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6677. }
  6678. static void
  6679. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6680. struct intel_initial_plane_config *plane_config)
  6681. {
  6682. struct drm_device *dev = crtc->base.dev;
  6683. struct drm_i915_private *dev_priv = dev->dev_private;
  6684. u32 val, base, offset;
  6685. int pipe = crtc->pipe, plane = crtc->plane;
  6686. int fourcc, pixel_format;
  6687. unsigned int aligned_height;
  6688. struct drm_framebuffer *fb;
  6689. struct intel_framebuffer *intel_fb;
  6690. val = I915_READ(DSPCNTR(plane));
  6691. if (!(val & DISPLAY_PLANE_ENABLE))
  6692. return;
  6693. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6694. if (!intel_fb) {
  6695. DRM_DEBUG_KMS("failed to alloc fb\n");
  6696. return;
  6697. }
  6698. fb = &intel_fb->base;
  6699. if (INTEL_INFO(dev)->gen >= 4) {
  6700. if (val & DISPPLANE_TILED) {
  6701. plane_config->tiling = I915_TILING_X;
  6702. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6703. }
  6704. }
  6705. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6706. fourcc = i9xx_format_to_fourcc(pixel_format);
  6707. fb->pixel_format = fourcc;
  6708. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6709. if (INTEL_INFO(dev)->gen >= 4) {
  6710. if (plane_config->tiling)
  6711. offset = I915_READ(DSPTILEOFF(plane));
  6712. else
  6713. offset = I915_READ(DSPLINOFF(plane));
  6714. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6715. } else {
  6716. base = I915_READ(DSPADDR(plane));
  6717. }
  6718. plane_config->base = base;
  6719. val = I915_READ(PIPESRC(pipe));
  6720. fb->width = ((val >> 16) & 0xfff) + 1;
  6721. fb->height = ((val >> 0) & 0xfff) + 1;
  6722. val = I915_READ(DSPSTRIDE(pipe));
  6723. fb->pitches[0] = val & 0xffffffc0;
  6724. aligned_height = intel_fb_align_height(dev, fb->height,
  6725. fb->pixel_format,
  6726. fb->modifier[0]);
  6727. plane_config->size = fb->pitches[0] * aligned_height;
  6728. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6729. pipe_name(pipe), plane, fb->width, fb->height,
  6730. fb->bits_per_pixel, base, fb->pitches[0],
  6731. plane_config->size);
  6732. plane_config->fb = intel_fb;
  6733. }
  6734. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6735. struct intel_crtc_state *pipe_config)
  6736. {
  6737. struct drm_device *dev = crtc->base.dev;
  6738. struct drm_i915_private *dev_priv = dev->dev_private;
  6739. int pipe = pipe_config->cpu_transcoder;
  6740. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6741. intel_clock_t clock;
  6742. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6743. int refclk = 100000;
  6744. mutex_lock(&dev_priv->sb_lock);
  6745. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6746. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6747. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6748. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6749. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6750. mutex_unlock(&dev_priv->sb_lock);
  6751. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6752. clock.m2 = (pll_dw0 & 0xff) << 22;
  6753. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6754. clock.m2 |= pll_dw2 & 0x3fffff;
  6755. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6756. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6757. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6758. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6759. }
  6760. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6761. struct intel_crtc_state *pipe_config)
  6762. {
  6763. struct drm_device *dev = crtc->base.dev;
  6764. struct drm_i915_private *dev_priv = dev->dev_private;
  6765. uint32_t tmp;
  6766. if (!intel_display_power_is_enabled(dev_priv,
  6767. POWER_DOMAIN_PIPE(crtc->pipe)))
  6768. return false;
  6769. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6770. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6771. tmp = I915_READ(PIPECONF(crtc->pipe));
  6772. if (!(tmp & PIPECONF_ENABLE))
  6773. return false;
  6774. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6775. switch (tmp & PIPECONF_BPC_MASK) {
  6776. case PIPECONF_6BPC:
  6777. pipe_config->pipe_bpp = 18;
  6778. break;
  6779. case PIPECONF_8BPC:
  6780. pipe_config->pipe_bpp = 24;
  6781. break;
  6782. case PIPECONF_10BPC:
  6783. pipe_config->pipe_bpp = 30;
  6784. break;
  6785. default:
  6786. break;
  6787. }
  6788. }
  6789. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6790. pipe_config->limited_color_range = true;
  6791. if (INTEL_INFO(dev)->gen < 4)
  6792. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6793. intel_get_pipe_timings(crtc, pipe_config);
  6794. i9xx_get_pfit_config(crtc, pipe_config);
  6795. if (INTEL_INFO(dev)->gen >= 4) {
  6796. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6797. pipe_config->pixel_multiplier =
  6798. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6799. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6800. pipe_config->dpll_hw_state.dpll_md = tmp;
  6801. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6802. tmp = I915_READ(DPLL(crtc->pipe));
  6803. pipe_config->pixel_multiplier =
  6804. ((tmp & SDVO_MULTIPLIER_MASK)
  6805. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6806. } else {
  6807. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6808. * port and will be fixed up in the encoder->get_config
  6809. * function. */
  6810. pipe_config->pixel_multiplier = 1;
  6811. }
  6812. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6813. if (!IS_VALLEYVIEW(dev)) {
  6814. /*
  6815. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6816. * on 830. Filter it out here so that we don't
  6817. * report errors due to that.
  6818. */
  6819. if (IS_I830(dev))
  6820. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6821. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6822. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6823. } else {
  6824. /* Mask out read-only status bits. */
  6825. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6826. DPLL_PORTC_READY_MASK |
  6827. DPLL_PORTB_READY_MASK);
  6828. }
  6829. if (IS_CHERRYVIEW(dev))
  6830. chv_crtc_clock_get(crtc, pipe_config);
  6831. else if (IS_VALLEYVIEW(dev))
  6832. vlv_crtc_clock_get(crtc, pipe_config);
  6833. else
  6834. i9xx_crtc_clock_get(crtc, pipe_config);
  6835. return true;
  6836. }
  6837. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6838. {
  6839. struct drm_i915_private *dev_priv = dev->dev_private;
  6840. struct intel_encoder *encoder;
  6841. u32 val, final;
  6842. bool has_lvds = false;
  6843. bool has_cpu_edp = false;
  6844. bool has_panel = false;
  6845. bool has_ck505 = false;
  6846. bool can_ssc = false;
  6847. /* We need to take the global config into account */
  6848. for_each_intel_encoder(dev, encoder) {
  6849. switch (encoder->type) {
  6850. case INTEL_OUTPUT_LVDS:
  6851. has_panel = true;
  6852. has_lvds = true;
  6853. break;
  6854. case INTEL_OUTPUT_EDP:
  6855. has_panel = true;
  6856. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6857. has_cpu_edp = true;
  6858. break;
  6859. default:
  6860. break;
  6861. }
  6862. }
  6863. if (HAS_PCH_IBX(dev)) {
  6864. has_ck505 = dev_priv->vbt.display_clock_mode;
  6865. can_ssc = has_ck505;
  6866. } else {
  6867. has_ck505 = false;
  6868. can_ssc = true;
  6869. }
  6870. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6871. has_panel, has_lvds, has_ck505);
  6872. /* Ironlake: try to setup display ref clock before DPLL
  6873. * enabling. This is only under driver's control after
  6874. * PCH B stepping, previous chipset stepping should be
  6875. * ignoring this setting.
  6876. */
  6877. val = I915_READ(PCH_DREF_CONTROL);
  6878. /* As we must carefully and slowly disable/enable each source in turn,
  6879. * compute the final state we want first and check if we need to
  6880. * make any changes at all.
  6881. */
  6882. final = val;
  6883. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6884. if (has_ck505)
  6885. final |= DREF_NONSPREAD_CK505_ENABLE;
  6886. else
  6887. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6888. final &= ~DREF_SSC_SOURCE_MASK;
  6889. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6890. final &= ~DREF_SSC1_ENABLE;
  6891. if (has_panel) {
  6892. final |= DREF_SSC_SOURCE_ENABLE;
  6893. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6894. final |= DREF_SSC1_ENABLE;
  6895. if (has_cpu_edp) {
  6896. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6897. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6898. else
  6899. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6900. } else
  6901. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6902. } else {
  6903. final |= DREF_SSC_SOURCE_DISABLE;
  6904. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6905. }
  6906. if (final == val)
  6907. return;
  6908. /* Always enable nonspread source */
  6909. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6910. if (has_ck505)
  6911. val |= DREF_NONSPREAD_CK505_ENABLE;
  6912. else
  6913. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6914. if (has_panel) {
  6915. val &= ~DREF_SSC_SOURCE_MASK;
  6916. val |= DREF_SSC_SOURCE_ENABLE;
  6917. /* SSC must be turned on before enabling the CPU output */
  6918. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6919. DRM_DEBUG_KMS("Using SSC on panel\n");
  6920. val |= DREF_SSC1_ENABLE;
  6921. } else
  6922. val &= ~DREF_SSC1_ENABLE;
  6923. /* Get SSC going before enabling the outputs */
  6924. I915_WRITE(PCH_DREF_CONTROL, val);
  6925. POSTING_READ(PCH_DREF_CONTROL);
  6926. udelay(200);
  6927. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6928. /* Enable CPU source on CPU attached eDP */
  6929. if (has_cpu_edp) {
  6930. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6931. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6932. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6933. } else
  6934. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6935. } else
  6936. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6937. I915_WRITE(PCH_DREF_CONTROL, val);
  6938. POSTING_READ(PCH_DREF_CONTROL);
  6939. udelay(200);
  6940. } else {
  6941. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6942. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6943. /* Turn off CPU output */
  6944. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6945. I915_WRITE(PCH_DREF_CONTROL, val);
  6946. POSTING_READ(PCH_DREF_CONTROL);
  6947. udelay(200);
  6948. /* Turn off the SSC source */
  6949. val &= ~DREF_SSC_SOURCE_MASK;
  6950. val |= DREF_SSC_SOURCE_DISABLE;
  6951. /* Turn off SSC1 */
  6952. val &= ~DREF_SSC1_ENABLE;
  6953. I915_WRITE(PCH_DREF_CONTROL, val);
  6954. POSTING_READ(PCH_DREF_CONTROL);
  6955. udelay(200);
  6956. }
  6957. BUG_ON(val != final);
  6958. }
  6959. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6960. {
  6961. uint32_t tmp;
  6962. tmp = I915_READ(SOUTH_CHICKEN2);
  6963. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6964. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6965. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6966. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6967. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6968. tmp = I915_READ(SOUTH_CHICKEN2);
  6969. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6970. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6971. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6972. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6973. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6974. }
  6975. /* WaMPhyProgramming:hsw */
  6976. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6977. {
  6978. uint32_t tmp;
  6979. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6980. tmp &= ~(0xFF << 24);
  6981. tmp |= (0x12 << 24);
  6982. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6983. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6984. tmp |= (1 << 11);
  6985. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6986. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6987. tmp |= (1 << 11);
  6988. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6989. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6990. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6991. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6992. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6993. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6994. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6995. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6996. tmp &= ~(7 << 13);
  6997. tmp |= (5 << 13);
  6998. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6999. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7000. tmp &= ~(7 << 13);
  7001. tmp |= (5 << 13);
  7002. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7003. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7004. tmp &= ~0xFF;
  7005. tmp |= 0x1C;
  7006. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7007. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7008. tmp &= ~0xFF;
  7009. tmp |= 0x1C;
  7010. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7011. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7012. tmp &= ~(0xFF << 16);
  7013. tmp |= (0x1C << 16);
  7014. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7015. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7016. tmp &= ~(0xFF << 16);
  7017. tmp |= (0x1C << 16);
  7018. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7019. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7020. tmp |= (1 << 27);
  7021. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7022. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7023. tmp |= (1 << 27);
  7024. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7025. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7026. tmp &= ~(0xF << 28);
  7027. tmp |= (4 << 28);
  7028. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7029. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7030. tmp &= ~(0xF << 28);
  7031. tmp |= (4 << 28);
  7032. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7033. }
  7034. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7035. * Programming" based on the parameters passed:
  7036. * - Sequence to enable CLKOUT_DP
  7037. * - Sequence to enable CLKOUT_DP without spread
  7038. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7039. */
  7040. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7041. bool with_fdi)
  7042. {
  7043. struct drm_i915_private *dev_priv = dev->dev_private;
  7044. uint32_t reg, tmp;
  7045. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7046. with_spread = true;
  7047. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7048. with_fdi, "LP PCH doesn't have FDI\n"))
  7049. with_fdi = false;
  7050. mutex_lock(&dev_priv->sb_lock);
  7051. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7052. tmp &= ~SBI_SSCCTL_DISABLE;
  7053. tmp |= SBI_SSCCTL_PATHALT;
  7054. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7055. udelay(24);
  7056. if (with_spread) {
  7057. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7058. tmp &= ~SBI_SSCCTL_PATHALT;
  7059. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7060. if (with_fdi) {
  7061. lpt_reset_fdi_mphy(dev_priv);
  7062. lpt_program_fdi_mphy(dev_priv);
  7063. }
  7064. }
  7065. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7066. SBI_GEN0 : SBI_DBUFF0;
  7067. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7068. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7069. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7070. mutex_unlock(&dev_priv->sb_lock);
  7071. }
  7072. /* Sequence to disable CLKOUT_DP */
  7073. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7074. {
  7075. struct drm_i915_private *dev_priv = dev->dev_private;
  7076. uint32_t reg, tmp;
  7077. mutex_lock(&dev_priv->sb_lock);
  7078. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7079. SBI_GEN0 : SBI_DBUFF0;
  7080. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7081. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7082. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7083. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7084. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7085. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7086. tmp |= SBI_SSCCTL_PATHALT;
  7087. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7088. udelay(32);
  7089. }
  7090. tmp |= SBI_SSCCTL_DISABLE;
  7091. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7092. }
  7093. mutex_unlock(&dev_priv->sb_lock);
  7094. }
  7095. static void lpt_init_pch_refclk(struct drm_device *dev)
  7096. {
  7097. struct intel_encoder *encoder;
  7098. bool has_vga = false;
  7099. for_each_intel_encoder(dev, encoder) {
  7100. switch (encoder->type) {
  7101. case INTEL_OUTPUT_ANALOG:
  7102. has_vga = true;
  7103. break;
  7104. default:
  7105. break;
  7106. }
  7107. }
  7108. if (has_vga)
  7109. lpt_enable_clkout_dp(dev, true, true);
  7110. else
  7111. lpt_disable_clkout_dp(dev);
  7112. }
  7113. /*
  7114. * Initialize reference clocks when the driver loads
  7115. */
  7116. void intel_init_pch_refclk(struct drm_device *dev)
  7117. {
  7118. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7119. ironlake_init_pch_refclk(dev);
  7120. else if (HAS_PCH_LPT(dev))
  7121. lpt_init_pch_refclk(dev);
  7122. }
  7123. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7124. {
  7125. struct drm_device *dev = crtc_state->base.crtc->dev;
  7126. struct drm_i915_private *dev_priv = dev->dev_private;
  7127. struct drm_atomic_state *state = crtc_state->base.state;
  7128. struct drm_connector *connector;
  7129. struct drm_connector_state *connector_state;
  7130. struct intel_encoder *encoder;
  7131. int num_connectors = 0, i;
  7132. bool is_lvds = false;
  7133. for_each_connector_in_state(state, connector, connector_state, i) {
  7134. if (connector_state->crtc != crtc_state->base.crtc)
  7135. continue;
  7136. encoder = to_intel_encoder(connector_state->best_encoder);
  7137. switch (encoder->type) {
  7138. case INTEL_OUTPUT_LVDS:
  7139. is_lvds = true;
  7140. break;
  7141. default:
  7142. break;
  7143. }
  7144. num_connectors++;
  7145. }
  7146. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7147. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7148. dev_priv->vbt.lvds_ssc_freq);
  7149. return dev_priv->vbt.lvds_ssc_freq;
  7150. }
  7151. return 120000;
  7152. }
  7153. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7154. {
  7155. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7156. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7157. int pipe = intel_crtc->pipe;
  7158. uint32_t val;
  7159. val = 0;
  7160. switch (intel_crtc->config->pipe_bpp) {
  7161. case 18:
  7162. val |= PIPECONF_6BPC;
  7163. break;
  7164. case 24:
  7165. val |= PIPECONF_8BPC;
  7166. break;
  7167. case 30:
  7168. val |= PIPECONF_10BPC;
  7169. break;
  7170. case 36:
  7171. val |= PIPECONF_12BPC;
  7172. break;
  7173. default:
  7174. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7175. BUG();
  7176. }
  7177. if (intel_crtc->config->dither)
  7178. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7179. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7180. val |= PIPECONF_INTERLACED_ILK;
  7181. else
  7182. val |= PIPECONF_PROGRESSIVE;
  7183. if (intel_crtc->config->limited_color_range)
  7184. val |= PIPECONF_COLOR_RANGE_SELECT;
  7185. I915_WRITE(PIPECONF(pipe), val);
  7186. POSTING_READ(PIPECONF(pipe));
  7187. }
  7188. /*
  7189. * Set up the pipe CSC unit.
  7190. *
  7191. * Currently only full range RGB to limited range RGB conversion
  7192. * is supported, but eventually this should handle various
  7193. * RGB<->YCbCr scenarios as well.
  7194. */
  7195. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7196. {
  7197. struct drm_device *dev = crtc->dev;
  7198. struct drm_i915_private *dev_priv = dev->dev_private;
  7199. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7200. int pipe = intel_crtc->pipe;
  7201. uint16_t coeff = 0x7800; /* 1.0 */
  7202. /*
  7203. * TODO: Check what kind of values actually come out of the pipe
  7204. * with these coeff/postoff values and adjust to get the best
  7205. * accuracy. Perhaps we even need to take the bpc value into
  7206. * consideration.
  7207. */
  7208. if (intel_crtc->config->limited_color_range)
  7209. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7210. /*
  7211. * GY/GU and RY/RU should be the other way around according
  7212. * to BSpec, but reality doesn't agree. Just set them up in
  7213. * a way that results in the correct picture.
  7214. */
  7215. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7216. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7217. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7218. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7219. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7220. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7221. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7222. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7223. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7224. if (INTEL_INFO(dev)->gen > 6) {
  7225. uint16_t postoff = 0;
  7226. if (intel_crtc->config->limited_color_range)
  7227. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7228. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7229. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7230. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7231. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7232. } else {
  7233. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7234. if (intel_crtc->config->limited_color_range)
  7235. mode |= CSC_BLACK_SCREEN_OFFSET;
  7236. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7237. }
  7238. }
  7239. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7240. {
  7241. struct drm_device *dev = crtc->dev;
  7242. struct drm_i915_private *dev_priv = dev->dev_private;
  7243. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7244. enum pipe pipe = intel_crtc->pipe;
  7245. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7246. uint32_t val;
  7247. val = 0;
  7248. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7249. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7250. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7251. val |= PIPECONF_INTERLACED_ILK;
  7252. else
  7253. val |= PIPECONF_PROGRESSIVE;
  7254. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7255. POSTING_READ(PIPECONF(cpu_transcoder));
  7256. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7257. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7258. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7259. val = 0;
  7260. switch (intel_crtc->config->pipe_bpp) {
  7261. case 18:
  7262. val |= PIPEMISC_DITHER_6_BPC;
  7263. break;
  7264. case 24:
  7265. val |= PIPEMISC_DITHER_8_BPC;
  7266. break;
  7267. case 30:
  7268. val |= PIPEMISC_DITHER_10_BPC;
  7269. break;
  7270. case 36:
  7271. val |= PIPEMISC_DITHER_12_BPC;
  7272. break;
  7273. default:
  7274. /* Case prevented by pipe_config_set_bpp. */
  7275. BUG();
  7276. }
  7277. if (intel_crtc->config->dither)
  7278. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7279. I915_WRITE(PIPEMISC(pipe), val);
  7280. }
  7281. }
  7282. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7283. struct intel_crtc_state *crtc_state,
  7284. intel_clock_t *clock,
  7285. bool *has_reduced_clock,
  7286. intel_clock_t *reduced_clock)
  7287. {
  7288. struct drm_device *dev = crtc->dev;
  7289. struct drm_i915_private *dev_priv = dev->dev_private;
  7290. int refclk;
  7291. const intel_limit_t *limit;
  7292. bool ret;
  7293. refclk = ironlake_get_refclk(crtc_state);
  7294. /*
  7295. * Returns a set of divisors for the desired target clock with the given
  7296. * refclk, or FALSE. The returned values represent the clock equation:
  7297. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7298. */
  7299. limit = intel_limit(crtc_state, refclk);
  7300. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7301. crtc_state->port_clock,
  7302. refclk, NULL, clock);
  7303. if (!ret)
  7304. return false;
  7305. return true;
  7306. }
  7307. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7308. {
  7309. /*
  7310. * Account for spread spectrum to avoid
  7311. * oversubscribing the link. Max center spread
  7312. * is 2.5%; use 5% for safety's sake.
  7313. */
  7314. u32 bps = target_clock * bpp * 21 / 20;
  7315. return DIV_ROUND_UP(bps, link_bw * 8);
  7316. }
  7317. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7318. {
  7319. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7320. }
  7321. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7322. struct intel_crtc_state *crtc_state,
  7323. u32 *fp,
  7324. intel_clock_t *reduced_clock, u32 *fp2)
  7325. {
  7326. struct drm_crtc *crtc = &intel_crtc->base;
  7327. struct drm_device *dev = crtc->dev;
  7328. struct drm_i915_private *dev_priv = dev->dev_private;
  7329. struct drm_atomic_state *state = crtc_state->base.state;
  7330. struct drm_connector *connector;
  7331. struct drm_connector_state *connector_state;
  7332. struct intel_encoder *encoder;
  7333. uint32_t dpll;
  7334. int factor, num_connectors = 0, i;
  7335. bool is_lvds = false, is_sdvo = false;
  7336. for_each_connector_in_state(state, connector, connector_state, i) {
  7337. if (connector_state->crtc != crtc_state->base.crtc)
  7338. continue;
  7339. encoder = to_intel_encoder(connector_state->best_encoder);
  7340. switch (encoder->type) {
  7341. case INTEL_OUTPUT_LVDS:
  7342. is_lvds = true;
  7343. break;
  7344. case INTEL_OUTPUT_SDVO:
  7345. case INTEL_OUTPUT_HDMI:
  7346. is_sdvo = true;
  7347. break;
  7348. default:
  7349. break;
  7350. }
  7351. num_connectors++;
  7352. }
  7353. /* Enable autotuning of the PLL clock (if permissible) */
  7354. factor = 21;
  7355. if (is_lvds) {
  7356. if ((intel_panel_use_ssc(dev_priv) &&
  7357. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7358. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7359. factor = 25;
  7360. } else if (crtc_state->sdvo_tv_clock)
  7361. factor = 20;
  7362. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7363. *fp |= FP_CB_TUNE;
  7364. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7365. *fp2 |= FP_CB_TUNE;
  7366. dpll = 0;
  7367. if (is_lvds)
  7368. dpll |= DPLLB_MODE_LVDS;
  7369. else
  7370. dpll |= DPLLB_MODE_DAC_SERIAL;
  7371. dpll |= (crtc_state->pixel_multiplier - 1)
  7372. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7373. if (is_sdvo)
  7374. dpll |= DPLL_SDVO_HIGH_SPEED;
  7375. if (crtc_state->has_dp_encoder)
  7376. dpll |= DPLL_SDVO_HIGH_SPEED;
  7377. /* compute bitmask from p1 value */
  7378. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7379. /* also FPA1 */
  7380. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7381. switch (crtc_state->dpll.p2) {
  7382. case 5:
  7383. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7384. break;
  7385. case 7:
  7386. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7387. break;
  7388. case 10:
  7389. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7390. break;
  7391. case 14:
  7392. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7393. break;
  7394. }
  7395. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7396. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7397. else
  7398. dpll |= PLL_REF_INPUT_DREFCLK;
  7399. return dpll | DPLL_VCO_ENABLE;
  7400. }
  7401. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7402. struct intel_crtc_state *crtc_state)
  7403. {
  7404. struct drm_device *dev = crtc->base.dev;
  7405. intel_clock_t clock, reduced_clock;
  7406. u32 dpll = 0, fp = 0, fp2 = 0;
  7407. bool ok, has_reduced_clock = false;
  7408. bool is_lvds = false;
  7409. struct intel_shared_dpll *pll;
  7410. memset(&crtc_state->dpll_hw_state, 0,
  7411. sizeof(crtc_state->dpll_hw_state));
  7412. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7413. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7414. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7415. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7416. &has_reduced_clock, &reduced_clock);
  7417. if (!ok && !crtc_state->clock_set) {
  7418. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7419. return -EINVAL;
  7420. }
  7421. /* Compat-code for transition, will disappear. */
  7422. if (!crtc_state->clock_set) {
  7423. crtc_state->dpll.n = clock.n;
  7424. crtc_state->dpll.m1 = clock.m1;
  7425. crtc_state->dpll.m2 = clock.m2;
  7426. crtc_state->dpll.p1 = clock.p1;
  7427. crtc_state->dpll.p2 = clock.p2;
  7428. }
  7429. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7430. if (crtc_state->has_pch_encoder) {
  7431. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7432. if (has_reduced_clock)
  7433. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7434. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7435. &fp, &reduced_clock,
  7436. has_reduced_clock ? &fp2 : NULL);
  7437. crtc_state->dpll_hw_state.dpll = dpll;
  7438. crtc_state->dpll_hw_state.fp0 = fp;
  7439. if (has_reduced_clock)
  7440. crtc_state->dpll_hw_state.fp1 = fp2;
  7441. else
  7442. crtc_state->dpll_hw_state.fp1 = fp;
  7443. pll = intel_get_shared_dpll(crtc, crtc_state);
  7444. if (pll == NULL) {
  7445. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7446. pipe_name(crtc->pipe));
  7447. return -EINVAL;
  7448. }
  7449. }
  7450. if (is_lvds && has_reduced_clock)
  7451. crtc->lowfreq_avail = true;
  7452. else
  7453. crtc->lowfreq_avail = false;
  7454. return 0;
  7455. }
  7456. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7457. struct intel_link_m_n *m_n)
  7458. {
  7459. struct drm_device *dev = crtc->base.dev;
  7460. struct drm_i915_private *dev_priv = dev->dev_private;
  7461. enum pipe pipe = crtc->pipe;
  7462. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7463. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7464. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7465. & ~TU_SIZE_MASK;
  7466. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7467. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7468. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7469. }
  7470. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7471. enum transcoder transcoder,
  7472. struct intel_link_m_n *m_n,
  7473. struct intel_link_m_n *m2_n2)
  7474. {
  7475. struct drm_device *dev = crtc->base.dev;
  7476. struct drm_i915_private *dev_priv = dev->dev_private;
  7477. enum pipe pipe = crtc->pipe;
  7478. if (INTEL_INFO(dev)->gen >= 5) {
  7479. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7480. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7481. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7482. & ~TU_SIZE_MASK;
  7483. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7484. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7485. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7486. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7487. * gen < 8) and if DRRS is supported (to make sure the
  7488. * registers are not unnecessarily read).
  7489. */
  7490. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7491. crtc->config->has_drrs) {
  7492. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7493. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7494. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7495. & ~TU_SIZE_MASK;
  7496. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7497. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7498. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7499. }
  7500. } else {
  7501. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7502. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7503. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7504. & ~TU_SIZE_MASK;
  7505. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7506. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7507. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7508. }
  7509. }
  7510. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7511. struct intel_crtc_state *pipe_config)
  7512. {
  7513. if (pipe_config->has_pch_encoder)
  7514. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7515. else
  7516. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7517. &pipe_config->dp_m_n,
  7518. &pipe_config->dp_m2_n2);
  7519. }
  7520. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7521. struct intel_crtc_state *pipe_config)
  7522. {
  7523. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7524. &pipe_config->fdi_m_n, NULL);
  7525. }
  7526. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7527. struct intel_crtc_state *pipe_config)
  7528. {
  7529. struct drm_device *dev = crtc->base.dev;
  7530. struct drm_i915_private *dev_priv = dev->dev_private;
  7531. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7532. uint32_t ps_ctrl = 0;
  7533. int id = -1;
  7534. int i;
  7535. /* find scaler attached to this pipe */
  7536. for (i = 0; i < crtc->num_scalers; i++) {
  7537. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7538. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7539. id = i;
  7540. pipe_config->pch_pfit.enabled = true;
  7541. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7542. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7543. break;
  7544. }
  7545. }
  7546. scaler_state->scaler_id = id;
  7547. if (id >= 0) {
  7548. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7549. } else {
  7550. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7551. }
  7552. }
  7553. static void
  7554. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7555. struct intel_initial_plane_config *plane_config)
  7556. {
  7557. struct drm_device *dev = crtc->base.dev;
  7558. struct drm_i915_private *dev_priv = dev->dev_private;
  7559. u32 val, base, offset, stride_mult, tiling;
  7560. int pipe = crtc->pipe;
  7561. int fourcc, pixel_format;
  7562. unsigned int aligned_height;
  7563. struct drm_framebuffer *fb;
  7564. struct intel_framebuffer *intel_fb;
  7565. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7566. if (!intel_fb) {
  7567. DRM_DEBUG_KMS("failed to alloc fb\n");
  7568. return;
  7569. }
  7570. fb = &intel_fb->base;
  7571. val = I915_READ(PLANE_CTL(pipe, 0));
  7572. if (!(val & PLANE_CTL_ENABLE))
  7573. goto error;
  7574. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7575. fourcc = skl_format_to_fourcc(pixel_format,
  7576. val & PLANE_CTL_ORDER_RGBX,
  7577. val & PLANE_CTL_ALPHA_MASK);
  7578. fb->pixel_format = fourcc;
  7579. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7580. tiling = val & PLANE_CTL_TILED_MASK;
  7581. switch (tiling) {
  7582. case PLANE_CTL_TILED_LINEAR:
  7583. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7584. break;
  7585. case PLANE_CTL_TILED_X:
  7586. plane_config->tiling = I915_TILING_X;
  7587. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7588. break;
  7589. case PLANE_CTL_TILED_Y:
  7590. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7591. break;
  7592. case PLANE_CTL_TILED_YF:
  7593. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7594. break;
  7595. default:
  7596. MISSING_CASE(tiling);
  7597. goto error;
  7598. }
  7599. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7600. plane_config->base = base;
  7601. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7602. val = I915_READ(PLANE_SIZE(pipe, 0));
  7603. fb->height = ((val >> 16) & 0xfff) + 1;
  7604. fb->width = ((val >> 0) & 0x1fff) + 1;
  7605. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7606. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7607. fb->pixel_format);
  7608. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7609. aligned_height = intel_fb_align_height(dev, fb->height,
  7610. fb->pixel_format,
  7611. fb->modifier[0]);
  7612. plane_config->size = fb->pitches[0] * aligned_height;
  7613. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7614. pipe_name(pipe), fb->width, fb->height,
  7615. fb->bits_per_pixel, base, fb->pitches[0],
  7616. plane_config->size);
  7617. plane_config->fb = intel_fb;
  7618. return;
  7619. error:
  7620. kfree(fb);
  7621. }
  7622. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7623. struct intel_crtc_state *pipe_config)
  7624. {
  7625. struct drm_device *dev = crtc->base.dev;
  7626. struct drm_i915_private *dev_priv = dev->dev_private;
  7627. uint32_t tmp;
  7628. tmp = I915_READ(PF_CTL(crtc->pipe));
  7629. if (tmp & PF_ENABLE) {
  7630. pipe_config->pch_pfit.enabled = true;
  7631. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7632. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7633. /* We currently do not free assignements of panel fitters on
  7634. * ivb/hsw (since we don't use the higher upscaling modes which
  7635. * differentiates them) so just WARN about this case for now. */
  7636. if (IS_GEN7(dev)) {
  7637. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7638. PF_PIPE_SEL_IVB(crtc->pipe));
  7639. }
  7640. }
  7641. }
  7642. static void
  7643. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7644. struct intel_initial_plane_config *plane_config)
  7645. {
  7646. struct drm_device *dev = crtc->base.dev;
  7647. struct drm_i915_private *dev_priv = dev->dev_private;
  7648. u32 val, base, offset;
  7649. int pipe = crtc->pipe;
  7650. int fourcc, pixel_format;
  7651. unsigned int aligned_height;
  7652. struct drm_framebuffer *fb;
  7653. struct intel_framebuffer *intel_fb;
  7654. val = I915_READ(DSPCNTR(pipe));
  7655. if (!(val & DISPLAY_PLANE_ENABLE))
  7656. return;
  7657. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7658. if (!intel_fb) {
  7659. DRM_DEBUG_KMS("failed to alloc fb\n");
  7660. return;
  7661. }
  7662. fb = &intel_fb->base;
  7663. if (INTEL_INFO(dev)->gen >= 4) {
  7664. if (val & DISPPLANE_TILED) {
  7665. plane_config->tiling = I915_TILING_X;
  7666. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7667. }
  7668. }
  7669. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7670. fourcc = i9xx_format_to_fourcc(pixel_format);
  7671. fb->pixel_format = fourcc;
  7672. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7673. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7674. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7675. offset = I915_READ(DSPOFFSET(pipe));
  7676. } else {
  7677. if (plane_config->tiling)
  7678. offset = I915_READ(DSPTILEOFF(pipe));
  7679. else
  7680. offset = I915_READ(DSPLINOFF(pipe));
  7681. }
  7682. plane_config->base = base;
  7683. val = I915_READ(PIPESRC(pipe));
  7684. fb->width = ((val >> 16) & 0xfff) + 1;
  7685. fb->height = ((val >> 0) & 0xfff) + 1;
  7686. val = I915_READ(DSPSTRIDE(pipe));
  7687. fb->pitches[0] = val & 0xffffffc0;
  7688. aligned_height = intel_fb_align_height(dev, fb->height,
  7689. fb->pixel_format,
  7690. fb->modifier[0]);
  7691. plane_config->size = fb->pitches[0] * aligned_height;
  7692. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7693. pipe_name(pipe), fb->width, fb->height,
  7694. fb->bits_per_pixel, base, fb->pitches[0],
  7695. plane_config->size);
  7696. plane_config->fb = intel_fb;
  7697. }
  7698. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7699. struct intel_crtc_state *pipe_config)
  7700. {
  7701. struct drm_device *dev = crtc->base.dev;
  7702. struct drm_i915_private *dev_priv = dev->dev_private;
  7703. uint32_t tmp;
  7704. if (!intel_display_power_is_enabled(dev_priv,
  7705. POWER_DOMAIN_PIPE(crtc->pipe)))
  7706. return false;
  7707. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7708. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7709. tmp = I915_READ(PIPECONF(crtc->pipe));
  7710. if (!(tmp & PIPECONF_ENABLE))
  7711. return false;
  7712. switch (tmp & PIPECONF_BPC_MASK) {
  7713. case PIPECONF_6BPC:
  7714. pipe_config->pipe_bpp = 18;
  7715. break;
  7716. case PIPECONF_8BPC:
  7717. pipe_config->pipe_bpp = 24;
  7718. break;
  7719. case PIPECONF_10BPC:
  7720. pipe_config->pipe_bpp = 30;
  7721. break;
  7722. case PIPECONF_12BPC:
  7723. pipe_config->pipe_bpp = 36;
  7724. break;
  7725. default:
  7726. break;
  7727. }
  7728. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7729. pipe_config->limited_color_range = true;
  7730. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7731. struct intel_shared_dpll *pll;
  7732. pipe_config->has_pch_encoder = true;
  7733. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7734. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7735. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7736. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7737. if (HAS_PCH_IBX(dev_priv->dev)) {
  7738. pipe_config->shared_dpll =
  7739. (enum intel_dpll_id) crtc->pipe;
  7740. } else {
  7741. tmp = I915_READ(PCH_DPLL_SEL);
  7742. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7743. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7744. else
  7745. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7746. }
  7747. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7748. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7749. &pipe_config->dpll_hw_state));
  7750. tmp = pipe_config->dpll_hw_state.dpll;
  7751. pipe_config->pixel_multiplier =
  7752. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7753. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7754. ironlake_pch_clock_get(crtc, pipe_config);
  7755. } else {
  7756. pipe_config->pixel_multiplier = 1;
  7757. }
  7758. intel_get_pipe_timings(crtc, pipe_config);
  7759. ironlake_get_pfit_config(crtc, pipe_config);
  7760. return true;
  7761. }
  7762. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7763. {
  7764. struct drm_device *dev = dev_priv->dev;
  7765. struct intel_crtc *crtc;
  7766. for_each_intel_crtc(dev, crtc)
  7767. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7768. pipe_name(crtc->pipe));
  7769. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7770. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7771. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7772. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7773. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7774. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7775. "CPU PWM1 enabled\n");
  7776. if (IS_HASWELL(dev))
  7777. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7778. "CPU PWM2 enabled\n");
  7779. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7780. "PCH PWM1 enabled\n");
  7781. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7782. "Utility pin enabled\n");
  7783. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7784. /*
  7785. * In theory we can still leave IRQs enabled, as long as only the HPD
  7786. * interrupts remain enabled. We used to check for that, but since it's
  7787. * gen-specific and since we only disable LCPLL after we fully disable
  7788. * the interrupts, the check below should be enough.
  7789. */
  7790. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7791. }
  7792. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7793. {
  7794. struct drm_device *dev = dev_priv->dev;
  7795. if (IS_HASWELL(dev))
  7796. return I915_READ(D_COMP_HSW);
  7797. else
  7798. return I915_READ(D_COMP_BDW);
  7799. }
  7800. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7801. {
  7802. struct drm_device *dev = dev_priv->dev;
  7803. if (IS_HASWELL(dev)) {
  7804. mutex_lock(&dev_priv->rps.hw_lock);
  7805. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7806. val))
  7807. DRM_ERROR("Failed to write to D_COMP\n");
  7808. mutex_unlock(&dev_priv->rps.hw_lock);
  7809. } else {
  7810. I915_WRITE(D_COMP_BDW, val);
  7811. POSTING_READ(D_COMP_BDW);
  7812. }
  7813. }
  7814. /*
  7815. * This function implements pieces of two sequences from BSpec:
  7816. * - Sequence for display software to disable LCPLL
  7817. * - Sequence for display software to allow package C8+
  7818. * The steps implemented here are just the steps that actually touch the LCPLL
  7819. * register. Callers should take care of disabling all the display engine
  7820. * functions, doing the mode unset, fixing interrupts, etc.
  7821. */
  7822. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7823. bool switch_to_fclk, bool allow_power_down)
  7824. {
  7825. uint32_t val;
  7826. assert_can_disable_lcpll(dev_priv);
  7827. val = I915_READ(LCPLL_CTL);
  7828. if (switch_to_fclk) {
  7829. val |= LCPLL_CD_SOURCE_FCLK;
  7830. I915_WRITE(LCPLL_CTL, val);
  7831. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7832. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7833. DRM_ERROR("Switching to FCLK failed\n");
  7834. val = I915_READ(LCPLL_CTL);
  7835. }
  7836. val |= LCPLL_PLL_DISABLE;
  7837. I915_WRITE(LCPLL_CTL, val);
  7838. POSTING_READ(LCPLL_CTL);
  7839. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7840. DRM_ERROR("LCPLL still locked\n");
  7841. val = hsw_read_dcomp(dev_priv);
  7842. val |= D_COMP_COMP_DISABLE;
  7843. hsw_write_dcomp(dev_priv, val);
  7844. ndelay(100);
  7845. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7846. 1))
  7847. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7848. if (allow_power_down) {
  7849. val = I915_READ(LCPLL_CTL);
  7850. val |= LCPLL_POWER_DOWN_ALLOW;
  7851. I915_WRITE(LCPLL_CTL, val);
  7852. POSTING_READ(LCPLL_CTL);
  7853. }
  7854. }
  7855. /*
  7856. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7857. * source.
  7858. */
  7859. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7860. {
  7861. uint32_t val;
  7862. val = I915_READ(LCPLL_CTL);
  7863. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7864. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7865. return;
  7866. /*
  7867. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7868. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7869. */
  7870. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7871. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7872. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7873. I915_WRITE(LCPLL_CTL, val);
  7874. POSTING_READ(LCPLL_CTL);
  7875. }
  7876. val = hsw_read_dcomp(dev_priv);
  7877. val |= D_COMP_COMP_FORCE;
  7878. val &= ~D_COMP_COMP_DISABLE;
  7879. hsw_write_dcomp(dev_priv, val);
  7880. val = I915_READ(LCPLL_CTL);
  7881. val &= ~LCPLL_PLL_DISABLE;
  7882. I915_WRITE(LCPLL_CTL, val);
  7883. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7884. DRM_ERROR("LCPLL not locked yet\n");
  7885. if (val & LCPLL_CD_SOURCE_FCLK) {
  7886. val = I915_READ(LCPLL_CTL);
  7887. val &= ~LCPLL_CD_SOURCE_FCLK;
  7888. I915_WRITE(LCPLL_CTL, val);
  7889. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7890. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7891. DRM_ERROR("Switching back to LCPLL failed\n");
  7892. }
  7893. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7894. intel_update_cdclk(dev_priv->dev);
  7895. }
  7896. /*
  7897. * Package states C8 and deeper are really deep PC states that can only be
  7898. * reached when all the devices on the system allow it, so even if the graphics
  7899. * device allows PC8+, it doesn't mean the system will actually get to these
  7900. * states. Our driver only allows PC8+ when going into runtime PM.
  7901. *
  7902. * The requirements for PC8+ are that all the outputs are disabled, the power
  7903. * well is disabled and most interrupts are disabled, and these are also
  7904. * requirements for runtime PM. When these conditions are met, we manually do
  7905. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7906. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7907. * hang the machine.
  7908. *
  7909. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7910. * the state of some registers, so when we come back from PC8+ we need to
  7911. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7912. * need to take care of the registers kept by RC6. Notice that this happens even
  7913. * if we don't put the device in PCI D3 state (which is what currently happens
  7914. * because of the runtime PM support).
  7915. *
  7916. * For more, read "Display Sequences for Package C8" on the hardware
  7917. * documentation.
  7918. */
  7919. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7920. {
  7921. struct drm_device *dev = dev_priv->dev;
  7922. uint32_t val;
  7923. DRM_DEBUG_KMS("Enabling package C8+\n");
  7924. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7925. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7926. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7927. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7928. }
  7929. lpt_disable_clkout_dp(dev);
  7930. hsw_disable_lcpll(dev_priv, true, true);
  7931. }
  7932. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7933. {
  7934. struct drm_device *dev = dev_priv->dev;
  7935. uint32_t val;
  7936. DRM_DEBUG_KMS("Disabling package C8+\n");
  7937. hsw_restore_lcpll(dev_priv);
  7938. lpt_init_pch_refclk(dev);
  7939. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7940. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7941. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7942. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7943. }
  7944. intel_prepare_ddi(dev);
  7945. }
  7946. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7947. {
  7948. struct drm_device *dev = old_state->dev;
  7949. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7950. broxton_set_cdclk(dev, req_cdclk);
  7951. }
  7952. /* compute the max rate for new configuration */
  7953. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7954. {
  7955. struct intel_crtc *intel_crtc;
  7956. struct intel_crtc_state *crtc_state;
  7957. int max_pixel_rate = 0;
  7958. for_each_intel_crtc(state->dev, intel_crtc) {
  7959. int pixel_rate;
  7960. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7961. if (IS_ERR(crtc_state))
  7962. return PTR_ERR(crtc_state);
  7963. if (!crtc_state->base.enable)
  7964. continue;
  7965. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7966. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7967. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7968. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7969. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7970. }
  7971. return max_pixel_rate;
  7972. }
  7973. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7974. {
  7975. struct drm_i915_private *dev_priv = dev->dev_private;
  7976. uint32_t val, data;
  7977. int ret;
  7978. if (WARN((I915_READ(LCPLL_CTL) &
  7979. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7980. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  7981. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  7982. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  7983. "trying to change cdclk frequency with cdclk not enabled\n"))
  7984. return;
  7985. mutex_lock(&dev_priv->rps.hw_lock);
  7986. ret = sandybridge_pcode_write(dev_priv,
  7987. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  7988. mutex_unlock(&dev_priv->rps.hw_lock);
  7989. if (ret) {
  7990. DRM_ERROR("failed to inform pcode about cdclk change\n");
  7991. return;
  7992. }
  7993. val = I915_READ(LCPLL_CTL);
  7994. val |= LCPLL_CD_SOURCE_FCLK;
  7995. I915_WRITE(LCPLL_CTL, val);
  7996. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7997. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7998. DRM_ERROR("Switching to FCLK failed\n");
  7999. val = I915_READ(LCPLL_CTL);
  8000. val &= ~LCPLL_CLK_FREQ_MASK;
  8001. switch (cdclk) {
  8002. case 450000:
  8003. val |= LCPLL_CLK_FREQ_450;
  8004. data = 0;
  8005. break;
  8006. case 540000:
  8007. val |= LCPLL_CLK_FREQ_54O_BDW;
  8008. data = 1;
  8009. break;
  8010. case 337500:
  8011. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8012. data = 2;
  8013. break;
  8014. case 675000:
  8015. val |= LCPLL_CLK_FREQ_675_BDW;
  8016. data = 3;
  8017. break;
  8018. default:
  8019. WARN(1, "invalid cdclk frequency\n");
  8020. return;
  8021. }
  8022. I915_WRITE(LCPLL_CTL, val);
  8023. val = I915_READ(LCPLL_CTL);
  8024. val &= ~LCPLL_CD_SOURCE_FCLK;
  8025. I915_WRITE(LCPLL_CTL, val);
  8026. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8027. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8028. DRM_ERROR("Switching back to LCPLL failed\n");
  8029. mutex_lock(&dev_priv->rps.hw_lock);
  8030. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8031. mutex_unlock(&dev_priv->rps.hw_lock);
  8032. intel_update_cdclk(dev);
  8033. WARN(cdclk != dev_priv->cdclk_freq,
  8034. "cdclk requested %d kHz but got %d kHz\n",
  8035. cdclk, dev_priv->cdclk_freq);
  8036. }
  8037. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8038. {
  8039. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8040. int max_pixclk = ilk_max_pixel_rate(state);
  8041. int cdclk;
  8042. /*
  8043. * FIXME should also account for plane ratio
  8044. * once 64bpp pixel formats are supported.
  8045. */
  8046. if (max_pixclk > 540000)
  8047. cdclk = 675000;
  8048. else if (max_pixclk > 450000)
  8049. cdclk = 540000;
  8050. else if (max_pixclk > 337500)
  8051. cdclk = 450000;
  8052. else
  8053. cdclk = 337500;
  8054. /*
  8055. * FIXME move the cdclk caclulation to
  8056. * compute_config() so we can fail gracegully.
  8057. */
  8058. if (cdclk > dev_priv->max_cdclk_freq) {
  8059. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8060. cdclk, dev_priv->max_cdclk_freq);
  8061. cdclk = dev_priv->max_cdclk_freq;
  8062. }
  8063. to_intel_atomic_state(state)->cdclk = cdclk;
  8064. return 0;
  8065. }
  8066. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8067. {
  8068. struct drm_device *dev = old_state->dev;
  8069. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8070. broadwell_set_cdclk(dev, req_cdclk);
  8071. }
  8072. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8073. struct intel_crtc_state *crtc_state)
  8074. {
  8075. if (!intel_ddi_pll_select(crtc, crtc_state))
  8076. return -EINVAL;
  8077. crtc->lowfreq_avail = false;
  8078. return 0;
  8079. }
  8080. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8081. enum port port,
  8082. struct intel_crtc_state *pipe_config)
  8083. {
  8084. switch (port) {
  8085. case PORT_A:
  8086. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8087. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8088. break;
  8089. case PORT_B:
  8090. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8091. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8092. break;
  8093. case PORT_C:
  8094. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8095. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8096. break;
  8097. default:
  8098. DRM_ERROR("Incorrect port type\n");
  8099. }
  8100. }
  8101. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8102. enum port port,
  8103. struct intel_crtc_state *pipe_config)
  8104. {
  8105. u32 temp, dpll_ctl1;
  8106. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8107. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8108. switch (pipe_config->ddi_pll_sel) {
  8109. case SKL_DPLL0:
  8110. /*
  8111. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8112. * of the shared DPLL framework and thus needs to be read out
  8113. * separately
  8114. */
  8115. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8116. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8117. break;
  8118. case SKL_DPLL1:
  8119. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8120. break;
  8121. case SKL_DPLL2:
  8122. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8123. break;
  8124. case SKL_DPLL3:
  8125. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8126. break;
  8127. }
  8128. }
  8129. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8130. enum port port,
  8131. struct intel_crtc_state *pipe_config)
  8132. {
  8133. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8134. switch (pipe_config->ddi_pll_sel) {
  8135. case PORT_CLK_SEL_WRPLL1:
  8136. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8137. break;
  8138. case PORT_CLK_SEL_WRPLL2:
  8139. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8140. break;
  8141. }
  8142. }
  8143. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8144. struct intel_crtc_state *pipe_config)
  8145. {
  8146. struct drm_device *dev = crtc->base.dev;
  8147. struct drm_i915_private *dev_priv = dev->dev_private;
  8148. struct intel_shared_dpll *pll;
  8149. enum port port;
  8150. uint32_t tmp;
  8151. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8152. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8153. if (IS_SKYLAKE(dev))
  8154. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8155. else if (IS_BROXTON(dev))
  8156. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8157. else
  8158. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8159. if (pipe_config->shared_dpll >= 0) {
  8160. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8161. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8162. &pipe_config->dpll_hw_state));
  8163. }
  8164. /*
  8165. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8166. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8167. * the PCH transcoder is on.
  8168. */
  8169. if (INTEL_INFO(dev)->gen < 9 &&
  8170. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8171. pipe_config->has_pch_encoder = true;
  8172. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8173. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8174. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8175. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8176. }
  8177. }
  8178. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8179. struct intel_crtc_state *pipe_config)
  8180. {
  8181. struct drm_device *dev = crtc->base.dev;
  8182. struct drm_i915_private *dev_priv = dev->dev_private;
  8183. enum intel_display_power_domain pfit_domain;
  8184. uint32_t tmp;
  8185. if (!intel_display_power_is_enabled(dev_priv,
  8186. POWER_DOMAIN_PIPE(crtc->pipe)))
  8187. return false;
  8188. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8189. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8190. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8191. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8192. enum pipe trans_edp_pipe;
  8193. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8194. default:
  8195. WARN(1, "unknown pipe linked to edp transcoder\n");
  8196. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8197. case TRANS_DDI_EDP_INPUT_A_ON:
  8198. trans_edp_pipe = PIPE_A;
  8199. break;
  8200. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8201. trans_edp_pipe = PIPE_B;
  8202. break;
  8203. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8204. trans_edp_pipe = PIPE_C;
  8205. break;
  8206. }
  8207. if (trans_edp_pipe == crtc->pipe)
  8208. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8209. }
  8210. if (!intel_display_power_is_enabled(dev_priv,
  8211. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8212. return false;
  8213. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8214. if (!(tmp & PIPECONF_ENABLE))
  8215. return false;
  8216. haswell_get_ddi_port_state(crtc, pipe_config);
  8217. intel_get_pipe_timings(crtc, pipe_config);
  8218. if (INTEL_INFO(dev)->gen >= 9) {
  8219. skl_init_scalers(dev, crtc, pipe_config);
  8220. }
  8221. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8222. if (INTEL_INFO(dev)->gen >= 9) {
  8223. pipe_config->scaler_state.scaler_id = -1;
  8224. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8225. }
  8226. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8227. if (INTEL_INFO(dev)->gen == 9)
  8228. skylake_get_pfit_config(crtc, pipe_config);
  8229. else if (INTEL_INFO(dev)->gen < 9)
  8230. ironlake_get_pfit_config(crtc, pipe_config);
  8231. else
  8232. MISSING_CASE(INTEL_INFO(dev)->gen);
  8233. }
  8234. if (IS_HASWELL(dev))
  8235. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8236. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8237. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8238. pipe_config->pixel_multiplier =
  8239. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8240. } else {
  8241. pipe_config->pixel_multiplier = 1;
  8242. }
  8243. return true;
  8244. }
  8245. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8246. {
  8247. struct drm_device *dev = crtc->dev;
  8248. struct drm_i915_private *dev_priv = dev->dev_private;
  8249. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8250. uint32_t cntl = 0, size = 0;
  8251. if (base) {
  8252. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8253. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8254. unsigned int stride = roundup_pow_of_two(width) * 4;
  8255. switch (stride) {
  8256. default:
  8257. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8258. width, stride);
  8259. stride = 256;
  8260. /* fallthrough */
  8261. case 256:
  8262. case 512:
  8263. case 1024:
  8264. case 2048:
  8265. break;
  8266. }
  8267. cntl |= CURSOR_ENABLE |
  8268. CURSOR_GAMMA_ENABLE |
  8269. CURSOR_FORMAT_ARGB |
  8270. CURSOR_STRIDE(stride);
  8271. size = (height << 12) | width;
  8272. }
  8273. if (intel_crtc->cursor_cntl != 0 &&
  8274. (intel_crtc->cursor_base != base ||
  8275. intel_crtc->cursor_size != size ||
  8276. intel_crtc->cursor_cntl != cntl)) {
  8277. /* On these chipsets we can only modify the base/size/stride
  8278. * whilst the cursor is disabled.
  8279. */
  8280. I915_WRITE(_CURACNTR, 0);
  8281. POSTING_READ(_CURACNTR);
  8282. intel_crtc->cursor_cntl = 0;
  8283. }
  8284. if (intel_crtc->cursor_base != base) {
  8285. I915_WRITE(_CURABASE, base);
  8286. intel_crtc->cursor_base = base;
  8287. }
  8288. if (intel_crtc->cursor_size != size) {
  8289. I915_WRITE(CURSIZE, size);
  8290. intel_crtc->cursor_size = size;
  8291. }
  8292. if (intel_crtc->cursor_cntl != cntl) {
  8293. I915_WRITE(_CURACNTR, cntl);
  8294. POSTING_READ(_CURACNTR);
  8295. intel_crtc->cursor_cntl = cntl;
  8296. }
  8297. }
  8298. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8299. {
  8300. struct drm_device *dev = crtc->dev;
  8301. struct drm_i915_private *dev_priv = dev->dev_private;
  8302. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8303. int pipe = intel_crtc->pipe;
  8304. uint32_t cntl;
  8305. cntl = 0;
  8306. if (base) {
  8307. cntl = MCURSOR_GAMMA_ENABLE;
  8308. switch (intel_crtc->base.cursor->state->crtc_w) {
  8309. case 64:
  8310. cntl |= CURSOR_MODE_64_ARGB_AX;
  8311. break;
  8312. case 128:
  8313. cntl |= CURSOR_MODE_128_ARGB_AX;
  8314. break;
  8315. case 256:
  8316. cntl |= CURSOR_MODE_256_ARGB_AX;
  8317. break;
  8318. default:
  8319. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8320. return;
  8321. }
  8322. cntl |= pipe << 28; /* Connect to correct pipe */
  8323. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8324. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8325. }
  8326. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8327. cntl |= CURSOR_ROTATE_180;
  8328. if (intel_crtc->cursor_cntl != cntl) {
  8329. I915_WRITE(CURCNTR(pipe), cntl);
  8330. POSTING_READ(CURCNTR(pipe));
  8331. intel_crtc->cursor_cntl = cntl;
  8332. }
  8333. /* and commit changes on next vblank */
  8334. I915_WRITE(CURBASE(pipe), base);
  8335. POSTING_READ(CURBASE(pipe));
  8336. intel_crtc->cursor_base = base;
  8337. }
  8338. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8339. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8340. bool on)
  8341. {
  8342. struct drm_device *dev = crtc->dev;
  8343. struct drm_i915_private *dev_priv = dev->dev_private;
  8344. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8345. int pipe = intel_crtc->pipe;
  8346. int x = crtc->cursor_x;
  8347. int y = crtc->cursor_y;
  8348. u32 base = 0, pos = 0;
  8349. if (on)
  8350. base = intel_crtc->cursor_addr;
  8351. if (x >= intel_crtc->config->pipe_src_w)
  8352. base = 0;
  8353. if (y >= intel_crtc->config->pipe_src_h)
  8354. base = 0;
  8355. if (x < 0) {
  8356. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8357. base = 0;
  8358. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8359. x = -x;
  8360. }
  8361. pos |= x << CURSOR_X_SHIFT;
  8362. if (y < 0) {
  8363. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8364. base = 0;
  8365. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8366. y = -y;
  8367. }
  8368. pos |= y << CURSOR_Y_SHIFT;
  8369. if (base == 0 && intel_crtc->cursor_base == 0)
  8370. return;
  8371. I915_WRITE(CURPOS(pipe), pos);
  8372. /* ILK+ do this automagically */
  8373. if (HAS_GMCH_DISPLAY(dev) &&
  8374. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8375. base += (intel_crtc->base.cursor->state->crtc_h *
  8376. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8377. }
  8378. if (IS_845G(dev) || IS_I865G(dev))
  8379. i845_update_cursor(crtc, base);
  8380. else
  8381. i9xx_update_cursor(crtc, base);
  8382. }
  8383. static bool cursor_size_ok(struct drm_device *dev,
  8384. uint32_t width, uint32_t height)
  8385. {
  8386. if (width == 0 || height == 0)
  8387. return false;
  8388. /*
  8389. * 845g/865g are special in that they are only limited by
  8390. * the width of their cursors, the height is arbitrary up to
  8391. * the precision of the register. Everything else requires
  8392. * square cursors, limited to a few power-of-two sizes.
  8393. */
  8394. if (IS_845G(dev) || IS_I865G(dev)) {
  8395. if ((width & 63) != 0)
  8396. return false;
  8397. if (width > (IS_845G(dev) ? 64 : 512))
  8398. return false;
  8399. if (height > 1023)
  8400. return false;
  8401. } else {
  8402. switch (width | height) {
  8403. case 256:
  8404. case 128:
  8405. if (IS_GEN2(dev))
  8406. return false;
  8407. case 64:
  8408. break;
  8409. default:
  8410. return false;
  8411. }
  8412. }
  8413. return true;
  8414. }
  8415. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8416. u16 *blue, uint32_t start, uint32_t size)
  8417. {
  8418. int end = (start + size > 256) ? 256 : start + size, i;
  8419. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8420. for (i = start; i < end; i++) {
  8421. intel_crtc->lut_r[i] = red[i] >> 8;
  8422. intel_crtc->lut_g[i] = green[i] >> 8;
  8423. intel_crtc->lut_b[i] = blue[i] >> 8;
  8424. }
  8425. intel_crtc_load_lut(crtc);
  8426. }
  8427. /* VESA 640x480x72Hz mode to set on the pipe */
  8428. static struct drm_display_mode load_detect_mode = {
  8429. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8430. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8431. };
  8432. struct drm_framebuffer *
  8433. __intel_framebuffer_create(struct drm_device *dev,
  8434. struct drm_mode_fb_cmd2 *mode_cmd,
  8435. struct drm_i915_gem_object *obj)
  8436. {
  8437. struct intel_framebuffer *intel_fb;
  8438. int ret;
  8439. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8440. if (!intel_fb) {
  8441. drm_gem_object_unreference(&obj->base);
  8442. return ERR_PTR(-ENOMEM);
  8443. }
  8444. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8445. if (ret)
  8446. goto err;
  8447. return &intel_fb->base;
  8448. err:
  8449. drm_gem_object_unreference(&obj->base);
  8450. kfree(intel_fb);
  8451. return ERR_PTR(ret);
  8452. }
  8453. static struct drm_framebuffer *
  8454. intel_framebuffer_create(struct drm_device *dev,
  8455. struct drm_mode_fb_cmd2 *mode_cmd,
  8456. struct drm_i915_gem_object *obj)
  8457. {
  8458. struct drm_framebuffer *fb;
  8459. int ret;
  8460. ret = i915_mutex_lock_interruptible(dev);
  8461. if (ret)
  8462. return ERR_PTR(ret);
  8463. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8464. mutex_unlock(&dev->struct_mutex);
  8465. return fb;
  8466. }
  8467. static u32
  8468. intel_framebuffer_pitch_for_width(int width, int bpp)
  8469. {
  8470. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8471. return ALIGN(pitch, 64);
  8472. }
  8473. static u32
  8474. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8475. {
  8476. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8477. return PAGE_ALIGN(pitch * mode->vdisplay);
  8478. }
  8479. static struct drm_framebuffer *
  8480. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8481. struct drm_display_mode *mode,
  8482. int depth, int bpp)
  8483. {
  8484. struct drm_i915_gem_object *obj;
  8485. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8486. obj = i915_gem_alloc_object(dev,
  8487. intel_framebuffer_size_for_mode(mode, bpp));
  8488. if (obj == NULL)
  8489. return ERR_PTR(-ENOMEM);
  8490. mode_cmd.width = mode->hdisplay;
  8491. mode_cmd.height = mode->vdisplay;
  8492. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8493. bpp);
  8494. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8495. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8496. }
  8497. static struct drm_framebuffer *
  8498. mode_fits_in_fbdev(struct drm_device *dev,
  8499. struct drm_display_mode *mode)
  8500. {
  8501. #ifdef CONFIG_DRM_I915_FBDEV
  8502. struct drm_i915_private *dev_priv = dev->dev_private;
  8503. struct drm_i915_gem_object *obj;
  8504. struct drm_framebuffer *fb;
  8505. if (!dev_priv->fbdev)
  8506. return NULL;
  8507. if (!dev_priv->fbdev->fb)
  8508. return NULL;
  8509. obj = dev_priv->fbdev->fb->obj;
  8510. BUG_ON(!obj);
  8511. fb = &dev_priv->fbdev->fb->base;
  8512. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8513. fb->bits_per_pixel))
  8514. return NULL;
  8515. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8516. return NULL;
  8517. return fb;
  8518. #else
  8519. return NULL;
  8520. #endif
  8521. }
  8522. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8523. struct drm_crtc *crtc,
  8524. struct drm_display_mode *mode,
  8525. struct drm_framebuffer *fb,
  8526. int x, int y)
  8527. {
  8528. struct drm_plane_state *plane_state;
  8529. int hdisplay, vdisplay;
  8530. int ret;
  8531. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8532. if (IS_ERR(plane_state))
  8533. return PTR_ERR(plane_state);
  8534. if (mode)
  8535. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8536. else
  8537. hdisplay = vdisplay = 0;
  8538. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8539. if (ret)
  8540. return ret;
  8541. drm_atomic_set_fb_for_plane(plane_state, fb);
  8542. plane_state->crtc_x = 0;
  8543. plane_state->crtc_y = 0;
  8544. plane_state->crtc_w = hdisplay;
  8545. plane_state->crtc_h = vdisplay;
  8546. plane_state->src_x = x << 16;
  8547. plane_state->src_y = y << 16;
  8548. plane_state->src_w = hdisplay << 16;
  8549. plane_state->src_h = vdisplay << 16;
  8550. return 0;
  8551. }
  8552. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8553. struct drm_display_mode *mode,
  8554. struct intel_load_detect_pipe *old,
  8555. struct drm_modeset_acquire_ctx *ctx)
  8556. {
  8557. struct intel_crtc *intel_crtc;
  8558. struct intel_encoder *intel_encoder =
  8559. intel_attached_encoder(connector);
  8560. struct drm_crtc *possible_crtc;
  8561. struct drm_encoder *encoder = &intel_encoder->base;
  8562. struct drm_crtc *crtc = NULL;
  8563. struct drm_device *dev = encoder->dev;
  8564. struct drm_framebuffer *fb;
  8565. struct drm_mode_config *config = &dev->mode_config;
  8566. struct drm_atomic_state *state = NULL;
  8567. struct drm_connector_state *connector_state;
  8568. struct intel_crtc_state *crtc_state;
  8569. int ret, i = -1;
  8570. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8571. connector->base.id, connector->name,
  8572. encoder->base.id, encoder->name);
  8573. retry:
  8574. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8575. if (ret)
  8576. goto fail;
  8577. /*
  8578. * Algorithm gets a little messy:
  8579. *
  8580. * - if the connector already has an assigned crtc, use it (but make
  8581. * sure it's on first)
  8582. *
  8583. * - try to find the first unused crtc that can drive this connector,
  8584. * and use that if we find one
  8585. */
  8586. /* See if we already have a CRTC for this connector */
  8587. if (encoder->crtc) {
  8588. crtc = encoder->crtc;
  8589. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8590. if (ret)
  8591. goto fail;
  8592. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8593. if (ret)
  8594. goto fail;
  8595. old->dpms_mode = connector->dpms;
  8596. old->load_detect_temp = false;
  8597. /* Make sure the crtc and connector are running */
  8598. if (connector->dpms != DRM_MODE_DPMS_ON)
  8599. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8600. return true;
  8601. }
  8602. /* Find an unused one (if possible) */
  8603. for_each_crtc(dev, possible_crtc) {
  8604. i++;
  8605. if (!(encoder->possible_crtcs & (1 << i)))
  8606. continue;
  8607. if (possible_crtc->state->enable)
  8608. continue;
  8609. crtc = possible_crtc;
  8610. break;
  8611. }
  8612. /*
  8613. * If we didn't find an unused CRTC, don't use any.
  8614. */
  8615. if (!crtc) {
  8616. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8617. goto fail;
  8618. }
  8619. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8620. if (ret)
  8621. goto fail;
  8622. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8623. if (ret)
  8624. goto fail;
  8625. intel_crtc = to_intel_crtc(crtc);
  8626. old->dpms_mode = connector->dpms;
  8627. old->load_detect_temp = true;
  8628. old->release_fb = NULL;
  8629. state = drm_atomic_state_alloc(dev);
  8630. if (!state)
  8631. return false;
  8632. state->acquire_ctx = ctx;
  8633. connector_state = drm_atomic_get_connector_state(state, connector);
  8634. if (IS_ERR(connector_state)) {
  8635. ret = PTR_ERR(connector_state);
  8636. goto fail;
  8637. }
  8638. connector_state->crtc = crtc;
  8639. connector_state->best_encoder = &intel_encoder->base;
  8640. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8641. if (IS_ERR(crtc_state)) {
  8642. ret = PTR_ERR(crtc_state);
  8643. goto fail;
  8644. }
  8645. crtc_state->base.active = crtc_state->base.enable = true;
  8646. if (!mode)
  8647. mode = &load_detect_mode;
  8648. /* We need a framebuffer large enough to accommodate all accesses
  8649. * that the plane may generate whilst we perform load detection.
  8650. * We can not rely on the fbcon either being present (we get called
  8651. * during its initialisation to detect all boot displays, or it may
  8652. * not even exist) or that it is large enough to satisfy the
  8653. * requested mode.
  8654. */
  8655. fb = mode_fits_in_fbdev(dev, mode);
  8656. if (fb == NULL) {
  8657. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8658. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8659. old->release_fb = fb;
  8660. } else
  8661. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8662. if (IS_ERR(fb)) {
  8663. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8664. goto fail;
  8665. }
  8666. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8667. if (ret)
  8668. goto fail;
  8669. drm_mode_copy(&crtc_state->base.mode, mode);
  8670. if (drm_atomic_commit(state)) {
  8671. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8672. if (old->release_fb)
  8673. old->release_fb->funcs->destroy(old->release_fb);
  8674. goto fail;
  8675. }
  8676. crtc->primary->crtc = crtc;
  8677. /* let the connector get through one full cycle before testing */
  8678. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8679. return true;
  8680. fail:
  8681. drm_atomic_state_free(state);
  8682. state = NULL;
  8683. if (ret == -EDEADLK) {
  8684. drm_modeset_backoff(ctx);
  8685. goto retry;
  8686. }
  8687. return false;
  8688. }
  8689. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8690. struct intel_load_detect_pipe *old,
  8691. struct drm_modeset_acquire_ctx *ctx)
  8692. {
  8693. struct drm_device *dev = connector->dev;
  8694. struct intel_encoder *intel_encoder =
  8695. intel_attached_encoder(connector);
  8696. struct drm_encoder *encoder = &intel_encoder->base;
  8697. struct drm_crtc *crtc = encoder->crtc;
  8698. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8699. struct drm_atomic_state *state;
  8700. struct drm_connector_state *connector_state;
  8701. struct intel_crtc_state *crtc_state;
  8702. int ret;
  8703. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8704. connector->base.id, connector->name,
  8705. encoder->base.id, encoder->name);
  8706. if (old->load_detect_temp) {
  8707. state = drm_atomic_state_alloc(dev);
  8708. if (!state)
  8709. goto fail;
  8710. state->acquire_ctx = ctx;
  8711. connector_state = drm_atomic_get_connector_state(state, connector);
  8712. if (IS_ERR(connector_state))
  8713. goto fail;
  8714. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8715. if (IS_ERR(crtc_state))
  8716. goto fail;
  8717. connector_state->best_encoder = NULL;
  8718. connector_state->crtc = NULL;
  8719. crtc_state->base.enable = crtc_state->base.active = false;
  8720. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8721. 0, 0);
  8722. if (ret)
  8723. goto fail;
  8724. ret = drm_atomic_commit(state);
  8725. if (ret)
  8726. goto fail;
  8727. if (old->release_fb) {
  8728. drm_framebuffer_unregister_private(old->release_fb);
  8729. drm_framebuffer_unreference(old->release_fb);
  8730. }
  8731. return;
  8732. }
  8733. /* Switch crtc and encoder back off if necessary */
  8734. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8735. connector->funcs->dpms(connector, old->dpms_mode);
  8736. return;
  8737. fail:
  8738. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8739. drm_atomic_state_free(state);
  8740. }
  8741. static int i9xx_pll_refclk(struct drm_device *dev,
  8742. const struct intel_crtc_state *pipe_config)
  8743. {
  8744. struct drm_i915_private *dev_priv = dev->dev_private;
  8745. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8746. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8747. return dev_priv->vbt.lvds_ssc_freq;
  8748. else if (HAS_PCH_SPLIT(dev))
  8749. return 120000;
  8750. else if (!IS_GEN2(dev))
  8751. return 96000;
  8752. else
  8753. return 48000;
  8754. }
  8755. /* Returns the clock of the currently programmed mode of the given pipe. */
  8756. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8757. struct intel_crtc_state *pipe_config)
  8758. {
  8759. struct drm_device *dev = crtc->base.dev;
  8760. struct drm_i915_private *dev_priv = dev->dev_private;
  8761. int pipe = pipe_config->cpu_transcoder;
  8762. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8763. u32 fp;
  8764. intel_clock_t clock;
  8765. int port_clock;
  8766. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8767. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8768. fp = pipe_config->dpll_hw_state.fp0;
  8769. else
  8770. fp = pipe_config->dpll_hw_state.fp1;
  8771. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8772. if (IS_PINEVIEW(dev)) {
  8773. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8774. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8775. } else {
  8776. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8777. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8778. }
  8779. if (!IS_GEN2(dev)) {
  8780. if (IS_PINEVIEW(dev))
  8781. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8782. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8783. else
  8784. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8785. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8786. switch (dpll & DPLL_MODE_MASK) {
  8787. case DPLLB_MODE_DAC_SERIAL:
  8788. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8789. 5 : 10;
  8790. break;
  8791. case DPLLB_MODE_LVDS:
  8792. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8793. 7 : 14;
  8794. break;
  8795. default:
  8796. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8797. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8798. return;
  8799. }
  8800. if (IS_PINEVIEW(dev))
  8801. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8802. else
  8803. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8804. } else {
  8805. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8806. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8807. if (is_lvds) {
  8808. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8809. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8810. if (lvds & LVDS_CLKB_POWER_UP)
  8811. clock.p2 = 7;
  8812. else
  8813. clock.p2 = 14;
  8814. } else {
  8815. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8816. clock.p1 = 2;
  8817. else {
  8818. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8819. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8820. }
  8821. if (dpll & PLL_P2_DIVIDE_BY_4)
  8822. clock.p2 = 4;
  8823. else
  8824. clock.p2 = 2;
  8825. }
  8826. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8827. }
  8828. /*
  8829. * This value includes pixel_multiplier. We will use
  8830. * port_clock to compute adjusted_mode.crtc_clock in the
  8831. * encoder's get_config() function.
  8832. */
  8833. pipe_config->port_clock = port_clock;
  8834. }
  8835. int intel_dotclock_calculate(int link_freq,
  8836. const struct intel_link_m_n *m_n)
  8837. {
  8838. /*
  8839. * The calculation for the data clock is:
  8840. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8841. * But we want to avoid losing precison if possible, so:
  8842. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8843. *
  8844. * and the link clock is simpler:
  8845. * link_clock = (m * link_clock) / n
  8846. */
  8847. if (!m_n->link_n)
  8848. return 0;
  8849. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8850. }
  8851. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8852. struct intel_crtc_state *pipe_config)
  8853. {
  8854. struct drm_device *dev = crtc->base.dev;
  8855. /* read out port_clock from the DPLL */
  8856. i9xx_crtc_clock_get(crtc, pipe_config);
  8857. /*
  8858. * This value does not include pixel_multiplier.
  8859. * We will check that port_clock and adjusted_mode.crtc_clock
  8860. * agree once we know their relationship in the encoder's
  8861. * get_config() function.
  8862. */
  8863. pipe_config->base.adjusted_mode.crtc_clock =
  8864. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8865. &pipe_config->fdi_m_n);
  8866. }
  8867. /** Returns the currently programmed mode of the given pipe. */
  8868. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8869. struct drm_crtc *crtc)
  8870. {
  8871. struct drm_i915_private *dev_priv = dev->dev_private;
  8872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8873. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8874. struct drm_display_mode *mode;
  8875. struct intel_crtc_state pipe_config;
  8876. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8877. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8878. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8879. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8880. enum pipe pipe = intel_crtc->pipe;
  8881. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8882. if (!mode)
  8883. return NULL;
  8884. /*
  8885. * Construct a pipe_config sufficient for getting the clock info
  8886. * back out of crtc_clock_get.
  8887. *
  8888. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8889. * to use a real value here instead.
  8890. */
  8891. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8892. pipe_config.pixel_multiplier = 1;
  8893. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8894. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8895. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8896. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8897. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8898. mode->hdisplay = (htot & 0xffff) + 1;
  8899. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8900. mode->hsync_start = (hsync & 0xffff) + 1;
  8901. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8902. mode->vdisplay = (vtot & 0xffff) + 1;
  8903. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8904. mode->vsync_start = (vsync & 0xffff) + 1;
  8905. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8906. drm_mode_set_name(mode);
  8907. return mode;
  8908. }
  8909. void intel_mark_busy(struct drm_device *dev)
  8910. {
  8911. struct drm_i915_private *dev_priv = dev->dev_private;
  8912. if (dev_priv->mm.busy)
  8913. return;
  8914. intel_runtime_pm_get(dev_priv);
  8915. i915_update_gfx_val(dev_priv);
  8916. if (INTEL_INFO(dev)->gen >= 6)
  8917. gen6_rps_busy(dev_priv);
  8918. dev_priv->mm.busy = true;
  8919. }
  8920. void intel_mark_idle(struct drm_device *dev)
  8921. {
  8922. struct drm_i915_private *dev_priv = dev->dev_private;
  8923. if (!dev_priv->mm.busy)
  8924. return;
  8925. dev_priv->mm.busy = false;
  8926. if (INTEL_INFO(dev)->gen >= 6)
  8927. gen6_rps_idle(dev->dev_private);
  8928. intel_runtime_pm_put(dev_priv);
  8929. }
  8930. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8931. {
  8932. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8933. struct drm_device *dev = crtc->dev;
  8934. struct intel_unpin_work *work;
  8935. spin_lock_irq(&dev->event_lock);
  8936. work = intel_crtc->unpin_work;
  8937. intel_crtc->unpin_work = NULL;
  8938. spin_unlock_irq(&dev->event_lock);
  8939. if (work) {
  8940. cancel_work_sync(&work->work);
  8941. kfree(work);
  8942. }
  8943. drm_crtc_cleanup(crtc);
  8944. kfree(intel_crtc);
  8945. }
  8946. static void intel_unpin_work_fn(struct work_struct *__work)
  8947. {
  8948. struct intel_unpin_work *work =
  8949. container_of(__work, struct intel_unpin_work, work);
  8950. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8951. struct drm_device *dev = crtc->base.dev;
  8952. struct drm_plane *primary = crtc->base.primary;
  8953. mutex_lock(&dev->struct_mutex);
  8954. intel_unpin_fb_obj(work->old_fb, primary->state);
  8955. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8956. if (work->flip_queued_req)
  8957. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8958. mutex_unlock(&dev->struct_mutex);
  8959. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8960. drm_framebuffer_unreference(work->old_fb);
  8961. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8962. atomic_dec(&crtc->unpin_work_count);
  8963. kfree(work);
  8964. }
  8965. static void do_intel_finish_page_flip(struct drm_device *dev,
  8966. struct drm_crtc *crtc)
  8967. {
  8968. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8969. struct intel_unpin_work *work;
  8970. unsigned long flags;
  8971. /* Ignore early vblank irqs */
  8972. if (intel_crtc == NULL)
  8973. return;
  8974. /*
  8975. * This is called both by irq handlers and the reset code (to complete
  8976. * lost pageflips) so needs the full irqsave spinlocks.
  8977. */
  8978. spin_lock_irqsave(&dev->event_lock, flags);
  8979. work = intel_crtc->unpin_work;
  8980. /* Ensure we don't miss a work->pending update ... */
  8981. smp_rmb();
  8982. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8983. spin_unlock_irqrestore(&dev->event_lock, flags);
  8984. return;
  8985. }
  8986. page_flip_completed(intel_crtc);
  8987. spin_unlock_irqrestore(&dev->event_lock, flags);
  8988. }
  8989. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8990. {
  8991. struct drm_i915_private *dev_priv = dev->dev_private;
  8992. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8993. do_intel_finish_page_flip(dev, crtc);
  8994. }
  8995. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  8996. {
  8997. struct drm_i915_private *dev_priv = dev->dev_private;
  8998. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  8999. do_intel_finish_page_flip(dev, crtc);
  9000. }
  9001. /* Is 'a' after or equal to 'b'? */
  9002. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9003. {
  9004. return !((a - b) & 0x80000000);
  9005. }
  9006. static bool page_flip_finished(struct intel_crtc *crtc)
  9007. {
  9008. struct drm_device *dev = crtc->base.dev;
  9009. struct drm_i915_private *dev_priv = dev->dev_private;
  9010. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9011. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9012. return true;
  9013. /*
  9014. * The relevant registers doen't exist on pre-ctg.
  9015. * As the flip done interrupt doesn't trigger for mmio
  9016. * flips on gmch platforms, a flip count check isn't
  9017. * really needed there. But since ctg has the registers,
  9018. * include it in the check anyway.
  9019. */
  9020. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9021. return true;
  9022. /*
  9023. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9024. * used the same base address. In that case the mmio flip might
  9025. * have completed, but the CS hasn't even executed the flip yet.
  9026. *
  9027. * A flip count check isn't enough as the CS might have updated
  9028. * the base address just after start of vblank, but before we
  9029. * managed to process the interrupt. This means we'd complete the
  9030. * CS flip too soon.
  9031. *
  9032. * Combining both checks should get us a good enough result. It may
  9033. * still happen that the CS flip has been executed, but has not
  9034. * yet actually completed. But in case the base address is the same
  9035. * anyway, we don't really care.
  9036. */
  9037. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9038. crtc->unpin_work->gtt_offset &&
  9039. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9040. crtc->unpin_work->flip_count);
  9041. }
  9042. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9043. {
  9044. struct drm_i915_private *dev_priv = dev->dev_private;
  9045. struct intel_crtc *intel_crtc =
  9046. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9047. unsigned long flags;
  9048. /*
  9049. * This is called both by irq handlers and the reset code (to complete
  9050. * lost pageflips) so needs the full irqsave spinlocks.
  9051. *
  9052. * NB: An MMIO update of the plane base pointer will also
  9053. * generate a page-flip completion irq, i.e. every modeset
  9054. * is also accompanied by a spurious intel_prepare_page_flip().
  9055. */
  9056. spin_lock_irqsave(&dev->event_lock, flags);
  9057. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9058. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9059. spin_unlock_irqrestore(&dev->event_lock, flags);
  9060. }
  9061. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9062. {
  9063. /* Ensure that the work item is consistent when activating it ... */
  9064. smp_wmb();
  9065. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9066. /* and that it is marked active as soon as the irq could fire. */
  9067. smp_wmb();
  9068. }
  9069. static int intel_gen2_queue_flip(struct drm_device *dev,
  9070. struct drm_crtc *crtc,
  9071. struct drm_framebuffer *fb,
  9072. struct drm_i915_gem_object *obj,
  9073. struct drm_i915_gem_request *req,
  9074. uint32_t flags)
  9075. {
  9076. struct intel_engine_cs *ring = req->ring;
  9077. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9078. u32 flip_mask;
  9079. int ret;
  9080. ret = intel_ring_begin(req, 6);
  9081. if (ret)
  9082. return ret;
  9083. /* Can't queue multiple flips, so wait for the previous
  9084. * one to finish before executing the next.
  9085. */
  9086. if (intel_crtc->plane)
  9087. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9088. else
  9089. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9090. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9091. intel_ring_emit(ring, MI_NOOP);
  9092. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9093. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9094. intel_ring_emit(ring, fb->pitches[0]);
  9095. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9096. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9097. intel_mark_page_flip_active(intel_crtc);
  9098. return 0;
  9099. }
  9100. static int intel_gen3_queue_flip(struct drm_device *dev,
  9101. struct drm_crtc *crtc,
  9102. struct drm_framebuffer *fb,
  9103. struct drm_i915_gem_object *obj,
  9104. struct drm_i915_gem_request *req,
  9105. uint32_t flags)
  9106. {
  9107. struct intel_engine_cs *ring = req->ring;
  9108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9109. u32 flip_mask;
  9110. int ret;
  9111. ret = intel_ring_begin(req, 6);
  9112. if (ret)
  9113. return ret;
  9114. if (intel_crtc->plane)
  9115. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9116. else
  9117. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9118. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9119. intel_ring_emit(ring, MI_NOOP);
  9120. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9121. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9122. intel_ring_emit(ring, fb->pitches[0]);
  9123. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9124. intel_ring_emit(ring, MI_NOOP);
  9125. intel_mark_page_flip_active(intel_crtc);
  9126. return 0;
  9127. }
  9128. static int intel_gen4_queue_flip(struct drm_device *dev,
  9129. struct drm_crtc *crtc,
  9130. struct drm_framebuffer *fb,
  9131. struct drm_i915_gem_object *obj,
  9132. struct drm_i915_gem_request *req,
  9133. uint32_t flags)
  9134. {
  9135. struct intel_engine_cs *ring = req->ring;
  9136. struct drm_i915_private *dev_priv = dev->dev_private;
  9137. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9138. uint32_t pf, pipesrc;
  9139. int ret;
  9140. ret = intel_ring_begin(req, 4);
  9141. if (ret)
  9142. return ret;
  9143. /* i965+ uses the linear or tiled offsets from the
  9144. * Display Registers (which do not change across a page-flip)
  9145. * so we need only reprogram the base address.
  9146. */
  9147. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9148. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9149. intel_ring_emit(ring, fb->pitches[0]);
  9150. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9151. obj->tiling_mode);
  9152. /* XXX Enabling the panel-fitter across page-flip is so far
  9153. * untested on non-native modes, so ignore it for now.
  9154. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9155. */
  9156. pf = 0;
  9157. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9158. intel_ring_emit(ring, pf | pipesrc);
  9159. intel_mark_page_flip_active(intel_crtc);
  9160. return 0;
  9161. }
  9162. static int intel_gen6_queue_flip(struct drm_device *dev,
  9163. struct drm_crtc *crtc,
  9164. struct drm_framebuffer *fb,
  9165. struct drm_i915_gem_object *obj,
  9166. struct drm_i915_gem_request *req,
  9167. uint32_t flags)
  9168. {
  9169. struct intel_engine_cs *ring = req->ring;
  9170. struct drm_i915_private *dev_priv = dev->dev_private;
  9171. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9172. uint32_t pf, pipesrc;
  9173. int ret;
  9174. ret = intel_ring_begin(req, 4);
  9175. if (ret)
  9176. return ret;
  9177. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9178. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9179. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9180. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9181. /* Contrary to the suggestions in the documentation,
  9182. * "Enable Panel Fitter" does not seem to be required when page
  9183. * flipping with a non-native mode, and worse causes a normal
  9184. * modeset to fail.
  9185. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9186. */
  9187. pf = 0;
  9188. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9189. intel_ring_emit(ring, pf | pipesrc);
  9190. intel_mark_page_flip_active(intel_crtc);
  9191. return 0;
  9192. }
  9193. static int intel_gen7_queue_flip(struct drm_device *dev,
  9194. struct drm_crtc *crtc,
  9195. struct drm_framebuffer *fb,
  9196. struct drm_i915_gem_object *obj,
  9197. struct drm_i915_gem_request *req,
  9198. uint32_t flags)
  9199. {
  9200. struct intel_engine_cs *ring = req->ring;
  9201. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9202. uint32_t plane_bit = 0;
  9203. int len, ret;
  9204. switch (intel_crtc->plane) {
  9205. case PLANE_A:
  9206. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9207. break;
  9208. case PLANE_B:
  9209. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9210. break;
  9211. case PLANE_C:
  9212. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9213. break;
  9214. default:
  9215. WARN_ONCE(1, "unknown plane in flip command\n");
  9216. return -ENODEV;
  9217. }
  9218. len = 4;
  9219. if (ring->id == RCS) {
  9220. len += 6;
  9221. /*
  9222. * On Gen 8, SRM is now taking an extra dword to accommodate
  9223. * 48bits addresses, and we need a NOOP for the batch size to
  9224. * stay even.
  9225. */
  9226. if (IS_GEN8(dev))
  9227. len += 2;
  9228. }
  9229. /*
  9230. * BSpec MI_DISPLAY_FLIP for IVB:
  9231. * "The full packet must be contained within the same cache line."
  9232. *
  9233. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9234. * cacheline, if we ever start emitting more commands before
  9235. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9236. * then do the cacheline alignment, and finally emit the
  9237. * MI_DISPLAY_FLIP.
  9238. */
  9239. ret = intel_ring_cacheline_align(req);
  9240. if (ret)
  9241. return ret;
  9242. ret = intel_ring_begin(req, len);
  9243. if (ret)
  9244. return ret;
  9245. /* Unmask the flip-done completion message. Note that the bspec says that
  9246. * we should do this for both the BCS and RCS, and that we must not unmask
  9247. * more than one flip event at any time (or ensure that one flip message
  9248. * can be sent by waiting for flip-done prior to queueing new flips).
  9249. * Experimentation says that BCS works despite DERRMR masking all
  9250. * flip-done completion events and that unmasking all planes at once
  9251. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9252. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9253. */
  9254. if (ring->id == RCS) {
  9255. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9256. intel_ring_emit(ring, DERRMR);
  9257. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9258. DERRMR_PIPEB_PRI_FLIP_DONE |
  9259. DERRMR_PIPEC_PRI_FLIP_DONE));
  9260. if (IS_GEN8(dev))
  9261. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9262. MI_SRM_LRM_GLOBAL_GTT);
  9263. else
  9264. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9265. MI_SRM_LRM_GLOBAL_GTT);
  9266. intel_ring_emit(ring, DERRMR);
  9267. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9268. if (IS_GEN8(dev)) {
  9269. intel_ring_emit(ring, 0);
  9270. intel_ring_emit(ring, MI_NOOP);
  9271. }
  9272. }
  9273. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9274. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9275. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9276. intel_ring_emit(ring, (MI_NOOP));
  9277. intel_mark_page_flip_active(intel_crtc);
  9278. return 0;
  9279. }
  9280. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9281. struct drm_i915_gem_object *obj)
  9282. {
  9283. /*
  9284. * This is not being used for older platforms, because
  9285. * non-availability of flip done interrupt forces us to use
  9286. * CS flips. Older platforms derive flip done using some clever
  9287. * tricks involving the flip_pending status bits and vblank irqs.
  9288. * So using MMIO flips there would disrupt this mechanism.
  9289. */
  9290. if (ring == NULL)
  9291. return true;
  9292. if (INTEL_INFO(ring->dev)->gen < 5)
  9293. return false;
  9294. if (i915.use_mmio_flip < 0)
  9295. return false;
  9296. else if (i915.use_mmio_flip > 0)
  9297. return true;
  9298. else if (i915.enable_execlists)
  9299. return true;
  9300. else
  9301. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9302. }
  9303. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9304. {
  9305. struct drm_device *dev = intel_crtc->base.dev;
  9306. struct drm_i915_private *dev_priv = dev->dev_private;
  9307. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9308. const enum pipe pipe = intel_crtc->pipe;
  9309. u32 ctl, stride;
  9310. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9311. ctl &= ~PLANE_CTL_TILED_MASK;
  9312. switch (fb->modifier[0]) {
  9313. case DRM_FORMAT_MOD_NONE:
  9314. break;
  9315. case I915_FORMAT_MOD_X_TILED:
  9316. ctl |= PLANE_CTL_TILED_X;
  9317. break;
  9318. case I915_FORMAT_MOD_Y_TILED:
  9319. ctl |= PLANE_CTL_TILED_Y;
  9320. break;
  9321. case I915_FORMAT_MOD_Yf_TILED:
  9322. ctl |= PLANE_CTL_TILED_YF;
  9323. break;
  9324. default:
  9325. MISSING_CASE(fb->modifier[0]);
  9326. }
  9327. /*
  9328. * The stride is either expressed as a multiple of 64 bytes chunks for
  9329. * linear buffers or in number of tiles for tiled buffers.
  9330. */
  9331. stride = fb->pitches[0] /
  9332. intel_fb_stride_alignment(dev, fb->modifier[0],
  9333. fb->pixel_format);
  9334. /*
  9335. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9336. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9337. */
  9338. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9339. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9340. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9341. POSTING_READ(PLANE_SURF(pipe, 0));
  9342. }
  9343. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9344. {
  9345. struct drm_device *dev = intel_crtc->base.dev;
  9346. struct drm_i915_private *dev_priv = dev->dev_private;
  9347. struct intel_framebuffer *intel_fb =
  9348. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9349. struct drm_i915_gem_object *obj = intel_fb->obj;
  9350. u32 dspcntr;
  9351. u32 reg;
  9352. reg = DSPCNTR(intel_crtc->plane);
  9353. dspcntr = I915_READ(reg);
  9354. if (obj->tiling_mode != I915_TILING_NONE)
  9355. dspcntr |= DISPPLANE_TILED;
  9356. else
  9357. dspcntr &= ~DISPPLANE_TILED;
  9358. I915_WRITE(reg, dspcntr);
  9359. I915_WRITE(DSPSURF(intel_crtc->plane),
  9360. intel_crtc->unpin_work->gtt_offset);
  9361. POSTING_READ(DSPSURF(intel_crtc->plane));
  9362. }
  9363. /*
  9364. * XXX: This is the temporary way to update the plane registers until we get
  9365. * around to using the usual plane update functions for MMIO flips
  9366. */
  9367. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9368. {
  9369. struct drm_device *dev = intel_crtc->base.dev;
  9370. u32 start_vbl_count;
  9371. intel_mark_page_flip_active(intel_crtc);
  9372. intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9373. if (INTEL_INFO(dev)->gen >= 9)
  9374. skl_do_mmio_flip(intel_crtc);
  9375. else
  9376. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9377. ilk_do_mmio_flip(intel_crtc);
  9378. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9379. }
  9380. static void intel_mmio_flip_work_func(struct work_struct *work)
  9381. {
  9382. struct intel_mmio_flip *mmio_flip =
  9383. container_of(work, struct intel_mmio_flip, work);
  9384. if (mmio_flip->req)
  9385. WARN_ON(__i915_wait_request(mmio_flip->req,
  9386. mmio_flip->crtc->reset_counter,
  9387. false, NULL,
  9388. &mmio_flip->i915->rps.mmioflips));
  9389. intel_do_mmio_flip(mmio_flip->crtc);
  9390. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9391. kfree(mmio_flip);
  9392. }
  9393. static int intel_queue_mmio_flip(struct drm_device *dev,
  9394. struct drm_crtc *crtc,
  9395. struct drm_framebuffer *fb,
  9396. struct drm_i915_gem_object *obj,
  9397. struct intel_engine_cs *ring,
  9398. uint32_t flags)
  9399. {
  9400. struct intel_mmio_flip *mmio_flip;
  9401. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9402. if (mmio_flip == NULL)
  9403. return -ENOMEM;
  9404. mmio_flip->i915 = to_i915(dev);
  9405. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9406. mmio_flip->crtc = to_intel_crtc(crtc);
  9407. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9408. schedule_work(&mmio_flip->work);
  9409. return 0;
  9410. }
  9411. static int intel_default_queue_flip(struct drm_device *dev,
  9412. struct drm_crtc *crtc,
  9413. struct drm_framebuffer *fb,
  9414. struct drm_i915_gem_object *obj,
  9415. struct drm_i915_gem_request *req,
  9416. uint32_t flags)
  9417. {
  9418. return -ENODEV;
  9419. }
  9420. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9421. struct drm_crtc *crtc)
  9422. {
  9423. struct drm_i915_private *dev_priv = dev->dev_private;
  9424. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9425. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9426. u32 addr;
  9427. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9428. return true;
  9429. if (!work->enable_stall_check)
  9430. return false;
  9431. if (work->flip_ready_vblank == 0) {
  9432. if (work->flip_queued_req &&
  9433. !i915_gem_request_completed(work->flip_queued_req, true))
  9434. return false;
  9435. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9436. }
  9437. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9438. return false;
  9439. /* Potential stall - if we see that the flip has happened,
  9440. * assume a missed interrupt. */
  9441. if (INTEL_INFO(dev)->gen >= 4)
  9442. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9443. else
  9444. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9445. /* There is a potential issue here with a false positive after a flip
  9446. * to the same address. We could address this by checking for a
  9447. * non-incrementing frame counter.
  9448. */
  9449. return addr == work->gtt_offset;
  9450. }
  9451. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9452. {
  9453. struct drm_i915_private *dev_priv = dev->dev_private;
  9454. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9455. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9456. struct intel_unpin_work *work;
  9457. WARN_ON(!in_interrupt());
  9458. if (crtc == NULL)
  9459. return;
  9460. spin_lock(&dev->event_lock);
  9461. work = intel_crtc->unpin_work;
  9462. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9463. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9464. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9465. page_flip_completed(intel_crtc);
  9466. work = NULL;
  9467. }
  9468. if (work != NULL &&
  9469. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9470. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9471. spin_unlock(&dev->event_lock);
  9472. }
  9473. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9474. struct drm_framebuffer *fb,
  9475. struct drm_pending_vblank_event *event,
  9476. uint32_t page_flip_flags)
  9477. {
  9478. struct drm_device *dev = crtc->dev;
  9479. struct drm_i915_private *dev_priv = dev->dev_private;
  9480. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9481. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9482. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9483. struct drm_plane *primary = crtc->primary;
  9484. enum pipe pipe = intel_crtc->pipe;
  9485. struct intel_unpin_work *work;
  9486. struct intel_engine_cs *ring;
  9487. bool mmio_flip;
  9488. struct drm_i915_gem_request *request = NULL;
  9489. int ret;
  9490. /*
  9491. * drm_mode_page_flip_ioctl() should already catch this, but double
  9492. * check to be safe. In the future we may enable pageflipping from
  9493. * a disabled primary plane.
  9494. */
  9495. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9496. return -EBUSY;
  9497. /* Can't change pixel format via MI display flips. */
  9498. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9499. return -EINVAL;
  9500. /*
  9501. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9502. * Note that pitch changes could also affect these register.
  9503. */
  9504. if (INTEL_INFO(dev)->gen > 3 &&
  9505. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9506. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9507. return -EINVAL;
  9508. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9509. goto out_hang;
  9510. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9511. if (work == NULL)
  9512. return -ENOMEM;
  9513. work->event = event;
  9514. work->crtc = crtc;
  9515. work->old_fb = old_fb;
  9516. INIT_WORK(&work->work, intel_unpin_work_fn);
  9517. ret = drm_crtc_vblank_get(crtc);
  9518. if (ret)
  9519. goto free_work;
  9520. /* We borrow the event spin lock for protecting unpin_work */
  9521. spin_lock_irq(&dev->event_lock);
  9522. if (intel_crtc->unpin_work) {
  9523. /* Before declaring the flip queue wedged, check if
  9524. * the hardware completed the operation behind our backs.
  9525. */
  9526. if (__intel_pageflip_stall_check(dev, crtc)) {
  9527. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9528. page_flip_completed(intel_crtc);
  9529. } else {
  9530. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9531. spin_unlock_irq(&dev->event_lock);
  9532. drm_crtc_vblank_put(crtc);
  9533. kfree(work);
  9534. return -EBUSY;
  9535. }
  9536. }
  9537. intel_crtc->unpin_work = work;
  9538. spin_unlock_irq(&dev->event_lock);
  9539. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9540. flush_workqueue(dev_priv->wq);
  9541. /* Reference the objects for the scheduled work. */
  9542. drm_framebuffer_reference(work->old_fb);
  9543. drm_gem_object_reference(&obj->base);
  9544. crtc->primary->fb = fb;
  9545. update_state_fb(crtc->primary);
  9546. work->pending_flip_obj = obj;
  9547. ret = i915_mutex_lock_interruptible(dev);
  9548. if (ret)
  9549. goto cleanup;
  9550. atomic_inc(&intel_crtc->unpin_work_count);
  9551. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9552. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9553. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9554. if (IS_VALLEYVIEW(dev)) {
  9555. ring = &dev_priv->ring[BCS];
  9556. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9557. /* vlv: DISPLAY_FLIP fails to change tiling */
  9558. ring = NULL;
  9559. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9560. ring = &dev_priv->ring[BCS];
  9561. } else if (INTEL_INFO(dev)->gen >= 7) {
  9562. ring = i915_gem_request_get_ring(obj->last_write_req);
  9563. if (ring == NULL || ring->id != RCS)
  9564. ring = &dev_priv->ring[BCS];
  9565. } else {
  9566. ring = &dev_priv->ring[RCS];
  9567. }
  9568. mmio_flip = use_mmio_flip(ring, obj);
  9569. /* When using CS flips, we want to emit semaphores between rings.
  9570. * However, when using mmio flips we will create a task to do the
  9571. * synchronisation, so all we want here is to pin the framebuffer
  9572. * into the display plane and skip any waits.
  9573. */
  9574. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9575. crtc->primary->state,
  9576. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9577. if (ret)
  9578. goto cleanup_pending;
  9579. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9580. + intel_crtc->dspaddr_offset;
  9581. if (mmio_flip) {
  9582. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9583. page_flip_flags);
  9584. if (ret)
  9585. goto cleanup_unpin;
  9586. i915_gem_request_assign(&work->flip_queued_req,
  9587. obj->last_write_req);
  9588. } else {
  9589. if (!request) {
  9590. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9591. if (ret)
  9592. goto cleanup_unpin;
  9593. }
  9594. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9595. page_flip_flags);
  9596. if (ret)
  9597. goto cleanup_unpin;
  9598. i915_gem_request_assign(&work->flip_queued_req, request);
  9599. }
  9600. if (request)
  9601. i915_add_request_no_flush(request);
  9602. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9603. work->enable_stall_check = true;
  9604. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9605. to_intel_plane(primary)->frontbuffer_bit);
  9606. mutex_unlock(&dev->struct_mutex);
  9607. intel_fbc_disable_crtc(intel_crtc);
  9608. intel_frontbuffer_flip_prepare(dev,
  9609. to_intel_plane(primary)->frontbuffer_bit);
  9610. trace_i915_flip_request(intel_crtc->plane, obj);
  9611. return 0;
  9612. cleanup_unpin:
  9613. intel_unpin_fb_obj(fb, crtc->primary->state);
  9614. cleanup_pending:
  9615. if (request)
  9616. i915_gem_request_cancel(request);
  9617. atomic_dec(&intel_crtc->unpin_work_count);
  9618. mutex_unlock(&dev->struct_mutex);
  9619. cleanup:
  9620. crtc->primary->fb = old_fb;
  9621. update_state_fb(crtc->primary);
  9622. drm_gem_object_unreference_unlocked(&obj->base);
  9623. drm_framebuffer_unreference(work->old_fb);
  9624. spin_lock_irq(&dev->event_lock);
  9625. intel_crtc->unpin_work = NULL;
  9626. spin_unlock_irq(&dev->event_lock);
  9627. drm_crtc_vblank_put(crtc);
  9628. free_work:
  9629. kfree(work);
  9630. if (ret == -EIO) {
  9631. struct drm_atomic_state *state;
  9632. struct drm_plane_state *plane_state;
  9633. out_hang:
  9634. state = drm_atomic_state_alloc(dev);
  9635. if (!state)
  9636. return -ENOMEM;
  9637. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9638. retry:
  9639. plane_state = drm_atomic_get_plane_state(state, primary);
  9640. ret = PTR_ERR_OR_ZERO(plane_state);
  9641. if (!ret) {
  9642. drm_atomic_set_fb_for_plane(plane_state, fb);
  9643. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9644. if (!ret)
  9645. ret = drm_atomic_commit(state);
  9646. }
  9647. if (ret == -EDEADLK) {
  9648. drm_modeset_backoff(state->acquire_ctx);
  9649. drm_atomic_state_clear(state);
  9650. goto retry;
  9651. }
  9652. if (ret)
  9653. drm_atomic_state_free(state);
  9654. if (ret == 0 && event) {
  9655. spin_lock_irq(&dev->event_lock);
  9656. drm_send_vblank_event(dev, pipe, event);
  9657. spin_unlock_irq(&dev->event_lock);
  9658. }
  9659. }
  9660. return ret;
  9661. }
  9662. /**
  9663. * intel_wm_need_update - Check whether watermarks need updating
  9664. * @plane: drm plane
  9665. * @state: new plane state
  9666. *
  9667. * Check current plane state versus the new one to determine whether
  9668. * watermarks need to be recalculated.
  9669. *
  9670. * Returns true or false.
  9671. */
  9672. static bool intel_wm_need_update(struct drm_plane *plane,
  9673. struct drm_plane_state *state)
  9674. {
  9675. /* Update watermarks on tiling changes. */
  9676. if (!plane->state->fb || !state->fb ||
  9677. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9678. plane->state->rotation != state->rotation)
  9679. return true;
  9680. if (plane->state->crtc_w != state->crtc_w)
  9681. return true;
  9682. return false;
  9683. }
  9684. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9685. struct drm_plane_state *plane_state)
  9686. {
  9687. struct drm_crtc *crtc = crtc_state->crtc;
  9688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9689. struct drm_plane *plane = plane_state->plane;
  9690. struct drm_device *dev = crtc->dev;
  9691. struct drm_i915_private *dev_priv = dev->dev_private;
  9692. struct intel_plane_state *old_plane_state =
  9693. to_intel_plane_state(plane->state);
  9694. int idx = intel_crtc->base.base.id, ret;
  9695. int i = drm_plane_index(plane);
  9696. bool mode_changed = needs_modeset(crtc_state);
  9697. bool was_crtc_enabled = crtc->state->active;
  9698. bool is_crtc_enabled = crtc_state->active;
  9699. bool turn_off, turn_on, visible, was_visible;
  9700. struct drm_framebuffer *fb = plane_state->fb;
  9701. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9702. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9703. ret = skl_update_scaler_plane(
  9704. to_intel_crtc_state(crtc_state),
  9705. to_intel_plane_state(plane_state));
  9706. if (ret)
  9707. return ret;
  9708. }
  9709. /*
  9710. * Disabling a plane is always okay; we just need to update
  9711. * fb tracking in a special way since cleanup_fb() won't
  9712. * get called by the plane helpers.
  9713. */
  9714. if (old_plane_state->base.fb && !fb)
  9715. intel_crtc->atomic.disabled_planes |= 1 << i;
  9716. was_visible = old_plane_state->visible;
  9717. visible = to_intel_plane_state(plane_state)->visible;
  9718. if (!was_crtc_enabled && WARN_ON(was_visible))
  9719. was_visible = false;
  9720. if (!is_crtc_enabled && WARN_ON(visible))
  9721. visible = false;
  9722. if (!was_visible && !visible)
  9723. return 0;
  9724. turn_off = was_visible && (!visible || mode_changed);
  9725. turn_on = visible && (!was_visible || mode_changed);
  9726. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9727. plane->base.id, fb ? fb->base.id : -1);
  9728. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9729. plane->base.id, was_visible, visible,
  9730. turn_off, turn_on, mode_changed);
  9731. if (turn_on) {
  9732. intel_crtc->atomic.update_wm_pre = true;
  9733. /* must disable cxsr around plane enable/disable */
  9734. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9735. intel_crtc->atomic.disable_cxsr = true;
  9736. /* to potentially re-enable cxsr */
  9737. intel_crtc->atomic.wait_vblank = true;
  9738. intel_crtc->atomic.update_wm_post = true;
  9739. }
  9740. } else if (turn_off) {
  9741. intel_crtc->atomic.update_wm_post = true;
  9742. /* must disable cxsr around plane enable/disable */
  9743. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9744. if (is_crtc_enabled)
  9745. intel_crtc->atomic.wait_vblank = true;
  9746. intel_crtc->atomic.disable_cxsr = true;
  9747. }
  9748. } else if (intel_wm_need_update(plane, plane_state)) {
  9749. intel_crtc->atomic.update_wm_pre = true;
  9750. }
  9751. if (visible)
  9752. intel_crtc->atomic.fb_bits |=
  9753. to_intel_plane(plane)->frontbuffer_bit;
  9754. switch (plane->type) {
  9755. case DRM_PLANE_TYPE_PRIMARY:
  9756. intel_crtc->atomic.wait_for_flips = true;
  9757. intel_crtc->atomic.pre_disable_primary = turn_off;
  9758. intel_crtc->atomic.post_enable_primary = turn_on;
  9759. if (turn_off) {
  9760. /*
  9761. * FIXME: Actually if we will still have any other
  9762. * plane enabled on the pipe we could let IPS enabled
  9763. * still, but for now lets consider that when we make
  9764. * primary invisible by setting DSPCNTR to 0 on
  9765. * update_primary_plane function IPS needs to be
  9766. * disable.
  9767. */
  9768. intel_crtc->atomic.disable_ips = true;
  9769. intel_crtc->atomic.disable_fbc = true;
  9770. }
  9771. /*
  9772. * FBC does not work on some platforms for rotated
  9773. * planes, so disable it when rotation is not 0 and
  9774. * update it when rotation is set back to 0.
  9775. *
  9776. * FIXME: This is redundant with the fbc update done in
  9777. * the primary plane enable function except that that
  9778. * one is done too late. We eventually need to unify
  9779. * this.
  9780. */
  9781. if (visible &&
  9782. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9783. dev_priv->fbc.crtc == intel_crtc &&
  9784. plane_state->rotation != BIT(DRM_ROTATE_0))
  9785. intel_crtc->atomic.disable_fbc = true;
  9786. /*
  9787. * BDW signals flip done immediately if the plane
  9788. * is disabled, even if the plane enable is already
  9789. * armed to occur at the next vblank :(
  9790. */
  9791. if (turn_on && IS_BROADWELL(dev))
  9792. intel_crtc->atomic.wait_vblank = true;
  9793. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9794. break;
  9795. case DRM_PLANE_TYPE_CURSOR:
  9796. break;
  9797. case DRM_PLANE_TYPE_OVERLAY:
  9798. if (turn_off && !mode_changed) {
  9799. intel_crtc->atomic.wait_vblank = true;
  9800. intel_crtc->atomic.update_sprite_watermarks |=
  9801. 1 << i;
  9802. }
  9803. }
  9804. return 0;
  9805. }
  9806. static bool encoders_cloneable(const struct intel_encoder *a,
  9807. const struct intel_encoder *b)
  9808. {
  9809. /* masks could be asymmetric, so check both ways */
  9810. return a == b || (a->cloneable & (1 << b->type) &&
  9811. b->cloneable & (1 << a->type));
  9812. }
  9813. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9814. struct intel_crtc *crtc,
  9815. struct intel_encoder *encoder)
  9816. {
  9817. struct intel_encoder *source_encoder;
  9818. struct drm_connector *connector;
  9819. struct drm_connector_state *connector_state;
  9820. int i;
  9821. for_each_connector_in_state(state, connector, connector_state, i) {
  9822. if (connector_state->crtc != &crtc->base)
  9823. continue;
  9824. source_encoder =
  9825. to_intel_encoder(connector_state->best_encoder);
  9826. if (!encoders_cloneable(encoder, source_encoder))
  9827. return false;
  9828. }
  9829. return true;
  9830. }
  9831. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9832. struct intel_crtc *crtc)
  9833. {
  9834. struct intel_encoder *encoder;
  9835. struct drm_connector *connector;
  9836. struct drm_connector_state *connector_state;
  9837. int i;
  9838. for_each_connector_in_state(state, connector, connector_state, i) {
  9839. if (connector_state->crtc != &crtc->base)
  9840. continue;
  9841. encoder = to_intel_encoder(connector_state->best_encoder);
  9842. if (!check_single_encoder_cloning(state, crtc, encoder))
  9843. return false;
  9844. }
  9845. return true;
  9846. }
  9847. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9848. struct drm_crtc_state *crtc_state)
  9849. {
  9850. struct drm_device *dev = crtc->dev;
  9851. struct drm_i915_private *dev_priv = dev->dev_private;
  9852. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9853. struct intel_crtc_state *pipe_config =
  9854. to_intel_crtc_state(crtc_state);
  9855. struct drm_atomic_state *state = crtc_state->state;
  9856. int ret;
  9857. bool mode_changed = needs_modeset(crtc_state);
  9858. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9859. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9860. return -EINVAL;
  9861. }
  9862. if (mode_changed && !crtc_state->active)
  9863. intel_crtc->atomic.update_wm_post = true;
  9864. if (mode_changed && crtc_state->enable &&
  9865. dev_priv->display.crtc_compute_clock &&
  9866. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9867. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9868. pipe_config);
  9869. if (ret)
  9870. return ret;
  9871. }
  9872. ret = 0;
  9873. if (INTEL_INFO(dev)->gen >= 9) {
  9874. if (mode_changed)
  9875. ret = skl_update_scaler_crtc(pipe_config);
  9876. if (!ret)
  9877. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9878. pipe_config);
  9879. }
  9880. return ret;
  9881. }
  9882. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9883. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9884. .load_lut = intel_crtc_load_lut,
  9885. .atomic_begin = intel_begin_crtc_commit,
  9886. .atomic_flush = intel_finish_crtc_commit,
  9887. .atomic_check = intel_crtc_atomic_check,
  9888. };
  9889. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9890. {
  9891. struct intel_connector *connector;
  9892. for_each_intel_connector(dev, connector) {
  9893. if (connector->base.encoder) {
  9894. connector->base.state->best_encoder =
  9895. connector->base.encoder;
  9896. connector->base.state->crtc =
  9897. connector->base.encoder->crtc;
  9898. } else {
  9899. connector->base.state->best_encoder = NULL;
  9900. connector->base.state->crtc = NULL;
  9901. }
  9902. }
  9903. }
  9904. static void
  9905. connected_sink_compute_bpp(struct intel_connector *connector,
  9906. struct intel_crtc_state *pipe_config)
  9907. {
  9908. int bpp = pipe_config->pipe_bpp;
  9909. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9910. connector->base.base.id,
  9911. connector->base.name);
  9912. /* Don't use an invalid EDID bpc value */
  9913. if (connector->base.display_info.bpc &&
  9914. connector->base.display_info.bpc * 3 < bpp) {
  9915. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9916. bpp, connector->base.display_info.bpc*3);
  9917. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9918. }
  9919. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9920. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9921. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9922. bpp);
  9923. pipe_config->pipe_bpp = 24;
  9924. }
  9925. }
  9926. static int
  9927. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9928. struct intel_crtc_state *pipe_config)
  9929. {
  9930. struct drm_device *dev = crtc->base.dev;
  9931. struct drm_atomic_state *state;
  9932. struct drm_connector *connector;
  9933. struct drm_connector_state *connector_state;
  9934. int bpp, i;
  9935. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9936. bpp = 10*3;
  9937. else if (INTEL_INFO(dev)->gen >= 5)
  9938. bpp = 12*3;
  9939. else
  9940. bpp = 8*3;
  9941. pipe_config->pipe_bpp = bpp;
  9942. state = pipe_config->base.state;
  9943. /* Clamp display bpp to EDID value */
  9944. for_each_connector_in_state(state, connector, connector_state, i) {
  9945. if (connector_state->crtc != &crtc->base)
  9946. continue;
  9947. connected_sink_compute_bpp(to_intel_connector(connector),
  9948. pipe_config);
  9949. }
  9950. return bpp;
  9951. }
  9952. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9953. {
  9954. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9955. "type: 0x%x flags: 0x%x\n",
  9956. mode->crtc_clock,
  9957. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9958. mode->crtc_hsync_end, mode->crtc_htotal,
  9959. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9960. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9961. }
  9962. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9963. struct intel_crtc_state *pipe_config,
  9964. const char *context)
  9965. {
  9966. struct drm_device *dev = crtc->base.dev;
  9967. struct drm_plane *plane;
  9968. struct intel_plane *intel_plane;
  9969. struct intel_plane_state *state;
  9970. struct drm_framebuffer *fb;
  9971. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9972. context, pipe_config, pipe_name(crtc->pipe));
  9973. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9974. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9975. pipe_config->pipe_bpp, pipe_config->dither);
  9976. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9977. pipe_config->has_pch_encoder,
  9978. pipe_config->fdi_lanes,
  9979. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9980. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9981. pipe_config->fdi_m_n.tu);
  9982. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9983. pipe_config->has_dp_encoder,
  9984. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9985. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9986. pipe_config->dp_m_n.tu);
  9987. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9988. pipe_config->has_dp_encoder,
  9989. pipe_config->dp_m2_n2.gmch_m,
  9990. pipe_config->dp_m2_n2.gmch_n,
  9991. pipe_config->dp_m2_n2.link_m,
  9992. pipe_config->dp_m2_n2.link_n,
  9993. pipe_config->dp_m2_n2.tu);
  9994. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9995. pipe_config->has_audio,
  9996. pipe_config->has_infoframe);
  9997. DRM_DEBUG_KMS("requested mode:\n");
  9998. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9999. DRM_DEBUG_KMS("adjusted mode:\n");
  10000. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10001. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10002. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10003. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10004. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10005. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10006. crtc->num_scalers,
  10007. pipe_config->scaler_state.scaler_users,
  10008. pipe_config->scaler_state.scaler_id);
  10009. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10010. pipe_config->gmch_pfit.control,
  10011. pipe_config->gmch_pfit.pgm_ratios,
  10012. pipe_config->gmch_pfit.lvds_border_bits);
  10013. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10014. pipe_config->pch_pfit.pos,
  10015. pipe_config->pch_pfit.size,
  10016. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10017. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10018. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10019. if (IS_BROXTON(dev)) {
  10020. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10021. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10022. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10023. pipe_config->ddi_pll_sel,
  10024. pipe_config->dpll_hw_state.ebb0,
  10025. pipe_config->dpll_hw_state.ebb4,
  10026. pipe_config->dpll_hw_state.pll0,
  10027. pipe_config->dpll_hw_state.pll1,
  10028. pipe_config->dpll_hw_state.pll2,
  10029. pipe_config->dpll_hw_state.pll3,
  10030. pipe_config->dpll_hw_state.pll6,
  10031. pipe_config->dpll_hw_state.pll8,
  10032. pipe_config->dpll_hw_state.pll9,
  10033. pipe_config->dpll_hw_state.pll10,
  10034. pipe_config->dpll_hw_state.pcsdw12);
  10035. } else if (IS_SKYLAKE(dev)) {
  10036. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10037. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10038. pipe_config->ddi_pll_sel,
  10039. pipe_config->dpll_hw_state.ctrl1,
  10040. pipe_config->dpll_hw_state.cfgcr1,
  10041. pipe_config->dpll_hw_state.cfgcr2);
  10042. } else if (HAS_DDI(dev)) {
  10043. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10044. pipe_config->ddi_pll_sel,
  10045. pipe_config->dpll_hw_state.wrpll);
  10046. } else {
  10047. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10048. "fp0: 0x%x, fp1: 0x%x\n",
  10049. pipe_config->dpll_hw_state.dpll,
  10050. pipe_config->dpll_hw_state.dpll_md,
  10051. pipe_config->dpll_hw_state.fp0,
  10052. pipe_config->dpll_hw_state.fp1);
  10053. }
  10054. DRM_DEBUG_KMS("planes on this crtc\n");
  10055. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10056. intel_plane = to_intel_plane(plane);
  10057. if (intel_plane->pipe != crtc->pipe)
  10058. continue;
  10059. state = to_intel_plane_state(plane->state);
  10060. fb = state->base.fb;
  10061. if (!fb) {
  10062. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10063. "disabled, scaler_id = %d\n",
  10064. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10065. plane->base.id, intel_plane->pipe,
  10066. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10067. drm_plane_index(plane), state->scaler_id);
  10068. continue;
  10069. }
  10070. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10071. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10072. plane->base.id, intel_plane->pipe,
  10073. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10074. drm_plane_index(plane));
  10075. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10076. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10077. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10078. state->scaler_id,
  10079. state->src.x1 >> 16, state->src.y1 >> 16,
  10080. drm_rect_width(&state->src) >> 16,
  10081. drm_rect_height(&state->src) >> 16,
  10082. state->dst.x1, state->dst.y1,
  10083. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10084. }
  10085. }
  10086. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10087. {
  10088. struct drm_device *dev = state->dev;
  10089. struct intel_encoder *encoder;
  10090. struct drm_connector *connector;
  10091. struct drm_connector_state *connector_state;
  10092. unsigned int used_ports = 0;
  10093. int i;
  10094. /*
  10095. * Walk the connector list instead of the encoder
  10096. * list to detect the problem on ddi platforms
  10097. * where there's just one encoder per digital port.
  10098. */
  10099. for_each_connector_in_state(state, connector, connector_state, i) {
  10100. if (!connector_state->best_encoder)
  10101. continue;
  10102. encoder = to_intel_encoder(connector_state->best_encoder);
  10103. WARN_ON(!connector_state->crtc);
  10104. switch (encoder->type) {
  10105. unsigned int port_mask;
  10106. case INTEL_OUTPUT_UNKNOWN:
  10107. if (WARN_ON(!HAS_DDI(dev)))
  10108. break;
  10109. case INTEL_OUTPUT_DISPLAYPORT:
  10110. case INTEL_OUTPUT_HDMI:
  10111. case INTEL_OUTPUT_EDP:
  10112. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10113. /* the same port mustn't appear more than once */
  10114. if (used_ports & port_mask)
  10115. return false;
  10116. used_ports |= port_mask;
  10117. default:
  10118. break;
  10119. }
  10120. }
  10121. return true;
  10122. }
  10123. static void
  10124. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10125. {
  10126. struct drm_crtc_state tmp_state;
  10127. struct intel_crtc_scaler_state scaler_state;
  10128. struct intel_dpll_hw_state dpll_hw_state;
  10129. enum intel_dpll_id shared_dpll;
  10130. uint32_t ddi_pll_sel;
  10131. bool force_thru;
  10132. /* FIXME: before the switch to atomic started, a new pipe_config was
  10133. * kzalloc'd. Code that depends on any field being zero should be
  10134. * fixed, so that the crtc_state can be safely duplicated. For now,
  10135. * only fields that are know to not cause problems are preserved. */
  10136. tmp_state = crtc_state->base;
  10137. scaler_state = crtc_state->scaler_state;
  10138. shared_dpll = crtc_state->shared_dpll;
  10139. dpll_hw_state = crtc_state->dpll_hw_state;
  10140. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10141. force_thru = crtc_state->pch_pfit.force_thru;
  10142. memset(crtc_state, 0, sizeof *crtc_state);
  10143. crtc_state->base = tmp_state;
  10144. crtc_state->scaler_state = scaler_state;
  10145. crtc_state->shared_dpll = shared_dpll;
  10146. crtc_state->dpll_hw_state = dpll_hw_state;
  10147. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10148. crtc_state->pch_pfit.force_thru = force_thru;
  10149. }
  10150. static int
  10151. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10152. struct intel_crtc_state *pipe_config)
  10153. {
  10154. struct drm_atomic_state *state = pipe_config->base.state;
  10155. struct intel_encoder *encoder;
  10156. struct drm_connector *connector;
  10157. struct drm_connector_state *connector_state;
  10158. int base_bpp, ret = -EINVAL;
  10159. int i;
  10160. bool retry = true;
  10161. clear_intel_crtc_state(pipe_config);
  10162. pipe_config->cpu_transcoder =
  10163. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10164. /*
  10165. * Sanitize sync polarity flags based on requested ones. If neither
  10166. * positive or negative polarity is requested, treat this as meaning
  10167. * negative polarity.
  10168. */
  10169. if (!(pipe_config->base.adjusted_mode.flags &
  10170. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10171. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10172. if (!(pipe_config->base.adjusted_mode.flags &
  10173. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10174. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10175. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10176. * plane pixel format and any sink constraints into account. Returns the
  10177. * source plane bpp so that dithering can be selected on mismatches
  10178. * after encoders and crtc also have had their say. */
  10179. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10180. pipe_config);
  10181. if (base_bpp < 0)
  10182. goto fail;
  10183. /*
  10184. * Determine the real pipe dimensions. Note that stereo modes can
  10185. * increase the actual pipe size due to the frame doubling and
  10186. * insertion of additional space for blanks between the frame. This
  10187. * is stored in the crtc timings. We use the requested mode to do this
  10188. * computation to clearly distinguish it from the adjusted mode, which
  10189. * can be changed by the connectors in the below retry loop.
  10190. */
  10191. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10192. &pipe_config->pipe_src_w,
  10193. &pipe_config->pipe_src_h);
  10194. encoder_retry:
  10195. /* Ensure the port clock defaults are reset when retrying. */
  10196. pipe_config->port_clock = 0;
  10197. pipe_config->pixel_multiplier = 1;
  10198. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10199. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10200. CRTC_STEREO_DOUBLE);
  10201. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10202. * adjust it according to limitations or connector properties, and also
  10203. * a chance to reject the mode entirely.
  10204. */
  10205. for_each_connector_in_state(state, connector, connector_state, i) {
  10206. if (connector_state->crtc != crtc)
  10207. continue;
  10208. encoder = to_intel_encoder(connector_state->best_encoder);
  10209. if (!(encoder->compute_config(encoder, pipe_config))) {
  10210. DRM_DEBUG_KMS("Encoder config failure\n");
  10211. goto fail;
  10212. }
  10213. }
  10214. /* Set default port clock if not overwritten by the encoder. Needs to be
  10215. * done afterwards in case the encoder adjusts the mode. */
  10216. if (!pipe_config->port_clock)
  10217. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10218. * pipe_config->pixel_multiplier;
  10219. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10220. if (ret < 0) {
  10221. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10222. goto fail;
  10223. }
  10224. if (ret == RETRY) {
  10225. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10226. ret = -EINVAL;
  10227. goto fail;
  10228. }
  10229. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10230. retry = false;
  10231. goto encoder_retry;
  10232. }
  10233. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10234. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10235. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10236. fail:
  10237. return ret;
  10238. }
  10239. static void
  10240. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10241. {
  10242. struct drm_crtc *crtc;
  10243. struct drm_crtc_state *crtc_state;
  10244. int i;
  10245. /* Double check state. */
  10246. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10247. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10248. /* Update hwmode for vblank functions */
  10249. if (crtc->state->active)
  10250. crtc->hwmode = crtc->state->adjusted_mode;
  10251. else
  10252. crtc->hwmode.crtc_clock = 0;
  10253. }
  10254. }
  10255. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10256. {
  10257. int diff;
  10258. if (clock1 == clock2)
  10259. return true;
  10260. if (!clock1 || !clock2)
  10261. return false;
  10262. diff = abs(clock1 - clock2);
  10263. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10264. return true;
  10265. return false;
  10266. }
  10267. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10268. list_for_each_entry((intel_crtc), \
  10269. &(dev)->mode_config.crtc_list, \
  10270. base.head) \
  10271. if (mask & (1 <<(intel_crtc)->pipe))
  10272. static bool
  10273. intel_compare_m_n(unsigned int m, unsigned int n,
  10274. unsigned int m2, unsigned int n2,
  10275. bool exact)
  10276. {
  10277. if (m == m2 && n == n2)
  10278. return true;
  10279. if (exact || !m || !n || !m2 || !n2)
  10280. return false;
  10281. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10282. if (m > m2) {
  10283. while (m > m2) {
  10284. m2 <<= 1;
  10285. n2 <<= 1;
  10286. }
  10287. } else if (m < m2) {
  10288. while (m < m2) {
  10289. m <<= 1;
  10290. n <<= 1;
  10291. }
  10292. }
  10293. return m == m2 && n == n2;
  10294. }
  10295. static bool
  10296. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10297. struct intel_link_m_n *m2_n2,
  10298. bool adjust)
  10299. {
  10300. if (m_n->tu == m2_n2->tu &&
  10301. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10302. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10303. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10304. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10305. if (adjust)
  10306. *m2_n2 = *m_n;
  10307. return true;
  10308. }
  10309. return false;
  10310. }
  10311. static bool
  10312. intel_pipe_config_compare(struct drm_device *dev,
  10313. struct intel_crtc_state *current_config,
  10314. struct intel_crtc_state *pipe_config,
  10315. bool adjust)
  10316. {
  10317. bool ret = true;
  10318. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10319. do { \
  10320. if (!adjust) \
  10321. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10322. else \
  10323. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10324. } while (0)
  10325. #define PIPE_CONF_CHECK_X(name) \
  10326. if (current_config->name != pipe_config->name) { \
  10327. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10328. "(expected 0x%08x, found 0x%08x)\n", \
  10329. current_config->name, \
  10330. pipe_config->name); \
  10331. ret = false; \
  10332. }
  10333. #define PIPE_CONF_CHECK_I(name) \
  10334. if (current_config->name != pipe_config->name) { \
  10335. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10336. "(expected %i, found %i)\n", \
  10337. current_config->name, \
  10338. pipe_config->name); \
  10339. ret = false; \
  10340. }
  10341. #define PIPE_CONF_CHECK_M_N(name) \
  10342. if (!intel_compare_link_m_n(&current_config->name, \
  10343. &pipe_config->name,\
  10344. adjust)) { \
  10345. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10346. "(expected tu %i gmch %i/%i link %i/%i, " \
  10347. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10348. current_config->name.tu, \
  10349. current_config->name.gmch_m, \
  10350. current_config->name.gmch_n, \
  10351. current_config->name.link_m, \
  10352. current_config->name.link_n, \
  10353. pipe_config->name.tu, \
  10354. pipe_config->name.gmch_m, \
  10355. pipe_config->name.gmch_n, \
  10356. pipe_config->name.link_m, \
  10357. pipe_config->name.link_n); \
  10358. ret = false; \
  10359. }
  10360. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10361. if (!intel_compare_link_m_n(&current_config->name, \
  10362. &pipe_config->name, adjust) && \
  10363. !intel_compare_link_m_n(&current_config->alt_name, \
  10364. &pipe_config->name, adjust)) { \
  10365. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10366. "(expected tu %i gmch %i/%i link %i/%i, " \
  10367. "or tu %i gmch %i/%i link %i/%i, " \
  10368. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10369. current_config->name.tu, \
  10370. current_config->name.gmch_m, \
  10371. current_config->name.gmch_n, \
  10372. current_config->name.link_m, \
  10373. current_config->name.link_n, \
  10374. current_config->alt_name.tu, \
  10375. current_config->alt_name.gmch_m, \
  10376. current_config->alt_name.gmch_n, \
  10377. current_config->alt_name.link_m, \
  10378. current_config->alt_name.link_n, \
  10379. pipe_config->name.tu, \
  10380. pipe_config->name.gmch_m, \
  10381. pipe_config->name.gmch_n, \
  10382. pipe_config->name.link_m, \
  10383. pipe_config->name.link_n); \
  10384. ret = false; \
  10385. }
  10386. /* This is required for BDW+ where there is only one set of registers for
  10387. * switching between high and low RR.
  10388. * This macro can be used whenever a comparison has to be made between one
  10389. * hw state and multiple sw state variables.
  10390. */
  10391. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10392. if ((current_config->name != pipe_config->name) && \
  10393. (current_config->alt_name != pipe_config->name)) { \
  10394. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10395. "(expected %i or %i, found %i)\n", \
  10396. current_config->name, \
  10397. current_config->alt_name, \
  10398. pipe_config->name); \
  10399. ret = false; \
  10400. }
  10401. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10402. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10403. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10404. "(expected %i, found %i)\n", \
  10405. current_config->name & (mask), \
  10406. pipe_config->name & (mask)); \
  10407. ret = false; \
  10408. }
  10409. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10410. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10411. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10412. "(expected %i, found %i)\n", \
  10413. current_config->name, \
  10414. pipe_config->name); \
  10415. ret = false; \
  10416. }
  10417. #define PIPE_CONF_QUIRK(quirk) \
  10418. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10419. PIPE_CONF_CHECK_I(cpu_transcoder);
  10420. PIPE_CONF_CHECK_I(has_pch_encoder);
  10421. PIPE_CONF_CHECK_I(fdi_lanes);
  10422. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10423. PIPE_CONF_CHECK_I(has_dp_encoder);
  10424. if (INTEL_INFO(dev)->gen < 8) {
  10425. PIPE_CONF_CHECK_M_N(dp_m_n);
  10426. PIPE_CONF_CHECK_I(has_drrs);
  10427. if (current_config->has_drrs)
  10428. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10429. } else
  10430. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10431. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10432. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10433. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10434. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10435. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10436. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10437. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10438. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10439. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10440. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10441. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10442. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10443. PIPE_CONF_CHECK_I(pixel_multiplier);
  10444. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10445. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10446. IS_VALLEYVIEW(dev))
  10447. PIPE_CONF_CHECK_I(limited_color_range);
  10448. PIPE_CONF_CHECK_I(has_infoframe);
  10449. PIPE_CONF_CHECK_I(has_audio);
  10450. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10451. DRM_MODE_FLAG_INTERLACE);
  10452. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10453. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10454. DRM_MODE_FLAG_PHSYNC);
  10455. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10456. DRM_MODE_FLAG_NHSYNC);
  10457. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10458. DRM_MODE_FLAG_PVSYNC);
  10459. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10460. DRM_MODE_FLAG_NVSYNC);
  10461. }
  10462. PIPE_CONF_CHECK_I(pipe_src_w);
  10463. PIPE_CONF_CHECK_I(pipe_src_h);
  10464. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10465. /* pfit ratios are autocomputed by the hw on gen4+ */
  10466. if (INTEL_INFO(dev)->gen < 4)
  10467. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10468. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10469. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10470. if (current_config->pch_pfit.enabled) {
  10471. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10472. PIPE_CONF_CHECK_I(pch_pfit.size);
  10473. }
  10474. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10475. /* BDW+ don't expose a synchronous way to read the state */
  10476. if (IS_HASWELL(dev))
  10477. PIPE_CONF_CHECK_I(ips_enabled);
  10478. PIPE_CONF_CHECK_I(double_wide);
  10479. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10480. PIPE_CONF_CHECK_I(shared_dpll);
  10481. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10482. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10483. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10484. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10485. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10486. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10487. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10488. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10489. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10490. PIPE_CONF_CHECK_I(pipe_bpp);
  10491. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10492. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10493. #undef PIPE_CONF_CHECK_X
  10494. #undef PIPE_CONF_CHECK_I
  10495. #undef PIPE_CONF_CHECK_I_ALT
  10496. #undef PIPE_CONF_CHECK_FLAGS
  10497. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10498. #undef PIPE_CONF_QUIRK
  10499. #undef INTEL_ERR_OR_DBG_KMS
  10500. return ret;
  10501. }
  10502. static void check_wm_state(struct drm_device *dev)
  10503. {
  10504. struct drm_i915_private *dev_priv = dev->dev_private;
  10505. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10506. struct intel_crtc *intel_crtc;
  10507. int plane;
  10508. if (INTEL_INFO(dev)->gen < 9)
  10509. return;
  10510. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10511. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10512. for_each_intel_crtc(dev, intel_crtc) {
  10513. struct skl_ddb_entry *hw_entry, *sw_entry;
  10514. const enum pipe pipe = intel_crtc->pipe;
  10515. if (!intel_crtc->active)
  10516. continue;
  10517. /* planes */
  10518. for_each_plane(dev_priv, pipe, plane) {
  10519. hw_entry = &hw_ddb.plane[pipe][plane];
  10520. sw_entry = &sw_ddb->plane[pipe][plane];
  10521. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10522. continue;
  10523. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10524. "(expected (%u,%u), found (%u,%u))\n",
  10525. pipe_name(pipe), plane + 1,
  10526. sw_entry->start, sw_entry->end,
  10527. hw_entry->start, hw_entry->end);
  10528. }
  10529. /* cursor */
  10530. hw_entry = &hw_ddb.cursor[pipe];
  10531. sw_entry = &sw_ddb->cursor[pipe];
  10532. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10533. continue;
  10534. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10535. "(expected (%u,%u), found (%u,%u))\n",
  10536. pipe_name(pipe),
  10537. sw_entry->start, sw_entry->end,
  10538. hw_entry->start, hw_entry->end);
  10539. }
  10540. }
  10541. static void
  10542. check_connector_state(struct drm_device *dev,
  10543. struct drm_atomic_state *old_state)
  10544. {
  10545. struct drm_connector_state *old_conn_state;
  10546. struct drm_connector *connector;
  10547. int i;
  10548. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10549. struct drm_encoder *encoder = connector->encoder;
  10550. struct drm_connector_state *state = connector->state;
  10551. /* This also checks the encoder/connector hw state with the
  10552. * ->get_hw_state callbacks. */
  10553. intel_connector_check_state(to_intel_connector(connector));
  10554. I915_STATE_WARN(state->best_encoder != encoder,
  10555. "connector's atomic encoder doesn't match legacy encoder\n");
  10556. }
  10557. }
  10558. static void
  10559. check_encoder_state(struct drm_device *dev)
  10560. {
  10561. struct intel_encoder *encoder;
  10562. struct intel_connector *connector;
  10563. for_each_intel_encoder(dev, encoder) {
  10564. bool enabled = false;
  10565. enum pipe pipe;
  10566. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10567. encoder->base.base.id,
  10568. encoder->base.name);
  10569. for_each_intel_connector(dev, connector) {
  10570. if (connector->base.state->best_encoder != &encoder->base)
  10571. continue;
  10572. enabled = true;
  10573. I915_STATE_WARN(connector->base.state->crtc !=
  10574. encoder->base.crtc,
  10575. "connector's crtc doesn't match encoder crtc\n");
  10576. }
  10577. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10578. "encoder's enabled state mismatch "
  10579. "(expected %i, found %i)\n",
  10580. !!encoder->base.crtc, enabled);
  10581. if (!encoder->base.crtc) {
  10582. bool active;
  10583. active = encoder->get_hw_state(encoder, &pipe);
  10584. I915_STATE_WARN(active,
  10585. "encoder detached but still enabled on pipe %c.\n",
  10586. pipe_name(pipe));
  10587. }
  10588. }
  10589. }
  10590. static void
  10591. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10592. {
  10593. struct drm_i915_private *dev_priv = dev->dev_private;
  10594. struct intel_encoder *encoder;
  10595. struct drm_crtc_state *old_crtc_state;
  10596. struct drm_crtc *crtc;
  10597. int i;
  10598. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10599. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10600. struct intel_crtc_state *pipe_config, *sw_config;
  10601. bool active;
  10602. if (!needs_modeset(crtc->state))
  10603. continue;
  10604. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10605. pipe_config = to_intel_crtc_state(old_crtc_state);
  10606. memset(pipe_config, 0, sizeof(*pipe_config));
  10607. pipe_config->base.crtc = crtc;
  10608. pipe_config->base.state = old_state;
  10609. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10610. crtc->base.id);
  10611. active = dev_priv->display.get_pipe_config(intel_crtc,
  10612. pipe_config);
  10613. /* hw state is inconsistent with the pipe quirk */
  10614. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10615. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10616. active = crtc->state->active;
  10617. I915_STATE_WARN(crtc->state->active != active,
  10618. "crtc active state doesn't match with hw state "
  10619. "(expected %i, found %i)\n", crtc->state->active, active);
  10620. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10621. "transitional active state does not match atomic hw state "
  10622. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10623. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10624. enum pipe pipe;
  10625. active = encoder->get_hw_state(encoder, &pipe);
  10626. I915_STATE_WARN(active != crtc->state->active,
  10627. "[ENCODER:%i] active %i with crtc active %i\n",
  10628. encoder->base.base.id, active, crtc->state->active);
  10629. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10630. "Encoder connected to wrong pipe %c\n",
  10631. pipe_name(pipe));
  10632. if (active)
  10633. encoder->get_config(encoder, pipe_config);
  10634. }
  10635. if (!crtc->state->active)
  10636. continue;
  10637. sw_config = to_intel_crtc_state(crtc->state);
  10638. if (!intel_pipe_config_compare(dev, sw_config,
  10639. pipe_config, false)) {
  10640. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10641. intel_dump_pipe_config(intel_crtc, pipe_config,
  10642. "[hw state]");
  10643. intel_dump_pipe_config(intel_crtc, sw_config,
  10644. "[sw state]");
  10645. }
  10646. }
  10647. }
  10648. static void
  10649. check_shared_dpll_state(struct drm_device *dev)
  10650. {
  10651. struct drm_i915_private *dev_priv = dev->dev_private;
  10652. struct intel_crtc *crtc;
  10653. struct intel_dpll_hw_state dpll_hw_state;
  10654. int i;
  10655. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10656. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10657. int enabled_crtcs = 0, active_crtcs = 0;
  10658. bool active;
  10659. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10660. DRM_DEBUG_KMS("%s\n", pll->name);
  10661. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10662. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10663. "more active pll users than references: %i vs %i\n",
  10664. pll->active, hweight32(pll->config.crtc_mask));
  10665. I915_STATE_WARN(pll->active && !pll->on,
  10666. "pll in active use but not on in sw tracking\n");
  10667. I915_STATE_WARN(pll->on && !pll->active,
  10668. "pll in on but not on in use in sw tracking\n");
  10669. I915_STATE_WARN(pll->on != active,
  10670. "pll on state mismatch (expected %i, found %i)\n",
  10671. pll->on, active);
  10672. for_each_intel_crtc(dev, crtc) {
  10673. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10674. enabled_crtcs++;
  10675. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10676. active_crtcs++;
  10677. }
  10678. I915_STATE_WARN(pll->active != active_crtcs,
  10679. "pll active crtcs mismatch (expected %i, found %i)\n",
  10680. pll->active, active_crtcs);
  10681. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10682. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10683. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10684. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10685. sizeof(dpll_hw_state)),
  10686. "pll hw state mismatch\n");
  10687. }
  10688. }
  10689. static void
  10690. intel_modeset_check_state(struct drm_device *dev,
  10691. struct drm_atomic_state *old_state)
  10692. {
  10693. check_wm_state(dev);
  10694. check_connector_state(dev, old_state);
  10695. check_encoder_state(dev);
  10696. check_crtc_state(dev, old_state);
  10697. check_shared_dpll_state(dev);
  10698. }
  10699. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10700. int dotclock)
  10701. {
  10702. /*
  10703. * FDI already provided one idea for the dotclock.
  10704. * Yell if the encoder disagrees.
  10705. */
  10706. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10707. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10708. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10709. }
  10710. static void update_scanline_offset(struct intel_crtc *crtc)
  10711. {
  10712. struct drm_device *dev = crtc->base.dev;
  10713. /*
  10714. * The scanline counter increments at the leading edge of hsync.
  10715. *
  10716. * On most platforms it starts counting from vtotal-1 on the
  10717. * first active line. That means the scanline counter value is
  10718. * always one less than what we would expect. Ie. just after
  10719. * start of vblank, which also occurs at start of hsync (on the
  10720. * last active line), the scanline counter will read vblank_start-1.
  10721. *
  10722. * On gen2 the scanline counter starts counting from 1 instead
  10723. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10724. * to keep the value positive), instead of adding one.
  10725. *
  10726. * On HSW+ the behaviour of the scanline counter depends on the output
  10727. * type. For DP ports it behaves like most other platforms, but on HDMI
  10728. * there's an extra 1 line difference. So we need to add two instead of
  10729. * one to the value.
  10730. */
  10731. if (IS_GEN2(dev)) {
  10732. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10733. int vtotal;
  10734. vtotal = mode->crtc_vtotal;
  10735. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10736. vtotal /= 2;
  10737. crtc->scanline_offset = vtotal - 1;
  10738. } else if (HAS_DDI(dev) &&
  10739. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10740. crtc->scanline_offset = 2;
  10741. } else
  10742. crtc->scanline_offset = 1;
  10743. }
  10744. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10745. {
  10746. struct drm_device *dev = state->dev;
  10747. struct drm_i915_private *dev_priv = to_i915(dev);
  10748. struct intel_shared_dpll_config *shared_dpll = NULL;
  10749. struct intel_crtc *intel_crtc;
  10750. struct intel_crtc_state *intel_crtc_state;
  10751. struct drm_crtc *crtc;
  10752. struct drm_crtc_state *crtc_state;
  10753. int i;
  10754. if (!dev_priv->display.crtc_compute_clock)
  10755. return;
  10756. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10757. int dpll;
  10758. intel_crtc = to_intel_crtc(crtc);
  10759. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10760. dpll = intel_crtc_state->shared_dpll;
  10761. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10762. continue;
  10763. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10764. if (!shared_dpll)
  10765. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10766. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10767. }
  10768. }
  10769. /*
  10770. * This implements the workaround described in the "notes" section of the mode
  10771. * set sequence documentation. When going from no pipes or single pipe to
  10772. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10773. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10774. */
  10775. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10776. {
  10777. struct drm_crtc_state *crtc_state;
  10778. struct intel_crtc *intel_crtc;
  10779. struct drm_crtc *crtc;
  10780. struct intel_crtc_state *first_crtc_state = NULL;
  10781. struct intel_crtc_state *other_crtc_state = NULL;
  10782. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10783. int i;
  10784. /* look at all crtc's that are going to be enabled in during modeset */
  10785. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10786. intel_crtc = to_intel_crtc(crtc);
  10787. if (!crtc_state->active || !needs_modeset(crtc_state))
  10788. continue;
  10789. if (first_crtc_state) {
  10790. other_crtc_state = to_intel_crtc_state(crtc_state);
  10791. break;
  10792. } else {
  10793. first_crtc_state = to_intel_crtc_state(crtc_state);
  10794. first_pipe = intel_crtc->pipe;
  10795. }
  10796. }
  10797. /* No workaround needed? */
  10798. if (!first_crtc_state)
  10799. return 0;
  10800. /* w/a possibly needed, check how many crtc's are already enabled. */
  10801. for_each_intel_crtc(state->dev, intel_crtc) {
  10802. struct intel_crtc_state *pipe_config;
  10803. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10804. if (IS_ERR(pipe_config))
  10805. return PTR_ERR(pipe_config);
  10806. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10807. if (!pipe_config->base.active ||
  10808. needs_modeset(&pipe_config->base))
  10809. continue;
  10810. /* 2 or more enabled crtcs means no need for w/a */
  10811. if (enabled_pipe != INVALID_PIPE)
  10812. return 0;
  10813. enabled_pipe = intel_crtc->pipe;
  10814. }
  10815. if (enabled_pipe != INVALID_PIPE)
  10816. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10817. else if (other_crtc_state)
  10818. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10819. return 0;
  10820. }
  10821. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10822. {
  10823. struct drm_crtc *crtc;
  10824. struct drm_crtc_state *crtc_state;
  10825. int ret = 0;
  10826. /* add all active pipes to the state */
  10827. for_each_crtc(state->dev, crtc) {
  10828. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10829. if (IS_ERR(crtc_state))
  10830. return PTR_ERR(crtc_state);
  10831. if (!crtc_state->active || needs_modeset(crtc_state))
  10832. continue;
  10833. crtc_state->mode_changed = true;
  10834. ret = drm_atomic_add_affected_connectors(state, crtc);
  10835. if (ret)
  10836. break;
  10837. ret = drm_atomic_add_affected_planes(state, crtc);
  10838. if (ret)
  10839. break;
  10840. }
  10841. return ret;
  10842. }
  10843. static int intel_modeset_checks(struct drm_atomic_state *state)
  10844. {
  10845. struct drm_device *dev = state->dev;
  10846. struct drm_i915_private *dev_priv = dev->dev_private;
  10847. int ret;
  10848. if (!check_digital_port_conflicts(state)) {
  10849. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10850. return -EINVAL;
  10851. }
  10852. /*
  10853. * See if the config requires any additional preparation, e.g.
  10854. * to adjust global state with pipes off. We need to do this
  10855. * here so we can get the modeset_pipe updated config for the new
  10856. * mode set on this crtc. For other crtcs we need to use the
  10857. * adjusted_mode bits in the crtc directly.
  10858. */
  10859. if (dev_priv->display.modeset_calc_cdclk) {
  10860. unsigned int cdclk;
  10861. ret = dev_priv->display.modeset_calc_cdclk(state);
  10862. cdclk = to_intel_atomic_state(state)->cdclk;
  10863. if (!ret && cdclk != dev_priv->cdclk_freq)
  10864. ret = intel_modeset_all_pipes(state);
  10865. if (ret < 0)
  10866. return ret;
  10867. } else
  10868. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10869. intel_modeset_clear_plls(state);
  10870. if (IS_HASWELL(dev))
  10871. return haswell_mode_set_planes_workaround(state);
  10872. return 0;
  10873. }
  10874. /**
  10875. * intel_atomic_check - validate state object
  10876. * @dev: drm device
  10877. * @state: state to validate
  10878. */
  10879. static int intel_atomic_check(struct drm_device *dev,
  10880. struct drm_atomic_state *state)
  10881. {
  10882. struct drm_crtc *crtc;
  10883. struct drm_crtc_state *crtc_state;
  10884. int ret, i;
  10885. bool any_ms = false;
  10886. ret = drm_atomic_helper_check_modeset(dev, state);
  10887. if (ret)
  10888. return ret;
  10889. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10890. struct intel_crtc_state *pipe_config =
  10891. to_intel_crtc_state(crtc_state);
  10892. /* Catch I915_MODE_FLAG_INHERITED */
  10893. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10894. crtc_state->mode_changed = true;
  10895. if (!crtc_state->enable) {
  10896. if (needs_modeset(crtc_state))
  10897. any_ms = true;
  10898. continue;
  10899. }
  10900. if (!needs_modeset(crtc_state))
  10901. continue;
  10902. /* FIXME: For only active_changed we shouldn't need to do any
  10903. * state recomputation at all. */
  10904. ret = drm_atomic_add_affected_connectors(state, crtc);
  10905. if (ret)
  10906. return ret;
  10907. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10908. if (ret)
  10909. return ret;
  10910. if (i915.fastboot &&
  10911. intel_pipe_config_compare(state->dev,
  10912. to_intel_crtc_state(crtc->state),
  10913. pipe_config, true)) {
  10914. crtc_state->mode_changed = false;
  10915. }
  10916. if (needs_modeset(crtc_state)) {
  10917. any_ms = true;
  10918. ret = drm_atomic_add_affected_planes(state, crtc);
  10919. if (ret)
  10920. return ret;
  10921. }
  10922. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10923. needs_modeset(crtc_state) ?
  10924. "[modeset]" : "[fastset]");
  10925. }
  10926. if (any_ms) {
  10927. ret = intel_modeset_checks(state);
  10928. if (ret)
  10929. return ret;
  10930. } else
  10931. to_intel_atomic_state(state)->cdclk =
  10932. to_i915(state->dev)->cdclk_freq;
  10933. return drm_atomic_helper_check_planes(state->dev, state);
  10934. }
  10935. /**
  10936. * intel_atomic_commit - commit validated state object
  10937. * @dev: DRM device
  10938. * @state: the top-level driver state object
  10939. * @async: asynchronous commit
  10940. *
  10941. * This function commits a top-level state object that has been validated
  10942. * with drm_atomic_helper_check().
  10943. *
  10944. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  10945. * we can only handle plane-related operations and do not yet support
  10946. * asynchronous commit.
  10947. *
  10948. * RETURNS
  10949. * Zero for success or -errno.
  10950. */
  10951. static int intel_atomic_commit(struct drm_device *dev,
  10952. struct drm_atomic_state *state,
  10953. bool async)
  10954. {
  10955. struct drm_i915_private *dev_priv = dev->dev_private;
  10956. struct drm_crtc *crtc;
  10957. struct drm_crtc_state *crtc_state;
  10958. int ret = 0;
  10959. int i;
  10960. bool any_ms = false;
  10961. if (async) {
  10962. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  10963. return -EINVAL;
  10964. }
  10965. ret = drm_atomic_helper_prepare_planes(dev, state);
  10966. if (ret)
  10967. return ret;
  10968. drm_atomic_helper_swap_state(dev, state);
  10969. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10970. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10971. if (!needs_modeset(crtc->state))
  10972. continue;
  10973. any_ms = true;
  10974. intel_pre_plane_update(intel_crtc);
  10975. if (crtc_state->active) {
  10976. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  10977. dev_priv->display.crtc_disable(crtc);
  10978. intel_crtc->active = false;
  10979. intel_disable_shared_dpll(intel_crtc);
  10980. }
  10981. }
  10982. /* Only after disabling all output pipelines that will be changed can we
  10983. * update the the output configuration. */
  10984. intel_modeset_update_crtc_state(state);
  10985. if (any_ms) {
  10986. intel_shared_dpll_commit(state);
  10987. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10988. modeset_update_crtc_power_domains(state);
  10989. }
  10990. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10991. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10992. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10993. bool modeset = needs_modeset(crtc->state);
  10994. if (modeset && crtc->state->active) {
  10995. update_scanline_offset(to_intel_crtc(crtc));
  10996. dev_priv->display.crtc_enable(crtc);
  10997. }
  10998. if (!modeset)
  10999. intel_pre_plane_update(intel_crtc);
  11000. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11001. intel_post_plane_update(intel_crtc);
  11002. }
  11003. /* FIXME: add subpixel order */
  11004. drm_atomic_helper_wait_for_vblanks(dev, state);
  11005. drm_atomic_helper_cleanup_planes(dev, state);
  11006. if (any_ms)
  11007. intel_modeset_check_state(dev, state);
  11008. drm_atomic_state_free(state);
  11009. return 0;
  11010. }
  11011. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11012. {
  11013. struct drm_device *dev = crtc->dev;
  11014. struct drm_atomic_state *state;
  11015. struct drm_crtc_state *crtc_state;
  11016. int ret;
  11017. state = drm_atomic_state_alloc(dev);
  11018. if (!state) {
  11019. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11020. crtc->base.id);
  11021. return;
  11022. }
  11023. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11024. retry:
  11025. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11026. ret = PTR_ERR_OR_ZERO(crtc_state);
  11027. if (!ret) {
  11028. if (!crtc_state->active)
  11029. goto out;
  11030. crtc_state->mode_changed = true;
  11031. ret = drm_atomic_commit(state);
  11032. }
  11033. if (ret == -EDEADLK) {
  11034. drm_atomic_state_clear(state);
  11035. drm_modeset_backoff(state->acquire_ctx);
  11036. goto retry;
  11037. }
  11038. if (ret)
  11039. out:
  11040. drm_atomic_state_free(state);
  11041. }
  11042. #undef for_each_intel_crtc_masked
  11043. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11044. .gamma_set = intel_crtc_gamma_set,
  11045. .set_config = drm_atomic_helper_set_config,
  11046. .destroy = intel_crtc_destroy,
  11047. .page_flip = intel_crtc_page_flip,
  11048. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11049. .atomic_destroy_state = intel_crtc_destroy_state,
  11050. };
  11051. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11052. struct intel_shared_dpll *pll,
  11053. struct intel_dpll_hw_state *hw_state)
  11054. {
  11055. uint32_t val;
  11056. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11057. return false;
  11058. val = I915_READ(PCH_DPLL(pll->id));
  11059. hw_state->dpll = val;
  11060. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11061. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11062. return val & DPLL_VCO_ENABLE;
  11063. }
  11064. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11065. struct intel_shared_dpll *pll)
  11066. {
  11067. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11068. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11069. }
  11070. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11071. struct intel_shared_dpll *pll)
  11072. {
  11073. /* PCH refclock must be enabled first */
  11074. ibx_assert_pch_refclk_enabled(dev_priv);
  11075. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11076. /* Wait for the clocks to stabilize. */
  11077. POSTING_READ(PCH_DPLL(pll->id));
  11078. udelay(150);
  11079. /* The pixel multiplier can only be updated once the
  11080. * DPLL is enabled and the clocks are stable.
  11081. *
  11082. * So write it again.
  11083. */
  11084. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11085. POSTING_READ(PCH_DPLL(pll->id));
  11086. udelay(200);
  11087. }
  11088. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11089. struct intel_shared_dpll *pll)
  11090. {
  11091. struct drm_device *dev = dev_priv->dev;
  11092. struct intel_crtc *crtc;
  11093. /* Make sure no transcoder isn't still depending on us. */
  11094. for_each_intel_crtc(dev, crtc) {
  11095. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11096. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11097. }
  11098. I915_WRITE(PCH_DPLL(pll->id), 0);
  11099. POSTING_READ(PCH_DPLL(pll->id));
  11100. udelay(200);
  11101. }
  11102. static char *ibx_pch_dpll_names[] = {
  11103. "PCH DPLL A",
  11104. "PCH DPLL B",
  11105. };
  11106. static void ibx_pch_dpll_init(struct drm_device *dev)
  11107. {
  11108. struct drm_i915_private *dev_priv = dev->dev_private;
  11109. int i;
  11110. dev_priv->num_shared_dpll = 2;
  11111. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11112. dev_priv->shared_dplls[i].id = i;
  11113. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11114. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11115. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11116. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11117. dev_priv->shared_dplls[i].get_hw_state =
  11118. ibx_pch_dpll_get_hw_state;
  11119. }
  11120. }
  11121. static void intel_shared_dpll_init(struct drm_device *dev)
  11122. {
  11123. struct drm_i915_private *dev_priv = dev->dev_private;
  11124. intel_update_cdclk(dev);
  11125. if (HAS_DDI(dev))
  11126. intel_ddi_pll_init(dev);
  11127. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11128. ibx_pch_dpll_init(dev);
  11129. else
  11130. dev_priv->num_shared_dpll = 0;
  11131. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11132. }
  11133. /**
  11134. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11135. * @plane: drm plane to prepare for
  11136. * @fb: framebuffer to prepare for presentation
  11137. *
  11138. * Prepares a framebuffer for usage on a display plane. Generally this
  11139. * involves pinning the underlying object and updating the frontbuffer tracking
  11140. * bits. Some older platforms need special physical address handling for
  11141. * cursor planes.
  11142. *
  11143. * Returns 0 on success, negative error code on failure.
  11144. */
  11145. int
  11146. intel_prepare_plane_fb(struct drm_plane *plane,
  11147. struct drm_framebuffer *fb,
  11148. const struct drm_plane_state *new_state)
  11149. {
  11150. struct drm_device *dev = plane->dev;
  11151. struct intel_plane *intel_plane = to_intel_plane(plane);
  11152. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11153. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11154. int ret = 0;
  11155. if (!obj)
  11156. return 0;
  11157. mutex_lock(&dev->struct_mutex);
  11158. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11159. INTEL_INFO(dev)->cursor_needs_physical) {
  11160. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11161. ret = i915_gem_object_attach_phys(obj, align);
  11162. if (ret)
  11163. DRM_DEBUG_KMS("failed to attach phys object\n");
  11164. } else {
  11165. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11166. }
  11167. if (ret == 0)
  11168. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11169. mutex_unlock(&dev->struct_mutex);
  11170. return ret;
  11171. }
  11172. /**
  11173. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11174. * @plane: drm plane to clean up for
  11175. * @fb: old framebuffer that was on plane
  11176. *
  11177. * Cleans up a framebuffer that has just been removed from a plane.
  11178. */
  11179. void
  11180. intel_cleanup_plane_fb(struct drm_plane *plane,
  11181. struct drm_framebuffer *fb,
  11182. const struct drm_plane_state *old_state)
  11183. {
  11184. struct drm_device *dev = plane->dev;
  11185. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11186. if (WARN_ON(!obj))
  11187. return;
  11188. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11189. !INTEL_INFO(dev)->cursor_needs_physical) {
  11190. mutex_lock(&dev->struct_mutex);
  11191. intel_unpin_fb_obj(fb, old_state);
  11192. mutex_unlock(&dev->struct_mutex);
  11193. }
  11194. }
  11195. int
  11196. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11197. {
  11198. int max_scale;
  11199. struct drm_device *dev;
  11200. struct drm_i915_private *dev_priv;
  11201. int crtc_clock, cdclk;
  11202. if (!intel_crtc || !crtc_state)
  11203. return DRM_PLANE_HELPER_NO_SCALING;
  11204. dev = intel_crtc->base.dev;
  11205. dev_priv = dev->dev_private;
  11206. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11207. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11208. if (!crtc_clock || !cdclk)
  11209. return DRM_PLANE_HELPER_NO_SCALING;
  11210. /*
  11211. * skl max scale is lower of:
  11212. * close to 3 but not 3, -1 is for that purpose
  11213. * or
  11214. * cdclk/crtc_clock
  11215. */
  11216. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11217. return max_scale;
  11218. }
  11219. static int
  11220. intel_check_primary_plane(struct drm_plane *plane,
  11221. struct intel_crtc_state *crtc_state,
  11222. struct intel_plane_state *state)
  11223. {
  11224. struct drm_crtc *crtc = state->base.crtc;
  11225. struct drm_framebuffer *fb = state->base.fb;
  11226. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11227. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11228. bool can_position = false;
  11229. /* use scaler when colorkey is not required */
  11230. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11231. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11232. min_scale = 1;
  11233. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11234. can_position = true;
  11235. }
  11236. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11237. &state->dst, &state->clip,
  11238. min_scale, max_scale,
  11239. can_position, true,
  11240. &state->visible);
  11241. }
  11242. static void
  11243. intel_commit_primary_plane(struct drm_plane *plane,
  11244. struct intel_plane_state *state)
  11245. {
  11246. struct drm_crtc *crtc = state->base.crtc;
  11247. struct drm_framebuffer *fb = state->base.fb;
  11248. struct drm_device *dev = plane->dev;
  11249. struct drm_i915_private *dev_priv = dev->dev_private;
  11250. struct intel_crtc *intel_crtc;
  11251. struct drm_rect *src = &state->src;
  11252. crtc = crtc ? crtc : plane->crtc;
  11253. intel_crtc = to_intel_crtc(crtc);
  11254. plane->fb = fb;
  11255. crtc->x = src->x1 >> 16;
  11256. crtc->y = src->y1 >> 16;
  11257. if (!crtc->state->active)
  11258. return;
  11259. if (state->visible)
  11260. /* FIXME: kill this fastboot hack */
  11261. intel_update_pipe_size(intel_crtc);
  11262. dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
  11263. }
  11264. static void
  11265. intel_disable_primary_plane(struct drm_plane *plane,
  11266. struct drm_crtc *crtc)
  11267. {
  11268. struct drm_device *dev = plane->dev;
  11269. struct drm_i915_private *dev_priv = dev->dev_private;
  11270. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11271. }
  11272. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11273. struct drm_crtc_state *old_crtc_state)
  11274. {
  11275. struct drm_device *dev = crtc->dev;
  11276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11277. if (intel_crtc->atomic.update_wm_pre)
  11278. intel_update_watermarks(crtc);
  11279. /* Perform vblank evasion around commit operation */
  11280. if (crtc->state->active)
  11281. intel_pipe_update_start(intel_crtc, &intel_crtc->start_vbl_count);
  11282. if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
  11283. skl_detach_scalers(intel_crtc);
  11284. }
  11285. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11286. struct drm_crtc_state *old_crtc_state)
  11287. {
  11288. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11289. if (crtc->state->active)
  11290. intel_pipe_update_end(intel_crtc, intel_crtc->start_vbl_count);
  11291. }
  11292. /**
  11293. * intel_plane_destroy - destroy a plane
  11294. * @plane: plane to destroy
  11295. *
  11296. * Common destruction function for all types of planes (primary, cursor,
  11297. * sprite).
  11298. */
  11299. void intel_plane_destroy(struct drm_plane *plane)
  11300. {
  11301. struct intel_plane *intel_plane = to_intel_plane(plane);
  11302. drm_plane_cleanup(plane);
  11303. kfree(intel_plane);
  11304. }
  11305. const struct drm_plane_funcs intel_plane_funcs = {
  11306. .update_plane = drm_atomic_helper_update_plane,
  11307. .disable_plane = drm_atomic_helper_disable_plane,
  11308. .destroy = intel_plane_destroy,
  11309. .set_property = drm_atomic_helper_plane_set_property,
  11310. .atomic_get_property = intel_plane_atomic_get_property,
  11311. .atomic_set_property = intel_plane_atomic_set_property,
  11312. .atomic_duplicate_state = intel_plane_duplicate_state,
  11313. .atomic_destroy_state = intel_plane_destroy_state,
  11314. };
  11315. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11316. int pipe)
  11317. {
  11318. struct intel_plane *primary;
  11319. struct intel_plane_state *state;
  11320. const uint32_t *intel_primary_formats;
  11321. int num_formats;
  11322. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11323. if (primary == NULL)
  11324. return NULL;
  11325. state = intel_create_plane_state(&primary->base);
  11326. if (!state) {
  11327. kfree(primary);
  11328. return NULL;
  11329. }
  11330. primary->base.state = &state->base;
  11331. primary->can_scale = false;
  11332. primary->max_downscale = 1;
  11333. if (INTEL_INFO(dev)->gen >= 9) {
  11334. primary->can_scale = true;
  11335. state->scaler_id = -1;
  11336. }
  11337. primary->pipe = pipe;
  11338. primary->plane = pipe;
  11339. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11340. primary->check_plane = intel_check_primary_plane;
  11341. primary->commit_plane = intel_commit_primary_plane;
  11342. primary->disable_plane = intel_disable_primary_plane;
  11343. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11344. primary->plane = !pipe;
  11345. if (INTEL_INFO(dev)->gen >= 9) {
  11346. intel_primary_formats = skl_primary_formats;
  11347. num_formats = ARRAY_SIZE(skl_primary_formats);
  11348. } else if (INTEL_INFO(dev)->gen >= 4) {
  11349. intel_primary_formats = i965_primary_formats;
  11350. num_formats = ARRAY_SIZE(i965_primary_formats);
  11351. } else {
  11352. intel_primary_formats = i8xx_primary_formats;
  11353. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11354. }
  11355. drm_universal_plane_init(dev, &primary->base, 0,
  11356. &intel_plane_funcs,
  11357. intel_primary_formats, num_formats,
  11358. DRM_PLANE_TYPE_PRIMARY);
  11359. if (INTEL_INFO(dev)->gen >= 4)
  11360. intel_create_rotation_property(dev, primary);
  11361. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11362. return &primary->base;
  11363. }
  11364. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11365. {
  11366. if (!dev->mode_config.rotation_property) {
  11367. unsigned long flags = BIT(DRM_ROTATE_0) |
  11368. BIT(DRM_ROTATE_180);
  11369. if (INTEL_INFO(dev)->gen >= 9)
  11370. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11371. dev->mode_config.rotation_property =
  11372. drm_mode_create_rotation_property(dev, flags);
  11373. }
  11374. if (dev->mode_config.rotation_property)
  11375. drm_object_attach_property(&plane->base.base,
  11376. dev->mode_config.rotation_property,
  11377. plane->base.state->rotation);
  11378. }
  11379. static int
  11380. intel_check_cursor_plane(struct drm_plane *plane,
  11381. struct intel_crtc_state *crtc_state,
  11382. struct intel_plane_state *state)
  11383. {
  11384. struct drm_crtc *crtc = crtc_state->base.crtc;
  11385. struct drm_framebuffer *fb = state->base.fb;
  11386. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11387. unsigned stride;
  11388. int ret;
  11389. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11390. &state->dst, &state->clip,
  11391. DRM_PLANE_HELPER_NO_SCALING,
  11392. DRM_PLANE_HELPER_NO_SCALING,
  11393. true, true, &state->visible);
  11394. if (ret)
  11395. return ret;
  11396. /* if we want to turn off the cursor ignore width and height */
  11397. if (!obj)
  11398. return 0;
  11399. /* Check for which cursor types we support */
  11400. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11401. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11402. state->base.crtc_w, state->base.crtc_h);
  11403. return -EINVAL;
  11404. }
  11405. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11406. if (obj->base.size < stride * state->base.crtc_h) {
  11407. DRM_DEBUG_KMS("buffer is too small\n");
  11408. return -ENOMEM;
  11409. }
  11410. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11411. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11412. return -EINVAL;
  11413. }
  11414. return 0;
  11415. }
  11416. static void
  11417. intel_disable_cursor_plane(struct drm_plane *plane,
  11418. struct drm_crtc *crtc)
  11419. {
  11420. intel_crtc_update_cursor(crtc, false);
  11421. }
  11422. static void
  11423. intel_commit_cursor_plane(struct drm_plane *plane,
  11424. struct intel_plane_state *state)
  11425. {
  11426. struct drm_crtc *crtc = state->base.crtc;
  11427. struct drm_device *dev = plane->dev;
  11428. struct intel_crtc *intel_crtc;
  11429. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11430. uint32_t addr;
  11431. crtc = crtc ? crtc : plane->crtc;
  11432. intel_crtc = to_intel_crtc(crtc);
  11433. plane->fb = state->base.fb;
  11434. crtc->cursor_x = state->base.crtc_x;
  11435. crtc->cursor_y = state->base.crtc_y;
  11436. if (intel_crtc->cursor_bo == obj)
  11437. goto update;
  11438. if (!obj)
  11439. addr = 0;
  11440. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11441. addr = i915_gem_obj_ggtt_offset(obj);
  11442. else
  11443. addr = obj->phys_handle->busaddr;
  11444. intel_crtc->cursor_addr = addr;
  11445. intel_crtc->cursor_bo = obj;
  11446. update:
  11447. if (crtc->state->active)
  11448. intel_crtc_update_cursor(crtc, state->visible);
  11449. }
  11450. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11451. int pipe)
  11452. {
  11453. struct intel_plane *cursor;
  11454. struct intel_plane_state *state;
  11455. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11456. if (cursor == NULL)
  11457. return NULL;
  11458. state = intel_create_plane_state(&cursor->base);
  11459. if (!state) {
  11460. kfree(cursor);
  11461. return NULL;
  11462. }
  11463. cursor->base.state = &state->base;
  11464. cursor->can_scale = false;
  11465. cursor->max_downscale = 1;
  11466. cursor->pipe = pipe;
  11467. cursor->plane = pipe;
  11468. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11469. cursor->check_plane = intel_check_cursor_plane;
  11470. cursor->commit_plane = intel_commit_cursor_plane;
  11471. cursor->disable_plane = intel_disable_cursor_plane;
  11472. drm_universal_plane_init(dev, &cursor->base, 0,
  11473. &intel_plane_funcs,
  11474. intel_cursor_formats,
  11475. ARRAY_SIZE(intel_cursor_formats),
  11476. DRM_PLANE_TYPE_CURSOR);
  11477. if (INTEL_INFO(dev)->gen >= 4) {
  11478. if (!dev->mode_config.rotation_property)
  11479. dev->mode_config.rotation_property =
  11480. drm_mode_create_rotation_property(dev,
  11481. BIT(DRM_ROTATE_0) |
  11482. BIT(DRM_ROTATE_180));
  11483. if (dev->mode_config.rotation_property)
  11484. drm_object_attach_property(&cursor->base.base,
  11485. dev->mode_config.rotation_property,
  11486. state->base.rotation);
  11487. }
  11488. if (INTEL_INFO(dev)->gen >=9)
  11489. state->scaler_id = -1;
  11490. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11491. return &cursor->base;
  11492. }
  11493. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11494. struct intel_crtc_state *crtc_state)
  11495. {
  11496. int i;
  11497. struct intel_scaler *intel_scaler;
  11498. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11499. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11500. intel_scaler = &scaler_state->scalers[i];
  11501. intel_scaler->in_use = 0;
  11502. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11503. }
  11504. scaler_state->scaler_id = -1;
  11505. }
  11506. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11507. {
  11508. struct drm_i915_private *dev_priv = dev->dev_private;
  11509. struct intel_crtc *intel_crtc;
  11510. struct intel_crtc_state *crtc_state = NULL;
  11511. struct drm_plane *primary = NULL;
  11512. struct drm_plane *cursor = NULL;
  11513. int i, ret;
  11514. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11515. if (intel_crtc == NULL)
  11516. return;
  11517. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11518. if (!crtc_state)
  11519. goto fail;
  11520. intel_crtc->config = crtc_state;
  11521. intel_crtc->base.state = &crtc_state->base;
  11522. crtc_state->base.crtc = &intel_crtc->base;
  11523. /* initialize shared scalers */
  11524. if (INTEL_INFO(dev)->gen >= 9) {
  11525. if (pipe == PIPE_C)
  11526. intel_crtc->num_scalers = 1;
  11527. else
  11528. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11529. skl_init_scalers(dev, intel_crtc, crtc_state);
  11530. }
  11531. primary = intel_primary_plane_create(dev, pipe);
  11532. if (!primary)
  11533. goto fail;
  11534. cursor = intel_cursor_plane_create(dev, pipe);
  11535. if (!cursor)
  11536. goto fail;
  11537. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11538. cursor, &intel_crtc_funcs);
  11539. if (ret)
  11540. goto fail;
  11541. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11542. for (i = 0; i < 256; i++) {
  11543. intel_crtc->lut_r[i] = i;
  11544. intel_crtc->lut_g[i] = i;
  11545. intel_crtc->lut_b[i] = i;
  11546. }
  11547. /*
  11548. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11549. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11550. */
  11551. intel_crtc->pipe = pipe;
  11552. intel_crtc->plane = pipe;
  11553. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11554. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11555. intel_crtc->plane = !pipe;
  11556. }
  11557. intel_crtc->cursor_base = ~0;
  11558. intel_crtc->cursor_cntl = ~0;
  11559. intel_crtc->cursor_size = ~0;
  11560. intel_crtc->wm.cxsr_allowed = true;
  11561. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11562. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11563. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11564. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11565. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11566. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11567. return;
  11568. fail:
  11569. if (primary)
  11570. drm_plane_cleanup(primary);
  11571. if (cursor)
  11572. drm_plane_cleanup(cursor);
  11573. kfree(crtc_state);
  11574. kfree(intel_crtc);
  11575. }
  11576. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11577. {
  11578. struct drm_encoder *encoder = connector->base.encoder;
  11579. struct drm_device *dev = connector->base.dev;
  11580. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11581. if (!encoder || WARN_ON(!encoder->crtc))
  11582. return INVALID_PIPE;
  11583. return to_intel_crtc(encoder->crtc)->pipe;
  11584. }
  11585. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11586. struct drm_file *file)
  11587. {
  11588. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11589. struct drm_crtc *drmmode_crtc;
  11590. struct intel_crtc *crtc;
  11591. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11592. if (!drmmode_crtc) {
  11593. DRM_ERROR("no such CRTC id\n");
  11594. return -ENOENT;
  11595. }
  11596. crtc = to_intel_crtc(drmmode_crtc);
  11597. pipe_from_crtc_id->pipe = crtc->pipe;
  11598. return 0;
  11599. }
  11600. static int intel_encoder_clones(struct intel_encoder *encoder)
  11601. {
  11602. struct drm_device *dev = encoder->base.dev;
  11603. struct intel_encoder *source_encoder;
  11604. int index_mask = 0;
  11605. int entry = 0;
  11606. for_each_intel_encoder(dev, source_encoder) {
  11607. if (encoders_cloneable(encoder, source_encoder))
  11608. index_mask |= (1 << entry);
  11609. entry++;
  11610. }
  11611. return index_mask;
  11612. }
  11613. static bool has_edp_a(struct drm_device *dev)
  11614. {
  11615. struct drm_i915_private *dev_priv = dev->dev_private;
  11616. if (!IS_MOBILE(dev))
  11617. return false;
  11618. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11619. return false;
  11620. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11621. return false;
  11622. return true;
  11623. }
  11624. static bool intel_crt_present(struct drm_device *dev)
  11625. {
  11626. struct drm_i915_private *dev_priv = dev->dev_private;
  11627. if (INTEL_INFO(dev)->gen >= 9)
  11628. return false;
  11629. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11630. return false;
  11631. if (IS_CHERRYVIEW(dev))
  11632. return false;
  11633. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11634. return false;
  11635. return true;
  11636. }
  11637. static void intel_setup_outputs(struct drm_device *dev)
  11638. {
  11639. struct drm_i915_private *dev_priv = dev->dev_private;
  11640. struct intel_encoder *encoder;
  11641. bool dpd_is_edp = false;
  11642. intel_lvds_init(dev);
  11643. if (intel_crt_present(dev))
  11644. intel_crt_init(dev);
  11645. if (IS_BROXTON(dev)) {
  11646. /*
  11647. * FIXME: Broxton doesn't support port detection via the
  11648. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11649. * detect the ports.
  11650. */
  11651. intel_ddi_init(dev, PORT_A);
  11652. intel_ddi_init(dev, PORT_B);
  11653. intel_ddi_init(dev, PORT_C);
  11654. } else if (HAS_DDI(dev)) {
  11655. int found;
  11656. /*
  11657. * Haswell uses DDI functions to detect digital outputs.
  11658. * On SKL pre-D0 the strap isn't connected, so we assume
  11659. * it's there.
  11660. */
  11661. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11662. /* WaIgnoreDDIAStrap: skl */
  11663. if (found ||
  11664. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11665. intel_ddi_init(dev, PORT_A);
  11666. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11667. * register */
  11668. found = I915_READ(SFUSE_STRAP);
  11669. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11670. intel_ddi_init(dev, PORT_B);
  11671. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11672. intel_ddi_init(dev, PORT_C);
  11673. if (found & SFUSE_STRAP_DDID_DETECTED)
  11674. intel_ddi_init(dev, PORT_D);
  11675. } else if (HAS_PCH_SPLIT(dev)) {
  11676. int found;
  11677. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11678. if (has_edp_a(dev))
  11679. intel_dp_init(dev, DP_A, PORT_A);
  11680. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11681. /* PCH SDVOB multiplex with HDMIB */
  11682. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11683. if (!found)
  11684. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11685. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11686. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11687. }
  11688. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11689. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11690. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11691. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11692. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11693. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11694. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11695. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11696. } else if (IS_VALLEYVIEW(dev)) {
  11697. /*
  11698. * The DP_DETECTED bit is the latched state of the DDC
  11699. * SDA pin at boot. However since eDP doesn't require DDC
  11700. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11701. * eDP ports may have been muxed to an alternate function.
  11702. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11703. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11704. * detect eDP ports.
  11705. */
  11706. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11707. !intel_dp_is_edp(dev, PORT_B))
  11708. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11709. PORT_B);
  11710. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11711. intel_dp_is_edp(dev, PORT_B))
  11712. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11713. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11714. !intel_dp_is_edp(dev, PORT_C))
  11715. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11716. PORT_C);
  11717. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11718. intel_dp_is_edp(dev, PORT_C))
  11719. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11720. if (IS_CHERRYVIEW(dev)) {
  11721. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11722. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11723. PORT_D);
  11724. /* eDP not supported on port D, so don't check VBT */
  11725. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11726. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11727. }
  11728. intel_dsi_init(dev);
  11729. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11730. bool found = false;
  11731. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11732. DRM_DEBUG_KMS("probing SDVOB\n");
  11733. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11734. if (!found && IS_G4X(dev)) {
  11735. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11736. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11737. }
  11738. if (!found && IS_G4X(dev))
  11739. intel_dp_init(dev, DP_B, PORT_B);
  11740. }
  11741. /* Before G4X SDVOC doesn't have its own detect register */
  11742. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11743. DRM_DEBUG_KMS("probing SDVOC\n");
  11744. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11745. }
  11746. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11747. if (IS_G4X(dev)) {
  11748. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11749. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11750. }
  11751. if (IS_G4X(dev))
  11752. intel_dp_init(dev, DP_C, PORT_C);
  11753. }
  11754. if (IS_G4X(dev) &&
  11755. (I915_READ(DP_D) & DP_DETECTED))
  11756. intel_dp_init(dev, DP_D, PORT_D);
  11757. } else if (IS_GEN2(dev))
  11758. intel_dvo_init(dev);
  11759. if (SUPPORTS_TV(dev))
  11760. intel_tv_init(dev);
  11761. intel_psr_init(dev);
  11762. for_each_intel_encoder(dev, encoder) {
  11763. encoder->base.possible_crtcs = encoder->crtc_mask;
  11764. encoder->base.possible_clones =
  11765. intel_encoder_clones(encoder);
  11766. }
  11767. intel_init_pch_refclk(dev);
  11768. drm_helper_move_panel_connectors_to_head(dev);
  11769. }
  11770. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11771. {
  11772. struct drm_device *dev = fb->dev;
  11773. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11774. drm_framebuffer_cleanup(fb);
  11775. mutex_lock(&dev->struct_mutex);
  11776. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11777. drm_gem_object_unreference(&intel_fb->obj->base);
  11778. mutex_unlock(&dev->struct_mutex);
  11779. kfree(intel_fb);
  11780. }
  11781. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11782. struct drm_file *file,
  11783. unsigned int *handle)
  11784. {
  11785. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11786. struct drm_i915_gem_object *obj = intel_fb->obj;
  11787. return drm_gem_handle_create(file, &obj->base, handle);
  11788. }
  11789. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11790. struct drm_file *file,
  11791. unsigned flags, unsigned color,
  11792. struct drm_clip_rect *clips,
  11793. unsigned num_clips)
  11794. {
  11795. struct drm_device *dev = fb->dev;
  11796. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11797. struct drm_i915_gem_object *obj = intel_fb->obj;
  11798. mutex_lock(&dev->struct_mutex);
  11799. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11800. mutex_unlock(&dev->struct_mutex);
  11801. return 0;
  11802. }
  11803. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11804. .destroy = intel_user_framebuffer_destroy,
  11805. .create_handle = intel_user_framebuffer_create_handle,
  11806. .dirty = intel_user_framebuffer_dirty,
  11807. };
  11808. static
  11809. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11810. uint32_t pixel_format)
  11811. {
  11812. u32 gen = INTEL_INFO(dev)->gen;
  11813. if (gen >= 9) {
  11814. /* "The stride in bytes must not exceed the of the size of 8K
  11815. * pixels and 32K bytes."
  11816. */
  11817. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11818. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11819. return 32*1024;
  11820. } else if (gen >= 4) {
  11821. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11822. return 16*1024;
  11823. else
  11824. return 32*1024;
  11825. } else if (gen >= 3) {
  11826. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11827. return 8*1024;
  11828. else
  11829. return 16*1024;
  11830. } else {
  11831. /* XXX DSPC is limited to 4k tiled */
  11832. return 8*1024;
  11833. }
  11834. }
  11835. static int intel_framebuffer_init(struct drm_device *dev,
  11836. struct intel_framebuffer *intel_fb,
  11837. struct drm_mode_fb_cmd2 *mode_cmd,
  11838. struct drm_i915_gem_object *obj)
  11839. {
  11840. unsigned int aligned_height;
  11841. int ret;
  11842. u32 pitch_limit, stride_alignment;
  11843. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11844. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11845. /* Enforce that fb modifier and tiling mode match, but only for
  11846. * X-tiled. This is needed for FBC. */
  11847. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11848. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11849. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11850. return -EINVAL;
  11851. }
  11852. } else {
  11853. if (obj->tiling_mode == I915_TILING_X)
  11854. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11855. else if (obj->tiling_mode == I915_TILING_Y) {
  11856. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11857. return -EINVAL;
  11858. }
  11859. }
  11860. /* Passed in modifier sanity checking. */
  11861. switch (mode_cmd->modifier[0]) {
  11862. case I915_FORMAT_MOD_Y_TILED:
  11863. case I915_FORMAT_MOD_Yf_TILED:
  11864. if (INTEL_INFO(dev)->gen < 9) {
  11865. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11866. mode_cmd->modifier[0]);
  11867. return -EINVAL;
  11868. }
  11869. case DRM_FORMAT_MOD_NONE:
  11870. case I915_FORMAT_MOD_X_TILED:
  11871. break;
  11872. default:
  11873. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11874. mode_cmd->modifier[0]);
  11875. return -EINVAL;
  11876. }
  11877. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11878. mode_cmd->pixel_format);
  11879. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11880. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11881. mode_cmd->pitches[0], stride_alignment);
  11882. return -EINVAL;
  11883. }
  11884. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11885. mode_cmd->pixel_format);
  11886. if (mode_cmd->pitches[0] > pitch_limit) {
  11887. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11888. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11889. "tiled" : "linear",
  11890. mode_cmd->pitches[0], pitch_limit);
  11891. return -EINVAL;
  11892. }
  11893. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11894. mode_cmd->pitches[0] != obj->stride) {
  11895. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11896. mode_cmd->pitches[0], obj->stride);
  11897. return -EINVAL;
  11898. }
  11899. /* Reject formats not supported by any plane early. */
  11900. switch (mode_cmd->pixel_format) {
  11901. case DRM_FORMAT_C8:
  11902. case DRM_FORMAT_RGB565:
  11903. case DRM_FORMAT_XRGB8888:
  11904. case DRM_FORMAT_ARGB8888:
  11905. break;
  11906. case DRM_FORMAT_XRGB1555:
  11907. if (INTEL_INFO(dev)->gen > 3) {
  11908. DRM_DEBUG("unsupported pixel format: %s\n",
  11909. drm_get_format_name(mode_cmd->pixel_format));
  11910. return -EINVAL;
  11911. }
  11912. break;
  11913. case DRM_FORMAT_ABGR8888:
  11914. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  11915. DRM_DEBUG("unsupported pixel format: %s\n",
  11916. drm_get_format_name(mode_cmd->pixel_format));
  11917. return -EINVAL;
  11918. }
  11919. break;
  11920. case DRM_FORMAT_XBGR8888:
  11921. case DRM_FORMAT_XRGB2101010:
  11922. case DRM_FORMAT_XBGR2101010:
  11923. if (INTEL_INFO(dev)->gen < 4) {
  11924. DRM_DEBUG("unsupported pixel format: %s\n",
  11925. drm_get_format_name(mode_cmd->pixel_format));
  11926. return -EINVAL;
  11927. }
  11928. break;
  11929. case DRM_FORMAT_ABGR2101010:
  11930. if (!IS_VALLEYVIEW(dev)) {
  11931. DRM_DEBUG("unsupported pixel format: %s\n",
  11932. drm_get_format_name(mode_cmd->pixel_format));
  11933. return -EINVAL;
  11934. }
  11935. break;
  11936. case DRM_FORMAT_YUYV:
  11937. case DRM_FORMAT_UYVY:
  11938. case DRM_FORMAT_YVYU:
  11939. case DRM_FORMAT_VYUY:
  11940. if (INTEL_INFO(dev)->gen < 5) {
  11941. DRM_DEBUG("unsupported pixel format: %s\n",
  11942. drm_get_format_name(mode_cmd->pixel_format));
  11943. return -EINVAL;
  11944. }
  11945. break;
  11946. default:
  11947. DRM_DEBUG("unsupported pixel format: %s\n",
  11948. drm_get_format_name(mode_cmd->pixel_format));
  11949. return -EINVAL;
  11950. }
  11951. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11952. if (mode_cmd->offsets[0] != 0)
  11953. return -EINVAL;
  11954. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11955. mode_cmd->pixel_format,
  11956. mode_cmd->modifier[0]);
  11957. /* FIXME drm helper for size checks (especially planar formats)? */
  11958. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11959. return -EINVAL;
  11960. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11961. intel_fb->obj = obj;
  11962. intel_fb->obj->framebuffer_references++;
  11963. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11964. if (ret) {
  11965. DRM_ERROR("framebuffer init failed %d\n", ret);
  11966. return ret;
  11967. }
  11968. return 0;
  11969. }
  11970. static struct drm_framebuffer *
  11971. intel_user_framebuffer_create(struct drm_device *dev,
  11972. struct drm_file *filp,
  11973. struct drm_mode_fb_cmd2 *mode_cmd)
  11974. {
  11975. struct drm_i915_gem_object *obj;
  11976. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11977. mode_cmd->handles[0]));
  11978. if (&obj->base == NULL)
  11979. return ERR_PTR(-ENOENT);
  11980. return intel_framebuffer_create(dev, mode_cmd, obj);
  11981. }
  11982. #ifndef CONFIG_DRM_I915_FBDEV
  11983. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  11984. {
  11985. }
  11986. #endif
  11987. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11988. .fb_create = intel_user_framebuffer_create,
  11989. .output_poll_changed = intel_fbdev_output_poll_changed,
  11990. .atomic_check = intel_atomic_check,
  11991. .atomic_commit = intel_atomic_commit,
  11992. .atomic_state_alloc = intel_atomic_state_alloc,
  11993. .atomic_state_clear = intel_atomic_state_clear,
  11994. };
  11995. /* Set up chip specific display functions */
  11996. static void intel_init_display(struct drm_device *dev)
  11997. {
  11998. struct drm_i915_private *dev_priv = dev->dev_private;
  11999. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12000. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12001. else if (IS_CHERRYVIEW(dev))
  12002. dev_priv->display.find_dpll = chv_find_best_dpll;
  12003. else if (IS_VALLEYVIEW(dev))
  12004. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12005. else if (IS_PINEVIEW(dev))
  12006. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12007. else
  12008. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12009. if (INTEL_INFO(dev)->gen >= 9) {
  12010. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12011. dev_priv->display.get_initial_plane_config =
  12012. skylake_get_initial_plane_config;
  12013. dev_priv->display.crtc_compute_clock =
  12014. haswell_crtc_compute_clock;
  12015. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12016. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12017. dev_priv->display.update_primary_plane =
  12018. skylake_update_primary_plane;
  12019. } else if (HAS_DDI(dev)) {
  12020. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12021. dev_priv->display.get_initial_plane_config =
  12022. ironlake_get_initial_plane_config;
  12023. dev_priv->display.crtc_compute_clock =
  12024. haswell_crtc_compute_clock;
  12025. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12026. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12027. dev_priv->display.update_primary_plane =
  12028. ironlake_update_primary_plane;
  12029. } else if (HAS_PCH_SPLIT(dev)) {
  12030. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12031. dev_priv->display.get_initial_plane_config =
  12032. ironlake_get_initial_plane_config;
  12033. dev_priv->display.crtc_compute_clock =
  12034. ironlake_crtc_compute_clock;
  12035. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12036. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12037. dev_priv->display.update_primary_plane =
  12038. ironlake_update_primary_plane;
  12039. } else if (IS_VALLEYVIEW(dev)) {
  12040. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12041. dev_priv->display.get_initial_plane_config =
  12042. i9xx_get_initial_plane_config;
  12043. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12044. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12045. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12046. dev_priv->display.update_primary_plane =
  12047. i9xx_update_primary_plane;
  12048. } else {
  12049. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12050. dev_priv->display.get_initial_plane_config =
  12051. i9xx_get_initial_plane_config;
  12052. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12053. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12054. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12055. dev_priv->display.update_primary_plane =
  12056. i9xx_update_primary_plane;
  12057. }
  12058. /* Returns the core display clock speed */
  12059. if (IS_SKYLAKE(dev))
  12060. dev_priv->display.get_display_clock_speed =
  12061. skylake_get_display_clock_speed;
  12062. else if (IS_BROXTON(dev))
  12063. dev_priv->display.get_display_clock_speed =
  12064. broxton_get_display_clock_speed;
  12065. else if (IS_BROADWELL(dev))
  12066. dev_priv->display.get_display_clock_speed =
  12067. broadwell_get_display_clock_speed;
  12068. else if (IS_HASWELL(dev))
  12069. dev_priv->display.get_display_clock_speed =
  12070. haswell_get_display_clock_speed;
  12071. else if (IS_VALLEYVIEW(dev))
  12072. dev_priv->display.get_display_clock_speed =
  12073. valleyview_get_display_clock_speed;
  12074. else if (IS_GEN5(dev))
  12075. dev_priv->display.get_display_clock_speed =
  12076. ilk_get_display_clock_speed;
  12077. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12078. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12079. dev_priv->display.get_display_clock_speed =
  12080. i945_get_display_clock_speed;
  12081. else if (IS_GM45(dev))
  12082. dev_priv->display.get_display_clock_speed =
  12083. gm45_get_display_clock_speed;
  12084. else if (IS_CRESTLINE(dev))
  12085. dev_priv->display.get_display_clock_speed =
  12086. i965gm_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_G33(dev) || IS_G4X(dev))
  12091. dev_priv->display.get_display_clock_speed =
  12092. g33_get_display_clock_speed;
  12093. else if (IS_I915G(dev))
  12094. dev_priv->display.get_display_clock_speed =
  12095. i915_get_display_clock_speed;
  12096. else if (IS_I945GM(dev) || IS_845G(dev))
  12097. dev_priv->display.get_display_clock_speed =
  12098. i9xx_misc_get_display_clock_speed;
  12099. else if (IS_PINEVIEW(dev))
  12100. dev_priv->display.get_display_clock_speed =
  12101. pnv_get_display_clock_speed;
  12102. else if (IS_I915GM(dev))
  12103. dev_priv->display.get_display_clock_speed =
  12104. i915gm_get_display_clock_speed;
  12105. else if (IS_I865G(dev))
  12106. dev_priv->display.get_display_clock_speed =
  12107. i865_get_display_clock_speed;
  12108. else if (IS_I85X(dev))
  12109. dev_priv->display.get_display_clock_speed =
  12110. i85x_get_display_clock_speed;
  12111. else { /* 830 */
  12112. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12113. dev_priv->display.get_display_clock_speed =
  12114. i830_get_display_clock_speed;
  12115. }
  12116. if (IS_GEN5(dev)) {
  12117. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12118. } else if (IS_GEN6(dev)) {
  12119. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12120. } else if (IS_IVYBRIDGE(dev)) {
  12121. /* FIXME: detect B0+ stepping and use auto training */
  12122. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12123. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12124. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12125. if (IS_BROADWELL(dev)) {
  12126. dev_priv->display.modeset_commit_cdclk =
  12127. broadwell_modeset_commit_cdclk;
  12128. dev_priv->display.modeset_calc_cdclk =
  12129. broadwell_modeset_calc_cdclk;
  12130. }
  12131. } else if (IS_VALLEYVIEW(dev)) {
  12132. dev_priv->display.modeset_commit_cdclk =
  12133. valleyview_modeset_commit_cdclk;
  12134. dev_priv->display.modeset_calc_cdclk =
  12135. valleyview_modeset_calc_cdclk;
  12136. } else if (IS_BROXTON(dev)) {
  12137. dev_priv->display.modeset_commit_cdclk =
  12138. broxton_modeset_commit_cdclk;
  12139. dev_priv->display.modeset_calc_cdclk =
  12140. broxton_modeset_calc_cdclk;
  12141. }
  12142. switch (INTEL_INFO(dev)->gen) {
  12143. case 2:
  12144. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12145. break;
  12146. case 3:
  12147. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12148. break;
  12149. case 4:
  12150. case 5:
  12151. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12152. break;
  12153. case 6:
  12154. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12155. break;
  12156. case 7:
  12157. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12158. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12159. break;
  12160. case 9:
  12161. /* Drop through - unsupported since execlist only. */
  12162. default:
  12163. /* Default just returns -ENODEV to indicate unsupported */
  12164. dev_priv->display.queue_flip = intel_default_queue_flip;
  12165. }
  12166. intel_panel_init_backlight_funcs(dev);
  12167. mutex_init(&dev_priv->pps_mutex);
  12168. }
  12169. /*
  12170. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12171. * resume, or other times. This quirk makes sure that's the case for
  12172. * affected systems.
  12173. */
  12174. static void quirk_pipea_force(struct drm_device *dev)
  12175. {
  12176. struct drm_i915_private *dev_priv = dev->dev_private;
  12177. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12178. DRM_INFO("applying pipe a force quirk\n");
  12179. }
  12180. static void quirk_pipeb_force(struct drm_device *dev)
  12181. {
  12182. struct drm_i915_private *dev_priv = dev->dev_private;
  12183. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12184. DRM_INFO("applying pipe b force quirk\n");
  12185. }
  12186. /*
  12187. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12188. */
  12189. static void quirk_ssc_force_disable(struct drm_device *dev)
  12190. {
  12191. struct drm_i915_private *dev_priv = dev->dev_private;
  12192. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12193. DRM_INFO("applying lvds SSC disable quirk\n");
  12194. }
  12195. /*
  12196. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12197. * brightness value
  12198. */
  12199. static void quirk_invert_brightness(struct drm_device *dev)
  12200. {
  12201. struct drm_i915_private *dev_priv = dev->dev_private;
  12202. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12203. DRM_INFO("applying inverted panel brightness quirk\n");
  12204. }
  12205. /* Some VBT's incorrectly indicate no backlight is present */
  12206. static void quirk_backlight_present(struct drm_device *dev)
  12207. {
  12208. struct drm_i915_private *dev_priv = dev->dev_private;
  12209. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12210. DRM_INFO("applying backlight present quirk\n");
  12211. }
  12212. struct intel_quirk {
  12213. int device;
  12214. int subsystem_vendor;
  12215. int subsystem_device;
  12216. void (*hook)(struct drm_device *dev);
  12217. };
  12218. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12219. struct intel_dmi_quirk {
  12220. void (*hook)(struct drm_device *dev);
  12221. const struct dmi_system_id (*dmi_id_list)[];
  12222. };
  12223. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12224. {
  12225. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12226. return 1;
  12227. }
  12228. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12229. {
  12230. .dmi_id_list = &(const struct dmi_system_id[]) {
  12231. {
  12232. .callback = intel_dmi_reverse_brightness,
  12233. .ident = "NCR Corporation",
  12234. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12235. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12236. },
  12237. },
  12238. { } /* terminating entry */
  12239. },
  12240. .hook = quirk_invert_brightness,
  12241. },
  12242. };
  12243. static struct intel_quirk intel_quirks[] = {
  12244. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12245. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12246. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12247. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12248. /* 830 needs to leave pipe A & dpll A up */
  12249. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12250. /* 830 needs to leave pipe B & dpll B up */
  12251. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12252. /* Lenovo U160 cannot use SSC on LVDS */
  12253. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12254. /* Sony Vaio Y cannot use SSC on LVDS */
  12255. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12256. /* Acer Aspire 5734Z must invert backlight brightness */
  12257. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12258. /* Acer/eMachines G725 */
  12259. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12260. /* Acer/eMachines e725 */
  12261. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12262. /* Acer/Packard Bell NCL20 */
  12263. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12264. /* Acer Aspire 4736Z */
  12265. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12266. /* Acer Aspire 5336 */
  12267. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12268. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12269. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12270. /* Acer C720 Chromebook (Core i3 4005U) */
  12271. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12272. /* Apple Macbook 2,1 (Core 2 T7400) */
  12273. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12274. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12275. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12276. /* HP Chromebook 14 (Celeron 2955U) */
  12277. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12278. /* Dell Chromebook 11 */
  12279. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12280. };
  12281. static void intel_init_quirks(struct drm_device *dev)
  12282. {
  12283. struct pci_dev *d = dev->pdev;
  12284. int i;
  12285. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12286. struct intel_quirk *q = &intel_quirks[i];
  12287. if (d->device == q->device &&
  12288. (d->subsystem_vendor == q->subsystem_vendor ||
  12289. q->subsystem_vendor == PCI_ANY_ID) &&
  12290. (d->subsystem_device == q->subsystem_device ||
  12291. q->subsystem_device == PCI_ANY_ID))
  12292. q->hook(dev);
  12293. }
  12294. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12295. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12296. intel_dmi_quirks[i].hook(dev);
  12297. }
  12298. }
  12299. /* Disable the VGA plane that we never use */
  12300. static void i915_disable_vga(struct drm_device *dev)
  12301. {
  12302. struct drm_i915_private *dev_priv = dev->dev_private;
  12303. u8 sr1;
  12304. u32 vga_reg = i915_vgacntrl_reg(dev);
  12305. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12306. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12307. outb(SR01, VGA_SR_INDEX);
  12308. sr1 = inb(VGA_SR_DATA);
  12309. outb(sr1 | 1<<5, VGA_SR_DATA);
  12310. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12311. udelay(300);
  12312. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12313. POSTING_READ(vga_reg);
  12314. }
  12315. void intel_modeset_init_hw(struct drm_device *dev)
  12316. {
  12317. intel_update_cdclk(dev);
  12318. intel_prepare_ddi(dev);
  12319. intel_init_clock_gating(dev);
  12320. intel_enable_gt_powersave(dev);
  12321. }
  12322. void intel_modeset_init(struct drm_device *dev)
  12323. {
  12324. struct drm_i915_private *dev_priv = dev->dev_private;
  12325. int sprite, ret;
  12326. enum pipe pipe;
  12327. struct intel_crtc *crtc;
  12328. drm_mode_config_init(dev);
  12329. dev->mode_config.min_width = 0;
  12330. dev->mode_config.min_height = 0;
  12331. dev->mode_config.preferred_depth = 24;
  12332. dev->mode_config.prefer_shadow = 1;
  12333. dev->mode_config.allow_fb_modifiers = true;
  12334. dev->mode_config.funcs = &intel_mode_funcs;
  12335. intel_init_quirks(dev);
  12336. intel_init_pm(dev);
  12337. if (INTEL_INFO(dev)->num_pipes == 0)
  12338. return;
  12339. intel_init_display(dev);
  12340. intel_init_audio(dev);
  12341. if (IS_GEN2(dev)) {
  12342. dev->mode_config.max_width = 2048;
  12343. dev->mode_config.max_height = 2048;
  12344. } else if (IS_GEN3(dev)) {
  12345. dev->mode_config.max_width = 4096;
  12346. dev->mode_config.max_height = 4096;
  12347. } else {
  12348. dev->mode_config.max_width = 8192;
  12349. dev->mode_config.max_height = 8192;
  12350. }
  12351. if (IS_845G(dev) || IS_I865G(dev)) {
  12352. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12353. dev->mode_config.cursor_height = 1023;
  12354. } else if (IS_GEN2(dev)) {
  12355. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12356. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12357. } else {
  12358. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12359. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12360. }
  12361. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12362. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12363. INTEL_INFO(dev)->num_pipes,
  12364. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12365. for_each_pipe(dev_priv, pipe) {
  12366. intel_crtc_init(dev, pipe);
  12367. for_each_sprite(dev_priv, pipe, sprite) {
  12368. ret = intel_plane_init(dev, pipe, sprite);
  12369. if (ret)
  12370. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12371. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12372. }
  12373. }
  12374. intel_init_dpio(dev);
  12375. intel_shared_dpll_init(dev);
  12376. /* Just disable it once at startup */
  12377. i915_disable_vga(dev);
  12378. intel_setup_outputs(dev);
  12379. /* Just in case the BIOS is doing something questionable. */
  12380. intel_fbc_disable(dev_priv);
  12381. drm_modeset_lock_all(dev);
  12382. intel_modeset_setup_hw_state(dev);
  12383. drm_modeset_unlock_all(dev);
  12384. for_each_intel_crtc(dev, crtc) {
  12385. struct intel_initial_plane_config plane_config = {};
  12386. if (!crtc->active)
  12387. continue;
  12388. /*
  12389. * Note that reserving the BIOS fb up front prevents us
  12390. * from stuffing other stolen allocations like the ring
  12391. * on top. This prevents some ugliness at boot time, and
  12392. * can even allow for smooth boot transitions if the BIOS
  12393. * fb is large enough for the active pipe configuration.
  12394. */
  12395. dev_priv->display.get_initial_plane_config(crtc,
  12396. &plane_config);
  12397. /*
  12398. * If the fb is shared between multiple heads, we'll
  12399. * just get the first one.
  12400. */
  12401. intel_find_initial_plane_obj(crtc, &plane_config);
  12402. }
  12403. }
  12404. static void intel_enable_pipe_a(struct drm_device *dev)
  12405. {
  12406. struct intel_connector *connector;
  12407. struct drm_connector *crt = NULL;
  12408. struct intel_load_detect_pipe load_detect_temp;
  12409. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12410. /* We can't just switch on the pipe A, we need to set things up with a
  12411. * proper mode and output configuration. As a gross hack, enable pipe A
  12412. * by enabling the load detect pipe once. */
  12413. for_each_intel_connector(dev, connector) {
  12414. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12415. crt = &connector->base;
  12416. break;
  12417. }
  12418. }
  12419. if (!crt)
  12420. return;
  12421. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12422. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12423. }
  12424. static bool
  12425. intel_check_plane_mapping(struct intel_crtc *crtc)
  12426. {
  12427. struct drm_device *dev = crtc->base.dev;
  12428. struct drm_i915_private *dev_priv = dev->dev_private;
  12429. u32 reg, val;
  12430. if (INTEL_INFO(dev)->num_pipes == 1)
  12431. return true;
  12432. reg = DSPCNTR(!crtc->plane);
  12433. val = I915_READ(reg);
  12434. if ((val & DISPLAY_PLANE_ENABLE) &&
  12435. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12436. return false;
  12437. return true;
  12438. }
  12439. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12440. {
  12441. struct drm_device *dev = crtc->base.dev;
  12442. struct drm_i915_private *dev_priv = dev->dev_private;
  12443. struct intel_encoder *encoder;
  12444. u32 reg;
  12445. bool enable;
  12446. /* Clear any frame start delays used for debugging left by the BIOS */
  12447. reg = PIPECONF(crtc->config->cpu_transcoder);
  12448. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12449. /* restore vblank interrupts to correct state */
  12450. drm_crtc_vblank_reset(&crtc->base);
  12451. if (crtc->active) {
  12452. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12453. update_scanline_offset(crtc);
  12454. drm_crtc_vblank_on(&crtc->base);
  12455. }
  12456. /* We need to sanitize the plane -> pipe mapping first because this will
  12457. * disable the crtc (and hence change the state) if it is wrong. Note
  12458. * that gen4+ has a fixed plane -> pipe mapping. */
  12459. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12460. bool plane;
  12461. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12462. crtc->base.base.id);
  12463. /* Pipe has the wrong plane attached and the plane is active.
  12464. * Temporarily change the plane mapping and disable everything
  12465. * ... */
  12466. plane = crtc->plane;
  12467. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12468. crtc->plane = !plane;
  12469. intel_crtc_disable_noatomic(&crtc->base);
  12470. crtc->plane = plane;
  12471. }
  12472. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12473. crtc->pipe == PIPE_A && !crtc->active) {
  12474. /* BIOS forgot to enable pipe A, this mostly happens after
  12475. * resume. Force-enable the pipe to fix this, the update_dpms
  12476. * call below we restore the pipe to the right state, but leave
  12477. * the required bits on. */
  12478. intel_enable_pipe_a(dev);
  12479. }
  12480. /* Adjust the state of the output pipe according to whether we
  12481. * have active connectors/encoders. */
  12482. enable = false;
  12483. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12484. enable = true;
  12485. break;
  12486. }
  12487. if (!enable)
  12488. intel_crtc_disable_noatomic(&crtc->base);
  12489. if (crtc->active != crtc->base.state->active) {
  12490. /* This can happen either due to bugs in the get_hw_state
  12491. * functions or because of calls to intel_crtc_disable_noatomic,
  12492. * or because the pipe is force-enabled due to the
  12493. * pipe A quirk. */
  12494. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12495. crtc->base.base.id,
  12496. crtc->base.state->enable ? "enabled" : "disabled",
  12497. crtc->active ? "enabled" : "disabled");
  12498. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12499. crtc->base.state->active = crtc->active;
  12500. crtc->base.enabled = crtc->active;
  12501. /* Because we only establish the connector -> encoder ->
  12502. * crtc links if something is active, this means the
  12503. * crtc is now deactivated. Break the links. connector
  12504. * -> encoder links are only establish when things are
  12505. * actually up, hence no need to break them. */
  12506. WARN_ON(crtc->active);
  12507. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12508. encoder->base.crtc = NULL;
  12509. }
  12510. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12511. /*
  12512. * We start out with underrun reporting disabled to avoid races.
  12513. * For correct bookkeeping mark this on active crtcs.
  12514. *
  12515. * Also on gmch platforms we dont have any hardware bits to
  12516. * disable the underrun reporting. Which means we need to start
  12517. * out with underrun reporting disabled also on inactive pipes,
  12518. * since otherwise we'll complain about the garbage we read when
  12519. * e.g. coming up after runtime pm.
  12520. *
  12521. * No protection against concurrent access is required - at
  12522. * worst a fifo underrun happens which also sets this to false.
  12523. */
  12524. crtc->cpu_fifo_underrun_disabled = true;
  12525. crtc->pch_fifo_underrun_disabled = true;
  12526. }
  12527. }
  12528. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12529. {
  12530. struct intel_connector *connector;
  12531. struct drm_device *dev = encoder->base.dev;
  12532. bool active = false;
  12533. /* We need to check both for a crtc link (meaning that the
  12534. * encoder is active and trying to read from a pipe) and the
  12535. * pipe itself being active. */
  12536. bool has_active_crtc = encoder->base.crtc &&
  12537. to_intel_crtc(encoder->base.crtc)->active;
  12538. for_each_intel_connector(dev, connector) {
  12539. if (connector->base.encoder != &encoder->base)
  12540. continue;
  12541. active = true;
  12542. break;
  12543. }
  12544. if (active && !has_active_crtc) {
  12545. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12546. encoder->base.base.id,
  12547. encoder->base.name);
  12548. /* Connector is active, but has no active pipe. This is
  12549. * fallout from our resume register restoring. Disable
  12550. * the encoder manually again. */
  12551. if (encoder->base.crtc) {
  12552. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12553. encoder->base.base.id,
  12554. encoder->base.name);
  12555. encoder->disable(encoder);
  12556. if (encoder->post_disable)
  12557. encoder->post_disable(encoder);
  12558. }
  12559. encoder->base.crtc = NULL;
  12560. /* Inconsistent output/port/pipe state happens presumably due to
  12561. * a bug in one of the get_hw_state functions. Or someplace else
  12562. * in our code, like the register restore mess on resume. Clamp
  12563. * things to off as a safer default. */
  12564. for_each_intel_connector(dev, connector) {
  12565. if (connector->encoder != encoder)
  12566. continue;
  12567. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12568. connector->base.encoder = NULL;
  12569. }
  12570. }
  12571. /* Enabled encoders without active connectors will be fixed in
  12572. * the crtc fixup. */
  12573. }
  12574. void i915_redisable_vga_power_on(struct drm_device *dev)
  12575. {
  12576. struct drm_i915_private *dev_priv = dev->dev_private;
  12577. u32 vga_reg = i915_vgacntrl_reg(dev);
  12578. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12579. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12580. i915_disable_vga(dev);
  12581. }
  12582. }
  12583. void i915_redisable_vga(struct drm_device *dev)
  12584. {
  12585. struct drm_i915_private *dev_priv = dev->dev_private;
  12586. /* This function can be called both from intel_modeset_setup_hw_state or
  12587. * at a very early point in our resume sequence, where the power well
  12588. * structures are not yet restored. Since this function is at a very
  12589. * paranoid "someone might have enabled VGA while we were not looking"
  12590. * level, just check if the power well is enabled instead of trying to
  12591. * follow the "don't touch the power well if we don't need it" policy
  12592. * the rest of the driver uses. */
  12593. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12594. return;
  12595. i915_redisable_vga_power_on(dev);
  12596. }
  12597. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12598. {
  12599. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12600. return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
  12601. }
  12602. static void readout_plane_state(struct intel_crtc *crtc,
  12603. struct intel_crtc_state *crtc_state)
  12604. {
  12605. struct intel_plane *p;
  12606. struct intel_plane_state *plane_state;
  12607. bool active = crtc_state->base.active;
  12608. for_each_intel_plane(crtc->base.dev, p) {
  12609. if (crtc->pipe != p->pipe)
  12610. continue;
  12611. plane_state = to_intel_plane_state(p->base.state);
  12612. if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
  12613. plane_state->visible = primary_get_hw_state(crtc);
  12614. else {
  12615. if (active)
  12616. p->disable_plane(&p->base, &crtc->base);
  12617. plane_state->visible = false;
  12618. }
  12619. }
  12620. }
  12621. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12622. {
  12623. struct drm_i915_private *dev_priv = dev->dev_private;
  12624. enum pipe pipe;
  12625. struct intel_crtc *crtc;
  12626. struct intel_encoder *encoder;
  12627. struct intel_connector *connector;
  12628. int i;
  12629. for_each_intel_crtc(dev, crtc) {
  12630. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12631. memset(crtc->config, 0, sizeof(*crtc->config));
  12632. crtc->config->base.crtc = &crtc->base;
  12633. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12634. crtc->config);
  12635. crtc->base.state->active = crtc->active;
  12636. crtc->base.enabled = crtc->active;
  12637. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12638. if (crtc->base.state->active) {
  12639. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12640. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12641. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12642. /*
  12643. * The initial mode needs to be set in order to keep
  12644. * the atomic core happy. It wants a valid mode if the
  12645. * crtc's enabled, so we do the above call.
  12646. *
  12647. * At this point some state updated by the connectors
  12648. * in their ->detect() callback has not run yet, so
  12649. * no recalculation can be done yet.
  12650. *
  12651. * Even if we could do a recalculation and modeset
  12652. * right now it would cause a double modeset if
  12653. * fbdev or userspace chooses a different initial mode.
  12654. *
  12655. * If that happens, someone indicated they wanted a
  12656. * mode change, which means it's safe to do a full
  12657. * recalculation.
  12658. */
  12659. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12660. }
  12661. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12662. readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
  12663. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12664. crtc->base.base.id,
  12665. crtc->active ? "enabled" : "disabled");
  12666. }
  12667. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12668. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12669. pll->on = pll->get_hw_state(dev_priv, pll,
  12670. &pll->config.hw_state);
  12671. pll->active = 0;
  12672. pll->config.crtc_mask = 0;
  12673. for_each_intel_crtc(dev, crtc) {
  12674. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12675. pll->active++;
  12676. pll->config.crtc_mask |= 1 << crtc->pipe;
  12677. }
  12678. }
  12679. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12680. pll->name, pll->config.crtc_mask, pll->on);
  12681. if (pll->config.crtc_mask)
  12682. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12683. }
  12684. for_each_intel_encoder(dev, encoder) {
  12685. pipe = 0;
  12686. if (encoder->get_hw_state(encoder, &pipe)) {
  12687. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12688. encoder->base.crtc = &crtc->base;
  12689. encoder->get_config(encoder, crtc->config);
  12690. } else {
  12691. encoder->base.crtc = NULL;
  12692. }
  12693. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12694. encoder->base.base.id,
  12695. encoder->base.name,
  12696. encoder->base.crtc ? "enabled" : "disabled",
  12697. pipe_name(pipe));
  12698. }
  12699. for_each_intel_connector(dev, connector) {
  12700. if (connector->get_hw_state(connector)) {
  12701. connector->base.dpms = DRM_MODE_DPMS_ON;
  12702. connector->base.encoder = &connector->encoder->base;
  12703. } else {
  12704. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12705. connector->base.encoder = NULL;
  12706. }
  12707. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12708. connector->base.base.id,
  12709. connector->base.name,
  12710. connector->base.encoder ? "enabled" : "disabled");
  12711. }
  12712. }
  12713. /* Scan out the current hw modeset state,
  12714. * and sanitizes it to the current state
  12715. */
  12716. static void
  12717. intel_modeset_setup_hw_state(struct drm_device *dev)
  12718. {
  12719. struct drm_i915_private *dev_priv = dev->dev_private;
  12720. enum pipe pipe;
  12721. struct intel_crtc *crtc;
  12722. struct intel_encoder *encoder;
  12723. int i;
  12724. intel_modeset_readout_hw_state(dev);
  12725. /* HW state is read out, now we need to sanitize this mess. */
  12726. for_each_intel_encoder(dev, encoder) {
  12727. intel_sanitize_encoder(encoder);
  12728. }
  12729. for_each_pipe(dev_priv, pipe) {
  12730. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12731. intel_sanitize_crtc(crtc);
  12732. intel_dump_pipe_config(crtc, crtc->config,
  12733. "[setup_hw_state]");
  12734. }
  12735. intel_modeset_update_connector_atomic_state(dev);
  12736. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12737. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12738. if (!pll->on || pll->active)
  12739. continue;
  12740. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12741. pll->disable(dev_priv, pll);
  12742. pll->on = false;
  12743. }
  12744. if (IS_VALLEYVIEW(dev))
  12745. vlv_wm_get_hw_state(dev);
  12746. else if (IS_GEN9(dev))
  12747. skl_wm_get_hw_state(dev);
  12748. else if (HAS_PCH_SPLIT(dev))
  12749. ilk_wm_get_hw_state(dev);
  12750. for_each_intel_crtc(dev, crtc) {
  12751. unsigned long put_domains;
  12752. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  12753. if (WARN_ON(put_domains))
  12754. modeset_put_power_domains(dev_priv, put_domains);
  12755. }
  12756. intel_display_set_init_power(dev_priv, false);
  12757. }
  12758. void intel_display_resume(struct drm_device *dev)
  12759. {
  12760. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  12761. struct intel_connector *conn;
  12762. struct intel_plane *plane;
  12763. struct drm_crtc *crtc;
  12764. int ret;
  12765. if (!state)
  12766. return;
  12767. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12768. /* preserve complete old state, including dpll */
  12769. intel_atomic_get_shared_dpll_state(state);
  12770. for_each_crtc(dev, crtc) {
  12771. struct drm_crtc_state *crtc_state =
  12772. drm_atomic_get_crtc_state(state, crtc);
  12773. ret = PTR_ERR_OR_ZERO(crtc_state);
  12774. if (ret)
  12775. goto err;
  12776. /* force a restore */
  12777. crtc_state->mode_changed = true;
  12778. }
  12779. for_each_intel_plane(dev, plane) {
  12780. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  12781. if (ret)
  12782. goto err;
  12783. }
  12784. for_each_intel_connector(dev, conn) {
  12785. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  12786. if (ret)
  12787. goto err;
  12788. }
  12789. intel_modeset_setup_hw_state(dev);
  12790. i915_redisable_vga(dev);
  12791. ret = drm_atomic_commit(state);
  12792. if (!ret)
  12793. return;
  12794. err:
  12795. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12796. drm_atomic_state_free(state);
  12797. }
  12798. void intel_modeset_gem_init(struct drm_device *dev)
  12799. {
  12800. struct drm_i915_private *dev_priv = dev->dev_private;
  12801. struct drm_crtc *c;
  12802. struct drm_i915_gem_object *obj;
  12803. int ret;
  12804. mutex_lock(&dev->struct_mutex);
  12805. intel_init_gt_powersave(dev);
  12806. mutex_unlock(&dev->struct_mutex);
  12807. /*
  12808. * There may be no VBT; and if the BIOS enabled SSC we can
  12809. * just keep using it to avoid unnecessary flicker. Whereas if the
  12810. * BIOS isn't using it, don't assume it will work even if the VBT
  12811. * indicates as much.
  12812. */
  12813. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  12814. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12815. DREF_SSC1_ENABLE);
  12816. intel_modeset_init_hw(dev);
  12817. intel_setup_overlay(dev);
  12818. /*
  12819. * Make sure any fbs we allocated at startup are properly
  12820. * pinned & fenced. When we do the allocation it's too early
  12821. * for this.
  12822. */
  12823. for_each_crtc(dev, c) {
  12824. obj = intel_fb_obj(c->primary->fb);
  12825. if (obj == NULL)
  12826. continue;
  12827. mutex_lock(&dev->struct_mutex);
  12828. ret = intel_pin_and_fence_fb_obj(c->primary,
  12829. c->primary->fb,
  12830. c->primary->state,
  12831. NULL, NULL);
  12832. mutex_unlock(&dev->struct_mutex);
  12833. if (ret) {
  12834. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12835. to_intel_crtc(c)->pipe);
  12836. drm_framebuffer_unreference(c->primary->fb);
  12837. c->primary->fb = NULL;
  12838. c->primary->crtc = c->primary->state->crtc = NULL;
  12839. update_state_fb(c->primary);
  12840. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12841. }
  12842. }
  12843. intel_backlight_register(dev);
  12844. }
  12845. void intel_connector_unregister(struct intel_connector *intel_connector)
  12846. {
  12847. struct drm_connector *connector = &intel_connector->base;
  12848. intel_panel_destroy_backlight(connector);
  12849. drm_connector_unregister(connector);
  12850. }
  12851. void intel_modeset_cleanup(struct drm_device *dev)
  12852. {
  12853. struct drm_i915_private *dev_priv = dev->dev_private;
  12854. struct drm_connector *connector;
  12855. intel_disable_gt_powersave(dev);
  12856. intel_backlight_unregister(dev);
  12857. /*
  12858. * Interrupts and polling as the first thing to avoid creating havoc.
  12859. * Too much stuff here (turning of connectors, ...) would
  12860. * experience fancy races otherwise.
  12861. */
  12862. intel_irq_uninstall(dev_priv);
  12863. /*
  12864. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12865. * poll handlers. Hence disable polling after hpd handling is shut down.
  12866. */
  12867. drm_kms_helper_poll_fini(dev);
  12868. intel_unregister_dsm_handler();
  12869. intel_fbc_disable(dev_priv);
  12870. /* flush any delayed tasks or pending work */
  12871. flush_scheduled_work();
  12872. /* destroy the backlight and sysfs files before encoders/connectors */
  12873. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12874. struct intel_connector *intel_connector;
  12875. intel_connector = to_intel_connector(connector);
  12876. intel_connector->unregister(intel_connector);
  12877. }
  12878. drm_mode_config_cleanup(dev);
  12879. intel_cleanup_overlay(dev);
  12880. mutex_lock(&dev->struct_mutex);
  12881. intel_cleanup_gt_powersave(dev);
  12882. mutex_unlock(&dev->struct_mutex);
  12883. }
  12884. /*
  12885. * Return which encoder is currently attached for connector.
  12886. */
  12887. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12888. {
  12889. return &intel_attached_encoder(connector)->base;
  12890. }
  12891. void intel_connector_attach_encoder(struct intel_connector *connector,
  12892. struct intel_encoder *encoder)
  12893. {
  12894. connector->encoder = encoder;
  12895. drm_mode_connector_attach_encoder(&connector->base,
  12896. &encoder->base);
  12897. }
  12898. /*
  12899. * set vga decode state - true == enable VGA decode
  12900. */
  12901. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12902. {
  12903. struct drm_i915_private *dev_priv = dev->dev_private;
  12904. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12905. u16 gmch_ctrl;
  12906. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12907. DRM_ERROR("failed to read control word\n");
  12908. return -EIO;
  12909. }
  12910. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12911. return 0;
  12912. if (state)
  12913. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12914. else
  12915. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12916. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12917. DRM_ERROR("failed to write control word\n");
  12918. return -EIO;
  12919. }
  12920. return 0;
  12921. }
  12922. struct intel_display_error_state {
  12923. u32 power_well_driver;
  12924. int num_transcoders;
  12925. struct intel_cursor_error_state {
  12926. u32 control;
  12927. u32 position;
  12928. u32 base;
  12929. u32 size;
  12930. } cursor[I915_MAX_PIPES];
  12931. struct intel_pipe_error_state {
  12932. bool power_domain_on;
  12933. u32 source;
  12934. u32 stat;
  12935. } pipe[I915_MAX_PIPES];
  12936. struct intel_plane_error_state {
  12937. u32 control;
  12938. u32 stride;
  12939. u32 size;
  12940. u32 pos;
  12941. u32 addr;
  12942. u32 surface;
  12943. u32 tile_offset;
  12944. } plane[I915_MAX_PIPES];
  12945. struct intel_transcoder_error_state {
  12946. bool power_domain_on;
  12947. enum transcoder cpu_transcoder;
  12948. u32 conf;
  12949. u32 htotal;
  12950. u32 hblank;
  12951. u32 hsync;
  12952. u32 vtotal;
  12953. u32 vblank;
  12954. u32 vsync;
  12955. } transcoder[4];
  12956. };
  12957. struct intel_display_error_state *
  12958. intel_display_capture_error_state(struct drm_device *dev)
  12959. {
  12960. struct drm_i915_private *dev_priv = dev->dev_private;
  12961. struct intel_display_error_state *error;
  12962. int transcoders[] = {
  12963. TRANSCODER_A,
  12964. TRANSCODER_B,
  12965. TRANSCODER_C,
  12966. TRANSCODER_EDP,
  12967. };
  12968. int i;
  12969. if (INTEL_INFO(dev)->num_pipes == 0)
  12970. return NULL;
  12971. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12972. if (error == NULL)
  12973. return NULL;
  12974. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12975. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12976. for_each_pipe(dev_priv, i) {
  12977. error->pipe[i].power_domain_on =
  12978. __intel_display_power_is_enabled(dev_priv,
  12979. POWER_DOMAIN_PIPE(i));
  12980. if (!error->pipe[i].power_domain_on)
  12981. continue;
  12982. error->cursor[i].control = I915_READ(CURCNTR(i));
  12983. error->cursor[i].position = I915_READ(CURPOS(i));
  12984. error->cursor[i].base = I915_READ(CURBASE(i));
  12985. error->plane[i].control = I915_READ(DSPCNTR(i));
  12986. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12987. if (INTEL_INFO(dev)->gen <= 3) {
  12988. error->plane[i].size = I915_READ(DSPSIZE(i));
  12989. error->plane[i].pos = I915_READ(DSPPOS(i));
  12990. }
  12991. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12992. error->plane[i].addr = I915_READ(DSPADDR(i));
  12993. if (INTEL_INFO(dev)->gen >= 4) {
  12994. error->plane[i].surface = I915_READ(DSPSURF(i));
  12995. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12996. }
  12997. error->pipe[i].source = I915_READ(PIPESRC(i));
  12998. if (HAS_GMCH_DISPLAY(dev))
  12999. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13000. }
  13001. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13002. if (HAS_DDI(dev_priv->dev))
  13003. error->num_transcoders++; /* Account for eDP. */
  13004. for (i = 0; i < error->num_transcoders; i++) {
  13005. enum transcoder cpu_transcoder = transcoders[i];
  13006. error->transcoder[i].power_domain_on =
  13007. __intel_display_power_is_enabled(dev_priv,
  13008. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13009. if (!error->transcoder[i].power_domain_on)
  13010. continue;
  13011. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13012. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13013. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13014. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13015. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13016. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13017. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13018. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13019. }
  13020. return error;
  13021. }
  13022. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13023. void
  13024. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13025. struct drm_device *dev,
  13026. struct intel_display_error_state *error)
  13027. {
  13028. struct drm_i915_private *dev_priv = dev->dev_private;
  13029. int i;
  13030. if (!error)
  13031. return;
  13032. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13033. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13034. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13035. error->power_well_driver);
  13036. for_each_pipe(dev_priv, i) {
  13037. err_printf(m, "Pipe [%d]:\n", i);
  13038. err_printf(m, " Power: %s\n",
  13039. error->pipe[i].power_domain_on ? "on" : "off");
  13040. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13041. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13042. err_printf(m, "Plane [%d]:\n", i);
  13043. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13044. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13045. if (INTEL_INFO(dev)->gen <= 3) {
  13046. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13047. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13048. }
  13049. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13050. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13051. if (INTEL_INFO(dev)->gen >= 4) {
  13052. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13053. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13054. }
  13055. err_printf(m, "Cursor [%d]:\n", i);
  13056. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13057. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13058. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13059. }
  13060. for (i = 0; i < error->num_transcoders; i++) {
  13061. err_printf(m, "CPU transcoder: %c\n",
  13062. transcoder_name(error->transcoder[i].cpu_transcoder));
  13063. err_printf(m, " Power: %s\n",
  13064. error->transcoder[i].power_domain_on ? "on" : "off");
  13065. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13066. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13067. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13068. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13069. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13070. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13071. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13072. }
  13073. }
  13074. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13075. {
  13076. struct intel_crtc *crtc;
  13077. for_each_intel_crtc(dev, crtc) {
  13078. struct intel_unpin_work *work;
  13079. spin_lock_irq(&dev->event_lock);
  13080. work = crtc->unpin_work;
  13081. if (work && work->event &&
  13082. work->event->base.file_priv == file) {
  13083. kfree(work->event);
  13084. work->event = NULL;
  13085. }
  13086. spin_unlock_irq(&dev->event_lock);
  13087. }
  13088. }