intel_display.c 428 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511
  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 supported by all gen */
  47. #define COMMON_PRIMARY_FORMATS \
  48. DRM_FORMAT_C8, \
  49. DRM_FORMAT_RGB565, \
  50. DRM_FORMAT_XRGB8888, \
  51. DRM_FORMAT_ARGB8888
  52. /* Primary plane formats for gen <= 3 */
  53. static const uint32_t intel_primary_formats_gen2[] = {
  54. COMMON_PRIMARY_FORMATS,
  55. DRM_FORMAT_XRGB1555,
  56. DRM_FORMAT_ARGB1555,
  57. };
  58. /* Primary plane formats for gen >= 4 */
  59. static const uint32_t intel_primary_formats_gen4[] = {
  60. COMMON_PRIMARY_FORMATS, \
  61. DRM_FORMAT_XBGR8888,
  62. DRM_FORMAT_ABGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_ARGB2101010,
  65. DRM_FORMAT_XBGR2101010,
  66. DRM_FORMAT_ABGR2101010,
  67. };
  68. /* Cursor formats */
  69. static const uint32_t intel_cursor_formats[] = {
  70. DRM_FORMAT_ARGB8888,
  71. };
  72. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  73. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_state *pipe_config);
  75. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  76. struct intel_crtc_state *pipe_config);
  77. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  78. int x, int y, struct drm_framebuffer *old_fb,
  79. struct drm_atomic_state *state);
  80. static int intel_framebuffer_init(struct drm_device *dev,
  81. struct intel_framebuffer *ifb,
  82. struct drm_mode_fb_cmd2 *mode_cmd,
  83. struct drm_i915_gem_object *obj);
  84. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  85. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  86. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  87. struct intel_link_m_n *m_n,
  88. struct intel_link_m_n *m2_n2);
  89. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  90. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  91. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  92. static void vlv_prepare_pll(struct intel_crtc *crtc,
  93. const struct intel_crtc_state *pipe_config);
  94. static void chv_prepare_pll(struct intel_crtc *crtc,
  95. const struct intel_crtc_state *pipe_config);
  96. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  97. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  98. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  99. struct intel_crtc_state *crtc_state);
  100. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  101. int num_connectors);
  102. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  103. {
  104. if (!connector->mst_port)
  105. return connector->encoder;
  106. else
  107. return &connector->mst_port->mst_encoders[pipe]->base;
  108. }
  109. typedef struct {
  110. int min, max;
  111. } intel_range_t;
  112. typedef struct {
  113. int dot_limit;
  114. int p2_slow, p2_fast;
  115. } intel_p2_t;
  116. typedef struct intel_limit intel_limit_t;
  117. struct intel_limit {
  118. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  119. intel_p2_t p2;
  120. };
  121. int
  122. intel_pch_rawclk(struct drm_device *dev)
  123. {
  124. struct drm_i915_private *dev_priv = dev->dev_private;
  125. WARN_ON(!HAS_PCH_SPLIT(dev));
  126. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  127. }
  128. static inline u32 /* units of 100MHz */
  129. intel_fdi_link_freq(struct drm_device *dev)
  130. {
  131. if (IS_GEN5(dev)) {
  132. struct drm_i915_private *dev_priv = dev->dev_private;
  133. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  134. } else
  135. return 27;
  136. }
  137. static const intel_limit_t intel_limits_i8xx_dac = {
  138. .dot = { .min = 25000, .max = 350000 },
  139. .vco = { .min = 908000, .max = 1512000 },
  140. .n = { .min = 2, .max = 16 },
  141. .m = { .min = 96, .max = 140 },
  142. .m1 = { .min = 18, .max = 26 },
  143. .m2 = { .min = 6, .max = 16 },
  144. .p = { .min = 4, .max = 128 },
  145. .p1 = { .min = 2, .max = 33 },
  146. .p2 = { .dot_limit = 165000,
  147. .p2_slow = 4, .p2_fast = 2 },
  148. };
  149. static const intel_limit_t intel_limits_i8xx_dvo = {
  150. .dot = { .min = 25000, .max = 350000 },
  151. .vco = { .min = 908000, .max = 1512000 },
  152. .n = { .min = 2, .max = 16 },
  153. .m = { .min = 96, .max = 140 },
  154. .m1 = { .min = 18, .max = 26 },
  155. .m2 = { .min = 6, .max = 16 },
  156. .p = { .min = 4, .max = 128 },
  157. .p1 = { .min = 2, .max = 33 },
  158. .p2 = { .dot_limit = 165000,
  159. .p2_slow = 4, .p2_fast = 4 },
  160. };
  161. static const intel_limit_t intel_limits_i8xx_lvds = {
  162. .dot = { .min = 25000, .max = 350000 },
  163. .vco = { .min = 908000, .max = 1512000 },
  164. .n = { .min = 2, .max = 16 },
  165. .m = { .min = 96, .max = 140 },
  166. .m1 = { .min = 18, .max = 26 },
  167. .m2 = { .min = 6, .max = 16 },
  168. .p = { .min = 4, .max = 128 },
  169. .p1 = { .min = 1, .max = 6 },
  170. .p2 = { .dot_limit = 165000,
  171. .p2_slow = 14, .p2_fast = 7 },
  172. };
  173. static const intel_limit_t intel_limits_i9xx_sdvo = {
  174. .dot = { .min = 20000, .max = 400000 },
  175. .vco = { .min = 1400000, .max = 2800000 },
  176. .n = { .min = 1, .max = 6 },
  177. .m = { .min = 70, .max = 120 },
  178. .m1 = { .min = 8, .max = 18 },
  179. .m2 = { .min = 3, .max = 7 },
  180. .p = { .min = 5, .max = 80 },
  181. .p1 = { .min = 1, .max = 8 },
  182. .p2 = { .dot_limit = 200000,
  183. .p2_slow = 10, .p2_fast = 5 },
  184. };
  185. static const intel_limit_t intel_limits_i9xx_lvds = {
  186. .dot = { .min = 20000, .max = 400000 },
  187. .vco = { .min = 1400000, .max = 2800000 },
  188. .n = { .min = 1, .max = 6 },
  189. .m = { .min = 70, .max = 120 },
  190. .m1 = { .min = 8, .max = 18 },
  191. .m2 = { .min = 3, .max = 7 },
  192. .p = { .min = 7, .max = 98 },
  193. .p1 = { .min = 1, .max = 8 },
  194. .p2 = { .dot_limit = 112000,
  195. .p2_slow = 14, .p2_fast = 7 },
  196. };
  197. static const intel_limit_t intel_limits_g4x_sdvo = {
  198. .dot = { .min = 25000, .max = 270000 },
  199. .vco = { .min = 1750000, .max = 3500000},
  200. .n = { .min = 1, .max = 4 },
  201. .m = { .min = 104, .max = 138 },
  202. .m1 = { .min = 17, .max = 23 },
  203. .m2 = { .min = 5, .max = 11 },
  204. .p = { .min = 10, .max = 30 },
  205. .p1 = { .min = 1, .max = 3},
  206. .p2 = { .dot_limit = 270000,
  207. .p2_slow = 10,
  208. .p2_fast = 10
  209. },
  210. };
  211. static const intel_limit_t intel_limits_g4x_hdmi = {
  212. .dot = { .min = 22000, .max = 400000 },
  213. .vco = { .min = 1750000, .max = 3500000},
  214. .n = { .min = 1, .max = 4 },
  215. .m = { .min = 104, .max = 138 },
  216. .m1 = { .min = 16, .max = 23 },
  217. .m2 = { .min = 5, .max = 11 },
  218. .p = { .min = 5, .max = 80 },
  219. .p1 = { .min = 1, .max = 8},
  220. .p2 = { .dot_limit = 165000,
  221. .p2_slow = 10, .p2_fast = 5 },
  222. };
  223. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  224. .dot = { .min = 20000, .max = 115000 },
  225. .vco = { .min = 1750000, .max = 3500000 },
  226. .n = { .min = 1, .max = 3 },
  227. .m = { .min = 104, .max = 138 },
  228. .m1 = { .min = 17, .max = 23 },
  229. .m2 = { .min = 5, .max = 11 },
  230. .p = { .min = 28, .max = 112 },
  231. .p1 = { .min = 2, .max = 8 },
  232. .p2 = { .dot_limit = 0,
  233. .p2_slow = 14, .p2_fast = 14
  234. },
  235. };
  236. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  237. .dot = { .min = 80000, .max = 224000 },
  238. .vco = { .min = 1750000, .max = 3500000 },
  239. .n = { .min = 1, .max = 3 },
  240. .m = { .min = 104, .max = 138 },
  241. .m1 = { .min = 17, .max = 23 },
  242. .m2 = { .min = 5, .max = 11 },
  243. .p = { .min = 14, .max = 42 },
  244. .p1 = { .min = 2, .max = 6 },
  245. .p2 = { .dot_limit = 0,
  246. .p2_slow = 7, .p2_fast = 7
  247. },
  248. };
  249. static const intel_limit_t intel_limits_pineview_sdvo = {
  250. .dot = { .min = 20000, .max = 400000},
  251. .vco = { .min = 1700000, .max = 3500000 },
  252. /* Pineview's Ncounter is a ring counter */
  253. .n = { .min = 3, .max = 6 },
  254. .m = { .min = 2, .max = 256 },
  255. /* Pineview only has one combined m divider, which we treat as m2. */
  256. .m1 = { .min = 0, .max = 0 },
  257. .m2 = { .min = 0, .max = 254 },
  258. .p = { .min = 5, .max = 80 },
  259. .p1 = { .min = 1, .max = 8 },
  260. .p2 = { .dot_limit = 200000,
  261. .p2_slow = 10, .p2_fast = 5 },
  262. };
  263. static const intel_limit_t intel_limits_pineview_lvds = {
  264. .dot = { .min = 20000, .max = 400000 },
  265. .vco = { .min = 1700000, .max = 3500000 },
  266. .n = { .min = 3, .max = 6 },
  267. .m = { .min = 2, .max = 256 },
  268. .m1 = { .min = 0, .max = 0 },
  269. .m2 = { .min = 0, .max = 254 },
  270. .p = { .min = 7, .max = 112 },
  271. .p1 = { .min = 1, .max = 8 },
  272. .p2 = { .dot_limit = 112000,
  273. .p2_slow = 14, .p2_fast = 14 },
  274. };
  275. /* Ironlake / Sandybridge
  276. *
  277. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  278. * the range value for them is (actual_value - 2).
  279. */
  280. static const intel_limit_t intel_limits_ironlake_dac = {
  281. .dot = { .min = 25000, .max = 350000 },
  282. .vco = { .min = 1760000, .max = 3510000 },
  283. .n = { .min = 1, .max = 5 },
  284. .m = { .min = 79, .max = 127 },
  285. .m1 = { .min = 12, .max = 22 },
  286. .m2 = { .min = 5, .max = 9 },
  287. .p = { .min = 5, .max = 80 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 225000,
  290. .p2_slow = 10, .p2_fast = 5 },
  291. };
  292. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  293. .dot = { .min = 25000, .max = 350000 },
  294. .vco = { .min = 1760000, .max = 3510000 },
  295. .n = { .min = 1, .max = 3 },
  296. .m = { .min = 79, .max = 118 },
  297. .m1 = { .min = 12, .max = 22 },
  298. .m2 = { .min = 5, .max = 9 },
  299. .p = { .min = 28, .max = 112 },
  300. .p1 = { .min = 2, .max = 8 },
  301. .p2 = { .dot_limit = 225000,
  302. .p2_slow = 14, .p2_fast = 14 },
  303. };
  304. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  305. .dot = { .min = 25000, .max = 350000 },
  306. .vco = { .min = 1760000, .max = 3510000 },
  307. .n = { .min = 1, .max = 3 },
  308. .m = { .min = 79, .max = 127 },
  309. .m1 = { .min = 12, .max = 22 },
  310. .m2 = { .min = 5, .max = 9 },
  311. .p = { .min = 14, .max = 56 },
  312. .p1 = { .min = 2, .max = 8 },
  313. .p2 = { .dot_limit = 225000,
  314. .p2_slow = 7, .p2_fast = 7 },
  315. };
  316. /* LVDS 100mhz refclk limits. */
  317. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  318. .dot = { .min = 25000, .max = 350000 },
  319. .vco = { .min = 1760000, .max = 3510000 },
  320. .n = { .min = 1, .max = 2 },
  321. .m = { .min = 79, .max = 126 },
  322. .m1 = { .min = 12, .max = 22 },
  323. .m2 = { .min = 5, .max = 9 },
  324. .p = { .min = 28, .max = 112 },
  325. .p1 = { .min = 2, .max = 8 },
  326. .p2 = { .dot_limit = 225000,
  327. .p2_slow = 14, .p2_fast = 14 },
  328. };
  329. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  330. .dot = { .min = 25000, .max = 350000 },
  331. .vco = { .min = 1760000, .max = 3510000 },
  332. .n = { .min = 1, .max = 3 },
  333. .m = { .min = 79, .max = 126 },
  334. .m1 = { .min = 12, .max = 22 },
  335. .m2 = { .min = 5, .max = 9 },
  336. .p = { .min = 14, .max = 42 },
  337. .p1 = { .min = 2, .max = 6 },
  338. .p2 = { .dot_limit = 225000,
  339. .p2_slow = 7, .p2_fast = 7 },
  340. };
  341. static const intel_limit_t intel_limits_vlv = {
  342. /*
  343. * These are the data rate limits (measured in fast clocks)
  344. * since those are the strictest limits we have. The fast
  345. * clock and actual rate limits are more relaxed, so checking
  346. * them would make no difference.
  347. */
  348. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  349. .vco = { .min = 4000000, .max = 6000000 },
  350. .n = { .min = 1, .max = 7 },
  351. .m1 = { .min = 2, .max = 3 },
  352. .m2 = { .min = 11, .max = 156 },
  353. .p1 = { .min = 2, .max = 3 },
  354. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  355. };
  356. static const intel_limit_t intel_limits_chv = {
  357. /*
  358. * These are the data rate limits (measured in fast clocks)
  359. * since those are the strictest limits we have. The fast
  360. * clock and actual rate limits are more relaxed, so checking
  361. * them would make no difference.
  362. */
  363. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  364. .vco = { .min = 4800000, .max = 6480000 },
  365. .n = { .min = 1, .max = 1 },
  366. .m1 = { .min = 2, .max = 2 },
  367. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  368. .p1 = { .min = 2, .max = 4 },
  369. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  370. };
  371. static const intel_limit_t intel_limits_bxt = {
  372. /* FIXME: find real dot limits */
  373. .dot = { .min = 0, .max = INT_MAX },
  374. .vco = { .min = 4800000, .max = 6480000 },
  375. .n = { .min = 1, .max = 1 },
  376. .m1 = { .min = 2, .max = 2 },
  377. /* FIXME: find real m2 limits */
  378. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  379. .p1 = { .min = 2, .max = 4 },
  380. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  381. };
  382. static void vlv_clock(int refclk, intel_clock_t *clock)
  383. {
  384. clock->m = clock->m1 * clock->m2;
  385. clock->p = clock->p1 * clock->p2;
  386. if (WARN_ON(clock->n == 0 || clock->p == 0))
  387. return;
  388. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  389. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  390. }
  391. /**
  392. * Returns whether any output on the specified pipe is of the specified type
  393. */
  394. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  395. {
  396. struct drm_device *dev = crtc->base.dev;
  397. struct intel_encoder *encoder;
  398. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  399. if (encoder->type == type)
  400. return true;
  401. return false;
  402. }
  403. /**
  404. * Returns whether any output on the specified pipe will have the specified
  405. * type after a staged modeset is complete, i.e., the same as
  406. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  407. * encoder->crtc.
  408. */
  409. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  410. int type)
  411. {
  412. struct drm_atomic_state *state = crtc_state->base.state;
  413. struct drm_connector_state *connector_state;
  414. struct intel_encoder *encoder;
  415. int i, num_connectors = 0;
  416. for (i = 0; i < state->num_connector; i++) {
  417. if (!state->connectors[i])
  418. continue;
  419. connector_state = state->connector_states[i];
  420. if (connector_state->crtc != crtc_state->base.crtc)
  421. continue;
  422. num_connectors++;
  423. encoder = to_intel_encoder(connector_state->best_encoder);
  424. if (encoder->type == type)
  425. return true;
  426. }
  427. WARN_ON(num_connectors == 0);
  428. return false;
  429. }
  430. static const intel_limit_t *
  431. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  432. {
  433. struct drm_device *dev = crtc_state->base.crtc->dev;
  434. const intel_limit_t *limit;
  435. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  436. if (intel_is_dual_link_lvds(dev)) {
  437. if (refclk == 100000)
  438. limit = &intel_limits_ironlake_dual_lvds_100m;
  439. else
  440. limit = &intel_limits_ironlake_dual_lvds;
  441. } else {
  442. if (refclk == 100000)
  443. limit = &intel_limits_ironlake_single_lvds_100m;
  444. else
  445. limit = &intel_limits_ironlake_single_lvds;
  446. }
  447. } else
  448. limit = &intel_limits_ironlake_dac;
  449. return limit;
  450. }
  451. static const intel_limit_t *
  452. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  453. {
  454. struct drm_device *dev = crtc_state->base.crtc->dev;
  455. const intel_limit_t *limit;
  456. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  457. if (intel_is_dual_link_lvds(dev))
  458. limit = &intel_limits_g4x_dual_channel_lvds;
  459. else
  460. limit = &intel_limits_g4x_single_channel_lvds;
  461. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  462. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  463. limit = &intel_limits_g4x_hdmi;
  464. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  465. limit = &intel_limits_g4x_sdvo;
  466. } else /* The option is for other outputs */
  467. limit = &intel_limits_i9xx_sdvo;
  468. return limit;
  469. }
  470. static const intel_limit_t *
  471. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  472. {
  473. struct drm_device *dev = crtc_state->base.crtc->dev;
  474. const intel_limit_t *limit;
  475. if (IS_BROXTON(dev))
  476. limit = &intel_limits_bxt;
  477. else if (HAS_PCH_SPLIT(dev))
  478. limit = intel_ironlake_limit(crtc_state, refclk);
  479. else if (IS_G4X(dev)) {
  480. limit = intel_g4x_limit(crtc_state);
  481. } else if (IS_PINEVIEW(dev)) {
  482. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  483. limit = &intel_limits_pineview_lvds;
  484. else
  485. limit = &intel_limits_pineview_sdvo;
  486. } else if (IS_CHERRYVIEW(dev)) {
  487. limit = &intel_limits_chv;
  488. } else if (IS_VALLEYVIEW(dev)) {
  489. limit = &intel_limits_vlv;
  490. } else if (!IS_GEN2(dev)) {
  491. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  492. limit = &intel_limits_i9xx_lvds;
  493. else
  494. limit = &intel_limits_i9xx_sdvo;
  495. } else {
  496. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  497. limit = &intel_limits_i8xx_lvds;
  498. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  499. limit = &intel_limits_i8xx_dvo;
  500. else
  501. limit = &intel_limits_i8xx_dac;
  502. }
  503. return limit;
  504. }
  505. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  506. static void pineview_clock(int refclk, intel_clock_t *clock)
  507. {
  508. clock->m = clock->m2 + 2;
  509. clock->p = clock->p1 * clock->p2;
  510. if (WARN_ON(clock->n == 0 || clock->p == 0))
  511. return;
  512. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  513. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  514. }
  515. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  516. {
  517. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  518. }
  519. static void i9xx_clock(int refclk, intel_clock_t *clock)
  520. {
  521. clock->m = i9xx_dpll_compute_m(clock);
  522. clock->p = clock->p1 * clock->p2;
  523. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  524. return;
  525. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  526. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  527. }
  528. static void chv_clock(int refclk, intel_clock_t *clock)
  529. {
  530. clock->m = clock->m1 * clock->m2;
  531. clock->p = clock->p1 * clock->p2;
  532. if (WARN_ON(clock->n == 0 || clock->p == 0))
  533. return;
  534. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  535. clock->n << 22);
  536. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  537. }
  538. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  539. /**
  540. * Returns whether the given set of divisors are valid for a given refclk with
  541. * the given connectors.
  542. */
  543. static bool intel_PLL_is_valid(struct drm_device *dev,
  544. const intel_limit_t *limit,
  545. const intel_clock_t *clock)
  546. {
  547. if (clock->n < limit->n.min || limit->n.max < clock->n)
  548. INTELPllInvalid("n out of range\n");
  549. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  550. INTELPllInvalid("p1 out of range\n");
  551. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  552. INTELPllInvalid("m2 out of range\n");
  553. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  554. INTELPllInvalid("m1 out of range\n");
  555. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  556. if (clock->m1 <= clock->m2)
  557. INTELPllInvalid("m1 <= m2\n");
  558. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  559. if (clock->p < limit->p.min || limit->p.max < clock->p)
  560. INTELPllInvalid("p out of range\n");
  561. if (clock->m < limit->m.min || limit->m.max < clock->m)
  562. INTELPllInvalid("m out of range\n");
  563. }
  564. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  565. INTELPllInvalid("vco out of range\n");
  566. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  567. * connector, etc., rather than just a single range.
  568. */
  569. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  570. INTELPllInvalid("dot out of range\n");
  571. return true;
  572. }
  573. static bool
  574. i9xx_find_best_dpll(const intel_limit_t *limit,
  575. struct intel_crtc_state *crtc_state,
  576. int target, int refclk, intel_clock_t *match_clock,
  577. intel_clock_t *best_clock)
  578. {
  579. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  580. struct drm_device *dev = crtc->base.dev;
  581. intel_clock_t clock;
  582. int err = target;
  583. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  584. /*
  585. * For LVDS just rely on its current settings for dual-channel.
  586. * We haven't figured out how to reliably set up different
  587. * single/dual channel state, if we even can.
  588. */
  589. if (intel_is_dual_link_lvds(dev))
  590. clock.p2 = limit->p2.p2_fast;
  591. else
  592. clock.p2 = limit->p2.p2_slow;
  593. } else {
  594. if (target < limit->p2.dot_limit)
  595. clock.p2 = limit->p2.p2_slow;
  596. else
  597. clock.p2 = limit->p2.p2_fast;
  598. }
  599. memset(best_clock, 0, sizeof(*best_clock));
  600. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  601. clock.m1++) {
  602. for (clock.m2 = limit->m2.min;
  603. clock.m2 <= limit->m2.max; clock.m2++) {
  604. if (clock.m2 >= clock.m1)
  605. break;
  606. for (clock.n = limit->n.min;
  607. clock.n <= limit->n.max; clock.n++) {
  608. for (clock.p1 = limit->p1.min;
  609. clock.p1 <= limit->p1.max; clock.p1++) {
  610. int this_err;
  611. i9xx_clock(refclk, &clock);
  612. if (!intel_PLL_is_valid(dev, limit,
  613. &clock))
  614. continue;
  615. if (match_clock &&
  616. clock.p != match_clock->p)
  617. continue;
  618. this_err = abs(clock.dot - target);
  619. if (this_err < err) {
  620. *best_clock = clock;
  621. err = this_err;
  622. }
  623. }
  624. }
  625. }
  626. }
  627. return (err != target);
  628. }
  629. static bool
  630. pnv_find_best_dpll(const intel_limit_t *limit,
  631. struct intel_crtc_state *crtc_state,
  632. int target, int refclk, intel_clock_t *match_clock,
  633. intel_clock_t *best_clock)
  634. {
  635. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  636. struct drm_device *dev = crtc->base.dev;
  637. intel_clock_t clock;
  638. int err = target;
  639. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  640. /*
  641. * For LVDS just rely on its current settings for dual-channel.
  642. * We haven't figured out how to reliably set up different
  643. * single/dual channel state, if we even can.
  644. */
  645. if (intel_is_dual_link_lvds(dev))
  646. clock.p2 = limit->p2.p2_fast;
  647. else
  648. clock.p2 = limit->p2.p2_slow;
  649. } else {
  650. if (target < limit->p2.dot_limit)
  651. clock.p2 = limit->p2.p2_slow;
  652. else
  653. clock.p2 = limit->p2.p2_fast;
  654. }
  655. memset(best_clock, 0, sizeof(*best_clock));
  656. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  657. clock.m1++) {
  658. for (clock.m2 = limit->m2.min;
  659. clock.m2 <= limit->m2.max; clock.m2++) {
  660. for (clock.n = limit->n.min;
  661. clock.n <= limit->n.max; clock.n++) {
  662. for (clock.p1 = limit->p1.min;
  663. clock.p1 <= limit->p1.max; clock.p1++) {
  664. int this_err;
  665. pineview_clock(refclk, &clock);
  666. if (!intel_PLL_is_valid(dev, limit,
  667. &clock))
  668. continue;
  669. if (match_clock &&
  670. clock.p != match_clock->p)
  671. continue;
  672. this_err = abs(clock.dot - target);
  673. if (this_err < err) {
  674. *best_clock = clock;
  675. err = this_err;
  676. }
  677. }
  678. }
  679. }
  680. }
  681. return (err != target);
  682. }
  683. static bool
  684. g4x_find_best_dpll(const intel_limit_t *limit,
  685. struct intel_crtc_state *crtc_state,
  686. int target, int refclk, intel_clock_t *match_clock,
  687. intel_clock_t *best_clock)
  688. {
  689. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  690. struct drm_device *dev = crtc->base.dev;
  691. intel_clock_t clock;
  692. int max_n;
  693. bool found;
  694. /* approximately equals target * 0.00585 */
  695. int err_most = (target >> 8) + (target >> 9);
  696. found = false;
  697. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  698. if (intel_is_dual_link_lvds(dev))
  699. clock.p2 = limit->p2.p2_fast;
  700. else
  701. clock.p2 = limit->p2.p2_slow;
  702. } else {
  703. if (target < limit->p2.dot_limit)
  704. clock.p2 = limit->p2.p2_slow;
  705. else
  706. clock.p2 = limit->p2.p2_fast;
  707. }
  708. memset(best_clock, 0, sizeof(*best_clock));
  709. max_n = limit->n.max;
  710. /* based on hardware requirement, prefer smaller n to precision */
  711. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  712. /* based on hardware requirement, prefere larger m1,m2 */
  713. for (clock.m1 = limit->m1.max;
  714. clock.m1 >= limit->m1.min; clock.m1--) {
  715. for (clock.m2 = limit->m2.max;
  716. clock.m2 >= limit->m2.min; clock.m2--) {
  717. for (clock.p1 = limit->p1.max;
  718. clock.p1 >= limit->p1.min; clock.p1--) {
  719. int this_err;
  720. i9xx_clock(refclk, &clock);
  721. if (!intel_PLL_is_valid(dev, limit,
  722. &clock))
  723. continue;
  724. this_err = abs(clock.dot - target);
  725. if (this_err < err_most) {
  726. *best_clock = clock;
  727. err_most = this_err;
  728. max_n = clock.n;
  729. found = true;
  730. }
  731. }
  732. }
  733. }
  734. }
  735. return found;
  736. }
  737. /*
  738. * Check if the calculated PLL configuration is more optimal compared to the
  739. * best configuration and error found so far. Return the calculated error.
  740. */
  741. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  742. const intel_clock_t *calculated_clock,
  743. const intel_clock_t *best_clock,
  744. unsigned int best_error_ppm,
  745. unsigned int *error_ppm)
  746. {
  747. /*
  748. * For CHV ignore the error and consider only the P value.
  749. * Prefer a bigger P value based on HW requirements.
  750. */
  751. if (IS_CHERRYVIEW(dev)) {
  752. *error_ppm = 0;
  753. return calculated_clock->p > best_clock->p;
  754. }
  755. if (WARN_ON_ONCE(!target_freq))
  756. return false;
  757. *error_ppm = div_u64(1000000ULL *
  758. abs(target_freq - calculated_clock->dot),
  759. target_freq);
  760. /*
  761. * Prefer a better P value over a better (smaller) error if the error
  762. * is small. Ensure this preference for future configurations too by
  763. * setting the error to 0.
  764. */
  765. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  766. *error_ppm = 0;
  767. return true;
  768. }
  769. return *error_ppm + 10 < best_error_ppm;
  770. }
  771. static bool
  772. vlv_find_best_dpll(const intel_limit_t *limit,
  773. struct intel_crtc_state *crtc_state,
  774. int target, int refclk, intel_clock_t *match_clock,
  775. intel_clock_t *best_clock)
  776. {
  777. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  778. struct drm_device *dev = crtc->base.dev;
  779. intel_clock_t clock;
  780. unsigned int bestppm = 1000000;
  781. /* min update 19.2 MHz */
  782. int max_n = min(limit->n.max, refclk / 19200);
  783. bool found = false;
  784. target *= 5; /* fast clock */
  785. memset(best_clock, 0, sizeof(*best_clock));
  786. /* based on hardware requirement, prefer smaller n to precision */
  787. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  788. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  789. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  790. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  791. clock.p = clock.p1 * clock.p2;
  792. /* based on hardware requirement, prefer bigger m1,m2 values */
  793. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  794. unsigned int ppm;
  795. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  796. refclk * clock.m1);
  797. vlv_clock(refclk, &clock);
  798. if (!intel_PLL_is_valid(dev, limit,
  799. &clock))
  800. continue;
  801. if (!vlv_PLL_is_optimal(dev, target,
  802. &clock,
  803. best_clock,
  804. bestppm, &ppm))
  805. continue;
  806. *best_clock = clock;
  807. bestppm = ppm;
  808. found = true;
  809. }
  810. }
  811. }
  812. }
  813. return found;
  814. }
  815. static bool
  816. chv_find_best_dpll(const intel_limit_t *limit,
  817. struct intel_crtc_state *crtc_state,
  818. int target, int refclk, intel_clock_t *match_clock,
  819. intel_clock_t *best_clock)
  820. {
  821. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  822. struct drm_device *dev = crtc->base.dev;
  823. unsigned int best_error_ppm;
  824. intel_clock_t clock;
  825. uint64_t m2;
  826. int found = false;
  827. memset(best_clock, 0, sizeof(*best_clock));
  828. best_error_ppm = 1000000;
  829. /*
  830. * Based on hardware doc, the n always set to 1, and m1 always
  831. * set to 2. If requires to support 200Mhz refclk, we need to
  832. * revisit this because n may not 1 anymore.
  833. */
  834. clock.n = 1, clock.m1 = 2;
  835. target *= 5; /* fast clock */
  836. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  837. for (clock.p2 = limit->p2.p2_fast;
  838. clock.p2 >= limit->p2.p2_slow;
  839. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  840. unsigned int error_ppm;
  841. clock.p = clock.p1 * clock.p2;
  842. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  843. clock.n) << 22, refclk * clock.m1);
  844. if (m2 > INT_MAX/clock.m1)
  845. continue;
  846. clock.m2 = m2;
  847. chv_clock(refclk, &clock);
  848. if (!intel_PLL_is_valid(dev, limit, &clock))
  849. continue;
  850. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  851. best_error_ppm, &error_ppm))
  852. continue;
  853. *best_clock = clock;
  854. best_error_ppm = error_ppm;
  855. found = true;
  856. }
  857. }
  858. return found;
  859. }
  860. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  861. intel_clock_t *best_clock)
  862. {
  863. int refclk = i9xx_get_refclk(crtc_state, 0);
  864. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  865. target_clock, refclk, NULL, best_clock);
  866. }
  867. bool intel_crtc_active(struct drm_crtc *crtc)
  868. {
  869. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  870. /* Be paranoid as we can arrive here with only partial
  871. * state retrieved from the hardware during setup.
  872. *
  873. * We can ditch the adjusted_mode.crtc_clock check as soon
  874. * as Haswell has gained clock readout/fastboot support.
  875. *
  876. * We can ditch the crtc->primary->fb check as soon as we can
  877. * properly reconstruct framebuffers.
  878. *
  879. * FIXME: The intel_crtc->active here should be switched to
  880. * crtc->state->active once we have proper CRTC states wired up
  881. * for atomic.
  882. */
  883. return intel_crtc->active && crtc->primary->state->fb &&
  884. intel_crtc->config->base.adjusted_mode.crtc_clock;
  885. }
  886. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  887. enum pipe pipe)
  888. {
  889. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  890. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  891. return intel_crtc->config->cpu_transcoder;
  892. }
  893. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  894. {
  895. struct drm_i915_private *dev_priv = dev->dev_private;
  896. u32 reg = PIPEDSL(pipe);
  897. u32 line1, line2;
  898. u32 line_mask;
  899. if (IS_GEN2(dev))
  900. line_mask = DSL_LINEMASK_GEN2;
  901. else
  902. line_mask = DSL_LINEMASK_GEN3;
  903. line1 = I915_READ(reg) & line_mask;
  904. mdelay(5);
  905. line2 = I915_READ(reg) & line_mask;
  906. return line1 == line2;
  907. }
  908. /*
  909. * intel_wait_for_pipe_off - wait for pipe to turn off
  910. * @crtc: crtc whose pipe to wait for
  911. *
  912. * After disabling a pipe, we can't wait for vblank in the usual way,
  913. * spinning on the vblank interrupt status bit, since we won't actually
  914. * see an interrupt when the pipe is disabled.
  915. *
  916. * On Gen4 and above:
  917. * wait for the pipe register state bit to turn off
  918. *
  919. * Otherwise:
  920. * wait for the display line value to settle (it usually
  921. * ends up stopping at the start of the next frame).
  922. *
  923. */
  924. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  925. {
  926. struct drm_device *dev = crtc->base.dev;
  927. struct drm_i915_private *dev_priv = dev->dev_private;
  928. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  929. enum pipe pipe = crtc->pipe;
  930. if (INTEL_INFO(dev)->gen >= 4) {
  931. int reg = PIPECONF(cpu_transcoder);
  932. /* Wait for the Pipe State to go off */
  933. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  934. 100))
  935. WARN(1, "pipe_off wait timed out\n");
  936. } else {
  937. /* Wait for the display line to settle */
  938. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  939. WARN(1, "pipe_off wait timed out\n");
  940. }
  941. }
  942. /*
  943. * ibx_digital_port_connected - is the specified port connected?
  944. * @dev_priv: i915 private structure
  945. * @port: the port to test
  946. *
  947. * Returns true if @port is connected, false otherwise.
  948. */
  949. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  950. struct intel_digital_port *port)
  951. {
  952. u32 bit;
  953. if (HAS_PCH_IBX(dev_priv->dev)) {
  954. switch (port->port) {
  955. case PORT_B:
  956. bit = SDE_PORTB_HOTPLUG;
  957. break;
  958. case PORT_C:
  959. bit = SDE_PORTC_HOTPLUG;
  960. break;
  961. case PORT_D:
  962. bit = SDE_PORTD_HOTPLUG;
  963. break;
  964. default:
  965. return true;
  966. }
  967. } else {
  968. switch (port->port) {
  969. case PORT_B:
  970. bit = SDE_PORTB_HOTPLUG_CPT;
  971. break;
  972. case PORT_C:
  973. bit = SDE_PORTC_HOTPLUG_CPT;
  974. break;
  975. case PORT_D:
  976. bit = SDE_PORTD_HOTPLUG_CPT;
  977. break;
  978. default:
  979. return true;
  980. }
  981. }
  982. return I915_READ(SDEISR) & bit;
  983. }
  984. static const char *state_string(bool enabled)
  985. {
  986. return enabled ? "on" : "off";
  987. }
  988. /* Only for pre-ILK configs */
  989. void assert_pll(struct drm_i915_private *dev_priv,
  990. enum pipe pipe, bool state)
  991. {
  992. int reg;
  993. u32 val;
  994. bool cur_state;
  995. reg = DPLL(pipe);
  996. val = I915_READ(reg);
  997. cur_state = !!(val & DPLL_VCO_ENABLE);
  998. I915_STATE_WARN(cur_state != state,
  999. "PLL state assertion failure (expected %s, current %s)\n",
  1000. state_string(state), state_string(cur_state));
  1001. }
  1002. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1003. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1004. {
  1005. u32 val;
  1006. bool cur_state;
  1007. mutex_lock(&dev_priv->dpio_lock);
  1008. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1009. mutex_unlock(&dev_priv->dpio_lock);
  1010. cur_state = val & DSI_PLL_VCO_EN;
  1011. I915_STATE_WARN(cur_state != state,
  1012. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1013. state_string(state), state_string(cur_state));
  1014. }
  1015. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1016. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1017. struct intel_shared_dpll *
  1018. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1019. {
  1020. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1021. if (crtc->config->shared_dpll < 0)
  1022. return NULL;
  1023. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1024. }
  1025. /* For ILK+ */
  1026. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1027. struct intel_shared_dpll *pll,
  1028. bool state)
  1029. {
  1030. bool cur_state;
  1031. struct intel_dpll_hw_state hw_state;
  1032. if (WARN (!pll,
  1033. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1034. return;
  1035. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1036. I915_STATE_WARN(cur_state != state,
  1037. "%s assertion failure (expected %s, current %s)\n",
  1038. pll->name, state_string(state), state_string(cur_state));
  1039. }
  1040. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1041. enum pipe pipe, bool state)
  1042. {
  1043. int reg;
  1044. u32 val;
  1045. bool cur_state;
  1046. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1047. pipe);
  1048. if (HAS_DDI(dev_priv->dev)) {
  1049. /* DDI does not have a specific FDI_TX register */
  1050. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1051. val = I915_READ(reg);
  1052. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1053. } else {
  1054. reg = FDI_TX_CTL(pipe);
  1055. val = I915_READ(reg);
  1056. cur_state = !!(val & FDI_TX_ENABLE);
  1057. }
  1058. I915_STATE_WARN(cur_state != state,
  1059. "FDI TX state assertion failure (expected %s, current %s)\n",
  1060. state_string(state), state_string(cur_state));
  1061. }
  1062. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1063. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1064. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1065. enum pipe pipe, bool state)
  1066. {
  1067. int reg;
  1068. u32 val;
  1069. bool cur_state;
  1070. reg = FDI_RX_CTL(pipe);
  1071. val = I915_READ(reg);
  1072. cur_state = !!(val & FDI_RX_ENABLE);
  1073. I915_STATE_WARN(cur_state != state,
  1074. "FDI RX state assertion failure (expected %s, current %s)\n",
  1075. state_string(state), state_string(cur_state));
  1076. }
  1077. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1078. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1079. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1080. enum pipe pipe)
  1081. {
  1082. int reg;
  1083. u32 val;
  1084. /* ILK FDI PLL is always enabled */
  1085. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1086. return;
  1087. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1088. if (HAS_DDI(dev_priv->dev))
  1089. return;
  1090. reg = FDI_TX_CTL(pipe);
  1091. val = I915_READ(reg);
  1092. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1093. }
  1094. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1095. enum pipe pipe, bool state)
  1096. {
  1097. int reg;
  1098. u32 val;
  1099. bool cur_state;
  1100. reg = FDI_RX_CTL(pipe);
  1101. val = I915_READ(reg);
  1102. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1103. I915_STATE_WARN(cur_state != state,
  1104. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1105. state_string(state), state_string(cur_state));
  1106. }
  1107. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1108. enum pipe pipe)
  1109. {
  1110. struct drm_device *dev = dev_priv->dev;
  1111. int pp_reg;
  1112. u32 val;
  1113. enum pipe panel_pipe = PIPE_A;
  1114. bool locked = true;
  1115. if (WARN_ON(HAS_DDI(dev)))
  1116. return;
  1117. if (HAS_PCH_SPLIT(dev)) {
  1118. u32 port_sel;
  1119. pp_reg = PCH_PP_CONTROL;
  1120. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1121. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1122. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1123. panel_pipe = PIPE_B;
  1124. /* XXX: else fix for eDP */
  1125. } else if (IS_VALLEYVIEW(dev)) {
  1126. /* presumably write lock depends on pipe, not port select */
  1127. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1128. panel_pipe = pipe;
  1129. } else {
  1130. pp_reg = PP_CONTROL;
  1131. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1132. panel_pipe = PIPE_B;
  1133. }
  1134. val = I915_READ(pp_reg);
  1135. if (!(val & PANEL_POWER_ON) ||
  1136. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1137. locked = false;
  1138. I915_STATE_WARN(panel_pipe == pipe && locked,
  1139. "panel assertion failure, pipe %c regs locked\n",
  1140. pipe_name(pipe));
  1141. }
  1142. static void assert_cursor(struct drm_i915_private *dev_priv,
  1143. enum pipe pipe, bool state)
  1144. {
  1145. struct drm_device *dev = dev_priv->dev;
  1146. bool cur_state;
  1147. if (IS_845G(dev) || IS_I865G(dev))
  1148. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1149. else
  1150. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1151. I915_STATE_WARN(cur_state != state,
  1152. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1153. pipe_name(pipe), state_string(state), state_string(cur_state));
  1154. }
  1155. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1156. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1157. void assert_pipe(struct drm_i915_private *dev_priv,
  1158. enum pipe pipe, bool state)
  1159. {
  1160. int reg;
  1161. u32 val;
  1162. bool cur_state;
  1163. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1164. pipe);
  1165. /* if we need the pipe quirk it must be always on */
  1166. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1167. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1168. state = true;
  1169. if (!intel_display_power_is_enabled(dev_priv,
  1170. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1171. cur_state = false;
  1172. } else {
  1173. reg = PIPECONF(cpu_transcoder);
  1174. val = I915_READ(reg);
  1175. cur_state = !!(val & PIPECONF_ENABLE);
  1176. }
  1177. I915_STATE_WARN(cur_state != state,
  1178. "pipe %c assertion failure (expected %s, current %s)\n",
  1179. pipe_name(pipe), state_string(state), state_string(cur_state));
  1180. }
  1181. static void assert_plane(struct drm_i915_private *dev_priv,
  1182. enum plane plane, bool state)
  1183. {
  1184. int reg;
  1185. u32 val;
  1186. bool cur_state;
  1187. reg = DSPCNTR(plane);
  1188. val = I915_READ(reg);
  1189. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1190. I915_STATE_WARN(cur_state != state,
  1191. "plane %c assertion failure (expected %s, current %s)\n",
  1192. plane_name(plane), state_string(state), state_string(cur_state));
  1193. }
  1194. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1195. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1196. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe)
  1198. {
  1199. struct drm_device *dev = dev_priv->dev;
  1200. int reg, i;
  1201. u32 val;
  1202. int cur_pipe;
  1203. /* Primary planes are fixed to pipes on gen4+ */
  1204. if (INTEL_INFO(dev)->gen >= 4) {
  1205. reg = DSPCNTR(pipe);
  1206. val = I915_READ(reg);
  1207. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1208. "plane %c assertion failure, should be disabled but not\n",
  1209. plane_name(pipe));
  1210. return;
  1211. }
  1212. /* Need to check both planes against the pipe */
  1213. for_each_pipe(dev_priv, i) {
  1214. reg = DSPCNTR(i);
  1215. val = I915_READ(reg);
  1216. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1217. DISPPLANE_SEL_PIPE_SHIFT;
  1218. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1219. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1220. plane_name(i), pipe_name(pipe));
  1221. }
  1222. }
  1223. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1224. enum pipe pipe)
  1225. {
  1226. struct drm_device *dev = dev_priv->dev;
  1227. int reg, sprite;
  1228. u32 val;
  1229. if (INTEL_INFO(dev)->gen >= 9) {
  1230. for_each_sprite(dev_priv, pipe, sprite) {
  1231. val = I915_READ(PLANE_CTL(pipe, sprite));
  1232. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1233. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1234. sprite, pipe_name(pipe));
  1235. }
  1236. } else if (IS_VALLEYVIEW(dev)) {
  1237. for_each_sprite(dev_priv, pipe, sprite) {
  1238. reg = SPCNTR(pipe, sprite);
  1239. val = I915_READ(reg);
  1240. I915_STATE_WARN(val & SP_ENABLE,
  1241. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1242. sprite_name(pipe, sprite), pipe_name(pipe));
  1243. }
  1244. } else if (INTEL_INFO(dev)->gen >= 7) {
  1245. reg = SPRCTL(pipe);
  1246. val = I915_READ(reg);
  1247. I915_STATE_WARN(val & SPRITE_ENABLE,
  1248. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1249. plane_name(pipe), pipe_name(pipe));
  1250. } else if (INTEL_INFO(dev)->gen >= 5) {
  1251. reg = DVSCNTR(pipe);
  1252. val = I915_READ(reg);
  1253. I915_STATE_WARN(val & DVS_ENABLE,
  1254. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1255. plane_name(pipe), pipe_name(pipe));
  1256. }
  1257. }
  1258. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1259. {
  1260. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1261. drm_crtc_vblank_put(crtc);
  1262. }
  1263. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1264. {
  1265. u32 val;
  1266. bool enabled;
  1267. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1268. val = I915_READ(PCH_DREF_CONTROL);
  1269. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1270. DREF_SUPERSPREAD_SOURCE_MASK));
  1271. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1272. }
  1273. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1274. enum pipe pipe)
  1275. {
  1276. int reg;
  1277. u32 val;
  1278. bool enabled;
  1279. reg = PCH_TRANSCONF(pipe);
  1280. val = I915_READ(reg);
  1281. enabled = !!(val & TRANS_ENABLE);
  1282. I915_STATE_WARN(enabled,
  1283. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1284. pipe_name(pipe));
  1285. }
  1286. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1287. enum pipe pipe, u32 port_sel, u32 val)
  1288. {
  1289. if ((val & DP_PORT_EN) == 0)
  1290. return false;
  1291. if (HAS_PCH_CPT(dev_priv->dev)) {
  1292. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1293. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1294. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1295. return false;
  1296. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1297. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1298. return false;
  1299. } else {
  1300. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1301. return false;
  1302. }
  1303. return true;
  1304. }
  1305. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1306. enum pipe pipe, u32 val)
  1307. {
  1308. if ((val & SDVO_ENABLE) == 0)
  1309. return false;
  1310. if (HAS_PCH_CPT(dev_priv->dev)) {
  1311. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1312. return false;
  1313. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1314. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1315. return false;
  1316. } else {
  1317. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1318. return false;
  1319. }
  1320. return true;
  1321. }
  1322. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1323. enum pipe pipe, u32 val)
  1324. {
  1325. if ((val & LVDS_PORT_EN) == 0)
  1326. return false;
  1327. if (HAS_PCH_CPT(dev_priv->dev)) {
  1328. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1329. return false;
  1330. } else {
  1331. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1332. return false;
  1333. }
  1334. return true;
  1335. }
  1336. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1337. enum pipe pipe, u32 val)
  1338. {
  1339. if ((val & ADPA_DAC_ENABLE) == 0)
  1340. return false;
  1341. if (HAS_PCH_CPT(dev_priv->dev)) {
  1342. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1343. return false;
  1344. } else {
  1345. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1346. return false;
  1347. }
  1348. return true;
  1349. }
  1350. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1351. enum pipe pipe, int reg, u32 port_sel)
  1352. {
  1353. u32 val = I915_READ(reg);
  1354. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1355. "PCH DP (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 & DP_PORT_EN) == 0
  1358. && (val & DP_PIPEB_SELECT),
  1359. "IBX PCH dp port still using transcoder B\n");
  1360. }
  1361. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1362. enum pipe pipe, int reg)
  1363. {
  1364. u32 val = I915_READ(reg);
  1365. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1366. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1367. reg, pipe_name(pipe));
  1368. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1369. && (val & SDVO_PIPE_B_SELECT),
  1370. "IBX PCH hdmi port still using transcoder B\n");
  1371. }
  1372. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1373. enum pipe pipe)
  1374. {
  1375. int reg;
  1376. u32 val;
  1377. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1378. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1379. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1380. reg = PCH_ADPA;
  1381. val = I915_READ(reg);
  1382. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1383. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1384. pipe_name(pipe));
  1385. reg = PCH_LVDS;
  1386. val = I915_READ(reg);
  1387. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1388. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1389. pipe_name(pipe));
  1390. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1391. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1392. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1393. }
  1394. static void intel_init_dpio(struct drm_device *dev)
  1395. {
  1396. struct drm_i915_private *dev_priv = dev->dev_private;
  1397. if (!IS_VALLEYVIEW(dev))
  1398. return;
  1399. /*
  1400. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1401. * CHV x1 PHY (DP/HDMI D)
  1402. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1403. */
  1404. if (IS_CHERRYVIEW(dev)) {
  1405. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1406. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1407. } else {
  1408. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1409. }
  1410. }
  1411. static void vlv_enable_pll(struct intel_crtc *crtc,
  1412. const struct intel_crtc_state *pipe_config)
  1413. {
  1414. struct drm_device *dev = crtc->base.dev;
  1415. struct drm_i915_private *dev_priv = dev->dev_private;
  1416. int reg = DPLL(crtc->pipe);
  1417. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1418. assert_pipe_disabled(dev_priv, crtc->pipe);
  1419. /* No really, not for ILK+ */
  1420. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1421. /* PLL is protected by panel, make sure we can write it */
  1422. if (IS_MOBILE(dev_priv->dev))
  1423. assert_panel_unlocked(dev_priv, crtc->pipe);
  1424. I915_WRITE(reg, dpll);
  1425. POSTING_READ(reg);
  1426. udelay(150);
  1427. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1428. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1429. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1430. POSTING_READ(DPLL_MD(crtc->pipe));
  1431. /* We do this three times for luck */
  1432. I915_WRITE(reg, dpll);
  1433. POSTING_READ(reg);
  1434. udelay(150); /* wait for warmup */
  1435. I915_WRITE(reg, dpll);
  1436. POSTING_READ(reg);
  1437. udelay(150); /* wait for warmup */
  1438. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. }
  1442. static void chv_enable_pll(struct intel_crtc *crtc,
  1443. const struct intel_crtc_state *pipe_config)
  1444. {
  1445. struct drm_device *dev = crtc->base.dev;
  1446. struct drm_i915_private *dev_priv = dev->dev_private;
  1447. int pipe = crtc->pipe;
  1448. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1449. u32 tmp;
  1450. assert_pipe_disabled(dev_priv, crtc->pipe);
  1451. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1452. mutex_lock(&dev_priv->dpio_lock);
  1453. /* Enable back the 10bit clock to display controller */
  1454. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1455. tmp |= DPIO_DCLKP_EN;
  1456. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1457. /*
  1458. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1459. */
  1460. udelay(1);
  1461. /* Enable PLL */
  1462. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1463. /* Check PLL is locked */
  1464. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1465. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1466. /* not sure when this should be written */
  1467. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1468. POSTING_READ(DPLL_MD(pipe));
  1469. mutex_unlock(&dev_priv->dpio_lock);
  1470. }
  1471. static int intel_num_dvo_pipes(struct drm_device *dev)
  1472. {
  1473. struct intel_crtc *crtc;
  1474. int count = 0;
  1475. for_each_intel_crtc(dev, crtc)
  1476. count += crtc->active &&
  1477. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1478. return count;
  1479. }
  1480. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1481. {
  1482. struct drm_device *dev = crtc->base.dev;
  1483. struct drm_i915_private *dev_priv = dev->dev_private;
  1484. int reg = DPLL(crtc->pipe);
  1485. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1486. assert_pipe_disabled(dev_priv, crtc->pipe);
  1487. /* No really, not for ILK+ */
  1488. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1489. /* PLL is protected by panel, make sure we can write it */
  1490. if (IS_MOBILE(dev) && !IS_I830(dev))
  1491. assert_panel_unlocked(dev_priv, crtc->pipe);
  1492. /* Enable DVO 2x clock on both PLLs if necessary */
  1493. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1494. /*
  1495. * It appears to be important that we don't enable this
  1496. * for the current pipe before otherwise configuring the
  1497. * PLL. No idea how this should be handled if multiple
  1498. * DVO outputs are enabled simultaneosly.
  1499. */
  1500. dpll |= DPLL_DVO_2X_MODE;
  1501. I915_WRITE(DPLL(!crtc->pipe),
  1502. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1503. }
  1504. /* Wait for the clocks to stabilize. */
  1505. POSTING_READ(reg);
  1506. udelay(150);
  1507. if (INTEL_INFO(dev)->gen >= 4) {
  1508. I915_WRITE(DPLL_MD(crtc->pipe),
  1509. crtc->config->dpll_hw_state.dpll_md);
  1510. } else {
  1511. /* The pixel multiplier can only be updated once the
  1512. * DPLL is enabled and the clocks are stable.
  1513. *
  1514. * So write it again.
  1515. */
  1516. I915_WRITE(reg, dpll);
  1517. }
  1518. /* We do this three times for luck */
  1519. I915_WRITE(reg, dpll);
  1520. POSTING_READ(reg);
  1521. udelay(150); /* wait for warmup */
  1522. I915_WRITE(reg, dpll);
  1523. POSTING_READ(reg);
  1524. udelay(150); /* wait for warmup */
  1525. I915_WRITE(reg, dpll);
  1526. POSTING_READ(reg);
  1527. udelay(150); /* wait for warmup */
  1528. }
  1529. /**
  1530. * i9xx_disable_pll - disable a PLL
  1531. * @dev_priv: i915 private structure
  1532. * @pipe: pipe PLL to disable
  1533. *
  1534. * Disable the PLL for @pipe, making sure the pipe is off first.
  1535. *
  1536. * Note! This is for pre-ILK only.
  1537. */
  1538. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1539. {
  1540. struct drm_device *dev = crtc->base.dev;
  1541. struct drm_i915_private *dev_priv = dev->dev_private;
  1542. enum pipe pipe = crtc->pipe;
  1543. /* Disable DVO 2x clock on both PLLs if necessary */
  1544. if (IS_I830(dev) &&
  1545. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1546. intel_num_dvo_pipes(dev) == 1) {
  1547. I915_WRITE(DPLL(PIPE_B),
  1548. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1549. I915_WRITE(DPLL(PIPE_A),
  1550. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1551. }
  1552. /* Don't disable pipe or pipe PLLs if needed */
  1553. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1554. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1555. return;
  1556. /* Make sure the pipe isn't still relying on us */
  1557. assert_pipe_disabled(dev_priv, pipe);
  1558. I915_WRITE(DPLL(pipe), 0);
  1559. POSTING_READ(DPLL(pipe));
  1560. }
  1561. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1562. {
  1563. u32 val = 0;
  1564. /* Make sure the pipe isn't still relying on us */
  1565. assert_pipe_disabled(dev_priv, pipe);
  1566. /*
  1567. * Leave integrated clock source and reference clock enabled for pipe B.
  1568. * The latter is needed for VGA hotplug / manual detection.
  1569. */
  1570. if (pipe == PIPE_B)
  1571. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1572. I915_WRITE(DPLL(pipe), val);
  1573. POSTING_READ(DPLL(pipe));
  1574. }
  1575. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1576. {
  1577. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1578. u32 val;
  1579. /* Make sure the pipe isn't still relying on us */
  1580. assert_pipe_disabled(dev_priv, pipe);
  1581. /* Set PLL en = 0 */
  1582. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1583. if (pipe != PIPE_A)
  1584. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1585. I915_WRITE(DPLL(pipe), val);
  1586. POSTING_READ(DPLL(pipe));
  1587. mutex_lock(&dev_priv->dpio_lock);
  1588. /* Disable 10bit clock to display controller */
  1589. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1590. val &= ~DPIO_DCLKP_EN;
  1591. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1592. /* disable left/right clock distribution */
  1593. if (pipe != PIPE_B) {
  1594. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1595. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1596. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1597. } else {
  1598. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1599. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1600. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1601. }
  1602. mutex_unlock(&dev_priv->dpio_lock);
  1603. }
  1604. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1605. struct intel_digital_port *dport)
  1606. {
  1607. u32 port_mask;
  1608. int dpll_reg;
  1609. switch (dport->port) {
  1610. case PORT_B:
  1611. port_mask = DPLL_PORTB_READY_MASK;
  1612. dpll_reg = DPLL(0);
  1613. break;
  1614. case PORT_C:
  1615. port_mask = DPLL_PORTC_READY_MASK;
  1616. dpll_reg = DPLL(0);
  1617. break;
  1618. case PORT_D:
  1619. port_mask = DPLL_PORTD_READY_MASK;
  1620. dpll_reg = DPIO_PHY_STATUS;
  1621. break;
  1622. default:
  1623. BUG();
  1624. }
  1625. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1626. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1627. port_name(dport->port), I915_READ(dpll_reg));
  1628. }
  1629. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1630. {
  1631. struct drm_device *dev = crtc->base.dev;
  1632. struct drm_i915_private *dev_priv = dev->dev_private;
  1633. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1634. if (WARN_ON(pll == NULL))
  1635. return;
  1636. WARN_ON(!pll->config.crtc_mask);
  1637. if (pll->active == 0) {
  1638. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1639. WARN_ON(pll->on);
  1640. assert_shared_dpll_disabled(dev_priv, pll);
  1641. pll->mode_set(dev_priv, pll);
  1642. }
  1643. }
  1644. /**
  1645. * intel_enable_shared_dpll - enable PCH PLL
  1646. * @dev_priv: i915 private structure
  1647. * @pipe: pipe PLL to enable
  1648. *
  1649. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1650. * drives the transcoder clock.
  1651. */
  1652. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1653. {
  1654. struct drm_device *dev = crtc->base.dev;
  1655. struct drm_i915_private *dev_priv = dev->dev_private;
  1656. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1657. if (WARN_ON(pll == NULL))
  1658. return;
  1659. if (WARN_ON(pll->config.crtc_mask == 0))
  1660. return;
  1661. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1662. pll->name, pll->active, pll->on,
  1663. crtc->base.base.id);
  1664. if (pll->active++) {
  1665. WARN_ON(!pll->on);
  1666. assert_shared_dpll_enabled(dev_priv, pll);
  1667. return;
  1668. }
  1669. WARN_ON(pll->on);
  1670. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1671. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1672. pll->enable(dev_priv, pll);
  1673. pll->on = true;
  1674. }
  1675. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1676. {
  1677. struct drm_device *dev = crtc->base.dev;
  1678. struct drm_i915_private *dev_priv = dev->dev_private;
  1679. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1680. /* PCH only available on ILK+ */
  1681. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1682. if (WARN_ON(pll == NULL))
  1683. return;
  1684. if (WARN_ON(pll->config.crtc_mask == 0))
  1685. return;
  1686. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1687. pll->name, pll->active, pll->on,
  1688. crtc->base.base.id);
  1689. if (WARN_ON(pll->active == 0)) {
  1690. assert_shared_dpll_disabled(dev_priv, pll);
  1691. return;
  1692. }
  1693. assert_shared_dpll_enabled(dev_priv, pll);
  1694. WARN_ON(!pll->on);
  1695. if (--pll->active)
  1696. return;
  1697. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1698. pll->disable(dev_priv, pll);
  1699. pll->on = false;
  1700. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1701. }
  1702. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1703. enum pipe pipe)
  1704. {
  1705. struct drm_device *dev = dev_priv->dev;
  1706. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1707. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1708. uint32_t reg, val, pipeconf_val;
  1709. /* PCH only available on ILK+ */
  1710. BUG_ON(!HAS_PCH_SPLIT(dev));
  1711. /* Make sure PCH DPLL is enabled */
  1712. assert_shared_dpll_enabled(dev_priv,
  1713. intel_crtc_to_shared_dpll(intel_crtc));
  1714. /* FDI must be feeding us bits for PCH ports */
  1715. assert_fdi_tx_enabled(dev_priv, pipe);
  1716. assert_fdi_rx_enabled(dev_priv, pipe);
  1717. if (HAS_PCH_CPT(dev)) {
  1718. /* Workaround: Set the timing override bit before enabling the
  1719. * pch transcoder. */
  1720. reg = TRANS_CHICKEN2(pipe);
  1721. val = I915_READ(reg);
  1722. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1723. I915_WRITE(reg, val);
  1724. }
  1725. reg = PCH_TRANSCONF(pipe);
  1726. val = I915_READ(reg);
  1727. pipeconf_val = I915_READ(PIPECONF(pipe));
  1728. if (HAS_PCH_IBX(dev_priv->dev)) {
  1729. /*
  1730. * make the BPC in transcoder be consistent with
  1731. * that in pipeconf reg.
  1732. */
  1733. val &= ~PIPECONF_BPC_MASK;
  1734. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1735. }
  1736. val &= ~TRANS_INTERLACE_MASK;
  1737. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1738. if (HAS_PCH_IBX(dev_priv->dev) &&
  1739. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1740. val |= TRANS_LEGACY_INTERLACED_ILK;
  1741. else
  1742. val |= TRANS_INTERLACED;
  1743. else
  1744. val |= TRANS_PROGRESSIVE;
  1745. I915_WRITE(reg, val | TRANS_ENABLE);
  1746. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1747. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1748. }
  1749. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1750. enum transcoder cpu_transcoder)
  1751. {
  1752. u32 val, pipeconf_val;
  1753. /* PCH only available on ILK+ */
  1754. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1755. /* FDI must be feeding us bits for PCH ports */
  1756. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1757. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1758. /* Workaround: set timing override bit. */
  1759. val = I915_READ(_TRANSA_CHICKEN2);
  1760. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1761. I915_WRITE(_TRANSA_CHICKEN2, val);
  1762. val = TRANS_ENABLE;
  1763. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1764. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1765. PIPECONF_INTERLACED_ILK)
  1766. val |= TRANS_INTERLACED;
  1767. else
  1768. val |= TRANS_PROGRESSIVE;
  1769. I915_WRITE(LPT_TRANSCONF, val);
  1770. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1771. DRM_ERROR("Failed to enable PCH transcoder\n");
  1772. }
  1773. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1774. enum pipe pipe)
  1775. {
  1776. struct drm_device *dev = dev_priv->dev;
  1777. uint32_t reg, val;
  1778. /* FDI relies on the transcoder */
  1779. assert_fdi_tx_disabled(dev_priv, pipe);
  1780. assert_fdi_rx_disabled(dev_priv, pipe);
  1781. /* Ports must be off as well */
  1782. assert_pch_ports_disabled(dev_priv, pipe);
  1783. reg = PCH_TRANSCONF(pipe);
  1784. val = I915_READ(reg);
  1785. val &= ~TRANS_ENABLE;
  1786. I915_WRITE(reg, val);
  1787. /* wait for PCH transcoder off, transcoder state */
  1788. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1789. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1790. if (!HAS_PCH_IBX(dev)) {
  1791. /* Workaround: Clear the timing override chicken bit again. */
  1792. reg = TRANS_CHICKEN2(pipe);
  1793. val = I915_READ(reg);
  1794. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1795. I915_WRITE(reg, val);
  1796. }
  1797. }
  1798. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1799. {
  1800. u32 val;
  1801. val = I915_READ(LPT_TRANSCONF);
  1802. val &= ~TRANS_ENABLE;
  1803. I915_WRITE(LPT_TRANSCONF, val);
  1804. /* wait for PCH transcoder off, transcoder state */
  1805. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1806. DRM_ERROR("Failed to disable PCH transcoder\n");
  1807. /* Workaround: clear timing override bit. */
  1808. val = I915_READ(_TRANSA_CHICKEN2);
  1809. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1810. I915_WRITE(_TRANSA_CHICKEN2, val);
  1811. }
  1812. /**
  1813. * intel_enable_pipe - enable a pipe, asserting requirements
  1814. * @crtc: crtc responsible for the pipe
  1815. *
  1816. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1817. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1818. */
  1819. static void intel_enable_pipe(struct intel_crtc *crtc)
  1820. {
  1821. struct drm_device *dev = crtc->base.dev;
  1822. struct drm_i915_private *dev_priv = dev->dev_private;
  1823. enum pipe pipe = crtc->pipe;
  1824. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1825. pipe);
  1826. enum pipe pch_transcoder;
  1827. int reg;
  1828. u32 val;
  1829. assert_planes_disabled(dev_priv, pipe);
  1830. assert_cursor_disabled(dev_priv, pipe);
  1831. assert_sprites_disabled(dev_priv, pipe);
  1832. if (HAS_PCH_LPT(dev_priv->dev))
  1833. pch_transcoder = TRANSCODER_A;
  1834. else
  1835. pch_transcoder = pipe;
  1836. /*
  1837. * A pipe without a PLL won't actually be able to drive bits from
  1838. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1839. * need the check.
  1840. */
  1841. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1842. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1843. assert_dsi_pll_enabled(dev_priv);
  1844. else
  1845. assert_pll_enabled(dev_priv, pipe);
  1846. else {
  1847. if (crtc->config->has_pch_encoder) {
  1848. /* if driving the PCH, we need FDI enabled */
  1849. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1850. assert_fdi_tx_pll_enabled(dev_priv,
  1851. (enum pipe) cpu_transcoder);
  1852. }
  1853. /* FIXME: assert CPU port conditions for SNB+ */
  1854. }
  1855. reg = PIPECONF(cpu_transcoder);
  1856. val = I915_READ(reg);
  1857. if (val & PIPECONF_ENABLE) {
  1858. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1859. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1860. return;
  1861. }
  1862. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1863. POSTING_READ(reg);
  1864. }
  1865. /**
  1866. * intel_disable_pipe - disable a pipe, asserting requirements
  1867. * @crtc: crtc whose pipes is to be disabled
  1868. *
  1869. * Disable the pipe of @crtc, making sure that various hardware
  1870. * specific requirements are met, if applicable, e.g. plane
  1871. * disabled, panel fitter off, etc.
  1872. *
  1873. * Will wait until the pipe has shut down before returning.
  1874. */
  1875. static void intel_disable_pipe(struct intel_crtc *crtc)
  1876. {
  1877. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1878. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1879. enum pipe pipe = crtc->pipe;
  1880. int reg;
  1881. u32 val;
  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. /*
  1908. * Plane regs are double buffered, going from enabled->disabled needs a
  1909. * trigger in order to latch. The display address reg provides this.
  1910. */
  1911. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1912. enum plane plane)
  1913. {
  1914. struct drm_device *dev = dev_priv->dev;
  1915. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1916. I915_WRITE(reg, I915_READ(reg));
  1917. POSTING_READ(reg);
  1918. }
  1919. /**
  1920. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1921. * @plane: plane to be enabled
  1922. * @crtc: crtc for the plane
  1923. *
  1924. * Enable @plane on @crtc, making sure that the pipe is running first.
  1925. */
  1926. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1927. struct drm_crtc *crtc)
  1928. {
  1929. struct drm_device *dev = plane->dev;
  1930. struct drm_i915_private *dev_priv = dev->dev_private;
  1931. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1932. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1933. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1934. if (intel_crtc->primary_enabled)
  1935. return;
  1936. intel_crtc->primary_enabled = true;
  1937. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1938. crtc->x, crtc->y);
  1939. /*
  1940. * BDW signals flip done immediately if the plane
  1941. * is disabled, even if the plane enable is already
  1942. * armed to occur at the next vblank :(
  1943. */
  1944. if (IS_BROADWELL(dev))
  1945. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1946. }
  1947. /**
  1948. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1949. * @plane: plane to be disabled
  1950. * @crtc: crtc for the plane
  1951. *
  1952. * Disable @plane on @crtc, making sure that the pipe is running first.
  1953. */
  1954. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1955. struct drm_crtc *crtc)
  1956. {
  1957. struct drm_device *dev = plane->dev;
  1958. struct drm_i915_private *dev_priv = dev->dev_private;
  1959. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1960. if (WARN_ON(!intel_crtc->active))
  1961. return;
  1962. if (!intel_crtc->primary_enabled)
  1963. return;
  1964. intel_crtc->primary_enabled = false;
  1965. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1966. crtc->x, crtc->y);
  1967. }
  1968. static bool need_vtd_wa(struct drm_device *dev)
  1969. {
  1970. #ifdef CONFIG_INTEL_IOMMU
  1971. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1972. return true;
  1973. #endif
  1974. return false;
  1975. }
  1976. unsigned int
  1977. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1978. uint64_t fb_format_modifier)
  1979. {
  1980. unsigned int tile_height;
  1981. uint32_t pixel_bytes;
  1982. switch (fb_format_modifier) {
  1983. case DRM_FORMAT_MOD_NONE:
  1984. tile_height = 1;
  1985. break;
  1986. case I915_FORMAT_MOD_X_TILED:
  1987. tile_height = IS_GEN2(dev) ? 16 : 8;
  1988. break;
  1989. case I915_FORMAT_MOD_Y_TILED:
  1990. tile_height = 32;
  1991. break;
  1992. case I915_FORMAT_MOD_Yf_TILED:
  1993. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1994. switch (pixel_bytes) {
  1995. default:
  1996. case 1:
  1997. tile_height = 64;
  1998. break;
  1999. case 2:
  2000. case 4:
  2001. tile_height = 32;
  2002. break;
  2003. case 8:
  2004. tile_height = 16;
  2005. break;
  2006. case 16:
  2007. WARN_ONCE(1,
  2008. "128-bit pixels are not supported for display!");
  2009. tile_height = 16;
  2010. break;
  2011. }
  2012. break;
  2013. default:
  2014. MISSING_CASE(fb_format_modifier);
  2015. tile_height = 1;
  2016. break;
  2017. }
  2018. return tile_height;
  2019. }
  2020. unsigned int
  2021. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  2022. uint32_t pixel_format, uint64_t fb_format_modifier)
  2023. {
  2024. return ALIGN(height, intel_tile_height(dev, pixel_format,
  2025. fb_format_modifier));
  2026. }
  2027. static int
  2028. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  2029. const struct drm_plane_state *plane_state)
  2030. {
  2031. struct intel_rotation_info *info = &view->rotation_info;
  2032. *view = i915_ggtt_view_normal;
  2033. if (!plane_state)
  2034. return 0;
  2035. if (!intel_rotation_90_or_270(plane_state->rotation))
  2036. return 0;
  2037. *view = i915_ggtt_view_rotated;
  2038. info->height = fb->height;
  2039. info->pixel_format = fb->pixel_format;
  2040. info->pitch = fb->pitches[0];
  2041. info->fb_modifier = fb->modifier[0];
  2042. return 0;
  2043. }
  2044. int
  2045. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2046. struct drm_framebuffer *fb,
  2047. const struct drm_plane_state *plane_state,
  2048. struct intel_engine_cs *pipelined)
  2049. {
  2050. struct drm_device *dev = fb->dev;
  2051. struct drm_i915_private *dev_priv = dev->dev_private;
  2052. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2053. struct i915_ggtt_view view;
  2054. u32 alignment;
  2055. int ret;
  2056. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2057. switch (fb->modifier[0]) {
  2058. case DRM_FORMAT_MOD_NONE:
  2059. if (INTEL_INFO(dev)->gen >= 9)
  2060. alignment = 256 * 1024;
  2061. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  2062. alignment = 128 * 1024;
  2063. else if (INTEL_INFO(dev)->gen >= 4)
  2064. alignment = 4 * 1024;
  2065. else
  2066. alignment = 64 * 1024;
  2067. break;
  2068. case I915_FORMAT_MOD_X_TILED:
  2069. if (INTEL_INFO(dev)->gen >= 9)
  2070. alignment = 256 * 1024;
  2071. else {
  2072. /* pin() will align the object as required by fence */
  2073. alignment = 0;
  2074. }
  2075. break;
  2076. case I915_FORMAT_MOD_Y_TILED:
  2077. case I915_FORMAT_MOD_Yf_TILED:
  2078. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2079. "Y tiling bo slipped through, driver bug!\n"))
  2080. return -EINVAL;
  2081. alignment = 1 * 1024 * 1024;
  2082. break;
  2083. default:
  2084. MISSING_CASE(fb->modifier[0]);
  2085. return -EINVAL;
  2086. }
  2087. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2088. if (ret)
  2089. return ret;
  2090. /* Note that the w/a also requires 64 PTE of padding following the
  2091. * bo. We currently fill all unused PTE with the shadow page and so
  2092. * we should always have valid PTE following the scanout preventing
  2093. * the VT-d warning.
  2094. */
  2095. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2096. alignment = 256 * 1024;
  2097. /*
  2098. * Global gtt pte registers are special registers which actually forward
  2099. * writes to a chunk of system memory. Which means that there is no risk
  2100. * that the register values disappear as soon as we call
  2101. * intel_runtime_pm_put(), so it is correct to wrap only the
  2102. * pin/unpin/fence and not more.
  2103. */
  2104. intel_runtime_pm_get(dev_priv);
  2105. dev_priv->mm.interruptible = false;
  2106. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2107. &view);
  2108. if (ret)
  2109. goto err_interruptible;
  2110. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2111. * fence, whereas 965+ only requires a fence if using
  2112. * framebuffer compression. For simplicity, we always install
  2113. * a fence as the cost is not that onerous.
  2114. */
  2115. ret = i915_gem_object_get_fence(obj);
  2116. if (ret)
  2117. goto err_unpin;
  2118. i915_gem_object_pin_fence(obj);
  2119. dev_priv->mm.interruptible = true;
  2120. intel_runtime_pm_put(dev_priv);
  2121. return 0;
  2122. err_unpin:
  2123. i915_gem_object_unpin_from_display_plane(obj, &view);
  2124. err_interruptible:
  2125. dev_priv->mm.interruptible = true;
  2126. intel_runtime_pm_put(dev_priv);
  2127. return ret;
  2128. }
  2129. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2130. const struct drm_plane_state *plane_state)
  2131. {
  2132. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2133. struct i915_ggtt_view view;
  2134. int ret;
  2135. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2136. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2137. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2138. i915_gem_object_unpin_fence(obj);
  2139. i915_gem_object_unpin_from_display_plane(obj, &view);
  2140. }
  2141. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2142. * is assumed to be a power-of-two. */
  2143. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2144. unsigned int tiling_mode,
  2145. unsigned int cpp,
  2146. unsigned int pitch)
  2147. {
  2148. if (tiling_mode != I915_TILING_NONE) {
  2149. unsigned int tile_rows, tiles;
  2150. tile_rows = *y / 8;
  2151. *y %= 8;
  2152. tiles = *x / (512/cpp);
  2153. *x %= 512/cpp;
  2154. return tile_rows * pitch * 8 + tiles * 4096;
  2155. } else {
  2156. unsigned int offset;
  2157. offset = *y * pitch + *x * cpp;
  2158. *y = 0;
  2159. *x = (offset & 4095) / cpp;
  2160. return offset & -4096;
  2161. }
  2162. }
  2163. static int i9xx_format_to_fourcc(int format)
  2164. {
  2165. switch (format) {
  2166. case DISPPLANE_8BPP:
  2167. return DRM_FORMAT_C8;
  2168. case DISPPLANE_BGRX555:
  2169. return DRM_FORMAT_XRGB1555;
  2170. case DISPPLANE_BGRX565:
  2171. return DRM_FORMAT_RGB565;
  2172. default:
  2173. case DISPPLANE_BGRX888:
  2174. return DRM_FORMAT_XRGB8888;
  2175. case DISPPLANE_RGBX888:
  2176. return DRM_FORMAT_XBGR8888;
  2177. case DISPPLANE_BGRX101010:
  2178. return DRM_FORMAT_XRGB2101010;
  2179. case DISPPLANE_RGBX101010:
  2180. return DRM_FORMAT_XBGR2101010;
  2181. }
  2182. }
  2183. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2184. {
  2185. switch (format) {
  2186. case PLANE_CTL_FORMAT_RGB_565:
  2187. return DRM_FORMAT_RGB565;
  2188. default:
  2189. case PLANE_CTL_FORMAT_XRGB_8888:
  2190. if (rgb_order) {
  2191. if (alpha)
  2192. return DRM_FORMAT_ABGR8888;
  2193. else
  2194. return DRM_FORMAT_XBGR8888;
  2195. } else {
  2196. if (alpha)
  2197. return DRM_FORMAT_ARGB8888;
  2198. else
  2199. return DRM_FORMAT_XRGB8888;
  2200. }
  2201. case PLANE_CTL_FORMAT_XRGB_2101010:
  2202. if (rgb_order)
  2203. return DRM_FORMAT_XBGR2101010;
  2204. else
  2205. return DRM_FORMAT_XRGB2101010;
  2206. }
  2207. }
  2208. static bool
  2209. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2210. struct intel_initial_plane_config *plane_config)
  2211. {
  2212. struct drm_device *dev = crtc->base.dev;
  2213. struct drm_i915_gem_object *obj = NULL;
  2214. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2215. struct drm_framebuffer *fb = &plane_config->fb->base;
  2216. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2217. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2218. PAGE_SIZE);
  2219. size_aligned -= base_aligned;
  2220. if (plane_config->size == 0)
  2221. return false;
  2222. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2223. base_aligned,
  2224. base_aligned,
  2225. size_aligned);
  2226. if (!obj)
  2227. return false;
  2228. obj->tiling_mode = plane_config->tiling;
  2229. if (obj->tiling_mode == I915_TILING_X)
  2230. obj->stride = fb->pitches[0];
  2231. mode_cmd.pixel_format = fb->pixel_format;
  2232. mode_cmd.width = fb->width;
  2233. mode_cmd.height = fb->height;
  2234. mode_cmd.pitches[0] = fb->pitches[0];
  2235. mode_cmd.modifier[0] = fb->modifier[0];
  2236. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2237. mutex_lock(&dev->struct_mutex);
  2238. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2239. &mode_cmd, obj)) {
  2240. DRM_DEBUG_KMS("intel fb init failed\n");
  2241. goto out_unref_obj;
  2242. }
  2243. mutex_unlock(&dev->struct_mutex);
  2244. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2245. return true;
  2246. out_unref_obj:
  2247. drm_gem_object_unreference(&obj->base);
  2248. mutex_unlock(&dev->struct_mutex);
  2249. return false;
  2250. }
  2251. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2252. static void
  2253. update_state_fb(struct drm_plane *plane)
  2254. {
  2255. if (plane->fb == plane->state->fb)
  2256. return;
  2257. if (plane->state->fb)
  2258. drm_framebuffer_unreference(plane->state->fb);
  2259. plane->state->fb = plane->fb;
  2260. if (plane->state->fb)
  2261. drm_framebuffer_reference(plane->state->fb);
  2262. }
  2263. static void
  2264. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2265. struct intel_initial_plane_config *plane_config)
  2266. {
  2267. struct drm_device *dev = intel_crtc->base.dev;
  2268. struct drm_i915_private *dev_priv = dev->dev_private;
  2269. struct drm_crtc *c;
  2270. struct intel_crtc *i;
  2271. struct drm_i915_gem_object *obj;
  2272. struct drm_plane *primary = intel_crtc->base.primary;
  2273. struct drm_framebuffer *fb;
  2274. if (!plane_config->fb)
  2275. return;
  2276. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2277. fb = &plane_config->fb->base;
  2278. goto valid_fb;
  2279. }
  2280. kfree(plane_config->fb);
  2281. /*
  2282. * Failed to alloc the obj, check to see if we should share
  2283. * an fb with another CRTC instead
  2284. */
  2285. for_each_crtc(dev, c) {
  2286. i = to_intel_crtc(c);
  2287. if (c == &intel_crtc->base)
  2288. continue;
  2289. if (!i->active)
  2290. continue;
  2291. fb = c->primary->fb;
  2292. if (!fb)
  2293. continue;
  2294. obj = intel_fb_obj(fb);
  2295. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2296. drm_framebuffer_reference(fb);
  2297. goto valid_fb;
  2298. }
  2299. }
  2300. return;
  2301. valid_fb:
  2302. obj = intel_fb_obj(fb);
  2303. if (obj->tiling_mode != I915_TILING_NONE)
  2304. dev_priv->preserve_bios_swizzle = true;
  2305. primary->fb = fb;
  2306. primary->state->crtc = &intel_crtc->base;
  2307. primary->crtc = &intel_crtc->base;
  2308. update_state_fb(primary);
  2309. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2310. }
  2311. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2312. struct drm_framebuffer *fb,
  2313. int x, int y)
  2314. {
  2315. struct drm_device *dev = crtc->dev;
  2316. struct drm_i915_private *dev_priv = dev->dev_private;
  2317. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2318. struct drm_i915_gem_object *obj;
  2319. int plane = intel_crtc->plane;
  2320. unsigned long linear_offset;
  2321. u32 dspcntr;
  2322. u32 reg = DSPCNTR(plane);
  2323. int pixel_size;
  2324. if (!intel_crtc->primary_enabled) {
  2325. I915_WRITE(reg, 0);
  2326. if (INTEL_INFO(dev)->gen >= 4)
  2327. I915_WRITE(DSPSURF(plane), 0);
  2328. else
  2329. I915_WRITE(DSPADDR(plane), 0);
  2330. POSTING_READ(reg);
  2331. return;
  2332. }
  2333. obj = intel_fb_obj(fb);
  2334. if (WARN_ON(obj == NULL))
  2335. return;
  2336. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2337. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2338. dspcntr |= DISPLAY_PLANE_ENABLE;
  2339. if (INTEL_INFO(dev)->gen < 4) {
  2340. if (intel_crtc->pipe == PIPE_B)
  2341. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2342. /* pipesrc and dspsize control the size that is scaled from,
  2343. * which should always be the user's requested size.
  2344. */
  2345. I915_WRITE(DSPSIZE(plane),
  2346. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2347. (intel_crtc->config->pipe_src_w - 1));
  2348. I915_WRITE(DSPPOS(plane), 0);
  2349. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2350. I915_WRITE(PRIMSIZE(plane),
  2351. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2352. (intel_crtc->config->pipe_src_w - 1));
  2353. I915_WRITE(PRIMPOS(plane), 0);
  2354. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2355. }
  2356. switch (fb->pixel_format) {
  2357. case DRM_FORMAT_C8:
  2358. dspcntr |= DISPPLANE_8BPP;
  2359. break;
  2360. case DRM_FORMAT_XRGB1555:
  2361. case DRM_FORMAT_ARGB1555:
  2362. dspcntr |= DISPPLANE_BGRX555;
  2363. break;
  2364. case DRM_FORMAT_RGB565:
  2365. dspcntr |= DISPPLANE_BGRX565;
  2366. break;
  2367. case DRM_FORMAT_XRGB8888:
  2368. case DRM_FORMAT_ARGB8888:
  2369. dspcntr |= DISPPLANE_BGRX888;
  2370. break;
  2371. case DRM_FORMAT_XBGR8888:
  2372. case DRM_FORMAT_ABGR8888:
  2373. dspcntr |= DISPPLANE_RGBX888;
  2374. break;
  2375. case DRM_FORMAT_XRGB2101010:
  2376. case DRM_FORMAT_ARGB2101010:
  2377. dspcntr |= DISPPLANE_BGRX101010;
  2378. break;
  2379. case DRM_FORMAT_XBGR2101010:
  2380. case DRM_FORMAT_ABGR2101010:
  2381. dspcntr |= DISPPLANE_RGBX101010;
  2382. break;
  2383. default:
  2384. BUG();
  2385. }
  2386. if (INTEL_INFO(dev)->gen >= 4 &&
  2387. obj->tiling_mode != I915_TILING_NONE)
  2388. dspcntr |= DISPPLANE_TILED;
  2389. if (IS_G4X(dev))
  2390. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2391. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2392. if (INTEL_INFO(dev)->gen >= 4) {
  2393. intel_crtc->dspaddr_offset =
  2394. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2395. pixel_size,
  2396. fb->pitches[0]);
  2397. linear_offset -= intel_crtc->dspaddr_offset;
  2398. } else {
  2399. intel_crtc->dspaddr_offset = linear_offset;
  2400. }
  2401. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2402. dspcntr |= DISPPLANE_ROTATE_180;
  2403. x += (intel_crtc->config->pipe_src_w - 1);
  2404. y += (intel_crtc->config->pipe_src_h - 1);
  2405. /* Finding the last pixel of the last line of the display
  2406. data and adding to linear_offset*/
  2407. linear_offset +=
  2408. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2409. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2410. }
  2411. I915_WRITE(reg, dspcntr);
  2412. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2413. if (INTEL_INFO(dev)->gen >= 4) {
  2414. I915_WRITE(DSPSURF(plane),
  2415. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2416. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2417. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2418. } else
  2419. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2420. POSTING_READ(reg);
  2421. }
  2422. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2423. struct drm_framebuffer *fb,
  2424. int x, int y)
  2425. {
  2426. struct drm_device *dev = crtc->dev;
  2427. struct drm_i915_private *dev_priv = dev->dev_private;
  2428. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2429. struct drm_i915_gem_object *obj;
  2430. int plane = intel_crtc->plane;
  2431. unsigned long linear_offset;
  2432. u32 dspcntr;
  2433. u32 reg = DSPCNTR(plane);
  2434. int pixel_size;
  2435. if (!intel_crtc->primary_enabled) {
  2436. I915_WRITE(reg, 0);
  2437. I915_WRITE(DSPSURF(plane), 0);
  2438. POSTING_READ(reg);
  2439. return;
  2440. }
  2441. obj = intel_fb_obj(fb);
  2442. if (WARN_ON(obj == NULL))
  2443. return;
  2444. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2445. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2446. dspcntr |= DISPLAY_PLANE_ENABLE;
  2447. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2448. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2449. switch (fb->pixel_format) {
  2450. case DRM_FORMAT_C8:
  2451. dspcntr |= DISPPLANE_8BPP;
  2452. break;
  2453. case DRM_FORMAT_RGB565:
  2454. dspcntr |= DISPPLANE_BGRX565;
  2455. break;
  2456. case DRM_FORMAT_XRGB8888:
  2457. case DRM_FORMAT_ARGB8888:
  2458. dspcntr |= DISPPLANE_BGRX888;
  2459. break;
  2460. case DRM_FORMAT_XBGR8888:
  2461. case DRM_FORMAT_ABGR8888:
  2462. dspcntr |= DISPPLANE_RGBX888;
  2463. break;
  2464. case DRM_FORMAT_XRGB2101010:
  2465. case DRM_FORMAT_ARGB2101010:
  2466. dspcntr |= DISPPLANE_BGRX101010;
  2467. break;
  2468. case DRM_FORMAT_XBGR2101010:
  2469. case DRM_FORMAT_ABGR2101010:
  2470. dspcntr |= DISPPLANE_RGBX101010;
  2471. break;
  2472. default:
  2473. BUG();
  2474. }
  2475. if (obj->tiling_mode != I915_TILING_NONE)
  2476. dspcntr |= DISPPLANE_TILED;
  2477. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2478. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2479. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2480. intel_crtc->dspaddr_offset =
  2481. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2482. pixel_size,
  2483. fb->pitches[0]);
  2484. linear_offset -= intel_crtc->dspaddr_offset;
  2485. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2486. dspcntr |= DISPPLANE_ROTATE_180;
  2487. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2488. x += (intel_crtc->config->pipe_src_w - 1);
  2489. y += (intel_crtc->config->pipe_src_h - 1);
  2490. /* Finding the last pixel of the last line of the display
  2491. data and adding to linear_offset*/
  2492. linear_offset +=
  2493. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2494. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2495. }
  2496. }
  2497. I915_WRITE(reg, dspcntr);
  2498. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2499. I915_WRITE(DSPSURF(plane),
  2500. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2501. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2502. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2503. } else {
  2504. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2505. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2506. }
  2507. POSTING_READ(reg);
  2508. }
  2509. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2510. uint32_t pixel_format)
  2511. {
  2512. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2513. /*
  2514. * The stride is either expressed as a multiple of 64 bytes
  2515. * chunks for linear buffers or in number of tiles for tiled
  2516. * buffers.
  2517. */
  2518. switch (fb_modifier) {
  2519. case DRM_FORMAT_MOD_NONE:
  2520. return 64;
  2521. case I915_FORMAT_MOD_X_TILED:
  2522. if (INTEL_INFO(dev)->gen == 2)
  2523. return 128;
  2524. return 512;
  2525. case I915_FORMAT_MOD_Y_TILED:
  2526. /* No need to check for old gens and Y tiling since this is
  2527. * about the display engine and those will be blocked before
  2528. * we get here.
  2529. */
  2530. return 128;
  2531. case I915_FORMAT_MOD_Yf_TILED:
  2532. if (bits_per_pixel == 8)
  2533. return 64;
  2534. else
  2535. return 128;
  2536. default:
  2537. MISSING_CASE(fb_modifier);
  2538. return 64;
  2539. }
  2540. }
  2541. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2542. struct drm_i915_gem_object *obj)
  2543. {
  2544. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2545. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2546. view = &i915_ggtt_view_rotated;
  2547. return i915_gem_obj_ggtt_offset_view(obj, view);
  2548. }
  2549. /*
  2550. * This function detaches (aka. unbinds) unused scalers in hardware
  2551. */
  2552. void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2553. {
  2554. struct drm_device *dev;
  2555. struct drm_i915_private *dev_priv;
  2556. struct intel_crtc_scaler_state *scaler_state;
  2557. int i;
  2558. if (!intel_crtc || !intel_crtc->config)
  2559. return;
  2560. dev = intel_crtc->base.dev;
  2561. dev_priv = dev->dev_private;
  2562. scaler_state = &intel_crtc->config->scaler_state;
  2563. /* loop through and disable scalers that aren't in use */
  2564. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2565. if (!scaler_state->scalers[i].in_use) {
  2566. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2567. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2568. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2569. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2570. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2571. }
  2572. }
  2573. }
  2574. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2575. {
  2576. u32 plane_ctl_format = 0;
  2577. switch (pixel_format) {
  2578. case DRM_FORMAT_RGB565:
  2579. plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
  2580. break;
  2581. case DRM_FORMAT_XBGR8888:
  2582. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2583. break;
  2584. case DRM_FORMAT_XRGB8888:
  2585. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
  2586. break;
  2587. /*
  2588. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2589. * to be already pre-multiplied. We need to add a knob (or a different
  2590. * DRM_FORMAT) for user-space to configure that.
  2591. */
  2592. case DRM_FORMAT_ABGR8888:
  2593. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2594. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2595. break;
  2596. case DRM_FORMAT_ARGB8888:
  2597. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
  2598. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2599. break;
  2600. case DRM_FORMAT_XRGB2101010:
  2601. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
  2602. break;
  2603. case DRM_FORMAT_XBGR2101010:
  2604. plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2605. break;
  2606. case DRM_FORMAT_YUYV:
  2607. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2608. break;
  2609. case DRM_FORMAT_YVYU:
  2610. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2611. break;
  2612. case DRM_FORMAT_UYVY:
  2613. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2614. break;
  2615. case DRM_FORMAT_VYUY:
  2616. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2617. break;
  2618. default:
  2619. BUG();
  2620. }
  2621. return plane_ctl_format;
  2622. }
  2623. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2624. {
  2625. u32 plane_ctl_tiling = 0;
  2626. switch (fb_modifier) {
  2627. case DRM_FORMAT_MOD_NONE:
  2628. break;
  2629. case I915_FORMAT_MOD_X_TILED:
  2630. plane_ctl_tiling = PLANE_CTL_TILED_X;
  2631. break;
  2632. case I915_FORMAT_MOD_Y_TILED:
  2633. plane_ctl_tiling = PLANE_CTL_TILED_Y;
  2634. break;
  2635. case I915_FORMAT_MOD_Yf_TILED:
  2636. plane_ctl_tiling = PLANE_CTL_TILED_YF;
  2637. break;
  2638. default:
  2639. MISSING_CASE(fb_modifier);
  2640. }
  2641. return plane_ctl_tiling;
  2642. }
  2643. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2644. {
  2645. u32 plane_ctl_rotation = 0;
  2646. switch (rotation) {
  2647. case BIT(DRM_ROTATE_0):
  2648. break;
  2649. case BIT(DRM_ROTATE_90):
  2650. plane_ctl_rotation = PLANE_CTL_ROTATE_90;
  2651. break;
  2652. case BIT(DRM_ROTATE_180):
  2653. plane_ctl_rotation = PLANE_CTL_ROTATE_180;
  2654. break;
  2655. case BIT(DRM_ROTATE_270):
  2656. plane_ctl_rotation = PLANE_CTL_ROTATE_270;
  2657. break;
  2658. default:
  2659. MISSING_CASE(rotation);
  2660. }
  2661. return plane_ctl_rotation;
  2662. }
  2663. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2664. struct drm_framebuffer *fb,
  2665. int x, int y)
  2666. {
  2667. struct drm_device *dev = crtc->dev;
  2668. struct drm_i915_private *dev_priv = dev->dev_private;
  2669. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2670. struct drm_i915_gem_object *obj;
  2671. int pipe = intel_crtc->pipe;
  2672. u32 plane_ctl, stride_div, stride;
  2673. u32 tile_height, plane_offset, plane_size;
  2674. unsigned int rotation;
  2675. int x_offset, y_offset;
  2676. unsigned long surf_addr;
  2677. struct drm_plane *plane;
  2678. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2679. struct intel_plane_state *plane_state;
  2680. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2681. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2682. int scaler_id = -1;
  2683. plane = crtc->primary;
  2684. plane_state = to_intel_plane_state(plane->state);
  2685. if (!intel_crtc->primary_enabled) {
  2686. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2687. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2688. POSTING_READ(PLANE_CTL(pipe, 0));
  2689. return;
  2690. }
  2691. plane_ctl = PLANE_CTL_ENABLE |
  2692. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2693. PLANE_CTL_PIPE_CSC_ENABLE;
  2694. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2695. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2696. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2697. rotation = plane->state->rotation;
  2698. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2699. obj = intel_fb_obj(fb);
  2700. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2701. fb->pixel_format);
  2702. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2703. /*
  2704. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2705. * update_plane helpers are called from legacy paths.
  2706. * Once full atomic crtc is available, below check can be avoided.
  2707. */
  2708. if (drm_rect_width(&plane_state->src)) {
  2709. scaler_id = plane_state->scaler_id;
  2710. src_x = plane_state->src.x1 >> 16;
  2711. src_y = plane_state->src.y1 >> 16;
  2712. src_w = drm_rect_width(&plane_state->src) >> 16;
  2713. src_h = drm_rect_height(&plane_state->src) >> 16;
  2714. dst_x = plane_state->dst.x1;
  2715. dst_y = plane_state->dst.y1;
  2716. dst_w = drm_rect_width(&plane_state->dst);
  2717. dst_h = drm_rect_height(&plane_state->dst);
  2718. WARN_ON(x != src_x || y != src_y);
  2719. } else {
  2720. src_w = intel_crtc->config->pipe_src_w;
  2721. src_h = intel_crtc->config->pipe_src_h;
  2722. }
  2723. if (intel_rotation_90_or_270(rotation)) {
  2724. /* stride = Surface height in tiles */
  2725. tile_height = intel_tile_height(dev, fb->bits_per_pixel,
  2726. fb->modifier[0]);
  2727. stride = DIV_ROUND_UP(fb->height, tile_height);
  2728. x_offset = stride * tile_height - y - src_h;
  2729. y_offset = x;
  2730. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2731. } else {
  2732. stride = fb->pitches[0] / stride_div;
  2733. x_offset = x;
  2734. y_offset = y;
  2735. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2736. }
  2737. plane_offset = y_offset << 16 | x_offset;
  2738. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2739. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2740. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2741. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2742. if (scaler_id >= 0) {
  2743. uint32_t ps_ctrl = 0;
  2744. WARN_ON(!dst_w || !dst_h);
  2745. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2746. crtc_state->scaler_state.scalers[scaler_id].mode;
  2747. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2748. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2749. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2750. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2751. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2752. } else {
  2753. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2754. }
  2755. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2756. POSTING_READ(PLANE_SURF(pipe, 0));
  2757. }
  2758. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2759. static int
  2760. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2761. int x, int y, enum mode_set_atomic state)
  2762. {
  2763. struct drm_device *dev = crtc->dev;
  2764. struct drm_i915_private *dev_priv = dev->dev_private;
  2765. if (dev_priv->display.disable_fbc)
  2766. dev_priv->display.disable_fbc(dev);
  2767. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2768. return 0;
  2769. }
  2770. static void intel_complete_page_flips(struct drm_device *dev)
  2771. {
  2772. struct drm_crtc *crtc;
  2773. for_each_crtc(dev, crtc) {
  2774. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2775. enum plane plane = intel_crtc->plane;
  2776. intel_prepare_page_flip(dev, plane);
  2777. intel_finish_page_flip_plane(dev, plane);
  2778. }
  2779. }
  2780. static void intel_update_primary_planes(struct drm_device *dev)
  2781. {
  2782. struct drm_i915_private *dev_priv = dev->dev_private;
  2783. struct drm_crtc *crtc;
  2784. for_each_crtc(dev, crtc) {
  2785. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2786. drm_modeset_lock(&crtc->mutex, NULL);
  2787. /*
  2788. * FIXME: Once we have proper support for primary planes (and
  2789. * disabling them without disabling the entire crtc) allow again
  2790. * a NULL crtc->primary->fb.
  2791. */
  2792. if (intel_crtc->active && crtc->primary->fb)
  2793. dev_priv->display.update_primary_plane(crtc,
  2794. crtc->primary->fb,
  2795. crtc->x,
  2796. crtc->y);
  2797. drm_modeset_unlock(&crtc->mutex);
  2798. }
  2799. }
  2800. void intel_prepare_reset(struct drm_device *dev)
  2801. {
  2802. struct drm_i915_private *dev_priv = to_i915(dev);
  2803. struct intel_crtc *crtc;
  2804. /* no reset support for gen2 */
  2805. if (IS_GEN2(dev))
  2806. return;
  2807. /* reset doesn't touch the display */
  2808. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2809. return;
  2810. drm_modeset_lock_all(dev);
  2811. /*
  2812. * Disabling the crtcs gracefully seems nicer. Also the
  2813. * g33 docs say we should at least disable all the planes.
  2814. */
  2815. for_each_intel_crtc(dev, crtc) {
  2816. if (crtc->active)
  2817. dev_priv->display.crtc_disable(&crtc->base);
  2818. }
  2819. }
  2820. void intel_finish_reset(struct drm_device *dev)
  2821. {
  2822. struct drm_i915_private *dev_priv = to_i915(dev);
  2823. /*
  2824. * Flips in the rings will be nuked by the reset,
  2825. * so complete all pending flips so that user space
  2826. * will get its events and not get stuck.
  2827. */
  2828. intel_complete_page_flips(dev);
  2829. /* no reset support for gen2 */
  2830. if (IS_GEN2(dev))
  2831. return;
  2832. /* reset doesn't touch the display */
  2833. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2834. /*
  2835. * Flips in the rings have been nuked by the reset,
  2836. * so update the base address of all primary
  2837. * planes to the the last fb to make sure we're
  2838. * showing the correct fb after a reset.
  2839. */
  2840. intel_update_primary_planes(dev);
  2841. return;
  2842. }
  2843. /*
  2844. * The display has been reset as well,
  2845. * so need a full re-initialization.
  2846. */
  2847. intel_runtime_pm_disable_interrupts(dev_priv);
  2848. intel_runtime_pm_enable_interrupts(dev_priv);
  2849. intel_modeset_init_hw(dev);
  2850. spin_lock_irq(&dev_priv->irq_lock);
  2851. if (dev_priv->display.hpd_irq_setup)
  2852. dev_priv->display.hpd_irq_setup(dev);
  2853. spin_unlock_irq(&dev_priv->irq_lock);
  2854. intel_modeset_setup_hw_state(dev, true);
  2855. intel_hpd_init(dev_priv);
  2856. drm_modeset_unlock_all(dev);
  2857. }
  2858. static int
  2859. intel_finish_fb(struct drm_framebuffer *old_fb)
  2860. {
  2861. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2862. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2863. bool was_interruptible = dev_priv->mm.interruptible;
  2864. int ret;
  2865. /* Big Hammer, we also need to ensure that any pending
  2866. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2867. * current scanout is retired before unpinning the old
  2868. * framebuffer.
  2869. *
  2870. * This should only fail upon a hung GPU, in which case we
  2871. * can safely continue.
  2872. */
  2873. dev_priv->mm.interruptible = false;
  2874. ret = i915_gem_object_finish_gpu(obj);
  2875. dev_priv->mm.interruptible = was_interruptible;
  2876. return ret;
  2877. }
  2878. static bool intel_crtc_has_pending_flip(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. bool pending;
  2884. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2885. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2886. return false;
  2887. spin_lock_irq(&dev->event_lock);
  2888. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2889. spin_unlock_irq(&dev->event_lock);
  2890. return pending;
  2891. }
  2892. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2893. {
  2894. struct drm_device *dev = crtc->base.dev;
  2895. struct drm_i915_private *dev_priv = dev->dev_private;
  2896. const struct drm_display_mode *adjusted_mode;
  2897. if (!i915.fastboot)
  2898. return;
  2899. /*
  2900. * Update pipe size and adjust fitter if needed: the reason for this is
  2901. * that in compute_mode_changes we check the native mode (not the pfit
  2902. * mode) to see if we can flip rather than do a full mode set. In the
  2903. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2904. * pfit state, we'll end up with a big fb scanned out into the wrong
  2905. * sized surface.
  2906. *
  2907. * To fix this properly, we need to hoist the checks up into
  2908. * compute_mode_changes (or above), check the actual pfit state and
  2909. * whether the platform allows pfit disable with pipe active, and only
  2910. * then update the pipesrc and pfit state, even on the flip path.
  2911. */
  2912. adjusted_mode = &crtc->config->base.adjusted_mode;
  2913. I915_WRITE(PIPESRC(crtc->pipe),
  2914. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2915. (adjusted_mode->crtc_vdisplay - 1));
  2916. if (!crtc->config->pch_pfit.enabled &&
  2917. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2918. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2919. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2920. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2921. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2922. }
  2923. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2924. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2925. }
  2926. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2927. {
  2928. struct drm_device *dev = crtc->dev;
  2929. struct drm_i915_private *dev_priv = dev->dev_private;
  2930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2931. int pipe = intel_crtc->pipe;
  2932. u32 reg, temp;
  2933. /* enable normal train */
  2934. reg = FDI_TX_CTL(pipe);
  2935. temp = I915_READ(reg);
  2936. if (IS_IVYBRIDGE(dev)) {
  2937. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2938. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2939. } else {
  2940. temp &= ~FDI_LINK_TRAIN_NONE;
  2941. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2942. }
  2943. I915_WRITE(reg, temp);
  2944. reg = FDI_RX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. if (HAS_PCH_CPT(dev)) {
  2947. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2948. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2949. } else {
  2950. temp &= ~FDI_LINK_TRAIN_NONE;
  2951. temp |= FDI_LINK_TRAIN_NONE;
  2952. }
  2953. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2954. /* wait one idle pattern time */
  2955. POSTING_READ(reg);
  2956. udelay(1000);
  2957. /* IVB wants error correction enabled */
  2958. if (IS_IVYBRIDGE(dev))
  2959. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2960. FDI_FE_ERRC_ENABLE);
  2961. }
  2962. /* The FDI link training functions for ILK/Ibexpeak. */
  2963. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2964. {
  2965. struct drm_device *dev = crtc->dev;
  2966. struct drm_i915_private *dev_priv = dev->dev_private;
  2967. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2968. int pipe = intel_crtc->pipe;
  2969. u32 reg, temp, tries;
  2970. /* FDI needs bits from pipe first */
  2971. assert_pipe_enabled(dev_priv, pipe);
  2972. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2973. for train result */
  2974. reg = FDI_RX_IMR(pipe);
  2975. temp = I915_READ(reg);
  2976. temp &= ~FDI_RX_SYMBOL_LOCK;
  2977. temp &= ~FDI_RX_BIT_LOCK;
  2978. I915_WRITE(reg, temp);
  2979. I915_READ(reg);
  2980. udelay(150);
  2981. /* enable CPU FDI TX and PCH FDI RX */
  2982. reg = FDI_TX_CTL(pipe);
  2983. temp = I915_READ(reg);
  2984. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2985. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2986. temp &= ~FDI_LINK_TRAIN_NONE;
  2987. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2988. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2989. reg = FDI_RX_CTL(pipe);
  2990. temp = I915_READ(reg);
  2991. temp &= ~FDI_LINK_TRAIN_NONE;
  2992. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2993. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2994. POSTING_READ(reg);
  2995. udelay(150);
  2996. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2997. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2998. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2999. FDI_RX_PHASE_SYNC_POINTER_EN);
  3000. reg = FDI_RX_IIR(pipe);
  3001. for (tries = 0; tries < 5; tries++) {
  3002. temp = I915_READ(reg);
  3003. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3004. if ((temp & FDI_RX_BIT_LOCK)) {
  3005. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3006. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3007. break;
  3008. }
  3009. }
  3010. if (tries == 5)
  3011. DRM_ERROR("FDI train 1 fail!\n");
  3012. /* Train 2 */
  3013. reg = FDI_TX_CTL(pipe);
  3014. temp = I915_READ(reg);
  3015. temp &= ~FDI_LINK_TRAIN_NONE;
  3016. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3017. I915_WRITE(reg, temp);
  3018. reg = FDI_RX_CTL(pipe);
  3019. temp = I915_READ(reg);
  3020. temp &= ~FDI_LINK_TRAIN_NONE;
  3021. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3022. I915_WRITE(reg, temp);
  3023. POSTING_READ(reg);
  3024. udelay(150);
  3025. reg = FDI_RX_IIR(pipe);
  3026. for (tries = 0; tries < 5; tries++) {
  3027. temp = I915_READ(reg);
  3028. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3029. if (temp & FDI_RX_SYMBOL_LOCK) {
  3030. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3031. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3032. break;
  3033. }
  3034. }
  3035. if (tries == 5)
  3036. DRM_ERROR("FDI train 2 fail!\n");
  3037. DRM_DEBUG_KMS("FDI train done\n");
  3038. }
  3039. static const int snb_b_fdi_train_param[] = {
  3040. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3041. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3042. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3043. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3044. };
  3045. /* The FDI link training functions for SNB/Cougarpoint. */
  3046. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3047. {
  3048. struct drm_device *dev = crtc->dev;
  3049. struct drm_i915_private *dev_priv = dev->dev_private;
  3050. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3051. int pipe = intel_crtc->pipe;
  3052. u32 reg, temp, i, retry;
  3053. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3054. for train result */
  3055. reg = FDI_RX_IMR(pipe);
  3056. temp = I915_READ(reg);
  3057. temp &= ~FDI_RX_SYMBOL_LOCK;
  3058. temp &= ~FDI_RX_BIT_LOCK;
  3059. I915_WRITE(reg, temp);
  3060. POSTING_READ(reg);
  3061. udelay(150);
  3062. /* enable CPU FDI TX and PCH FDI RX */
  3063. reg = FDI_TX_CTL(pipe);
  3064. temp = I915_READ(reg);
  3065. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3066. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3067. temp &= ~FDI_LINK_TRAIN_NONE;
  3068. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3069. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3070. /* SNB-B */
  3071. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3072. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3073. I915_WRITE(FDI_RX_MISC(pipe),
  3074. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3075. reg = FDI_RX_CTL(pipe);
  3076. temp = I915_READ(reg);
  3077. if (HAS_PCH_CPT(dev)) {
  3078. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3079. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3080. } else {
  3081. temp &= ~FDI_LINK_TRAIN_NONE;
  3082. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3083. }
  3084. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3085. POSTING_READ(reg);
  3086. udelay(150);
  3087. for (i = 0; i < 4; i++) {
  3088. reg = FDI_TX_CTL(pipe);
  3089. temp = I915_READ(reg);
  3090. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3091. temp |= snb_b_fdi_train_param[i];
  3092. I915_WRITE(reg, temp);
  3093. POSTING_READ(reg);
  3094. udelay(500);
  3095. for (retry = 0; retry < 5; retry++) {
  3096. reg = FDI_RX_IIR(pipe);
  3097. temp = I915_READ(reg);
  3098. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3099. if (temp & FDI_RX_BIT_LOCK) {
  3100. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3101. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3102. break;
  3103. }
  3104. udelay(50);
  3105. }
  3106. if (retry < 5)
  3107. break;
  3108. }
  3109. if (i == 4)
  3110. DRM_ERROR("FDI train 1 fail!\n");
  3111. /* Train 2 */
  3112. reg = FDI_TX_CTL(pipe);
  3113. temp = I915_READ(reg);
  3114. temp &= ~FDI_LINK_TRAIN_NONE;
  3115. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3116. if (IS_GEN6(dev)) {
  3117. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3118. /* SNB-B */
  3119. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3120. }
  3121. I915_WRITE(reg, temp);
  3122. reg = FDI_RX_CTL(pipe);
  3123. temp = I915_READ(reg);
  3124. if (HAS_PCH_CPT(dev)) {
  3125. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3126. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3127. } else {
  3128. temp &= ~FDI_LINK_TRAIN_NONE;
  3129. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3130. }
  3131. I915_WRITE(reg, temp);
  3132. POSTING_READ(reg);
  3133. udelay(150);
  3134. for (i = 0; i < 4; i++) {
  3135. reg = FDI_TX_CTL(pipe);
  3136. temp = I915_READ(reg);
  3137. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3138. temp |= snb_b_fdi_train_param[i];
  3139. I915_WRITE(reg, temp);
  3140. POSTING_READ(reg);
  3141. udelay(500);
  3142. for (retry = 0; retry < 5; retry++) {
  3143. reg = FDI_RX_IIR(pipe);
  3144. temp = I915_READ(reg);
  3145. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3146. if (temp & FDI_RX_SYMBOL_LOCK) {
  3147. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3148. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3149. break;
  3150. }
  3151. udelay(50);
  3152. }
  3153. if (retry < 5)
  3154. break;
  3155. }
  3156. if (i == 4)
  3157. DRM_ERROR("FDI train 2 fail!\n");
  3158. DRM_DEBUG_KMS("FDI train done.\n");
  3159. }
  3160. /* Manual link training for Ivy Bridge A0 parts */
  3161. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3162. {
  3163. struct drm_device *dev = crtc->dev;
  3164. struct drm_i915_private *dev_priv = dev->dev_private;
  3165. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3166. int pipe = intel_crtc->pipe;
  3167. u32 reg, temp, i, j;
  3168. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3169. for train result */
  3170. reg = FDI_RX_IMR(pipe);
  3171. temp = I915_READ(reg);
  3172. temp &= ~FDI_RX_SYMBOL_LOCK;
  3173. temp &= ~FDI_RX_BIT_LOCK;
  3174. I915_WRITE(reg, temp);
  3175. POSTING_READ(reg);
  3176. udelay(150);
  3177. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3178. I915_READ(FDI_RX_IIR(pipe)));
  3179. /* Try each vswing and preemphasis setting twice before moving on */
  3180. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3181. /* disable first in case we need to retry */
  3182. reg = FDI_TX_CTL(pipe);
  3183. temp = I915_READ(reg);
  3184. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3185. temp &= ~FDI_TX_ENABLE;
  3186. I915_WRITE(reg, temp);
  3187. reg = FDI_RX_CTL(pipe);
  3188. temp = I915_READ(reg);
  3189. temp &= ~FDI_LINK_TRAIN_AUTO;
  3190. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3191. temp &= ~FDI_RX_ENABLE;
  3192. I915_WRITE(reg, temp);
  3193. /* enable CPU FDI TX and PCH FDI RX */
  3194. reg = FDI_TX_CTL(pipe);
  3195. temp = I915_READ(reg);
  3196. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3197. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3198. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3199. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3200. temp |= snb_b_fdi_train_param[j/2];
  3201. temp |= FDI_COMPOSITE_SYNC;
  3202. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3203. I915_WRITE(FDI_RX_MISC(pipe),
  3204. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3205. reg = FDI_RX_CTL(pipe);
  3206. temp = I915_READ(reg);
  3207. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3208. temp |= FDI_COMPOSITE_SYNC;
  3209. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3210. POSTING_READ(reg);
  3211. udelay(1); /* should be 0.5us */
  3212. for (i = 0; i < 4; i++) {
  3213. reg = FDI_RX_IIR(pipe);
  3214. temp = I915_READ(reg);
  3215. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3216. if (temp & FDI_RX_BIT_LOCK ||
  3217. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3218. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3219. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3220. i);
  3221. break;
  3222. }
  3223. udelay(1); /* should be 0.5us */
  3224. }
  3225. if (i == 4) {
  3226. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3227. continue;
  3228. }
  3229. /* Train 2 */
  3230. reg = FDI_TX_CTL(pipe);
  3231. temp = I915_READ(reg);
  3232. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3233. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3234. I915_WRITE(reg, temp);
  3235. reg = FDI_RX_CTL(pipe);
  3236. temp = I915_READ(reg);
  3237. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3238. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3239. I915_WRITE(reg, temp);
  3240. POSTING_READ(reg);
  3241. udelay(2); /* should be 1.5us */
  3242. for (i = 0; i < 4; i++) {
  3243. reg = FDI_RX_IIR(pipe);
  3244. temp = I915_READ(reg);
  3245. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3246. if (temp & FDI_RX_SYMBOL_LOCK ||
  3247. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3248. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3249. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3250. i);
  3251. goto train_done;
  3252. }
  3253. udelay(2); /* should be 1.5us */
  3254. }
  3255. if (i == 4)
  3256. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3257. }
  3258. train_done:
  3259. DRM_DEBUG_KMS("FDI train done.\n");
  3260. }
  3261. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3262. {
  3263. struct drm_device *dev = intel_crtc->base.dev;
  3264. struct drm_i915_private *dev_priv = dev->dev_private;
  3265. int pipe = intel_crtc->pipe;
  3266. u32 reg, temp;
  3267. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3268. reg = FDI_RX_CTL(pipe);
  3269. temp = I915_READ(reg);
  3270. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3271. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3272. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3273. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3274. POSTING_READ(reg);
  3275. udelay(200);
  3276. /* Switch from Rawclk to PCDclk */
  3277. temp = I915_READ(reg);
  3278. I915_WRITE(reg, temp | FDI_PCDCLK);
  3279. POSTING_READ(reg);
  3280. udelay(200);
  3281. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3282. reg = FDI_TX_CTL(pipe);
  3283. temp = I915_READ(reg);
  3284. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3285. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3286. POSTING_READ(reg);
  3287. udelay(100);
  3288. }
  3289. }
  3290. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3291. {
  3292. struct drm_device *dev = intel_crtc->base.dev;
  3293. struct drm_i915_private *dev_priv = dev->dev_private;
  3294. int pipe = intel_crtc->pipe;
  3295. u32 reg, temp;
  3296. /* Switch from PCDclk to Rawclk */
  3297. reg = FDI_RX_CTL(pipe);
  3298. temp = I915_READ(reg);
  3299. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3300. /* Disable CPU FDI TX PLL */
  3301. reg = FDI_TX_CTL(pipe);
  3302. temp = I915_READ(reg);
  3303. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3304. POSTING_READ(reg);
  3305. udelay(100);
  3306. reg = FDI_RX_CTL(pipe);
  3307. temp = I915_READ(reg);
  3308. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3309. /* Wait for the clocks to turn off. */
  3310. POSTING_READ(reg);
  3311. udelay(100);
  3312. }
  3313. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3314. {
  3315. struct drm_device *dev = crtc->dev;
  3316. struct drm_i915_private *dev_priv = dev->dev_private;
  3317. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3318. int pipe = intel_crtc->pipe;
  3319. u32 reg, temp;
  3320. /* disable CPU FDI tx and PCH FDI rx */
  3321. reg = FDI_TX_CTL(pipe);
  3322. temp = I915_READ(reg);
  3323. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3324. POSTING_READ(reg);
  3325. reg = FDI_RX_CTL(pipe);
  3326. temp = I915_READ(reg);
  3327. temp &= ~(0x7 << 16);
  3328. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3329. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3330. POSTING_READ(reg);
  3331. udelay(100);
  3332. /* Ironlake workaround, disable clock pointer after downing FDI */
  3333. if (HAS_PCH_IBX(dev))
  3334. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3335. /* still set train pattern 1 */
  3336. reg = FDI_TX_CTL(pipe);
  3337. temp = I915_READ(reg);
  3338. temp &= ~FDI_LINK_TRAIN_NONE;
  3339. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3340. I915_WRITE(reg, temp);
  3341. reg = FDI_RX_CTL(pipe);
  3342. temp = I915_READ(reg);
  3343. if (HAS_PCH_CPT(dev)) {
  3344. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3345. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3346. } else {
  3347. temp &= ~FDI_LINK_TRAIN_NONE;
  3348. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3349. }
  3350. /* BPC in FDI rx is consistent with that in PIPECONF */
  3351. temp &= ~(0x07 << 16);
  3352. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3353. I915_WRITE(reg, temp);
  3354. POSTING_READ(reg);
  3355. udelay(100);
  3356. }
  3357. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3358. {
  3359. struct intel_crtc *crtc;
  3360. /* Note that we don't need to be called with mode_config.lock here
  3361. * as our list of CRTC objects is static for the lifetime of the
  3362. * device and so cannot disappear as we iterate. Similarly, we can
  3363. * happily treat the predicates as racy, atomic checks as userspace
  3364. * cannot claim and pin a new fb without at least acquring the
  3365. * struct_mutex and so serialising with us.
  3366. */
  3367. for_each_intel_crtc(dev, crtc) {
  3368. if (atomic_read(&crtc->unpin_work_count) == 0)
  3369. continue;
  3370. if (crtc->unpin_work)
  3371. intel_wait_for_vblank(dev, crtc->pipe);
  3372. return true;
  3373. }
  3374. return false;
  3375. }
  3376. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3377. {
  3378. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3379. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3380. /* ensure that the unpin work is consistent wrt ->pending. */
  3381. smp_rmb();
  3382. intel_crtc->unpin_work = NULL;
  3383. if (work->event)
  3384. drm_send_vblank_event(intel_crtc->base.dev,
  3385. intel_crtc->pipe,
  3386. work->event);
  3387. drm_crtc_vblank_put(&intel_crtc->base);
  3388. wake_up_all(&dev_priv->pending_flip_queue);
  3389. queue_work(dev_priv->wq, &work->work);
  3390. trace_i915_flip_complete(intel_crtc->plane,
  3391. work->pending_flip_obj);
  3392. }
  3393. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3394. {
  3395. struct drm_device *dev = crtc->dev;
  3396. struct drm_i915_private *dev_priv = dev->dev_private;
  3397. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3398. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3399. !intel_crtc_has_pending_flip(crtc),
  3400. 60*HZ) == 0)) {
  3401. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3402. spin_lock_irq(&dev->event_lock);
  3403. if (intel_crtc->unpin_work) {
  3404. WARN_ONCE(1, "Removing stuck page flip\n");
  3405. page_flip_completed(intel_crtc);
  3406. }
  3407. spin_unlock_irq(&dev->event_lock);
  3408. }
  3409. if (crtc->primary->fb) {
  3410. mutex_lock(&dev->struct_mutex);
  3411. intel_finish_fb(crtc->primary->fb);
  3412. mutex_unlock(&dev->struct_mutex);
  3413. }
  3414. }
  3415. /* Program iCLKIP clock to the desired frequency */
  3416. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3417. {
  3418. struct drm_device *dev = crtc->dev;
  3419. struct drm_i915_private *dev_priv = dev->dev_private;
  3420. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3421. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3422. u32 temp;
  3423. mutex_lock(&dev_priv->dpio_lock);
  3424. /* It is necessary to ungate the pixclk gate prior to programming
  3425. * the divisors, and gate it back when it is done.
  3426. */
  3427. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3428. /* Disable SSCCTL */
  3429. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3430. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3431. SBI_SSCCTL_DISABLE,
  3432. SBI_ICLK);
  3433. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3434. if (clock == 20000) {
  3435. auxdiv = 1;
  3436. divsel = 0x41;
  3437. phaseinc = 0x20;
  3438. } else {
  3439. /* The iCLK virtual clock root frequency is in MHz,
  3440. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3441. * divisors, it is necessary to divide one by another, so we
  3442. * convert the virtual clock precision to KHz here for higher
  3443. * precision.
  3444. */
  3445. u32 iclk_virtual_root_freq = 172800 * 1000;
  3446. u32 iclk_pi_range = 64;
  3447. u32 desired_divisor, msb_divisor_value, pi_value;
  3448. desired_divisor = (iclk_virtual_root_freq / clock);
  3449. msb_divisor_value = desired_divisor / iclk_pi_range;
  3450. pi_value = desired_divisor % iclk_pi_range;
  3451. auxdiv = 0;
  3452. divsel = msb_divisor_value - 2;
  3453. phaseinc = pi_value;
  3454. }
  3455. /* This should not happen with any sane values */
  3456. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3457. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3458. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3459. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3460. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3461. clock,
  3462. auxdiv,
  3463. divsel,
  3464. phasedir,
  3465. phaseinc);
  3466. /* Program SSCDIVINTPHASE6 */
  3467. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3468. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3469. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3470. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3471. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3472. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3473. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3474. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3475. /* Program SSCAUXDIV */
  3476. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3477. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3478. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3479. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3480. /* Enable modulator and associated divider */
  3481. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3482. temp &= ~SBI_SSCCTL_DISABLE;
  3483. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3484. /* Wait for initialization time */
  3485. udelay(24);
  3486. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3487. mutex_unlock(&dev_priv->dpio_lock);
  3488. }
  3489. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3490. enum pipe pch_transcoder)
  3491. {
  3492. struct drm_device *dev = crtc->base.dev;
  3493. struct drm_i915_private *dev_priv = dev->dev_private;
  3494. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3495. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3496. I915_READ(HTOTAL(cpu_transcoder)));
  3497. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3498. I915_READ(HBLANK(cpu_transcoder)));
  3499. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3500. I915_READ(HSYNC(cpu_transcoder)));
  3501. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3502. I915_READ(VTOTAL(cpu_transcoder)));
  3503. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3504. I915_READ(VBLANK(cpu_transcoder)));
  3505. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3506. I915_READ(VSYNC(cpu_transcoder)));
  3507. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3508. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3509. }
  3510. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3511. {
  3512. struct drm_i915_private *dev_priv = dev->dev_private;
  3513. uint32_t temp;
  3514. temp = I915_READ(SOUTH_CHICKEN1);
  3515. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3516. return;
  3517. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3518. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3519. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3520. if (enable)
  3521. temp |= FDI_BC_BIFURCATION_SELECT;
  3522. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3523. I915_WRITE(SOUTH_CHICKEN1, temp);
  3524. POSTING_READ(SOUTH_CHICKEN1);
  3525. }
  3526. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3527. {
  3528. struct drm_device *dev = intel_crtc->base.dev;
  3529. switch (intel_crtc->pipe) {
  3530. case PIPE_A:
  3531. break;
  3532. case PIPE_B:
  3533. if (intel_crtc->config->fdi_lanes > 2)
  3534. cpt_set_fdi_bc_bifurcation(dev, false);
  3535. else
  3536. cpt_set_fdi_bc_bifurcation(dev, true);
  3537. break;
  3538. case PIPE_C:
  3539. cpt_set_fdi_bc_bifurcation(dev, true);
  3540. break;
  3541. default:
  3542. BUG();
  3543. }
  3544. }
  3545. /*
  3546. * Enable PCH resources required for PCH ports:
  3547. * - PCH PLLs
  3548. * - FDI training & RX/TX
  3549. * - update transcoder timings
  3550. * - DP transcoding bits
  3551. * - transcoder
  3552. */
  3553. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3554. {
  3555. struct drm_device *dev = crtc->dev;
  3556. struct drm_i915_private *dev_priv = dev->dev_private;
  3557. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3558. int pipe = intel_crtc->pipe;
  3559. u32 reg, temp;
  3560. assert_pch_transcoder_disabled(dev_priv, pipe);
  3561. if (IS_IVYBRIDGE(dev))
  3562. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3563. /* Write the TU size bits before fdi link training, so that error
  3564. * detection works. */
  3565. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3566. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3567. /* For PCH output, training FDI link */
  3568. dev_priv->display.fdi_link_train(crtc);
  3569. /* We need to program the right clock selection before writing the pixel
  3570. * mutliplier into the DPLL. */
  3571. if (HAS_PCH_CPT(dev)) {
  3572. u32 sel;
  3573. temp = I915_READ(PCH_DPLL_SEL);
  3574. temp |= TRANS_DPLL_ENABLE(pipe);
  3575. sel = TRANS_DPLLB_SEL(pipe);
  3576. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3577. temp |= sel;
  3578. else
  3579. temp &= ~sel;
  3580. I915_WRITE(PCH_DPLL_SEL, temp);
  3581. }
  3582. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3583. * transcoder, and we actually should do this to not upset any PCH
  3584. * transcoder that already use the clock when we share it.
  3585. *
  3586. * Note that enable_shared_dpll tries to do the right thing, but
  3587. * get_shared_dpll unconditionally resets the pll - we need that to have
  3588. * the right LVDS enable sequence. */
  3589. intel_enable_shared_dpll(intel_crtc);
  3590. /* set transcoder timing, panel must allow it */
  3591. assert_panel_unlocked(dev_priv, pipe);
  3592. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3593. intel_fdi_normal_train(crtc);
  3594. /* For PCH DP, enable TRANS_DP_CTL */
  3595. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3596. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3597. reg = TRANS_DP_CTL(pipe);
  3598. temp = I915_READ(reg);
  3599. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3600. TRANS_DP_SYNC_MASK |
  3601. TRANS_DP_BPC_MASK);
  3602. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3603. TRANS_DP_ENH_FRAMING);
  3604. temp |= bpc << 9; /* same format but at 11:9 */
  3605. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3606. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3607. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3608. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3609. switch (intel_trans_dp_port_sel(crtc)) {
  3610. case PCH_DP_B:
  3611. temp |= TRANS_DP_PORT_SEL_B;
  3612. break;
  3613. case PCH_DP_C:
  3614. temp |= TRANS_DP_PORT_SEL_C;
  3615. break;
  3616. case PCH_DP_D:
  3617. temp |= TRANS_DP_PORT_SEL_D;
  3618. break;
  3619. default:
  3620. BUG();
  3621. }
  3622. I915_WRITE(reg, temp);
  3623. }
  3624. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3625. }
  3626. static void lpt_pch_enable(struct drm_crtc *crtc)
  3627. {
  3628. struct drm_device *dev = crtc->dev;
  3629. struct drm_i915_private *dev_priv = dev->dev_private;
  3630. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3631. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3632. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3633. lpt_program_iclkip(crtc);
  3634. /* Set transcoder timing. */
  3635. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3636. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3637. }
  3638. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3639. {
  3640. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3641. if (pll == NULL)
  3642. return;
  3643. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3644. WARN(1, "bad %s crtc mask\n", pll->name);
  3645. return;
  3646. }
  3647. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3648. if (pll->config.crtc_mask == 0) {
  3649. WARN_ON(pll->on);
  3650. WARN_ON(pll->active);
  3651. }
  3652. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3653. }
  3654. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3655. struct intel_crtc_state *crtc_state)
  3656. {
  3657. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3658. struct intel_shared_dpll *pll;
  3659. enum intel_dpll_id i;
  3660. if (HAS_PCH_IBX(dev_priv->dev)) {
  3661. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3662. i = (enum intel_dpll_id) crtc->pipe;
  3663. pll = &dev_priv->shared_dplls[i];
  3664. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3665. crtc->base.base.id, pll->name);
  3666. WARN_ON(pll->new_config->crtc_mask);
  3667. goto found;
  3668. }
  3669. if (IS_BROXTON(dev_priv->dev)) {
  3670. /* PLL is attached to port in bxt */
  3671. struct intel_encoder *encoder;
  3672. struct intel_digital_port *intel_dig_port;
  3673. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3674. if (WARN_ON(!encoder))
  3675. return NULL;
  3676. intel_dig_port = enc_to_dig_port(&encoder->base);
  3677. /* 1:1 mapping between ports and PLLs */
  3678. i = (enum intel_dpll_id)intel_dig_port->port;
  3679. pll = &dev_priv->shared_dplls[i];
  3680. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3681. crtc->base.base.id, pll->name);
  3682. WARN_ON(pll->new_config->crtc_mask);
  3683. goto found;
  3684. }
  3685. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3686. pll = &dev_priv->shared_dplls[i];
  3687. /* Only want to check enabled timings first */
  3688. if (pll->new_config->crtc_mask == 0)
  3689. continue;
  3690. if (memcmp(&crtc_state->dpll_hw_state,
  3691. &pll->new_config->hw_state,
  3692. sizeof(pll->new_config->hw_state)) == 0) {
  3693. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3694. crtc->base.base.id, pll->name,
  3695. pll->new_config->crtc_mask,
  3696. pll->active);
  3697. goto found;
  3698. }
  3699. }
  3700. /* Ok no matching timings, maybe there's a free one? */
  3701. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3702. pll = &dev_priv->shared_dplls[i];
  3703. if (pll->new_config->crtc_mask == 0) {
  3704. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3705. crtc->base.base.id, pll->name);
  3706. goto found;
  3707. }
  3708. }
  3709. return NULL;
  3710. found:
  3711. if (pll->new_config->crtc_mask == 0)
  3712. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3713. crtc_state->shared_dpll = i;
  3714. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3715. pipe_name(crtc->pipe));
  3716. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3717. return pll;
  3718. }
  3719. /**
  3720. * intel_shared_dpll_start_config - start a new PLL staged config
  3721. * @dev_priv: DRM device
  3722. * @clear_pipes: mask of pipes that will have their PLLs freed
  3723. *
  3724. * Starts a new PLL staged config, copying the current config but
  3725. * releasing the references of pipes specified in clear_pipes.
  3726. */
  3727. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3728. unsigned clear_pipes)
  3729. {
  3730. struct intel_shared_dpll *pll;
  3731. enum intel_dpll_id i;
  3732. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3733. pll = &dev_priv->shared_dplls[i];
  3734. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3735. GFP_KERNEL);
  3736. if (!pll->new_config)
  3737. goto cleanup;
  3738. pll->new_config->crtc_mask &= ~clear_pipes;
  3739. }
  3740. return 0;
  3741. cleanup:
  3742. while (--i >= 0) {
  3743. pll = &dev_priv->shared_dplls[i];
  3744. kfree(pll->new_config);
  3745. pll->new_config = NULL;
  3746. }
  3747. return -ENOMEM;
  3748. }
  3749. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3750. {
  3751. struct intel_shared_dpll *pll;
  3752. enum intel_dpll_id i;
  3753. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3754. pll = &dev_priv->shared_dplls[i];
  3755. WARN_ON(pll->new_config == &pll->config);
  3756. pll->config = *pll->new_config;
  3757. kfree(pll->new_config);
  3758. pll->new_config = NULL;
  3759. }
  3760. }
  3761. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3762. {
  3763. struct intel_shared_dpll *pll;
  3764. enum intel_dpll_id i;
  3765. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3766. pll = &dev_priv->shared_dplls[i];
  3767. WARN_ON(pll->new_config == &pll->config);
  3768. kfree(pll->new_config);
  3769. pll->new_config = NULL;
  3770. }
  3771. }
  3772. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3773. {
  3774. struct drm_i915_private *dev_priv = dev->dev_private;
  3775. int dslreg = PIPEDSL(pipe);
  3776. u32 temp;
  3777. temp = I915_READ(dslreg);
  3778. udelay(500);
  3779. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3780. if (wait_for(I915_READ(dslreg) != temp, 5))
  3781. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3782. }
  3783. }
  3784. /**
  3785. * skl_update_scaler_users - Stages update to crtc's scaler state
  3786. * @intel_crtc: crtc
  3787. * @crtc_state: crtc_state
  3788. * @plane: plane (NULL indicates crtc is requesting update)
  3789. * @plane_state: plane's state
  3790. * @force_detach: request unconditional detachment of scaler
  3791. *
  3792. * This function updates scaler state for requested plane or crtc.
  3793. * To request scaler usage update for a plane, caller shall pass plane pointer.
  3794. * To request scaler usage update for crtc, caller shall pass plane pointer
  3795. * as NULL.
  3796. *
  3797. * Return
  3798. * 0 - scaler_usage updated successfully
  3799. * error - requested scaling cannot be supported or other error condition
  3800. */
  3801. int
  3802. skl_update_scaler_users(
  3803. struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
  3804. struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
  3805. int force_detach)
  3806. {
  3807. int need_scaling;
  3808. int idx;
  3809. int src_w, src_h, dst_w, dst_h;
  3810. int *scaler_id;
  3811. struct drm_framebuffer *fb;
  3812. struct intel_crtc_scaler_state *scaler_state;
  3813. unsigned int rotation;
  3814. if (!intel_crtc || !crtc_state)
  3815. return 0;
  3816. scaler_state = &crtc_state->scaler_state;
  3817. idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
  3818. fb = intel_plane ? plane_state->base.fb : NULL;
  3819. if (intel_plane) {
  3820. src_w = drm_rect_width(&plane_state->src) >> 16;
  3821. src_h = drm_rect_height(&plane_state->src) >> 16;
  3822. dst_w = drm_rect_width(&plane_state->dst);
  3823. dst_h = drm_rect_height(&plane_state->dst);
  3824. scaler_id = &plane_state->scaler_id;
  3825. rotation = plane_state->base.rotation;
  3826. } else {
  3827. struct drm_display_mode *adjusted_mode =
  3828. &crtc_state->base.adjusted_mode;
  3829. src_w = crtc_state->pipe_src_w;
  3830. src_h = crtc_state->pipe_src_h;
  3831. dst_w = adjusted_mode->hdisplay;
  3832. dst_h = adjusted_mode->vdisplay;
  3833. scaler_id = &scaler_state->scaler_id;
  3834. rotation = DRM_ROTATE_0;
  3835. }
  3836. need_scaling = intel_rotation_90_or_270(rotation) ?
  3837. (src_h != dst_w || src_w != dst_h):
  3838. (src_w != dst_w || src_h != dst_h);
  3839. /*
  3840. * if plane is being disabled or scaler is no more required or force detach
  3841. * - free scaler binded to this plane/crtc
  3842. * - in order to do this, update crtc->scaler_usage
  3843. *
  3844. * Here scaler state in crtc_state is set free so that
  3845. * scaler can be assigned to other user. Actual register
  3846. * update to free the scaler is done in plane/panel-fit programming.
  3847. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3848. */
  3849. if (force_detach || !need_scaling || (intel_plane &&
  3850. (!fb || !plane_state->visible))) {
  3851. if (*scaler_id >= 0) {
  3852. scaler_state->scaler_users &= ~(1 << idx);
  3853. scaler_state->scalers[*scaler_id].in_use = 0;
  3854. DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
  3855. "crtc_state = %p scaler_users = 0x%x\n",
  3856. intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
  3857. intel_plane ? intel_plane->base.base.id :
  3858. intel_crtc->base.base.id, crtc_state,
  3859. scaler_state->scaler_users);
  3860. *scaler_id = -1;
  3861. }
  3862. return 0;
  3863. }
  3864. /* range checks */
  3865. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3866. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3867. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3868. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3869. DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
  3870. "size is out of scaler range\n",
  3871. intel_plane ? "PLANE" : "CRTC",
  3872. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3873. intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
  3874. return -EINVAL;
  3875. }
  3876. /* check colorkey */
  3877. if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
  3878. DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
  3879. intel_plane->base.base.id);
  3880. return -EINVAL;
  3881. }
  3882. /* Check src format */
  3883. if (intel_plane) {
  3884. switch (fb->pixel_format) {
  3885. case DRM_FORMAT_RGB565:
  3886. case DRM_FORMAT_XBGR8888:
  3887. case DRM_FORMAT_XRGB8888:
  3888. case DRM_FORMAT_ABGR8888:
  3889. case DRM_FORMAT_ARGB8888:
  3890. case DRM_FORMAT_XRGB2101010:
  3891. case DRM_FORMAT_ARGB2101010:
  3892. case DRM_FORMAT_XBGR2101010:
  3893. case DRM_FORMAT_ABGR2101010:
  3894. case DRM_FORMAT_YUYV:
  3895. case DRM_FORMAT_YVYU:
  3896. case DRM_FORMAT_UYVY:
  3897. case DRM_FORMAT_VYUY:
  3898. break;
  3899. default:
  3900. DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
  3901. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3902. return -EINVAL;
  3903. }
  3904. }
  3905. /* mark this plane as a scaler user in crtc_state */
  3906. scaler_state->scaler_users |= (1 << idx);
  3907. DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
  3908. "crtc_state = %p scaler_users = 0x%x\n",
  3909. intel_plane ? "PLANE" : "CRTC",
  3910. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3911. src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
  3912. return 0;
  3913. }
  3914. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3915. {
  3916. struct drm_device *dev = crtc->base.dev;
  3917. struct drm_i915_private *dev_priv = dev->dev_private;
  3918. int pipe = crtc->pipe;
  3919. struct intel_crtc_scaler_state *scaler_state =
  3920. &crtc->config->scaler_state;
  3921. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3922. /* To update pfit, first update scaler state */
  3923. skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
  3924. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3925. skl_detach_scalers(crtc);
  3926. if (!enable)
  3927. return;
  3928. if (crtc->config->pch_pfit.enabled) {
  3929. int id;
  3930. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3931. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3932. return;
  3933. }
  3934. id = scaler_state->scaler_id;
  3935. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3936. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3937. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3938. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3939. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3940. }
  3941. }
  3942. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3943. {
  3944. struct drm_device *dev = crtc->base.dev;
  3945. struct drm_i915_private *dev_priv = dev->dev_private;
  3946. int pipe = crtc->pipe;
  3947. if (crtc->config->pch_pfit.enabled) {
  3948. /* Force use of hard-coded filter coefficients
  3949. * as some pre-programmed values are broken,
  3950. * e.g. x201.
  3951. */
  3952. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3953. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3954. PF_PIPE_SEL_IVB(pipe));
  3955. else
  3956. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3957. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3958. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3959. }
  3960. }
  3961. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3962. {
  3963. struct drm_device *dev = crtc->dev;
  3964. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3965. struct drm_plane *plane;
  3966. struct intel_plane *intel_plane;
  3967. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3968. intel_plane = to_intel_plane(plane);
  3969. if (intel_plane->pipe == pipe)
  3970. intel_plane_restore(&intel_plane->base);
  3971. }
  3972. }
  3973. /*
  3974. * Disable a plane internally without actually modifying the plane's state.
  3975. * This will allow us to easily restore the plane later by just reprogramming
  3976. * its state.
  3977. */
  3978. static void disable_plane_internal(struct drm_plane *plane)
  3979. {
  3980. struct intel_plane *intel_plane = to_intel_plane(plane);
  3981. struct drm_plane_state *state =
  3982. plane->funcs->atomic_duplicate_state(plane);
  3983. struct intel_plane_state *intel_state = to_intel_plane_state(state);
  3984. intel_state->visible = false;
  3985. intel_plane->commit_plane(plane, intel_state);
  3986. intel_plane_destroy_state(plane, state);
  3987. }
  3988. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3989. {
  3990. struct drm_device *dev = crtc->dev;
  3991. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3992. struct drm_plane *plane;
  3993. struct intel_plane *intel_plane;
  3994. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3995. intel_plane = to_intel_plane(plane);
  3996. if (plane->fb && intel_plane->pipe == pipe)
  3997. disable_plane_internal(plane);
  3998. }
  3999. }
  4000. void hsw_enable_ips(struct intel_crtc *crtc)
  4001. {
  4002. struct drm_device *dev = crtc->base.dev;
  4003. struct drm_i915_private *dev_priv = dev->dev_private;
  4004. if (!crtc->config->ips_enabled)
  4005. return;
  4006. /* We can only enable IPS after we enable a plane and wait for a vblank */
  4007. intel_wait_for_vblank(dev, crtc->pipe);
  4008. assert_plane_enabled(dev_priv, crtc->plane);
  4009. if (IS_BROADWELL(dev)) {
  4010. mutex_lock(&dev_priv->rps.hw_lock);
  4011. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4012. mutex_unlock(&dev_priv->rps.hw_lock);
  4013. /* Quoting Art Runyan: "its not safe to expect any particular
  4014. * value in IPS_CTL bit 31 after enabling IPS through the
  4015. * mailbox." Moreover, the mailbox may return a bogus state,
  4016. * so we need to just enable it and continue on.
  4017. */
  4018. } else {
  4019. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4020. /* The bit only becomes 1 in the next vblank, so this wait here
  4021. * is essentially intel_wait_for_vblank. If we don't have this
  4022. * and don't wait for vblanks until the end of crtc_enable, then
  4023. * the HW state readout code will complain that the expected
  4024. * IPS_CTL value is not the one we read. */
  4025. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  4026. DRM_ERROR("Timed out waiting for IPS enable\n");
  4027. }
  4028. }
  4029. void hsw_disable_ips(struct intel_crtc *crtc)
  4030. {
  4031. struct drm_device *dev = crtc->base.dev;
  4032. struct drm_i915_private *dev_priv = dev->dev_private;
  4033. if (!crtc->config->ips_enabled)
  4034. return;
  4035. assert_plane_enabled(dev_priv, crtc->plane);
  4036. if (IS_BROADWELL(dev)) {
  4037. mutex_lock(&dev_priv->rps.hw_lock);
  4038. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4039. mutex_unlock(&dev_priv->rps.hw_lock);
  4040. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4041. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  4042. DRM_ERROR("Timed out waiting for IPS disable\n");
  4043. } else {
  4044. I915_WRITE(IPS_CTL, 0);
  4045. POSTING_READ(IPS_CTL);
  4046. }
  4047. /* We need to wait for a vblank before we can disable the plane. */
  4048. intel_wait_for_vblank(dev, crtc->pipe);
  4049. }
  4050. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  4051. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  4052. {
  4053. struct drm_device *dev = crtc->dev;
  4054. struct drm_i915_private *dev_priv = dev->dev_private;
  4055. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4056. enum pipe pipe = intel_crtc->pipe;
  4057. int palreg = PALETTE(pipe);
  4058. int i;
  4059. bool reenable_ips = false;
  4060. /* The clocks have to be on to load the palette. */
  4061. if (!crtc->state->enable || !intel_crtc->active)
  4062. return;
  4063. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  4064. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  4065. assert_dsi_pll_enabled(dev_priv);
  4066. else
  4067. assert_pll_enabled(dev_priv, pipe);
  4068. }
  4069. /* use legacy palette for Ironlake */
  4070. if (!HAS_GMCH_DISPLAY(dev))
  4071. palreg = LGC_PALETTE(pipe);
  4072. /* Workaround : Do not read or write the pipe palette/gamma data while
  4073. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  4074. */
  4075. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  4076. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  4077. GAMMA_MODE_MODE_SPLIT)) {
  4078. hsw_disable_ips(intel_crtc);
  4079. reenable_ips = true;
  4080. }
  4081. for (i = 0; i < 256; i++) {
  4082. I915_WRITE(palreg + 4 * i,
  4083. (intel_crtc->lut_r[i] << 16) |
  4084. (intel_crtc->lut_g[i] << 8) |
  4085. intel_crtc->lut_b[i]);
  4086. }
  4087. if (reenable_ips)
  4088. hsw_enable_ips(intel_crtc);
  4089. }
  4090. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  4091. {
  4092. if (!enable && intel_crtc->overlay) {
  4093. struct drm_device *dev = intel_crtc->base.dev;
  4094. struct drm_i915_private *dev_priv = dev->dev_private;
  4095. mutex_lock(&dev->struct_mutex);
  4096. dev_priv->mm.interruptible = false;
  4097. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4098. dev_priv->mm.interruptible = true;
  4099. mutex_unlock(&dev->struct_mutex);
  4100. }
  4101. /* Let userspace switch the overlay on again. In most cases userspace
  4102. * has to recompute where to put it anyway.
  4103. */
  4104. }
  4105. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  4106. {
  4107. struct drm_device *dev = crtc->dev;
  4108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4109. int pipe = intel_crtc->pipe;
  4110. intel_enable_primary_hw_plane(crtc->primary, crtc);
  4111. intel_enable_sprite_planes(crtc);
  4112. intel_crtc_update_cursor(crtc, true);
  4113. intel_crtc_dpms_overlay(intel_crtc, true);
  4114. hsw_enable_ips(intel_crtc);
  4115. mutex_lock(&dev->struct_mutex);
  4116. intel_fbc_update(dev);
  4117. mutex_unlock(&dev->struct_mutex);
  4118. /*
  4119. * FIXME: Once we grow proper nuclear flip support out of this we need
  4120. * to compute the mask of flip planes precisely. For the time being
  4121. * consider this a flip from a NULL plane.
  4122. */
  4123. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4124. }
  4125. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  4126. {
  4127. struct drm_device *dev = crtc->dev;
  4128. struct drm_i915_private *dev_priv = dev->dev_private;
  4129. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4130. int pipe = intel_crtc->pipe;
  4131. intel_crtc_wait_for_pending_flips(crtc);
  4132. if (dev_priv->fbc.crtc == intel_crtc)
  4133. intel_fbc_disable(dev);
  4134. hsw_disable_ips(intel_crtc);
  4135. intel_crtc_dpms_overlay(intel_crtc, false);
  4136. intel_crtc_update_cursor(crtc, false);
  4137. intel_disable_sprite_planes(crtc);
  4138. intel_disable_primary_hw_plane(crtc->primary, crtc);
  4139. /*
  4140. * FIXME: Once we grow proper nuclear flip support out of this we need
  4141. * to compute the mask of flip planes precisely. For the time being
  4142. * consider this a flip to a NULL plane.
  4143. */
  4144. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4145. }
  4146. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4147. {
  4148. struct drm_device *dev = crtc->dev;
  4149. struct drm_i915_private *dev_priv = dev->dev_private;
  4150. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4151. struct intel_encoder *encoder;
  4152. int pipe = intel_crtc->pipe;
  4153. WARN_ON(!crtc->state->enable);
  4154. if (intel_crtc->active)
  4155. return;
  4156. if (intel_crtc->config->has_pch_encoder)
  4157. intel_prepare_shared_dpll(intel_crtc);
  4158. if (intel_crtc->config->has_dp_encoder)
  4159. intel_dp_set_m_n(intel_crtc, M1_N1);
  4160. intel_set_pipe_timings(intel_crtc);
  4161. if (intel_crtc->config->has_pch_encoder) {
  4162. intel_cpu_transcoder_set_m_n(intel_crtc,
  4163. &intel_crtc->config->fdi_m_n, NULL);
  4164. }
  4165. ironlake_set_pipeconf(crtc);
  4166. intel_crtc->active = true;
  4167. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4168. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4169. for_each_encoder_on_crtc(dev, crtc, encoder)
  4170. if (encoder->pre_enable)
  4171. encoder->pre_enable(encoder);
  4172. if (intel_crtc->config->has_pch_encoder) {
  4173. /* Note: FDI PLL enabling _must_ be done before we enable the
  4174. * cpu pipes, hence this is separate from all the other fdi/pch
  4175. * enabling. */
  4176. ironlake_fdi_pll_enable(intel_crtc);
  4177. } else {
  4178. assert_fdi_tx_disabled(dev_priv, pipe);
  4179. assert_fdi_rx_disabled(dev_priv, pipe);
  4180. }
  4181. ironlake_pfit_enable(intel_crtc);
  4182. /*
  4183. * On ILK+ LUT must be loaded before the pipe is running but with
  4184. * clocks enabled
  4185. */
  4186. intel_crtc_load_lut(crtc);
  4187. intel_update_watermarks(crtc);
  4188. intel_enable_pipe(intel_crtc);
  4189. if (intel_crtc->config->has_pch_encoder)
  4190. ironlake_pch_enable(crtc);
  4191. assert_vblank_disabled(crtc);
  4192. drm_crtc_vblank_on(crtc);
  4193. for_each_encoder_on_crtc(dev, crtc, encoder)
  4194. encoder->enable(encoder);
  4195. if (HAS_PCH_CPT(dev))
  4196. cpt_verify_modeset(dev, intel_crtc->pipe);
  4197. intel_crtc_enable_planes(crtc);
  4198. }
  4199. /* IPS only exists on ULT machines and is tied to pipe A. */
  4200. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4201. {
  4202. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4203. }
  4204. /*
  4205. * This implements the workaround described in the "notes" section of the mode
  4206. * set sequence documentation. When going from no pipes or single pipe to
  4207. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  4208. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  4209. */
  4210. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  4211. {
  4212. struct drm_device *dev = crtc->base.dev;
  4213. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  4214. /* We want to get the other_active_crtc only if there's only 1 other
  4215. * active crtc. */
  4216. for_each_intel_crtc(dev, crtc_it) {
  4217. if (!crtc_it->active || crtc_it == crtc)
  4218. continue;
  4219. if (other_active_crtc)
  4220. return;
  4221. other_active_crtc = crtc_it;
  4222. }
  4223. if (!other_active_crtc)
  4224. return;
  4225. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4226. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4227. }
  4228. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4229. {
  4230. struct drm_device *dev = crtc->dev;
  4231. struct drm_i915_private *dev_priv = dev->dev_private;
  4232. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4233. struct intel_encoder *encoder;
  4234. int pipe = intel_crtc->pipe;
  4235. WARN_ON(!crtc->state->enable);
  4236. if (intel_crtc->active)
  4237. return;
  4238. if (intel_crtc_to_shared_dpll(intel_crtc))
  4239. intel_enable_shared_dpll(intel_crtc);
  4240. if (intel_crtc->config->has_dp_encoder)
  4241. intel_dp_set_m_n(intel_crtc, M1_N1);
  4242. intel_set_pipe_timings(intel_crtc);
  4243. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4244. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4245. intel_crtc->config->pixel_multiplier - 1);
  4246. }
  4247. if (intel_crtc->config->has_pch_encoder) {
  4248. intel_cpu_transcoder_set_m_n(intel_crtc,
  4249. &intel_crtc->config->fdi_m_n, NULL);
  4250. }
  4251. haswell_set_pipeconf(crtc);
  4252. intel_set_pipe_csc(crtc);
  4253. intel_crtc->active = true;
  4254. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4255. for_each_encoder_on_crtc(dev, crtc, encoder)
  4256. if (encoder->pre_enable)
  4257. encoder->pre_enable(encoder);
  4258. if (intel_crtc->config->has_pch_encoder) {
  4259. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4260. true);
  4261. dev_priv->display.fdi_link_train(crtc);
  4262. }
  4263. intel_ddi_enable_pipe_clock(intel_crtc);
  4264. if (INTEL_INFO(dev)->gen == 9)
  4265. skylake_pfit_update(intel_crtc, 1);
  4266. else if (INTEL_INFO(dev)->gen < 9)
  4267. ironlake_pfit_enable(intel_crtc);
  4268. else
  4269. MISSING_CASE(INTEL_INFO(dev)->gen);
  4270. /*
  4271. * On ILK+ LUT must be loaded before the pipe is running but with
  4272. * clocks enabled
  4273. */
  4274. intel_crtc_load_lut(crtc);
  4275. intel_ddi_set_pipe_settings(crtc);
  4276. intel_ddi_enable_transcoder_func(crtc);
  4277. intel_update_watermarks(crtc);
  4278. intel_enable_pipe(intel_crtc);
  4279. if (intel_crtc->config->has_pch_encoder)
  4280. lpt_pch_enable(crtc);
  4281. if (intel_crtc->config->dp_encoder_is_mst)
  4282. intel_ddi_set_vc_payload_alloc(crtc, true);
  4283. assert_vblank_disabled(crtc);
  4284. drm_crtc_vblank_on(crtc);
  4285. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4286. encoder->enable(encoder);
  4287. intel_opregion_notify_encoder(encoder, true);
  4288. }
  4289. /* If we change the relative order between pipe/planes enabling, we need
  4290. * to change the workaround. */
  4291. haswell_mode_set_planes_workaround(intel_crtc);
  4292. intel_crtc_enable_planes(crtc);
  4293. }
  4294. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4295. {
  4296. struct drm_device *dev = crtc->base.dev;
  4297. struct drm_i915_private *dev_priv = dev->dev_private;
  4298. int pipe = crtc->pipe;
  4299. /* To avoid upsetting the power well on haswell only disable the pfit if
  4300. * it's in use. The hw state code will make sure we get this right. */
  4301. if (crtc->config->pch_pfit.enabled) {
  4302. I915_WRITE(PF_CTL(pipe), 0);
  4303. I915_WRITE(PF_WIN_POS(pipe), 0);
  4304. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4305. }
  4306. }
  4307. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4308. {
  4309. struct drm_device *dev = crtc->dev;
  4310. struct drm_i915_private *dev_priv = dev->dev_private;
  4311. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4312. struct intel_encoder *encoder;
  4313. int pipe = intel_crtc->pipe;
  4314. u32 reg, temp;
  4315. if (!intel_crtc->active)
  4316. return;
  4317. intel_crtc_disable_planes(crtc);
  4318. for_each_encoder_on_crtc(dev, crtc, encoder)
  4319. encoder->disable(encoder);
  4320. drm_crtc_vblank_off(crtc);
  4321. assert_vblank_disabled(crtc);
  4322. if (intel_crtc->config->has_pch_encoder)
  4323. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4324. intel_disable_pipe(intel_crtc);
  4325. ironlake_pfit_disable(intel_crtc);
  4326. for_each_encoder_on_crtc(dev, crtc, encoder)
  4327. if (encoder->post_disable)
  4328. encoder->post_disable(encoder);
  4329. if (intel_crtc->config->has_pch_encoder) {
  4330. ironlake_fdi_disable(crtc);
  4331. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4332. if (HAS_PCH_CPT(dev)) {
  4333. /* disable TRANS_DP_CTL */
  4334. reg = TRANS_DP_CTL(pipe);
  4335. temp = I915_READ(reg);
  4336. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4337. TRANS_DP_PORT_SEL_MASK);
  4338. temp |= TRANS_DP_PORT_SEL_NONE;
  4339. I915_WRITE(reg, temp);
  4340. /* disable DPLL_SEL */
  4341. temp = I915_READ(PCH_DPLL_SEL);
  4342. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4343. I915_WRITE(PCH_DPLL_SEL, temp);
  4344. }
  4345. /* disable PCH DPLL */
  4346. intel_disable_shared_dpll(intel_crtc);
  4347. ironlake_fdi_pll_disable(intel_crtc);
  4348. }
  4349. intel_crtc->active = false;
  4350. intel_update_watermarks(crtc);
  4351. mutex_lock(&dev->struct_mutex);
  4352. intel_fbc_update(dev);
  4353. mutex_unlock(&dev->struct_mutex);
  4354. }
  4355. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4356. {
  4357. struct drm_device *dev = crtc->dev;
  4358. struct drm_i915_private *dev_priv = dev->dev_private;
  4359. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4360. struct intel_encoder *encoder;
  4361. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4362. if (!intel_crtc->active)
  4363. return;
  4364. intel_crtc_disable_planes(crtc);
  4365. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4366. intel_opregion_notify_encoder(encoder, false);
  4367. encoder->disable(encoder);
  4368. }
  4369. drm_crtc_vblank_off(crtc);
  4370. assert_vblank_disabled(crtc);
  4371. if (intel_crtc->config->has_pch_encoder)
  4372. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4373. false);
  4374. intel_disable_pipe(intel_crtc);
  4375. if (intel_crtc->config->dp_encoder_is_mst)
  4376. intel_ddi_set_vc_payload_alloc(crtc, false);
  4377. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4378. if (INTEL_INFO(dev)->gen == 9)
  4379. skylake_pfit_update(intel_crtc, 0);
  4380. else if (INTEL_INFO(dev)->gen < 9)
  4381. ironlake_pfit_disable(intel_crtc);
  4382. else
  4383. MISSING_CASE(INTEL_INFO(dev)->gen);
  4384. intel_ddi_disable_pipe_clock(intel_crtc);
  4385. if (intel_crtc->config->has_pch_encoder) {
  4386. lpt_disable_pch_transcoder(dev_priv);
  4387. intel_ddi_fdi_disable(crtc);
  4388. }
  4389. for_each_encoder_on_crtc(dev, crtc, encoder)
  4390. if (encoder->post_disable)
  4391. encoder->post_disable(encoder);
  4392. intel_crtc->active = false;
  4393. intel_update_watermarks(crtc);
  4394. mutex_lock(&dev->struct_mutex);
  4395. intel_fbc_update(dev);
  4396. mutex_unlock(&dev->struct_mutex);
  4397. if (intel_crtc_to_shared_dpll(intel_crtc))
  4398. intel_disable_shared_dpll(intel_crtc);
  4399. }
  4400. static void ironlake_crtc_off(struct drm_crtc *crtc)
  4401. {
  4402. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4403. intel_put_shared_dpll(intel_crtc);
  4404. }
  4405. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4406. {
  4407. struct drm_device *dev = crtc->base.dev;
  4408. struct drm_i915_private *dev_priv = dev->dev_private;
  4409. struct intel_crtc_state *pipe_config = crtc->config;
  4410. if (!pipe_config->gmch_pfit.control)
  4411. return;
  4412. /*
  4413. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4414. * according to register description and PRM.
  4415. */
  4416. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4417. assert_pipe_disabled(dev_priv, crtc->pipe);
  4418. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4419. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4420. /* Border color in case we don't scale up to the full screen. Black by
  4421. * default, change to something else for debugging. */
  4422. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4423. }
  4424. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4425. {
  4426. switch (port) {
  4427. case PORT_A:
  4428. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4429. case PORT_B:
  4430. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4431. case PORT_C:
  4432. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4433. case PORT_D:
  4434. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4435. default:
  4436. WARN_ON_ONCE(1);
  4437. return POWER_DOMAIN_PORT_OTHER;
  4438. }
  4439. }
  4440. #define for_each_power_domain(domain, mask) \
  4441. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4442. if ((1 << (domain)) & (mask))
  4443. enum intel_display_power_domain
  4444. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4445. {
  4446. struct drm_device *dev = intel_encoder->base.dev;
  4447. struct intel_digital_port *intel_dig_port;
  4448. switch (intel_encoder->type) {
  4449. case INTEL_OUTPUT_UNKNOWN:
  4450. /* Only DDI platforms should ever use this output type */
  4451. WARN_ON_ONCE(!HAS_DDI(dev));
  4452. case INTEL_OUTPUT_DISPLAYPORT:
  4453. case INTEL_OUTPUT_HDMI:
  4454. case INTEL_OUTPUT_EDP:
  4455. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4456. return port_to_power_domain(intel_dig_port->port);
  4457. case INTEL_OUTPUT_DP_MST:
  4458. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4459. return port_to_power_domain(intel_dig_port->port);
  4460. case INTEL_OUTPUT_ANALOG:
  4461. return POWER_DOMAIN_PORT_CRT;
  4462. case INTEL_OUTPUT_DSI:
  4463. return POWER_DOMAIN_PORT_DSI;
  4464. default:
  4465. return POWER_DOMAIN_PORT_OTHER;
  4466. }
  4467. }
  4468. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4469. {
  4470. struct drm_device *dev = crtc->dev;
  4471. struct intel_encoder *intel_encoder;
  4472. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4473. enum pipe pipe = intel_crtc->pipe;
  4474. unsigned long mask;
  4475. enum transcoder transcoder;
  4476. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4477. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4478. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4479. if (intel_crtc->config->pch_pfit.enabled ||
  4480. intel_crtc->config->pch_pfit.force_thru)
  4481. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4482. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4483. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4484. return mask;
  4485. }
  4486. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4487. {
  4488. struct drm_device *dev = state->dev;
  4489. struct drm_i915_private *dev_priv = dev->dev_private;
  4490. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4491. struct intel_crtc *crtc;
  4492. /*
  4493. * First get all needed power domains, then put all unneeded, to avoid
  4494. * any unnecessary toggling of the power wells.
  4495. */
  4496. for_each_intel_crtc(dev, crtc) {
  4497. enum intel_display_power_domain domain;
  4498. if (!crtc->base.state->enable)
  4499. continue;
  4500. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4501. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4502. intel_display_power_get(dev_priv, domain);
  4503. }
  4504. if (dev_priv->display.modeset_global_resources)
  4505. dev_priv->display.modeset_global_resources(state);
  4506. for_each_intel_crtc(dev, crtc) {
  4507. enum intel_display_power_domain domain;
  4508. for_each_power_domain(domain, crtc->enabled_power_domains)
  4509. intel_display_power_put(dev_priv, domain);
  4510. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4511. }
  4512. intel_display_set_init_power(dev_priv, false);
  4513. }
  4514. void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4515. {
  4516. struct drm_i915_private *dev_priv = dev->dev_private;
  4517. uint32_t divider;
  4518. uint32_t ratio;
  4519. uint32_t current_freq;
  4520. int ret;
  4521. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4522. switch (frequency) {
  4523. case 144000:
  4524. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4525. ratio = BXT_DE_PLL_RATIO(60);
  4526. break;
  4527. case 288000:
  4528. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4529. ratio = BXT_DE_PLL_RATIO(60);
  4530. break;
  4531. case 384000:
  4532. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4533. ratio = BXT_DE_PLL_RATIO(60);
  4534. break;
  4535. case 576000:
  4536. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4537. ratio = BXT_DE_PLL_RATIO(60);
  4538. break;
  4539. case 624000:
  4540. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4541. ratio = BXT_DE_PLL_RATIO(65);
  4542. break;
  4543. case 19200:
  4544. /*
  4545. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4546. * to suppress GCC warning.
  4547. */
  4548. ratio = 0;
  4549. divider = 0;
  4550. break;
  4551. default:
  4552. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4553. return;
  4554. }
  4555. mutex_lock(&dev_priv->rps.hw_lock);
  4556. /* Inform power controller of upcoming frequency change */
  4557. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4558. 0x80000000);
  4559. mutex_unlock(&dev_priv->rps.hw_lock);
  4560. if (ret) {
  4561. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4562. ret, frequency);
  4563. return;
  4564. }
  4565. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4566. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4567. current_freq = current_freq * 500 + 1000;
  4568. /*
  4569. * DE PLL has to be disabled when
  4570. * - setting to 19.2MHz (bypass, PLL isn't used)
  4571. * - before setting to 624MHz (PLL needs toggling)
  4572. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4573. */
  4574. if (frequency == 19200 || frequency == 624000 ||
  4575. current_freq == 624000) {
  4576. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4577. /* Timeout 200us */
  4578. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4579. 1))
  4580. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4581. }
  4582. if (frequency != 19200) {
  4583. uint32_t val;
  4584. val = I915_READ(BXT_DE_PLL_CTL);
  4585. val &= ~BXT_DE_PLL_RATIO_MASK;
  4586. val |= ratio;
  4587. I915_WRITE(BXT_DE_PLL_CTL, val);
  4588. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4589. /* Timeout 200us */
  4590. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4591. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4592. val = I915_READ(CDCLK_CTL);
  4593. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4594. val |= divider;
  4595. /*
  4596. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4597. * enable otherwise.
  4598. */
  4599. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4600. if (frequency >= 500000)
  4601. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4602. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4603. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4604. val |= (frequency - 1000) / 500;
  4605. I915_WRITE(CDCLK_CTL, val);
  4606. }
  4607. mutex_lock(&dev_priv->rps.hw_lock);
  4608. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4609. DIV_ROUND_UP(frequency, 25000));
  4610. mutex_unlock(&dev_priv->rps.hw_lock);
  4611. if (ret) {
  4612. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4613. ret, frequency);
  4614. return;
  4615. }
  4616. dev_priv->cdclk_freq = frequency;
  4617. }
  4618. void broxton_init_cdclk(struct drm_device *dev)
  4619. {
  4620. struct drm_i915_private *dev_priv = dev->dev_private;
  4621. uint32_t val;
  4622. /*
  4623. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4624. * or else the reset will hang because there is no PCH to respond.
  4625. * Move the handshake programming to initialization sequence.
  4626. * Previously was left up to BIOS.
  4627. */
  4628. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4629. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4630. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4631. /* Enable PG1 for cdclk */
  4632. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4633. /* check if cd clock is enabled */
  4634. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4635. DRM_DEBUG_KMS("Display already initialized\n");
  4636. return;
  4637. }
  4638. /*
  4639. * FIXME:
  4640. * - The initial CDCLK needs to be read from VBT.
  4641. * Need to make this change after VBT has changes for BXT.
  4642. * - check if setting the max (or any) cdclk freq is really necessary
  4643. * here, it belongs to modeset time
  4644. */
  4645. broxton_set_cdclk(dev, 624000);
  4646. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4647. POSTING_READ(DBUF_CTL);
  4648. udelay(10);
  4649. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4650. DRM_ERROR("DBuf power enable timeout!\n");
  4651. }
  4652. void broxton_uninit_cdclk(struct drm_device *dev)
  4653. {
  4654. struct drm_i915_private *dev_priv = dev->dev_private;
  4655. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4656. POSTING_READ(DBUF_CTL);
  4657. udelay(10);
  4658. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4659. DRM_ERROR("DBuf power disable timeout!\n");
  4660. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4661. broxton_set_cdclk(dev, 19200);
  4662. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4663. }
  4664. /* returns HPLL frequency in kHz */
  4665. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4666. {
  4667. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4668. /* Obtain SKU information */
  4669. mutex_lock(&dev_priv->dpio_lock);
  4670. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4671. CCK_FUSE_HPLL_FREQ_MASK;
  4672. mutex_unlock(&dev_priv->dpio_lock);
  4673. return vco_freq[hpll_freq] * 1000;
  4674. }
  4675. static void vlv_update_cdclk(struct drm_device *dev)
  4676. {
  4677. struct drm_i915_private *dev_priv = dev->dev_private;
  4678. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4679. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4680. dev_priv->cdclk_freq);
  4681. /*
  4682. * Program the gmbus_freq based on the cdclk frequency.
  4683. * BSpec erroneously claims we should aim for 4MHz, but
  4684. * in fact 1MHz is the correct frequency.
  4685. */
  4686. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4687. }
  4688. /* Adjust CDclk dividers to allow high res or save power if possible */
  4689. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4690. {
  4691. struct drm_i915_private *dev_priv = dev->dev_private;
  4692. u32 val, cmd;
  4693. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4694. != dev_priv->cdclk_freq);
  4695. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4696. cmd = 2;
  4697. else if (cdclk == 266667)
  4698. cmd = 1;
  4699. else
  4700. cmd = 0;
  4701. mutex_lock(&dev_priv->rps.hw_lock);
  4702. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4703. val &= ~DSPFREQGUAR_MASK;
  4704. val |= (cmd << DSPFREQGUAR_SHIFT);
  4705. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4706. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4707. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4708. 50)) {
  4709. DRM_ERROR("timed out waiting for CDclk change\n");
  4710. }
  4711. mutex_unlock(&dev_priv->rps.hw_lock);
  4712. if (cdclk == 400000) {
  4713. u32 divider;
  4714. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4715. mutex_lock(&dev_priv->dpio_lock);
  4716. /* adjust cdclk divider */
  4717. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4718. val &= ~DISPLAY_FREQUENCY_VALUES;
  4719. val |= divider;
  4720. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4721. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4722. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4723. 50))
  4724. DRM_ERROR("timed out waiting for CDclk change\n");
  4725. mutex_unlock(&dev_priv->dpio_lock);
  4726. }
  4727. mutex_lock(&dev_priv->dpio_lock);
  4728. /* adjust self-refresh exit latency value */
  4729. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4730. val &= ~0x7f;
  4731. /*
  4732. * For high bandwidth configs, we set a higher latency in the bunit
  4733. * so that the core display fetch happens in time to avoid underruns.
  4734. */
  4735. if (cdclk == 400000)
  4736. val |= 4500 / 250; /* 4.5 usec */
  4737. else
  4738. val |= 3000 / 250; /* 3.0 usec */
  4739. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4740. mutex_unlock(&dev_priv->dpio_lock);
  4741. vlv_update_cdclk(dev);
  4742. }
  4743. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4744. {
  4745. struct drm_i915_private *dev_priv = dev->dev_private;
  4746. u32 val, cmd;
  4747. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4748. != dev_priv->cdclk_freq);
  4749. switch (cdclk) {
  4750. case 333333:
  4751. case 320000:
  4752. case 266667:
  4753. case 200000:
  4754. break;
  4755. default:
  4756. MISSING_CASE(cdclk);
  4757. return;
  4758. }
  4759. /*
  4760. * Specs are full of misinformation, but testing on actual
  4761. * hardware has shown that we just need to write the desired
  4762. * CCK divider into the Punit register.
  4763. */
  4764. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4765. mutex_lock(&dev_priv->rps.hw_lock);
  4766. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4767. val &= ~DSPFREQGUAR_MASK_CHV;
  4768. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4769. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4770. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4771. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4772. 50)) {
  4773. DRM_ERROR("timed out waiting for CDclk change\n");
  4774. }
  4775. mutex_unlock(&dev_priv->rps.hw_lock);
  4776. vlv_update_cdclk(dev);
  4777. }
  4778. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4779. int max_pixclk)
  4780. {
  4781. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4782. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4783. /*
  4784. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4785. * 200MHz
  4786. * 267MHz
  4787. * 320/333MHz (depends on HPLL freq)
  4788. * 400MHz (VLV only)
  4789. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4790. * of the lower bin and adjust if needed.
  4791. *
  4792. * We seem to get an unstable or solid color picture at 200MHz.
  4793. * Not sure what's wrong. For now use 200MHz only when all pipes
  4794. * are off.
  4795. */
  4796. if (!IS_CHERRYVIEW(dev_priv) &&
  4797. max_pixclk > freq_320*limit/100)
  4798. return 400000;
  4799. else if (max_pixclk > 266667*limit/100)
  4800. return freq_320;
  4801. else if (max_pixclk > 0)
  4802. return 266667;
  4803. else
  4804. return 200000;
  4805. }
  4806. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4807. int max_pixclk)
  4808. {
  4809. /*
  4810. * FIXME:
  4811. * - remove the guardband, it's not needed on BXT
  4812. * - set 19.2MHz bypass frequency if there are no active pipes
  4813. */
  4814. if (max_pixclk > 576000*9/10)
  4815. return 624000;
  4816. else if (max_pixclk > 384000*9/10)
  4817. return 576000;
  4818. else if (max_pixclk > 288000*9/10)
  4819. return 384000;
  4820. else if (max_pixclk > 144000*9/10)
  4821. return 288000;
  4822. else
  4823. return 144000;
  4824. }
  4825. /* compute the max pixel clock for new configuration */
  4826. static int intel_mode_max_pixclk(struct drm_atomic_state *state)
  4827. {
  4828. struct drm_device *dev = state->dev;
  4829. struct intel_crtc *intel_crtc;
  4830. struct intel_crtc_state *crtc_state;
  4831. int max_pixclk = 0;
  4832. for_each_intel_crtc(dev, intel_crtc) {
  4833. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4834. if (IS_ERR(crtc_state))
  4835. return PTR_ERR(crtc_state);
  4836. if (!crtc_state->base.enable)
  4837. continue;
  4838. max_pixclk = max(max_pixclk,
  4839. crtc_state->base.adjusted_mode.crtc_clock);
  4840. }
  4841. return max_pixclk;
  4842. }
  4843. static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
  4844. unsigned *prepare_pipes)
  4845. {
  4846. struct drm_i915_private *dev_priv = to_i915(state->dev);
  4847. struct intel_crtc *intel_crtc;
  4848. int max_pixclk = intel_mode_max_pixclk(state);
  4849. int cdclk;
  4850. if (max_pixclk < 0)
  4851. return max_pixclk;
  4852. if (IS_VALLEYVIEW(dev_priv))
  4853. cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4854. else
  4855. cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  4856. if (cdclk == dev_priv->cdclk_freq)
  4857. return 0;
  4858. /* disable/enable all currently active pipes while we change cdclk */
  4859. for_each_intel_crtc(state->dev, intel_crtc)
  4860. if (intel_crtc->base.state->enable)
  4861. *prepare_pipes |= (1 << intel_crtc->pipe);
  4862. return 0;
  4863. }
  4864. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  4865. {
  4866. unsigned int credits, default_credits;
  4867. if (IS_CHERRYVIEW(dev_priv))
  4868. default_credits = PFI_CREDIT(12);
  4869. else
  4870. default_credits = PFI_CREDIT(8);
  4871. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  4872. /* CHV suggested value is 31 or 63 */
  4873. if (IS_CHERRYVIEW(dev_priv))
  4874. credits = PFI_CREDIT_31;
  4875. else
  4876. credits = PFI_CREDIT(15);
  4877. } else {
  4878. credits = default_credits;
  4879. }
  4880. /*
  4881. * WA - write default credits before re-programming
  4882. * FIXME: should we also set the resend bit here?
  4883. */
  4884. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4885. default_credits);
  4886. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4887. credits | PFI_CREDIT_RESEND);
  4888. /*
  4889. * FIXME is this guaranteed to clear
  4890. * immediately or should we poll for it?
  4891. */
  4892. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  4893. }
  4894. static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
  4895. {
  4896. struct drm_device *dev = state->dev;
  4897. struct drm_i915_private *dev_priv = dev->dev_private;
  4898. int max_pixclk = intel_mode_max_pixclk(state);
  4899. int req_cdclk;
  4900. /* The only reason this can fail is if we fail to add the crtc_state
  4901. * to the atomic state. But that can't happen since the call to
  4902. * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
  4903. * can't have failed otherwise the mode set would be aborted) added all
  4904. * the states already. */
  4905. if (WARN_ON(max_pixclk < 0))
  4906. return;
  4907. req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4908. if (req_cdclk != dev_priv->cdclk_freq) {
  4909. /*
  4910. * FIXME: We can end up here with all power domains off, yet
  4911. * with a CDCLK frequency other than the minimum. To account
  4912. * for this take the PIPE-A power domain, which covers the HW
  4913. * blocks needed for the following programming. This can be
  4914. * removed once it's guaranteed that we get here either with
  4915. * the minimum CDCLK set, or the required power domains
  4916. * enabled.
  4917. */
  4918. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4919. if (IS_CHERRYVIEW(dev))
  4920. cherryview_set_cdclk(dev, req_cdclk);
  4921. else
  4922. valleyview_set_cdclk(dev, req_cdclk);
  4923. vlv_program_pfi_credits(dev_priv);
  4924. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4925. }
  4926. }
  4927. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4928. {
  4929. struct drm_device *dev = crtc->dev;
  4930. struct drm_i915_private *dev_priv = to_i915(dev);
  4931. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4932. struct intel_encoder *encoder;
  4933. int pipe = intel_crtc->pipe;
  4934. bool is_dsi;
  4935. WARN_ON(!crtc->state->enable);
  4936. if (intel_crtc->active)
  4937. return;
  4938. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4939. if (!is_dsi) {
  4940. if (IS_CHERRYVIEW(dev))
  4941. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4942. else
  4943. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4944. }
  4945. if (intel_crtc->config->has_dp_encoder)
  4946. intel_dp_set_m_n(intel_crtc, M1_N1);
  4947. intel_set_pipe_timings(intel_crtc);
  4948. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4949. struct drm_i915_private *dev_priv = dev->dev_private;
  4950. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4951. I915_WRITE(CHV_CANVAS(pipe), 0);
  4952. }
  4953. i9xx_set_pipeconf(intel_crtc);
  4954. intel_crtc->active = true;
  4955. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4956. for_each_encoder_on_crtc(dev, crtc, encoder)
  4957. if (encoder->pre_pll_enable)
  4958. encoder->pre_pll_enable(encoder);
  4959. if (!is_dsi) {
  4960. if (IS_CHERRYVIEW(dev))
  4961. chv_enable_pll(intel_crtc, intel_crtc->config);
  4962. else
  4963. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4964. }
  4965. for_each_encoder_on_crtc(dev, crtc, encoder)
  4966. if (encoder->pre_enable)
  4967. encoder->pre_enable(encoder);
  4968. i9xx_pfit_enable(intel_crtc);
  4969. intel_crtc_load_lut(crtc);
  4970. intel_update_watermarks(crtc);
  4971. intel_enable_pipe(intel_crtc);
  4972. assert_vblank_disabled(crtc);
  4973. drm_crtc_vblank_on(crtc);
  4974. for_each_encoder_on_crtc(dev, crtc, encoder)
  4975. encoder->enable(encoder);
  4976. intel_crtc_enable_planes(crtc);
  4977. /* Underruns don't raise interrupts, so check manually. */
  4978. i9xx_check_fifo_underruns(dev_priv);
  4979. }
  4980. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4981. {
  4982. struct drm_device *dev = crtc->base.dev;
  4983. struct drm_i915_private *dev_priv = dev->dev_private;
  4984. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4985. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4986. }
  4987. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4988. {
  4989. struct drm_device *dev = crtc->dev;
  4990. struct drm_i915_private *dev_priv = to_i915(dev);
  4991. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4992. struct intel_encoder *encoder;
  4993. int pipe = intel_crtc->pipe;
  4994. WARN_ON(!crtc->state->enable);
  4995. if (intel_crtc->active)
  4996. return;
  4997. i9xx_set_pll_dividers(intel_crtc);
  4998. if (intel_crtc->config->has_dp_encoder)
  4999. intel_dp_set_m_n(intel_crtc, M1_N1);
  5000. intel_set_pipe_timings(intel_crtc);
  5001. i9xx_set_pipeconf(intel_crtc);
  5002. intel_crtc->active = true;
  5003. if (!IS_GEN2(dev))
  5004. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5005. for_each_encoder_on_crtc(dev, crtc, encoder)
  5006. if (encoder->pre_enable)
  5007. encoder->pre_enable(encoder);
  5008. i9xx_enable_pll(intel_crtc);
  5009. i9xx_pfit_enable(intel_crtc);
  5010. intel_crtc_load_lut(crtc);
  5011. intel_update_watermarks(crtc);
  5012. intel_enable_pipe(intel_crtc);
  5013. assert_vblank_disabled(crtc);
  5014. drm_crtc_vblank_on(crtc);
  5015. for_each_encoder_on_crtc(dev, crtc, encoder)
  5016. encoder->enable(encoder);
  5017. intel_crtc_enable_planes(crtc);
  5018. /*
  5019. * Gen2 reports pipe underruns whenever all planes are disabled.
  5020. * So don't enable underrun reporting before at least some planes
  5021. * are enabled.
  5022. * FIXME: Need to fix the logic to work when we turn off all planes
  5023. * but leave the pipe running.
  5024. */
  5025. if (IS_GEN2(dev))
  5026. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5027. /* Underruns don't raise interrupts, so check manually. */
  5028. i9xx_check_fifo_underruns(dev_priv);
  5029. }
  5030. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5031. {
  5032. struct drm_device *dev = crtc->base.dev;
  5033. struct drm_i915_private *dev_priv = dev->dev_private;
  5034. if (!crtc->config->gmch_pfit.control)
  5035. return;
  5036. assert_pipe_disabled(dev_priv, crtc->pipe);
  5037. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5038. I915_READ(PFIT_CONTROL));
  5039. I915_WRITE(PFIT_CONTROL, 0);
  5040. }
  5041. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5042. {
  5043. struct drm_device *dev = crtc->dev;
  5044. struct drm_i915_private *dev_priv = dev->dev_private;
  5045. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5046. struct intel_encoder *encoder;
  5047. int pipe = intel_crtc->pipe;
  5048. if (!intel_crtc->active)
  5049. return;
  5050. /*
  5051. * Gen2 reports pipe underruns whenever all planes are disabled.
  5052. * So diasble underrun reporting before all the planes get disabled.
  5053. * FIXME: Need to fix the logic to work when we turn off all planes
  5054. * but leave the pipe running.
  5055. */
  5056. if (IS_GEN2(dev))
  5057. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5058. /*
  5059. * Vblank time updates from the shadow to live plane control register
  5060. * are blocked if the memory self-refresh mode is active at that
  5061. * moment. So to make sure the plane gets truly disabled, disable
  5062. * first the self-refresh mode. The self-refresh enable bit in turn
  5063. * will be checked/applied by the HW only at the next frame start
  5064. * event which is after the vblank start event, so we need to have a
  5065. * wait-for-vblank between disabling the plane and the pipe.
  5066. */
  5067. intel_set_memory_cxsr(dev_priv, false);
  5068. intel_crtc_disable_planes(crtc);
  5069. /*
  5070. * On gen2 planes are double buffered but the pipe isn't, so we must
  5071. * wait for planes to fully turn off before disabling the pipe.
  5072. * We also need to wait on all gmch platforms because of the
  5073. * self-refresh mode constraint explained above.
  5074. */
  5075. intel_wait_for_vblank(dev, pipe);
  5076. for_each_encoder_on_crtc(dev, crtc, encoder)
  5077. encoder->disable(encoder);
  5078. drm_crtc_vblank_off(crtc);
  5079. assert_vblank_disabled(crtc);
  5080. intel_disable_pipe(intel_crtc);
  5081. i9xx_pfit_disable(intel_crtc);
  5082. for_each_encoder_on_crtc(dev, crtc, encoder)
  5083. if (encoder->post_disable)
  5084. encoder->post_disable(encoder);
  5085. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5086. if (IS_CHERRYVIEW(dev))
  5087. chv_disable_pll(dev_priv, pipe);
  5088. else if (IS_VALLEYVIEW(dev))
  5089. vlv_disable_pll(dev_priv, pipe);
  5090. else
  5091. i9xx_disable_pll(intel_crtc);
  5092. }
  5093. if (!IS_GEN2(dev))
  5094. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5095. intel_crtc->active = false;
  5096. intel_update_watermarks(crtc);
  5097. mutex_lock(&dev->struct_mutex);
  5098. intel_fbc_update(dev);
  5099. mutex_unlock(&dev->struct_mutex);
  5100. }
  5101. static void i9xx_crtc_off(struct drm_crtc *crtc)
  5102. {
  5103. }
  5104. /* Master function to enable/disable CRTC and corresponding power wells */
  5105. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5106. {
  5107. struct drm_device *dev = crtc->dev;
  5108. struct drm_i915_private *dev_priv = dev->dev_private;
  5109. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5110. enum intel_display_power_domain domain;
  5111. unsigned long domains;
  5112. if (enable) {
  5113. if (!intel_crtc->active) {
  5114. domains = get_crtc_power_domains(crtc);
  5115. for_each_power_domain(domain, domains)
  5116. intel_display_power_get(dev_priv, domain);
  5117. intel_crtc->enabled_power_domains = domains;
  5118. dev_priv->display.crtc_enable(crtc);
  5119. }
  5120. } else {
  5121. if (intel_crtc->active) {
  5122. dev_priv->display.crtc_disable(crtc);
  5123. domains = intel_crtc->enabled_power_domains;
  5124. for_each_power_domain(domain, domains)
  5125. intel_display_power_put(dev_priv, domain);
  5126. intel_crtc->enabled_power_domains = 0;
  5127. }
  5128. }
  5129. }
  5130. /**
  5131. * Sets the power management mode of the pipe and plane.
  5132. */
  5133. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5134. {
  5135. struct drm_device *dev = crtc->dev;
  5136. struct intel_encoder *intel_encoder;
  5137. bool enable = false;
  5138. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5139. enable |= intel_encoder->connectors_active;
  5140. intel_crtc_control(crtc, enable);
  5141. }
  5142. static void intel_crtc_disable(struct drm_crtc *crtc)
  5143. {
  5144. struct drm_device *dev = crtc->dev;
  5145. struct drm_connector *connector;
  5146. struct drm_i915_private *dev_priv = dev->dev_private;
  5147. /* crtc should still be enabled when we disable it. */
  5148. WARN_ON(!crtc->state->enable);
  5149. dev_priv->display.crtc_disable(crtc);
  5150. dev_priv->display.off(crtc);
  5151. drm_plane_helper_disable(crtc->primary);
  5152. /* Update computed state. */
  5153. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  5154. if (!connector->encoder || !connector->encoder->crtc)
  5155. continue;
  5156. if (connector->encoder->crtc != crtc)
  5157. continue;
  5158. connector->dpms = DRM_MODE_DPMS_OFF;
  5159. to_intel_encoder(connector->encoder)->connectors_active = false;
  5160. }
  5161. }
  5162. void intel_encoder_destroy(struct drm_encoder *encoder)
  5163. {
  5164. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5165. drm_encoder_cleanup(encoder);
  5166. kfree(intel_encoder);
  5167. }
  5168. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5169. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5170. * state of the entire output pipe. */
  5171. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5172. {
  5173. if (mode == DRM_MODE_DPMS_ON) {
  5174. encoder->connectors_active = true;
  5175. intel_crtc_update_dpms(encoder->base.crtc);
  5176. } else {
  5177. encoder->connectors_active = false;
  5178. intel_crtc_update_dpms(encoder->base.crtc);
  5179. }
  5180. }
  5181. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5182. * internal consistency). */
  5183. static void intel_connector_check_state(struct intel_connector *connector)
  5184. {
  5185. if (connector->get_hw_state(connector)) {
  5186. struct intel_encoder *encoder = connector->encoder;
  5187. struct drm_crtc *crtc;
  5188. bool encoder_enabled;
  5189. enum pipe pipe;
  5190. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5191. connector->base.base.id,
  5192. connector->base.name);
  5193. /* there is no real hw state for MST connectors */
  5194. if (connector->mst_port)
  5195. return;
  5196. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5197. "wrong connector dpms state\n");
  5198. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5199. "active connector not linked to encoder\n");
  5200. if (encoder) {
  5201. I915_STATE_WARN(!encoder->connectors_active,
  5202. "encoder->connectors_active not set\n");
  5203. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5204. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5205. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5206. return;
  5207. crtc = encoder->base.crtc;
  5208. I915_STATE_WARN(!crtc->state->enable,
  5209. "crtc not enabled\n");
  5210. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5211. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5212. "encoder active on the wrong pipe\n");
  5213. }
  5214. }
  5215. }
  5216. int intel_connector_init(struct intel_connector *connector)
  5217. {
  5218. struct drm_connector_state *connector_state;
  5219. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5220. if (!connector_state)
  5221. return -ENOMEM;
  5222. connector->base.state = connector_state;
  5223. return 0;
  5224. }
  5225. struct intel_connector *intel_connector_alloc(void)
  5226. {
  5227. struct intel_connector *connector;
  5228. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5229. if (!connector)
  5230. return NULL;
  5231. if (intel_connector_init(connector) < 0) {
  5232. kfree(connector);
  5233. return NULL;
  5234. }
  5235. return connector;
  5236. }
  5237. /* Even simpler default implementation, if there's really no special case to
  5238. * consider. */
  5239. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5240. {
  5241. /* All the simple cases only support two dpms states. */
  5242. if (mode != DRM_MODE_DPMS_ON)
  5243. mode = DRM_MODE_DPMS_OFF;
  5244. if (mode == connector->dpms)
  5245. return;
  5246. connector->dpms = mode;
  5247. /* Only need to change hw state when actually enabled */
  5248. if (connector->encoder)
  5249. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5250. intel_modeset_check_state(connector->dev);
  5251. }
  5252. /* Simple connector->get_hw_state implementation for encoders that support only
  5253. * one connector and no cloning and hence the encoder state determines the state
  5254. * of the connector. */
  5255. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5256. {
  5257. enum pipe pipe = 0;
  5258. struct intel_encoder *encoder = connector->encoder;
  5259. return encoder->get_hw_state(encoder, &pipe);
  5260. }
  5261. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5262. {
  5263. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5264. return crtc_state->fdi_lanes;
  5265. return 0;
  5266. }
  5267. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5268. struct intel_crtc_state *pipe_config)
  5269. {
  5270. struct drm_atomic_state *state = pipe_config->base.state;
  5271. struct intel_crtc *other_crtc;
  5272. struct intel_crtc_state *other_crtc_state;
  5273. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5274. pipe_name(pipe), pipe_config->fdi_lanes);
  5275. if (pipe_config->fdi_lanes > 4) {
  5276. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5277. pipe_name(pipe), pipe_config->fdi_lanes);
  5278. return -EINVAL;
  5279. }
  5280. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5281. if (pipe_config->fdi_lanes > 2) {
  5282. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5283. pipe_config->fdi_lanes);
  5284. return -EINVAL;
  5285. } else {
  5286. return 0;
  5287. }
  5288. }
  5289. if (INTEL_INFO(dev)->num_pipes == 2)
  5290. return 0;
  5291. /* Ivybridge 3 pipe is really complicated */
  5292. switch (pipe) {
  5293. case PIPE_A:
  5294. return 0;
  5295. case PIPE_B:
  5296. if (pipe_config->fdi_lanes <= 2)
  5297. return 0;
  5298. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5299. other_crtc_state =
  5300. intel_atomic_get_crtc_state(state, other_crtc);
  5301. if (IS_ERR(other_crtc_state))
  5302. return PTR_ERR(other_crtc_state);
  5303. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5304. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5305. pipe_name(pipe), pipe_config->fdi_lanes);
  5306. return -EINVAL;
  5307. }
  5308. return 0;
  5309. case PIPE_C:
  5310. if (pipe_config->fdi_lanes > 2) {
  5311. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5312. pipe_name(pipe), pipe_config->fdi_lanes);
  5313. return -EINVAL;
  5314. }
  5315. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5316. other_crtc_state =
  5317. intel_atomic_get_crtc_state(state, other_crtc);
  5318. if (IS_ERR(other_crtc_state))
  5319. return PTR_ERR(other_crtc_state);
  5320. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5321. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5322. return -EINVAL;
  5323. }
  5324. return 0;
  5325. default:
  5326. BUG();
  5327. }
  5328. }
  5329. #define RETRY 1
  5330. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5331. struct intel_crtc_state *pipe_config)
  5332. {
  5333. struct drm_device *dev = intel_crtc->base.dev;
  5334. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5335. int lane, link_bw, fdi_dotclock, ret;
  5336. bool needs_recompute = false;
  5337. retry:
  5338. /* FDI is a binary signal running at ~2.7GHz, encoding
  5339. * each output octet as 10 bits. The actual frequency
  5340. * is stored as a divider into a 100MHz clock, and the
  5341. * mode pixel clock is stored in units of 1KHz.
  5342. * Hence the bw of each lane in terms of the mode signal
  5343. * is:
  5344. */
  5345. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5346. fdi_dotclock = adjusted_mode->crtc_clock;
  5347. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5348. pipe_config->pipe_bpp);
  5349. pipe_config->fdi_lanes = lane;
  5350. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5351. link_bw, &pipe_config->fdi_m_n);
  5352. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5353. intel_crtc->pipe, pipe_config);
  5354. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5355. pipe_config->pipe_bpp -= 2*3;
  5356. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5357. pipe_config->pipe_bpp);
  5358. needs_recompute = true;
  5359. pipe_config->bw_constrained = true;
  5360. goto retry;
  5361. }
  5362. if (needs_recompute)
  5363. return RETRY;
  5364. return ret;
  5365. }
  5366. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5367. struct intel_crtc_state *pipe_config)
  5368. {
  5369. pipe_config->ips_enabled = i915.enable_ips &&
  5370. hsw_crtc_supports_ips(crtc) &&
  5371. pipe_config->pipe_bpp <= 24;
  5372. }
  5373. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5374. struct intel_crtc_state *pipe_config)
  5375. {
  5376. struct drm_device *dev = crtc->base.dev;
  5377. struct drm_i915_private *dev_priv = dev->dev_private;
  5378. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5379. int ret;
  5380. /* FIXME should check pixel clock limits on all platforms */
  5381. if (INTEL_INFO(dev)->gen < 4) {
  5382. int clock_limit =
  5383. dev_priv->display.get_display_clock_speed(dev);
  5384. /*
  5385. * Enable pixel doubling when the dot clock
  5386. * is > 90% of the (display) core speed.
  5387. *
  5388. * GDG double wide on either pipe,
  5389. * otherwise pipe A only.
  5390. */
  5391. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5392. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5393. clock_limit *= 2;
  5394. pipe_config->double_wide = true;
  5395. }
  5396. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5397. return -EINVAL;
  5398. }
  5399. /*
  5400. * Pipe horizontal size must be even in:
  5401. * - DVO ganged mode
  5402. * - LVDS dual channel mode
  5403. * - Double wide pipe
  5404. */
  5405. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5406. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5407. pipe_config->pipe_src_w &= ~1;
  5408. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5409. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5410. */
  5411. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5412. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5413. return -EINVAL;
  5414. if (HAS_IPS(dev))
  5415. hsw_compute_ips_config(crtc, pipe_config);
  5416. if (pipe_config->has_pch_encoder)
  5417. return ironlake_fdi_compute_config(crtc, pipe_config);
  5418. /* FIXME: remove below call once atomic mode set is place and all crtc
  5419. * related checks called from atomic_crtc_check function */
  5420. ret = 0;
  5421. DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
  5422. crtc, pipe_config->base.state);
  5423. ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
  5424. return ret;
  5425. }
  5426. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5427. {
  5428. struct drm_i915_private *dev_priv = to_i915(dev);
  5429. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5430. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5431. uint32_t linkrate;
  5432. if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
  5433. WARN(1, "LCPLL1 not enabled\n");
  5434. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5435. }
  5436. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5437. return 540000;
  5438. linkrate = (I915_READ(DPLL_CTRL1) &
  5439. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5440. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5441. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5442. /* vco 8640 */
  5443. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5444. case CDCLK_FREQ_450_432:
  5445. return 432000;
  5446. case CDCLK_FREQ_337_308:
  5447. return 308570;
  5448. case CDCLK_FREQ_675_617:
  5449. return 617140;
  5450. default:
  5451. WARN(1, "Unknown cd freq selection\n");
  5452. }
  5453. } else {
  5454. /* vco 8100 */
  5455. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5456. case CDCLK_FREQ_450_432:
  5457. return 450000;
  5458. case CDCLK_FREQ_337_308:
  5459. return 337500;
  5460. case CDCLK_FREQ_675_617:
  5461. return 675000;
  5462. default:
  5463. WARN(1, "Unknown cd freq selection\n");
  5464. }
  5465. }
  5466. /* error case, do as if DPLL0 isn't enabled */
  5467. return 24000;
  5468. }
  5469. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5470. {
  5471. struct drm_i915_private *dev_priv = dev->dev_private;
  5472. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5473. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5474. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5475. return 800000;
  5476. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5477. return 450000;
  5478. else if (freq == LCPLL_CLK_FREQ_450)
  5479. return 450000;
  5480. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5481. return 540000;
  5482. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5483. return 337500;
  5484. else
  5485. return 675000;
  5486. }
  5487. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5488. {
  5489. struct drm_i915_private *dev_priv = dev->dev_private;
  5490. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5491. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5492. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5493. return 800000;
  5494. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5495. return 450000;
  5496. else if (freq == LCPLL_CLK_FREQ_450)
  5497. return 450000;
  5498. else if (IS_HSW_ULT(dev))
  5499. return 337500;
  5500. else
  5501. return 540000;
  5502. }
  5503. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5504. {
  5505. struct drm_i915_private *dev_priv = dev->dev_private;
  5506. u32 val;
  5507. int divider;
  5508. if (dev_priv->hpll_freq == 0)
  5509. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5510. mutex_lock(&dev_priv->dpio_lock);
  5511. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5512. mutex_unlock(&dev_priv->dpio_lock);
  5513. divider = val & DISPLAY_FREQUENCY_VALUES;
  5514. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5515. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5516. "cdclk change in progress\n");
  5517. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5518. }
  5519. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5520. {
  5521. return 450000;
  5522. }
  5523. static int i945_get_display_clock_speed(struct drm_device *dev)
  5524. {
  5525. return 400000;
  5526. }
  5527. static int i915_get_display_clock_speed(struct drm_device *dev)
  5528. {
  5529. return 333333;
  5530. }
  5531. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5532. {
  5533. return 200000;
  5534. }
  5535. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5536. {
  5537. u16 gcfgc = 0;
  5538. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5539. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5540. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5541. return 266667;
  5542. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5543. return 333333;
  5544. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5545. return 444444;
  5546. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5547. return 200000;
  5548. default:
  5549. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5550. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5551. return 133333;
  5552. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5553. return 166667;
  5554. }
  5555. }
  5556. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5557. {
  5558. u16 gcfgc = 0;
  5559. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5560. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5561. return 133333;
  5562. else {
  5563. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5564. case GC_DISPLAY_CLOCK_333_MHZ:
  5565. return 333333;
  5566. default:
  5567. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5568. return 190000;
  5569. }
  5570. }
  5571. }
  5572. static int i865_get_display_clock_speed(struct drm_device *dev)
  5573. {
  5574. return 266667;
  5575. }
  5576. static int i855_get_display_clock_speed(struct drm_device *dev)
  5577. {
  5578. u16 hpllcc = 0;
  5579. /* Assume that the hardware is in the high speed state. This
  5580. * should be the default.
  5581. */
  5582. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5583. case GC_CLOCK_133_200:
  5584. case GC_CLOCK_100_200:
  5585. return 200000;
  5586. case GC_CLOCK_166_250:
  5587. return 250000;
  5588. case GC_CLOCK_100_133:
  5589. return 133333;
  5590. }
  5591. /* Shouldn't happen */
  5592. return 0;
  5593. }
  5594. static int i830_get_display_clock_speed(struct drm_device *dev)
  5595. {
  5596. return 133333;
  5597. }
  5598. static void
  5599. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5600. {
  5601. while (*num > DATA_LINK_M_N_MASK ||
  5602. *den > DATA_LINK_M_N_MASK) {
  5603. *num >>= 1;
  5604. *den >>= 1;
  5605. }
  5606. }
  5607. static void compute_m_n(unsigned int m, unsigned int n,
  5608. uint32_t *ret_m, uint32_t *ret_n)
  5609. {
  5610. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5611. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5612. intel_reduce_m_n_ratio(ret_m, ret_n);
  5613. }
  5614. void
  5615. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5616. int pixel_clock, int link_clock,
  5617. struct intel_link_m_n *m_n)
  5618. {
  5619. m_n->tu = 64;
  5620. compute_m_n(bits_per_pixel * pixel_clock,
  5621. link_clock * nlanes * 8,
  5622. &m_n->gmch_m, &m_n->gmch_n);
  5623. compute_m_n(pixel_clock, link_clock,
  5624. &m_n->link_m, &m_n->link_n);
  5625. }
  5626. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5627. {
  5628. if (i915.panel_use_ssc >= 0)
  5629. return i915.panel_use_ssc != 0;
  5630. return dev_priv->vbt.lvds_use_ssc
  5631. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5632. }
  5633. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5634. int num_connectors)
  5635. {
  5636. struct drm_device *dev = crtc_state->base.crtc->dev;
  5637. struct drm_i915_private *dev_priv = dev->dev_private;
  5638. int refclk;
  5639. WARN_ON(!crtc_state->base.state);
  5640. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5641. refclk = 100000;
  5642. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5643. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5644. refclk = dev_priv->vbt.lvds_ssc_freq;
  5645. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5646. } else if (!IS_GEN2(dev)) {
  5647. refclk = 96000;
  5648. } else {
  5649. refclk = 48000;
  5650. }
  5651. return refclk;
  5652. }
  5653. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5654. {
  5655. return (1 << dpll->n) << 16 | dpll->m2;
  5656. }
  5657. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5658. {
  5659. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5660. }
  5661. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5662. struct intel_crtc_state *crtc_state,
  5663. intel_clock_t *reduced_clock)
  5664. {
  5665. struct drm_device *dev = crtc->base.dev;
  5666. u32 fp, fp2 = 0;
  5667. if (IS_PINEVIEW(dev)) {
  5668. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5669. if (reduced_clock)
  5670. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5671. } else {
  5672. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5673. if (reduced_clock)
  5674. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5675. }
  5676. crtc_state->dpll_hw_state.fp0 = fp;
  5677. crtc->lowfreq_avail = false;
  5678. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5679. reduced_clock) {
  5680. crtc_state->dpll_hw_state.fp1 = fp2;
  5681. crtc->lowfreq_avail = true;
  5682. } else {
  5683. crtc_state->dpll_hw_state.fp1 = fp;
  5684. }
  5685. }
  5686. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5687. pipe)
  5688. {
  5689. u32 reg_val;
  5690. /*
  5691. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5692. * and set it to a reasonable value instead.
  5693. */
  5694. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5695. reg_val &= 0xffffff00;
  5696. reg_val |= 0x00000030;
  5697. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5698. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5699. reg_val &= 0x8cffffff;
  5700. reg_val = 0x8c000000;
  5701. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5702. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5703. reg_val &= 0xffffff00;
  5704. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5705. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5706. reg_val &= 0x00ffffff;
  5707. reg_val |= 0xb0000000;
  5708. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5709. }
  5710. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5711. struct intel_link_m_n *m_n)
  5712. {
  5713. struct drm_device *dev = crtc->base.dev;
  5714. struct drm_i915_private *dev_priv = dev->dev_private;
  5715. int pipe = crtc->pipe;
  5716. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5717. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5718. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5719. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5720. }
  5721. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5722. struct intel_link_m_n *m_n,
  5723. struct intel_link_m_n *m2_n2)
  5724. {
  5725. struct drm_device *dev = crtc->base.dev;
  5726. struct drm_i915_private *dev_priv = dev->dev_private;
  5727. int pipe = crtc->pipe;
  5728. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5729. if (INTEL_INFO(dev)->gen >= 5) {
  5730. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5731. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5732. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5733. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5734. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5735. * for gen < 8) and if DRRS is supported (to make sure the
  5736. * registers are not unnecessarily accessed).
  5737. */
  5738. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  5739. crtc->config->has_drrs) {
  5740. I915_WRITE(PIPE_DATA_M2(transcoder),
  5741. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5742. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5743. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5744. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5745. }
  5746. } else {
  5747. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5748. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5749. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5750. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5751. }
  5752. }
  5753. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5754. {
  5755. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5756. if (m_n == M1_N1) {
  5757. dp_m_n = &crtc->config->dp_m_n;
  5758. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5759. } else if (m_n == M2_N2) {
  5760. /*
  5761. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5762. * needs to be programmed into M1_N1.
  5763. */
  5764. dp_m_n = &crtc->config->dp_m2_n2;
  5765. } else {
  5766. DRM_ERROR("Unsupported divider value\n");
  5767. return;
  5768. }
  5769. if (crtc->config->has_pch_encoder)
  5770. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5771. else
  5772. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5773. }
  5774. static void vlv_update_pll(struct intel_crtc *crtc,
  5775. struct intel_crtc_state *pipe_config)
  5776. {
  5777. u32 dpll, dpll_md;
  5778. /*
  5779. * Enable DPIO clock input. We should never disable the reference
  5780. * clock for pipe B, since VGA hotplug / manual detection depends
  5781. * on it.
  5782. */
  5783. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  5784. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  5785. /* We should never disable this, set it here for state tracking */
  5786. if (crtc->pipe == PIPE_B)
  5787. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5788. dpll |= DPLL_VCO_ENABLE;
  5789. pipe_config->dpll_hw_state.dpll = dpll;
  5790. dpll_md = (pipe_config->pixel_multiplier - 1)
  5791. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5792. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  5793. }
  5794. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5795. const struct intel_crtc_state *pipe_config)
  5796. {
  5797. struct drm_device *dev = crtc->base.dev;
  5798. struct drm_i915_private *dev_priv = dev->dev_private;
  5799. int pipe = crtc->pipe;
  5800. u32 mdiv;
  5801. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5802. u32 coreclk, reg_val;
  5803. mutex_lock(&dev_priv->dpio_lock);
  5804. bestn = pipe_config->dpll.n;
  5805. bestm1 = pipe_config->dpll.m1;
  5806. bestm2 = pipe_config->dpll.m2;
  5807. bestp1 = pipe_config->dpll.p1;
  5808. bestp2 = pipe_config->dpll.p2;
  5809. /* See eDP HDMI DPIO driver vbios notes doc */
  5810. /* PLL B needs special handling */
  5811. if (pipe == PIPE_B)
  5812. vlv_pllb_recal_opamp(dev_priv, pipe);
  5813. /* Set up Tx target for periodic Rcomp update */
  5814. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5815. /* Disable target IRef on PLL */
  5816. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5817. reg_val &= 0x00ffffff;
  5818. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5819. /* Disable fast lock */
  5820. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5821. /* Set idtafcrecal before PLL is enabled */
  5822. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5823. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5824. mdiv |= ((bestn << DPIO_N_SHIFT));
  5825. mdiv |= (1 << DPIO_K_SHIFT);
  5826. /*
  5827. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5828. * but we don't support that).
  5829. * Note: don't use the DAC post divider as it seems unstable.
  5830. */
  5831. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5832. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5833. mdiv |= DPIO_ENABLE_CALIBRATION;
  5834. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5835. /* Set HBR and RBR LPF coefficients */
  5836. if (pipe_config->port_clock == 162000 ||
  5837. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  5838. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  5839. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5840. 0x009f0003);
  5841. else
  5842. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5843. 0x00d0000f);
  5844. if (pipe_config->has_dp_encoder) {
  5845. /* Use SSC source */
  5846. if (pipe == PIPE_A)
  5847. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5848. 0x0df40000);
  5849. else
  5850. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5851. 0x0df70000);
  5852. } else { /* HDMI or VGA */
  5853. /* Use bend source */
  5854. if (pipe == PIPE_A)
  5855. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5856. 0x0df70000);
  5857. else
  5858. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5859. 0x0df40000);
  5860. }
  5861. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5862. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5863. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5864. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5865. coreclk |= 0x01000000;
  5866. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5867. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5868. mutex_unlock(&dev_priv->dpio_lock);
  5869. }
  5870. static void chv_update_pll(struct intel_crtc *crtc,
  5871. struct intel_crtc_state *pipe_config)
  5872. {
  5873. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5874. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5875. DPLL_VCO_ENABLE;
  5876. if (crtc->pipe != PIPE_A)
  5877. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5878. pipe_config->dpll_hw_state.dpll_md =
  5879. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5880. }
  5881. static void chv_prepare_pll(struct intel_crtc *crtc,
  5882. const struct intel_crtc_state *pipe_config)
  5883. {
  5884. struct drm_device *dev = crtc->base.dev;
  5885. struct drm_i915_private *dev_priv = dev->dev_private;
  5886. int pipe = crtc->pipe;
  5887. int dpll_reg = DPLL(crtc->pipe);
  5888. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5889. u32 loopfilter, tribuf_calcntr;
  5890. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5891. u32 dpio_val;
  5892. int vco;
  5893. bestn = pipe_config->dpll.n;
  5894. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5895. bestm1 = pipe_config->dpll.m1;
  5896. bestm2 = pipe_config->dpll.m2 >> 22;
  5897. bestp1 = pipe_config->dpll.p1;
  5898. bestp2 = pipe_config->dpll.p2;
  5899. vco = pipe_config->dpll.vco;
  5900. dpio_val = 0;
  5901. loopfilter = 0;
  5902. /*
  5903. * Enable Refclk and SSC
  5904. */
  5905. I915_WRITE(dpll_reg,
  5906. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5907. mutex_lock(&dev_priv->dpio_lock);
  5908. /* p1 and p2 divider */
  5909. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5910. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5911. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5912. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5913. 1 << DPIO_CHV_K_DIV_SHIFT);
  5914. /* Feedback post-divider - m2 */
  5915. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5916. /* Feedback refclk divider - n and m1 */
  5917. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5918. DPIO_CHV_M1_DIV_BY_2 |
  5919. 1 << DPIO_CHV_N_DIV_SHIFT);
  5920. /* M2 fraction division */
  5921. if (bestm2_frac)
  5922. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5923. /* M2 fraction division enable */
  5924. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5925. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5926. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5927. if (bestm2_frac)
  5928. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5929. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5930. /* Program digital lock detect threshold */
  5931. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5932. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5933. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5934. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5935. if (!bestm2_frac)
  5936. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5937. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5938. /* Loop filter */
  5939. if (vco == 5400000) {
  5940. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5941. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5942. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5943. tribuf_calcntr = 0x9;
  5944. } else if (vco <= 6200000) {
  5945. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5946. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5947. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5948. tribuf_calcntr = 0x9;
  5949. } else if (vco <= 6480000) {
  5950. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5951. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5952. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5953. tribuf_calcntr = 0x8;
  5954. } else {
  5955. /* Not supported. Apply the same limits as in the max case */
  5956. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5957. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5958. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5959. tribuf_calcntr = 0;
  5960. }
  5961. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5962. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5963. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5964. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5965. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5966. /* AFC Recal */
  5967. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5968. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5969. DPIO_AFC_RECAL);
  5970. mutex_unlock(&dev_priv->dpio_lock);
  5971. }
  5972. /**
  5973. * vlv_force_pll_on - forcibly enable just the PLL
  5974. * @dev_priv: i915 private structure
  5975. * @pipe: pipe PLL to enable
  5976. * @dpll: PLL configuration
  5977. *
  5978. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5979. * in cases where we need the PLL enabled even when @pipe is not going to
  5980. * be enabled.
  5981. */
  5982. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5983. const struct dpll *dpll)
  5984. {
  5985. struct intel_crtc *crtc =
  5986. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5987. struct intel_crtc_state pipe_config = {
  5988. .base.crtc = &crtc->base,
  5989. .pixel_multiplier = 1,
  5990. .dpll = *dpll,
  5991. };
  5992. if (IS_CHERRYVIEW(dev)) {
  5993. chv_update_pll(crtc, &pipe_config);
  5994. chv_prepare_pll(crtc, &pipe_config);
  5995. chv_enable_pll(crtc, &pipe_config);
  5996. } else {
  5997. vlv_update_pll(crtc, &pipe_config);
  5998. vlv_prepare_pll(crtc, &pipe_config);
  5999. vlv_enable_pll(crtc, &pipe_config);
  6000. }
  6001. }
  6002. /**
  6003. * vlv_force_pll_off - forcibly disable just the PLL
  6004. * @dev_priv: i915 private structure
  6005. * @pipe: pipe PLL to disable
  6006. *
  6007. * Disable the PLL for @pipe. To be used in cases where we need
  6008. * the PLL enabled even when @pipe is not going to be enabled.
  6009. */
  6010. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6011. {
  6012. if (IS_CHERRYVIEW(dev))
  6013. chv_disable_pll(to_i915(dev), pipe);
  6014. else
  6015. vlv_disable_pll(to_i915(dev), pipe);
  6016. }
  6017. static void i9xx_update_pll(struct intel_crtc *crtc,
  6018. struct intel_crtc_state *crtc_state,
  6019. intel_clock_t *reduced_clock,
  6020. int num_connectors)
  6021. {
  6022. struct drm_device *dev = crtc->base.dev;
  6023. struct drm_i915_private *dev_priv = dev->dev_private;
  6024. u32 dpll;
  6025. bool is_sdvo;
  6026. struct dpll *clock = &crtc_state->dpll;
  6027. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6028. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6029. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6030. dpll = DPLL_VGA_MODE_DIS;
  6031. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6032. dpll |= DPLLB_MODE_LVDS;
  6033. else
  6034. dpll |= DPLLB_MODE_DAC_SERIAL;
  6035. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6036. dpll |= (crtc_state->pixel_multiplier - 1)
  6037. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6038. }
  6039. if (is_sdvo)
  6040. dpll |= DPLL_SDVO_HIGH_SPEED;
  6041. if (crtc_state->has_dp_encoder)
  6042. dpll |= DPLL_SDVO_HIGH_SPEED;
  6043. /* compute bitmask from p1 value */
  6044. if (IS_PINEVIEW(dev))
  6045. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6046. else {
  6047. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6048. if (IS_G4X(dev) && reduced_clock)
  6049. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6050. }
  6051. switch (clock->p2) {
  6052. case 5:
  6053. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6054. break;
  6055. case 7:
  6056. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6057. break;
  6058. case 10:
  6059. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6060. break;
  6061. case 14:
  6062. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6063. break;
  6064. }
  6065. if (INTEL_INFO(dev)->gen >= 4)
  6066. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6067. if (crtc_state->sdvo_tv_clock)
  6068. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6069. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6070. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6071. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6072. else
  6073. dpll |= PLL_REF_INPUT_DREFCLK;
  6074. dpll |= DPLL_VCO_ENABLE;
  6075. crtc_state->dpll_hw_state.dpll = dpll;
  6076. if (INTEL_INFO(dev)->gen >= 4) {
  6077. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6078. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6079. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6080. }
  6081. }
  6082. static void i8xx_update_pll(struct intel_crtc *crtc,
  6083. struct intel_crtc_state *crtc_state,
  6084. intel_clock_t *reduced_clock,
  6085. int num_connectors)
  6086. {
  6087. struct drm_device *dev = crtc->base.dev;
  6088. struct drm_i915_private *dev_priv = dev->dev_private;
  6089. u32 dpll;
  6090. struct dpll *clock = &crtc_state->dpll;
  6091. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6092. dpll = DPLL_VGA_MODE_DIS;
  6093. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6094. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6095. } else {
  6096. if (clock->p1 == 2)
  6097. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6098. else
  6099. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6100. if (clock->p2 == 4)
  6101. dpll |= PLL_P2_DIVIDE_BY_4;
  6102. }
  6103. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6104. dpll |= DPLL_DVO_2X_MODE;
  6105. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6106. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6107. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6108. else
  6109. dpll |= PLL_REF_INPUT_DREFCLK;
  6110. dpll |= DPLL_VCO_ENABLE;
  6111. crtc_state->dpll_hw_state.dpll = dpll;
  6112. }
  6113. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6114. {
  6115. struct drm_device *dev = intel_crtc->base.dev;
  6116. struct drm_i915_private *dev_priv = dev->dev_private;
  6117. enum pipe pipe = intel_crtc->pipe;
  6118. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6119. struct drm_display_mode *adjusted_mode =
  6120. &intel_crtc->config->base.adjusted_mode;
  6121. uint32_t crtc_vtotal, crtc_vblank_end;
  6122. int vsyncshift = 0;
  6123. /* We need to be careful not to changed the adjusted mode, for otherwise
  6124. * the hw state checker will get angry at the mismatch. */
  6125. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6126. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6127. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6128. /* the chip adds 2 halflines automatically */
  6129. crtc_vtotal -= 1;
  6130. crtc_vblank_end -= 1;
  6131. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6132. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6133. else
  6134. vsyncshift = adjusted_mode->crtc_hsync_start -
  6135. adjusted_mode->crtc_htotal / 2;
  6136. if (vsyncshift < 0)
  6137. vsyncshift += adjusted_mode->crtc_htotal;
  6138. }
  6139. if (INTEL_INFO(dev)->gen > 3)
  6140. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6141. I915_WRITE(HTOTAL(cpu_transcoder),
  6142. (adjusted_mode->crtc_hdisplay - 1) |
  6143. ((adjusted_mode->crtc_htotal - 1) << 16));
  6144. I915_WRITE(HBLANK(cpu_transcoder),
  6145. (adjusted_mode->crtc_hblank_start - 1) |
  6146. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6147. I915_WRITE(HSYNC(cpu_transcoder),
  6148. (adjusted_mode->crtc_hsync_start - 1) |
  6149. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6150. I915_WRITE(VTOTAL(cpu_transcoder),
  6151. (adjusted_mode->crtc_vdisplay - 1) |
  6152. ((crtc_vtotal - 1) << 16));
  6153. I915_WRITE(VBLANK(cpu_transcoder),
  6154. (adjusted_mode->crtc_vblank_start - 1) |
  6155. ((crtc_vblank_end - 1) << 16));
  6156. I915_WRITE(VSYNC(cpu_transcoder),
  6157. (adjusted_mode->crtc_vsync_start - 1) |
  6158. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6159. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6160. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6161. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6162. * bits. */
  6163. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6164. (pipe == PIPE_B || pipe == PIPE_C))
  6165. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6166. /* pipesrc controls the size that is scaled from, which should
  6167. * always be the user's requested size.
  6168. */
  6169. I915_WRITE(PIPESRC(pipe),
  6170. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6171. (intel_crtc->config->pipe_src_h - 1));
  6172. }
  6173. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6174. struct intel_crtc_state *pipe_config)
  6175. {
  6176. struct drm_device *dev = crtc->base.dev;
  6177. struct drm_i915_private *dev_priv = dev->dev_private;
  6178. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6179. uint32_t tmp;
  6180. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6181. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6182. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6183. tmp = I915_READ(HBLANK(cpu_transcoder));
  6184. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6185. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6186. tmp = I915_READ(HSYNC(cpu_transcoder));
  6187. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6188. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6189. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6190. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6191. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6192. tmp = I915_READ(VBLANK(cpu_transcoder));
  6193. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6194. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6195. tmp = I915_READ(VSYNC(cpu_transcoder));
  6196. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6197. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6198. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6199. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6200. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6201. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6202. }
  6203. tmp = I915_READ(PIPESRC(crtc->pipe));
  6204. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6205. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6206. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6207. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6208. }
  6209. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6210. struct intel_crtc_state *pipe_config)
  6211. {
  6212. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6213. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6214. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6215. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6216. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6217. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6218. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6219. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6220. mode->flags = pipe_config->base.adjusted_mode.flags;
  6221. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6222. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6223. }
  6224. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6225. {
  6226. struct drm_device *dev = intel_crtc->base.dev;
  6227. struct drm_i915_private *dev_priv = dev->dev_private;
  6228. uint32_t pipeconf;
  6229. pipeconf = 0;
  6230. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6231. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6232. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6233. if (intel_crtc->config->double_wide)
  6234. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6235. /* only g4x and later have fancy bpc/dither controls */
  6236. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6237. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6238. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6239. pipeconf |= PIPECONF_DITHER_EN |
  6240. PIPECONF_DITHER_TYPE_SP;
  6241. switch (intel_crtc->config->pipe_bpp) {
  6242. case 18:
  6243. pipeconf |= PIPECONF_6BPC;
  6244. break;
  6245. case 24:
  6246. pipeconf |= PIPECONF_8BPC;
  6247. break;
  6248. case 30:
  6249. pipeconf |= PIPECONF_10BPC;
  6250. break;
  6251. default:
  6252. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6253. BUG();
  6254. }
  6255. }
  6256. if (HAS_PIPE_CXSR(dev)) {
  6257. if (intel_crtc->lowfreq_avail) {
  6258. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6259. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6260. } else {
  6261. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6262. }
  6263. }
  6264. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6265. if (INTEL_INFO(dev)->gen < 4 ||
  6266. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6267. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6268. else
  6269. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6270. } else
  6271. pipeconf |= PIPECONF_PROGRESSIVE;
  6272. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6273. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6274. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6275. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6276. }
  6277. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6278. struct intel_crtc_state *crtc_state)
  6279. {
  6280. struct drm_device *dev = crtc->base.dev;
  6281. struct drm_i915_private *dev_priv = dev->dev_private;
  6282. int refclk, num_connectors = 0;
  6283. intel_clock_t clock, reduced_clock;
  6284. bool ok, has_reduced_clock = false;
  6285. bool is_lvds = false, is_dsi = false;
  6286. struct intel_encoder *encoder;
  6287. const intel_limit_t *limit;
  6288. struct drm_atomic_state *state = crtc_state->base.state;
  6289. struct drm_connector_state *connector_state;
  6290. int i;
  6291. for (i = 0; i < state->num_connector; i++) {
  6292. if (!state->connectors[i])
  6293. continue;
  6294. connector_state = state->connector_states[i];
  6295. if (connector_state->crtc != &crtc->base)
  6296. continue;
  6297. encoder = to_intel_encoder(connector_state->best_encoder);
  6298. switch (encoder->type) {
  6299. case INTEL_OUTPUT_LVDS:
  6300. is_lvds = true;
  6301. break;
  6302. case INTEL_OUTPUT_DSI:
  6303. is_dsi = true;
  6304. break;
  6305. default:
  6306. break;
  6307. }
  6308. num_connectors++;
  6309. }
  6310. if (is_dsi)
  6311. return 0;
  6312. if (!crtc_state->clock_set) {
  6313. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6314. /*
  6315. * Returns a set of divisors for the desired target clock with
  6316. * the given refclk, or FALSE. The returned values represent
  6317. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6318. * 2) / p1 / p2.
  6319. */
  6320. limit = intel_limit(crtc_state, refclk);
  6321. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6322. crtc_state->port_clock,
  6323. refclk, NULL, &clock);
  6324. if (!ok) {
  6325. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6326. return -EINVAL;
  6327. }
  6328. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6329. /*
  6330. * Ensure we match the reduced clock's P to the target
  6331. * clock. If the clocks don't match, we can't switch
  6332. * the display clock by using the FP0/FP1. In such case
  6333. * we will disable the LVDS downclock feature.
  6334. */
  6335. has_reduced_clock =
  6336. dev_priv->display.find_dpll(limit, crtc_state,
  6337. dev_priv->lvds_downclock,
  6338. refclk, &clock,
  6339. &reduced_clock);
  6340. }
  6341. /* Compat-code for transition, will disappear. */
  6342. crtc_state->dpll.n = clock.n;
  6343. crtc_state->dpll.m1 = clock.m1;
  6344. crtc_state->dpll.m2 = clock.m2;
  6345. crtc_state->dpll.p1 = clock.p1;
  6346. crtc_state->dpll.p2 = clock.p2;
  6347. }
  6348. if (IS_GEN2(dev)) {
  6349. i8xx_update_pll(crtc, crtc_state,
  6350. has_reduced_clock ? &reduced_clock : NULL,
  6351. num_connectors);
  6352. } else if (IS_CHERRYVIEW(dev)) {
  6353. chv_update_pll(crtc, crtc_state);
  6354. } else if (IS_VALLEYVIEW(dev)) {
  6355. vlv_update_pll(crtc, crtc_state);
  6356. } else {
  6357. i9xx_update_pll(crtc, crtc_state,
  6358. has_reduced_clock ? &reduced_clock : NULL,
  6359. num_connectors);
  6360. }
  6361. return 0;
  6362. }
  6363. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6364. struct intel_crtc_state *pipe_config)
  6365. {
  6366. struct drm_device *dev = crtc->base.dev;
  6367. struct drm_i915_private *dev_priv = dev->dev_private;
  6368. uint32_t tmp;
  6369. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6370. return;
  6371. tmp = I915_READ(PFIT_CONTROL);
  6372. if (!(tmp & PFIT_ENABLE))
  6373. return;
  6374. /* Check whether the pfit is attached to our pipe. */
  6375. if (INTEL_INFO(dev)->gen < 4) {
  6376. if (crtc->pipe != PIPE_B)
  6377. return;
  6378. } else {
  6379. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6380. return;
  6381. }
  6382. pipe_config->gmch_pfit.control = tmp;
  6383. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6384. if (INTEL_INFO(dev)->gen < 5)
  6385. pipe_config->gmch_pfit.lvds_border_bits =
  6386. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6387. }
  6388. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6389. struct intel_crtc_state *pipe_config)
  6390. {
  6391. struct drm_device *dev = crtc->base.dev;
  6392. struct drm_i915_private *dev_priv = dev->dev_private;
  6393. int pipe = pipe_config->cpu_transcoder;
  6394. intel_clock_t clock;
  6395. u32 mdiv;
  6396. int refclk = 100000;
  6397. /* In case of MIPI DPLL will not even be used */
  6398. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6399. return;
  6400. mutex_lock(&dev_priv->dpio_lock);
  6401. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6402. mutex_unlock(&dev_priv->dpio_lock);
  6403. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6404. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6405. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6406. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6407. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6408. vlv_clock(refclk, &clock);
  6409. /* clock.dot is the fast clock */
  6410. pipe_config->port_clock = clock.dot / 5;
  6411. }
  6412. static void
  6413. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6414. struct intel_initial_plane_config *plane_config)
  6415. {
  6416. struct drm_device *dev = crtc->base.dev;
  6417. struct drm_i915_private *dev_priv = dev->dev_private;
  6418. u32 val, base, offset;
  6419. int pipe = crtc->pipe, plane = crtc->plane;
  6420. int fourcc, pixel_format;
  6421. unsigned int aligned_height;
  6422. struct drm_framebuffer *fb;
  6423. struct intel_framebuffer *intel_fb;
  6424. val = I915_READ(DSPCNTR(plane));
  6425. if (!(val & DISPLAY_PLANE_ENABLE))
  6426. return;
  6427. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6428. if (!intel_fb) {
  6429. DRM_DEBUG_KMS("failed to alloc fb\n");
  6430. return;
  6431. }
  6432. fb = &intel_fb->base;
  6433. if (INTEL_INFO(dev)->gen >= 4) {
  6434. if (val & DISPPLANE_TILED) {
  6435. plane_config->tiling = I915_TILING_X;
  6436. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6437. }
  6438. }
  6439. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6440. fourcc = i9xx_format_to_fourcc(pixel_format);
  6441. fb->pixel_format = fourcc;
  6442. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6443. if (INTEL_INFO(dev)->gen >= 4) {
  6444. if (plane_config->tiling)
  6445. offset = I915_READ(DSPTILEOFF(plane));
  6446. else
  6447. offset = I915_READ(DSPLINOFF(plane));
  6448. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6449. } else {
  6450. base = I915_READ(DSPADDR(plane));
  6451. }
  6452. plane_config->base = base;
  6453. val = I915_READ(PIPESRC(pipe));
  6454. fb->width = ((val >> 16) & 0xfff) + 1;
  6455. fb->height = ((val >> 0) & 0xfff) + 1;
  6456. val = I915_READ(DSPSTRIDE(pipe));
  6457. fb->pitches[0] = val & 0xffffffc0;
  6458. aligned_height = intel_fb_align_height(dev, fb->height,
  6459. fb->pixel_format,
  6460. fb->modifier[0]);
  6461. plane_config->size = fb->pitches[0] * aligned_height;
  6462. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6463. pipe_name(pipe), plane, fb->width, fb->height,
  6464. fb->bits_per_pixel, base, fb->pitches[0],
  6465. plane_config->size);
  6466. plane_config->fb = intel_fb;
  6467. }
  6468. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6469. struct intel_crtc_state *pipe_config)
  6470. {
  6471. struct drm_device *dev = crtc->base.dev;
  6472. struct drm_i915_private *dev_priv = dev->dev_private;
  6473. int pipe = pipe_config->cpu_transcoder;
  6474. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6475. intel_clock_t clock;
  6476. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6477. int refclk = 100000;
  6478. mutex_lock(&dev_priv->dpio_lock);
  6479. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6480. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6481. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6482. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6483. mutex_unlock(&dev_priv->dpio_lock);
  6484. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6485. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6486. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6487. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6488. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6489. chv_clock(refclk, &clock);
  6490. /* clock.dot is the fast clock */
  6491. pipe_config->port_clock = clock.dot / 5;
  6492. }
  6493. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6494. struct intel_crtc_state *pipe_config)
  6495. {
  6496. struct drm_device *dev = crtc->base.dev;
  6497. struct drm_i915_private *dev_priv = dev->dev_private;
  6498. uint32_t tmp;
  6499. if (!intel_display_power_is_enabled(dev_priv,
  6500. POWER_DOMAIN_PIPE(crtc->pipe)))
  6501. return false;
  6502. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6503. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6504. tmp = I915_READ(PIPECONF(crtc->pipe));
  6505. if (!(tmp & PIPECONF_ENABLE))
  6506. return false;
  6507. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6508. switch (tmp & PIPECONF_BPC_MASK) {
  6509. case PIPECONF_6BPC:
  6510. pipe_config->pipe_bpp = 18;
  6511. break;
  6512. case PIPECONF_8BPC:
  6513. pipe_config->pipe_bpp = 24;
  6514. break;
  6515. case PIPECONF_10BPC:
  6516. pipe_config->pipe_bpp = 30;
  6517. break;
  6518. default:
  6519. break;
  6520. }
  6521. }
  6522. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6523. pipe_config->limited_color_range = true;
  6524. if (INTEL_INFO(dev)->gen < 4)
  6525. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6526. intel_get_pipe_timings(crtc, pipe_config);
  6527. i9xx_get_pfit_config(crtc, pipe_config);
  6528. if (INTEL_INFO(dev)->gen >= 4) {
  6529. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6530. pipe_config->pixel_multiplier =
  6531. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6532. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6533. pipe_config->dpll_hw_state.dpll_md = tmp;
  6534. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6535. tmp = I915_READ(DPLL(crtc->pipe));
  6536. pipe_config->pixel_multiplier =
  6537. ((tmp & SDVO_MULTIPLIER_MASK)
  6538. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6539. } else {
  6540. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6541. * port and will be fixed up in the encoder->get_config
  6542. * function. */
  6543. pipe_config->pixel_multiplier = 1;
  6544. }
  6545. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6546. if (!IS_VALLEYVIEW(dev)) {
  6547. /*
  6548. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6549. * on 830. Filter it out here so that we don't
  6550. * report errors due to that.
  6551. */
  6552. if (IS_I830(dev))
  6553. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6554. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6555. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6556. } else {
  6557. /* Mask out read-only status bits. */
  6558. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6559. DPLL_PORTC_READY_MASK |
  6560. DPLL_PORTB_READY_MASK);
  6561. }
  6562. if (IS_CHERRYVIEW(dev))
  6563. chv_crtc_clock_get(crtc, pipe_config);
  6564. else if (IS_VALLEYVIEW(dev))
  6565. vlv_crtc_clock_get(crtc, pipe_config);
  6566. else
  6567. i9xx_crtc_clock_get(crtc, pipe_config);
  6568. return true;
  6569. }
  6570. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6571. {
  6572. struct drm_i915_private *dev_priv = dev->dev_private;
  6573. struct intel_encoder *encoder;
  6574. u32 val, final;
  6575. bool has_lvds = false;
  6576. bool has_cpu_edp = false;
  6577. bool has_panel = false;
  6578. bool has_ck505 = false;
  6579. bool can_ssc = false;
  6580. /* We need to take the global config into account */
  6581. for_each_intel_encoder(dev, encoder) {
  6582. switch (encoder->type) {
  6583. case INTEL_OUTPUT_LVDS:
  6584. has_panel = true;
  6585. has_lvds = true;
  6586. break;
  6587. case INTEL_OUTPUT_EDP:
  6588. has_panel = true;
  6589. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6590. has_cpu_edp = true;
  6591. break;
  6592. default:
  6593. break;
  6594. }
  6595. }
  6596. if (HAS_PCH_IBX(dev)) {
  6597. has_ck505 = dev_priv->vbt.display_clock_mode;
  6598. can_ssc = has_ck505;
  6599. } else {
  6600. has_ck505 = false;
  6601. can_ssc = true;
  6602. }
  6603. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6604. has_panel, has_lvds, has_ck505);
  6605. /* Ironlake: try to setup display ref clock before DPLL
  6606. * enabling. This is only under driver's control after
  6607. * PCH B stepping, previous chipset stepping should be
  6608. * ignoring this setting.
  6609. */
  6610. val = I915_READ(PCH_DREF_CONTROL);
  6611. /* As we must carefully and slowly disable/enable each source in turn,
  6612. * compute the final state we want first and check if we need to
  6613. * make any changes at all.
  6614. */
  6615. final = val;
  6616. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6617. if (has_ck505)
  6618. final |= DREF_NONSPREAD_CK505_ENABLE;
  6619. else
  6620. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6621. final &= ~DREF_SSC_SOURCE_MASK;
  6622. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6623. final &= ~DREF_SSC1_ENABLE;
  6624. if (has_panel) {
  6625. final |= DREF_SSC_SOURCE_ENABLE;
  6626. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6627. final |= DREF_SSC1_ENABLE;
  6628. if (has_cpu_edp) {
  6629. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6630. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6631. else
  6632. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6633. } else
  6634. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6635. } else {
  6636. final |= DREF_SSC_SOURCE_DISABLE;
  6637. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6638. }
  6639. if (final == val)
  6640. return;
  6641. /* Always enable nonspread source */
  6642. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6643. if (has_ck505)
  6644. val |= DREF_NONSPREAD_CK505_ENABLE;
  6645. else
  6646. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6647. if (has_panel) {
  6648. val &= ~DREF_SSC_SOURCE_MASK;
  6649. val |= DREF_SSC_SOURCE_ENABLE;
  6650. /* SSC must be turned on before enabling the CPU output */
  6651. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6652. DRM_DEBUG_KMS("Using SSC on panel\n");
  6653. val |= DREF_SSC1_ENABLE;
  6654. } else
  6655. val &= ~DREF_SSC1_ENABLE;
  6656. /* Get SSC going before enabling the outputs */
  6657. I915_WRITE(PCH_DREF_CONTROL, val);
  6658. POSTING_READ(PCH_DREF_CONTROL);
  6659. udelay(200);
  6660. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6661. /* Enable CPU source on CPU attached eDP */
  6662. if (has_cpu_edp) {
  6663. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6664. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6665. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6666. } else
  6667. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6668. } else
  6669. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6670. I915_WRITE(PCH_DREF_CONTROL, val);
  6671. POSTING_READ(PCH_DREF_CONTROL);
  6672. udelay(200);
  6673. } else {
  6674. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6675. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6676. /* Turn off CPU output */
  6677. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6678. I915_WRITE(PCH_DREF_CONTROL, val);
  6679. POSTING_READ(PCH_DREF_CONTROL);
  6680. udelay(200);
  6681. /* Turn off the SSC source */
  6682. val &= ~DREF_SSC_SOURCE_MASK;
  6683. val |= DREF_SSC_SOURCE_DISABLE;
  6684. /* Turn off SSC1 */
  6685. val &= ~DREF_SSC1_ENABLE;
  6686. I915_WRITE(PCH_DREF_CONTROL, val);
  6687. POSTING_READ(PCH_DREF_CONTROL);
  6688. udelay(200);
  6689. }
  6690. BUG_ON(val != final);
  6691. }
  6692. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6693. {
  6694. uint32_t tmp;
  6695. tmp = I915_READ(SOUTH_CHICKEN2);
  6696. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6697. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6698. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6699. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6700. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6701. tmp = I915_READ(SOUTH_CHICKEN2);
  6702. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6703. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6704. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6705. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6706. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6707. }
  6708. /* WaMPhyProgramming:hsw */
  6709. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6710. {
  6711. uint32_t tmp;
  6712. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6713. tmp &= ~(0xFF << 24);
  6714. tmp |= (0x12 << 24);
  6715. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6716. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6717. tmp |= (1 << 11);
  6718. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6719. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6720. tmp |= (1 << 11);
  6721. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6722. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6723. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6724. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6725. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6726. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6727. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6728. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6729. tmp &= ~(7 << 13);
  6730. tmp |= (5 << 13);
  6731. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6732. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6733. tmp &= ~(7 << 13);
  6734. tmp |= (5 << 13);
  6735. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6736. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6737. tmp &= ~0xFF;
  6738. tmp |= 0x1C;
  6739. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6740. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6741. tmp &= ~0xFF;
  6742. tmp |= 0x1C;
  6743. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6744. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6745. tmp &= ~(0xFF << 16);
  6746. tmp |= (0x1C << 16);
  6747. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6748. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6749. tmp &= ~(0xFF << 16);
  6750. tmp |= (0x1C << 16);
  6751. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6752. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6753. tmp |= (1 << 27);
  6754. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6755. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6756. tmp |= (1 << 27);
  6757. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6758. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6759. tmp &= ~(0xF << 28);
  6760. tmp |= (4 << 28);
  6761. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6762. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6763. tmp &= ~(0xF << 28);
  6764. tmp |= (4 << 28);
  6765. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6766. }
  6767. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6768. * Programming" based on the parameters passed:
  6769. * - Sequence to enable CLKOUT_DP
  6770. * - Sequence to enable CLKOUT_DP without spread
  6771. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6772. */
  6773. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  6774. bool with_fdi)
  6775. {
  6776. struct drm_i915_private *dev_priv = dev->dev_private;
  6777. uint32_t reg, tmp;
  6778. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6779. with_spread = true;
  6780. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  6781. with_fdi, "LP PCH doesn't have FDI\n"))
  6782. with_fdi = false;
  6783. mutex_lock(&dev_priv->dpio_lock);
  6784. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6785. tmp &= ~SBI_SSCCTL_DISABLE;
  6786. tmp |= SBI_SSCCTL_PATHALT;
  6787. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6788. udelay(24);
  6789. if (with_spread) {
  6790. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6791. tmp &= ~SBI_SSCCTL_PATHALT;
  6792. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6793. if (with_fdi) {
  6794. lpt_reset_fdi_mphy(dev_priv);
  6795. lpt_program_fdi_mphy(dev_priv);
  6796. }
  6797. }
  6798. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6799. SBI_GEN0 : SBI_DBUFF0;
  6800. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6801. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6802. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6803. mutex_unlock(&dev_priv->dpio_lock);
  6804. }
  6805. /* Sequence to disable CLKOUT_DP */
  6806. static void lpt_disable_clkout_dp(struct drm_device *dev)
  6807. {
  6808. struct drm_i915_private *dev_priv = dev->dev_private;
  6809. uint32_t reg, tmp;
  6810. mutex_lock(&dev_priv->dpio_lock);
  6811. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6812. SBI_GEN0 : SBI_DBUFF0;
  6813. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6814. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6815. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6816. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6817. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6818. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6819. tmp |= SBI_SSCCTL_PATHALT;
  6820. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6821. udelay(32);
  6822. }
  6823. tmp |= SBI_SSCCTL_DISABLE;
  6824. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6825. }
  6826. mutex_unlock(&dev_priv->dpio_lock);
  6827. }
  6828. static void lpt_init_pch_refclk(struct drm_device *dev)
  6829. {
  6830. struct intel_encoder *encoder;
  6831. bool has_vga = false;
  6832. for_each_intel_encoder(dev, encoder) {
  6833. switch (encoder->type) {
  6834. case INTEL_OUTPUT_ANALOG:
  6835. has_vga = true;
  6836. break;
  6837. default:
  6838. break;
  6839. }
  6840. }
  6841. if (has_vga)
  6842. lpt_enable_clkout_dp(dev, true, true);
  6843. else
  6844. lpt_disable_clkout_dp(dev);
  6845. }
  6846. /*
  6847. * Initialize reference clocks when the driver loads
  6848. */
  6849. void intel_init_pch_refclk(struct drm_device *dev)
  6850. {
  6851. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  6852. ironlake_init_pch_refclk(dev);
  6853. else if (HAS_PCH_LPT(dev))
  6854. lpt_init_pch_refclk(dev);
  6855. }
  6856. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  6857. {
  6858. struct drm_device *dev = crtc_state->base.crtc->dev;
  6859. struct drm_i915_private *dev_priv = dev->dev_private;
  6860. struct drm_atomic_state *state = crtc_state->base.state;
  6861. struct drm_connector_state *connector_state;
  6862. struct intel_encoder *encoder;
  6863. int num_connectors = 0, i;
  6864. bool is_lvds = false;
  6865. for (i = 0; i < state->num_connector; i++) {
  6866. if (!state->connectors[i])
  6867. continue;
  6868. connector_state = state->connector_states[i];
  6869. if (connector_state->crtc != crtc_state->base.crtc)
  6870. continue;
  6871. encoder = to_intel_encoder(connector_state->best_encoder);
  6872. switch (encoder->type) {
  6873. case INTEL_OUTPUT_LVDS:
  6874. is_lvds = true;
  6875. break;
  6876. default:
  6877. break;
  6878. }
  6879. num_connectors++;
  6880. }
  6881. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6882. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6883. dev_priv->vbt.lvds_ssc_freq);
  6884. return dev_priv->vbt.lvds_ssc_freq;
  6885. }
  6886. return 120000;
  6887. }
  6888. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6889. {
  6890. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  6891. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6892. int pipe = intel_crtc->pipe;
  6893. uint32_t val;
  6894. val = 0;
  6895. switch (intel_crtc->config->pipe_bpp) {
  6896. case 18:
  6897. val |= PIPECONF_6BPC;
  6898. break;
  6899. case 24:
  6900. val |= PIPECONF_8BPC;
  6901. break;
  6902. case 30:
  6903. val |= PIPECONF_10BPC;
  6904. break;
  6905. case 36:
  6906. val |= PIPECONF_12BPC;
  6907. break;
  6908. default:
  6909. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6910. BUG();
  6911. }
  6912. if (intel_crtc->config->dither)
  6913. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6914. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6915. val |= PIPECONF_INTERLACED_ILK;
  6916. else
  6917. val |= PIPECONF_PROGRESSIVE;
  6918. if (intel_crtc->config->limited_color_range)
  6919. val |= PIPECONF_COLOR_RANGE_SELECT;
  6920. I915_WRITE(PIPECONF(pipe), val);
  6921. POSTING_READ(PIPECONF(pipe));
  6922. }
  6923. /*
  6924. * Set up the pipe CSC unit.
  6925. *
  6926. * Currently only full range RGB to limited range RGB conversion
  6927. * is supported, but eventually this should handle various
  6928. * RGB<->YCbCr scenarios as well.
  6929. */
  6930. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6931. {
  6932. struct drm_device *dev = crtc->dev;
  6933. struct drm_i915_private *dev_priv = dev->dev_private;
  6934. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6935. int pipe = intel_crtc->pipe;
  6936. uint16_t coeff = 0x7800; /* 1.0 */
  6937. /*
  6938. * TODO: Check what kind of values actually come out of the pipe
  6939. * with these coeff/postoff values and adjust to get the best
  6940. * accuracy. Perhaps we even need to take the bpc value into
  6941. * consideration.
  6942. */
  6943. if (intel_crtc->config->limited_color_range)
  6944. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6945. /*
  6946. * GY/GU and RY/RU should be the other way around according
  6947. * to BSpec, but reality doesn't agree. Just set them up in
  6948. * a way that results in the correct picture.
  6949. */
  6950. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6951. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6952. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6953. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6954. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6955. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6956. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6957. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6958. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6959. if (INTEL_INFO(dev)->gen > 6) {
  6960. uint16_t postoff = 0;
  6961. if (intel_crtc->config->limited_color_range)
  6962. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6963. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6964. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6965. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6966. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6967. } else {
  6968. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6969. if (intel_crtc->config->limited_color_range)
  6970. mode |= CSC_BLACK_SCREEN_OFFSET;
  6971. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6972. }
  6973. }
  6974. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6975. {
  6976. struct drm_device *dev = crtc->dev;
  6977. struct drm_i915_private *dev_priv = dev->dev_private;
  6978. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6979. enum pipe pipe = intel_crtc->pipe;
  6980. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6981. uint32_t val;
  6982. val = 0;
  6983. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6984. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6985. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6986. val |= PIPECONF_INTERLACED_ILK;
  6987. else
  6988. val |= PIPECONF_PROGRESSIVE;
  6989. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6990. POSTING_READ(PIPECONF(cpu_transcoder));
  6991. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6992. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6993. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6994. val = 0;
  6995. switch (intel_crtc->config->pipe_bpp) {
  6996. case 18:
  6997. val |= PIPEMISC_DITHER_6_BPC;
  6998. break;
  6999. case 24:
  7000. val |= PIPEMISC_DITHER_8_BPC;
  7001. break;
  7002. case 30:
  7003. val |= PIPEMISC_DITHER_10_BPC;
  7004. break;
  7005. case 36:
  7006. val |= PIPEMISC_DITHER_12_BPC;
  7007. break;
  7008. default:
  7009. /* Case prevented by pipe_config_set_bpp. */
  7010. BUG();
  7011. }
  7012. if (intel_crtc->config->dither)
  7013. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7014. I915_WRITE(PIPEMISC(pipe), val);
  7015. }
  7016. }
  7017. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7018. struct intel_crtc_state *crtc_state,
  7019. intel_clock_t *clock,
  7020. bool *has_reduced_clock,
  7021. intel_clock_t *reduced_clock)
  7022. {
  7023. struct drm_device *dev = crtc->dev;
  7024. struct drm_i915_private *dev_priv = dev->dev_private;
  7025. int refclk;
  7026. const intel_limit_t *limit;
  7027. bool ret, is_lvds = false;
  7028. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7029. refclk = ironlake_get_refclk(crtc_state);
  7030. /*
  7031. * Returns a set of divisors for the desired target clock with the given
  7032. * refclk, or FALSE. The returned values represent the clock equation:
  7033. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7034. */
  7035. limit = intel_limit(crtc_state, refclk);
  7036. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7037. crtc_state->port_clock,
  7038. refclk, NULL, clock);
  7039. if (!ret)
  7040. return false;
  7041. if (is_lvds && dev_priv->lvds_downclock_avail) {
  7042. /*
  7043. * Ensure we match the reduced clock's P to the target clock.
  7044. * If the clocks don't match, we can't switch the display clock
  7045. * by using the FP0/FP1. In such case we will disable the LVDS
  7046. * downclock feature.
  7047. */
  7048. *has_reduced_clock =
  7049. dev_priv->display.find_dpll(limit, crtc_state,
  7050. dev_priv->lvds_downclock,
  7051. refclk, clock,
  7052. reduced_clock);
  7053. }
  7054. return true;
  7055. }
  7056. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7057. {
  7058. /*
  7059. * Account for spread spectrum to avoid
  7060. * oversubscribing the link. Max center spread
  7061. * is 2.5%; use 5% for safety's sake.
  7062. */
  7063. u32 bps = target_clock * bpp * 21 / 20;
  7064. return DIV_ROUND_UP(bps, link_bw * 8);
  7065. }
  7066. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7067. {
  7068. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7069. }
  7070. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7071. struct intel_crtc_state *crtc_state,
  7072. u32 *fp,
  7073. intel_clock_t *reduced_clock, u32 *fp2)
  7074. {
  7075. struct drm_crtc *crtc = &intel_crtc->base;
  7076. struct drm_device *dev = crtc->dev;
  7077. struct drm_i915_private *dev_priv = dev->dev_private;
  7078. struct drm_atomic_state *state = crtc_state->base.state;
  7079. struct drm_connector_state *connector_state;
  7080. struct intel_encoder *encoder;
  7081. uint32_t dpll;
  7082. int factor, num_connectors = 0, i;
  7083. bool is_lvds = false, is_sdvo = false;
  7084. for (i = 0; i < state->num_connector; i++) {
  7085. if (!state->connectors[i])
  7086. continue;
  7087. connector_state = state->connector_states[i];
  7088. if (connector_state->crtc != crtc_state->base.crtc)
  7089. continue;
  7090. encoder = to_intel_encoder(connector_state->best_encoder);
  7091. switch (encoder->type) {
  7092. case INTEL_OUTPUT_LVDS:
  7093. is_lvds = true;
  7094. break;
  7095. case INTEL_OUTPUT_SDVO:
  7096. case INTEL_OUTPUT_HDMI:
  7097. is_sdvo = true;
  7098. break;
  7099. default:
  7100. break;
  7101. }
  7102. num_connectors++;
  7103. }
  7104. /* Enable autotuning of the PLL clock (if permissible) */
  7105. factor = 21;
  7106. if (is_lvds) {
  7107. if ((intel_panel_use_ssc(dev_priv) &&
  7108. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7109. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7110. factor = 25;
  7111. } else if (crtc_state->sdvo_tv_clock)
  7112. factor = 20;
  7113. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7114. *fp |= FP_CB_TUNE;
  7115. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7116. *fp2 |= FP_CB_TUNE;
  7117. dpll = 0;
  7118. if (is_lvds)
  7119. dpll |= DPLLB_MODE_LVDS;
  7120. else
  7121. dpll |= DPLLB_MODE_DAC_SERIAL;
  7122. dpll |= (crtc_state->pixel_multiplier - 1)
  7123. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7124. if (is_sdvo)
  7125. dpll |= DPLL_SDVO_HIGH_SPEED;
  7126. if (crtc_state->has_dp_encoder)
  7127. dpll |= DPLL_SDVO_HIGH_SPEED;
  7128. /* compute bitmask from p1 value */
  7129. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7130. /* also FPA1 */
  7131. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7132. switch (crtc_state->dpll.p2) {
  7133. case 5:
  7134. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7135. break;
  7136. case 7:
  7137. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7138. break;
  7139. case 10:
  7140. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7141. break;
  7142. case 14:
  7143. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7144. break;
  7145. }
  7146. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7147. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7148. else
  7149. dpll |= PLL_REF_INPUT_DREFCLK;
  7150. return dpll | DPLL_VCO_ENABLE;
  7151. }
  7152. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7153. struct intel_crtc_state *crtc_state)
  7154. {
  7155. struct drm_device *dev = crtc->base.dev;
  7156. intel_clock_t clock, reduced_clock;
  7157. u32 dpll = 0, fp = 0, fp2 = 0;
  7158. bool ok, has_reduced_clock = false;
  7159. bool is_lvds = false;
  7160. struct intel_shared_dpll *pll;
  7161. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7162. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7163. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7164. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7165. &has_reduced_clock, &reduced_clock);
  7166. if (!ok && !crtc_state->clock_set) {
  7167. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7168. return -EINVAL;
  7169. }
  7170. /* Compat-code for transition, will disappear. */
  7171. if (!crtc_state->clock_set) {
  7172. crtc_state->dpll.n = clock.n;
  7173. crtc_state->dpll.m1 = clock.m1;
  7174. crtc_state->dpll.m2 = clock.m2;
  7175. crtc_state->dpll.p1 = clock.p1;
  7176. crtc_state->dpll.p2 = clock.p2;
  7177. }
  7178. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7179. if (crtc_state->has_pch_encoder) {
  7180. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7181. if (has_reduced_clock)
  7182. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7183. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7184. &fp, &reduced_clock,
  7185. has_reduced_clock ? &fp2 : NULL);
  7186. crtc_state->dpll_hw_state.dpll = dpll;
  7187. crtc_state->dpll_hw_state.fp0 = fp;
  7188. if (has_reduced_clock)
  7189. crtc_state->dpll_hw_state.fp1 = fp2;
  7190. else
  7191. crtc_state->dpll_hw_state.fp1 = fp;
  7192. pll = intel_get_shared_dpll(crtc, crtc_state);
  7193. if (pll == NULL) {
  7194. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7195. pipe_name(crtc->pipe));
  7196. return -EINVAL;
  7197. }
  7198. }
  7199. if (is_lvds && has_reduced_clock)
  7200. crtc->lowfreq_avail = true;
  7201. else
  7202. crtc->lowfreq_avail = false;
  7203. return 0;
  7204. }
  7205. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7206. struct intel_link_m_n *m_n)
  7207. {
  7208. struct drm_device *dev = crtc->base.dev;
  7209. struct drm_i915_private *dev_priv = dev->dev_private;
  7210. enum pipe pipe = crtc->pipe;
  7211. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7212. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7213. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7214. & ~TU_SIZE_MASK;
  7215. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7216. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7217. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7218. }
  7219. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7220. enum transcoder transcoder,
  7221. struct intel_link_m_n *m_n,
  7222. struct intel_link_m_n *m2_n2)
  7223. {
  7224. struct drm_device *dev = crtc->base.dev;
  7225. struct drm_i915_private *dev_priv = dev->dev_private;
  7226. enum pipe pipe = crtc->pipe;
  7227. if (INTEL_INFO(dev)->gen >= 5) {
  7228. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7229. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7230. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7231. & ~TU_SIZE_MASK;
  7232. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7233. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7234. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7235. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7236. * gen < 8) and if DRRS is supported (to make sure the
  7237. * registers are not unnecessarily read).
  7238. */
  7239. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7240. crtc->config->has_drrs) {
  7241. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7242. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7243. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7244. & ~TU_SIZE_MASK;
  7245. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7246. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7247. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7248. }
  7249. } else {
  7250. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7251. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7252. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7253. & ~TU_SIZE_MASK;
  7254. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7255. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7256. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7257. }
  7258. }
  7259. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7260. struct intel_crtc_state *pipe_config)
  7261. {
  7262. if (pipe_config->has_pch_encoder)
  7263. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7264. else
  7265. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7266. &pipe_config->dp_m_n,
  7267. &pipe_config->dp_m2_n2);
  7268. }
  7269. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7270. struct intel_crtc_state *pipe_config)
  7271. {
  7272. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7273. &pipe_config->fdi_m_n, NULL);
  7274. }
  7275. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7276. struct intel_crtc_state *pipe_config)
  7277. {
  7278. struct drm_device *dev = crtc->base.dev;
  7279. struct drm_i915_private *dev_priv = dev->dev_private;
  7280. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7281. uint32_t ps_ctrl = 0;
  7282. int id = -1;
  7283. int i;
  7284. /* find scaler attached to this pipe */
  7285. for (i = 0; i < crtc->num_scalers; i++) {
  7286. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7287. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7288. id = i;
  7289. pipe_config->pch_pfit.enabled = true;
  7290. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7291. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7292. break;
  7293. }
  7294. }
  7295. scaler_state->scaler_id = id;
  7296. if (id >= 0) {
  7297. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7298. } else {
  7299. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7300. }
  7301. }
  7302. static void
  7303. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7304. struct intel_initial_plane_config *plane_config)
  7305. {
  7306. struct drm_device *dev = crtc->base.dev;
  7307. struct drm_i915_private *dev_priv = dev->dev_private;
  7308. u32 val, base, offset, stride_mult, tiling;
  7309. int pipe = crtc->pipe;
  7310. int fourcc, pixel_format;
  7311. unsigned int aligned_height;
  7312. struct drm_framebuffer *fb;
  7313. struct intel_framebuffer *intel_fb;
  7314. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7315. if (!intel_fb) {
  7316. DRM_DEBUG_KMS("failed to alloc fb\n");
  7317. return;
  7318. }
  7319. fb = &intel_fb->base;
  7320. val = I915_READ(PLANE_CTL(pipe, 0));
  7321. if (!(val & PLANE_CTL_ENABLE))
  7322. goto error;
  7323. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7324. fourcc = skl_format_to_fourcc(pixel_format,
  7325. val & PLANE_CTL_ORDER_RGBX,
  7326. val & PLANE_CTL_ALPHA_MASK);
  7327. fb->pixel_format = fourcc;
  7328. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7329. tiling = val & PLANE_CTL_TILED_MASK;
  7330. switch (tiling) {
  7331. case PLANE_CTL_TILED_LINEAR:
  7332. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7333. break;
  7334. case PLANE_CTL_TILED_X:
  7335. plane_config->tiling = I915_TILING_X;
  7336. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7337. break;
  7338. case PLANE_CTL_TILED_Y:
  7339. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7340. break;
  7341. case PLANE_CTL_TILED_YF:
  7342. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7343. break;
  7344. default:
  7345. MISSING_CASE(tiling);
  7346. goto error;
  7347. }
  7348. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7349. plane_config->base = base;
  7350. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7351. val = I915_READ(PLANE_SIZE(pipe, 0));
  7352. fb->height = ((val >> 16) & 0xfff) + 1;
  7353. fb->width = ((val >> 0) & 0x1fff) + 1;
  7354. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7355. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7356. fb->pixel_format);
  7357. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7358. aligned_height = intel_fb_align_height(dev, fb->height,
  7359. fb->pixel_format,
  7360. fb->modifier[0]);
  7361. plane_config->size = fb->pitches[0] * aligned_height;
  7362. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7363. pipe_name(pipe), fb->width, fb->height,
  7364. fb->bits_per_pixel, base, fb->pitches[0],
  7365. plane_config->size);
  7366. plane_config->fb = intel_fb;
  7367. return;
  7368. error:
  7369. kfree(fb);
  7370. }
  7371. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7372. struct intel_crtc_state *pipe_config)
  7373. {
  7374. struct drm_device *dev = crtc->base.dev;
  7375. struct drm_i915_private *dev_priv = dev->dev_private;
  7376. uint32_t tmp;
  7377. tmp = I915_READ(PF_CTL(crtc->pipe));
  7378. if (tmp & PF_ENABLE) {
  7379. pipe_config->pch_pfit.enabled = true;
  7380. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7381. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7382. /* We currently do not free assignements of panel fitters on
  7383. * ivb/hsw (since we don't use the higher upscaling modes which
  7384. * differentiates them) so just WARN about this case for now. */
  7385. if (IS_GEN7(dev)) {
  7386. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7387. PF_PIPE_SEL_IVB(crtc->pipe));
  7388. }
  7389. }
  7390. }
  7391. static void
  7392. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7393. struct intel_initial_plane_config *plane_config)
  7394. {
  7395. struct drm_device *dev = crtc->base.dev;
  7396. struct drm_i915_private *dev_priv = dev->dev_private;
  7397. u32 val, base, offset;
  7398. int pipe = crtc->pipe;
  7399. int fourcc, pixel_format;
  7400. unsigned int aligned_height;
  7401. struct drm_framebuffer *fb;
  7402. struct intel_framebuffer *intel_fb;
  7403. val = I915_READ(DSPCNTR(pipe));
  7404. if (!(val & DISPLAY_PLANE_ENABLE))
  7405. return;
  7406. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7407. if (!intel_fb) {
  7408. DRM_DEBUG_KMS("failed to alloc fb\n");
  7409. return;
  7410. }
  7411. fb = &intel_fb->base;
  7412. if (INTEL_INFO(dev)->gen >= 4) {
  7413. if (val & DISPPLANE_TILED) {
  7414. plane_config->tiling = I915_TILING_X;
  7415. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7416. }
  7417. }
  7418. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7419. fourcc = i9xx_format_to_fourcc(pixel_format);
  7420. fb->pixel_format = fourcc;
  7421. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7422. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7423. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7424. offset = I915_READ(DSPOFFSET(pipe));
  7425. } else {
  7426. if (plane_config->tiling)
  7427. offset = I915_READ(DSPTILEOFF(pipe));
  7428. else
  7429. offset = I915_READ(DSPLINOFF(pipe));
  7430. }
  7431. plane_config->base = base;
  7432. val = I915_READ(PIPESRC(pipe));
  7433. fb->width = ((val >> 16) & 0xfff) + 1;
  7434. fb->height = ((val >> 0) & 0xfff) + 1;
  7435. val = I915_READ(DSPSTRIDE(pipe));
  7436. fb->pitches[0] = val & 0xffffffc0;
  7437. aligned_height = intel_fb_align_height(dev, fb->height,
  7438. fb->pixel_format,
  7439. fb->modifier[0]);
  7440. plane_config->size = fb->pitches[0] * aligned_height;
  7441. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7442. pipe_name(pipe), fb->width, fb->height,
  7443. fb->bits_per_pixel, base, fb->pitches[0],
  7444. plane_config->size);
  7445. plane_config->fb = intel_fb;
  7446. }
  7447. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7448. struct intel_crtc_state *pipe_config)
  7449. {
  7450. struct drm_device *dev = crtc->base.dev;
  7451. struct drm_i915_private *dev_priv = dev->dev_private;
  7452. uint32_t tmp;
  7453. if (!intel_display_power_is_enabled(dev_priv,
  7454. POWER_DOMAIN_PIPE(crtc->pipe)))
  7455. return false;
  7456. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7457. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7458. tmp = I915_READ(PIPECONF(crtc->pipe));
  7459. if (!(tmp & PIPECONF_ENABLE))
  7460. return false;
  7461. switch (tmp & PIPECONF_BPC_MASK) {
  7462. case PIPECONF_6BPC:
  7463. pipe_config->pipe_bpp = 18;
  7464. break;
  7465. case PIPECONF_8BPC:
  7466. pipe_config->pipe_bpp = 24;
  7467. break;
  7468. case PIPECONF_10BPC:
  7469. pipe_config->pipe_bpp = 30;
  7470. break;
  7471. case PIPECONF_12BPC:
  7472. pipe_config->pipe_bpp = 36;
  7473. break;
  7474. default:
  7475. break;
  7476. }
  7477. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7478. pipe_config->limited_color_range = true;
  7479. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7480. struct intel_shared_dpll *pll;
  7481. pipe_config->has_pch_encoder = true;
  7482. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7483. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7484. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7485. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7486. if (HAS_PCH_IBX(dev_priv->dev)) {
  7487. pipe_config->shared_dpll =
  7488. (enum intel_dpll_id) crtc->pipe;
  7489. } else {
  7490. tmp = I915_READ(PCH_DPLL_SEL);
  7491. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7492. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7493. else
  7494. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7495. }
  7496. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7497. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7498. &pipe_config->dpll_hw_state));
  7499. tmp = pipe_config->dpll_hw_state.dpll;
  7500. pipe_config->pixel_multiplier =
  7501. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7502. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7503. ironlake_pch_clock_get(crtc, pipe_config);
  7504. } else {
  7505. pipe_config->pixel_multiplier = 1;
  7506. }
  7507. intel_get_pipe_timings(crtc, pipe_config);
  7508. ironlake_get_pfit_config(crtc, pipe_config);
  7509. return true;
  7510. }
  7511. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7512. {
  7513. struct drm_device *dev = dev_priv->dev;
  7514. struct intel_crtc *crtc;
  7515. for_each_intel_crtc(dev, crtc)
  7516. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7517. pipe_name(crtc->pipe));
  7518. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7519. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7520. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7521. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7522. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7523. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7524. "CPU PWM1 enabled\n");
  7525. if (IS_HASWELL(dev))
  7526. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7527. "CPU PWM2 enabled\n");
  7528. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7529. "PCH PWM1 enabled\n");
  7530. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7531. "Utility pin enabled\n");
  7532. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7533. /*
  7534. * In theory we can still leave IRQs enabled, as long as only the HPD
  7535. * interrupts remain enabled. We used to check for that, but since it's
  7536. * gen-specific and since we only disable LCPLL after we fully disable
  7537. * the interrupts, the check below should be enough.
  7538. */
  7539. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7540. }
  7541. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7542. {
  7543. struct drm_device *dev = dev_priv->dev;
  7544. if (IS_HASWELL(dev))
  7545. return I915_READ(D_COMP_HSW);
  7546. else
  7547. return I915_READ(D_COMP_BDW);
  7548. }
  7549. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7550. {
  7551. struct drm_device *dev = dev_priv->dev;
  7552. if (IS_HASWELL(dev)) {
  7553. mutex_lock(&dev_priv->rps.hw_lock);
  7554. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7555. val))
  7556. DRM_ERROR("Failed to write to D_COMP\n");
  7557. mutex_unlock(&dev_priv->rps.hw_lock);
  7558. } else {
  7559. I915_WRITE(D_COMP_BDW, val);
  7560. POSTING_READ(D_COMP_BDW);
  7561. }
  7562. }
  7563. /*
  7564. * This function implements pieces of two sequences from BSpec:
  7565. * - Sequence for display software to disable LCPLL
  7566. * - Sequence for display software to allow package C8+
  7567. * The steps implemented here are just the steps that actually touch the LCPLL
  7568. * register. Callers should take care of disabling all the display engine
  7569. * functions, doing the mode unset, fixing interrupts, etc.
  7570. */
  7571. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7572. bool switch_to_fclk, bool allow_power_down)
  7573. {
  7574. uint32_t val;
  7575. assert_can_disable_lcpll(dev_priv);
  7576. val = I915_READ(LCPLL_CTL);
  7577. if (switch_to_fclk) {
  7578. val |= LCPLL_CD_SOURCE_FCLK;
  7579. I915_WRITE(LCPLL_CTL, val);
  7580. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7581. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7582. DRM_ERROR("Switching to FCLK failed\n");
  7583. val = I915_READ(LCPLL_CTL);
  7584. }
  7585. val |= LCPLL_PLL_DISABLE;
  7586. I915_WRITE(LCPLL_CTL, val);
  7587. POSTING_READ(LCPLL_CTL);
  7588. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7589. DRM_ERROR("LCPLL still locked\n");
  7590. val = hsw_read_dcomp(dev_priv);
  7591. val |= D_COMP_COMP_DISABLE;
  7592. hsw_write_dcomp(dev_priv, val);
  7593. ndelay(100);
  7594. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7595. 1))
  7596. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7597. if (allow_power_down) {
  7598. val = I915_READ(LCPLL_CTL);
  7599. val |= LCPLL_POWER_DOWN_ALLOW;
  7600. I915_WRITE(LCPLL_CTL, val);
  7601. POSTING_READ(LCPLL_CTL);
  7602. }
  7603. }
  7604. /*
  7605. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7606. * source.
  7607. */
  7608. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7609. {
  7610. uint32_t val;
  7611. val = I915_READ(LCPLL_CTL);
  7612. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7613. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7614. return;
  7615. /*
  7616. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7617. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7618. */
  7619. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7620. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7621. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7622. I915_WRITE(LCPLL_CTL, val);
  7623. POSTING_READ(LCPLL_CTL);
  7624. }
  7625. val = hsw_read_dcomp(dev_priv);
  7626. val |= D_COMP_COMP_FORCE;
  7627. val &= ~D_COMP_COMP_DISABLE;
  7628. hsw_write_dcomp(dev_priv, val);
  7629. val = I915_READ(LCPLL_CTL);
  7630. val &= ~LCPLL_PLL_DISABLE;
  7631. I915_WRITE(LCPLL_CTL, val);
  7632. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7633. DRM_ERROR("LCPLL not locked yet\n");
  7634. if (val & LCPLL_CD_SOURCE_FCLK) {
  7635. val = I915_READ(LCPLL_CTL);
  7636. val &= ~LCPLL_CD_SOURCE_FCLK;
  7637. I915_WRITE(LCPLL_CTL, val);
  7638. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7639. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7640. DRM_ERROR("Switching back to LCPLL failed\n");
  7641. }
  7642. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7643. }
  7644. /*
  7645. * Package states C8 and deeper are really deep PC states that can only be
  7646. * reached when all the devices on the system allow it, so even if the graphics
  7647. * device allows PC8+, it doesn't mean the system will actually get to these
  7648. * states. Our driver only allows PC8+ when going into runtime PM.
  7649. *
  7650. * The requirements for PC8+ are that all the outputs are disabled, the power
  7651. * well is disabled and most interrupts are disabled, and these are also
  7652. * requirements for runtime PM. When these conditions are met, we manually do
  7653. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7654. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7655. * hang the machine.
  7656. *
  7657. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7658. * the state of some registers, so when we come back from PC8+ we need to
  7659. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7660. * need to take care of the registers kept by RC6. Notice that this happens even
  7661. * if we don't put the device in PCI D3 state (which is what currently happens
  7662. * because of the runtime PM support).
  7663. *
  7664. * For more, read "Display Sequences for Package C8" on the hardware
  7665. * documentation.
  7666. */
  7667. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7668. {
  7669. struct drm_device *dev = dev_priv->dev;
  7670. uint32_t val;
  7671. DRM_DEBUG_KMS("Enabling package C8+\n");
  7672. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7673. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7674. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7675. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7676. }
  7677. lpt_disable_clkout_dp(dev);
  7678. hsw_disable_lcpll(dev_priv, true, true);
  7679. }
  7680. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7681. {
  7682. struct drm_device *dev = dev_priv->dev;
  7683. uint32_t val;
  7684. DRM_DEBUG_KMS("Disabling package C8+\n");
  7685. hsw_restore_lcpll(dev_priv);
  7686. lpt_init_pch_refclk(dev);
  7687. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7688. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7689. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7690. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7691. }
  7692. intel_prepare_ddi(dev);
  7693. }
  7694. static void broxton_modeset_global_resources(struct drm_atomic_state *state)
  7695. {
  7696. struct drm_device *dev = state->dev;
  7697. struct drm_i915_private *dev_priv = dev->dev_private;
  7698. int max_pixclk = intel_mode_max_pixclk(state);
  7699. int req_cdclk;
  7700. /* see the comment in valleyview_modeset_global_resources */
  7701. if (WARN_ON(max_pixclk < 0))
  7702. return;
  7703. req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  7704. if (req_cdclk != dev_priv->cdclk_freq)
  7705. broxton_set_cdclk(dev, req_cdclk);
  7706. }
  7707. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7708. struct intel_crtc_state *crtc_state)
  7709. {
  7710. if (!intel_ddi_pll_select(crtc, crtc_state))
  7711. return -EINVAL;
  7712. crtc->lowfreq_avail = false;
  7713. return 0;
  7714. }
  7715. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7716. enum port port,
  7717. struct intel_crtc_state *pipe_config)
  7718. {
  7719. switch (port) {
  7720. case PORT_A:
  7721. pipe_config->ddi_pll_sel = SKL_DPLL0;
  7722. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7723. break;
  7724. case PORT_B:
  7725. pipe_config->ddi_pll_sel = SKL_DPLL1;
  7726. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7727. break;
  7728. case PORT_C:
  7729. pipe_config->ddi_pll_sel = SKL_DPLL2;
  7730. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7731. break;
  7732. default:
  7733. DRM_ERROR("Incorrect port type\n");
  7734. }
  7735. }
  7736. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7737. enum port port,
  7738. struct intel_crtc_state *pipe_config)
  7739. {
  7740. u32 temp, dpll_ctl1;
  7741. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7742. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  7743. switch (pipe_config->ddi_pll_sel) {
  7744. case SKL_DPLL0:
  7745. /*
  7746. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  7747. * of the shared DPLL framework and thus needs to be read out
  7748. * separately
  7749. */
  7750. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  7751. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  7752. break;
  7753. case SKL_DPLL1:
  7754. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7755. break;
  7756. case SKL_DPLL2:
  7757. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7758. break;
  7759. case SKL_DPLL3:
  7760. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7761. break;
  7762. }
  7763. }
  7764. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7765. enum port port,
  7766. struct intel_crtc_state *pipe_config)
  7767. {
  7768. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7769. switch (pipe_config->ddi_pll_sel) {
  7770. case PORT_CLK_SEL_WRPLL1:
  7771. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  7772. break;
  7773. case PORT_CLK_SEL_WRPLL2:
  7774. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  7775. break;
  7776. }
  7777. }
  7778. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7779. struct intel_crtc_state *pipe_config)
  7780. {
  7781. struct drm_device *dev = crtc->base.dev;
  7782. struct drm_i915_private *dev_priv = dev->dev_private;
  7783. struct intel_shared_dpll *pll;
  7784. enum port port;
  7785. uint32_t tmp;
  7786. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7787. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7788. if (IS_SKYLAKE(dev))
  7789. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7790. else if (IS_BROXTON(dev))
  7791. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7792. else
  7793. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7794. if (pipe_config->shared_dpll >= 0) {
  7795. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7796. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7797. &pipe_config->dpll_hw_state));
  7798. }
  7799. /*
  7800. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7801. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7802. * the PCH transcoder is on.
  7803. */
  7804. if (INTEL_INFO(dev)->gen < 9 &&
  7805. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7806. pipe_config->has_pch_encoder = true;
  7807. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7808. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7809. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7810. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7811. }
  7812. }
  7813. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7814. struct intel_crtc_state *pipe_config)
  7815. {
  7816. struct drm_device *dev = crtc->base.dev;
  7817. struct drm_i915_private *dev_priv = dev->dev_private;
  7818. enum intel_display_power_domain pfit_domain;
  7819. uint32_t tmp;
  7820. if (!intel_display_power_is_enabled(dev_priv,
  7821. POWER_DOMAIN_PIPE(crtc->pipe)))
  7822. return false;
  7823. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7824. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7825. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7826. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7827. enum pipe trans_edp_pipe;
  7828. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7829. default:
  7830. WARN(1, "unknown pipe linked to edp transcoder\n");
  7831. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7832. case TRANS_DDI_EDP_INPUT_A_ON:
  7833. trans_edp_pipe = PIPE_A;
  7834. break;
  7835. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7836. trans_edp_pipe = PIPE_B;
  7837. break;
  7838. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7839. trans_edp_pipe = PIPE_C;
  7840. break;
  7841. }
  7842. if (trans_edp_pipe == crtc->pipe)
  7843. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7844. }
  7845. if (!intel_display_power_is_enabled(dev_priv,
  7846. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  7847. return false;
  7848. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7849. if (!(tmp & PIPECONF_ENABLE))
  7850. return false;
  7851. haswell_get_ddi_port_state(crtc, pipe_config);
  7852. intel_get_pipe_timings(crtc, pipe_config);
  7853. if (INTEL_INFO(dev)->gen >= 9) {
  7854. skl_init_scalers(dev, crtc, pipe_config);
  7855. }
  7856. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7857. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  7858. if (INTEL_INFO(dev)->gen == 9)
  7859. skylake_get_pfit_config(crtc, pipe_config);
  7860. else if (INTEL_INFO(dev)->gen < 9)
  7861. ironlake_get_pfit_config(crtc, pipe_config);
  7862. else
  7863. MISSING_CASE(INTEL_INFO(dev)->gen);
  7864. } else {
  7865. pipe_config->scaler_state.scaler_id = -1;
  7866. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7867. }
  7868. if (IS_HASWELL(dev))
  7869. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7870. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7871. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  7872. pipe_config->pixel_multiplier =
  7873. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7874. } else {
  7875. pipe_config->pixel_multiplier = 1;
  7876. }
  7877. return true;
  7878. }
  7879. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  7880. {
  7881. struct drm_device *dev = crtc->dev;
  7882. struct drm_i915_private *dev_priv = dev->dev_private;
  7883. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7884. uint32_t cntl = 0, size = 0;
  7885. if (base) {
  7886. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  7887. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  7888. unsigned int stride = roundup_pow_of_two(width) * 4;
  7889. switch (stride) {
  7890. default:
  7891. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7892. width, stride);
  7893. stride = 256;
  7894. /* fallthrough */
  7895. case 256:
  7896. case 512:
  7897. case 1024:
  7898. case 2048:
  7899. break;
  7900. }
  7901. cntl |= CURSOR_ENABLE |
  7902. CURSOR_GAMMA_ENABLE |
  7903. CURSOR_FORMAT_ARGB |
  7904. CURSOR_STRIDE(stride);
  7905. size = (height << 12) | width;
  7906. }
  7907. if (intel_crtc->cursor_cntl != 0 &&
  7908. (intel_crtc->cursor_base != base ||
  7909. intel_crtc->cursor_size != size ||
  7910. intel_crtc->cursor_cntl != cntl)) {
  7911. /* On these chipsets we can only modify the base/size/stride
  7912. * whilst the cursor is disabled.
  7913. */
  7914. I915_WRITE(_CURACNTR, 0);
  7915. POSTING_READ(_CURACNTR);
  7916. intel_crtc->cursor_cntl = 0;
  7917. }
  7918. if (intel_crtc->cursor_base != base) {
  7919. I915_WRITE(_CURABASE, base);
  7920. intel_crtc->cursor_base = base;
  7921. }
  7922. if (intel_crtc->cursor_size != size) {
  7923. I915_WRITE(CURSIZE, size);
  7924. intel_crtc->cursor_size = size;
  7925. }
  7926. if (intel_crtc->cursor_cntl != cntl) {
  7927. I915_WRITE(_CURACNTR, cntl);
  7928. POSTING_READ(_CURACNTR);
  7929. intel_crtc->cursor_cntl = cntl;
  7930. }
  7931. }
  7932. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  7933. {
  7934. struct drm_device *dev = crtc->dev;
  7935. struct drm_i915_private *dev_priv = dev->dev_private;
  7936. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7937. int pipe = intel_crtc->pipe;
  7938. uint32_t cntl;
  7939. cntl = 0;
  7940. if (base) {
  7941. cntl = MCURSOR_GAMMA_ENABLE;
  7942. switch (intel_crtc->base.cursor->state->crtc_w) {
  7943. case 64:
  7944. cntl |= CURSOR_MODE_64_ARGB_AX;
  7945. break;
  7946. case 128:
  7947. cntl |= CURSOR_MODE_128_ARGB_AX;
  7948. break;
  7949. case 256:
  7950. cntl |= CURSOR_MODE_256_ARGB_AX;
  7951. break;
  7952. default:
  7953. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  7954. return;
  7955. }
  7956. cntl |= pipe << 28; /* Connect to correct pipe */
  7957. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  7958. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7959. }
  7960. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  7961. cntl |= CURSOR_ROTATE_180;
  7962. if (intel_crtc->cursor_cntl != cntl) {
  7963. I915_WRITE(CURCNTR(pipe), cntl);
  7964. POSTING_READ(CURCNTR(pipe));
  7965. intel_crtc->cursor_cntl = cntl;
  7966. }
  7967. /* and commit changes on next vblank */
  7968. I915_WRITE(CURBASE(pipe), base);
  7969. POSTING_READ(CURBASE(pipe));
  7970. intel_crtc->cursor_base = base;
  7971. }
  7972. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7973. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7974. bool on)
  7975. {
  7976. struct drm_device *dev = crtc->dev;
  7977. struct drm_i915_private *dev_priv = dev->dev_private;
  7978. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7979. int pipe = intel_crtc->pipe;
  7980. int x = crtc->cursor_x;
  7981. int y = crtc->cursor_y;
  7982. u32 base = 0, pos = 0;
  7983. if (on)
  7984. base = intel_crtc->cursor_addr;
  7985. if (x >= intel_crtc->config->pipe_src_w)
  7986. base = 0;
  7987. if (y >= intel_crtc->config->pipe_src_h)
  7988. base = 0;
  7989. if (x < 0) {
  7990. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  7991. base = 0;
  7992. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7993. x = -x;
  7994. }
  7995. pos |= x << CURSOR_X_SHIFT;
  7996. if (y < 0) {
  7997. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  7998. base = 0;
  7999. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8000. y = -y;
  8001. }
  8002. pos |= y << CURSOR_Y_SHIFT;
  8003. if (base == 0 && intel_crtc->cursor_base == 0)
  8004. return;
  8005. I915_WRITE(CURPOS(pipe), pos);
  8006. /* ILK+ do this automagically */
  8007. if (HAS_GMCH_DISPLAY(dev) &&
  8008. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8009. base += (intel_crtc->base.cursor->state->crtc_h *
  8010. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8011. }
  8012. if (IS_845G(dev) || IS_I865G(dev))
  8013. i845_update_cursor(crtc, base);
  8014. else
  8015. i9xx_update_cursor(crtc, base);
  8016. }
  8017. static bool cursor_size_ok(struct drm_device *dev,
  8018. uint32_t width, uint32_t height)
  8019. {
  8020. if (width == 0 || height == 0)
  8021. return false;
  8022. /*
  8023. * 845g/865g are special in that they are only limited by
  8024. * the width of their cursors, the height is arbitrary up to
  8025. * the precision of the register. Everything else requires
  8026. * square cursors, limited to a few power-of-two sizes.
  8027. */
  8028. if (IS_845G(dev) || IS_I865G(dev)) {
  8029. if ((width & 63) != 0)
  8030. return false;
  8031. if (width > (IS_845G(dev) ? 64 : 512))
  8032. return false;
  8033. if (height > 1023)
  8034. return false;
  8035. } else {
  8036. switch (width | height) {
  8037. case 256:
  8038. case 128:
  8039. if (IS_GEN2(dev))
  8040. return false;
  8041. case 64:
  8042. break;
  8043. default:
  8044. return false;
  8045. }
  8046. }
  8047. return true;
  8048. }
  8049. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8050. u16 *blue, uint32_t start, uint32_t size)
  8051. {
  8052. int end = (start + size > 256) ? 256 : start + size, i;
  8053. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8054. for (i = start; i < end; i++) {
  8055. intel_crtc->lut_r[i] = red[i] >> 8;
  8056. intel_crtc->lut_g[i] = green[i] >> 8;
  8057. intel_crtc->lut_b[i] = blue[i] >> 8;
  8058. }
  8059. intel_crtc_load_lut(crtc);
  8060. }
  8061. /* VESA 640x480x72Hz mode to set on the pipe */
  8062. static struct drm_display_mode load_detect_mode = {
  8063. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8064. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8065. };
  8066. struct drm_framebuffer *
  8067. __intel_framebuffer_create(struct drm_device *dev,
  8068. struct drm_mode_fb_cmd2 *mode_cmd,
  8069. struct drm_i915_gem_object *obj)
  8070. {
  8071. struct intel_framebuffer *intel_fb;
  8072. int ret;
  8073. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8074. if (!intel_fb) {
  8075. drm_gem_object_unreference(&obj->base);
  8076. return ERR_PTR(-ENOMEM);
  8077. }
  8078. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8079. if (ret)
  8080. goto err;
  8081. return &intel_fb->base;
  8082. err:
  8083. drm_gem_object_unreference(&obj->base);
  8084. kfree(intel_fb);
  8085. return ERR_PTR(ret);
  8086. }
  8087. static struct drm_framebuffer *
  8088. intel_framebuffer_create(struct drm_device *dev,
  8089. struct drm_mode_fb_cmd2 *mode_cmd,
  8090. struct drm_i915_gem_object *obj)
  8091. {
  8092. struct drm_framebuffer *fb;
  8093. int ret;
  8094. ret = i915_mutex_lock_interruptible(dev);
  8095. if (ret)
  8096. return ERR_PTR(ret);
  8097. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8098. mutex_unlock(&dev->struct_mutex);
  8099. return fb;
  8100. }
  8101. static u32
  8102. intel_framebuffer_pitch_for_width(int width, int bpp)
  8103. {
  8104. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8105. return ALIGN(pitch, 64);
  8106. }
  8107. static u32
  8108. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8109. {
  8110. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8111. return PAGE_ALIGN(pitch * mode->vdisplay);
  8112. }
  8113. static struct drm_framebuffer *
  8114. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8115. struct drm_display_mode *mode,
  8116. int depth, int bpp)
  8117. {
  8118. struct drm_i915_gem_object *obj;
  8119. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8120. obj = i915_gem_alloc_object(dev,
  8121. intel_framebuffer_size_for_mode(mode, bpp));
  8122. if (obj == NULL)
  8123. return ERR_PTR(-ENOMEM);
  8124. mode_cmd.width = mode->hdisplay;
  8125. mode_cmd.height = mode->vdisplay;
  8126. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8127. bpp);
  8128. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8129. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8130. }
  8131. static struct drm_framebuffer *
  8132. mode_fits_in_fbdev(struct drm_device *dev,
  8133. struct drm_display_mode *mode)
  8134. {
  8135. #ifdef CONFIG_DRM_I915_FBDEV
  8136. struct drm_i915_private *dev_priv = dev->dev_private;
  8137. struct drm_i915_gem_object *obj;
  8138. struct drm_framebuffer *fb;
  8139. if (!dev_priv->fbdev)
  8140. return NULL;
  8141. if (!dev_priv->fbdev->fb)
  8142. return NULL;
  8143. obj = dev_priv->fbdev->fb->obj;
  8144. BUG_ON(!obj);
  8145. fb = &dev_priv->fbdev->fb->base;
  8146. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8147. fb->bits_per_pixel))
  8148. return NULL;
  8149. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8150. return NULL;
  8151. return fb;
  8152. #else
  8153. return NULL;
  8154. #endif
  8155. }
  8156. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8157. struct drm_display_mode *mode,
  8158. struct intel_load_detect_pipe *old,
  8159. struct drm_modeset_acquire_ctx *ctx)
  8160. {
  8161. struct intel_crtc *intel_crtc;
  8162. struct intel_encoder *intel_encoder =
  8163. intel_attached_encoder(connector);
  8164. struct drm_crtc *possible_crtc;
  8165. struct drm_encoder *encoder = &intel_encoder->base;
  8166. struct drm_crtc *crtc = NULL;
  8167. struct drm_device *dev = encoder->dev;
  8168. struct drm_framebuffer *fb;
  8169. struct drm_mode_config *config = &dev->mode_config;
  8170. struct drm_atomic_state *state = NULL;
  8171. struct drm_connector_state *connector_state;
  8172. int ret, i = -1;
  8173. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8174. connector->base.id, connector->name,
  8175. encoder->base.id, encoder->name);
  8176. retry:
  8177. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8178. if (ret)
  8179. goto fail_unlock;
  8180. /*
  8181. * Algorithm gets a little messy:
  8182. *
  8183. * - if the connector already has an assigned crtc, use it (but make
  8184. * sure it's on first)
  8185. *
  8186. * - try to find the first unused crtc that can drive this connector,
  8187. * and use that if we find one
  8188. */
  8189. /* See if we already have a CRTC for this connector */
  8190. if (encoder->crtc) {
  8191. crtc = encoder->crtc;
  8192. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8193. if (ret)
  8194. goto fail_unlock;
  8195. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8196. if (ret)
  8197. goto fail_unlock;
  8198. old->dpms_mode = connector->dpms;
  8199. old->load_detect_temp = false;
  8200. /* Make sure the crtc and connector are running */
  8201. if (connector->dpms != DRM_MODE_DPMS_ON)
  8202. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8203. return true;
  8204. }
  8205. /* Find an unused one (if possible) */
  8206. for_each_crtc(dev, possible_crtc) {
  8207. i++;
  8208. if (!(encoder->possible_crtcs & (1 << i)))
  8209. continue;
  8210. if (possible_crtc->state->enable)
  8211. continue;
  8212. /* This can occur when applying the pipe A quirk on resume. */
  8213. if (to_intel_crtc(possible_crtc)->new_enabled)
  8214. continue;
  8215. crtc = possible_crtc;
  8216. break;
  8217. }
  8218. /*
  8219. * If we didn't find an unused CRTC, don't use any.
  8220. */
  8221. if (!crtc) {
  8222. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8223. goto fail_unlock;
  8224. }
  8225. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8226. if (ret)
  8227. goto fail_unlock;
  8228. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8229. if (ret)
  8230. goto fail_unlock;
  8231. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8232. to_intel_connector(connector)->new_encoder = intel_encoder;
  8233. intel_crtc = to_intel_crtc(crtc);
  8234. intel_crtc->new_enabled = true;
  8235. old->dpms_mode = connector->dpms;
  8236. old->load_detect_temp = true;
  8237. old->release_fb = NULL;
  8238. state = drm_atomic_state_alloc(dev);
  8239. if (!state)
  8240. return false;
  8241. state->acquire_ctx = ctx;
  8242. connector_state = drm_atomic_get_connector_state(state, connector);
  8243. if (IS_ERR(connector_state)) {
  8244. ret = PTR_ERR(connector_state);
  8245. goto fail;
  8246. }
  8247. connector_state->crtc = crtc;
  8248. connector_state->best_encoder = &intel_encoder->base;
  8249. if (!mode)
  8250. mode = &load_detect_mode;
  8251. /* We need a framebuffer large enough to accommodate all accesses
  8252. * that the plane may generate whilst we perform load detection.
  8253. * We can not rely on the fbcon either being present (we get called
  8254. * during its initialisation to detect all boot displays, or it may
  8255. * not even exist) or that it is large enough to satisfy the
  8256. * requested mode.
  8257. */
  8258. fb = mode_fits_in_fbdev(dev, mode);
  8259. if (fb == NULL) {
  8260. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8261. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8262. old->release_fb = fb;
  8263. } else
  8264. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8265. if (IS_ERR(fb)) {
  8266. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8267. goto fail;
  8268. }
  8269. if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
  8270. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8271. if (old->release_fb)
  8272. old->release_fb->funcs->destroy(old->release_fb);
  8273. goto fail;
  8274. }
  8275. crtc->primary->crtc = crtc;
  8276. /* let the connector get through one full cycle before testing */
  8277. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8278. return true;
  8279. fail:
  8280. intel_crtc->new_enabled = crtc->state->enable;
  8281. fail_unlock:
  8282. if (state) {
  8283. drm_atomic_state_free(state);
  8284. state = NULL;
  8285. }
  8286. if (ret == -EDEADLK) {
  8287. drm_modeset_backoff(ctx);
  8288. goto retry;
  8289. }
  8290. return false;
  8291. }
  8292. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8293. struct intel_load_detect_pipe *old,
  8294. struct drm_modeset_acquire_ctx *ctx)
  8295. {
  8296. struct drm_device *dev = connector->dev;
  8297. struct intel_encoder *intel_encoder =
  8298. intel_attached_encoder(connector);
  8299. struct drm_encoder *encoder = &intel_encoder->base;
  8300. struct drm_crtc *crtc = encoder->crtc;
  8301. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8302. struct drm_atomic_state *state;
  8303. struct drm_connector_state *connector_state;
  8304. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8305. connector->base.id, connector->name,
  8306. encoder->base.id, encoder->name);
  8307. if (old->load_detect_temp) {
  8308. state = drm_atomic_state_alloc(dev);
  8309. if (!state)
  8310. goto fail;
  8311. state->acquire_ctx = ctx;
  8312. connector_state = drm_atomic_get_connector_state(state, connector);
  8313. if (IS_ERR(connector_state))
  8314. goto fail;
  8315. to_intel_connector(connector)->new_encoder = NULL;
  8316. intel_encoder->new_crtc = NULL;
  8317. intel_crtc->new_enabled = false;
  8318. connector_state->best_encoder = NULL;
  8319. connector_state->crtc = NULL;
  8320. intel_set_mode(crtc, NULL, 0, 0, NULL, state);
  8321. drm_atomic_state_free(state);
  8322. if (old->release_fb) {
  8323. drm_framebuffer_unregister_private(old->release_fb);
  8324. drm_framebuffer_unreference(old->release_fb);
  8325. }
  8326. return;
  8327. }
  8328. /* Switch crtc and encoder back off if necessary */
  8329. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8330. connector->funcs->dpms(connector, old->dpms_mode);
  8331. return;
  8332. fail:
  8333. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8334. drm_atomic_state_free(state);
  8335. }
  8336. static int i9xx_pll_refclk(struct drm_device *dev,
  8337. const struct intel_crtc_state *pipe_config)
  8338. {
  8339. struct drm_i915_private *dev_priv = dev->dev_private;
  8340. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8341. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8342. return dev_priv->vbt.lvds_ssc_freq;
  8343. else if (HAS_PCH_SPLIT(dev))
  8344. return 120000;
  8345. else if (!IS_GEN2(dev))
  8346. return 96000;
  8347. else
  8348. return 48000;
  8349. }
  8350. /* Returns the clock of the currently programmed mode of the given pipe. */
  8351. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8352. struct intel_crtc_state *pipe_config)
  8353. {
  8354. struct drm_device *dev = crtc->base.dev;
  8355. struct drm_i915_private *dev_priv = dev->dev_private;
  8356. int pipe = pipe_config->cpu_transcoder;
  8357. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8358. u32 fp;
  8359. intel_clock_t clock;
  8360. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8361. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8362. fp = pipe_config->dpll_hw_state.fp0;
  8363. else
  8364. fp = pipe_config->dpll_hw_state.fp1;
  8365. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8366. if (IS_PINEVIEW(dev)) {
  8367. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8368. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8369. } else {
  8370. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8371. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8372. }
  8373. if (!IS_GEN2(dev)) {
  8374. if (IS_PINEVIEW(dev))
  8375. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8376. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8377. else
  8378. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8379. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8380. switch (dpll & DPLL_MODE_MASK) {
  8381. case DPLLB_MODE_DAC_SERIAL:
  8382. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8383. 5 : 10;
  8384. break;
  8385. case DPLLB_MODE_LVDS:
  8386. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8387. 7 : 14;
  8388. break;
  8389. default:
  8390. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8391. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8392. return;
  8393. }
  8394. if (IS_PINEVIEW(dev))
  8395. pineview_clock(refclk, &clock);
  8396. else
  8397. i9xx_clock(refclk, &clock);
  8398. } else {
  8399. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8400. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8401. if (is_lvds) {
  8402. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8403. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8404. if (lvds & LVDS_CLKB_POWER_UP)
  8405. clock.p2 = 7;
  8406. else
  8407. clock.p2 = 14;
  8408. } else {
  8409. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8410. clock.p1 = 2;
  8411. else {
  8412. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8413. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8414. }
  8415. if (dpll & PLL_P2_DIVIDE_BY_4)
  8416. clock.p2 = 4;
  8417. else
  8418. clock.p2 = 2;
  8419. }
  8420. i9xx_clock(refclk, &clock);
  8421. }
  8422. /*
  8423. * This value includes pixel_multiplier. We will use
  8424. * port_clock to compute adjusted_mode.crtc_clock in the
  8425. * encoder's get_config() function.
  8426. */
  8427. pipe_config->port_clock = clock.dot;
  8428. }
  8429. int intel_dotclock_calculate(int link_freq,
  8430. const struct intel_link_m_n *m_n)
  8431. {
  8432. /*
  8433. * The calculation for the data clock is:
  8434. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8435. * But we want to avoid losing precison if possible, so:
  8436. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8437. *
  8438. * and the link clock is simpler:
  8439. * link_clock = (m * link_clock) / n
  8440. */
  8441. if (!m_n->link_n)
  8442. return 0;
  8443. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8444. }
  8445. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8446. struct intel_crtc_state *pipe_config)
  8447. {
  8448. struct drm_device *dev = crtc->base.dev;
  8449. /* read out port_clock from the DPLL */
  8450. i9xx_crtc_clock_get(crtc, pipe_config);
  8451. /*
  8452. * This value does not include pixel_multiplier.
  8453. * We will check that port_clock and adjusted_mode.crtc_clock
  8454. * agree once we know their relationship in the encoder's
  8455. * get_config() function.
  8456. */
  8457. pipe_config->base.adjusted_mode.crtc_clock =
  8458. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8459. &pipe_config->fdi_m_n);
  8460. }
  8461. /** Returns the currently programmed mode of the given pipe. */
  8462. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8463. struct drm_crtc *crtc)
  8464. {
  8465. struct drm_i915_private *dev_priv = dev->dev_private;
  8466. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8467. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8468. struct drm_display_mode *mode;
  8469. struct intel_crtc_state pipe_config;
  8470. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8471. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8472. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8473. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8474. enum pipe pipe = intel_crtc->pipe;
  8475. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8476. if (!mode)
  8477. return NULL;
  8478. /*
  8479. * Construct a pipe_config sufficient for getting the clock info
  8480. * back out of crtc_clock_get.
  8481. *
  8482. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8483. * to use a real value here instead.
  8484. */
  8485. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8486. pipe_config.pixel_multiplier = 1;
  8487. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8488. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8489. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8490. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8491. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8492. mode->hdisplay = (htot & 0xffff) + 1;
  8493. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8494. mode->hsync_start = (hsync & 0xffff) + 1;
  8495. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8496. mode->vdisplay = (vtot & 0xffff) + 1;
  8497. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8498. mode->vsync_start = (vsync & 0xffff) + 1;
  8499. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8500. drm_mode_set_name(mode);
  8501. return mode;
  8502. }
  8503. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  8504. {
  8505. struct drm_device *dev = crtc->dev;
  8506. struct drm_i915_private *dev_priv = dev->dev_private;
  8507. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8508. if (!HAS_GMCH_DISPLAY(dev))
  8509. return;
  8510. if (!dev_priv->lvds_downclock_avail)
  8511. return;
  8512. /*
  8513. * Since this is called by a timer, we should never get here in
  8514. * the manual case.
  8515. */
  8516. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  8517. int pipe = intel_crtc->pipe;
  8518. int dpll_reg = DPLL(pipe);
  8519. int dpll;
  8520. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  8521. assert_panel_unlocked(dev_priv, pipe);
  8522. dpll = I915_READ(dpll_reg);
  8523. dpll |= DISPLAY_RATE_SELECT_FPA1;
  8524. I915_WRITE(dpll_reg, dpll);
  8525. intel_wait_for_vblank(dev, pipe);
  8526. dpll = I915_READ(dpll_reg);
  8527. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  8528. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  8529. }
  8530. }
  8531. void intel_mark_busy(struct drm_device *dev)
  8532. {
  8533. struct drm_i915_private *dev_priv = dev->dev_private;
  8534. if (dev_priv->mm.busy)
  8535. return;
  8536. intel_runtime_pm_get(dev_priv);
  8537. i915_update_gfx_val(dev_priv);
  8538. if (INTEL_INFO(dev)->gen >= 6)
  8539. gen6_rps_busy(dev_priv);
  8540. dev_priv->mm.busy = true;
  8541. }
  8542. void intel_mark_idle(struct drm_device *dev)
  8543. {
  8544. struct drm_i915_private *dev_priv = dev->dev_private;
  8545. struct drm_crtc *crtc;
  8546. if (!dev_priv->mm.busy)
  8547. return;
  8548. dev_priv->mm.busy = false;
  8549. for_each_crtc(dev, crtc) {
  8550. if (!crtc->primary->fb)
  8551. continue;
  8552. intel_decrease_pllclock(crtc);
  8553. }
  8554. if (INTEL_INFO(dev)->gen >= 6)
  8555. gen6_rps_idle(dev->dev_private);
  8556. intel_runtime_pm_put(dev_priv);
  8557. }
  8558. static void intel_crtc_set_state(struct intel_crtc *crtc,
  8559. struct intel_crtc_state *crtc_state)
  8560. {
  8561. kfree(crtc->config);
  8562. crtc->config = crtc_state;
  8563. crtc->base.state = &crtc_state->base;
  8564. }
  8565. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8566. {
  8567. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8568. struct drm_device *dev = crtc->dev;
  8569. struct intel_unpin_work *work;
  8570. spin_lock_irq(&dev->event_lock);
  8571. work = intel_crtc->unpin_work;
  8572. intel_crtc->unpin_work = NULL;
  8573. spin_unlock_irq(&dev->event_lock);
  8574. if (work) {
  8575. cancel_work_sync(&work->work);
  8576. kfree(work);
  8577. }
  8578. intel_crtc_set_state(intel_crtc, NULL);
  8579. drm_crtc_cleanup(crtc);
  8580. kfree(intel_crtc);
  8581. }
  8582. static void intel_unpin_work_fn(struct work_struct *__work)
  8583. {
  8584. struct intel_unpin_work *work =
  8585. container_of(__work, struct intel_unpin_work, work);
  8586. struct drm_device *dev = work->crtc->dev;
  8587. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  8588. mutex_lock(&dev->struct_mutex);
  8589. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  8590. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8591. intel_fbc_update(dev);
  8592. if (work->flip_queued_req)
  8593. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8594. mutex_unlock(&dev->struct_mutex);
  8595. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8596. drm_framebuffer_unreference(work->old_fb);
  8597. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  8598. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  8599. kfree(work);
  8600. }
  8601. static void do_intel_finish_page_flip(struct drm_device *dev,
  8602. struct drm_crtc *crtc)
  8603. {
  8604. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8605. struct intel_unpin_work *work;
  8606. unsigned long flags;
  8607. /* Ignore early vblank irqs */
  8608. if (intel_crtc == NULL)
  8609. return;
  8610. /*
  8611. * This is called both by irq handlers and the reset code (to complete
  8612. * lost pageflips) so needs the full irqsave spinlocks.
  8613. */
  8614. spin_lock_irqsave(&dev->event_lock, flags);
  8615. work = intel_crtc->unpin_work;
  8616. /* Ensure we don't miss a work->pending update ... */
  8617. smp_rmb();
  8618. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8619. spin_unlock_irqrestore(&dev->event_lock, flags);
  8620. return;
  8621. }
  8622. page_flip_completed(intel_crtc);
  8623. spin_unlock_irqrestore(&dev->event_lock, flags);
  8624. }
  8625. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8626. {
  8627. struct drm_i915_private *dev_priv = dev->dev_private;
  8628. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8629. do_intel_finish_page_flip(dev, crtc);
  8630. }
  8631. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  8632. {
  8633. struct drm_i915_private *dev_priv = dev->dev_private;
  8634. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  8635. do_intel_finish_page_flip(dev, crtc);
  8636. }
  8637. /* Is 'a' after or equal to 'b'? */
  8638. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8639. {
  8640. return !((a - b) & 0x80000000);
  8641. }
  8642. static bool page_flip_finished(struct intel_crtc *crtc)
  8643. {
  8644. struct drm_device *dev = crtc->base.dev;
  8645. struct drm_i915_private *dev_priv = dev->dev_private;
  8646. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  8647. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  8648. return true;
  8649. /*
  8650. * The relevant registers doen't exist on pre-ctg.
  8651. * As the flip done interrupt doesn't trigger for mmio
  8652. * flips on gmch platforms, a flip count check isn't
  8653. * really needed there. But since ctg has the registers,
  8654. * include it in the check anyway.
  8655. */
  8656. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  8657. return true;
  8658. /*
  8659. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8660. * used the same base address. In that case the mmio flip might
  8661. * have completed, but the CS hasn't even executed the flip yet.
  8662. *
  8663. * A flip count check isn't enough as the CS might have updated
  8664. * the base address just after start of vblank, but before we
  8665. * managed to process the interrupt. This means we'd complete the
  8666. * CS flip too soon.
  8667. *
  8668. * Combining both checks should get us a good enough result. It may
  8669. * still happen that the CS flip has been executed, but has not
  8670. * yet actually completed. But in case the base address is the same
  8671. * anyway, we don't really care.
  8672. */
  8673. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8674. crtc->unpin_work->gtt_offset &&
  8675. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  8676. crtc->unpin_work->flip_count);
  8677. }
  8678. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  8679. {
  8680. struct drm_i915_private *dev_priv = dev->dev_private;
  8681. struct intel_crtc *intel_crtc =
  8682. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  8683. unsigned long flags;
  8684. /*
  8685. * This is called both by irq handlers and the reset code (to complete
  8686. * lost pageflips) so needs the full irqsave spinlocks.
  8687. *
  8688. * NB: An MMIO update of the plane base pointer will also
  8689. * generate a page-flip completion irq, i.e. every modeset
  8690. * is also accompanied by a spurious intel_prepare_page_flip().
  8691. */
  8692. spin_lock_irqsave(&dev->event_lock, flags);
  8693. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  8694. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  8695. spin_unlock_irqrestore(&dev->event_lock, flags);
  8696. }
  8697. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  8698. {
  8699. /* Ensure that the work item is consistent when activating it ... */
  8700. smp_wmb();
  8701. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  8702. /* and that it is marked active as soon as the irq could fire. */
  8703. smp_wmb();
  8704. }
  8705. static int intel_gen2_queue_flip(struct drm_device *dev,
  8706. struct drm_crtc *crtc,
  8707. struct drm_framebuffer *fb,
  8708. struct drm_i915_gem_object *obj,
  8709. struct intel_engine_cs *ring,
  8710. uint32_t flags)
  8711. {
  8712. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8713. u32 flip_mask;
  8714. int ret;
  8715. ret = intel_ring_begin(ring, 6);
  8716. if (ret)
  8717. return ret;
  8718. /* Can't queue multiple flips, so wait for the previous
  8719. * one to finish before executing the next.
  8720. */
  8721. if (intel_crtc->plane)
  8722. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8723. else
  8724. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8725. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8726. intel_ring_emit(ring, MI_NOOP);
  8727. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8728. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8729. intel_ring_emit(ring, fb->pitches[0]);
  8730. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8731. intel_ring_emit(ring, 0); /* aux display base address, unused */
  8732. intel_mark_page_flip_active(intel_crtc);
  8733. __intel_ring_advance(ring);
  8734. return 0;
  8735. }
  8736. static int intel_gen3_queue_flip(struct drm_device *dev,
  8737. struct drm_crtc *crtc,
  8738. struct drm_framebuffer *fb,
  8739. struct drm_i915_gem_object *obj,
  8740. struct intel_engine_cs *ring,
  8741. uint32_t flags)
  8742. {
  8743. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8744. u32 flip_mask;
  8745. int ret;
  8746. ret = intel_ring_begin(ring, 6);
  8747. if (ret)
  8748. return ret;
  8749. if (intel_crtc->plane)
  8750. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8751. else
  8752. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8753. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8754. intel_ring_emit(ring, MI_NOOP);
  8755. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  8756. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8757. intel_ring_emit(ring, fb->pitches[0]);
  8758. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8759. intel_ring_emit(ring, MI_NOOP);
  8760. intel_mark_page_flip_active(intel_crtc);
  8761. __intel_ring_advance(ring);
  8762. return 0;
  8763. }
  8764. static int intel_gen4_queue_flip(struct drm_device *dev,
  8765. struct drm_crtc *crtc,
  8766. struct drm_framebuffer *fb,
  8767. struct drm_i915_gem_object *obj,
  8768. struct intel_engine_cs *ring,
  8769. uint32_t flags)
  8770. {
  8771. struct drm_i915_private *dev_priv = dev->dev_private;
  8772. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8773. uint32_t pf, pipesrc;
  8774. int ret;
  8775. ret = intel_ring_begin(ring, 4);
  8776. if (ret)
  8777. return ret;
  8778. /* i965+ uses the linear or tiled offsets from the
  8779. * Display Registers (which do not change across a page-flip)
  8780. * so we need only reprogram the base address.
  8781. */
  8782. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8783. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8784. intel_ring_emit(ring, fb->pitches[0]);
  8785. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  8786. obj->tiling_mode);
  8787. /* XXX Enabling the panel-fitter across page-flip is so far
  8788. * untested on non-native modes, so ignore it for now.
  8789. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8790. */
  8791. pf = 0;
  8792. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8793. intel_ring_emit(ring, pf | pipesrc);
  8794. intel_mark_page_flip_active(intel_crtc);
  8795. __intel_ring_advance(ring);
  8796. return 0;
  8797. }
  8798. static int intel_gen6_queue_flip(struct drm_device *dev,
  8799. struct drm_crtc *crtc,
  8800. struct drm_framebuffer *fb,
  8801. struct drm_i915_gem_object *obj,
  8802. struct intel_engine_cs *ring,
  8803. uint32_t flags)
  8804. {
  8805. struct drm_i915_private *dev_priv = dev->dev_private;
  8806. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8807. uint32_t pf, pipesrc;
  8808. int ret;
  8809. ret = intel_ring_begin(ring, 4);
  8810. if (ret)
  8811. return ret;
  8812. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8813. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8814. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  8815. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8816. /* Contrary to the suggestions in the documentation,
  8817. * "Enable Panel Fitter" does not seem to be required when page
  8818. * flipping with a non-native mode, and worse causes a normal
  8819. * modeset to fail.
  8820. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8821. */
  8822. pf = 0;
  8823. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8824. intel_ring_emit(ring, pf | pipesrc);
  8825. intel_mark_page_flip_active(intel_crtc);
  8826. __intel_ring_advance(ring);
  8827. return 0;
  8828. }
  8829. static int intel_gen7_queue_flip(struct drm_device *dev,
  8830. struct drm_crtc *crtc,
  8831. struct drm_framebuffer *fb,
  8832. struct drm_i915_gem_object *obj,
  8833. struct intel_engine_cs *ring,
  8834. uint32_t flags)
  8835. {
  8836. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8837. uint32_t plane_bit = 0;
  8838. int len, ret;
  8839. switch (intel_crtc->plane) {
  8840. case PLANE_A:
  8841. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8842. break;
  8843. case PLANE_B:
  8844. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8845. break;
  8846. case PLANE_C:
  8847. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8848. break;
  8849. default:
  8850. WARN_ONCE(1, "unknown plane in flip command\n");
  8851. return -ENODEV;
  8852. }
  8853. len = 4;
  8854. if (ring->id == RCS) {
  8855. len += 6;
  8856. /*
  8857. * On Gen 8, SRM is now taking an extra dword to accommodate
  8858. * 48bits addresses, and we need a NOOP for the batch size to
  8859. * stay even.
  8860. */
  8861. if (IS_GEN8(dev))
  8862. len += 2;
  8863. }
  8864. /*
  8865. * BSpec MI_DISPLAY_FLIP for IVB:
  8866. * "The full packet must be contained within the same cache line."
  8867. *
  8868. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8869. * cacheline, if we ever start emitting more commands before
  8870. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8871. * then do the cacheline alignment, and finally emit the
  8872. * MI_DISPLAY_FLIP.
  8873. */
  8874. ret = intel_ring_cacheline_align(ring);
  8875. if (ret)
  8876. return ret;
  8877. ret = intel_ring_begin(ring, len);
  8878. if (ret)
  8879. return ret;
  8880. /* Unmask the flip-done completion message. Note that the bspec says that
  8881. * we should do this for both the BCS and RCS, and that we must not unmask
  8882. * more than one flip event at any time (or ensure that one flip message
  8883. * can be sent by waiting for flip-done prior to queueing new flips).
  8884. * Experimentation says that BCS works despite DERRMR masking all
  8885. * flip-done completion events and that unmasking all planes at once
  8886. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8887. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8888. */
  8889. if (ring->id == RCS) {
  8890. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8891. intel_ring_emit(ring, DERRMR);
  8892. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8893. DERRMR_PIPEB_PRI_FLIP_DONE |
  8894. DERRMR_PIPEC_PRI_FLIP_DONE));
  8895. if (IS_GEN8(dev))
  8896. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8897. MI_SRM_LRM_GLOBAL_GTT);
  8898. else
  8899. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8900. MI_SRM_LRM_GLOBAL_GTT);
  8901. intel_ring_emit(ring, DERRMR);
  8902. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8903. if (IS_GEN8(dev)) {
  8904. intel_ring_emit(ring, 0);
  8905. intel_ring_emit(ring, MI_NOOP);
  8906. }
  8907. }
  8908. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8909. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8910. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8911. intel_ring_emit(ring, (MI_NOOP));
  8912. intel_mark_page_flip_active(intel_crtc);
  8913. __intel_ring_advance(ring);
  8914. return 0;
  8915. }
  8916. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8917. struct drm_i915_gem_object *obj)
  8918. {
  8919. /*
  8920. * This is not being used for older platforms, because
  8921. * non-availability of flip done interrupt forces us to use
  8922. * CS flips. Older platforms derive flip done using some clever
  8923. * tricks involving the flip_pending status bits and vblank irqs.
  8924. * So using MMIO flips there would disrupt this mechanism.
  8925. */
  8926. if (ring == NULL)
  8927. return true;
  8928. if (INTEL_INFO(ring->dev)->gen < 5)
  8929. return false;
  8930. if (i915.use_mmio_flip < 0)
  8931. return false;
  8932. else if (i915.use_mmio_flip > 0)
  8933. return true;
  8934. else if (i915.enable_execlists)
  8935. return true;
  8936. else
  8937. return ring != i915_gem_request_get_ring(obj->last_read_req);
  8938. }
  8939. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  8940. {
  8941. struct drm_device *dev = intel_crtc->base.dev;
  8942. struct drm_i915_private *dev_priv = dev->dev_private;
  8943. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8944. const enum pipe pipe = intel_crtc->pipe;
  8945. u32 ctl, stride;
  8946. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8947. ctl &= ~PLANE_CTL_TILED_MASK;
  8948. switch (fb->modifier[0]) {
  8949. case DRM_FORMAT_MOD_NONE:
  8950. break;
  8951. case I915_FORMAT_MOD_X_TILED:
  8952. ctl |= PLANE_CTL_TILED_X;
  8953. break;
  8954. case I915_FORMAT_MOD_Y_TILED:
  8955. ctl |= PLANE_CTL_TILED_Y;
  8956. break;
  8957. case I915_FORMAT_MOD_Yf_TILED:
  8958. ctl |= PLANE_CTL_TILED_YF;
  8959. break;
  8960. default:
  8961. MISSING_CASE(fb->modifier[0]);
  8962. }
  8963. /*
  8964. * The stride is either expressed as a multiple of 64 bytes chunks for
  8965. * linear buffers or in number of tiles for tiled buffers.
  8966. */
  8967. stride = fb->pitches[0] /
  8968. intel_fb_stride_alignment(dev, fb->modifier[0],
  8969. fb->pixel_format);
  8970. /*
  8971. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8972. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8973. */
  8974. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8975. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8976. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  8977. POSTING_READ(PLANE_SURF(pipe, 0));
  8978. }
  8979. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  8980. {
  8981. struct drm_device *dev = intel_crtc->base.dev;
  8982. struct drm_i915_private *dev_priv = dev->dev_private;
  8983. struct intel_framebuffer *intel_fb =
  8984. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8985. struct drm_i915_gem_object *obj = intel_fb->obj;
  8986. u32 dspcntr;
  8987. u32 reg;
  8988. reg = DSPCNTR(intel_crtc->plane);
  8989. dspcntr = I915_READ(reg);
  8990. if (obj->tiling_mode != I915_TILING_NONE)
  8991. dspcntr |= DISPPLANE_TILED;
  8992. else
  8993. dspcntr &= ~DISPPLANE_TILED;
  8994. I915_WRITE(reg, dspcntr);
  8995. I915_WRITE(DSPSURF(intel_crtc->plane),
  8996. intel_crtc->unpin_work->gtt_offset);
  8997. POSTING_READ(DSPSURF(intel_crtc->plane));
  8998. }
  8999. /*
  9000. * XXX: This is the temporary way to update the plane registers until we get
  9001. * around to using the usual plane update functions for MMIO flips
  9002. */
  9003. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9004. {
  9005. struct drm_device *dev = intel_crtc->base.dev;
  9006. bool atomic_update;
  9007. u32 start_vbl_count;
  9008. intel_mark_page_flip_active(intel_crtc);
  9009. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9010. if (INTEL_INFO(dev)->gen >= 9)
  9011. skl_do_mmio_flip(intel_crtc);
  9012. else
  9013. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9014. ilk_do_mmio_flip(intel_crtc);
  9015. if (atomic_update)
  9016. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9017. }
  9018. static void intel_mmio_flip_work_func(struct work_struct *work)
  9019. {
  9020. struct intel_crtc *crtc =
  9021. container_of(work, struct intel_crtc, mmio_flip.work);
  9022. struct intel_mmio_flip *mmio_flip;
  9023. mmio_flip = &crtc->mmio_flip;
  9024. if (mmio_flip->req)
  9025. WARN_ON(__i915_wait_request(mmio_flip->req,
  9026. crtc->reset_counter,
  9027. false, NULL, NULL) != 0);
  9028. intel_do_mmio_flip(crtc);
  9029. if (mmio_flip->req) {
  9030. mutex_lock(&crtc->base.dev->struct_mutex);
  9031. i915_gem_request_assign(&mmio_flip->req, NULL);
  9032. mutex_unlock(&crtc->base.dev->struct_mutex);
  9033. }
  9034. }
  9035. static int intel_queue_mmio_flip(struct drm_device *dev,
  9036. struct drm_crtc *crtc,
  9037. struct drm_framebuffer *fb,
  9038. struct drm_i915_gem_object *obj,
  9039. struct intel_engine_cs *ring,
  9040. uint32_t flags)
  9041. {
  9042. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9043. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  9044. obj->last_write_req);
  9045. schedule_work(&intel_crtc->mmio_flip.work);
  9046. return 0;
  9047. }
  9048. static int intel_default_queue_flip(struct drm_device *dev,
  9049. struct drm_crtc *crtc,
  9050. struct drm_framebuffer *fb,
  9051. struct drm_i915_gem_object *obj,
  9052. struct intel_engine_cs *ring,
  9053. uint32_t flags)
  9054. {
  9055. return -ENODEV;
  9056. }
  9057. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9058. struct drm_crtc *crtc)
  9059. {
  9060. struct drm_i915_private *dev_priv = dev->dev_private;
  9061. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9062. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9063. u32 addr;
  9064. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9065. return true;
  9066. if (!work->enable_stall_check)
  9067. return false;
  9068. if (work->flip_ready_vblank == 0) {
  9069. if (work->flip_queued_req &&
  9070. !i915_gem_request_completed(work->flip_queued_req, true))
  9071. return false;
  9072. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9073. }
  9074. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9075. return false;
  9076. /* Potential stall - if we see that the flip has happened,
  9077. * assume a missed interrupt. */
  9078. if (INTEL_INFO(dev)->gen >= 4)
  9079. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9080. else
  9081. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9082. /* There is a potential issue here with a false positive after a flip
  9083. * to the same address. We could address this by checking for a
  9084. * non-incrementing frame counter.
  9085. */
  9086. return addr == work->gtt_offset;
  9087. }
  9088. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9089. {
  9090. struct drm_i915_private *dev_priv = dev->dev_private;
  9091. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9092. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9093. struct intel_unpin_work *work;
  9094. WARN_ON(!in_interrupt());
  9095. if (crtc == NULL)
  9096. return;
  9097. spin_lock(&dev->event_lock);
  9098. work = intel_crtc->unpin_work;
  9099. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9100. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9101. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9102. page_flip_completed(intel_crtc);
  9103. work = NULL;
  9104. }
  9105. if (work != NULL &&
  9106. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9107. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9108. spin_unlock(&dev->event_lock);
  9109. }
  9110. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9111. struct drm_framebuffer *fb,
  9112. struct drm_pending_vblank_event *event,
  9113. uint32_t page_flip_flags)
  9114. {
  9115. struct drm_device *dev = crtc->dev;
  9116. struct drm_i915_private *dev_priv = dev->dev_private;
  9117. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9118. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9119. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9120. struct drm_plane *primary = crtc->primary;
  9121. enum pipe pipe = intel_crtc->pipe;
  9122. struct intel_unpin_work *work;
  9123. struct intel_engine_cs *ring;
  9124. bool mmio_flip;
  9125. int ret;
  9126. /*
  9127. * drm_mode_page_flip_ioctl() should already catch this, but double
  9128. * check to be safe. In the future we may enable pageflipping from
  9129. * a disabled primary plane.
  9130. */
  9131. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9132. return -EBUSY;
  9133. /* Can't change pixel format via MI display flips. */
  9134. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9135. return -EINVAL;
  9136. /*
  9137. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9138. * Note that pitch changes could also affect these register.
  9139. */
  9140. if (INTEL_INFO(dev)->gen > 3 &&
  9141. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9142. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9143. return -EINVAL;
  9144. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9145. goto out_hang;
  9146. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9147. if (work == NULL)
  9148. return -ENOMEM;
  9149. work->event = event;
  9150. work->crtc = crtc;
  9151. work->old_fb = old_fb;
  9152. INIT_WORK(&work->work, intel_unpin_work_fn);
  9153. ret = drm_crtc_vblank_get(crtc);
  9154. if (ret)
  9155. goto free_work;
  9156. /* We borrow the event spin lock for protecting unpin_work */
  9157. spin_lock_irq(&dev->event_lock);
  9158. if (intel_crtc->unpin_work) {
  9159. /* Before declaring the flip queue wedged, check if
  9160. * the hardware completed the operation behind our backs.
  9161. */
  9162. if (__intel_pageflip_stall_check(dev, crtc)) {
  9163. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9164. page_flip_completed(intel_crtc);
  9165. } else {
  9166. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9167. spin_unlock_irq(&dev->event_lock);
  9168. drm_crtc_vblank_put(crtc);
  9169. kfree(work);
  9170. return -EBUSY;
  9171. }
  9172. }
  9173. intel_crtc->unpin_work = work;
  9174. spin_unlock_irq(&dev->event_lock);
  9175. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9176. flush_workqueue(dev_priv->wq);
  9177. /* Reference the objects for the scheduled work. */
  9178. drm_framebuffer_reference(work->old_fb);
  9179. drm_gem_object_reference(&obj->base);
  9180. crtc->primary->fb = fb;
  9181. update_state_fb(crtc->primary);
  9182. work->pending_flip_obj = obj;
  9183. ret = i915_mutex_lock_interruptible(dev);
  9184. if (ret)
  9185. goto cleanup;
  9186. atomic_inc(&intel_crtc->unpin_work_count);
  9187. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9188. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9189. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9190. if (IS_VALLEYVIEW(dev)) {
  9191. ring = &dev_priv->ring[BCS];
  9192. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9193. /* vlv: DISPLAY_FLIP fails to change tiling */
  9194. ring = NULL;
  9195. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9196. ring = &dev_priv->ring[BCS];
  9197. } else if (INTEL_INFO(dev)->gen >= 7) {
  9198. ring = i915_gem_request_get_ring(obj->last_read_req);
  9199. if (ring == NULL || ring->id != RCS)
  9200. ring = &dev_priv->ring[BCS];
  9201. } else {
  9202. ring = &dev_priv->ring[RCS];
  9203. }
  9204. mmio_flip = use_mmio_flip(ring, obj);
  9205. /* When using CS flips, we want to emit semaphores between rings.
  9206. * However, when using mmio flips we will create a task to do the
  9207. * synchronisation, so all we want here is to pin the framebuffer
  9208. * into the display plane and skip any waits.
  9209. */
  9210. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9211. crtc->primary->state,
  9212. mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
  9213. if (ret)
  9214. goto cleanup_pending;
  9215. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9216. + intel_crtc->dspaddr_offset;
  9217. if (mmio_flip) {
  9218. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9219. page_flip_flags);
  9220. if (ret)
  9221. goto cleanup_unpin;
  9222. i915_gem_request_assign(&work->flip_queued_req,
  9223. obj->last_write_req);
  9224. } else {
  9225. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  9226. page_flip_flags);
  9227. if (ret)
  9228. goto cleanup_unpin;
  9229. i915_gem_request_assign(&work->flip_queued_req,
  9230. intel_ring_get_request(ring));
  9231. }
  9232. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9233. work->enable_stall_check = true;
  9234. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9235. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9236. intel_fbc_disable(dev);
  9237. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9238. mutex_unlock(&dev->struct_mutex);
  9239. trace_i915_flip_request(intel_crtc->plane, obj);
  9240. return 0;
  9241. cleanup_unpin:
  9242. intel_unpin_fb_obj(fb, crtc->primary->state);
  9243. cleanup_pending:
  9244. atomic_dec(&intel_crtc->unpin_work_count);
  9245. mutex_unlock(&dev->struct_mutex);
  9246. cleanup:
  9247. crtc->primary->fb = old_fb;
  9248. update_state_fb(crtc->primary);
  9249. drm_gem_object_unreference_unlocked(&obj->base);
  9250. drm_framebuffer_unreference(work->old_fb);
  9251. spin_lock_irq(&dev->event_lock);
  9252. intel_crtc->unpin_work = NULL;
  9253. spin_unlock_irq(&dev->event_lock);
  9254. drm_crtc_vblank_put(crtc);
  9255. free_work:
  9256. kfree(work);
  9257. if (ret == -EIO) {
  9258. out_hang:
  9259. ret = intel_plane_restore(primary);
  9260. if (ret == 0 && event) {
  9261. spin_lock_irq(&dev->event_lock);
  9262. drm_send_vblank_event(dev, pipe, event);
  9263. spin_unlock_irq(&dev->event_lock);
  9264. }
  9265. }
  9266. return ret;
  9267. }
  9268. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9269. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9270. .load_lut = intel_crtc_load_lut,
  9271. .atomic_begin = intel_begin_crtc_commit,
  9272. .atomic_flush = intel_finish_crtc_commit,
  9273. };
  9274. /**
  9275. * intel_modeset_update_staged_output_state
  9276. *
  9277. * Updates the staged output configuration state, e.g. after we've read out the
  9278. * current hw state.
  9279. */
  9280. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  9281. {
  9282. struct intel_crtc *crtc;
  9283. struct intel_encoder *encoder;
  9284. struct intel_connector *connector;
  9285. for_each_intel_connector(dev, connector) {
  9286. connector->new_encoder =
  9287. to_intel_encoder(connector->base.encoder);
  9288. }
  9289. for_each_intel_encoder(dev, encoder) {
  9290. encoder->new_crtc =
  9291. to_intel_crtc(encoder->base.crtc);
  9292. }
  9293. for_each_intel_crtc(dev, crtc) {
  9294. crtc->new_enabled = crtc->base.state->enable;
  9295. }
  9296. }
  9297. /* Transitional helper to copy current connector/encoder state to
  9298. * connector->state. This is needed so that code that is partially
  9299. * converted to atomic does the right thing.
  9300. */
  9301. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9302. {
  9303. struct intel_connector *connector;
  9304. for_each_intel_connector(dev, connector) {
  9305. if (connector->base.encoder) {
  9306. connector->base.state->best_encoder =
  9307. connector->base.encoder;
  9308. connector->base.state->crtc =
  9309. connector->base.encoder->crtc;
  9310. } else {
  9311. connector->base.state->best_encoder = NULL;
  9312. connector->base.state->crtc = NULL;
  9313. }
  9314. }
  9315. }
  9316. /**
  9317. * intel_modeset_commit_output_state
  9318. *
  9319. * This function copies the stage display pipe configuration to the real one.
  9320. */
  9321. static void intel_modeset_commit_output_state(struct drm_device *dev)
  9322. {
  9323. struct intel_crtc *crtc;
  9324. struct intel_encoder *encoder;
  9325. struct intel_connector *connector;
  9326. for_each_intel_connector(dev, connector) {
  9327. connector->base.encoder = &connector->new_encoder->base;
  9328. }
  9329. for_each_intel_encoder(dev, encoder) {
  9330. encoder->base.crtc = &encoder->new_crtc->base;
  9331. }
  9332. for_each_intel_crtc(dev, crtc) {
  9333. crtc->base.state->enable = crtc->new_enabled;
  9334. crtc->base.enabled = crtc->new_enabled;
  9335. }
  9336. intel_modeset_update_connector_atomic_state(dev);
  9337. }
  9338. static void
  9339. connected_sink_compute_bpp(struct intel_connector *connector,
  9340. struct intel_crtc_state *pipe_config)
  9341. {
  9342. int bpp = pipe_config->pipe_bpp;
  9343. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9344. connector->base.base.id,
  9345. connector->base.name);
  9346. /* Don't use an invalid EDID bpc value */
  9347. if (connector->base.display_info.bpc &&
  9348. connector->base.display_info.bpc * 3 < bpp) {
  9349. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9350. bpp, connector->base.display_info.bpc*3);
  9351. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9352. }
  9353. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9354. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9355. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9356. bpp);
  9357. pipe_config->pipe_bpp = 24;
  9358. }
  9359. }
  9360. static int
  9361. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9362. struct intel_crtc_state *pipe_config)
  9363. {
  9364. struct drm_device *dev = crtc->base.dev;
  9365. struct drm_atomic_state *state;
  9366. struct intel_connector *connector;
  9367. int bpp, i;
  9368. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9369. bpp = 10*3;
  9370. else if (INTEL_INFO(dev)->gen >= 5)
  9371. bpp = 12*3;
  9372. else
  9373. bpp = 8*3;
  9374. pipe_config->pipe_bpp = bpp;
  9375. state = pipe_config->base.state;
  9376. /* Clamp display bpp to EDID value */
  9377. for (i = 0; i < state->num_connector; i++) {
  9378. if (!state->connectors[i])
  9379. continue;
  9380. connector = to_intel_connector(state->connectors[i]);
  9381. if (state->connector_states[i]->crtc != &crtc->base)
  9382. continue;
  9383. connected_sink_compute_bpp(connector, pipe_config);
  9384. }
  9385. return bpp;
  9386. }
  9387. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9388. {
  9389. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9390. "type: 0x%x flags: 0x%x\n",
  9391. mode->crtc_clock,
  9392. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9393. mode->crtc_hsync_end, mode->crtc_htotal,
  9394. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9395. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9396. }
  9397. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9398. struct intel_crtc_state *pipe_config,
  9399. const char *context)
  9400. {
  9401. struct drm_device *dev = crtc->base.dev;
  9402. struct drm_plane *plane;
  9403. struct intel_plane *intel_plane;
  9404. struct intel_plane_state *state;
  9405. struct drm_framebuffer *fb;
  9406. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9407. context, pipe_config, pipe_name(crtc->pipe));
  9408. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9409. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9410. pipe_config->pipe_bpp, pipe_config->dither);
  9411. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9412. pipe_config->has_pch_encoder,
  9413. pipe_config->fdi_lanes,
  9414. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9415. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9416. pipe_config->fdi_m_n.tu);
  9417. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9418. pipe_config->has_dp_encoder,
  9419. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9420. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9421. pipe_config->dp_m_n.tu);
  9422. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9423. pipe_config->has_dp_encoder,
  9424. pipe_config->dp_m2_n2.gmch_m,
  9425. pipe_config->dp_m2_n2.gmch_n,
  9426. pipe_config->dp_m2_n2.link_m,
  9427. pipe_config->dp_m2_n2.link_n,
  9428. pipe_config->dp_m2_n2.tu);
  9429. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9430. pipe_config->has_audio,
  9431. pipe_config->has_infoframe);
  9432. DRM_DEBUG_KMS("requested mode:\n");
  9433. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9434. DRM_DEBUG_KMS("adjusted mode:\n");
  9435. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9436. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9437. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9438. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9439. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9440. DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
  9441. DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
  9442. DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
  9443. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9444. pipe_config->gmch_pfit.control,
  9445. pipe_config->gmch_pfit.pgm_ratios,
  9446. pipe_config->gmch_pfit.lvds_border_bits);
  9447. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9448. pipe_config->pch_pfit.pos,
  9449. pipe_config->pch_pfit.size,
  9450. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  9451. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  9452. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  9453. DRM_DEBUG_KMS("planes on this crtc\n");
  9454. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9455. intel_plane = to_intel_plane(plane);
  9456. if (intel_plane->pipe != crtc->pipe)
  9457. continue;
  9458. state = to_intel_plane_state(plane->state);
  9459. fb = state->base.fb;
  9460. if (!fb) {
  9461. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  9462. "disabled, scaler_id = %d\n",
  9463. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9464. plane->base.id, intel_plane->pipe,
  9465. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  9466. drm_plane_index(plane), state->scaler_id);
  9467. continue;
  9468. }
  9469. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  9470. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9471. plane->base.id, intel_plane->pipe,
  9472. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  9473. drm_plane_index(plane));
  9474. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  9475. fb->base.id, fb->width, fb->height, fb->pixel_format);
  9476. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  9477. state->scaler_id,
  9478. state->src.x1 >> 16, state->src.y1 >> 16,
  9479. drm_rect_width(&state->src) >> 16,
  9480. drm_rect_height(&state->src) >> 16,
  9481. state->dst.x1, state->dst.y1,
  9482. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  9483. }
  9484. }
  9485. static bool encoders_cloneable(const struct intel_encoder *a,
  9486. const struct intel_encoder *b)
  9487. {
  9488. /* masks could be asymmetric, so check both ways */
  9489. return a == b || (a->cloneable & (1 << b->type) &&
  9490. b->cloneable & (1 << a->type));
  9491. }
  9492. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9493. struct intel_crtc *crtc,
  9494. struct intel_encoder *encoder)
  9495. {
  9496. struct intel_encoder *source_encoder;
  9497. struct drm_connector_state *connector_state;
  9498. int i;
  9499. for (i = 0; i < state->num_connector; i++) {
  9500. if (!state->connectors[i])
  9501. continue;
  9502. connector_state = state->connector_states[i];
  9503. if (connector_state->crtc != &crtc->base)
  9504. continue;
  9505. source_encoder =
  9506. to_intel_encoder(connector_state->best_encoder);
  9507. if (!encoders_cloneable(encoder, source_encoder))
  9508. return false;
  9509. }
  9510. return true;
  9511. }
  9512. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9513. struct intel_crtc *crtc)
  9514. {
  9515. struct intel_encoder *encoder;
  9516. struct drm_connector_state *connector_state;
  9517. int i;
  9518. for (i = 0; i < state->num_connector; i++) {
  9519. if (!state->connectors[i])
  9520. continue;
  9521. connector_state = state->connector_states[i];
  9522. if (connector_state->crtc != &crtc->base)
  9523. continue;
  9524. encoder = to_intel_encoder(connector_state->best_encoder);
  9525. if (!check_single_encoder_cloning(state, crtc, encoder))
  9526. return false;
  9527. }
  9528. return true;
  9529. }
  9530. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9531. {
  9532. struct drm_device *dev = state->dev;
  9533. struct intel_encoder *encoder;
  9534. struct drm_connector_state *connector_state;
  9535. unsigned int used_ports = 0;
  9536. int i;
  9537. /*
  9538. * Walk the connector list instead of the encoder
  9539. * list to detect the problem on ddi platforms
  9540. * where there's just one encoder per digital port.
  9541. */
  9542. for (i = 0; i < state->num_connector; i++) {
  9543. if (!state->connectors[i])
  9544. continue;
  9545. connector_state = state->connector_states[i];
  9546. if (!connector_state->best_encoder)
  9547. continue;
  9548. encoder = to_intel_encoder(connector_state->best_encoder);
  9549. WARN_ON(!connector_state->crtc);
  9550. switch (encoder->type) {
  9551. unsigned int port_mask;
  9552. case INTEL_OUTPUT_UNKNOWN:
  9553. if (WARN_ON(!HAS_DDI(dev)))
  9554. break;
  9555. case INTEL_OUTPUT_DISPLAYPORT:
  9556. case INTEL_OUTPUT_HDMI:
  9557. case INTEL_OUTPUT_EDP:
  9558. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9559. /* the same port mustn't appear more than once */
  9560. if (used_ports & port_mask)
  9561. return false;
  9562. used_ports |= port_mask;
  9563. default:
  9564. break;
  9565. }
  9566. }
  9567. return true;
  9568. }
  9569. static void
  9570. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9571. {
  9572. struct drm_crtc_state tmp_state;
  9573. struct intel_crtc_scaler_state scaler_state;
  9574. /* Clear only the intel specific part of the crtc state excluding scalers */
  9575. tmp_state = crtc_state->base;
  9576. scaler_state = crtc_state->scaler_state;
  9577. memset(crtc_state, 0, sizeof *crtc_state);
  9578. crtc_state->base = tmp_state;
  9579. crtc_state->scaler_state = scaler_state;
  9580. }
  9581. static struct intel_crtc_state *
  9582. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9583. struct drm_display_mode *mode,
  9584. struct drm_atomic_state *state)
  9585. {
  9586. struct intel_encoder *encoder;
  9587. struct intel_connector *connector;
  9588. struct drm_connector_state *connector_state;
  9589. struct intel_crtc_state *pipe_config;
  9590. int base_bpp, ret = -EINVAL;
  9591. int i;
  9592. bool retry = true;
  9593. if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
  9594. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9595. return ERR_PTR(-EINVAL);
  9596. }
  9597. if (!check_digital_port_conflicts(state)) {
  9598. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9599. return ERR_PTR(-EINVAL);
  9600. }
  9601. pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
  9602. if (IS_ERR(pipe_config))
  9603. return pipe_config;
  9604. clear_intel_crtc_state(pipe_config);
  9605. pipe_config->base.crtc = crtc;
  9606. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  9607. drm_mode_copy(&pipe_config->base.mode, mode);
  9608. pipe_config->cpu_transcoder =
  9609. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9610. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  9611. /*
  9612. * Sanitize sync polarity flags based on requested ones. If neither
  9613. * positive or negative polarity is requested, treat this as meaning
  9614. * negative polarity.
  9615. */
  9616. if (!(pipe_config->base.adjusted_mode.flags &
  9617. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9618. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9619. if (!(pipe_config->base.adjusted_mode.flags &
  9620. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9621. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9622. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  9623. * plane pixel format and any sink constraints into account. Returns the
  9624. * source plane bpp so that dithering can be selected on mismatches
  9625. * after encoders and crtc also have had their say. */
  9626. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9627. pipe_config);
  9628. if (base_bpp < 0)
  9629. goto fail;
  9630. /*
  9631. * Determine the real pipe dimensions. Note that stereo modes can
  9632. * increase the actual pipe size due to the frame doubling and
  9633. * insertion of additional space for blanks between the frame. This
  9634. * is stored in the crtc timings. We use the requested mode to do this
  9635. * computation to clearly distinguish it from the adjusted mode, which
  9636. * can be changed by the connectors in the below retry loop.
  9637. */
  9638. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  9639. &pipe_config->pipe_src_w,
  9640. &pipe_config->pipe_src_h);
  9641. encoder_retry:
  9642. /* Ensure the port clock defaults are reset when retrying. */
  9643. pipe_config->port_clock = 0;
  9644. pipe_config->pixel_multiplier = 1;
  9645. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9646. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9647. CRTC_STEREO_DOUBLE);
  9648. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9649. * adjust it according to limitations or connector properties, and also
  9650. * a chance to reject the mode entirely.
  9651. */
  9652. for (i = 0; i < state->num_connector; i++) {
  9653. connector = to_intel_connector(state->connectors[i]);
  9654. if (!connector)
  9655. continue;
  9656. connector_state = state->connector_states[i];
  9657. if (connector_state->crtc != crtc)
  9658. continue;
  9659. encoder = to_intel_encoder(connector_state->best_encoder);
  9660. if (!(encoder->compute_config(encoder, pipe_config))) {
  9661. DRM_DEBUG_KMS("Encoder config failure\n");
  9662. goto fail;
  9663. }
  9664. }
  9665. /* Set default port clock if not overwritten by the encoder. Needs to be
  9666. * done afterwards in case the encoder adjusts the mode. */
  9667. if (!pipe_config->port_clock)
  9668. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9669. * pipe_config->pixel_multiplier;
  9670. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9671. if (ret < 0) {
  9672. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9673. goto fail;
  9674. }
  9675. if (ret == RETRY) {
  9676. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9677. ret = -EINVAL;
  9678. goto fail;
  9679. }
  9680. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9681. retry = false;
  9682. goto encoder_retry;
  9683. }
  9684. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  9685. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  9686. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9687. return pipe_config;
  9688. fail:
  9689. return ERR_PTR(ret);
  9690. }
  9691. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  9692. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  9693. static void
  9694. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  9695. unsigned *prepare_pipes, unsigned *disable_pipes)
  9696. {
  9697. struct intel_crtc *intel_crtc;
  9698. struct drm_device *dev = crtc->dev;
  9699. struct intel_encoder *encoder;
  9700. struct intel_connector *connector;
  9701. struct drm_crtc *tmp_crtc;
  9702. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  9703. /* Check which crtcs have changed outputs connected to them, these need
  9704. * to be part of the prepare_pipes mask. We don't (yet) support global
  9705. * modeset across multiple crtcs, so modeset_pipes will only have one
  9706. * bit set at most. */
  9707. for_each_intel_connector(dev, connector) {
  9708. if (connector->base.encoder == &connector->new_encoder->base)
  9709. continue;
  9710. if (connector->base.encoder) {
  9711. tmp_crtc = connector->base.encoder->crtc;
  9712. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  9713. }
  9714. if (connector->new_encoder)
  9715. *prepare_pipes |=
  9716. 1 << connector->new_encoder->new_crtc->pipe;
  9717. }
  9718. for_each_intel_encoder(dev, encoder) {
  9719. if (encoder->base.crtc == &encoder->new_crtc->base)
  9720. continue;
  9721. if (encoder->base.crtc) {
  9722. tmp_crtc = encoder->base.crtc;
  9723. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  9724. }
  9725. if (encoder->new_crtc)
  9726. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  9727. }
  9728. /* Check for pipes that will be enabled/disabled ... */
  9729. for_each_intel_crtc(dev, intel_crtc) {
  9730. if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
  9731. continue;
  9732. if (!intel_crtc->new_enabled)
  9733. *disable_pipes |= 1 << intel_crtc->pipe;
  9734. else
  9735. *prepare_pipes |= 1 << intel_crtc->pipe;
  9736. }
  9737. /* set_mode is also used to update properties on life display pipes. */
  9738. intel_crtc = to_intel_crtc(crtc);
  9739. if (intel_crtc->new_enabled)
  9740. *prepare_pipes |= 1 << intel_crtc->pipe;
  9741. /*
  9742. * For simplicity do a full modeset on any pipe where the output routing
  9743. * changed. We could be more clever, but that would require us to be
  9744. * more careful with calling the relevant encoder->mode_set functions.
  9745. */
  9746. if (*prepare_pipes)
  9747. *modeset_pipes = *prepare_pipes;
  9748. /* ... and mask these out. */
  9749. *modeset_pipes &= ~(*disable_pipes);
  9750. *prepare_pipes &= ~(*disable_pipes);
  9751. /*
  9752. * HACK: We don't (yet) fully support global modesets. intel_set_config
  9753. * obies this rule, but the modeset restore mode of
  9754. * intel_modeset_setup_hw_state does not.
  9755. */
  9756. *modeset_pipes &= 1 << intel_crtc->pipe;
  9757. *prepare_pipes &= 1 << intel_crtc->pipe;
  9758. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  9759. *modeset_pipes, *prepare_pipes, *disable_pipes);
  9760. }
  9761. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  9762. {
  9763. struct drm_encoder *encoder;
  9764. struct drm_device *dev = crtc->dev;
  9765. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  9766. if (encoder->crtc == crtc)
  9767. return true;
  9768. return false;
  9769. }
  9770. static void
  9771. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  9772. {
  9773. struct drm_i915_private *dev_priv = dev->dev_private;
  9774. struct intel_encoder *intel_encoder;
  9775. struct intel_crtc *intel_crtc;
  9776. struct drm_connector *connector;
  9777. intel_shared_dpll_commit(dev_priv);
  9778. for_each_intel_encoder(dev, intel_encoder) {
  9779. if (!intel_encoder->base.crtc)
  9780. continue;
  9781. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  9782. if (prepare_pipes & (1 << intel_crtc->pipe))
  9783. intel_encoder->connectors_active = false;
  9784. }
  9785. intel_modeset_commit_output_state(dev);
  9786. /* Double check state. */
  9787. for_each_intel_crtc(dev, intel_crtc) {
  9788. WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
  9789. }
  9790. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9791. if (!connector->encoder || !connector->encoder->crtc)
  9792. continue;
  9793. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  9794. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  9795. struct drm_property *dpms_property =
  9796. dev->mode_config.dpms_property;
  9797. connector->dpms = DRM_MODE_DPMS_ON;
  9798. drm_object_property_set_value(&connector->base,
  9799. dpms_property,
  9800. DRM_MODE_DPMS_ON);
  9801. intel_encoder = to_intel_encoder(connector->encoder);
  9802. intel_encoder->connectors_active = true;
  9803. }
  9804. }
  9805. }
  9806. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9807. {
  9808. int diff;
  9809. if (clock1 == clock2)
  9810. return true;
  9811. if (!clock1 || !clock2)
  9812. return false;
  9813. diff = abs(clock1 - clock2);
  9814. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9815. return true;
  9816. return false;
  9817. }
  9818. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  9819. list_for_each_entry((intel_crtc), \
  9820. &(dev)->mode_config.crtc_list, \
  9821. base.head) \
  9822. if (mask & (1 <<(intel_crtc)->pipe))
  9823. static bool
  9824. intel_pipe_config_compare(struct drm_device *dev,
  9825. struct intel_crtc_state *current_config,
  9826. struct intel_crtc_state *pipe_config)
  9827. {
  9828. #define PIPE_CONF_CHECK_X(name) \
  9829. if (current_config->name != pipe_config->name) { \
  9830. DRM_ERROR("mismatch in " #name " " \
  9831. "(expected 0x%08x, found 0x%08x)\n", \
  9832. current_config->name, \
  9833. pipe_config->name); \
  9834. return false; \
  9835. }
  9836. #define PIPE_CONF_CHECK_I(name) \
  9837. if (current_config->name != pipe_config->name) { \
  9838. DRM_ERROR("mismatch in " #name " " \
  9839. "(expected %i, found %i)\n", \
  9840. current_config->name, \
  9841. pipe_config->name); \
  9842. return false; \
  9843. }
  9844. /* This is required for BDW+ where there is only one set of registers for
  9845. * switching between high and low RR.
  9846. * This macro can be used whenever a comparison has to be made between one
  9847. * hw state and multiple sw state variables.
  9848. */
  9849. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  9850. if ((current_config->name != pipe_config->name) && \
  9851. (current_config->alt_name != pipe_config->name)) { \
  9852. DRM_ERROR("mismatch in " #name " " \
  9853. "(expected %i or %i, found %i)\n", \
  9854. current_config->name, \
  9855. current_config->alt_name, \
  9856. pipe_config->name); \
  9857. return false; \
  9858. }
  9859. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9860. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9861. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  9862. "(expected %i, found %i)\n", \
  9863. current_config->name & (mask), \
  9864. pipe_config->name & (mask)); \
  9865. return false; \
  9866. }
  9867. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9868. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9869. DRM_ERROR("mismatch in " #name " " \
  9870. "(expected %i, found %i)\n", \
  9871. current_config->name, \
  9872. pipe_config->name); \
  9873. return false; \
  9874. }
  9875. #define PIPE_CONF_QUIRK(quirk) \
  9876. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9877. PIPE_CONF_CHECK_I(cpu_transcoder);
  9878. PIPE_CONF_CHECK_I(has_pch_encoder);
  9879. PIPE_CONF_CHECK_I(fdi_lanes);
  9880. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  9881. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  9882. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  9883. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  9884. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  9885. PIPE_CONF_CHECK_I(has_dp_encoder);
  9886. if (INTEL_INFO(dev)->gen < 8) {
  9887. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  9888. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  9889. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  9890. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  9891. PIPE_CONF_CHECK_I(dp_m_n.tu);
  9892. if (current_config->has_drrs) {
  9893. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  9894. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  9895. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  9896. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  9897. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  9898. }
  9899. } else {
  9900. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  9901. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  9902. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  9903. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  9904. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  9905. }
  9906. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9907. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9908. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9909. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9910. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9911. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9912. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9913. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9914. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9915. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9916. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9917. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9918. PIPE_CONF_CHECK_I(pixel_multiplier);
  9919. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9920. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  9921. IS_VALLEYVIEW(dev))
  9922. PIPE_CONF_CHECK_I(limited_color_range);
  9923. PIPE_CONF_CHECK_I(has_infoframe);
  9924. PIPE_CONF_CHECK_I(has_audio);
  9925. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9926. DRM_MODE_FLAG_INTERLACE);
  9927. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9928. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9929. DRM_MODE_FLAG_PHSYNC);
  9930. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9931. DRM_MODE_FLAG_NHSYNC);
  9932. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9933. DRM_MODE_FLAG_PVSYNC);
  9934. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9935. DRM_MODE_FLAG_NVSYNC);
  9936. }
  9937. PIPE_CONF_CHECK_I(pipe_src_w);
  9938. PIPE_CONF_CHECK_I(pipe_src_h);
  9939. /*
  9940. * FIXME: BIOS likes to set up a cloned config with lvds+external
  9941. * screen. Since we don't yet re-compute the pipe config when moving
  9942. * just the lvds port away to another pipe the sw tracking won't match.
  9943. *
  9944. * Proper atomic modesets with recomputed global state will fix this.
  9945. * Until then just don't check gmch state for inherited modes.
  9946. */
  9947. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  9948. PIPE_CONF_CHECK_I(gmch_pfit.control);
  9949. /* pfit ratios are autocomputed by the hw on gen4+ */
  9950. if (INTEL_INFO(dev)->gen < 4)
  9951. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  9952. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  9953. }
  9954. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9955. if (current_config->pch_pfit.enabled) {
  9956. PIPE_CONF_CHECK_I(pch_pfit.pos);
  9957. PIPE_CONF_CHECK_I(pch_pfit.size);
  9958. }
  9959. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9960. /* BDW+ don't expose a synchronous way to read the state */
  9961. if (IS_HASWELL(dev))
  9962. PIPE_CONF_CHECK_I(ips_enabled);
  9963. PIPE_CONF_CHECK_I(double_wide);
  9964. PIPE_CONF_CHECK_X(ddi_pll_sel);
  9965. PIPE_CONF_CHECK_I(shared_dpll);
  9966. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9967. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9968. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9969. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9970. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9971. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9972. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9973. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9974. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  9975. PIPE_CONF_CHECK_I(pipe_bpp);
  9976. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9977. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9978. #undef PIPE_CONF_CHECK_X
  9979. #undef PIPE_CONF_CHECK_I
  9980. #undef PIPE_CONF_CHECK_I_ALT
  9981. #undef PIPE_CONF_CHECK_FLAGS
  9982. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9983. #undef PIPE_CONF_QUIRK
  9984. return true;
  9985. }
  9986. static void check_wm_state(struct drm_device *dev)
  9987. {
  9988. struct drm_i915_private *dev_priv = dev->dev_private;
  9989. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9990. struct intel_crtc *intel_crtc;
  9991. int plane;
  9992. if (INTEL_INFO(dev)->gen < 9)
  9993. return;
  9994. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9995. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9996. for_each_intel_crtc(dev, intel_crtc) {
  9997. struct skl_ddb_entry *hw_entry, *sw_entry;
  9998. const enum pipe pipe = intel_crtc->pipe;
  9999. if (!intel_crtc->active)
  10000. continue;
  10001. /* planes */
  10002. for_each_plane(dev_priv, pipe, plane) {
  10003. hw_entry = &hw_ddb.plane[pipe][plane];
  10004. sw_entry = &sw_ddb->plane[pipe][plane];
  10005. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10006. continue;
  10007. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10008. "(expected (%u,%u), found (%u,%u))\n",
  10009. pipe_name(pipe), plane + 1,
  10010. sw_entry->start, sw_entry->end,
  10011. hw_entry->start, hw_entry->end);
  10012. }
  10013. /* cursor */
  10014. hw_entry = &hw_ddb.cursor[pipe];
  10015. sw_entry = &sw_ddb->cursor[pipe];
  10016. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10017. continue;
  10018. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10019. "(expected (%u,%u), found (%u,%u))\n",
  10020. pipe_name(pipe),
  10021. sw_entry->start, sw_entry->end,
  10022. hw_entry->start, hw_entry->end);
  10023. }
  10024. }
  10025. static void
  10026. check_connector_state(struct drm_device *dev)
  10027. {
  10028. struct intel_connector *connector;
  10029. for_each_intel_connector(dev, connector) {
  10030. /* This also checks the encoder/connector hw state with the
  10031. * ->get_hw_state callbacks. */
  10032. intel_connector_check_state(connector);
  10033. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10034. "connector's staged encoder doesn't match current encoder\n");
  10035. }
  10036. }
  10037. static void
  10038. check_encoder_state(struct drm_device *dev)
  10039. {
  10040. struct intel_encoder *encoder;
  10041. struct intel_connector *connector;
  10042. for_each_intel_encoder(dev, encoder) {
  10043. bool enabled = false;
  10044. bool active = false;
  10045. enum pipe pipe, tracked_pipe;
  10046. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10047. encoder->base.base.id,
  10048. encoder->base.name);
  10049. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10050. "encoder's stage crtc doesn't match current crtc\n");
  10051. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10052. "encoder's active_connectors set, but no crtc\n");
  10053. for_each_intel_connector(dev, connector) {
  10054. if (connector->base.encoder != &encoder->base)
  10055. continue;
  10056. enabled = true;
  10057. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10058. active = true;
  10059. }
  10060. /*
  10061. * for MST connectors if we unplug the connector is gone
  10062. * away but the encoder is still connected to a crtc
  10063. * until a modeset happens in response to the hotplug.
  10064. */
  10065. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10066. continue;
  10067. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10068. "encoder's enabled state mismatch "
  10069. "(expected %i, found %i)\n",
  10070. !!encoder->base.crtc, enabled);
  10071. I915_STATE_WARN(active && !encoder->base.crtc,
  10072. "active encoder with no crtc\n");
  10073. I915_STATE_WARN(encoder->connectors_active != active,
  10074. "encoder's computed active state doesn't match tracked active state "
  10075. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10076. active = encoder->get_hw_state(encoder, &pipe);
  10077. I915_STATE_WARN(active != encoder->connectors_active,
  10078. "encoder's hw state doesn't match sw tracking "
  10079. "(expected %i, found %i)\n",
  10080. encoder->connectors_active, active);
  10081. if (!encoder->base.crtc)
  10082. continue;
  10083. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10084. I915_STATE_WARN(active && pipe != tracked_pipe,
  10085. "active encoder's pipe doesn't match"
  10086. "(expected %i, found %i)\n",
  10087. tracked_pipe, pipe);
  10088. }
  10089. }
  10090. static void
  10091. check_crtc_state(struct drm_device *dev)
  10092. {
  10093. struct drm_i915_private *dev_priv = dev->dev_private;
  10094. struct intel_crtc *crtc;
  10095. struct intel_encoder *encoder;
  10096. struct intel_crtc_state pipe_config;
  10097. for_each_intel_crtc(dev, crtc) {
  10098. bool enabled = false;
  10099. bool active = false;
  10100. memset(&pipe_config, 0, sizeof(pipe_config));
  10101. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10102. crtc->base.base.id);
  10103. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10104. "active crtc, but not enabled in sw tracking\n");
  10105. for_each_intel_encoder(dev, encoder) {
  10106. if (encoder->base.crtc != &crtc->base)
  10107. continue;
  10108. enabled = true;
  10109. if (encoder->connectors_active)
  10110. active = true;
  10111. }
  10112. I915_STATE_WARN(active != crtc->active,
  10113. "crtc's computed active state doesn't match tracked active state "
  10114. "(expected %i, found %i)\n", active, crtc->active);
  10115. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10116. "crtc's computed enabled state doesn't match tracked enabled state "
  10117. "(expected %i, found %i)\n", enabled,
  10118. crtc->base.state->enable);
  10119. active = dev_priv->display.get_pipe_config(crtc,
  10120. &pipe_config);
  10121. /* hw state is inconsistent with the pipe quirk */
  10122. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10123. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10124. active = crtc->active;
  10125. for_each_intel_encoder(dev, encoder) {
  10126. enum pipe pipe;
  10127. if (encoder->base.crtc != &crtc->base)
  10128. continue;
  10129. if (encoder->get_hw_state(encoder, &pipe))
  10130. encoder->get_config(encoder, &pipe_config);
  10131. }
  10132. I915_STATE_WARN(crtc->active != active,
  10133. "crtc active state doesn't match with hw state "
  10134. "(expected %i, found %i)\n", crtc->active, active);
  10135. if (active &&
  10136. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10137. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10138. intel_dump_pipe_config(crtc, &pipe_config,
  10139. "[hw state]");
  10140. intel_dump_pipe_config(crtc, crtc->config,
  10141. "[sw state]");
  10142. }
  10143. }
  10144. }
  10145. static void
  10146. check_shared_dpll_state(struct drm_device *dev)
  10147. {
  10148. struct drm_i915_private *dev_priv = dev->dev_private;
  10149. struct intel_crtc *crtc;
  10150. struct intel_dpll_hw_state dpll_hw_state;
  10151. int i;
  10152. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10153. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10154. int enabled_crtcs = 0, active_crtcs = 0;
  10155. bool active;
  10156. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10157. DRM_DEBUG_KMS("%s\n", pll->name);
  10158. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10159. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10160. "more active pll users than references: %i vs %i\n",
  10161. pll->active, hweight32(pll->config.crtc_mask));
  10162. I915_STATE_WARN(pll->active && !pll->on,
  10163. "pll in active use but not on in sw tracking\n");
  10164. I915_STATE_WARN(pll->on && !pll->active,
  10165. "pll in on but not on in use in sw tracking\n");
  10166. I915_STATE_WARN(pll->on != active,
  10167. "pll on state mismatch (expected %i, found %i)\n",
  10168. pll->on, active);
  10169. for_each_intel_crtc(dev, crtc) {
  10170. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10171. enabled_crtcs++;
  10172. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10173. active_crtcs++;
  10174. }
  10175. I915_STATE_WARN(pll->active != active_crtcs,
  10176. "pll active crtcs mismatch (expected %i, found %i)\n",
  10177. pll->active, active_crtcs);
  10178. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10179. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10180. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10181. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10182. sizeof(dpll_hw_state)),
  10183. "pll hw state mismatch\n");
  10184. }
  10185. }
  10186. void
  10187. intel_modeset_check_state(struct drm_device *dev)
  10188. {
  10189. check_wm_state(dev);
  10190. check_connector_state(dev);
  10191. check_encoder_state(dev);
  10192. check_crtc_state(dev);
  10193. check_shared_dpll_state(dev);
  10194. }
  10195. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10196. int dotclock)
  10197. {
  10198. /*
  10199. * FDI already provided one idea for the dotclock.
  10200. * Yell if the encoder disagrees.
  10201. */
  10202. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10203. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10204. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10205. }
  10206. static void update_scanline_offset(struct intel_crtc *crtc)
  10207. {
  10208. struct drm_device *dev = crtc->base.dev;
  10209. /*
  10210. * The scanline counter increments at the leading edge of hsync.
  10211. *
  10212. * On most platforms it starts counting from vtotal-1 on the
  10213. * first active line. That means the scanline counter value is
  10214. * always one less than what we would expect. Ie. just after
  10215. * start of vblank, which also occurs at start of hsync (on the
  10216. * last active line), the scanline counter will read vblank_start-1.
  10217. *
  10218. * On gen2 the scanline counter starts counting from 1 instead
  10219. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10220. * to keep the value positive), instead of adding one.
  10221. *
  10222. * On HSW+ the behaviour of the scanline counter depends on the output
  10223. * type. For DP ports it behaves like most other platforms, but on HDMI
  10224. * there's an extra 1 line difference. So we need to add two instead of
  10225. * one to the value.
  10226. */
  10227. if (IS_GEN2(dev)) {
  10228. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10229. int vtotal;
  10230. vtotal = mode->crtc_vtotal;
  10231. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10232. vtotal /= 2;
  10233. crtc->scanline_offset = vtotal - 1;
  10234. } else if (HAS_DDI(dev) &&
  10235. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10236. crtc->scanline_offset = 2;
  10237. } else
  10238. crtc->scanline_offset = 1;
  10239. }
  10240. static struct intel_crtc_state *
  10241. intel_modeset_compute_config(struct drm_crtc *crtc,
  10242. struct drm_display_mode *mode,
  10243. struct drm_atomic_state *state,
  10244. unsigned *modeset_pipes,
  10245. unsigned *prepare_pipes,
  10246. unsigned *disable_pipes)
  10247. {
  10248. struct drm_device *dev = crtc->dev;
  10249. struct intel_crtc_state *pipe_config = NULL;
  10250. struct intel_crtc *intel_crtc;
  10251. int ret = 0;
  10252. ret = drm_atomic_add_affected_connectors(state, crtc);
  10253. if (ret)
  10254. return ERR_PTR(ret);
  10255. intel_modeset_affected_pipes(crtc, modeset_pipes,
  10256. prepare_pipes, disable_pipes);
  10257. for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
  10258. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10259. if (IS_ERR(pipe_config))
  10260. return pipe_config;
  10261. pipe_config->base.enable = false;
  10262. }
  10263. /*
  10264. * Note this needs changes when we start tracking multiple modes
  10265. * and crtcs. At that point we'll need to compute the whole config
  10266. * (i.e. one pipe_config for each crtc) rather than just the one
  10267. * for this crtc.
  10268. */
  10269. for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
  10270. /* FIXME: For now we still expect modeset_pipes has at most
  10271. * one bit set. */
  10272. if (WARN_ON(&intel_crtc->base != crtc))
  10273. continue;
  10274. pipe_config = intel_modeset_pipe_config(crtc, mode, state);
  10275. if (IS_ERR(pipe_config))
  10276. return pipe_config;
  10277. pipe_config->base.enable = true;
  10278. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10279. "[modeset]");
  10280. }
  10281. return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
  10282. }
  10283. static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
  10284. unsigned modeset_pipes,
  10285. unsigned disable_pipes)
  10286. {
  10287. struct drm_device *dev = state->dev;
  10288. struct drm_i915_private *dev_priv = to_i915(dev);
  10289. unsigned clear_pipes = modeset_pipes | disable_pipes;
  10290. struct intel_crtc *intel_crtc;
  10291. int ret = 0;
  10292. if (!dev_priv->display.crtc_compute_clock)
  10293. return 0;
  10294. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  10295. if (ret)
  10296. goto done;
  10297. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  10298. struct intel_crtc_state *crtc_state =
  10299. intel_atomic_get_crtc_state(state, intel_crtc);
  10300. /* Modeset pipes should have a new state by now */
  10301. if (WARN_ON(IS_ERR(crtc_state)))
  10302. continue;
  10303. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10304. crtc_state);
  10305. if (ret) {
  10306. intel_shared_dpll_abort_config(dev_priv);
  10307. goto done;
  10308. }
  10309. }
  10310. done:
  10311. return ret;
  10312. }
  10313. static int __intel_set_mode(struct drm_crtc *crtc,
  10314. struct drm_display_mode *mode,
  10315. int x, int y, struct drm_framebuffer *fb,
  10316. struct intel_crtc_state *pipe_config,
  10317. unsigned modeset_pipes,
  10318. unsigned prepare_pipes,
  10319. unsigned disable_pipes)
  10320. {
  10321. struct drm_device *dev = crtc->dev;
  10322. struct drm_i915_private *dev_priv = dev->dev_private;
  10323. struct drm_display_mode *saved_mode;
  10324. struct drm_atomic_state *state = pipe_config->base.state;
  10325. struct intel_crtc_state *crtc_state_copy = NULL;
  10326. struct intel_crtc *intel_crtc;
  10327. int ret = 0;
  10328. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  10329. if (!saved_mode)
  10330. return -ENOMEM;
  10331. crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
  10332. if (!crtc_state_copy) {
  10333. ret = -ENOMEM;
  10334. goto done;
  10335. }
  10336. *saved_mode = crtc->mode;
  10337. /*
  10338. * See if the config requires any additional preparation, e.g.
  10339. * to adjust global state with pipes off. We need to do this
  10340. * here so we can get the modeset_pipe updated config for the new
  10341. * mode set on this crtc. For other crtcs we need to use the
  10342. * adjusted_mode bits in the crtc directly.
  10343. */
  10344. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  10345. ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
  10346. if (ret)
  10347. goto done;
  10348. /* may have added more to prepare_pipes than we should */
  10349. prepare_pipes &= ~disable_pipes;
  10350. }
  10351. ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
  10352. if (ret)
  10353. goto done;
  10354. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  10355. intel_crtc_disable(&intel_crtc->base);
  10356. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  10357. if (intel_crtc->base.state->enable)
  10358. dev_priv->display.crtc_disable(&intel_crtc->base);
  10359. }
  10360. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  10361. * to set it here already despite that we pass it down the callchain.
  10362. *
  10363. * Note we'll need to fix this up when we start tracking multiple
  10364. * pipes; here we assume a single modeset_pipe and only track the
  10365. * single crtc and mode.
  10366. */
  10367. if (modeset_pipes) {
  10368. crtc->mode = *mode;
  10369. /* mode_set/enable/disable functions rely on a correct pipe
  10370. * config. */
  10371. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  10372. /*
  10373. * Calculate and store various constants which
  10374. * are later needed by vblank and swap-completion
  10375. * timestamping. They are derived from true hwmode.
  10376. */
  10377. drm_calc_timestamping_constants(crtc,
  10378. &pipe_config->base.adjusted_mode);
  10379. }
  10380. /* Only after disabling all output pipelines that will be changed can we
  10381. * update the the output configuration. */
  10382. intel_modeset_update_state(dev, prepare_pipes);
  10383. modeset_update_crtc_power_domains(state);
  10384. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  10385. struct drm_plane *primary = intel_crtc->base.primary;
  10386. int vdisplay, hdisplay;
  10387. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  10388. ret = drm_plane_helper_update(primary, &intel_crtc->base,
  10389. fb, 0, 0,
  10390. hdisplay, vdisplay,
  10391. x << 16, y << 16,
  10392. hdisplay << 16, vdisplay << 16);
  10393. }
  10394. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10395. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  10396. update_scanline_offset(intel_crtc);
  10397. dev_priv->display.crtc_enable(&intel_crtc->base);
  10398. }
  10399. /* FIXME: add subpixel order */
  10400. done:
  10401. if (ret && crtc->state->enable)
  10402. crtc->mode = *saved_mode;
  10403. if (ret == 0 && pipe_config) {
  10404. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10405. /* The pipe_config will be freed with the atomic state, so
  10406. * make a copy. */
  10407. memcpy(crtc_state_copy, intel_crtc->config,
  10408. sizeof *crtc_state_copy);
  10409. intel_crtc->config = crtc_state_copy;
  10410. intel_crtc->base.state = &crtc_state_copy->base;
  10411. } else {
  10412. kfree(crtc_state_copy);
  10413. }
  10414. kfree(saved_mode);
  10415. return ret;
  10416. }
  10417. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  10418. struct drm_display_mode *mode,
  10419. int x, int y, struct drm_framebuffer *fb,
  10420. struct intel_crtc_state *pipe_config,
  10421. unsigned modeset_pipes,
  10422. unsigned prepare_pipes,
  10423. unsigned disable_pipes)
  10424. {
  10425. int ret;
  10426. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  10427. prepare_pipes, disable_pipes);
  10428. if (ret == 0)
  10429. intel_modeset_check_state(crtc->dev);
  10430. return ret;
  10431. }
  10432. static int intel_set_mode(struct drm_crtc *crtc,
  10433. struct drm_display_mode *mode,
  10434. int x, int y, struct drm_framebuffer *fb,
  10435. struct drm_atomic_state *state)
  10436. {
  10437. struct intel_crtc_state *pipe_config;
  10438. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  10439. int ret = 0;
  10440. pipe_config = intel_modeset_compute_config(crtc, mode, state,
  10441. &modeset_pipes,
  10442. &prepare_pipes,
  10443. &disable_pipes);
  10444. if (IS_ERR(pipe_config)) {
  10445. ret = PTR_ERR(pipe_config);
  10446. goto out;
  10447. }
  10448. ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  10449. modeset_pipes, prepare_pipes,
  10450. disable_pipes);
  10451. if (ret)
  10452. goto out;
  10453. out:
  10454. return ret;
  10455. }
  10456. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10457. {
  10458. struct drm_device *dev = crtc->dev;
  10459. struct drm_atomic_state *state;
  10460. struct intel_encoder *encoder;
  10461. struct intel_connector *connector;
  10462. struct drm_connector_state *connector_state;
  10463. state = drm_atomic_state_alloc(dev);
  10464. if (!state) {
  10465. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  10466. crtc->base.id);
  10467. return;
  10468. }
  10469. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10470. /* The force restore path in the HW readout code relies on the staged
  10471. * config still keeping the user requested config while the actual
  10472. * state has been overwritten by the configuration read from HW. We
  10473. * need to copy the staged config to the atomic state, otherwise the
  10474. * mode set will just reapply the state the HW is already in. */
  10475. for_each_intel_encoder(dev, encoder) {
  10476. if (&encoder->new_crtc->base != crtc)
  10477. continue;
  10478. for_each_intel_connector(dev, connector) {
  10479. if (connector->new_encoder != encoder)
  10480. continue;
  10481. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  10482. if (IS_ERR(connector_state)) {
  10483. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  10484. connector->base.base.id,
  10485. connector->base.name,
  10486. PTR_ERR(connector_state));
  10487. continue;
  10488. }
  10489. connector_state->crtc = crtc;
  10490. connector_state->best_encoder = &encoder->base;
  10491. }
  10492. }
  10493. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
  10494. state);
  10495. drm_atomic_state_free(state);
  10496. }
  10497. #undef for_each_intel_crtc_masked
  10498. static void intel_set_config_free(struct intel_set_config *config)
  10499. {
  10500. if (!config)
  10501. return;
  10502. kfree(config->save_connector_encoders);
  10503. kfree(config->save_encoder_crtcs);
  10504. kfree(config->save_crtc_enabled);
  10505. kfree(config);
  10506. }
  10507. static int intel_set_config_save_state(struct drm_device *dev,
  10508. struct intel_set_config *config)
  10509. {
  10510. struct drm_crtc *crtc;
  10511. struct drm_encoder *encoder;
  10512. struct drm_connector *connector;
  10513. int count;
  10514. config->save_crtc_enabled =
  10515. kcalloc(dev->mode_config.num_crtc,
  10516. sizeof(bool), GFP_KERNEL);
  10517. if (!config->save_crtc_enabled)
  10518. return -ENOMEM;
  10519. config->save_encoder_crtcs =
  10520. kcalloc(dev->mode_config.num_encoder,
  10521. sizeof(struct drm_crtc *), GFP_KERNEL);
  10522. if (!config->save_encoder_crtcs)
  10523. return -ENOMEM;
  10524. config->save_connector_encoders =
  10525. kcalloc(dev->mode_config.num_connector,
  10526. sizeof(struct drm_encoder *), GFP_KERNEL);
  10527. if (!config->save_connector_encoders)
  10528. return -ENOMEM;
  10529. /* Copy data. Note that driver private data is not affected.
  10530. * Should anything bad happen only the expected state is
  10531. * restored, not the drivers personal bookkeeping.
  10532. */
  10533. count = 0;
  10534. for_each_crtc(dev, crtc) {
  10535. config->save_crtc_enabled[count++] = crtc->state->enable;
  10536. }
  10537. count = 0;
  10538. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  10539. config->save_encoder_crtcs[count++] = encoder->crtc;
  10540. }
  10541. count = 0;
  10542. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10543. config->save_connector_encoders[count++] = connector->encoder;
  10544. }
  10545. return 0;
  10546. }
  10547. static void intel_set_config_restore_state(struct drm_device *dev,
  10548. struct intel_set_config *config)
  10549. {
  10550. struct intel_crtc *crtc;
  10551. struct intel_encoder *encoder;
  10552. struct intel_connector *connector;
  10553. int count;
  10554. count = 0;
  10555. for_each_intel_crtc(dev, crtc) {
  10556. crtc->new_enabled = config->save_crtc_enabled[count++];
  10557. }
  10558. count = 0;
  10559. for_each_intel_encoder(dev, encoder) {
  10560. encoder->new_crtc =
  10561. to_intel_crtc(config->save_encoder_crtcs[count++]);
  10562. }
  10563. count = 0;
  10564. for_each_intel_connector(dev, connector) {
  10565. connector->new_encoder =
  10566. to_intel_encoder(config->save_connector_encoders[count++]);
  10567. }
  10568. }
  10569. static bool
  10570. is_crtc_connector_off(struct drm_mode_set *set)
  10571. {
  10572. int i;
  10573. if (set->num_connectors == 0)
  10574. return false;
  10575. if (WARN_ON(set->connectors == NULL))
  10576. return false;
  10577. for (i = 0; i < set->num_connectors; i++)
  10578. if (set->connectors[i]->encoder &&
  10579. set->connectors[i]->encoder->crtc == set->crtc &&
  10580. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  10581. return true;
  10582. return false;
  10583. }
  10584. static void
  10585. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  10586. struct intel_set_config *config)
  10587. {
  10588. /* We should be able to check here if the fb has the same properties
  10589. * and then just flip_or_move it */
  10590. if (is_crtc_connector_off(set)) {
  10591. config->mode_changed = true;
  10592. } else if (set->crtc->primary->fb != set->fb) {
  10593. /*
  10594. * If we have no fb, we can only flip as long as the crtc is
  10595. * active, otherwise we need a full mode set. The crtc may
  10596. * be active if we've only disabled the primary plane, or
  10597. * in fastboot situations.
  10598. */
  10599. if (set->crtc->primary->fb == NULL) {
  10600. struct intel_crtc *intel_crtc =
  10601. to_intel_crtc(set->crtc);
  10602. if (intel_crtc->active) {
  10603. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  10604. config->fb_changed = true;
  10605. } else {
  10606. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  10607. config->mode_changed = true;
  10608. }
  10609. } else if (set->fb == NULL) {
  10610. config->mode_changed = true;
  10611. } else if (set->fb->pixel_format !=
  10612. set->crtc->primary->fb->pixel_format) {
  10613. config->mode_changed = true;
  10614. } else {
  10615. config->fb_changed = true;
  10616. }
  10617. }
  10618. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  10619. config->fb_changed = true;
  10620. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  10621. DRM_DEBUG_KMS("modes are different, full mode set\n");
  10622. drm_mode_debug_printmodeline(&set->crtc->mode);
  10623. drm_mode_debug_printmodeline(set->mode);
  10624. config->mode_changed = true;
  10625. }
  10626. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  10627. set->crtc->base.id, config->mode_changed, config->fb_changed);
  10628. }
  10629. static int
  10630. intel_modeset_stage_output_state(struct drm_device *dev,
  10631. struct drm_mode_set *set,
  10632. struct intel_set_config *config,
  10633. struct drm_atomic_state *state)
  10634. {
  10635. struct intel_connector *connector;
  10636. struct drm_connector_state *connector_state;
  10637. struct intel_encoder *encoder;
  10638. struct intel_crtc *crtc;
  10639. int ro;
  10640. /* The upper layers ensure that we either disable a crtc or have a list
  10641. * of connectors. For paranoia, double-check this. */
  10642. WARN_ON(!set->fb && (set->num_connectors != 0));
  10643. WARN_ON(set->fb && (set->num_connectors == 0));
  10644. for_each_intel_connector(dev, connector) {
  10645. /* Otherwise traverse passed in connector list and get encoders
  10646. * for them. */
  10647. for (ro = 0; ro < set->num_connectors; ro++) {
  10648. if (set->connectors[ro] == &connector->base) {
  10649. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  10650. break;
  10651. }
  10652. }
  10653. /* If we disable the crtc, disable all its connectors. Also, if
  10654. * the connector is on the changing crtc but not on the new
  10655. * connector list, disable it. */
  10656. if ((!set->fb || ro == set->num_connectors) &&
  10657. connector->base.encoder &&
  10658. connector->base.encoder->crtc == set->crtc) {
  10659. connector->new_encoder = NULL;
  10660. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  10661. connector->base.base.id,
  10662. connector->base.name);
  10663. }
  10664. if (&connector->new_encoder->base != connector->base.encoder) {
  10665. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
  10666. connector->base.base.id,
  10667. connector->base.name);
  10668. config->mode_changed = true;
  10669. }
  10670. }
  10671. /* connector->new_encoder is now updated for all connectors. */
  10672. /* Update crtc of enabled connectors. */
  10673. for_each_intel_connector(dev, connector) {
  10674. struct drm_crtc *new_crtc;
  10675. if (!connector->new_encoder)
  10676. continue;
  10677. new_crtc = connector->new_encoder->base.crtc;
  10678. for (ro = 0; ro < set->num_connectors; ro++) {
  10679. if (set->connectors[ro] == &connector->base)
  10680. new_crtc = set->crtc;
  10681. }
  10682. /* Make sure the new CRTC will work with the encoder */
  10683. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  10684. new_crtc)) {
  10685. return -EINVAL;
  10686. }
  10687. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  10688. connector_state =
  10689. drm_atomic_get_connector_state(state, &connector->base);
  10690. if (IS_ERR(connector_state))
  10691. return PTR_ERR(connector_state);
  10692. connector_state->crtc = new_crtc;
  10693. connector_state->best_encoder = &connector->new_encoder->base;
  10694. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  10695. connector->base.base.id,
  10696. connector->base.name,
  10697. new_crtc->base.id);
  10698. }
  10699. /* Check for any encoders that needs to be disabled. */
  10700. for_each_intel_encoder(dev, encoder) {
  10701. int num_connectors = 0;
  10702. for_each_intel_connector(dev, connector) {
  10703. if (connector->new_encoder == encoder) {
  10704. WARN_ON(!connector->new_encoder->new_crtc);
  10705. num_connectors++;
  10706. }
  10707. }
  10708. if (num_connectors == 0)
  10709. encoder->new_crtc = NULL;
  10710. else if (num_connectors > 1)
  10711. return -EINVAL;
  10712. /* Only now check for crtc changes so we don't miss encoders
  10713. * that will be disabled. */
  10714. if (&encoder->new_crtc->base != encoder->base.crtc) {
  10715. DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
  10716. encoder->base.base.id,
  10717. encoder->base.name);
  10718. config->mode_changed = true;
  10719. }
  10720. }
  10721. /* Now we've also updated encoder->new_crtc for all encoders. */
  10722. for_each_intel_connector(dev, connector) {
  10723. connector_state =
  10724. drm_atomic_get_connector_state(state, &connector->base);
  10725. if (IS_ERR(connector_state))
  10726. return PTR_ERR(connector_state);
  10727. if (connector->new_encoder) {
  10728. if (connector->new_encoder != connector->encoder)
  10729. connector->encoder = connector->new_encoder;
  10730. } else {
  10731. connector_state->crtc = NULL;
  10732. connector_state->best_encoder = NULL;
  10733. }
  10734. }
  10735. for_each_intel_crtc(dev, crtc) {
  10736. crtc->new_enabled = false;
  10737. for_each_intel_encoder(dev, encoder) {
  10738. if (encoder->new_crtc == crtc) {
  10739. crtc->new_enabled = true;
  10740. break;
  10741. }
  10742. }
  10743. if (crtc->new_enabled != crtc->base.state->enable) {
  10744. DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
  10745. crtc->base.base.id,
  10746. crtc->new_enabled ? "en" : "dis");
  10747. config->mode_changed = true;
  10748. }
  10749. }
  10750. return 0;
  10751. }
  10752. static void disable_crtc_nofb(struct intel_crtc *crtc)
  10753. {
  10754. struct drm_device *dev = crtc->base.dev;
  10755. struct intel_encoder *encoder;
  10756. struct intel_connector *connector;
  10757. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  10758. pipe_name(crtc->pipe));
  10759. for_each_intel_connector(dev, connector) {
  10760. if (connector->new_encoder &&
  10761. connector->new_encoder->new_crtc == crtc)
  10762. connector->new_encoder = NULL;
  10763. }
  10764. for_each_intel_encoder(dev, encoder) {
  10765. if (encoder->new_crtc == crtc)
  10766. encoder->new_crtc = NULL;
  10767. }
  10768. crtc->new_enabled = false;
  10769. }
  10770. static int intel_crtc_set_config(struct drm_mode_set *set)
  10771. {
  10772. struct drm_device *dev;
  10773. struct drm_mode_set save_set;
  10774. struct drm_atomic_state *state = NULL;
  10775. struct intel_set_config *config;
  10776. struct intel_crtc_state *pipe_config;
  10777. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  10778. int ret;
  10779. BUG_ON(!set);
  10780. BUG_ON(!set->crtc);
  10781. BUG_ON(!set->crtc->helper_private);
  10782. /* Enforce sane interface api - has been abused by the fb helper. */
  10783. BUG_ON(!set->mode && set->fb);
  10784. BUG_ON(set->fb && set->num_connectors == 0);
  10785. if (set->fb) {
  10786. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  10787. set->crtc->base.id, set->fb->base.id,
  10788. (int)set->num_connectors, set->x, set->y);
  10789. } else {
  10790. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  10791. }
  10792. dev = set->crtc->dev;
  10793. ret = -ENOMEM;
  10794. config = kzalloc(sizeof(*config), GFP_KERNEL);
  10795. if (!config)
  10796. goto out_config;
  10797. ret = intel_set_config_save_state(dev, config);
  10798. if (ret)
  10799. goto out_config;
  10800. save_set.crtc = set->crtc;
  10801. save_set.mode = &set->crtc->mode;
  10802. save_set.x = set->crtc->x;
  10803. save_set.y = set->crtc->y;
  10804. save_set.fb = set->crtc->primary->fb;
  10805. /* Compute whether we need a full modeset, only an fb base update or no
  10806. * change at all. In the future we might also check whether only the
  10807. * mode changed, e.g. for LVDS where we only change the panel fitter in
  10808. * such cases. */
  10809. intel_set_config_compute_mode_changes(set, config);
  10810. state = drm_atomic_state_alloc(dev);
  10811. if (!state) {
  10812. ret = -ENOMEM;
  10813. goto out_config;
  10814. }
  10815. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10816. ret = intel_modeset_stage_output_state(dev, set, config, state);
  10817. if (ret)
  10818. goto fail;
  10819. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  10820. state,
  10821. &modeset_pipes,
  10822. &prepare_pipes,
  10823. &disable_pipes);
  10824. if (IS_ERR(pipe_config)) {
  10825. ret = PTR_ERR(pipe_config);
  10826. goto fail;
  10827. } else if (pipe_config) {
  10828. if (pipe_config->has_audio !=
  10829. to_intel_crtc(set->crtc)->config->has_audio)
  10830. config->mode_changed = true;
  10831. /*
  10832. * Note we have an issue here with infoframes: current code
  10833. * only updates them on the full mode set path per hw
  10834. * requirements. So here we should be checking for any
  10835. * required changes and forcing a mode set.
  10836. */
  10837. }
  10838. intel_update_pipe_size(to_intel_crtc(set->crtc));
  10839. if (config->mode_changed) {
  10840. ret = intel_set_mode_pipes(set->crtc, set->mode,
  10841. set->x, set->y, set->fb, pipe_config,
  10842. modeset_pipes, prepare_pipes,
  10843. disable_pipes);
  10844. } else if (config->fb_changed) {
  10845. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  10846. struct drm_plane *primary = set->crtc->primary;
  10847. int vdisplay, hdisplay;
  10848. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  10849. ret = drm_plane_helper_update(primary, set->crtc, set->fb,
  10850. 0, 0, hdisplay, vdisplay,
  10851. set->x << 16, set->y << 16,
  10852. hdisplay << 16, vdisplay << 16);
  10853. /*
  10854. * We need to make sure the primary plane is re-enabled if it
  10855. * has previously been turned off.
  10856. */
  10857. if (!intel_crtc->primary_enabled && ret == 0) {
  10858. WARN_ON(!intel_crtc->active);
  10859. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  10860. }
  10861. /*
  10862. * In the fastboot case this may be our only check of the
  10863. * state after boot. It would be better to only do it on
  10864. * the first update, but we don't have a nice way of doing that
  10865. * (and really, set_config isn't used much for high freq page
  10866. * flipping, so increasing its cost here shouldn't be a big
  10867. * deal).
  10868. */
  10869. if (i915.fastboot && ret == 0)
  10870. intel_modeset_check_state(set->crtc->dev);
  10871. }
  10872. if (ret) {
  10873. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  10874. set->crtc->base.id, ret);
  10875. fail:
  10876. intel_set_config_restore_state(dev, config);
  10877. drm_atomic_state_clear(state);
  10878. /*
  10879. * HACK: if the pipe was on, but we didn't have a framebuffer,
  10880. * force the pipe off to avoid oopsing in the modeset code
  10881. * due to fb==NULL. This should only happen during boot since
  10882. * we don't yet reconstruct the FB from the hardware state.
  10883. */
  10884. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  10885. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  10886. /* Try to restore the config */
  10887. if (config->mode_changed &&
  10888. intel_set_mode(save_set.crtc, save_set.mode,
  10889. save_set.x, save_set.y, save_set.fb,
  10890. state))
  10891. DRM_ERROR("failed to restore config after modeset failure\n");
  10892. }
  10893. out_config:
  10894. if (state)
  10895. drm_atomic_state_free(state);
  10896. intel_set_config_free(config);
  10897. return ret;
  10898. }
  10899. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10900. .gamma_set = intel_crtc_gamma_set,
  10901. .set_config = intel_crtc_set_config,
  10902. .destroy = intel_crtc_destroy,
  10903. .page_flip = intel_crtc_page_flip,
  10904. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10905. .atomic_destroy_state = intel_crtc_destroy_state,
  10906. };
  10907. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  10908. struct intel_shared_dpll *pll,
  10909. struct intel_dpll_hw_state *hw_state)
  10910. {
  10911. uint32_t val;
  10912. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  10913. return false;
  10914. val = I915_READ(PCH_DPLL(pll->id));
  10915. hw_state->dpll = val;
  10916. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  10917. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  10918. return val & DPLL_VCO_ENABLE;
  10919. }
  10920. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  10921. struct intel_shared_dpll *pll)
  10922. {
  10923. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  10924. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  10925. }
  10926. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  10927. struct intel_shared_dpll *pll)
  10928. {
  10929. /* PCH refclock must be enabled first */
  10930. ibx_assert_pch_refclk_enabled(dev_priv);
  10931. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10932. /* Wait for the clocks to stabilize. */
  10933. POSTING_READ(PCH_DPLL(pll->id));
  10934. udelay(150);
  10935. /* The pixel multiplier can only be updated once the
  10936. * DPLL is enabled and the clocks are stable.
  10937. *
  10938. * So write it again.
  10939. */
  10940. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10941. POSTING_READ(PCH_DPLL(pll->id));
  10942. udelay(200);
  10943. }
  10944. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  10945. struct intel_shared_dpll *pll)
  10946. {
  10947. struct drm_device *dev = dev_priv->dev;
  10948. struct intel_crtc *crtc;
  10949. /* Make sure no transcoder isn't still depending on us. */
  10950. for_each_intel_crtc(dev, crtc) {
  10951. if (intel_crtc_to_shared_dpll(crtc) == pll)
  10952. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  10953. }
  10954. I915_WRITE(PCH_DPLL(pll->id), 0);
  10955. POSTING_READ(PCH_DPLL(pll->id));
  10956. udelay(200);
  10957. }
  10958. static char *ibx_pch_dpll_names[] = {
  10959. "PCH DPLL A",
  10960. "PCH DPLL B",
  10961. };
  10962. static void ibx_pch_dpll_init(struct drm_device *dev)
  10963. {
  10964. struct drm_i915_private *dev_priv = dev->dev_private;
  10965. int i;
  10966. dev_priv->num_shared_dpll = 2;
  10967. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10968. dev_priv->shared_dplls[i].id = i;
  10969. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  10970. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  10971. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  10972. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  10973. dev_priv->shared_dplls[i].get_hw_state =
  10974. ibx_pch_dpll_get_hw_state;
  10975. }
  10976. }
  10977. static void intel_shared_dpll_init(struct drm_device *dev)
  10978. {
  10979. struct drm_i915_private *dev_priv = dev->dev_private;
  10980. if (HAS_DDI(dev))
  10981. intel_ddi_pll_init(dev);
  10982. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  10983. ibx_pch_dpll_init(dev);
  10984. else
  10985. dev_priv->num_shared_dpll = 0;
  10986. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  10987. }
  10988. /**
  10989. * intel_wm_need_update - Check whether watermarks need updating
  10990. * @plane: drm plane
  10991. * @state: new plane state
  10992. *
  10993. * Check current plane state versus the new one to determine whether
  10994. * watermarks need to be recalculated.
  10995. *
  10996. * Returns true or false.
  10997. */
  10998. bool intel_wm_need_update(struct drm_plane *plane,
  10999. struct drm_plane_state *state)
  11000. {
  11001. /* Update watermarks on tiling changes. */
  11002. if (!plane->state->fb || !state->fb ||
  11003. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  11004. plane->state->rotation != state->rotation)
  11005. return true;
  11006. return false;
  11007. }
  11008. /**
  11009. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11010. * @plane: drm plane to prepare for
  11011. * @fb: framebuffer to prepare for presentation
  11012. *
  11013. * Prepares a framebuffer for usage on a display plane. Generally this
  11014. * involves pinning the underlying object and updating the frontbuffer tracking
  11015. * bits. Some older platforms need special physical address handling for
  11016. * cursor planes.
  11017. *
  11018. * Returns 0 on success, negative error code on failure.
  11019. */
  11020. int
  11021. intel_prepare_plane_fb(struct drm_plane *plane,
  11022. struct drm_framebuffer *fb,
  11023. const struct drm_plane_state *new_state)
  11024. {
  11025. struct drm_device *dev = plane->dev;
  11026. struct intel_plane *intel_plane = to_intel_plane(plane);
  11027. enum pipe pipe = intel_plane->pipe;
  11028. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11029. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11030. unsigned frontbuffer_bits = 0;
  11031. int ret = 0;
  11032. if (!obj)
  11033. return 0;
  11034. switch (plane->type) {
  11035. case DRM_PLANE_TYPE_PRIMARY:
  11036. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11037. break;
  11038. case DRM_PLANE_TYPE_CURSOR:
  11039. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  11040. break;
  11041. case DRM_PLANE_TYPE_OVERLAY:
  11042. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  11043. break;
  11044. }
  11045. mutex_lock(&dev->struct_mutex);
  11046. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11047. INTEL_INFO(dev)->cursor_needs_physical) {
  11048. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11049. ret = i915_gem_object_attach_phys(obj, align);
  11050. if (ret)
  11051. DRM_DEBUG_KMS("failed to attach phys object\n");
  11052. } else {
  11053. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  11054. }
  11055. if (ret == 0)
  11056. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  11057. mutex_unlock(&dev->struct_mutex);
  11058. return ret;
  11059. }
  11060. /**
  11061. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11062. * @plane: drm plane to clean up for
  11063. * @fb: old framebuffer that was on plane
  11064. *
  11065. * Cleans up a framebuffer that has just been removed from a plane.
  11066. */
  11067. void
  11068. intel_cleanup_plane_fb(struct drm_plane *plane,
  11069. struct drm_framebuffer *fb,
  11070. const struct drm_plane_state *old_state)
  11071. {
  11072. struct drm_device *dev = plane->dev;
  11073. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11074. if (WARN_ON(!obj))
  11075. return;
  11076. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11077. !INTEL_INFO(dev)->cursor_needs_physical) {
  11078. mutex_lock(&dev->struct_mutex);
  11079. intel_unpin_fb_obj(fb, old_state);
  11080. mutex_unlock(&dev->struct_mutex);
  11081. }
  11082. }
  11083. int
  11084. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11085. {
  11086. int max_scale;
  11087. struct drm_device *dev;
  11088. struct drm_i915_private *dev_priv;
  11089. int crtc_clock, cdclk;
  11090. if (!intel_crtc || !crtc_state)
  11091. return DRM_PLANE_HELPER_NO_SCALING;
  11092. dev = intel_crtc->base.dev;
  11093. dev_priv = dev->dev_private;
  11094. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11095. cdclk = dev_priv->display.get_display_clock_speed(dev);
  11096. if (!crtc_clock || !cdclk)
  11097. return DRM_PLANE_HELPER_NO_SCALING;
  11098. /*
  11099. * skl max scale is lower of:
  11100. * close to 3 but not 3, -1 is for that purpose
  11101. * or
  11102. * cdclk/crtc_clock
  11103. */
  11104. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11105. return max_scale;
  11106. }
  11107. static int
  11108. intel_check_primary_plane(struct drm_plane *plane,
  11109. struct intel_plane_state *state)
  11110. {
  11111. struct drm_device *dev = plane->dev;
  11112. struct drm_i915_private *dev_priv = dev->dev_private;
  11113. struct drm_crtc *crtc = state->base.crtc;
  11114. struct intel_crtc *intel_crtc;
  11115. struct intel_crtc_state *crtc_state;
  11116. struct drm_framebuffer *fb = state->base.fb;
  11117. struct drm_rect *dest = &state->dst;
  11118. struct drm_rect *src = &state->src;
  11119. const struct drm_rect *clip = &state->clip;
  11120. bool can_position = false;
  11121. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11122. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11123. int ret;
  11124. crtc = crtc ? crtc : plane->crtc;
  11125. intel_crtc = to_intel_crtc(crtc);
  11126. crtc_state = state->base.state ?
  11127. intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
  11128. if (INTEL_INFO(dev)->gen >= 9) {
  11129. min_scale = 1;
  11130. max_scale = skl_max_scale(intel_crtc, crtc_state);
  11131. can_position = true;
  11132. }
  11133. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11134. src, dest, clip,
  11135. min_scale,
  11136. max_scale,
  11137. can_position, true,
  11138. &state->visible);
  11139. if (ret)
  11140. return ret;
  11141. if (intel_crtc->active) {
  11142. intel_crtc->atomic.wait_for_flips = true;
  11143. /*
  11144. * FBC does not work on some platforms for rotated
  11145. * planes, so disable it when rotation is not 0 and
  11146. * update it when rotation is set back to 0.
  11147. *
  11148. * FIXME: This is redundant with the fbc update done in
  11149. * the primary plane enable function except that that
  11150. * one is done too late. We eventually need to unify
  11151. * this.
  11152. */
  11153. if (intel_crtc->primary_enabled &&
  11154. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  11155. dev_priv->fbc.crtc == intel_crtc &&
  11156. state->base.rotation != BIT(DRM_ROTATE_0)) {
  11157. intel_crtc->atomic.disable_fbc = true;
  11158. }
  11159. if (state->visible) {
  11160. /*
  11161. * BDW signals flip done immediately if the plane
  11162. * is disabled, even if the plane enable is already
  11163. * armed to occur at the next vblank :(
  11164. */
  11165. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  11166. intel_crtc->atomic.wait_vblank = true;
  11167. }
  11168. intel_crtc->atomic.fb_bits |=
  11169. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  11170. intel_crtc->atomic.update_fbc = true;
  11171. if (intel_wm_need_update(plane, &state->base))
  11172. intel_crtc->atomic.update_wm = true;
  11173. }
  11174. if (INTEL_INFO(dev)->gen >= 9) {
  11175. ret = skl_update_scaler_users(intel_crtc, crtc_state,
  11176. to_intel_plane(plane), state, 0);
  11177. if (ret)
  11178. return ret;
  11179. }
  11180. return 0;
  11181. }
  11182. static void
  11183. intel_commit_primary_plane(struct drm_plane *plane,
  11184. struct intel_plane_state *state)
  11185. {
  11186. struct drm_crtc *crtc = state->base.crtc;
  11187. struct drm_framebuffer *fb = state->base.fb;
  11188. struct drm_device *dev = plane->dev;
  11189. struct drm_i915_private *dev_priv = dev->dev_private;
  11190. struct intel_crtc *intel_crtc;
  11191. struct drm_rect *src = &state->src;
  11192. crtc = crtc ? crtc : plane->crtc;
  11193. intel_crtc = to_intel_crtc(crtc);
  11194. plane->fb = fb;
  11195. crtc->x = src->x1 >> 16;
  11196. crtc->y = src->y1 >> 16;
  11197. if (intel_crtc->active) {
  11198. if (state->visible) {
  11199. /* FIXME: kill this fastboot hack */
  11200. intel_update_pipe_size(intel_crtc);
  11201. intel_crtc->primary_enabled = true;
  11202. dev_priv->display.update_primary_plane(crtc, plane->fb,
  11203. crtc->x, crtc->y);
  11204. } else {
  11205. /*
  11206. * If clipping results in a non-visible primary plane,
  11207. * we'll disable the primary plane. Note that this is
  11208. * a bit different than what happens if userspace
  11209. * explicitly disables the plane by passing fb=0
  11210. * because plane->fb still gets set and pinned.
  11211. */
  11212. intel_disable_primary_hw_plane(plane, crtc);
  11213. }
  11214. }
  11215. }
  11216. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11217. {
  11218. struct drm_device *dev = crtc->dev;
  11219. struct drm_i915_private *dev_priv = dev->dev_private;
  11220. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11221. struct intel_plane *intel_plane;
  11222. struct drm_plane *p;
  11223. unsigned fb_bits = 0;
  11224. /* Track fb's for any planes being disabled */
  11225. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  11226. intel_plane = to_intel_plane(p);
  11227. if (intel_crtc->atomic.disabled_planes &
  11228. (1 << drm_plane_index(p))) {
  11229. switch (p->type) {
  11230. case DRM_PLANE_TYPE_PRIMARY:
  11231. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  11232. break;
  11233. case DRM_PLANE_TYPE_CURSOR:
  11234. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  11235. break;
  11236. case DRM_PLANE_TYPE_OVERLAY:
  11237. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  11238. break;
  11239. }
  11240. mutex_lock(&dev->struct_mutex);
  11241. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  11242. mutex_unlock(&dev->struct_mutex);
  11243. }
  11244. }
  11245. if (intel_crtc->atomic.wait_for_flips)
  11246. intel_crtc_wait_for_pending_flips(crtc);
  11247. if (intel_crtc->atomic.disable_fbc)
  11248. intel_fbc_disable(dev);
  11249. if (intel_crtc->atomic.pre_disable_primary)
  11250. intel_pre_disable_primary(crtc);
  11251. if (intel_crtc->atomic.update_wm)
  11252. intel_update_watermarks(crtc);
  11253. intel_runtime_pm_get(dev_priv);
  11254. /* Perform vblank evasion around commit operation */
  11255. if (intel_crtc->active)
  11256. intel_crtc->atomic.evade =
  11257. intel_pipe_update_start(intel_crtc,
  11258. &intel_crtc->atomic.start_vbl_count);
  11259. }
  11260. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11261. {
  11262. struct drm_device *dev = crtc->dev;
  11263. struct drm_i915_private *dev_priv = dev->dev_private;
  11264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11265. struct drm_plane *p;
  11266. if (intel_crtc->atomic.evade)
  11267. intel_pipe_update_end(intel_crtc,
  11268. intel_crtc->atomic.start_vbl_count);
  11269. intel_runtime_pm_put(dev_priv);
  11270. if (intel_crtc->atomic.wait_vblank)
  11271. intel_wait_for_vblank(dev, intel_crtc->pipe);
  11272. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  11273. if (intel_crtc->atomic.update_fbc) {
  11274. mutex_lock(&dev->struct_mutex);
  11275. intel_fbc_update(dev);
  11276. mutex_unlock(&dev->struct_mutex);
  11277. }
  11278. if (intel_crtc->atomic.post_enable_primary)
  11279. intel_post_enable_primary(crtc);
  11280. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  11281. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  11282. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  11283. false, false);
  11284. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  11285. }
  11286. /**
  11287. * intel_plane_destroy - destroy a plane
  11288. * @plane: plane to destroy
  11289. *
  11290. * Common destruction function for all types of planes (primary, cursor,
  11291. * sprite).
  11292. */
  11293. void intel_plane_destroy(struct drm_plane *plane)
  11294. {
  11295. struct intel_plane *intel_plane = to_intel_plane(plane);
  11296. drm_plane_cleanup(plane);
  11297. kfree(intel_plane);
  11298. }
  11299. const struct drm_plane_funcs intel_plane_funcs = {
  11300. .update_plane = drm_atomic_helper_update_plane,
  11301. .disable_plane = drm_atomic_helper_disable_plane,
  11302. .destroy = intel_plane_destroy,
  11303. .set_property = drm_atomic_helper_plane_set_property,
  11304. .atomic_get_property = intel_plane_atomic_get_property,
  11305. .atomic_set_property = intel_plane_atomic_set_property,
  11306. .atomic_duplicate_state = intel_plane_duplicate_state,
  11307. .atomic_destroy_state = intel_plane_destroy_state,
  11308. };
  11309. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11310. int pipe)
  11311. {
  11312. struct intel_plane *primary;
  11313. struct intel_plane_state *state;
  11314. const uint32_t *intel_primary_formats;
  11315. int num_formats;
  11316. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11317. if (primary == NULL)
  11318. return NULL;
  11319. state = intel_create_plane_state(&primary->base);
  11320. if (!state) {
  11321. kfree(primary);
  11322. return NULL;
  11323. }
  11324. primary->base.state = &state->base;
  11325. primary->can_scale = false;
  11326. primary->max_downscale = 1;
  11327. if (INTEL_INFO(dev)->gen >= 9) {
  11328. primary->can_scale = true;
  11329. }
  11330. state->scaler_id = -1;
  11331. primary->pipe = pipe;
  11332. primary->plane = pipe;
  11333. primary->check_plane = intel_check_primary_plane;
  11334. primary->commit_plane = intel_commit_primary_plane;
  11335. primary->ckey.flags = I915_SET_COLORKEY_NONE;
  11336. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11337. primary->plane = !pipe;
  11338. if (INTEL_INFO(dev)->gen <= 3) {
  11339. intel_primary_formats = intel_primary_formats_gen2;
  11340. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  11341. } else {
  11342. intel_primary_formats = intel_primary_formats_gen4;
  11343. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  11344. }
  11345. drm_universal_plane_init(dev, &primary->base, 0,
  11346. &intel_plane_funcs,
  11347. intel_primary_formats, num_formats,
  11348. DRM_PLANE_TYPE_PRIMARY);
  11349. if (INTEL_INFO(dev)->gen >= 4)
  11350. intel_create_rotation_property(dev, primary);
  11351. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11352. return &primary->base;
  11353. }
  11354. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11355. {
  11356. if (!dev->mode_config.rotation_property) {
  11357. unsigned long flags = BIT(DRM_ROTATE_0) |
  11358. BIT(DRM_ROTATE_180);
  11359. if (INTEL_INFO(dev)->gen >= 9)
  11360. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11361. dev->mode_config.rotation_property =
  11362. drm_mode_create_rotation_property(dev, flags);
  11363. }
  11364. if (dev->mode_config.rotation_property)
  11365. drm_object_attach_property(&plane->base.base,
  11366. dev->mode_config.rotation_property,
  11367. plane->base.state->rotation);
  11368. }
  11369. static int
  11370. intel_check_cursor_plane(struct drm_plane *plane,
  11371. struct intel_plane_state *state)
  11372. {
  11373. struct drm_crtc *crtc = state->base.crtc;
  11374. struct drm_device *dev = plane->dev;
  11375. struct drm_framebuffer *fb = state->base.fb;
  11376. struct drm_rect *dest = &state->dst;
  11377. struct drm_rect *src = &state->src;
  11378. const struct drm_rect *clip = &state->clip;
  11379. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11380. struct intel_crtc *intel_crtc;
  11381. unsigned stride;
  11382. int ret;
  11383. crtc = crtc ? crtc : plane->crtc;
  11384. intel_crtc = to_intel_crtc(crtc);
  11385. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11386. src, dest, clip,
  11387. DRM_PLANE_HELPER_NO_SCALING,
  11388. DRM_PLANE_HELPER_NO_SCALING,
  11389. true, true, &state->visible);
  11390. if (ret)
  11391. return ret;
  11392. /* if we want to turn off the cursor ignore width and height */
  11393. if (!obj)
  11394. goto finish;
  11395. /* Check for which cursor types we support */
  11396. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  11397. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11398. state->base.crtc_w, state->base.crtc_h);
  11399. return -EINVAL;
  11400. }
  11401. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11402. if (obj->base.size < stride * state->base.crtc_h) {
  11403. DRM_DEBUG_KMS("buffer is too small\n");
  11404. return -ENOMEM;
  11405. }
  11406. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11407. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11408. ret = -EINVAL;
  11409. }
  11410. finish:
  11411. if (intel_crtc->active) {
  11412. if (plane->state->crtc_w != state->base.crtc_w)
  11413. intel_crtc->atomic.update_wm = true;
  11414. intel_crtc->atomic.fb_bits |=
  11415. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  11416. }
  11417. return ret;
  11418. }
  11419. static void
  11420. intel_commit_cursor_plane(struct drm_plane *plane,
  11421. struct intel_plane_state *state)
  11422. {
  11423. struct drm_crtc *crtc = state->base.crtc;
  11424. struct drm_device *dev = plane->dev;
  11425. struct intel_crtc *intel_crtc;
  11426. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11427. uint32_t addr;
  11428. crtc = crtc ? crtc : plane->crtc;
  11429. intel_crtc = to_intel_crtc(crtc);
  11430. plane->fb = state->base.fb;
  11431. crtc->cursor_x = state->base.crtc_x;
  11432. crtc->cursor_y = state->base.crtc_y;
  11433. if (intel_crtc->cursor_bo == obj)
  11434. goto update;
  11435. if (!obj)
  11436. addr = 0;
  11437. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11438. addr = i915_gem_obj_ggtt_offset(obj);
  11439. else
  11440. addr = obj->phys_handle->busaddr;
  11441. intel_crtc->cursor_addr = addr;
  11442. intel_crtc->cursor_bo = obj;
  11443. update:
  11444. if (intel_crtc->active)
  11445. intel_crtc_update_cursor(crtc, state->visible);
  11446. }
  11447. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11448. int pipe)
  11449. {
  11450. struct intel_plane *cursor;
  11451. struct intel_plane_state *state;
  11452. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11453. if (cursor == NULL)
  11454. return NULL;
  11455. state = intel_create_plane_state(&cursor->base);
  11456. if (!state) {
  11457. kfree(cursor);
  11458. return NULL;
  11459. }
  11460. cursor->base.state = &state->base;
  11461. cursor->can_scale = false;
  11462. cursor->max_downscale = 1;
  11463. cursor->pipe = pipe;
  11464. cursor->plane = pipe;
  11465. state->scaler_id = -1;
  11466. cursor->check_plane = intel_check_cursor_plane;
  11467. cursor->commit_plane = intel_commit_cursor_plane;
  11468. drm_universal_plane_init(dev, &cursor->base, 0,
  11469. &intel_plane_funcs,
  11470. intel_cursor_formats,
  11471. ARRAY_SIZE(intel_cursor_formats),
  11472. DRM_PLANE_TYPE_CURSOR);
  11473. if (INTEL_INFO(dev)->gen >= 4) {
  11474. if (!dev->mode_config.rotation_property)
  11475. dev->mode_config.rotation_property =
  11476. drm_mode_create_rotation_property(dev,
  11477. BIT(DRM_ROTATE_0) |
  11478. BIT(DRM_ROTATE_180));
  11479. if (dev->mode_config.rotation_property)
  11480. drm_object_attach_property(&cursor->base.base,
  11481. dev->mode_config.rotation_property,
  11482. state->base.rotation);
  11483. }
  11484. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11485. return &cursor->base;
  11486. }
  11487. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11488. struct intel_crtc_state *crtc_state)
  11489. {
  11490. int i;
  11491. struct intel_scaler *intel_scaler;
  11492. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11493. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11494. intel_scaler = &scaler_state->scalers[i];
  11495. intel_scaler->in_use = 0;
  11496. intel_scaler->id = i;
  11497. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11498. }
  11499. scaler_state->scaler_id = -1;
  11500. }
  11501. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11502. {
  11503. struct drm_i915_private *dev_priv = dev->dev_private;
  11504. struct intel_crtc *intel_crtc;
  11505. struct intel_crtc_state *crtc_state = NULL;
  11506. struct drm_plane *primary = NULL;
  11507. struct drm_plane *cursor = NULL;
  11508. int i, ret;
  11509. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11510. if (intel_crtc == NULL)
  11511. return;
  11512. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11513. if (!crtc_state)
  11514. goto fail;
  11515. intel_crtc_set_state(intel_crtc, crtc_state);
  11516. crtc_state->base.crtc = &intel_crtc->base;
  11517. /* initialize shared scalers */
  11518. if (INTEL_INFO(dev)->gen >= 9) {
  11519. if (pipe == PIPE_C)
  11520. intel_crtc->num_scalers = 1;
  11521. else
  11522. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11523. skl_init_scalers(dev, intel_crtc, crtc_state);
  11524. }
  11525. primary = intel_primary_plane_create(dev, pipe);
  11526. if (!primary)
  11527. goto fail;
  11528. cursor = intel_cursor_plane_create(dev, pipe);
  11529. if (!cursor)
  11530. goto fail;
  11531. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11532. cursor, &intel_crtc_funcs);
  11533. if (ret)
  11534. goto fail;
  11535. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11536. for (i = 0; i < 256; i++) {
  11537. intel_crtc->lut_r[i] = i;
  11538. intel_crtc->lut_g[i] = i;
  11539. intel_crtc->lut_b[i] = i;
  11540. }
  11541. /*
  11542. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11543. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11544. */
  11545. intel_crtc->pipe = pipe;
  11546. intel_crtc->plane = pipe;
  11547. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11548. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11549. intel_crtc->plane = !pipe;
  11550. }
  11551. intel_crtc->cursor_base = ~0;
  11552. intel_crtc->cursor_cntl = ~0;
  11553. intel_crtc->cursor_size = ~0;
  11554. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11555. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11556. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11557. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11558. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  11559. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11560. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11561. return;
  11562. fail:
  11563. if (primary)
  11564. drm_plane_cleanup(primary);
  11565. if (cursor)
  11566. drm_plane_cleanup(cursor);
  11567. kfree(crtc_state);
  11568. kfree(intel_crtc);
  11569. }
  11570. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11571. {
  11572. struct drm_encoder *encoder = connector->base.encoder;
  11573. struct drm_device *dev = connector->base.dev;
  11574. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11575. if (!encoder || WARN_ON(!encoder->crtc))
  11576. return INVALID_PIPE;
  11577. return to_intel_crtc(encoder->crtc)->pipe;
  11578. }
  11579. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11580. struct drm_file *file)
  11581. {
  11582. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11583. struct drm_crtc *drmmode_crtc;
  11584. struct intel_crtc *crtc;
  11585. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11586. if (!drmmode_crtc) {
  11587. DRM_ERROR("no such CRTC id\n");
  11588. return -ENOENT;
  11589. }
  11590. crtc = to_intel_crtc(drmmode_crtc);
  11591. pipe_from_crtc_id->pipe = crtc->pipe;
  11592. return 0;
  11593. }
  11594. static int intel_encoder_clones(struct intel_encoder *encoder)
  11595. {
  11596. struct drm_device *dev = encoder->base.dev;
  11597. struct intel_encoder *source_encoder;
  11598. int index_mask = 0;
  11599. int entry = 0;
  11600. for_each_intel_encoder(dev, source_encoder) {
  11601. if (encoders_cloneable(encoder, source_encoder))
  11602. index_mask |= (1 << entry);
  11603. entry++;
  11604. }
  11605. return index_mask;
  11606. }
  11607. static bool has_edp_a(struct drm_device *dev)
  11608. {
  11609. struct drm_i915_private *dev_priv = dev->dev_private;
  11610. if (!IS_MOBILE(dev))
  11611. return false;
  11612. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11613. return false;
  11614. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11615. return false;
  11616. return true;
  11617. }
  11618. static bool intel_crt_present(struct drm_device *dev)
  11619. {
  11620. struct drm_i915_private *dev_priv = dev->dev_private;
  11621. if (INTEL_INFO(dev)->gen >= 9)
  11622. return false;
  11623. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11624. return false;
  11625. if (IS_CHERRYVIEW(dev))
  11626. return false;
  11627. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11628. return false;
  11629. return true;
  11630. }
  11631. static void intel_setup_outputs(struct drm_device *dev)
  11632. {
  11633. struct drm_i915_private *dev_priv = dev->dev_private;
  11634. struct intel_encoder *encoder;
  11635. bool dpd_is_edp = false;
  11636. intel_lvds_init(dev);
  11637. if (intel_crt_present(dev))
  11638. intel_crt_init(dev);
  11639. if (IS_BROXTON(dev)) {
  11640. /*
  11641. * FIXME: Broxton doesn't support port detection via the
  11642. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11643. * detect the ports.
  11644. */
  11645. intel_ddi_init(dev, PORT_A);
  11646. intel_ddi_init(dev, PORT_B);
  11647. intel_ddi_init(dev, PORT_C);
  11648. } else if (HAS_DDI(dev)) {
  11649. int found;
  11650. /*
  11651. * Haswell uses DDI functions to detect digital outputs.
  11652. * On SKL pre-D0 the strap isn't connected, so we assume
  11653. * it's there.
  11654. */
  11655. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11656. /* WaIgnoreDDIAStrap: skl */
  11657. if (found ||
  11658. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11659. intel_ddi_init(dev, PORT_A);
  11660. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11661. * register */
  11662. found = I915_READ(SFUSE_STRAP);
  11663. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11664. intel_ddi_init(dev, PORT_B);
  11665. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11666. intel_ddi_init(dev, PORT_C);
  11667. if (found & SFUSE_STRAP_DDID_DETECTED)
  11668. intel_ddi_init(dev, PORT_D);
  11669. } else if (HAS_PCH_SPLIT(dev)) {
  11670. int found;
  11671. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11672. if (has_edp_a(dev))
  11673. intel_dp_init(dev, DP_A, PORT_A);
  11674. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11675. /* PCH SDVOB multiplex with HDMIB */
  11676. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11677. if (!found)
  11678. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11679. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11680. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11681. }
  11682. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11683. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11684. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11685. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11686. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11687. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11688. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11689. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11690. } else if (IS_VALLEYVIEW(dev)) {
  11691. /*
  11692. * The DP_DETECTED bit is the latched state of the DDC
  11693. * SDA pin at boot. However since eDP doesn't require DDC
  11694. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11695. * eDP ports may have been muxed to an alternate function.
  11696. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11697. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11698. * detect eDP ports.
  11699. */
  11700. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11701. !intel_dp_is_edp(dev, PORT_B))
  11702. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11703. PORT_B);
  11704. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11705. intel_dp_is_edp(dev, PORT_B))
  11706. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11707. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11708. !intel_dp_is_edp(dev, PORT_C))
  11709. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11710. PORT_C);
  11711. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11712. intel_dp_is_edp(dev, PORT_C))
  11713. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11714. if (IS_CHERRYVIEW(dev)) {
  11715. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11716. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11717. PORT_D);
  11718. /* eDP not supported on port D, so don't check VBT */
  11719. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11720. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11721. }
  11722. intel_dsi_init(dev);
  11723. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  11724. bool found = false;
  11725. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11726. DRM_DEBUG_KMS("probing SDVOB\n");
  11727. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11728. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  11729. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11730. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11731. }
  11732. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  11733. intel_dp_init(dev, DP_B, PORT_B);
  11734. }
  11735. /* Before G4X SDVOC doesn't have its own detect register */
  11736. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11737. DRM_DEBUG_KMS("probing SDVOC\n");
  11738. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11739. }
  11740. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11741. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  11742. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11743. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11744. }
  11745. if (SUPPORTS_INTEGRATED_DP(dev))
  11746. intel_dp_init(dev, DP_C, PORT_C);
  11747. }
  11748. if (SUPPORTS_INTEGRATED_DP(dev) &&
  11749. (I915_READ(DP_D) & DP_DETECTED))
  11750. intel_dp_init(dev, DP_D, PORT_D);
  11751. } else if (IS_GEN2(dev))
  11752. intel_dvo_init(dev);
  11753. if (SUPPORTS_TV(dev))
  11754. intel_tv_init(dev);
  11755. intel_psr_init(dev);
  11756. for_each_intel_encoder(dev, encoder) {
  11757. encoder->base.possible_crtcs = encoder->crtc_mask;
  11758. encoder->base.possible_clones =
  11759. intel_encoder_clones(encoder);
  11760. }
  11761. intel_init_pch_refclk(dev);
  11762. drm_helper_move_panel_connectors_to_head(dev);
  11763. }
  11764. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11765. {
  11766. struct drm_device *dev = fb->dev;
  11767. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11768. drm_framebuffer_cleanup(fb);
  11769. mutex_lock(&dev->struct_mutex);
  11770. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11771. drm_gem_object_unreference(&intel_fb->obj->base);
  11772. mutex_unlock(&dev->struct_mutex);
  11773. kfree(intel_fb);
  11774. }
  11775. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11776. struct drm_file *file,
  11777. unsigned int *handle)
  11778. {
  11779. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11780. struct drm_i915_gem_object *obj = intel_fb->obj;
  11781. return drm_gem_handle_create(file, &obj->base, handle);
  11782. }
  11783. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11784. .destroy = intel_user_framebuffer_destroy,
  11785. .create_handle = intel_user_framebuffer_create_handle,
  11786. };
  11787. static
  11788. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11789. uint32_t pixel_format)
  11790. {
  11791. u32 gen = INTEL_INFO(dev)->gen;
  11792. if (gen >= 9) {
  11793. /* "The stride in bytes must not exceed the of the size of 8K
  11794. * pixels and 32K bytes."
  11795. */
  11796. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11797. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11798. return 32*1024;
  11799. } else if (gen >= 4) {
  11800. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11801. return 16*1024;
  11802. else
  11803. return 32*1024;
  11804. } else if (gen >= 3) {
  11805. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11806. return 8*1024;
  11807. else
  11808. return 16*1024;
  11809. } else {
  11810. /* XXX DSPC is limited to 4k tiled */
  11811. return 8*1024;
  11812. }
  11813. }
  11814. static int intel_framebuffer_init(struct drm_device *dev,
  11815. struct intel_framebuffer *intel_fb,
  11816. struct drm_mode_fb_cmd2 *mode_cmd,
  11817. struct drm_i915_gem_object *obj)
  11818. {
  11819. unsigned int aligned_height;
  11820. int ret;
  11821. u32 pitch_limit, stride_alignment;
  11822. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11823. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11824. /* Enforce that fb modifier and tiling mode match, but only for
  11825. * X-tiled. This is needed for FBC. */
  11826. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11827. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11828. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11829. return -EINVAL;
  11830. }
  11831. } else {
  11832. if (obj->tiling_mode == I915_TILING_X)
  11833. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11834. else if (obj->tiling_mode == I915_TILING_Y) {
  11835. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11836. return -EINVAL;
  11837. }
  11838. }
  11839. /* Passed in modifier sanity checking. */
  11840. switch (mode_cmd->modifier[0]) {
  11841. case I915_FORMAT_MOD_Y_TILED:
  11842. case I915_FORMAT_MOD_Yf_TILED:
  11843. if (INTEL_INFO(dev)->gen < 9) {
  11844. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11845. mode_cmd->modifier[0]);
  11846. return -EINVAL;
  11847. }
  11848. case DRM_FORMAT_MOD_NONE:
  11849. case I915_FORMAT_MOD_X_TILED:
  11850. break;
  11851. default:
  11852. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11853. mode_cmd->modifier[0]);
  11854. return -EINVAL;
  11855. }
  11856. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11857. mode_cmd->pixel_format);
  11858. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11859. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11860. mode_cmd->pitches[0], stride_alignment);
  11861. return -EINVAL;
  11862. }
  11863. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11864. mode_cmd->pixel_format);
  11865. if (mode_cmd->pitches[0] > pitch_limit) {
  11866. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11867. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11868. "tiled" : "linear",
  11869. mode_cmd->pitches[0], pitch_limit);
  11870. return -EINVAL;
  11871. }
  11872. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11873. mode_cmd->pitches[0] != obj->stride) {
  11874. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11875. mode_cmd->pitches[0], obj->stride);
  11876. return -EINVAL;
  11877. }
  11878. /* Reject formats not supported by any plane early. */
  11879. switch (mode_cmd->pixel_format) {
  11880. case DRM_FORMAT_C8:
  11881. case DRM_FORMAT_RGB565:
  11882. case DRM_FORMAT_XRGB8888:
  11883. case DRM_FORMAT_ARGB8888:
  11884. break;
  11885. case DRM_FORMAT_XRGB1555:
  11886. case DRM_FORMAT_ARGB1555:
  11887. if (INTEL_INFO(dev)->gen > 3) {
  11888. DRM_DEBUG("unsupported pixel format: %s\n",
  11889. drm_get_format_name(mode_cmd->pixel_format));
  11890. return -EINVAL;
  11891. }
  11892. break;
  11893. case DRM_FORMAT_XBGR8888:
  11894. case DRM_FORMAT_ABGR8888:
  11895. case DRM_FORMAT_XRGB2101010:
  11896. case DRM_FORMAT_ARGB2101010:
  11897. case DRM_FORMAT_XBGR2101010:
  11898. case DRM_FORMAT_ABGR2101010:
  11899. if (INTEL_INFO(dev)->gen < 4) {
  11900. DRM_DEBUG("unsupported pixel format: %s\n",
  11901. drm_get_format_name(mode_cmd->pixel_format));
  11902. return -EINVAL;
  11903. }
  11904. break;
  11905. case DRM_FORMAT_YUYV:
  11906. case DRM_FORMAT_UYVY:
  11907. case DRM_FORMAT_YVYU:
  11908. case DRM_FORMAT_VYUY:
  11909. if (INTEL_INFO(dev)->gen < 5) {
  11910. DRM_DEBUG("unsupported pixel format: %s\n",
  11911. drm_get_format_name(mode_cmd->pixel_format));
  11912. return -EINVAL;
  11913. }
  11914. break;
  11915. default:
  11916. DRM_DEBUG("unsupported pixel format: %s\n",
  11917. drm_get_format_name(mode_cmd->pixel_format));
  11918. return -EINVAL;
  11919. }
  11920. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11921. if (mode_cmd->offsets[0] != 0)
  11922. return -EINVAL;
  11923. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11924. mode_cmd->pixel_format,
  11925. mode_cmd->modifier[0]);
  11926. /* FIXME drm helper for size checks (especially planar formats)? */
  11927. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11928. return -EINVAL;
  11929. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11930. intel_fb->obj = obj;
  11931. intel_fb->obj->framebuffer_references++;
  11932. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11933. if (ret) {
  11934. DRM_ERROR("framebuffer init failed %d\n", ret);
  11935. return ret;
  11936. }
  11937. return 0;
  11938. }
  11939. static struct drm_framebuffer *
  11940. intel_user_framebuffer_create(struct drm_device *dev,
  11941. struct drm_file *filp,
  11942. struct drm_mode_fb_cmd2 *mode_cmd)
  11943. {
  11944. struct drm_i915_gem_object *obj;
  11945. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11946. mode_cmd->handles[0]));
  11947. if (&obj->base == NULL)
  11948. return ERR_PTR(-ENOENT);
  11949. return intel_framebuffer_create(dev, mode_cmd, obj);
  11950. }
  11951. #ifndef CONFIG_DRM_I915_FBDEV
  11952. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  11953. {
  11954. }
  11955. #endif
  11956. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11957. .fb_create = intel_user_framebuffer_create,
  11958. .output_poll_changed = intel_fbdev_output_poll_changed,
  11959. .atomic_check = intel_atomic_check,
  11960. .atomic_commit = intel_atomic_commit,
  11961. };
  11962. /* Set up chip specific display functions */
  11963. static void intel_init_display(struct drm_device *dev)
  11964. {
  11965. struct drm_i915_private *dev_priv = dev->dev_private;
  11966. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  11967. dev_priv->display.find_dpll = g4x_find_best_dpll;
  11968. else if (IS_CHERRYVIEW(dev))
  11969. dev_priv->display.find_dpll = chv_find_best_dpll;
  11970. else if (IS_VALLEYVIEW(dev))
  11971. dev_priv->display.find_dpll = vlv_find_best_dpll;
  11972. else if (IS_PINEVIEW(dev))
  11973. dev_priv->display.find_dpll = pnv_find_best_dpll;
  11974. else
  11975. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  11976. if (INTEL_INFO(dev)->gen >= 9) {
  11977. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11978. dev_priv->display.get_initial_plane_config =
  11979. skylake_get_initial_plane_config;
  11980. dev_priv->display.crtc_compute_clock =
  11981. haswell_crtc_compute_clock;
  11982. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11983. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11984. dev_priv->display.off = ironlake_crtc_off;
  11985. dev_priv->display.update_primary_plane =
  11986. skylake_update_primary_plane;
  11987. } else if (HAS_DDI(dev)) {
  11988. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11989. dev_priv->display.get_initial_plane_config =
  11990. ironlake_get_initial_plane_config;
  11991. dev_priv->display.crtc_compute_clock =
  11992. haswell_crtc_compute_clock;
  11993. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11994. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11995. dev_priv->display.off = ironlake_crtc_off;
  11996. dev_priv->display.update_primary_plane =
  11997. ironlake_update_primary_plane;
  11998. } else if (HAS_PCH_SPLIT(dev)) {
  11999. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12000. dev_priv->display.get_initial_plane_config =
  12001. ironlake_get_initial_plane_config;
  12002. dev_priv->display.crtc_compute_clock =
  12003. ironlake_crtc_compute_clock;
  12004. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12005. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12006. dev_priv->display.off = ironlake_crtc_off;
  12007. dev_priv->display.update_primary_plane =
  12008. ironlake_update_primary_plane;
  12009. } else if (IS_VALLEYVIEW(dev)) {
  12010. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12011. dev_priv->display.get_initial_plane_config =
  12012. i9xx_get_initial_plane_config;
  12013. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12014. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12015. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12016. dev_priv->display.off = i9xx_crtc_off;
  12017. dev_priv->display.update_primary_plane =
  12018. i9xx_update_primary_plane;
  12019. } else {
  12020. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12021. dev_priv->display.get_initial_plane_config =
  12022. i9xx_get_initial_plane_config;
  12023. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12024. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12025. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12026. dev_priv->display.off = i9xx_crtc_off;
  12027. dev_priv->display.update_primary_plane =
  12028. i9xx_update_primary_plane;
  12029. }
  12030. /* Returns the core display clock speed */
  12031. if (IS_SKYLAKE(dev))
  12032. dev_priv->display.get_display_clock_speed =
  12033. skylake_get_display_clock_speed;
  12034. else if (IS_BROADWELL(dev))
  12035. dev_priv->display.get_display_clock_speed =
  12036. broadwell_get_display_clock_speed;
  12037. else if (IS_HASWELL(dev))
  12038. dev_priv->display.get_display_clock_speed =
  12039. haswell_get_display_clock_speed;
  12040. else if (IS_VALLEYVIEW(dev))
  12041. dev_priv->display.get_display_clock_speed =
  12042. valleyview_get_display_clock_speed;
  12043. else if (IS_GEN5(dev))
  12044. dev_priv->display.get_display_clock_speed =
  12045. ilk_get_display_clock_speed;
  12046. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12047. IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  12048. dev_priv->display.get_display_clock_speed =
  12049. i945_get_display_clock_speed;
  12050. else if (IS_I915G(dev))
  12051. dev_priv->display.get_display_clock_speed =
  12052. i915_get_display_clock_speed;
  12053. else if (IS_I945GM(dev) || IS_845G(dev))
  12054. dev_priv->display.get_display_clock_speed =
  12055. i9xx_misc_get_display_clock_speed;
  12056. else if (IS_PINEVIEW(dev))
  12057. dev_priv->display.get_display_clock_speed =
  12058. pnv_get_display_clock_speed;
  12059. else if (IS_I915GM(dev))
  12060. dev_priv->display.get_display_clock_speed =
  12061. i915gm_get_display_clock_speed;
  12062. else if (IS_I865G(dev))
  12063. dev_priv->display.get_display_clock_speed =
  12064. i865_get_display_clock_speed;
  12065. else if (IS_I85X(dev))
  12066. dev_priv->display.get_display_clock_speed =
  12067. i855_get_display_clock_speed;
  12068. else /* 852, 830 */
  12069. dev_priv->display.get_display_clock_speed =
  12070. i830_get_display_clock_speed;
  12071. if (IS_GEN5(dev)) {
  12072. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12073. } else if (IS_GEN6(dev)) {
  12074. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12075. } else if (IS_IVYBRIDGE(dev)) {
  12076. /* FIXME: detect B0+ stepping and use auto training */
  12077. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12078. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12079. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12080. } else if (IS_VALLEYVIEW(dev)) {
  12081. dev_priv->display.modeset_global_resources =
  12082. valleyview_modeset_global_resources;
  12083. } else if (IS_BROXTON(dev)) {
  12084. dev_priv->display.modeset_global_resources =
  12085. broxton_modeset_global_resources;
  12086. }
  12087. switch (INTEL_INFO(dev)->gen) {
  12088. case 2:
  12089. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12090. break;
  12091. case 3:
  12092. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12093. break;
  12094. case 4:
  12095. case 5:
  12096. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12097. break;
  12098. case 6:
  12099. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12100. break;
  12101. case 7:
  12102. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12103. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12104. break;
  12105. case 9:
  12106. /* Drop through - unsupported since execlist only. */
  12107. default:
  12108. /* Default just returns -ENODEV to indicate unsupported */
  12109. dev_priv->display.queue_flip = intel_default_queue_flip;
  12110. }
  12111. intel_panel_init_backlight_funcs(dev);
  12112. mutex_init(&dev_priv->pps_mutex);
  12113. }
  12114. /*
  12115. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12116. * resume, or other times. This quirk makes sure that's the case for
  12117. * affected systems.
  12118. */
  12119. static void quirk_pipea_force(struct drm_device *dev)
  12120. {
  12121. struct drm_i915_private *dev_priv = dev->dev_private;
  12122. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12123. DRM_INFO("applying pipe a force quirk\n");
  12124. }
  12125. static void quirk_pipeb_force(struct drm_device *dev)
  12126. {
  12127. struct drm_i915_private *dev_priv = dev->dev_private;
  12128. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12129. DRM_INFO("applying pipe b force quirk\n");
  12130. }
  12131. /*
  12132. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12133. */
  12134. static void quirk_ssc_force_disable(struct drm_device *dev)
  12135. {
  12136. struct drm_i915_private *dev_priv = dev->dev_private;
  12137. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12138. DRM_INFO("applying lvds SSC disable quirk\n");
  12139. }
  12140. /*
  12141. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12142. * brightness value
  12143. */
  12144. static void quirk_invert_brightness(struct drm_device *dev)
  12145. {
  12146. struct drm_i915_private *dev_priv = dev->dev_private;
  12147. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12148. DRM_INFO("applying inverted panel brightness quirk\n");
  12149. }
  12150. /* Some VBT's incorrectly indicate no backlight is present */
  12151. static void quirk_backlight_present(struct drm_device *dev)
  12152. {
  12153. struct drm_i915_private *dev_priv = dev->dev_private;
  12154. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12155. DRM_INFO("applying backlight present quirk\n");
  12156. }
  12157. struct intel_quirk {
  12158. int device;
  12159. int subsystem_vendor;
  12160. int subsystem_device;
  12161. void (*hook)(struct drm_device *dev);
  12162. };
  12163. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12164. struct intel_dmi_quirk {
  12165. void (*hook)(struct drm_device *dev);
  12166. const struct dmi_system_id (*dmi_id_list)[];
  12167. };
  12168. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12169. {
  12170. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12171. return 1;
  12172. }
  12173. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12174. {
  12175. .dmi_id_list = &(const struct dmi_system_id[]) {
  12176. {
  12177. .callback = intel_dmi_reverse_brightness,
  12178. .ident = "NCR Corporation",
  12179. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12180. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12181. },
  12182. },
  12183. { } /* terminating entry */
  12184. },
  12185. .hook = quirk_invert_brightness,
  12186. },
  12187. };
  12188. static struct intel_quirk intel_quirks[] = {
  12189. /* HP Mini needs pipe A force quirk (LP: #322104) */
  12190. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  12191. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12192. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12193. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12194. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12195. /* 830 needs to leave pipe A & dpll A up */
  12196. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12197. /* 830 needs to leave pipe B & dpll B up */
  12198. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12199. /* Lenovo U160 cannot use SSC on LVDS */
  12200. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12201. /* Sony Vaio Y cannot use SSC on LVDS */
  12202. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12203. /* Acer Aspire 5734Z must invert backlight brightness */
  12204. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12205. /* Acer/eMachines G725 */
  12206. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12207. /* Acer/eMachines e725 */
  12208. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12209. /* Acer/Packard Bell NCL20 */
  12210. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12211. /* Acer Aspire 4736Z */
  12212. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12213. /* Acer Aspire 5336 */
  12214. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12215. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12216. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12217. /* Acer C720 Chromebook (Core i3 4005U) */
  12218. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12219. /* Apple Macbook 2,1 (Core 2 T7400) */
  12220. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12221. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12222. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12223. /* HP Chromebook 14 (Celeron 2955U) */
  12224. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12225. /* Dell Chromebook 11 */
  12226. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12227. };
  12228. static void intel_init_quirks(struct drm_device *dev)
  12229. {
  12230. struct pci_dev *d = dev->pdev;
  12231. int i;
  12232. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12233. struct intel_quirk *q = &intel_quirks[i];
  12234. if (d->device == q->device &&
  12235. (d->subsystem_vendor == q->subsystem_vendor ||
  12236. q->subsystem_vendor == PCI_ANY_ID) &&
  12237. (d->subsystem_device == q->subsystem_device ||
  12238. q->subsystem_device == PCI_ANY_ID))
  12239. q->hook(dev);
  12240. }
  12241. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12242. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12243. intel_dmi_quirks[i].hook(dev);
  12244. }
  12245. }
  12246. /* Disable the VGA plane that we never use */
  12247. static void i915_disable_vga(struct drm_device *dev)
  12248. {
  12249. struct drm_i915_private *dev_priv = dev->dev_private;
  12250. u8 sr1;
  12251. u32 vga_reg = i915_vgacntrl_reg(dev);
  12252. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12253. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12254. outb(SR01, VGA_SR_INDEX);
  12255. sr1 = inb(VGA_SR_DATA);
  12256. outb(sr1 | 1<<5, VGA_SR_DATA);
  12257. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12258. udelay(300);
  12259. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12260. POSTING_READ(vga_reg);
  12261. }
  12262. void intel_modeset_init_hw(struct drm_device *dev)
  12263. {
  12264. intel_prepare_ddi(dev);
  12265. if (IS_VALLEYVIEW(dev))
  12266. vlv_update_cdclk(dev);
  12267. intel_init_clock_gating(dev);
  12268. intel_enable_gt_powersave(dev);
  12269. }
  12270. void intel_modeset_init(struct drm_device *dev)
  12271. {
  12272. struct drm_i915_private *dev_priv = dev->dev_private;
  12273. int sprite, ret;
  12274. enum pipe pipe;
  12275. struct intel_crtc *crtc;
  12276. drm_mode_config_init(dev);
  12277. dev->mode_config.min_width = 0;
  12278. dev->mode_config.min_height = 0;
  12279. dev->mode_config.preferred_depth = 24;
  12280. dev->mode_config.prefer_shadow = 1;
  12281. dev->mode_config.allow_fb_modifiers = true;
  12282. dev->mode_config.funcs = &intel_mode_funcs;
  12283. intel_init_quirks(dev);
  12284. intel_init_pm(dev);
  12285. if (INTEL_INFO(dev)->num_pipes == 0)
  12286. return;
  12287. intel_init_display(dev);
  12288. intel_init_audio(dev);
  12289. if (IS_GEN2(dev)) {
  12290. dev->mode_config.max_width = 2048;
  12291. dev->mode_config.max_height = 2048;
  12292. } else if (IS_GEN3(dev)) {
  12293. dev->mode_config.max_width = 4096;
  12294. dev->mode_config.max_height = 4096;
  12295. } else {
  12296. dev->mode_config.max_width = 8192;
  12297. dev->mode_config.max_height = 8192;
  12298. }
  12299. if (IS_845G(dev) || IS_I865G(dev)) {
  12300. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12301. dev->mode_config.cursor_height = 1023;
  12302. } else if (IS_GEN2(dev)) {
  12303. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12304. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12305. } else {
  12306. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12307. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12308. }
  12309. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12310. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12311. INTEL_INFO(dev)->num_pipes,
  12312. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12313. for_each_pipe(dev_priv, pipe) {
  12314. intel_crtc_init(dev, pipe);
  12315. for_each_sprite(dev_priv, pipe, sprite) {
  12316. ret = intel_plane_init(dev, pipe, sprite);
  12317. if (ret)
  12318. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12319. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12320. }
  12321. }
  12322. intel_init_dpio(dev);
  12323. intel_shared_dpll_init(dev);
  12324. /* Just disable it once at startup */
  12325. i915_disable_vga(dev);
  12326. intel_setup_outputs(dev);
  12327. /* Just in case the BIOS is doing something questionable. */
  12328. intel_fbc_disable(dev);
  12329. drm_modeset_lock_all(dev);
  12330. intel_modeset_setup_hw_state(dev, false);
  12331. drm_modeset_unlock_all(dev);
  12332. for_each_intel_crtc(dev, crtc) {
  12333. if (!crtc->active)
  12334. continue;
  12335. /*
  12336. * Note that reserving the BIOS fb up front prevents us
  12337. * from stuffing other stolen allocations like the ring
  12338. * on top. This prevents some ugliness at boot time, and
  12339. * can even allow for smooth boot transitions if the BIOS
  12340. * fb is large enough for the active pipe configuration.
  12341. */
  12342. if (dev_priv->display.get_initial_plane_config) {
  12343. dev_priv->display.get_initial_plane_config(crtc,
  12344. &crtc->plane_config);
  12345. /*
  12346. * If the fb is shared between multiple heads, we'll
  12347. * just get the first one.
  12348. */
  12349. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12350. }
  12351. }
  12352. }
  12353. static void intel_enable_pipe_a(struct drm_device *dev)
  12354. {
  12355. struct intel_connector *connector;
  12356. struct drm_connector *crt = NULL;
  12357. struct intel_load_detect_pipe load_detect_temp;
  12358. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12359. /* We can't just switch on the pipe A, we need to set things up with a
  12360. * proper mode and output configuration. As a gross hack, enable pipe A
  12361. * by enabling the load detect pipe once. */
  12362. for_each_intel_connector(dev, connector) {
  12363. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12364. crt = &connector->base;
  12365. break;
  12366. }
  12367. }
  12368. if (!crt)
  12369. return;
  12370. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12371. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12372. }
  12373. static bool
  12374. intel_check_plane_mapping(struct intel_crtc *crtc)
  12375. {
  12376. struct drm_device *dev = crtc->base.dev;
  12377. struct drm_i915_private *dev_priv = dev->dev_private;
  12378. u32 reg, val;
  12379. if (INTEL_INFO(dev)->num_pipes == 1)
  12380. return true;
  12381. reg = DSPCNTR(!crtc->plane);
  12382. val = I915_READ(reg);
  12383. if ((val & DISPLAY_PLANE_ENABLE) &&
  12384. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12385. return false;
  12386. return true;
  12387. }
  12388. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12389. {
  12390. struct drm_device *dev = crtc->base.dev;
  12391. struct drm_i915_private *dev_priv = dev->dev_private;
  12392. u32 reg;
  12393. /* Clear any frame start delays used for debugging left by the BIOS */
  12394. reg = PIPECONF(crtc->config->cpu_transcoder);
  12395. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12396. /* restore vblank interrupts to correct state */
  12397. drm_crtc_vblank_reset(&crtc->base);
  12398. if (crtc->active) {
  12399. update_scanline_offset(crtc);
  12400. drm_crtc_vblank_on(&crtc->base);
  12401. }
  12402. /* We need to sanitize the plane -> pipe mapping first because this will
  12403. * disable the crtc (and hence change the state) if it is wrong. Note
  12404. * that gen4+ has a fixed plane -> pipe mapping. */
  12405. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12406. struct intel_connector *connector;
  12407. bool plane;
  12408. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12409. crtc->base.base.id);
  12410. /* Pipe has the wrong plane attached and the plane is active.
  12411. * Temporarily change the plane mapping and disable everything
  12412. * ... */
  12413. plane = crtc->plane;
  12414. crtc->plane = !plane;
  12415. crtc->primary_enabled = true;
  12416. dev_priv->display.crtc_disable(&crtc->base);
  12417. crtc->plane = plane;
  12418. /* ... and break all links. */
  12419. for_each_intel_connector(dev, connector) {
  12420. if (connector->encoder->base.crtc != &crtc->base)
  12421. continue;
  12422. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12423. connector->base.encoder = NULL;
  12424. }
  12425. /* multiple connectors may have the same encoder:
  12426. * handle them and break crtc link separately */
  12427. for_each_intel_connector(dev, connector)
  12428. if (connector->encoder->base.crtc == &crtc->base) {
  12429. connector->encoder->base.crtc = NULL;
  12430. connector->encoder->connectors_active = false;
  12431. }
  12432. WARN_ON(crtc->active);
  12433. crtc->base.state->enable = false;
  12434. crtc->base.enabled = false;
  12435. }
  12436. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12437. crtc->pipe == PIPE_A && !crtc->active) {
  12438. /* BIOS forgot to enable pipe A, this mostly happens after
  12439. * resume. Force-enable the pipe to fix this, the update_dpms
  12440. * call below we restore the pipe to the right state, but leave
  12441. * the required bits on. */
  12442. intel_enable_pipe_a(dev);
  12443. }
  12444. /* Adjust the state of the output pipe according to whether we
  12445. * have active connectors/encoders. */
  12446. intel_crtc_update_dpms(&crtc->base);
  12447. if (crtc->active != crtc->base.state->enable) {
  12448. struct intel_encoder *encoder;
  12449. /* This can happen either due to bugs in the get_hw_state
  12450. * functions or because the pipe is force-enabled due to the
  12451. * pipe A quirk. */
  12452. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12453. crtc->base.base.id,
  12454. crtc->base.state->enable ? "enabled" : "disabled",
  12455. crtc->active ? "enabled" : "disabled");
  12456. crtc->base.state->enable = crtc->active;
  12457. crtc->base.enabled = crtc->active;
  12458. /* Because we only establish the connector -> encoder ->
  12459. * crtc links if something is active, this means the
  12460. * crtc is now deactivated. Break the links. connector
  12461. * -> encoder links are only establish when things are
  12462. * actually up, hence no need to break them. */
  12463. WARN_ON(crtc->active);
  12464. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12465. WARN_ON(encoder->connectors_active);
  12466. encoder->base.crtc = NULL;
  12467. }
  12468. }
  12469. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12470. /*
  12471. * We start out with underrun reporting disabled to avoid races.
  12472. * For correct bookkeeping mark this on active crtcs.
  12473. *
  12474. * Also on gmch platforms we dont have any hardware bits to
  12475. * disable the underrun reporting. Which means we need to start
  12476. * out with underrun reporting disabled also on inactive pipes,
  12477. * since otherwise we'll complain about the garbage we read when
  12478. * e.g. coming up after runtime pm.
  12479. *
  12480. * No protection against concurrent access is required - at
  12481. * worst a fifo underrun happens which also sets this to false.
  12482. */
  12483. crtc->cpu_fifo_underrun_disabled = true;
  12484. crtc->pch_fifo_underrun_disabled = true;
  12485. }
  12486. }
  12487. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12488. {
  12489. struct intel_connector *connector;
  12490. struct drm_device *dev = encoder->base.dev;
  12491. /* We need to check both for a crtc link (meaning that the
  12492. * encoder is active and trying to read from a pipe) and the
  12493. * pipe itself being active. */
  12494. bool has_active_crtc = encoder->base.crtc &&
  12495. to_intel_crtc(encoder->base.crtc)->active;
  12496. if (encoder->connectors_active && !has_active_crtc) {
  12497. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12498. encoder->base.base.id,
  12499. encoder->base.name);
  12500. /* Connector is active, but has no active pipe. This is
  12501. * fallout from our resume register restoring. Disable
  12502. * the encoder manually again. */
  12503. if (encoder->base.crtc) {
  12504. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12505. encoder->base.base.id,
  12506. encoder->base.name);
  12507. encoder->disable(encoder);
  12508. if (encoder->post_disable)
  12509. encoder->post_disable(encoder);
  12510. }
  12511. encoder->base.crtc = NULL;
  12512. encoder->connectors_active = false;
  12513. /* Inconsistent output/port/pipe state happens presumably due to
  12514. * a bug in one of the get_hw_state functions. Or someplace else
  12515. * in our code, like the register restore mess on resume. Clamp
  12516. * things to off as a safer default. */
  12517. for_each_intel_connector(dev, connector) {
  12518. if (connector->encoder != encoder)
  12519. continue;
  12520. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12521. connector->base.encoder = NULL;
  12522. }
  12523. }
  12524. /* Enabled encoders without active connectors will be fixed in
  12525. * the crtc fixup. */
  12526. }
  12527. void i915_redisable_vga_power_on(struct drm_device *dev)
  12528. {
  12529. struct drm_i915_private *dev_priv = dev->dev_private;
  12530. u32 vga_reg = i915_vgacntrl_reg(dev);
  12531. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12532. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12533. i915_disable_vga(dev);
  12534. }
  12535. }
  12536. void i915_redisable_vga(struct drm_device *dev)
  12537. {
  12538. struct drm_i915_private *dev_priv = dev->dev_private;
  12539. /* This function can be called both from intel_modeset_setup_hw_state or
  12540. * at a very early point in our resume sequence, where the power well
  12541. * structures are not yet restored. Since this function is at a very
  12542. * paranoid "someone might have enabled VGA while we were not looking"
  12543. * level, just check if the power well is enabled instead of trying to
  12544. * follow the "don't touch the power well if we don't need it" policy
  12545. * the rest of the driver uses. */
  12546. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12547. return;
  12548. i915_redisable_vga_power_on(dev);
  12549. }
  12550. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12551. {
  12552. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12553. if (!crtc->active)
  12554. return false;
  12555. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  12556. }
  12557. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12558. {
  12559. struct drm_i915_private *dev_priv = dev->dev_private;
  12560. enum pipe pipe;
  12561. struct intel_crtc *crtc;
  12562. struct intel_encoder *encoder;
  12563. struct intel_connector *connector;
  12564. int i;
  12565. for_each_intel_crtc(dev, crtc) {
  12566. memset(crtc->config, 0, sizeof(*crtc->config));
  12567. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12568. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12569. crtc->config);
  12570. crtc->base.state->enable = crtc->active;
  12571. crtc->base.enabled = crtc->active;
  12572. crtc->primary_enabled = primary_get_hw_state(crtc);
  12573. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12574. crtc->base.base.id,
  12575. crtc->active ? "enabled" : "disabled");
  12576. }
  12577. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12578. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12579. pll->on = pll->get_hw_state(dev_priv, pll,
  12580. &pll->config.hw_state);
  12581. pll->active = 0;
  12582. pll->config.crtc_mask = 0;
  12583. for_each_intel_crtc(dev, crtc) {
  12584. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12585. pll->active++;
  12586. pll->config.crtc_mask |= 1 << crtc->pipe;
  12587. }
  12588. }
  12589. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12590. pll->name, pll->config.crtc_mask, pll->on);
  12591. if (pll->config.crtc_mask)
  12592. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12593. }
  12594. for_each_intel_encoder(dev, encoder) {
  12595. pipe = 0;
  12596. if (encoder->get_hw_state(encoder, &pipe)) {
  12597. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12598. encoder->base.crtc = &crtc->base;
  12599. encoder->get_config(encoder, crtc->config);
  12600. } else {
  12601. encoder->base.crtc = NULL;
  12602. }
  12603. encoder->connectors_active = false;
  12604. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12605. encoder->base.base.id,
  12606. encoder->base.name,
  12607. encoder->base.crtc ? "enabled" : "disabled",
  12608. pipe_name(pipe));
  12609. }
  12610. for_each_intel_connector(dev, connector) {
  12611. if (connector->get_hw_state(connector)) {
  12612. connector->base.dpms = DRM_MODE_DPMS_ON;
  12613. connector->encoder->connectors_active = true;
  12614. connector->base.encoder = &connector->encoder->base;
  12615. } else {
  12616. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12617. connector->base.encoder = NULL;
  12618. }
  12619. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12620. connector->base.base.id,
  12621. connector->base.name,
  12622. connector->base.encoder ? "enabled" : "disabled");
  12623. }
  12624. }
  12625. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12626. * and i915 state tracking structures. */
  12627. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12628. bool force_restore)
  12629. {
  12630. struct drm_i915_private *dev_priv = dev->dev_private;
  12631. enum pipe pipe;
  12632. struct intel_crtc *crtc;
  12633. struct intel_encoder *encoder;
  12634. int i;
  12635. intel_modeset_readout_hw_state(dev);
  12636. /*
  12637. * Now that we have the config, copy it to each CRTC struct
  12638. * Note that this could go away if we move to using crtc_config
  12639. * checking everywhere.
  12640. */
  12641. for_each_intel_crtc(dev, crtc) {
  12642. if (crtc->active && i915.fastboot) {
  12643. intel_mode_from_pipe_config(&crtc->base.mode,
  12644. crtc->config);
  12645. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  12646. crtc->base.base.id);
  12647. drm_mode_debug_printmodeline(&crtc->base.mode);
  12648. }
  12649. }
  12650. /* HW state is read out, now we need to sanitize this mess. */
  12651. for_each_intel_encoder(dev, encoder) {
  12652. intel_sanitize_encoder(encoder);
  12653. }
  12654. for_each_pipe(dev_priv, pipe) {
  12655. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12656. intel_sanitize_crtc(crtc);
  12657. intel_dump_pipe_config(crtc, crtc->config,
  12658. "[setup_hw_state]");
  12659. }
  12660. intel_modeset_update_connector_atomic_state(dev);
  12661. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12662. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12663. if (!pll->on || pll->active)
  12664. continue;
  12665. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12666. pll->disable(dev_priv, pll);
  12667. pll->on = false;
  12668. }
  12669. if (IS_GEN9(dev))
  12670. skl_wm_get_hw_state(dev);
  12671. else if (HAS_PCH_SPLIT(dev))
  12672. ilk_wm_get_hw_state(dev);
  12673. if (force_restore) {
  12674. i915_redisable_vga(dev);
  12675. /*
  12676. * We need to use raw interfaces for restoring state to avoid
  12677. * checking (bogus) intermediate states.
  12678. */
  12679. for_each_pipe(dev_priv, pipe) {
  12680. struct drm_crtc *crtc =
  12681. dev_priv->pipe_to_crtc_mapping[pipe];
  12682. intel_crtc_restore_mode(crtc);
  12683. }
  12684. } else {
  12685. intel_modeset_update_staged_output_state(dev);
  12686. }
  12687. intel_modeset_check_state(dev);
  12688. }
  12689. void intel_modeset_gem_init(struct drm_device *dev)
  12690. {
  12691. struct drm_i915_private *dev_priv = dev->dev_private;
  12692. struct drm_crtc *c;
  12693. struct drm_i915_gem_object *obj;
  12694. int ret;
  12695. mutex_lock(&dev->struct_mutex);
  12696. intel_init_gt_powersave(dev);
  12697. mutex_unlock(&dev->struct_mutex);
  12698. /*
  12699. * There may be no VBT; and if the BIOS enabled SSC we can
  12700. * just keep using it to avoid unnecessary flicker. Whereas if the
  12701. * BIOS isn't using it, don't assume it will work even if the VBT
  12702. * indicates as much.
  12703. */
  12704. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  12705. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12706. DREF_SSC1_ENABLE);
  12707. intel_modeset_init_hw(dev);
  12708. intel_setup_overlay(dev);
  12709. /*
  12710. * Make sure any fbs we allocated at startup are properly
  12711. * pinned & fenced. When we do the allocation it's too early
  12712. * for this.
  12713. */
  12714. for_each_crtc(dev, c) {
  12715. obj = intel_fb_obj(c->primary->fb);
  12716. if (obj == NULL)
  12717. continue;
  12718. mutex_lock(&dev->struct_mutex);
  12719. ret = intel_pin_and_fence_fb_obj(c->primary,
  12720. c->primary->fb,
  12721. c->primary->state,
  12722. NULL);
  12723. mutex_unlock(&dev->struct_mutex);
  12724. if (ret) {
  12725. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12726. to_intel_crtc(c)->pipe);
  12727. drm_framebuffer_unreference(c->primary->fb);
  12728. c->primary->fb = NULL;
  12729. update_state_fb(c->primary);
  12730. }
  12731. }
  12732. intel_backlight_register(dev);
  12733. }
  12734. void intel_connector_unregister(struct intel_connector *intel_connector)
  12735. {
  12736. struct drm_connector *connector = &intel_connector->base;
  12737. intel_panel_destroy_backlight(connector);
  12738. drm_connector_unregister(connector);
  12739. }
  12740. void intel_modeset_cleanup(struct drm_device *dev)
  12741. {
  12742. struct drm_i915_private *dev_priv = dev->dev_private;
  12743. struct drm_connector *connector;
  12744. intel_disable_gt_powersave(dev);
  12745. intel_backlight_unregister(dev);
  12746. /*
  12747. * Interrupts and polling as the first thing to avoid creating havoc.
  12748. * Too much stuff here (turning of connectors, ...) would
  12749. * experience fancy races otherwise.
  12750. */
  12751. intel_irq_uninstall(dev_priv);
  12752. /*
  12753. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12754. * poll handlers. Hence disable polling after hpd handling is shut down.
  12755. */
  12756. drm_kms_helper_poll_fini(dev);
  12757. mutex_lock(&dev->struct_mutex);
  12758. intel_unregister_dsm_handler();
  12759. intel_fbc_disable(dev);
  12760. mutex_unlock(&dev->struct_mutex);
  12761. /* flush any delayed tasks or pending work */
  12762. flush_scheduled_work();
  12763. /* destroy the backlight and sysfs files before encoders/connectors */
  12764. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12765. struct intel_connector *intel_connector;
  12766. intel_connector = to_intel_connector(connector);
  12767. intel_connector->unregister(intel_connector);
  12768. }
  12769. drm_mode_config_cleanup(dev);
  12770. intel_cleanup_overlay(dev);
  12771. mutex_lock(&dev->struct_mutex);
  12772. intel_cleanup_gt_powersave(dev);
  12773. mutex_unlock(&dev->struct_mutex);
  12774. }
  12775. /*
  12776. * Return which encoder is currently attached for connector.
  12777. */
  12778. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12779. {
  12780. return &intel_attached_encoder(connector)->base;
  12781. }
  12782. void intel_connector_attach_encoder(struct intel_connector *connector,
  12783. struct intel_encoder *encoder)
  12784. {
  12785. connector->encoder = encoder;
  12786. drm_mode_connector_attach_encoder(&connector->base,
  12787. &encoder->base);
  12788. }
  12789. /*
  12790. * set vga decode state - true == enable VGA decode
  12791. */
  12792. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12793. {
  12794. struct drm_i915_private *dev_priv = dev->dev_private;
  12795. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12796. u16 gmch_ctrl;
  12797. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12798. DRM_ERROR("failed to read control word\n");
  12799. return -EIO;
  12800. }
  12801. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12802. return 0;
  12803. if (state)
  12804. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12805. else
  12806. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12807. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12808. DRM_ERROR("failed to write control word\n");
  12809. return -EIO;
  12810. }
  12811. return 0;
  12812. }
  12813. struct intel_display_error_state {
  12814. u32 power_well_driver;
  12815. int num_transcoders;
  12816. struct intel_cursor_error_state {
  12817. u32 control;
  12818. u32 position;
  12819. u32 base;
  12820. u32 size;
  12821. } cursor[I915_MAX_PIPES];
  12822. struct intel_pipe_error_state {
  12823. bool power_domain_on;
  12824. u32 source;
  12825. u32 stat;
  12826. } pipe[I915_MAX_PIPES];
  12827. struct intel_plane_error_state {
  12828. u32 control;
  12829. u32 stride;
  12830. u32 size;
  12831. u32 pos;
  12832. u32 addr;
  12833. u32 surface;
  12834. u32 tile_offset;
  12835. } plane[I915_MAX_PIPES];
  12836. struct intel_transcoder_error_state {
  12837. bool power_domain_on;
  12838. enum transcoder cpu_transcoder;
  12839. u32 conf;
  12840. u32 htotal;
  12841. u32 hblank;
  12842. u32 hsync;
  12843. u32 vtotal;
  12844. u32 vblank;
  12845. u32 vsync;
  12846. } transcoder[4];
  12847. };
  12848. struct intel_display_error_state *
  12849. intel_display_capture_error_state(struct drm_device *dev)
  12850. {
  12851. struct drm_i915_private *dev_priv = dev->dev_private;
  12852. struct intel_display_error_state *error;
  12853. int transcoders[] = {
  12854. TRANSCODER_A,
  12855. TRANSCODER_B,
  12856. TRANSCODER_C,
  12857. TRANSCODER_EDP,
  12858. };
  12859. int i;
  12860. if (INTEL_INFO(dev)->num_pipes == 0)
  12861. return NULL;
  12862. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12863. if (error == NULL)
  12864. return NULL;
  12865. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12866. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12867. for_each_pipe(dev_priv, i) {
  12868. error->pipe[i].power_domain_on =
  12869. __intel_display_power_is_enabled(dev_priv,
  12870. POWER_DOMAIN_PIPE(i));
  12871. if (!error->pipe[i].power_domain_on)
  12872. continue;
  12873. error->cursor[i].control = I915_READ(CURCNTR(i));
  12874. error->cursor[i].position = I915_READ(CURPOS(i));
  12875. error->cursor[i].base = I915_READ(CURBASE(i));
  12876. error->plane[i].control = I915_READ(DSPCNTR(i));
  12877. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12878. if (INTEL_INFO(dev)->gen <= 3) {
  12879. error->plane[i].size = I915_READ(DSPSIZE(i));
  12880. error->plane[i].pos = I915_READ(DSPPOS(i));
  12881. }
  12882. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12883. error->plane[i].addr = I915_READ(DSPADDR(i));
  12884. if (INTEL_INFO(dev)->gen >= 4) {
  12885. error->plane[i].surface = I915_READ(DSPSURF(i));
  12886. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12887. }
  12888. error->pipe[i].source = I915_READ(PIPESRC(i));
  12889. if (HAS_GMCH_DISPLAY(dev))
  12890. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12891. }
  12892. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  12893. if (HAS_DDI(dev_priv->dev))
  12894. error->num_transcoders++; /* Account for eDP. */
  12895. for (i = 0; i < error->num_transcoders; i++) {
  12896. enum transcoder cpu_transcoder = transcoders[i];
  12897. error->transcoder[i].power_domain_on =
  12898. __intel_display_power_is_enabled(dev_priv,
  12899. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12900. if (!error->transcoder[i].power_domain_on)
  12901. continue;
  12902. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12903. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12904. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12905. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12906. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12907. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12908. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12909. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12910. }
  12911. return error;
  12912. }
  12913. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12914. void
  12915. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12916. struct drm_device *dev,
  12917. struct intel_display_error_state *error)
  12918. {
  12919. struct drm_i915_private *dev_priv = dev->dev_private;
  12920. int i;
  12921. if (!error)
  12922. return;
  12923. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  12924. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12925. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12926. error->power_well_driver);
  12927. for_each_pipe(dev_priv, i) {
  12928. err_printf(m, "Pipe [%d]:\n", i);
  12929. err_printf(m, " Power: %s\n",
  12930. error->pipe[i].power_domain_on ? "on" : "off");
  12931. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12932. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12933. err_printf(m, "Plane [%d]:\n", i);
  12934. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12935. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12936. if (INTEL_INFO(dev)->gen <= 3) {
  12937. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12938. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12939. }
  12940. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12941. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12942. if (INTEL_INFO(dev)->gen >= 4) {
  12943. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12944. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12945. }
  12946. err_printf(m, "Cursor [%d]:\n", i);
  12947. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12948. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12949. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12950. }
  12951. for (i = 0; i < error->num_transcoders; i++) {
  12952. err_printf(m, "CPU transcoder: %c\n",
  12953. transcoder_name(error->transcoder[i].cpu_transcoder));
  12954. err_printf(m, " Power: %s\n",
  12955. error->transcoder[i].power_domain_on ? "on" : "off");
  12956. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12957. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12958. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12959. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12960. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12961. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12962. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12963. }
  12964. }
  12965. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  12966. {
  12967. struct intel_crtc *crtc;
  12968. for_each_intel_crtc(dev, crtc) {
  12969. struct intel_unpin_work *work;
  12970. spin_lock_irq(&dev->event_lock);
  12971. work = crtc->unpin_work;
  12972. if (work && work->event &&
  12973. work->event->base.file_priv == file) {
  12974. kfree(work->event);
  12975. work->event = NULL;
  12976. }
  12977. spin_unlock_irq(&dev->event_lock);
  12978. }
  12979. }