intel_display.c 429 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533
  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. to_intel_plane_state(plane->state)->visible = true;
  1935. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1936. crtc->x, crtc->y);
  1937. }
  1938. static bool need_vtd_wa(struct drm_device *dev)
  1939. {
  1940. #ifdef CONFIG_INTEL_IOMMU
  1941. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1942. return true;
  1943. #endif
  1944. return false;
  1945. }
  1946. unsigned int
  1947. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1948. uint64_t fb_format_modifier)
  1949. {
  1950. unsigned int tile_height;
  1951. uint32_t pixel_bytes;
  1952. switch (fb_format_modifier) {
  1953. case DRM_FORMAT_MOD_NONE:
  1954. tile_height = 1;
  1955. break;
  1956. case I915_FORMAT_MOD_X_TILED:
  1957. tile_height = IS_GEN2(dev) ? 16 : 8;
  1958. break;
  1959. case I915_FORMAT_MOD_Y_TILED:
  1960. tile_height = 32;
  1961. break;
  1962. case I915_FORMAT_MOD_Yf_TILED:
  1963. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1964. switch (pixel_bytes) {
  1965. default:
  1966. case 1:
  1967. tile_height = 64;
  1968. break;
  1969. case 2:
  1970. case 4:
  1971. tile_height = 32;
  1972. break;
  1973. case 8:
  1974. tile_height = 16;
  1975. break;
  1976. case 16:
  1977. WARN_ONCE(1,
  1978. "128-bit pixels are not supported for display!");
  1979. tile_height = 16;
  1980. break;
  1981. }
  1982. break;
  1983. default:
  1984. MISSING_CASE(fb_format_modifier);
  1985. tile_height = 1;
  1986. break;
  1987. }
  1988. return tile_height;
  1989. }
  1990. unsigned int
  1991. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1992. uint32_t pixel_format, uint64_t fb_format_modifier)
  1993. {
  1994. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1995. fb_format_modifier));
  1996. }
  1997. static int
  1998. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1999. const struct drm_plane_state *plane_state)
  2000. {
  2001. struct intel_rotation_info *info = &view->rotation_info;
  2002. *view = i915_ggtt_view_normal;
  2003. if (!plane_state)
  2004. return 0;
  2005. if (!intel_rotation_90_or_270(plane_state->rotation))
  2006. return 0;
  2007. *view = i915_ggtt_view_rotated;
  2008. info->height = fb->height;
  2009. info->pixel_format = fb->pixel_format;
  2010. info->pitch = fb->pitches[0];
  2011. info->fb_modifier = fb->modifier[0];
  2012. return 0;
  2013. }
  2014. int
  2015. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2016. struct drm_framebuffer *fb,
  2017. const struct drm_plane_state *plane_state,
  2018. struct intel_engine_cs *pipelined)
  2019. {
  2020. struct drm_device *dev = fb->dev;
  2021. struct drm_i915_private *dev_priv = dev->dev_private;
  2022. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2023. struct i915_ggtt_view view;
  2024. u32 alignment;
  2025. int ret;
  2026. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2027. switch (fb->modifier[0]) {
  2028. case DRM_FORMAT_MOD_NONE:
  2029. if (INTEL_INFO(dev)->gen >= 9)
  2030. alignment = 256 * 1024;
  2031. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  2032. alignment = 128 * 1024;
  2033. else if (INTEL_INFO(dev)->gen >= 4)
  2034. alignment = 4 * 1024;
  2035. else
  2036. alignment = 64 * 1024;
  2037. break;
  2038. case I915_FORMAT_MOD_X_TILED:
  2039. if (INTEL_INFO(dev)->gen >= 9)
  2040. alignment = 256 * 1024;
  2041. else {
  2042. /* pin() will align the object as required by fence */
  2043. alignment = 0;
  2044. }
  2045. break;
  2046. case I915_FORMAT_MOD_Y_TILED:
  2047. case I915_FORMAT_MOD_Yf_TILED:
  2048. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2049. "Y tiling bo slipped through, driver bug!\n"))
  2050. return -EINVAL;
  2051. alignment = 1 * 1024 * 1024;
  2052. break;
  2053. default:
  2054. MISSING_CASE(fb->modifier[0]);
  2055. return -EINVAL;
  2056. }
  2057. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2058. if (ret)
  2059. return ret;
  2060. /* Note that the w/a also requires 64 PTE of padding following the
  2061. * bo. We currently fill all unused PTE with the shadow page and so
  2062. * we should always have valid PTE following the scanout preventing
  2063. * the VT-d warning.
  2064. */
  2065. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2066. alignment = 256 * 1024;
  2067. /*
  2068. * Global gtt pte registers are special registers which actually forward
  2069. * writes to a chunk of system memory. Which means that there is no risk
  2070. * that the register values disappear as soon as we call
  2071. * intel_runtime_pm_put(), so it is correct to wrap only the
  2072. * pin/unpin/fence and not more.
  2073. */
  2074. intel_runtime_pm_get(dev_priv);
  2075. dev_priv->mm.interruptible = false;
  2076. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2077. &view);
  2078. if (ret)
  2079. goto err_interruptible;
  2080. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2081. * fence, whereas 965+ only requires a fence if using
  2082. * framebuffer compression. For simplicity, we always install
  2083. * a fence as the cost is not that onerous.
  2084. */
  2085. ret = i915_gem_object_get_fence(obj);
  2086. if (ret)
  2087. goto err_unpin;
  2088. i915_gem_object_pin_fence(obj);
  2089. dev_priv->mm.interruptible = true;
  2090. intel_runtime_pm_put(dev_priv);
  2091. return 0;
  2092. err_unpin:
  2093. i915_gem_object_unpin_from_display_plane(obj, &view);
  2094. err_interruptible:
  2095. dev_priv->mm.interruptible = true;
  2096. intel_runtime_pm_put(dev_priv);
  2097. return ret;
  2098. }
  2099. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2100. const struct drm_plane_state *plane_state)
  2101. {
  2102. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2103. struct i915_ggtt_view view;
  2104. int ret;
  2105. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2106. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2107. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2108. i915_gem_object_unpin_fence(obj);
  2109. i915_gem_object_unpin_from_display_plane(obj, &view);
  2110. }
  2111. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2112. * is assumed to be a power-of-two. */
  2113. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2114. unsigned int tiling_mode,
  2115. unsigned int cpp,
  2116. unsigned int pitch)
  2117. {
  2118. if (tiling_mode != I915_TILING_NONE) {
  2119. unsigned int tile_rows, tiles;
  2120. tile_rows = *y / 8;
  2121. *y %= 8;
  2122. tiles = *x / (512/cpp);
  2123. *x %= 512/cpp;
  2124. return tile_rows * pitch * 8 + tiles * 4096;
  2125. } else {
  2126. unsigned int offset;
  2127. offset = *y * pitch + *x * cpp;
  2128. *y = 0;
  2129. *x = (offset & 4095) / cpp;
  2130. return offset & -4096;
  2131. }
  2132. }
  2133. static int i9xx_format_to_fourcc(int format)
  2134. {
  2135. switch (format) {
  2136. case DISPPLANE_8BPP:
  2137. return DRM_FORMAT_C8;
  2138. case DISPPLANE_BGRX555:
  2139. return DRM_FORMAT_XRGB1555;
  2140. case DISPPLANE_BGRX565:
  2141. return DRM_FORMAT_RGB565;
  2142. default:
  2143. case DISPPLANE_BGRX888:
  2144. return DRM_FORMAT_XRGB8888;
  2145. case DISPPLANE_RGBX888:
  2146. return DRM_FORMAT_XBGR8888;
  2147. case DISPPLANE_BGRX101010:
  2148. return DRM_FORMAT_XRGB2101010;
  2149. case DISPPLANE_RGBX101010:
  2150. return DRM_FORMAT_XBGR2101010;
  2151. }
  2152. }
  2153. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2154. {
  2155. switch (format) {
  2156. case PLANE_CTL_FORMAT_RGB_565:
  2157. return DRM_FORMAT_RGB565;
  2158. default:
  2159. case PLANE_CTL_FORMAT_XRGB_8888:
  2160. if (rgb_order) {
  2161. if (alpha)
  2162. return DRM_FORMAT_ABGR8888;
  2163. else
  2164. return DRM_FORMAT_XBGR8888;
  2165. } else {
  2166. if (alpha)
  2167. return DRM_FORMAT_ARGB8888;
  2168. else
  2169. return DRM_FORMAT_XRGB8888;
  2170. }
  2171. case PLANE_CTL_FORMAT_XRGB_2101010:
  2172. if (rgb_order)
  2173. return DRM_FORMAT_XBGR2101010;
  2174. else
  2175. return DRM_FORMAT_XRGB2101010;
  2176. }
  2177. }
  2178. static bool
  2179. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2180. struct intel_initial_plane_config *plane_config)
  2181. {
  2182. struct drm_device *dev = crtc->base.dev;
  2183. struct drm_i915_gem_object *obj = NULL;
  2184. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2185. struct drm_framebuffer *fb = &plane_config->fb->base;
  2186. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2187. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2188. PAGE_SIZE);
  2189. size_aligned -= base_aligned;
  2190. if (plane_config->size == 0)
  2191. return false;
  2192. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2193. base_aligned,
  2194. base_aligned,
  2195. size_aligned);
  2196. if (!obj)
  2197. return false;
  2198. obj->tiling_mode = plane_config->tiling;
  2199. if (obj->tiling_mode == I915_TILING_X)
  2200. obj->stride = fb->pitches[0];
  2201. mode_cmd.pixel_format = fb->pixel_format;
  2202. mode_cmd.width = fb->width;
  2203. mode_cmd.height = fb->height;
  2204. mode_cmd.pitches[0] = fb->pitches[0];
  2205. mode_cmd.modifier[0] = fb->modifier[0];
  2206. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2207. mutex_lock(&dev->struct_mutex);
  2208. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2209. &mode_cmd, obj)) {
  2210. DRM_DEBUG_KMS("intel fb init failed\n");
  2211. goto out_unref_obj;
  2212. }
  2213. mutex_unlock(&dev->struct_mutex);
  2214. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2215. return true;
  2216. out_unref_obj:
  2217. drm_gem_object_unreference(&obj->base);
  2218. mutex_unlock(&dev->struct_mutex);
  2219. return false;
  2220. }
  2221. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2222. static void
  2223. update_state_fb(struct drm_plane *plane)
  2224. {
  2225. if (plane->fb == plane->state->fb)
  2226. return;
  2227. if (plane->state->fb)
  2228. drm_framebuffer_unreference(plane->state->fb);
  2229. plane->state->fb = plane->fb;
  2230. if (plane->state->fb)
  2231. drm_framebuffer_reference(plane->state->fb);
  2232. }
  2233. static void
  2234. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2235. struct intel_initial_plane_config *plane_config)
  2236. {
  2237. struct drm_device *dev = intel_crtc->base.dev;
  2238. struct drm_i915_private *dev_priv = dev->dev_private;
  2239. struct drm_crtc *c;
  2240. struct intel_crtc *i;
  2241. struct drm_i915_gem_object *obj;
  2242. struct drm_plane *primary = intel_crtc->base.primary;
  2243. struct drm_framebuffer *fb;
  2244. if (!plane_config->fb)
  2245. return;
  2246. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2247. fb = &plane_config->fb->base;
  2248. goto valid_fb;
  2249. }
  2250. kfree(plane_config->fb);
  2251. /*
  2252. * Failed to alloc the obj, check to see if we should share
  2253. * an fb with another CRTC instead
  2254. */
  2255. for_each_crtc(dev, c) {
  2256. i = to_intel_crtc(c);
  2257. if (c == &intel_crtc->base)
  2258. continue;
  2259. if (!i->active)
  2260. continue;
  2261. fb = c->primary->fb;
  2262. if (!fb)
  2263. continue;
  2264. obj = intel_fb_obj(fb);
  2265. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2266. drm_framebuffer_reference(fb);
  2267. goto valid_fb;
  2268. }
  2269. }
  2270. return;
  2271. valid_fb:
  2272. obj = intel_fb_obj(fb);
  2273. if (obj->tiling_mode != I915_TILING_NONE)
  2274. dev_priv->preserve_bios_swizzle = true;
  2275. primary->fb = fb;
  2276. primary->state->crtc = &intel_crtc->base;
  2277. primary->crtc = &intel_crtc->base;
  2278. update_state_fb(primary);
  2279. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2280. }
  2281. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2282. struct drm_framebuffer *fb,
  2283. int x, int y)
  2284. {
  2285. struct drm_device *dev = crtc->dev;
  2286. struct drm_i915_private *dev_priv = dev->dev_private;
  2287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2288. struct drm_plane *primary = crtc->primary;
  2289. bool visible = to_intel_plane_state(primary->state)->visible;
  2290. struct drm_i915_gem_object *obj;
  2291. int plane = intel_crtc->plane;
  2292. unsigned long linear_offset;
  2293. u32 dspcntr;
  2294. u32 reg = DSPCNTR(plane);
  2295. int pixel_size;
  2296. if (!visible || !fb) {
  2297. I915_WRITE(reg, 0);
  2298. if (INTEL_INFO(dev)->gen >= 4)
  2299. I915_WRITE(DSPSURF(plane), 0);
  2300. else
  2301. I915_WRITE(DSPADDR(plane), 0);
  2302. POSTING_READ(reg);
  2303. return;
  2304. }
  2305. obj = intel_fb_obj(fb);
  2306. if (WARN_ON(obj == NULL))
  2307. return;
  2308. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2309. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2310. dspcntr |= DISPLAY_PLANE_ENABLE;
  2311. if (INTEL_INFO(dev)->gen < 4) {
  2312. if (intel_crtc->pipe == PIPE_B)
  2313. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2314. /* pipesrc and dspsize control the size that is scaled from,
  2315. * which should always be the user's requested size.
  2316. */
  2317. I915_WRITE(DSPSIZE(plane),
  2318. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2319. (intel_crtc->config->pipe_src_w - 1));
  2320. I915_WRITE(DSPPOS(plane), 0);
  2321. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2322. I915_WRITE(PRIMSIZE(plane),
  2323. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2324. (intel_crtc->config->pipe_src_w - 1));
  2325. I915_WRITE(PRIMPOS(plane), 0);
  2326. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2327. }
  2328. switch (fb->pixel_format) {
  2329. case DRM_FORMAT_C8:
  2330. dspcntr |= DISPPLANE_8BPP;
  2331. break;
  2332. case DRM_FORMAT_XRGB1555:
  2333. case DRM_FORMAT_ARGB1555:
  2334. dspcntr |= DISPPLANE_BGRX555;
  2335. break;
  2336. case DRM_FORMAT_RGB565:
  2337. dspcntr |= DISPPLANE_BGRX565;
  2338. break;
  2339. case DRM_FORMAT_XRGB8888:
  2340. case DRM_FORMAT_ARGB8888:
  2341. dspcntr |= DISPPLANE_BGRX888;
  2342. break;
  2343. case DRM_FORMAT_XBGR8888:
  2344. case DRM_FORMAT_ABGR8888:
  2345. dspcntr |= DISPPLANE_RGBX888;
  2346. break;
  2347. case DRM_FORMAT_XRGB2101010:
  2348. case DRM_FORMAT_ARGB2101010:
  2349. dspcntr |= DISPPLANE_BGRX101010;
  2350. break;
  2351. case DRM_FORMAT_XBGR2101010:
  2352. case DRM_FORMAT_ABGR2101010:
  2353. dspcntr |= DISPPLANE_RGBX101010;
  2354. break;
  2355. default:
  2356. BUG();
  2357. }
  2358. if (INTEL_INFO(dev)->gen >= 4 &&
  2359. obj->tiling_mode != I915_TILING_NONE)
  2360. dspcntr |= DISPPLANE_TILED;
  2361. if (IS_G4X(dev))
  2362. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2363. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2364. if (INTEL_INFO(dev)->gen >= 4) {
  2365. intel_crtc->dspaddr_offset =
  2366. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2367. pixel_size,
  2368. fb->pitches[0]);
  2369. linear_offset -= intel_crtc->dspaddr_offset;
  2370. } else {
  2371. intel_crtc->dspaddr_offset = linear_offset;
  2372. }
  2373. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2374. dspcntr |= DISPPLANE_ROTATE_180;
  2375. x += (intel_crtc->config->pipe_src_w - 1);
  2376. y += (intel_crtc->config->pipe_src_h - 1);
  2377. /* Finding the last pixel of the last line of the display
  2378. data and adding to linear_offset*/
  2379. linear_offset +=
  2380. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2381. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2382. }
  2383. I915_WRITE(reg, dspcntr);
  2384. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2385. if (INTEL_INFO(dev)->gen >= 4) {
  2386. I915_WRITE(DSPSURF(plane),
  2387. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2388. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2389. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2390. } else
  2391. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2392. POSTING_READ(reg);
  2393. }
  2394. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2395. struct drm_framebuffer *fb,
  2396. int x, int y)
  2397. {
  2398. struct drm_device *dev = crtc->dev;
  2399. struct drm_i915_private *dev_priv = dev->dev_private;
  2400. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2401. struct drm_plane *primary = crtc->primary;
  2402. bool visible = to_intel_plane_state(primary->state)->visible;
  2403. struct drm_i915_gem_object *obj;
  2404. int plane = intel_crtc->plane;
  2405. unsigned long linear_offset;
  2406. u32 dspcntr;
  2407. u32 reg = DSPCNTR(plane);
  2408. int pixel_size;
  2409. if (!visible || !fb) {
  2410. I915_WRITE(reg, 0);
  2411. I915_WRITE(DSPSURF(plane), 0);
  2412. POSTING_READ(reg);
  2413. return;
  2414. }
  2415. obj = intel_fb_obj(fb);
  2416. if (WARN_ON(obj == NULL))
  2417. return;
  2418. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2419. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2420. dspcntr |= DISPLAY_PLANE_ENABLE;
  2421. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2422. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2423. switch (fb->pixel_format) {
  2424. case DRM_FORMAT_C8:
  2425. dspcntr |= DISPPLANE_8BPP;
  2426. break;
  2427. case DRM_FORMAT_RGB565:
  2428. dspcntr |= DISPPLANE_BGRX565;
  2429. break;
  2430. case DRM_FORMAT_XRGB8888:
  2431. case DRM_FORMAT_ARGB8888:
  2432. dspcntr |= DISPPLANE_BGRX888;
  2433. break;
  2434. case DRM_FORMAT_XBGR8888:
  2435. case DRM_FORMAT_ABGR8888:
  2436. dspcntr |= DISPPLANE_RGBX888;
  2437. break;
  2438. case DRM_FORMAT_XRGB2101010:
  2439. case DRM_FORMAT_ARGB2101010:
  2440. dspcntr |= DISPPLANE_BGRX101010;
  2441. break;
  2442. case DRM_FORMAT_XBGR2101010:
  2443. case DRM_FORMAT_ABGR2101010:
  2444. dspcntr |= DISPPLANE_RGBX101010;
  2445. break;
  2446. default:
  2447. BUG();
  2448. }
  2449. if (obj->tiling_mode != I915_TILING_NONE)
  2450. dspcntr |= DISPPLANE_TILED;
  2451. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2452. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2453. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2454. intel_crtc->dspaddr_offset =
  2455. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2456. pixel_size,
  2457. fb->pitches[0]);
  2458. linear_offset -= intel_crtc->dspaddr_offset;
  2459. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2460. dspcntr |= DISPPLANE_ROTATE_180;
  2461. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2462. x += (intel_crtc->config->pipe_src_w - 1);
  2463. y += (intel_crtc->config->pipe_src_h - 1);
  2464. /* Finding the last pixel of the last line of the display
  2465. data and adding to linear_offset*/
  2466. linear_offset +=
  2467. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2468. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2469. }
  2470. }
  2471. I915_WRITE(reg, dspcntr);
  2472. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2473. I915_WRITE(DSPSURF(plane),
  2474. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2475. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2476. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2477. } else {
  2478. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2479. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2480. }
  2481. POSTING_READ(reg);
  2482. }
  2483. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2484. uint32_t pixel_format)
  2485. {
  2486. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2487. /*
  2488. * The stride is either expressed as a multiple of 64 bytes
  2489. * chunks for linear buffers or in number of tiles for tiled
  2490. * buffers.
  2491. */
  2492. switch (fb_modifier) {
  2493. case DRM_FORMAT_MOD_NONE:
  2494. return 64;
  2495. case I915_FORMAT_MOD_X_TILED:
  2496. if (INTEL_INFO(dev)->gen == 2)
  2497. return 128;
  2498. return 512;
  2499. case I915_FORMAT_MOD_Y_TILED:
  2500. /* No need to check for old gens and Y tiling since this is
  2501. * about the display engine and those will be blocked before
  2502. * we get here.
  2503. */
  2504. return 128;
  2505. case I915_FORMAT_MOD_Yf_TILED:
  2506. if (bits_per_pixel == 8)
  2507. return 64;
  2508. else
  2509. return 128;
  2510. default:
  2511. MISSING_CASE(fb_modifier);
  2512. return 64;
  2513. }
  2514. }
  2515. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2516. struct drm_i915_gem_object *obj)
  2517. {
  2518. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2519. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2520. view = &i915_ggtt_view_rotated;
  2521. return i915_gem_obj_ggtt_offset_view(obj, view);
  2522. }
  2523. /*
  2524. * This function detaches (aka. unbinds) unused scalers in hardware
  2525. */
  2526. void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2527. {
  2528. struct drm_device *dev;
  2529. struct drm_i915_private *dev_priv;
  2530. struct intel_crtc_scaler_state *scaler_state;
  2531. int i;
  2532. if (!intel_crtc || !intel_crtc->config)
  2533. return;
  2534. dev = intel_crtc->base.dev;
  2535. dev_priv = dev->dev_private;
  2536. scaler_state = &intel_crtc->config->scaler_state;
  2537. /* loop through and disable scalers that aren't in use */
  2538. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2539. if (!scaler_state->scalers[i].in_use) {
  2540. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2541. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2542. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2543. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2544. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2545. }
  2546. }
  2547. }
  2548. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2549. {
  2550. u32 plane_ctl_format = 0;
  2551. switch (pixel_format) {
  2552. case DRM_FORMAT_RGB565:
  2553. plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
  2554. break;
  2555. case DRM_FORMAT_XBGR8888:
  2556. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2557. break;
  2558. case DRM_FORMAT_XRGB8888:
  2559. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
  2560. break;
  2561. /*
  2562. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2563. * to be already pre-multiplied. We need to add a knob (or a different
  2564. * DRM_FORMAT) for user-space to configure that.
  2565. */
  2566. case DRM_FORMAT_ABGR8888:
  2567. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2568. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2569. break;
  2570. case DRM_FORMAT_ARGB8888:
  2571. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
  2572. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2573. break;
  2574. case DRM_FORMAT_XRGB2101010:
  2575. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
  2576. break;
  2577. case DRM_FORMAT_XBGR2101010:
  2578. plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2579. break;
  2580. case DRM_FORMAT_YUYV:
  2581. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2582. break;
  2583. case DRM_FORMAT_YVYU:
  2584. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2585. break;
  2586. case DRM_FORMAT_UYVY:
  2587. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2588. break;
  2589. case DRM_FORMAT_VYUY:
  2590. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2591. break;
  2592. default:
  2593. BUG();
  2594. }
  2595. return plane_ctl_format;
  2596. }
  2597. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2598. {
  2599. u32 plane_ctl_tiling = 0;
  2600. switch (fb_modifier) {
  2601. case DRM_FORMAT_MOD_NONE:
  2602. break;
  2603. case I915_FORMAT_MOD_X_TILED:
  2604. plane_ctl_tiling = PLANE_CTL_TILED_X;
  2605. break;
  2606. case I915_FORMAT_MOD_Y_TILED:
  2607. plane_ctl_tiling = PLANE_CTL_TILED_Y;
  2608. break;
  2609. case I915_FORMAT_MOD_Yf_TILED:
  2610. plane_ctl_tiling = PLANE_CTL_TILED_YF;
  2611. break;
  2612. default:
  2613. MISSING_CASE(fb_modifier);
  2614. }
  2615. return plane_ctl_tiling;
  2616. }
  2617. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2618. {
  2619. u32 plane_ctl_rotation = 0;
  2620. switch (rotation) {
  2621. case BIT(DRM_ROTATE_0):
  2622. break;
  2623. case BIT(DRM_ROTATE_90):
  2624. plane_ctl_rotation = PLANE_CTL_ROTATE_90;
  2625. break;
  2626. case BIT(DRM_ROTATE_180):
  2627. plane_ctl_rotation = PLANE_CTL_ROTATE_180;
  2628. break;
  2629. case BIT(DRM_ROTATE_270):
  2630. plane_ctl_rotation = PLANE_CTL_ROTATE_270;
  2631. break;
  2632. default:
  2633. MISSING_CASE(rotation);
  2634. }
  2635. return plane_ctl_rotation;
  2636. }
  2637. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2638. struct drm_framebuffer *fb,
  2639. int x, int y)
  2640. {
  2641. struct drm_device *dev = crtc->dev;
  2642. struct drm_i915_private *dev_priv = dev->dev_private;
  2643. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2644. struct drm_plane *plane = crtc->primary;
  2645. bool visible = to_intel_plane_state(plane->state)->visible;
  2646. struct drm_i915_gem_object *obj;
  2647. int pipe = intel_crtc->pipe;
  2648. u32 plane_ctl, stride_div, stride;
  2649. u32 tile_height, plane_offset, plane_size;
  2650. unsigned int rotation;
  2651. int x_offset, y_offset;
  2652. unsigned long surf_addr;
  2653. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2654. struct intel_plane_state *plane_state;
  2655. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2656. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2657. int scaler_id = -1;
  2658. plane_state = to_intel_plane_state(plane->state);
  2659. if (!visible || !fb) {
  2660. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2661. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2662. POSTING_READ(PLANE_CTL(pipe, 0));
  2663. return;
  2664. }
  2665. plane_ctl = PLANE_CTL_ENABLE |
  2666. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2667. PLANE_CTL_PIPE_CSC_ENABLE;
  2668. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2669. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2670. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2671. rotation = plane->state->rotation;
  2672. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2673. obj = intel_fb_obj(fb);
  2674. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2675. fb->pixel_format);
  2676. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2677. /*
  2678. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2679. * update_plane helpers are called from legacy paths.
  2680. * Once full atomic crtc is available, below check can be avoided.
  2681. */
  2682. if (drm_rect_width(&plane_state->src)) {
  2683. scaler_id = plane_state->scaler_id;
  2684. src_x = plane_state->src.x1 >> 16;
  2685. src_y = plane_state->src.y1 >> 16;
  2686. src_w = drm_rect_width(&plane_state->src) >> 16;
  2687. src_h = drm_rect_height(&plane_state->src) >> 16;
  2688. dst_x = plane_state->dst.x1;
  2689. dst_y = plane_state->dst.y1;
  2690. dst_w = drm_rect_width(&plane_state->dst);
  2691. dst_h = drm_rect_height(&plane_state->dst);
  2692. WARN_ON(x != src_x || y != src_y);
  2693. } else {
  2694. src_w = intel_crtc->config->pipe_src_w;
  2695. src_h = intel_crtc->config->pipe_src_h;
  2696. }
  2697. if (intel_rotation_90_or_270(rotation)) {
  2698. /* stride = Surface height in tiles */
  2699. tile_height = intel_tile_height(dev, fb->bits_per_pixel,
  2700. fb->modifier[0]);
  2701. stride = DIV_ROUND_UP(fb->height, tile_height);
  2702. x_offset = stride * tile_height - y - src_h;
  2703. y_offset = x;
  2704. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2705. } else {
  2706. stride = fb->pitches[0] / stride_div;
  2707. x_offset = x;
  2708. y_offset = y;
  2709. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2710. }
  2711. plane_offset = y_offset << 16 | x_offset;
  2712. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2713. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2714. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2715. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2716. if (scaler_id >= 0) {
  2717. uint32_t ps_ctrl = 0;
  2718. WARN_ON(!dst_w || !dst_h);
  2719. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2720. crtc_state->scaler_state.scalers[scaler_id].mode;
  2721. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2722. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2723. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2724. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2725. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2726. } else {
  2727. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2728. }
  2729. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2730. POSTING_READ(PLANE_SURF(pipe, 0));
  2731. }
  2732. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2733. static int
  2734. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2735. int x, int y, enum mode_set_atomic state)
  2736. {
  2737. struct drm_device *dev = crtc->dev;
  2738. struct drm_i915_private *dev_priv = dev->dev_private;
  2739. if (dev_priv->display.disable_fbc)
  2740. dev_priv->display.disable_fbc(dev);
  2741. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2742. return 0;
  2743. }
  2744. static void intel_complete_page_flips(struct drm_device *dev)
  2745. {
  2746. struct drm_crtc *crtc;
  2747. for_each_crtc(dev, crtc) {
  2748. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2749. enum plane plane = intel_crtc->plane;
  2750. intel_prepare_page_flip(dev, plane);
  2751. intel_finish_page_flip_plane(dev, plane);
  2752. }
  2753. }
  2754. static void intel_update_primary_planes(struct drm_device *dev)
  2755. {
  2756. struct drm_i915_private *dev_priv = dev->dev_private;
  2757. struct drm_crtc *crtc;
  2758. for_each_crtc(dev, crtc) {
  2759. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2760. drm_modeset_lock(&crtc->mutex, NULL);
  2761. /*
  2762. * FIXME: Once we have proper support for primary planes (and
  2763. * disabling them without disabling the entire crtc) allow again
  2764. * a NULL crtc->primary->fb.
  2765. */
  2766. if (intel_crtc->active && crtc->primary->fb)
  2767. dev_priv->display.update_primary_plane(crtc,
  2768. crtc->primary->fb,
  2769. crtc->x,
  2770. crtc->y);
  2771. drm_modeset_unlock(&crtc->mutex);
  2772. }
  2773. }
  2774. void intel_prepare_reset(struct drm_device *dev)
  2775. {
  2776. struct drm_i915_private *dev_priv = to_i915(dev);
  2777. struct intel_crtc *crtc;
  2778. /* no reset support for gen2 */
  2779. if (IS_GEN2(dev))
  2780. return;
  2781. /* reset doesn't touch the display */
  2782. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2783. return;
  2784. drm_modeset_lock_all(dev);
  2785. /*
  2786. * Disabling the crtcs gracefully seems nicer. Also the
  2787. * g33 docs say we should at least disable all the planes.
  2788. */
  2789. for_each_intel_crtc(dev, crtc) {
  2790. if (crtc->active)
  2791. dev_priv->display.crtc_disable(&crtc->base);
  2792. }
  2793. }
  2794. void intel_finish_reset(struct drm_device *dev)
  2795. {
  2796. struct drm_i915_private *dev_priv = to_i915(dev);
  2797. /*
  2798. * Flips in the rings will be nuked by the reset,
  2799. * so complete all pending flips so that user space
  2800. * will get its events and not get stuck.
  2801. */
  2802. intel_complete_page_flips(dev);
  2803. /* no reset support for gen2 */
  2804. if (IS_GEN2(dev))
  2805. return;
  2806. /* reset doesn't touch the display */
  2807. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2808. /*
  2809. * Flips in the rings have been nuked by the reset,
  2810. * so update the base address of all primary
  2811. * planes to the the last fb to make sure we're
  2812. * showing the correct fb after a reset.
  2813. */
  2814. intel_update_primary_planes(dev);
  2815. return;
  2816. }
  2817. /*
  2818. * The display has been reset as well,
  2819. * so need a full re-initialization.
  2820. */
  2821. intel_runtime_pm_disable_interrupts(dev_priv);
  2822. intel_runtime_pm_enable_interrupts(dev_priv);
  2823. intel_modeset_init_hw(dev);
  2824. spin_lock_irq(&dev_priv->irq_lock);
  2825. if (dev_priv->display.hpd_irq_setup)
  2826. dev_priv->display.hpd_irq_setup(dev);
  2827. spin_unlock_irq(&dev_priv->irq_lock);
  2828. intel_modeset_setup_hw_state(dev, true);
  2829. intel_hpd_init(dev_priv);
  2830. drm_modeset_unlock_all(dev);
  2831. }
  2832. static int
  2833. intel_finish_fb(struct drm_framebuffer *old_fb)
  2834. {
  2835. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2836. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2837. bool was_interruptible = dev_priv->mm.interruptible;
  2838. int ret;
  2839. /* Big Hammer, we also need to ensure that any pending
  2840. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2841. * current scanout is retired before unpinning the old
  2842. * framebuffer.
  2843. *
  2844. * This should only fail upon a hung GPU, in which case we
  2845. * can safely continue.
  2846. */
  2847. dev_priv->mm.interruptible = false;
  2848. ret = i915_gem_object_finish_gpu(obj);
  2849. dev_priv->mm.interruptible = was_interruptible;
  2850. return ret;
  2851. }
  2852. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2853. {
  2854. struct drm_device *dev = crtc->dev;
  2855. struct drm_i915_private *dev_priv = dev->dev_private;
  2856. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2857. bool pending;
  2858. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2859. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2860. return false;
  2861. spin_lock_irq(&dev->event_lock);
  2862. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2863. spin_unlock_irq(&dev->event_lock);
  2864. return pending;
  2865. }
  2866. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2867. {
  2868. struct drm_device *dev = crtc->base.dev;
  2869. struct drm_i915_private *dev_priv = dev->dev_private;
  2870. const struct drm_display_mode *adjusted_mode;
  2871. if (!i915.fastboot)
  2872. return;
  2873. /*
  2874. * Update pipe size and adjust fitter if needed: the reason for this is
  2875. * that in compute_mode_changes we check the native mode (not the pfit
  2876. * mode) to see if we can flip rather than do a full mode set. In the
  2877. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2878. * pfit state, we'll end up with a big fb scanned out into the wrong
  2879. * sized surface.
  2880. *
  2881. * To fix this properly, we need to hoist the checks up into
  2882. * compute_mode_changes (or above), check the actual pfit state and
  2883. * whether the platform allows pfit disable with pipe active, and only
  2884. * then update the pipesrc and pfit state, even on the flip path.
  2885. */
  2886. adjusted_mode = &crtc->config->base.adjusted_mode;
  2887. I915_WRITE(PIPESRC(crtc->pipe),
  2888. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2889. (adjusted_mode->crtc_vdisplay - 1));
  2890. if (!crtc->config->pch_pfit.enabled &&
  2891. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2892. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2893. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2894. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2895. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2896. }
  2897. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2898. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2899. }
  2900. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2901. {
  2902. struct drm_device *dev = crtc->dev;
  2903. struct drm_i915_private *dev_priv = dev->dev_private;
  2904. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2905. int pipe = intel_crtc->pipe;
  2906. u32 reg, temp;
  2907. /* enable normal train */
  2908. reg = FDI_TX_CTL(pipe);
  2909. temp = I915_READ(reg);
  2910. if (IS_IVYBRIDGE(dev)) {
  2911. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2912. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2913. } else {
  2914. temp &= ~FDI_LINK_TRAIN_NONE;
  2915. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2916. }
  2917. I915_WRITE(reg, temp);
  2918. reg = FDI_RX_CTL(pipe);
  2919. temp = I915_READ(reg);
  2920. if (HAS_PCH_CPT(dev)) {
  2921. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2922. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2923. } else {
  2924. temp &= ~FDI_LINK_TRAIN_NONE;
  2925. temp |= FDI_LINK_TRAIN_NONE;
  2926. }
  2927. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2928. /* wait one idle pattern time */
  2929. POSTING_READ(reg);
  2930. udelay(1000);
  2931. /* IVB wants error correction enabled */
  2932. if (IS_IVYBRIDGE(dev))
  2933. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2934. FDI_FE_ERRC_ENABLE);
  2935. }
  2936. /* The FDI link training functions for ILK/Ibexpeak. */
  2937. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2938. {
  2939. struct drm_device *dev = crtc->dev;
  2940. struct drm_i915_private *dev_priv = dev->dev_private;
  2941. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2942. int pipe = intel_crtc->pipe;
  2943. u32 reg, temp, tries;
  2944. /* FDI needs bits from pipe first */
  2945. assert_pipe_enabled(dev_priv, pipe);
  2946. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2947. for train result */
  2948. reg = FDI_RX_IMR(pipe);
  2949. temp = I915_READ(reg);
  2950. temp &= ~FDI_RX_SYMBOL_LOCK;
  2951. temp &= ~FDI_RX_BIT_LOCK;
  2952. I915_WRITE(reg, temp);
  2953. I915_READ(reg);
  2954. udelay(150);
  2955. /* enable CPU FDI TX and PCH FDI RX */
  2956. reg = FDI_TX_CTL(pipe);
  2957. temp = I915_READ(reg);
  2958. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2959. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2960. temp &= ~FDI_LINK_TRAIN_NONE;
  2961. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2962. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2963. reg = FDI_RX_CTL(pipe);
  2964. temp = I915_READ(reg);
  2965. temp &= ~FDI_LINK_TRAIN_NONE;
  2966. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2967. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2968. POSTING_READ(reg);
  2969. udelay(150);
  2970. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2971. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2972. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2973. FDI_RX_PHASE_SYNC_POINTER_EN);
  2974. reg = FDI_RX_IIR(pipe);
  2975. for (tries = 0; tries < 5; tries++) {
  2976. temp = I915_READ(reg);
  2977. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2978. if ((temp & FDI_RX_BIT_LOCK)) {
  2979. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2980. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2981. break;
  2982. }
  2983. }
  2984. if (tries == 5)
  2985. DRM_ERROR("FDI train 1 fail!\n");
  2986. /* Train 2 */
  2987. reg = FDI_TX_CTL(pipe);
  2988. temp = I915_READ(reg);
  2989. temp &= ~FDI_LINK_TRAIN_NONE;
  2990. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2991. I915_WRITE(reg, temp);
  2992. reg = FDI_RX_CTL(pipe);
  2993. temp = I915_READ(reg);
  2994. temp &= ~FDI_LINK_TRAIN_NONE;
  2995. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2996. I915_WRITE(reg, temp);
  2997. POSTING_READ(reg);
  2998. udelay(150);
  2999. reg = FDI_RX_IIR(pipe);
  3000. for (tries = 0; tries < 5; tries++) {
  3001. temp = I915_READ(reg);
  3002. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3003. if (temp & FDI_RX_SYMBOL_LOCK) {
  3004. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3005. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3006. break;
  3007. }
  3008. }
  3009. if (tries == 5)
  3010. DRM_ERROR("FDI train 2 fail!\n");
  3011. DRM_DEBUG_KMS("FDI train done\n");
  3012. }
  3013. static const int snb_b_fdi_train_param[] = {
  3014. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3015. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3016. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3017. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3018. };
  3019. /* The FDI link training functions for SNB/Cougarpoint. */
  3020. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3021. {
  3022. struct drm_device *dev = crtc->dev;
  3023. struct drm_i915_private *dev_priv = dev->dev_private;
  3024. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3025. int pipe = intel_crtc->pipe;
  3026. u32 reg, temp, i, retry;
  3027. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3028. for train result */
  3029. reg = FDI_RX_IMR(pipe);
  3030. temp = I915_READ(reg);
  3031. temp &= ~FDI_RX_SYMBOL_LOCK;
  3032. temp &= ~FDI_RX_BIT_LOCK;
  3033. I915_WRITE(reg, temp);
  3034. POSTING_READ(reg);
  3035. udelay(150);
  3036. /* enable CPU FDI TX and PCH FDI RX */
  3037. reg = FDI_TX_CTL(pipe);
  3038. temp = I915_READ(reg);
  3039. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3040. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3041. temp &= ~FDI_LINK_TRAIN_NONE;
  3042. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3043. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3044. /* SNB-B */
  3045. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3046. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3047. I915_WRITE(FDI_RX_MISC(pipe),
  3048. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3049. reg = FDI_RX_CTL(pipe);
  3050. temp = I915_READ(reg);
  3051. if (HAS_PCH_CPT(dev)) {
  3052. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3053. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3054. } else {
  3055. temp &= ~FDI_LINK_TRAIN_NONE;
  3056. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3057. }
  3058. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3059. POSTING_READ(reg);
  3060. udelay(150);
  3061. for (i = 0; i < 4; i++) {
  3062. reg = FDI_TX_CTL(pipe);
  3063. temp = I915_READ(reg);
  3064. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3065. temp |= snb_b_fdi_train_param[i];
  3066. I915_WRITE(reg, temp);
  3067. POSTING_READ(reg);
  3068. udelay(500);
  3069. for (retry = 0; retry < 5; retry++) {
  3070. reg = FDI_RX_IIR(pipe);
  3071. temp = I915_READ(reg);
  3072. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3073. if (temp & FDI_RX_BIT_LOCK) {
  3074. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3075. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3076. break;
  3077. }
  3078. udelay(50);
  3079. }
  3080. if (retry < 5)
  3081. break;
  3082. }
  3083. if (i == 4)
  3084. DRM_ERROR("FDI train 1 fail!\n");
  3085. /* Train 2 */
  3086. reg = FDI_TX_CTL(pipe);
  3087. temp = I915_READ(reg);
  3088. temp &= ~FDI_LINK_TRAIN_NONE;
  3089. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3090. if (IS_GEN6(dev)) {
  3091. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3092. /* SNB-B */
  3093. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3094. }
  3095. I915_WRITE(reg, temp);
  3096. reg = FDI_RX_CTL(pipe);
  3097. temp = I915_READ(reg);
  3098. if (HAS_PCH_CPT(dev)) {
  3099. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3100. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3101. } else {
  3102. temp &= ~FDI_LINK_TRAIN_NONE;
  3103. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3104. }
  3105. I915_WRITE(reg, temp);
  3106. POSTING_READ(reg);
  3107. udelay(150);
  3108. for (i = 0; i < 4; i++) {
  3109. reg = FDI_TX_CTL(pipe);
  3110. temp = I915_READ(reg);
  3111. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3112. temp |= snb_b_fdi_train_param[i];
  3113. I915_WRITE(reg, temp);
  3114. POSTING_READ(reg);
  3115. udelay(500);
  3116. for (retry = 0; retry < 5; retry++) {
  3117. reg = FDI_RX_IIR(pipe);
  3118. temp = I915_READ(reg);
  3119. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3120. if (temp & FDI_RX_SYMBOL_LOCK) {
  3121. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3122. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3123. break;
  3124. }
  3125. udelay(50);
  3126. }
  3127. if (retry < 5)
  3128. break;
  3129. }
  3130. if (i == 4)
  3131. DRM_ERROR("FDI train 2 fail!\n");
  3132. DRM_DEBUG_KMS("FDI train done.\n");
  3133. }
  3134. /* Manual link training for Ivy Bridge A0 parts */
  3135. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3136. {
  3137. struct drm_device *dev = crtc->dev;
  3138. struct drm_i915_private *dev_priv = dev->dev_private;
  3139. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3140. int pipe = intel_crtc->pipe;
  3141. u32 reg, temp, i, j;
  3142. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3143. for train result */
  3144. reg = FDI_RX_IMR(pipe);
  3145. temp = I915_READ(reg);
  3146. temp &= ~FDI_RX_SYMBOL_LOCK;
  3147. temp &= ~FDI_RX_BIT_LOCK;
  3148. I915_WRITE(reg, temp);
  3149. POSTING_READ(reg);
  3150. udelay(150);
  3151. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3152. I915_READ(FDI_RX_IIR(pipe)));
  3153. /* Try each vswing and preemphasis setting twice before moving on */
  3154. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3155. /* disable first in case we need to retry */
  3156. reg = FDI_TX_CTL(pipe);
  3157. temp = I915_READ(reg);
  3158. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3159. temp &= ~FDI_TX_ENABLE;
  3160. I915_WRITE(reg, temp);
  3161. reg = FDI_RX_CTL(pipe);
  3162. temp = I915_READ(reg);
  3163. temp &= ~FDI_LINK_TRAIN_AUTO;
  3164. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3165. temp &= ~FDI_RX_ENABLE;
  3166. I915_WRITE(reg, temp);
  3167. /* enable CPU FDI TX and PCH FDI RX */
  3168. reg = FDI_TX_CTL(pipe);
  3169. temp = I915_READ(reg);
  3170. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3171. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3172. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3173. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3174. temp |= snb_b_fdi_train_param[j/2];
  3175. temp |= FDI_COMPOSITE_SYNC;
  3176. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3177. I915_WRITE(FDI_RX_MISC(pipe),
  3178. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3179. reg = FDI_RX_CTL(pipe);
  3180. temp = I915_READ(reg);
  3181. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3182. temp |= FDI_COMPOSITE_SYNC;
  3183. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3184. POSTING_READ(reg);
  3185. udelay(1); /* should be 0.5us */
  3186. for (i = 0; i < 4; i++) {
  3187. reg = FDI_RX_IIR(pipe);
  3188. temp = I915_READ(reg);
  3189. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3190. if (temp & FDI_RX_BIT_LOCK ||
  3191. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3192. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3193. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3194. i);
  3195. break;
  3196. }
  3197. udelay(1); /* should be 0.5us */
  3198. }
  3199. if (i == 4) {
  3200. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3201. continue;
  3202. }
  3203. /* Train 2 */
  3204. reg = FDI_TX_CTL(pipe);
  3205. temp = I915_READ(reg);
  3206. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3207. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3208. I915_WRITE(reg, temp);
  3209. reg = FDI_RX_CTL(pipe);
  3210. temp = I915_READ(reg);
  3211. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3212. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3213. I915_WRITE(reg, temp);
  3214. POSTING_READ(reg);
  3215. udelay(2); /* should be 1.5us */
  3216. for (i = 0; i < 4; i++) {
  3217. reg = FDI_RX_IIR(pipe);
  3218. temp = I915_READ(reg);
  3219. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3220. if (temp & FDI_RX_SYMBOL_LOCK ||
  3221. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3222. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3223. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3224. i);
  3225. goto train_done;
  3226. }
  3227. udelay(2); /* should be 1.5us */
  3228. }
  3229. if (i == 4)
  3230. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3231. }
  3232. train_done:
  3233. DRM_DEBUG_KMS("FDI train done.\n");
  3234. }
  3235. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3236. {
  3237. struct drm_device *dev = intel_crtc->base.dev;
  3238. struct drm_i915_private *dev_priv = dev->dev_private;
  3239. int pipe = intel_crtc->pipe;
  3240. u32 reg, temp;
  3241. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3242. reg = FDI_RX_CTL(pipe);
  3243. temp = I915_READ(reg);
  3244. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3245. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3246. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3247. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3248. POSTING_READ(reg);
  3249. udelay(200);
  3250. /* Switch from Rawclk to PCDclk */
  3251. temp = I915_READ(reg);
  3252. I915_WRITE(reg, temp | FDI_PCDCLK);
  3253. POSTING_READ(reg);
  3254. udelay(200);
  3255. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3256. reg = FDI_TX_CTL(pipe);
  3257. temp = I915_READ(reg);
  3258. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3259. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3260. POSTING_READ(reg);
  3261. udelay(100);
  3262. }
  3263. }
  3264. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3265. {
  3266. struct drm_device *dev = intel_crtc->base.dev;
  3267. struct drm_i915_private *dev_priv = dev->dev_private;
  3268. int pipe = intel_crtc->pipe;
  3269. u32 reg, temp;
  3270. /* Switch from PCDclk to Rawclk */
  3271. reg = FDI_RX_CTL(pipe);
  3272. temp = I915_READ(reg);
  3273. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3274. /* Disable CPU FDI TX PLL */
  3275. reg = FDI_TX_CTL(pipe);
  3276. temp = I915_READ(reg);
  3277. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3278. POSTING_READ(reg);
  3279. udelay(100);
  3280. reg = FDI_RX_CTL(pipe);
  3281. temp = I915_READ(reg);
  3282. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3283. /* Wait for the clocks to turn off. */
  3284. POSTING_READ(reg);
  3285. udelay(100);
  3286. }
  3287. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3288. {
  3289. struct drm_device *dev = crtc->dev;
  3290. struct drm_i915_private *dev_priv = dev->dev_private;
  3291. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3292. int pipe = intel_crtc->pipe;
  3293. u32 reg, temp;
  3294. /* disable CPU FDI tx and PCH FDI rx */
  3295. reg = FDI_TX_CTL(pipe);
  3296. temp = I915_READ(reg);
  3297. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3298. POSTING_READ(reg);
  3299. reg = FDI_RX_CTL(pipe);
  3300. temp = I915_READ(reg);
  3301. temp &= ~(0x7 << 16);
  3302. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3303. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3304. POSTING_READ(reg);
  3305. udelay(100);
  3306. /* Ironlake workaround, disable clock pointer after downing FDI */
  3307. if (HAS_PCH_IBX(dev))
  3308. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3309. /* still set train pattern 1 */
  3310. reg = FDI_TX_CTL(pipe);
  3311. temp = I915_READ(reg);
  3312. temp &= ~FDI_LINK_TRAIN_NONE;
  3313. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3314. I915_WRITE(reg, temp);
  3315. reg = FDI_RX_CTL(pipe);
  3316. temp = I915_READ(reg);
  3317. if (HAS_PCH_CPT(dev)) {
  3318. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3319. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3320. } else {
  3321. temp &= ~FDI_LINK_TRAIN_NONE;
  3322. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3323. }
  3324. /* BPC in FDI rx is consistent with that in PIPECONF */
  3325. temp &= ~(0x07 << 16);
  3326. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3327. I915_WRITE(reg, temp);
  3328. POSTING_READ(reg);
  3329. udelay(100);
  3330. }
  3331. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3332. {
  3333. struct intel_crtc *crtc;
  3334. /* Note that we don't need to be called with mode_config.lock here
  3335. * as our list of CRTC objects is static for the lifetime of the
  3336. * device and so cannot disappear as we iterate. Similarly, we can
  3337. * happily treat the predicates as racy, atomic checks as userspace
  3338. * cannot claim and pin a new fb without at least acquring the
  3339. * struct_mutex and so serialising with us.
  3340. */
  3341. for_each_intel_crtc(dev, crtc) {
  3342. if (atomic_read(&crtc->unpin_work_count) == 0)
  3343. continue;
  3344. if (crtc->unpin_work)
  3345. intel_wait_for_vblank(dev, crtc->pipe);
  3346. return true;
  3347. }
  3348. return false;
  3349. }
  3350. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3351. {
  3352. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3353. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3354. /* ensure that the unpin work is consistent wrt ->pending. */
  3355. smp_rmb();
  3356. intel_crtc->unpin_work = NULL;
  3357. if (work->event)
  3358. drm_send_vblank_event(intel_crtc->base.dev,
  3359. intel_crtc->pipe,
  3360. work->event);
  3361. drm_crtc_vblank_put(&intel_crtc->base);
  3362. wake_up_all(&dev_priv->pending_flip_queue);
  3363. queue_work(dev_priv->wq, &work->work);
  3364. trace_i915_flip_complete(intel_crtc->plane,
  3365. work->pending_flip_obj);
  3366. }
  3367. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3368. {
  3369. struct drm_device *dev = crtc->dev;
  3370. struct drm_i915_private *dev_priv = dev->dev_private;
  3371. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3372. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3373. !intel_crtc_has_pending_flip(crtc),
  3374. 60*HZ) == 0)) {
  3375. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3376. spin_lock_irq(&dev->event_lock);
  3377. if (intel_crtc->unpin_work) {
  3378. WARN_ONCE(1, "Removing stuck page flip\n");
  3379. page_flip_completed(intel_crtc);
  3380. }
  3381. spin_unlock_irq(&dev->event_lock);
  3382. }
  3383. if (crtc->primary->fb) {
  3384. mutex_lock(&dev->struct_mutex);
  3385. intel_finish_fb(crtc->primary->fb);
  3386. mutex_unlock(&dev->struct_mutex);
  3387. }
  3388. }
  3389. /* Program iCLKIP clock to the desired frequency */
  3390. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3391. {
  3392. struct drm_device *dev = crtc->dev;
  3393. struct drm_i915_private *dev_priv = dev->dev_private;
  3394. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3395. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3396. u32 temp;
  3397. mutex_lock(&dev_priv->dpio_lock);
  3398. /* It is necessary to ungate the pixclk gate prior to programming
  3399. * the divisors, and gate it back when it is done.
  3400. */
  3401. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3402. /* Disable SSCCTL */
  3403. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3404. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3405. SBI_SSCCTL_DISABLE,
  3406. SBI_ICLK);
  3407. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3408. if (clock == 20000) {
  3409. auxdiv = 1;
  3410. divsel = 0x41;
  3411. phaseinc = 0x20;
  3412. } else {
  3413. /* The iCLK virtual clock root frequency is in MHz,
  3414. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3415. * divisors, it is necessary to divide one by another, so we
  3416. * convert the virtual clock precision to KHz here for higher
  3417. * precision.
  3418. */
  3419. u32 iclk_virtual_root_freq = 172800 * 1000;
  3420. u32 iclk_pi_range = 64;
  3421. u32 desired_divisor, msb_divisor_value, pi_value;
  3422. desired_divisor = (iclk_virtual_root_freq / clock);
  3423. msb_divisor_value = desired_divisor / iclk_pi_range;
  3424. pi_value = desired_divisor % iclk_pi_range;
  3425. auxdiv = 0;
  3426. divsel = msb_divisor_value - 2;
  3427. phaseinc = pi_value;
  3428. }
  3429. /* This should not happen with any sane values */
  3430. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3431. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3432. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3433. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3434. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3435. clock,
  3436. auxdiv,
  3437. divsel,
  3438. phasedir,
  3439. phaseinc);
  3440. /* Program SSCDIVINTPHASE6 */
  3441. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3442. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3443. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3444. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3445. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3446. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3447. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3448. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3449. /* Program SSCAUXDIV */
  3450. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3451. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3452. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3453. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3454. /* Enable modulator and associated divider */
  3455. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3456. temp &= ~SBI_SSCCTL_DISABLE;
  3457. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3458. /* Wait for initialization time */
  3459. udelay(24);
  3460. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3461. mutex_unlock(&dev_priv->dpio_lock);
  3462. }
  3463. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3464. enum pipe pch_transcoder)
  3465. {
  3466. struct drm_device *dev = crtc->base.dev;
  3467. struct drm_i915_private *dev_priv = dev->dev_private;
  3468. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3469. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3470. I915_READ(HTOTAL(cpu_transcoder)));
  3471. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3472. I915_READ(HBLANK(cpu_transcoder)));
  3473. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3474. I915_READ(HSYNC(cpu_transcoder)));
  3475. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3476. I915_READ(VTOTAL(cpu_transcoder)));
  3477. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3478. I915_READ(VBLANK(cpu_transcoder)));
  3479. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3480. I915_READ(VSYNC(cpu_transcoder)));
  3481. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3482. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3483. }
  3484. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3485. {
  3486. struct drm_i915_private *dev_priv = dev->dev_private;
  3487. uint32_t temp;
  3488. temp = I915_READ(SOUTH_CHICKEN1);
  3489. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3490. return;
  3491. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3492. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3493. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3494. if (enable)
  3495. temp |= FDI_BC_BIFURCATION_SELECT;
  3496. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3497. I915_WRITE(SOUTH_CHICKEN1, temp);
  3498. POSTING_READ(SOUTH_CHICKEN1);
  3499. }
  3500. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3501. {
  3502. struct drm_device *dev = intel_crtc->base.dev;
  3503. switch (intel_crtc->pipe) {
  3504. case PIPE_A:
  3505. break;
  3506. case PIPE_B:
  3507. if (intel_crtc->config->fdi_lanes > 2)
  3508. cpt_set_fdi_bc_bifurcation(dev, false);
  3509. else
  3510. cpt_set_fdi_bc_bifurcation(dev, true);
  3511. break;
  3512. case PIPE_C:
  3513. cpt_set_fdi_bc_bifurcation(dev, true);
  3514. break;
  3515. default:
  3516. BUG();
  3517. }
  3518. }
  3519. /*
  3520. * Enable PCH resources required for PCH ports:
  3521. * - PCH PLLs
  3522. * - FDI training & RX/TX
  3523. * - update transcoder timings
  3524. * - DP transcoding bits
  3525. * - transcoder
  3526. */
  3527. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3528. {
  3529. struct drm_device *dev = crtc->dev;
  3530. struct drm_i915_private *dev_priv = dev->dev_private;
  3531. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3532. int pipe = intel_crtc->pipe;
  3533. u32 reg, temp;
  3534. assert_pch_transcoder_disabled(dev_priv, pipe);
  3535. if (IS_IVYBRIDGE(dev))
  3536. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3537. /* Write the TU size bits before fdi link training, so that error
  3538. * detection works. */
  3539. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3540. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3541. /* For PCH output, training FDI link */
  3542. dev_priv->display.fdi_link_train(crtc);
  3543. /* We need to program the right clock selection before writing the pixel
  3544. * mutliplier into the DPLL. */
  3545. if (HAS_PCH_CPT(dev)) {
  3546. u32 sel;
  3547. temp = I915_READ(PCH_DPLL_SEL);
  3548. temp |= TRANS_DPLL_ENABLE(pipe);
  3549. sel = TRANS_DPLLB_SEL(pipe);
  3550. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3551. temp |= sel;
  3552. else
  3553. temp &= ~sel;
  3554. I915_WRITE(PCH_DPLL_SEL, temp);
  3555. }
  3556. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3557. * transcoder, and we actually should do this to not upset any PCH
  3558. * transcoder that already use the clock when we share it.
  3559. *
  3560. * Note that enable_shared_dpll tries to do the right thing, but
  3561. * get_shared_dpll unconditionally resets the pll - we need that to have
  3562. * the right LVDS enable sequence. */
  3563. intel_enable_shared_dpll(intel_crtc);
  3564. /* set transcoder timing, panel must allow it */
  3565. assert_panel_unlocked(dev_priv, pipe);
  3566. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3567. intel_fdi_normal_train(crtc);
  3568. /* For PCH DP, enable TRANS_DP_CTL */
  3569. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3570. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3571. reg = TRANS_DP_CTL(pipe);
  3572. temp = I915_READ(reg);
  3573. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3574. TRANS_DP_SYNC_MASK |
  3575. TRANS_DP_BPC_MASK);
  3576. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3577. TRANS_DP_ENH_FRAMING);
  3578. temp |= bpc << 9; /* same format but at 11:9 */
  3579. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3580. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3581. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3582. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3583. switch (intel_trans_dp_port_sel(crtc)) {
  3584. case PCH_DP_B:
  3585. temp |= TRANS_DP_PORT_SEL_B;
  3586. break;
  3587. case PCH_DP_C:
  3588. temp |= TRANS_DP_PORT_SEL_C;
  3589. break;
  3590. case PCH_DP_D:
  3591. temp |= TRANS_DP_PORT_SEL_D;
  3592. break;
  3593. default:
  3594. BUG();
  3595. }
  3596. I915_WRITE(reg, temp);
  3597. }
  3598. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3599. }
  3600. static void lpt_pch_enable(struct drm_crtc *crtc)
  3601. {
  3602. struct drm_device *dev = crtc->dev;
  3603. struct drm_i915_private *dev_priv = dev->dev_private;
  3604. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3605. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3606. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3607. lpt_program_iclkip(crtc);
  3608. /* Set transcoder timing. */
  3609. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3610. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3611. }
  3612. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3613. {
  3614. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3615. if (pll == NULL)
  3616. return;
  3617. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3618. WARN(1, "bad %s crtc mask\n", pll->name);
  3619. return;
  3620. }
  3621. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3622. if (pll->config.crtc_mask == 0) {
  3623. WARN_ON(pll->on);
  3624. WARN_ON(pll->active);
  3625. }
  3626. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3627. }
  3628. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3629. struct intel_crtc_state *crtc_state)
  3630. {
  3631. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3632. struct intel_shared_dpll *pll;
  3633. enum intel_dpll_id i;
  3634. if (HAS_PCH_IBX(dev_priv->dev)) {
  3635. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3636. i = (enum intel_dpll_id) crtc->pipe;
  3637. pll = &dev_priv->shared_dplls[i];
  3638. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3639. crtc->base.base.id, pll->name);
  3640. WARN_ON(pll->new_config->crtc_mask);
  3641. goto found;
  3642. }
  3643. if (IS_BROXTON(dev_priv->dev)) {
  3644. /* PLL is attached to port in bxt */
  3645. struct intel_encoder *encoder;
  3646. struct intel_digital_port *intel_dig_port;
  3647. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3648. if (WARN_ON(!encoder))
  3649. return NULL;
  3650. intel_dig_port = enc_to_dig_port(&encoder->base);
  3651. /* 1:1 mapping between ports and PLLs */
  3652. i = (enum intel_dpll_id)intel_dig_port->port;
  3653. pll = &dev_priv->shared_dplls[i];
  3654. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3655. crtc->base.base.id, pll->name);
  3656. WARN_ON(pll->new_config->crtc_mask);
  3657. goto found;
  3658. }
  3659. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3660. pll = &dev_priv->shared_dplls[i];
  3661. /* Only want to check enabled timings first */
  3662. if (pll->new_config->crtc_mask == 0)
  3663. continue;
  3664. if (memcmp(&crtc_state->dpll_hw_state,
  3665. &pll->new_config->hw_state,
  3666. sizeof(pll->new_config->hw_state)) == 0) {
  3667. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3668. crtc->base.base.id, pll->name,
  3669. pll->new_config->crtc_mask,
  3670. pll->active);
  3671. goto found;
  3672. }
  3673. }
  3674. /* Ok no matching timings, maybe there's a free one? */
  3675. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3676. pll = &dev_priv->shared_dplls[i];
  3677. if (pll->new_config->crtc_mask == 0) {
  3678. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3679. crtc->base.base.id, pll->name);
  3680. goto found;
  3681. }
  3682. }
  3683. return NULL;
  3684. found:
  3685. if (pll->new_config->crtc_mask == 0)
  3686. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3687. crtc_state->shared_dpll = i;
  3688. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3689. pipe_name(crtc->pipe));
  3690. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3691. return pll;
  3692. }
  3693. /**
  3694. * intel_shared_dpll_start_config - start a new PLL staged config
  3695. * @dev_priv: DRM device
  3696. * @clear_pipes: mask of pipes that will have their PLLs freed
  3697. *
  3698. * Starts a new PLL staged config, copying the current config but
  3699. * releasing the references of pipes specified in clear_pipes.
  3700. */
  3701. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3702. unsigned clear_pipes)
  3703. {
  3704. struct intel_shared_dpll *pll;
  3705. enum intel_dpll_id i;
  3706. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3707. pll = &dev_priv->shared_dplls[i];
  3708. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3709. GFP_KERNEL);
  3710. if (!pll->new_config)
  3711. goto cleanup;
  3712. pll->new_config->crtc_mask &= ~clear_pipes;
  3713. }
  3714. return 0;
  3715. cleanup:
  3716. while (--i >= 0) {
  3717. pll = &dev_priv->shared_dplls[i];
  3718. kfree(pll->new_config);
  3719. pll->new_config = NULL;
  3720. }
  3721. return -ENOMEM;
  3722. }
  3723. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3724. {
  3725. struct intel_shared_dpll *pll;
  3726. enum intel_dpll_id i;
  3727. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3728. pll = &dev_priv->shared_dplls[i];
  3729. WARN_ON(pll->new_config == &pll->config);
  3730. pll->config = *pll->new_config;
  3731. kfree(pll->new_config);
  3732. pll->new_config = NULL;
  3733. }
  3734. }
  3735. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3736. {
  3737. struct intel_shared_dpll *pll;
  3738. enum intel_dpll_id i;
  3739. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3740. pll = &dev_priv->shared_dplls[i];
  3741. WARN_ON(pll->new_config == &pll->config);
  3742. kfree(pll->new_config);
  3743. pll->new_config = NULL;
  3744. }
  3745. }
  3746. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3747. {
  3748. struct drm_i915_private *dev_priv = dev->dev_private;
  3749. int dslreg = PIPEDSL(pipe);
  3750. u32 temp;
  3751. temp = I915_READ(dslreg);
  3752. udelay(500);
  3753. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3754. if (wait_for(I915_READ(dslreg) != temp, 5))
  3755. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3756. }
  3757. }
  3758. /**
  3759. * skl_update_scaler_users - Stages update to crtc's scaler state
  3760. * @intel_crtc: crtc
  3761. * @crtc_state: crtc_state
  3762. * @plane: plane (NULL indicates crtc is requesting update)
  3763. * @plane_state: plane's state
  3764. * @force_detach: request unconditional detachment of scaler
  3765. *
  3766. * This function updates scaler state for requested plane or crtc.
  3767. * To request scaler usage update for a plane, caller shall pass plane pointer.
  3768. * To request scaler usage update for crtc, caller shall pass plane pointer
  3769. * as NULL.
  3770. *
  3771. * Return
  3772. * 0 - scaler_usage updated successfully
  3773. * error - requested scaling cannot be supported or other error condition
  3774. */
  3775. int
  3776. skl_update_scaler_users(
  3777. struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
  3778. struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
  3779. int force_detach)
  3780. {
  3781. int need_scaling;
  3782. int idx;
  3783. int src_w, src_h, dst_w, dst_h;
  3784. int *scaler_id;
  3785. struct drm_framebuffer *fb;
  3786. struct intel_crtc_scaler_state *scaler_state;
  3787. unsigned int rotation;
  3788. if (!intel_crtc || !crtc_state)
  3789. return 0;
  3790. scaler_state = &crtc_state->scaler_state;
  3791. idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
  3792. fb = intel_plane ? plane_state->base.fb : NULL;
  3793. if (intel_plane) {
  3794. src_w = drm_rect_width(&plane_state->src) >> 16;
  3795. src_h = drm_rect_height(&plane_state->src) >> 16;
  3796. dst_w = drm_rect_width(&plane_state->dst);
  3797. dst_h = drm_rect_height(&plane_state->dst);
  3798. scaler_id = &plane_state->scaler_id;
  3799. rotation = plane_state->base.rotation;
  3800. } else {
  3801. struct drm_display_mode *adjusted_mode =
  3802. &crtc_state->base.adjusted_mode;
  3803. src_w = crtc_state->pipe_src_w;
  3804. src_h = crtc_state->pipe_src_h;
  3805. dst_w = adjusted_mode->hdisplay;
  3806. dst_h = adjusted_mode->vdisplay;
  3807. scaler_id = &scaler_state->scaler_id;
  3808. rotation = DRM_ROTATE_0;
  3809. }
  3810. need_scaling = intel_rotation_90_or_270(rotation) ?
  3811. (src_h != dst_w || src_w != dst_h):
  3812. (src_w != dst_w || src_h != dst_h);
  3813. /*
  3814. * if plane is being disabled or scaler is no more required or force detach
  3815. * - free scaler binded to this plane/crtc
  3816. * - in order to do this, update crtc->scaler_usage
  3817. *
  3818. * Here scaler state in crtc_state is set free so that
  3819. * scaler can be assigned to other user. Actual register
  3820. * update to free the scaler is done in plane/panel-fit programming.
  3821. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3822. */
  3823. if (force_detach || !need_scaling || (intel_plane &&
  3824. (!fb || !plane_state->visible))) {
  3825. if (*scaler_id >= 0) {
  3826. scaler_state->scaler_users &= ~(1 << idx);
  3827. scaler_state->scalers[*scaler_id].in_use = 0;
  3828. DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
  3829. "crtc_state = %p scaler_users = 0x%x\n",
  3830. intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
  3831. intel_plane ? intel_plane->base.base.id :
  3832. intel_crtc->base.base.id, crtc_state,
  3833. scaler_state->scaler_users);
  3834. *scaler_id = -1;
  3835. }
  3836. return 0;
  3837. }
  3838. /* range checks */
  3839. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3840. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3841. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3842. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3843. DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
  3844. "size is out of scaler range\n",
  3845. intel_plane ? "PLANE" : "CRTC",
  3846. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3847. intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
  3848. return -EINVAL;
  3849. }
  3850. /* check colorkey */
  3851. if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
  3852. DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
  3853. intel_plane->base.base.id);
  3854. return -EINVAL;
  3855. }
  3856. /* Check src format */
  3857. if (intel_plane) {
  3858. switch (fb->pixel_format) {
  3859. case DRM_FORMAT_RGB565:
  3860. case DRM_FORMAT_XBGR8888:
  3861. case DRM_FORMAT_XRGB8888:
  3862. case DRM_FORMAT_ABGR8888:
  3863. case DRM_FORMAT_ARGB8888:
  3864. case DRM_FORMAT_XRGB2101010:
  3865. case DRM_FORMAT_ARGB2101010:
  3866. case DRM_FORMAT_XBGR2101010:
  3867. case DRM_FORMAT_ABGR2101010:
  3868. case DRM_FORMAT_YUYV:
  3869. case DRM_FORMAT_YVYU:
  3870. case DRM_FORMAT_UYVY:
  3871. case DRM_FORMAT_VYUY:
  3872. break;
  3873. default:
  3874. DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
  3875. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3876. return -EINVAL;
  3877. }
  3878. }
  3879. /* mark this plane as a scaler user in crtc_state */
  3880. scaler_state->scaler_users |= (1 << idx);
  3881. DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
  3882. "crtc_state = %p scaler_users = 0x%x\n",
  3883. intel_plane ? "PLANE" : "CRTC",
  3884. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3885. src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
  3886. return 0;
  3887. }
  3888. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3889. {
  3890. struct drm_device *dev = crtc->base.dev;
  3891. struct drm_i915_private *dev_priv = dev->dev_private;
  3892. int pipe = crtc->pipe;
  3893. struct intel_crtc_scaler_state *scaler_state =
  3894. &crtc->config->scaler_state;
  3895. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3896. /* To update pfit, first update scaler state */
  3897. skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
  3898. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3899. skl_detach_scalers(crtc);
  3900. if (!enable)
  3901. return;
  3902. if (crtc->config->pch_pfit.enabled) {
  3903. int id;
  3904. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3905. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3906. return;
  3907. }
  3908. id = scaler_state->scaler_id;
  3909. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3910. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3911. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3912. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3913. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3914. }
  3915. }
  3916. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3917. {
  3918. struct drm_device *dev = crtc->base.dev;
  3919. struct drm_i915_private *dev_priv = dev->dev_private;
  3920. int pipe = crtc->pipe;
  3921. if (crtc->config->pch_pfit.enabled) {
  3922. /* Force use of hard-coded filter coefficients
  3923. * as some pre-programmed values are broken,
  3924. * e.g. x201.
  3925. */
  3926. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3927. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3928. PF_PIPE_SEL_IVB(pipe));
  3929. else
  3930. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3931. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3932. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3933. }
  3934. }
  3935. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3936. {
  3937. struct drm_device *dev = crtc->dev;
  3938. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3939. struct drm_plane *plane;
  3940. struct intel_plane *intel_plane;
  3941. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3942. intel_plane = to_intel_plane(plane);
  3943. if (intel_plane->pipe == pipe)
  3944. intel_plane_restore(&intel_plane->base);
  3945. }
  3946. }
  3947. void hsw_enable_ips(struct intel_crtc *crtc)
  3948. {
  3949. struct drm_device *dev = crtc->base.dev;
  3950. struct drm_i915_private *dev_priv = dev->dev_private;
  3951. if (!crtc->config->ips_enabled)
  3952. return;
  3953. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3954. intel_wait_for_vblank(dev, crtc->pipe);
  3955. assert_plane_enabled(dev_priv, crtc->plane);
  3956. if (IS_BROADWELL(dev)) {
  3957. mutex_lock(&dev_priv->rps.hw_lock);
  3958. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3959. mutex_unlock(&dev_priv->rps.hw_lock);
  3960. /* Quoting Art Runyan: "its not safe to expect any particular
  3961. * value in IPS_CTL bit 31 after enabling IPS through the
  3962. * mailbox." Moreover, the mailbox may return a bogus state,
  3963. * so we need to just enable it and continue on.
  3964. */
  3965. } else {
  3966. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3967. /* The bit only becomes 1 in the next vblank, so this wait here
  3968. * is essentially intel_wait_for_vblank. If we don't have this
  3969. * and don't wait for vblanks until the end of crtc_enable, then
  3970. * the HW state readout code will complain that the expected
  3971. * IPS_CTL value is not the one we read. */
  3972. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3973. DRM_ERROR("Timed out waiting for IPS enable\n");
  3974. }
  3975. }
  3976. void hsw_disable_ips(struct intel_crtc *crtc)
  3977. {
  3978. struct drm_device *dev = crtc->base.dev;
  3979. struct drm_i915_private *dev_priv = dev->dev_private;
  3980. if (!crtc->config->ips_enabled)
  3981. return;
  3982. assert_plane_enabled(dev_priv, crtc->plane);
  3983. if (IS_BROADWELL(dev)) {
  3984. mutex_lock(&dev_priv->rps.hw_lock);
  3985. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3986. mutex_unlock(&dev_priv->rps.hw_lock);
  3987. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3988. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3989. DRM_ERROR("Timed out waiting for IPS disable\n");
  3990. } else {
  3991. I915_WRITE(IPS_CTL, 0);
  3992. POSTING_READ(IPS_CTL);
  3993. }
  3994. /* We need to wait for a vblank before we can disable the plane. */
  3995. intel_wait_for_vblank(dev, crtc->pipe);
  3996. }
  3997. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3998. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3999. {
  4000. struct drm_device *dev = crtc->dev;
  4001. struct drm_i915_private *dev_priv = dev->dev_private;
  4002. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4003. enum pipe pipe = intel_crtc->pipe;
  4004. int palreg = PALETTE(pipe);
  4005. int i;
  4006. bool reenable_ips = false;
  4007. /* The clocks have to be on to load the palette. */
  4008. if (!crtc->state->enable || !intel_crtc->active)
  4009. return;
  4010. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  4011. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  4012. assert_dsi_pll_enabled(dev_priv);
  4013. else
  4014. assert_pll_enabled(dev_priv, pipe);
  4015. }
  4016. /* use legacy palette for Ironlake */
  4017. if (!HAS_GMCH_DISPLAY(dev))
  4018. palreg = LGC_PALETTE(pipe);
  4019. /* Workaround : Do not read or write the pipe palette/gamma data while
  4020. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  4021. */
  4022. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  4023. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  4024. GAMMA_MODE_MODE_SPLIT)) {
  4025. hsw_disable_ips(intel_crtc);
  4026. reenable_ips = true;
  4027. }
  4028. for (i = 0; i < 256; i++) {
  4029. I915_WRITE(palreg + 4 * i,
  4030. (intel_crtc->lut_r[i] << 16) |
  4031. (intel_crtc->lut_g[i] << 8) |
  4032. intel_crtc->lut_b[i]);
  4033. }
  4034. if (reenable_ips)
  4035. hsw_enable_ips(intel_crtc);
  4036. }
  4037. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4038. {
  4039. if (intel_crtc->overlay) {
  4040. struct drm_device *dev = intel_crtc->base.dev;
  4041. struct drm_i915_private *dev_priv = dev->dev_private;
  4042. mutex_lock(&dev->struct_mutex);
  4043. dev_priv->mm.interruptible = false;
  4044. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4045. dev_priv->mm.interruptible = true;
  4046. mutex_unlock(&dev->struct_mutex);
  4047. }
  4048. /* Let userspace switch the overlay on again. In most cases userspace
  4049. * has to recompute where to put it anyway.
  4050. */
  4051. }
  4052. /**
  4053. * intel_post_enable_primary - Perform operations after enabling primary plane
  4054. * @crtc: the CRTC whose primary plane was just enabled
  4055. *
  4056. * Performs potentially sleeping operations that must be done after the primary
  4057. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4058. * called due to an explicit primary plane update, or due to an implicit
  4059. * re-enable that is caused when a sprite plane is updated to no longer
  4060. * completely hide the primary plane.
  4061. */
  4062. static void
  4063. intel_post_enable_primary(struct drm_crtc *crtc)
  4064. {
  4065. struct drm_device *dev = crtc->dev;
  4066. struct drm_i915_private *dev_priv = dev->dev_private;
  4067. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4068. int pipe = intel_crtc->pipe;
  4069. /*
  4070. * BDW signals flip done immediately if the plane
  4071. * is disabled, even if the plane enable is already
  4072. * armed to occur at the next vblank :(
  4073. */
  4074. if (IS_BROADWELL(dev))
  4075. intel_wait_for_vblank(dev, pipe);
  4076. /*
  4077. * FIXME IPS should be fine as long as one plane is
  4078. * enabled, but in practice it seems to have problems
  4079. * when going from primary only to sprite only and vice
  4080. * versa.
  4081. */
  4082. hsw_enable_ips(intel_crtc);
  4083. mutex_lock(&dev->struct_mutex);
  4084. intel_fbc_update(dev);
  4085. mutex_unlock(&dev->struct_mutex);
  4086. /*
  4087. * Gen2 reports pipe underruns whenever all planes are disabled.
  4088. * So don't enable underrun reporting before at least some planes
  4089. * are enabled.
  4090. * FIXME: Need to fix the logic to work when we turn off all planes
  4091. * but leave the pipe running.
  4092. */
  4093. if (IS_GEN2(dev))
  4094. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4095. /* Underruns don't raise interrupts, so check manually. */
  4096. if (HAS_GMCH_DISPLAY(dev))
  4097. i9xx_check_fifo_underruns(dev_priv);
  4098. }
  4099. /**
  4100. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4101. * @crtc: the CRTC whose primary plane is to be disabled
  4102. *
  4103. * Performs potentially sleeping operations that must be done before the
  4104. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4105. * be called due to an explicit primary plane update, or due to an implicit
  4106. * disable that is caused when a sprite plane completely hides the primary
  4107. * plane.
  4108. */
  4109. static void
  4110. intel_pre_disable_primary(struct drm_crtc *crtc)
  4111. {
  4112. struct drm_device *dev = crtc->dev;
  4113. struct drm_i915_private *dev_priv = dev->dev_private;
  4114. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4115. int pipe = intel_crtc->pipe;
  4116. /*
  4117. * Gen2 reports pipe underruns whenever all planes are disabled.
  4118. * So diasble underrun reporting before all the planes get disabled.
  4119. * FIXME: Need to fix the logic to work when we turn off all planes
  4120. * but leave the pipe running.
  4121. */
  4122. if (IS_GEN2(dev))
  4123. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4124. /*
  4125. * Vblank time updates from the shadow to live plane control register
  4126. * are blocked if the memory self-refresh mode is active at that
  4127. * moment. So to make sure the plane gets truly disabled, disable
  4128. * first the self-refresh mode. The self-refresh enable bit in turn
  4129. * will be checked/applied by the HW only at the next frame start
  4130. * event which is after the vblank start event, so we need to have a
  4131. * wait-for-vblank between disabling the plane and the pipe.
  4132. */
  4133. if (HAS_GMCH_DISPLAY(dev))
  4134. intel_set_memory_cxsr(dev_priv, false);
  4135. mutex_lock(&dev->struct_mutex);
  4136. if (dev_priv->fbc.crtc == intel_crtc)
  4137. intel_fbc_disable(dev);
  4138. mutex_unlock(&dev->struct_mutex);
  4139. /*
  4140. * FIXME IPS should be fine as long as one plane is
  4141. * enabled, but in practice it seems to have problems
  4142. * when going from primary only to sprite only and vice
  4143. * versa.
  4144. */
  4145. hsw_disable_ips(intel_crtc);
  4146. }
  4147. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  4148. {
  4149. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4150. intel_enable_primary_hw_plane(crtc->primary, crtc);
  4151. intel_enable_sprite_planes(crtc);
  4152. intel_crtc_update_cursor(crtc, true);
  4153. intel_post_enable_primary(crtc);
  4154. }
  4155. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  4156. {
  4157. struct drm_device *dev = crtc->dev;
  4158. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4159. struct intel_plane *intel_plane;
  4160. int pipe = intel_crtc->pipe;
  4161. intel_crtc_wait_for_pending_flips(crtc);
  4162. intel_pre_disable_primary(crtc);
  4163. intel_crtc_dpms_overlay_disable(intel_crtc);
  4164. for_each_intel_plane(dev, intel_plane) {
  4165. if (intel_plane->pipe == pipe) {
  4166. struct drm_crtc *from = intel_plane->base.crtc;
  4167. intel_plane->disable_plane(&intel_plane->base,
  4168. from ?: crtc, true);
  4169. }
  4170. }
  4171. /*
  4172. * FIXME: Once we grow proper nuclear flip support out of this we need
  4173. * to compute the mask of flip planes precisely. For the time being
  4174. * consider this a flip to a NULL plane.
  4175. */
  4176. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4177. }
  4178. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4179. {
  4180. struct drm_device *dev = crtc->dev;
  4181. struct drm_i915_private *dev_priv = dev->dev_private;
  4182. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4183. struct intel_encoder *encoder;
  4184. int pipe = intel_crtc->pipe;
  4185. WARN_ON(!crtc->state->enable);
  4186. if (intel_crtc->active)
  4187. return;
  4188. if (intel_crtc->config->has_pch_encoder)
  4189. intel_prepare_shared_dpll(intel_crtc);
  4190. if (intel_crtc->config->has_dp_encoder)
  4191. intel_dp_set_m_n(intel_crtc, M1_N1);
  4192. intel_set_pipe_timings(intel_crtc);
  4193. if (intel_crtc->config->has_pch_encoder) {
  4194. intel_cpu_transcoder_set_m_n(intel_crtc,
  4195. &intel_crtc->config->fdi_m_n, NULL);
  4196. }
  4197. ironlake_set_pipeconf(crtc);
  4198. intel_crtc->active = true;
  4199. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4200. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4201. for_each_encoder_on_crtc(dev, crtc, encoder)
  4202. if (encoder->pre_enable)
  4203. encoder->pre_enable(encoder);
  4204. if (intel_crtc->config->has_pch_encoder) {
  4205. /* Note: FDI PLL enabling _must_ be done before we enable the
  4206. * cpu pipes, hence this is separate from all the other fdi/pch
  4207. * enabling. */
  4208. ironlake_fdi_pll_enable(intel_crtc);
  4209. } else {
  4210. assert_fdi_tx_disabled(dev_priv, pipe);
  4211. assert_fdi_rx_disabled(dev_priv, pipe);
  4212. }
  4213. ironlake_pfit_enable(intel_crtc);
  4214. /*
  4215. * On ILK+ LUT must be loaded before the pipe is running but with
  4216. * clocks enabled
  4217. */
  4218. intel_crtc_load_lut(crtc);
  4219. intel_update_watermarks(crtc);
  4220. intel_enable_pipe(intel_crtc);
  4221. if (intel_crtc->config->has_pch_encoder)
  4222. ironlake_pch_enable(crtc);
  4223. assert_vblank_disabled(crtc);
  4224. drm_crtc_vblank_on(crtc);
  4225. for_each_encoder_on_crtc(dev, crtc, encoder)
  4226. encoder->enable(encoder);
  4227. if (HAS_PCH_CPT(dev))
  4228. cpt_verify_modeset(dev, intel_crtc->pipe);
  4229. intel_crtc_enable_planes(crtc);
  4230. }
  4231. /* IPS only exists on ULT machines and is tied to pipe A. */
  4232. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4233. {
  4234. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4235. }
  4236. /*
  4237. * This implements the workaround described in the "notes" section of the mode
  4238. * set sequence documentation. When going from no pipes or single pipe to
  4239. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  4240. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  4241. */
  4242. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  4243. {
  4244. struct drm_device *dev = crtc->base.dev;
  4245. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  4246. /* We want to get the other_active_crtc only if there's only 1 other
  4247. * active crtc. */
  4248. for_each_intel_crtc(dev, crtc_it) {
  4249. if (!crtc_it->active || crtc_it == crtc)
  4250. continue;
  4251. if (other_active_crtc)
  4252. return;
  4253. other_active_crtc = crtc_it;
  4254. }
  4255. if (!other_active_crtc)
  4256. return;
  4257. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4258. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4259. }
  4260. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4261. {
  4262. struct drm_device *dev = crtc->dev;
  4263. struct drm_i915_private *dev_priv = dev->dev_private;
  4264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4265. struct intel_encoder *encoder;
  4266. int pipe = intel_crtc->pipe;
  4267. WARN_ON(!crtc->state->enable);
  4268. if (intel_crtc->active)
  4269. return;
  4270. if (intel_crtc_to_shared_dpll(intel_crtc))
  4271. intel_enable_shared_dpll(intel_crtc);
  4272. if (intel_crtc->config->has_dp_encoder)
  4273. intel_dp_set_m_n(intel_crtc, M1_N1);
  4274. intel_set_pipe_timings(intel_crtc);
  4275. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4276. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4277. intel_crtc->config->pixel_multiplier - 1);
  4278. }
  4279. if (intel_crtc->config->has_pch_encoder) {
  4280. intel_cpu_transcoder_set_m_n(intel_crtc,
  4281. &intel_crtc->config->fdi_m_n, NULL);
  4282. }
  4283. haswell_set_pipeconf(crtc);
  4284. intel_set_pipe_csc(crtc);
  4285. intel_crtc->active = true;
  4286. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4287. for_each_encoder_on_crtc(dev, crtc, encoder)
  4288. if (encoder->pre_enable)
  4289. encoder->pre_enable(encoder);
  4290. if (intel_crtc->config->has_pch_encoder) {
  4291. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4292. true);
  4293. dev_priv->display.fdi_link_train(crtc);
  4294. }
  4295. intel_ddi_enable_pipe_clock(intel_crtc);
  4296. if (INTEL_INFO(dev)->gen == 9)
  4297. skylake_pfit_update(intel_crtc, 1);
  4298. else if (INTEL_INFO(dev)->gen < 9)
  4299. ironlake_pfit_enable(intel_crtc);
  4300. else
  4301. MISSING_CASE(INTEL_INFO(dev)->gen);
  4302. /*
  4303. * On ILK+ LUT must be loaded before the pipe is running but with
  4304. * clocks enabled
  4305. */
  4306. intel_crtc_load_lut(crtc);
  4307. intel_ddi_set_pipe_settings(crtc);
  4308. intel_ddi_enable_transcoder_func(crtc);
  4309. intel_update_watermarks(crtc);
  4310. intel_enable_pipe(intel_crtc);
  4311. if (intel_crtc->config->has_pch_encoder)
  4312. lpt_pch_enable(crtc);
  4313. if (intel_crtc->config->dp_encoder_is_mst)
  4314. intel_ddi_set_vc_payload_alloc(crtc, true);
  4315. assert_vblank_disabled(crtc);
  4316. drm_crtc_vblank_on(crtc);
  4317. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4318. encoder->enable(encoder);
  4319. intel_opregion_notify_encoder(encoder, true);
  4320. }
  4321. /* If we change the relative order between pipe/planes enabling, we need
  4322. * to change the workaround. */
  4323. haswell_mode_set_planes_workaround(intel_crtc);
  4324. intel_crtc_enable_planes(crtc);
  4325. }
  4326. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4327. {
  4328. struct drm_device *dev = crtc->base.dev;
  4329. struct drm_i915_private *dev_priv = dev->dev_private;
  4330. int pipe = crtc->pipe;
  4331. /* To avoid upsetting the power well on haswell only disable the pfit if
  4332. * it's in use. The hw state code will make sure we get this right. */
  4333. if (crtc->config->pch_pfit.enabled) {
  4334. I915_WRITE(PF_CTL(pipe), 0);
  4335. I915_WRITE(PF_WIN_POS(pipe), 0);
  4336. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4337. }
  4338. }
  4339. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4340. {
  4341. struct drm_device *dev = crtc->dev;
  4342. struct drm_i915_private *dev_priv = dev->dev_private;
  4343. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4344. struct intel_encoder *encoder;
  4345. int pipe = intel_crtc->pipe;
  4346. u32 reg, temp;
  4347. if (!intel_crtc->active)
  4348. return;
  4349. intel_crtc_disable_planes(crtc);
  4350. for_each_encoder_on_crtc(dev, crtc, encoder)
  4351. encoder->disable(encoder);
  4352. drm_crtc_vblank_off(crtc);
  4353. assert_vblank_disabled(crtc);
  4354. if (intel_crtc->config->has_pch_encoder)
  4355. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4356. intel_disable_pipe(intel_crtc);
  4357. ironlake_pfit_disable(intel_crtc);
  4358. for_each_encoder_on_crtc(dev, crtc, encoder)
  4359. if (encoder->post_disable)
  4360. encoder->post_disable(encoder);
  4361. if (intel_crtc->config->has_pch_encoder) {
  4362. ironlake_fdi_disable(crtc);
  4363. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4364. if (HAS_PCH_CPT(dev)) {
  4365. /* disable TRANS_DP_CTL */
  4366. reg = TRANS_DP_CTL(pipe);
  4367. temp = I915_READ(reg);
  4368. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4369. TRANS_DP_PORT_SEL_MASK);
  4370. temp |= TRANS_DP_PORT_SEL_NONE;
  4371. I915_WRITE(reg, temp);
  4372. /* disable DPLL_SEL */
  4373. temp = I915_READ(PCH_DPLL_SEL);
  4374. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4375. I915_WRITE(PCH_DPLL_SEL, temp);
  4376. }
  4377. /* disable PCH DPLL */
  4378. intel_disable_shared_dpll(intel_crtc);
  4379. ironlake_fdi_pll_disable(intel_crtc);
  4380. }
  4381. intel_crtc->active = false;
  4382. intel_update_watermarks(crtc);
  4383. mutex_lock(&dev->struct_mutex);
  4384. intel_fbc_update(dev);
  4385. mutex_unlock(&dev->struct_mutex);
  4386. }
  4387. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4388. {
  4389. struct drm_device *dev = crtc->dev;
  4390. struct drm_i915_private *dev_priv = dev->dev_private;
  4391. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4392. struct intel_encoder *encoder;
  4393. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4394. if (!intel_crtc->active)
  4395. return;
  4396. intel_crtc_disable_planes(crtc);
  4397. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4398. intel_opregion_notify_encoder(encoder, false);
  4399. encoder->disable(encoder);
  4400. }
  4401. drm_crtc_vblank_off(crtc);
  4402. assert_vblank_disabled(crtc);
  4403. if (intel_crtc->config->has_pch_encoder)
  4404. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4405. false);
  4406. intel_disable_pipe(intel_crtc);
  4407. if (intel_crtc->config->dp_encoder_is_mst)
  4408. intel_ddi_set_vc_payload_alloc(crtc, false);
  4409. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4410. if (INTEL_INFO(dev)->gen == 9)
  4411. skylake_pfit_update(intel_crtc, 0);
  4412. else if (INTEL_INFO(dev)->gen < 9)
  4413. ironlake_pfit_disable(intel_crtc);
  4414. else
  4415. MISSING_CASE(INTEL_INFO(dev)->gen);
  4416. intel_ddi_disable_pipe_clock(intel_crtc);
  4417. if (intel_crtc->config->has_pch_encoder) {
  4418. lpt_disable_pch_transcoder(dev_priv);
  4419. intel_ddi_fdi_disable(crtc);
  4420. }
  4421. for_each_encoder_on_crtc(dev, crtc, encoder)
  4422. if (encoder->post_disable)
  4423. encoder->post_disable(encoder);
  4424. intel_crtc->active = false;
  4425. intel_update_watermarks(crtc);
  4426. mutex_lock(&dev->struct_mutex);
  4427. intel_fbc_update(dev);
  4428. mutex_unlock(&dev->struct_mutex);
  4429. if (intel_crtc_to_shared_dpll(intel_crtc))
  4430. intel_disable_shared_dpll(intel_crtc);
  4431. }
  4432. static void ironlake_crtc_off(struct drm_crtc *crtc)
  4433. {
  4434. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4435. intel_put_shared_dpll(intel_crtc);
  4436. }
  4437. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4438. {
  4439. struct drm_device *dev = crtc->base.dev;
  4440. struct drm_i915_private *dev_priv = dev->dev_private;
  4441. struct intel_crtc_state *pipe_config = crtc->config;
  4442. if (!pipe_config->gmch_pfit.control)
  4443. return;
  4444. /*
  4445. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4446. * according to register description and PRM.
  4447. */
  4448. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4449. assert_pipe_disabled(dev_priv, crtc->pipe);
  4450. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4451. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4452. /* Border color in case we don't scale up to the full screen. Black by
  4453. * default, change to something else for debugging. */
  4454. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4455. }
  4456. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4457. {
  4458. switch (port) {
  4459. case PORT_A:
  4460. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4461. case PORT_B:
  4462. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4463. case PORT_C:
  4464. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4465. case PORT_D:
  4466. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4467. default:
  4468. WARN_ON_ONCE(1);
  4469. return POWER_DOMAIN_PORT_OTHER;
  4470. }
  4471. }
  4472. #define for_each_power_domain(domain, mask) \
  4473. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4474. if ((1 << (domain)) & (mask))
  4475. enum intel_display_power_domain
  4476. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4477. {
  4478. struct drm_device *dev = intel_encoder->base.dev;
  4479. struct intel_digital_port *intel_dig_port;
  4480. switch (intel_encoder->type) {
  4481. case INTEL_OUTPUT_UNKNOWN:
  4482. /* Only DDI platforms should ever use this output type */
  4483. WARN_ON_ONCE(!HAS_DDI(dev));
  4484. case INTEL_OUTPUT_DISPLAYPORT:
  4485. case INTEL_OUTPUT_HDMI:
  4486. case INTEL_OUTPUT_EDP:
  4487. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4488. return port_to_power_domain(intel_dig_port->port);
  4489. case INTEL_OUTPUT_DP_MST:
  4490. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4491. return port_to_power_domain(intel_dig_port->port);
  4492. case INTEL_OUTPUT_ANALOG:
  4493. return POWER_DOMAIN_PORT_CRT;
  4494. case INTEL_OUTPUT_DSI:
  4495. return POWER_DOMAIN_PORT_DSI;
  4496. default:
  4497. return POWER_DOMAIN_PORT_OTHER;
  4498. }
  4499. }
  4500. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4501. {
  4502. struct drm_device *dev = crtc->dev;
  4503. struct intel_encoder *intel_encoder;
  4504. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4505. enum pipe pipe = intel_crtc->pipe;
  4506. unsigned long mask;
  4507. enum transcoder transcoder;
  4508. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4509. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4510. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4511. if (intel_crtc->config->pch_pfit.enabled ||
  4512. intel_crtc->config->pch_pfit.force_thru)
  4513. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4514. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4515. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4516. return mask;
  4517. }
  4518. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4519. {
  4520. struct drm_device *dev = state->dev;
  4521. struct drm_i915_private *dev_priv = dev->dev_private;
  4522. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4523. struct intel_crtc *crtc;
  4524. /*
  4525. * First get all needed power domains, then put all unneeded, to avoid
  4526. * any unnecessary toggling of the power wells.
  4527. */
  4528. for_each_intel_crtc(dev, crtc) {
  4529. enum intel_display_power_domain domain;
  4530. if (!crtc->base.state->enable)
  4531. continue;
  4532. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4533. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4534. intel_display_power_get(dev_priv, domain);
  4535. }
  4536. if (dev_priv->display.modeset_global_resources)
  4537. dev_priv->display.modeset_global_resources(state);
  4538. for_each_intel_crtc(dev, crtc) {
  4539. enum intel_display_power_domain domain;
  4540. for_each_power_domain(domain, crtc->enabled_power_domains)
  4541. intel_display_power_put(dev_priv, domain);
  4542. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4543. }
  4544. intel_display_set_init_power(dev_priv, false);
  4545. }
  4546. void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4547. {
  4548. struct drm_i915_private *dev_priv = dev->dev_private;
  4549. uint32_t divider;
  4550. uint32_t ratio;
  4551. uint32_t current_freq;
  4552. int ret;
  4553. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4554. switch (frequency) {
  4555. case 144000:
  4556. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4557. ratio = BXT_DE_PLL_RATIO(60);
  4558. break;
  4559. case 288000:
  4560. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4561. ratio = BXT_DE_PLL_RATIO(60);
  4562. break;
  4563. case 384000:
  4564. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4565. ratio = BXT_DE_PLL_RATIO(60);
  4566. break;
  4567. case 576000:
  4568. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4569. ratio = BXT_DE_PLL_RATIO(60);
  4570. break;
  4571. case 624000:
  4572. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4573. ratio = BXT_DE_PLL_RATIO(65);
  4574. break;
  4575. case 19200:
  4576. /*
  4577. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4578. * to suppress GCC warning.
  4579. */
  4580. ratio = 0;
  4581. divider = 0;
  4582. break;
  4583. default:
  4584. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4585. return;
  4586. }
  4587. mutex_lock(&dev_priv->rps.hw_lock);
  4588. /* Inform power controller of upcoming frequency change */
  4589. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4590. 0x80000000);
  4591. mutex_unlock(&dev_priv->rps.hw_lock);
  4592. if (ret) {
  4593. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4594. ret, frequency);
  4595. return;
  4596. }
  4597. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4598. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4599. current_freq = current_freq * 500 + 1000;
  4600. /*
  4601. * DE PLL has to be disabled when
  4602. * - setting to 19.2MHz (bypass, PLL isn't used)
  4603. * - before setting to 624MHz (PLL needs toggling)
  4604. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4605. */
  4606. if (frequency == 19200 || frequency == 624000 ||
  4607. current_freq == 624000) {
  4608. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4609. /* Timeout 200us */
  4610. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4611. 1))
  4612. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4613. }
  4614. if (frequency != 19200) {
  4615. uint32_t val;
  4616. val = I915_READ(BXT_DE_PLL_CTL);
  4617. val &= ~BXT_DE_PLL_RATIO_MASK;
  4618. val |= ratio;
  4619. I915_WRITE(BXT_DE_PLL_CTL, val);
  4620. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4621. /* Timeout 200us */
  4622. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4623. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4624. val = I915_READ(CDCLK_CTL);
  4625. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4626. val |= divider;
  4627. /*
  4628. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4629. * enable otherwise.
  4630. */
  4631. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4632. if (frequency >= 500000)
  4633. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4634. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4635. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4636. val |= (frequency - 1000) / 500;
  4637. I915_WRITE(CDCLK_CTL, val);
  4638. }
  4639. mutex_lock(&dev_priv->rps.hw_lock);
  4640. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4641. DIV_ROUND_UP(frequency, 25000));
  4642. mutex_unlock(&dev_priv->rps.hw_lock);
  4643. if (ret) {
  4644. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4645. ret, frequency);
  4646. return;
  4647. }
  4648. dev_priv->cdclk_freq = frequency;
  4649. }
  4650. void broxton_init_cdclk(struct drm_device *dev)
  4651. {
  4652. struct drm_i915_private *dev_priv = dev->dev_private;
  4653. uint32_t val;
  4654. /*
  4655. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4656. * or else the reset will hang because there is no PCH to respond.
  4657. * Move the handshake programming to initialization sequence.
  4658. * Previously was left up to BIOS.
  4659. */
  4660. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4661. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4662. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4663. /* Enable PG1 for cdclk */
  4664. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4665. /* check if cd clock is enabled */
  4666. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4667. DRM_DEBUG_KMS("Display already initialized\n");
  4668. return;
  4669. }
  4670. /*
  4671. * FIXME:
  4672. * - The initial CDCLK needs to be read from VBT.
  4673. * Need to make this change after VBT has changes for BXT.
  4674. * - check if setting the max (or any) cdclk freq is really necessary
  4675. * here, it belongs to modeset time
  4676. */
  4677. broxton_set_cdclk(dev, 624000);
  4678. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4679. POSTING_READ(DBUF_CTL);
  4680. udelay(10);
  4681. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4682. DRM_ERROR("DBuf power enable timeout!\n");
  4683. }
  4684. void broxton_uninit_cdclk(struct drm_device *dev)
  4685. {
  4686. struct drm_i915_private *dev_priv = dev->dev_private;
  4687. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4688. POSTING_READ(DBUF_CTL);
  4689. udelay(10);
  4690. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4691. DRM_ERROR("DBuf power disable timeout!\n");
  4692. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4693. broxton_set_cdclk(dev, 19200);
  4694. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4695. }
  4696. /* returns HPLL frequency in kHz */
  4697. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4698. {
  4699. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4700. /* Obtain SKU information */
  4701. mutex_lock(&dev_priv->dpio_lock);
  4702. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4703. CCK_FUSE_HPLL_FREQ_MASK;
  4704. mutex_unlock(&dev_priv->dpio_lock);
  4705. return vco_freq[hpll_freq] * 1000;
  4706. }
  4707. static void vlv_update_cdclk(struct drm_device *dev)
  4708. {
  4709. struct drm_i915_private *dev_priv = dev->dev_private;
  4710. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4711. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4712. dev_priv->cdclk_freq);
  4713. /*
  4714. * Program the gmbus_freq based on the cdclk frequency.
  4715. * BSpec erroneously claims we should aim for 4MHz, but
  4716. * in fact 1MHz is the correct frequency.
  4717. */
  4718. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4719. }
  4720. /* Adjust CDclk dividers to allow high res or save power if possible */
  4721. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4722. {
  4723. struct drm_i915_private *dev_priv = dev->dev_private;
  4724. u32 val, cmd;
  4725. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4726. != dev_priv->cdclk_freq);
  4727. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4728. cmd = 2;
  4729. else if (cdclk == 266667)
  4730. cmd = 1;
  4731. else
  4732. cmd = 0;
  4733. mutex_lock(&dev_priv->rps.hw_lock);
  4734. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4735. val &= ~DSPFREQGUAR_MASK;
  4736. val |= (cmd << DSPFREQGUAR_SHIFT);
  4737. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4738. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4739. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4740. 50)) {
  4741. DRM_ERROR("timed out waiting for CDclk change\n");
  4742. }
  4743. mutex_unlock(&dev_priv->rps.hw_lock);
  4744. if (cdclk == 400000) {
  4745. u32 divider;
  4746. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4747. mutex_lock(&dev_priv->dpio_lock);
  4748. /* adjust cdclk divider */
  4749. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4750. val &= ~DISPLAY_FREQUENCY_VALUES;
  4751. val |= divider;
  4752. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4753. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4754. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4755. 50))
  4756. DRM_ERROR("timed out waiting for CDclk change\n");
  4757. mutex_unlock(&dev_priv->dpio_lock);
  4758. }
  4759. mutex_lock(&dev_priv->dpio_lock);
  4760. /* adjust self-refresh exit latency value */
  4761. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4762. val &= ~0x7f;
  4763. /*
  4764. * For high bandwidth configs, we set a higher latency in the bunit
  4765. * so that the core display fetch happens in time to avoid underruns.
  4766. */
  4767. if (cdclk == 400000)
  4768. val |= 4500 / 250; /* 4.5 usec */
  4769. else
  4770. val |= 3000 / 250; /* 3.0 usec */
  4771. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4772. mutex_unlock(&dev_priv->dpio_lock);
  4773. vlv_update_cdclk(dev);
  4774. }
  4775. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4776. {
  4777. struct drm_i915_private *dev_priv = dev->dev_private;
  4778. u32 val, cmd;
  4779. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4780. != dev_priv->cdclk_freq);
  4781. switch (cdclk) {
  4782. case 333333:
  4783. case 320000:
  4784. case 266667:
  4785. case 200000:
  4786. break;
  4787. default:
  4788. MISSING_CASE(cdclk);
  4789. return;
  4790. }
  4791. /*
  4792. * Specs are full of misinformation, but testing on actual
  4793. * hardware has shown that we just need to write the desired
  4794. * CCK divider into the Punit register.
  4795. */
  4796. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4797. mutex_lock(&dev_priv->rps.hw_lock);
  4798. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4799. val &= ~DSPFREQGUAR_MASK_CHV;
  4800. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4801. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4802. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4803. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4804. 50)) {
  4805. DRM_ERROR("timed out waiting for CDclk change\n");
  4806. }
  4807. mutex_unlock(&dev_priv->rps.hw_lock);
  4808. vlv_update_cdclk(dev);
  4809. }
  4810. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4811. int max_pixclk)
  4812. {
  4813. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4814. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4815. /*
  4816. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4817. * 200MHz
  4818. * 267MHz
  4819. * 320/333MHz (depends on HPLL freq)
  4820. * 400MHz (VLV only)
  4821. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4822. * of the lower bin and adjust if needed.
  4823. *
  4824. * We seem to get an unstable or solid color picture at 200MHz.
  4825. * Not sure what's wrong. For now use 200MHz only when all pipes
  4826. * are off.
  4827. */
  4828. if (!IS_CHERRYVIEW(dev_priv) &&
  4829. max_pixclk > freq_320*limit/100)
  4830. return 400000;
  4831. else if (max_pixclk > 266667*limit/100)
  4832. return freq_320;
  4833. else if (max_pixclk > 0)
  4834. return 266667;
  4835. else
  4836. return 200000;
  4837. }
  4838. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4839. int max_pixclk)
  4840. {
  4841. /*
  4842. * FIXME:
  4843. * - remove the guardband, it's not needed on BXT
  4844. * - set 19.2MHz bypass frequency if there are no active pipes
  4845. */
  4846. if (max_pixclk > 576000*9/10)
  4847. return 624000;
  4848. else if (max_pixclk > 384000*9/10)
  4849. return 576000;
  4850. else if (max_pixclk > 288000*9/10)
  4851. return 384000;
  4852. else if (max_pixclk > 144000*9/10)
  4853. return 288000;
  4854. else
  4855. return 144000;
  4856. }
  4857. /* compute the max pixel clock for new configuration */
  4858. static int intel_mode_max_pixclk(struct drm_atomic_state *state)
  4859. {
  4860. struct drm_device *dev = state->dev;
  4861. struct intel_crtc *intel_crtc;
  4862. struct intel_crtc_state *crtc_state;
  4863. int max_pixclk = 0;
  4864. for_each_intel_crtc(dev, intel_crtc) {
  4865. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4866. if (IS_ERR(crtc_state))
  4867. return PTR_ERR(crtc_state);
  4868. if (!crtc_state->base.enable)
  4869. continue;
  4870. max_pixclk = max(max_pixclk,
  4871. crtc_state->base.adjusted_mode.crtc_clock);
  4872. }
  4873. return max_pixclk;
  4874. }
  4875. static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
  4876. unsigned *prepare_pipes)
  4877. {
  4878. struct drm_i915_private *dev_priv = to_i915(state->dev);
  4879. struct intel_crtc *intel_crtc;
  4880. int max_pixclk = intel_mode_max_pixclk(state);
  4881. int cdclk;
  4882. if (max_pixclk < 0)
  4883. return max_pixclk;
  4884. if (IS_VALLEYVIEW(dev_priv))
  4885. cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4886. else
  4887. cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  4888. if (cdclk == dev_priv->cdclk_freq)
  4889. return 0;
  4890. /* disable/enable all currently active pipes while we change cdclk */
  4891. for_each_intel_crtc(state->dev, intel_crtc)
  4892. if (intel_crtc->base.state->enable)
  4893. *prepare_pipes |= (1 << intel_crtc->pipe);
  4894. return 0;
  4895. }
  4896. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  4897. {
  4898. unsigned int credits, default_credits;
  4899. if (IS_CHERRYVIEW(dev_priv))
  4900. default_credits = PFI_CREDIT(12);
  4901. else
  4902. default_credits = PFI_CREDIT(8);
  4903. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  4904. /* CHV suggested value is 31 or 63 */
  4905. if (IS_CHERRYVIEW(dev_priv))
  4906. credits = PFI_CREDIT_31;
  4907. else
  4908. credits = PFI_CREDIT(15);
  4909. } else {
  4910. credits = default_credits;
  4911. }
  4912. /*
  4913. * WA - write default credits before re-programming
  4914. * FIXME: should we also set the resend bit here?
  4915. */
  4916. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4917. default_credits);
  4918. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4919. credits | PFI_CREDIT_RESEND);
  4920. /*
  4921. * FIXME is this guaranteed to clear
  4922. * immediately or should we poll for it?
  4923. */
  4924. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  4925. }
  4926. static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
  4927. {
  4928. struct drm_device *dev = state->dev;
  4929. struct drm_i915_private *dev_priv = dev->dev_private;
  4930. int max_pixclk = intel_mode_max_pixclk(state);
  4931. int req_cdclk;
  4932. /* The only reason this can fail is if we fail to add the crtc_state
  4933. * to the atomic state. But that can't happen since the call to
  4934. * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
  4935. * can't have failed otherwise the mode set would be aborted) added all
  4936. * the states already. */
  4937. if (WARN_ON(max_pixclk < 0))
  4938. return;
  4939. req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4940. if (req_cdclk != dev_priv->cdclk_freq) {
  4941. /*
  4942. * FIXME: We can end up here with all power domains off, yet
  4943. * with a CDCLK frequency other than the minimum. To account
  4944. * for this take the PIPE-A power domain, which covers the HW
  4945. * blocks needed for the following programming. This can be
  4946. * removed once it's guaranteed that we get here either with
  4947. * the minimum CDCLK set, or the required power domains
  4948. * enabled.
  4949. */
  4950. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4951. if (IS_CHERRYVIEW(dev))
  4952. cherryview_set_cdclk(dev, req_cdclk);
  4953. else
  4954. valleyview_set_cdclk(dev, req_cdclk);
  4955. vlv_program_pfi_credits(dev_priv);
  4956. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4957. }
  4958. }
  4959. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4960. {
  4961. struct drm_device *dev = crtc->dev;
  4962. struct drm_i915_private *dev_priv = to_i915(dev);
  4963. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4964. struct intel_encoder *encoder;
  4965. int pipe = intel_crtc->pipe;
  4966. bool is_dsi;
  4967. WARN_ON(!crtc->state->enable);
  4968. if (intel_crtc->active)
  4969. return;
  4970. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4971. if (!is_dsi) {
  4972. if (IS_CHERRYVIEW(dev))
  4973. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4974. else
  4975. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4976. }
  4977. if (intel_crtc->config->has_dp_encoder)
  4978. intel_dp_set_m_n(intel_crtc, M1_N1);
  4979. intel_set_pipe_timings(intel_crtc);
  4980. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4981. struct drm_i915_private *dev_priv = dev->dev_private;
  4982. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4983. I915_WRITE(CHV_CANVAS(pipe), 0);
  4984. }
  4985. i9xx_set_pipeconf(intel_crtc);
  4986. intel_crtc->active = true;
  4987. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4988. for_each_encoder_on_crtc(dev, crtc, encoder)
  4989. if (encoder->pre_pll_enable)
  4990. encoder->pre_pll_enable(encoder);
  4991. if (!is_dsi) {
  4992. if (IS_CHERRYVIEW(dev))
  4993. chv_enable_pll(intel_crtc, intel_crtc->config);
  4994. else
  4995. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4996. }
  4997. for_each_encoder_on_crtc(dev, crtc, encoder)
  4998. if (encoder->pre_enable)
  4999. encoder->pre_enable(encoder);
  5000. i9xx_pfit_enable(intel_crtc);
  5001. intel_crtc_load_lut(crtc);
  5002. intel_update_watermarks(crtc);
  5003. intel_enable_pipe(intel_crtc);
  5004. assert_vblank_disabled(crtc);
  5005. drm_crtc_vblank_on(crtc);
  5006. for_each_encoder_on_crtc(dev, crtc, encoder)
  5007. encoder->enable(encoder);
  5008. intel_crtc_enable_planes(crtc);
  5009. }
  5010. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5011. {
  5012. struct drm_device *dev = crtc->base.dev;
  5013. struct drm_i915_private *dev_priv = dev->dev_private;
  5014. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5015. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5016. }
  5017. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5018. {
  5019. struct drm_device *dev = crtc->dev;
  5020. struct drm_i915_private *dev_priv = to_i915(dev);
  5021. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5022. struct intel_encoder *encoder;
  5023. int pipe = intel_crtc->pipe;
  5024. WARN_ON(!crtc->state->enable);
  5025. if (intel_crtc->active)
  5026. return;
  5027. i9xx_set_pll_dividers(intel_crtc);
  5028. if (intel_crtc->config->has_dp_encoder)
  5029. intel_dp_set_m_n(intel_crtc, M1_N1);
  5030. intel_set_pipe_timings(intel_crtc);
  5031. i9xx_set_pipeconf(intel_crtc);
  5032. intel_crtc->active = true;
  5033. if (!IS_GEN2(dev))
  5034. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5035. for_each_encoder_on_crtc(dev, crtc, encoder)
  5036. if (encoder->pre_enable)
  5037. encoder->pre_enable(encoder);
  5038. i9xx_enable_pll(intel_crtc);
  5039. i9xx_pfit_enable(intel_crtc);
  5040. intel_crtc_load_lut(crtc);
  5041. intel_update_watermarks(crtc);
  5042. intel_enable_pipe(intel_crtc);
  5043. assert_vblank_disabled(crtc);
  5044. drm_crtc_vblank_on(crtc);
  5045. for_each_encoder_on_crtc(dev, crtc, encoder)
  5046. encoder->enable(encoder);
  5047. intel_crtc_enable_planes(crtc);
  5048. }
  5049. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5050. {
  5051. struct drm_device *dev = crtc->base.dev;
  5052. struct drm_i915_private *dev_priv = dev->dev_private;
  5053. if (!crtc->config->gmch_pfit.control)
  5054. return;
  5055. assert_pipe_disabled(dev_priv, crtc->pipe);
  5056. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5057. I915_READ(PFIT_CONTROL));
  5058. I915_WRITE(PFIT_CONTROL, 0);
  5059. }
  5060. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5061. {
  5062. struct drm_device *dev = crtc->dev;
  5063. struct drm_i915_private *dev_priv = dev->dev_private;
  5064. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5065. struct intel_encoder *encoder;
  5066. int pipe = intel_crtc->pipe;
  5067. if (!intel_crtc->active)
  5068. return;
  5069. intel_crtc_disable_planes(crtc);
  5070. /*
  5071. * On gen2 planes are double buffered but the pipe isn't, so we must
  5072. * wait for planes to fully turn off before disabling the pipe.
  5073. * We also need to wait on all gmch platforms because of the
  5074. * self-refresh mode constraint explained above.
  5075. */
  5076. intel_wait_for_vblank(dev, pipe);
  5077. for_each_encoder_on_crtc(dev, crtc, encoder)
  5078. encoder->disable(encoder);
  5079. drm_crtc_vblank_off(crtc);
  5080. assert_vblank_disabled(crtc);
  5081. intel_disable_pipe(intel_crtc);
  5082. i9xx_pfit_disable(intel_crtc);
  5083. for_each_encoder_on_crtc(dev, crtc, encoder)
  5084. if (encoder->post_disable)
  5085. encoder->post_disable(encoder);
  5086. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5087. if (IS_CHERRYVIEW(dev))
  5088. chv_disable_pll(dev_priv, pipe);
  5089. else if (IS_VALLEYVIEW(dev))
  5090. vlv_disable_pll(dev_priv, pipe);
  5091. else
  5092. i9xx_disable_pll(intel_crtc);
  5093. }
  5094. if (!IS_GEN2(dev))
  5095. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5096. intel_crtc->active = false;
  5097. intel_update_watermarks(crtc);
  5098. mutex_lock(&dev->struct_mutex);
  5099. intel_fbc_update(dev);
  5100. mutex_unlock(&dev->struct_mutex);
  5101. }
  5102. static void i9xx_crtc_off(struct drm_crtc *crtc)
  5103. {
  5104. }
  5105. /* Master function to enable/disable CRTC and corresponding power wells */
  5106. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5107. {
  5108. struct drm_device *dev = crtc->dev;
  5109. struct drm_i915_private *dev_priv = dev->dev_private;
  5110. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5111. enum intel_display_power_domain domain;
  5112. unsigned long domains;
  5113. if (enable) {
  5114. if (!intel_crtc->active) {
  5115. domains = get_crtc_power_domains(crtc);
  5116. for_each_power_domain(domain, domains)
  5117. intel_display_power_get(dev_priv, domain);
  5118. intel_crtc->enabled_power_domains = domains;
  5119. dev_priv->display.crtc_enable(crtc);
  5120. }
  5121. } else {
  5122. if (intel_crtc->active) {
  5123. dev_priv->display.crtc_disable(crtc);
  5124. domains = intel_crtc->enabled_power_domains;
  5125. for_each_power_domain(domain, domains)
  5126. intel_display_power_put(dev_priv, domain);
  5127. intel_crtc->enabled_power_domains = 0;
  5128. }
  5129. }
  5130. }
  5131. /**
  5132. * Sets the power management mode of the pipe and plane.
  5133. */
  5134. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5135. {
  5136. struct drm_device *dev = crtc->dev;
  5137. struct intel_encoder *intel_encoder;
  5138. bool enable = false;
  5139. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5140. enable |= intel_encoder->connectors_active;
  5141. intel_crtc_control(crtc, enable);
  5142. }
  5143. static void intel_crtc_disable(struct drm_crtc *crtc)
  5144. {
  5145. struct drm_device *dev = crtc->dev;
  5146. struct drm_connector *connector;
  5147. struct drm_i915_private *dev_priv = dev->dev_private;
  5148. /* crtc should still be enabled when we disable it. */
  5149. WARN_ON(!crtc->state->enable);
  5150. dev_priv->display.crtc_disable(crtc);
  5151. dev_priv->display.off(crtc);
  5152. drm_plane_helper_disable(crtc->primary);
  5153. /* Update computed state. */
  5154. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  5155. if (!connector->encoder || !connector->encoder->crtc)
  5156. continue;
  5157. if (connector->encoder->crtc != crtc)
  5158. continue;
  5159. connector->dpms = DRM_MODE_DPMS_OFF;
  5160. to_intel_encoder(connector->encoder)->connectors_active = false;
  5161. }
  5162. }
  5163. void intel_encoder_destroy(struct drm_encoder *encoder)
  5164. {
  5165. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5166. drm_encoder_cleanup(encoder);
  5167. kfree(intel_encoder);
  5168. }
  5169. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5170. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5171. * state of the entire output pipe. */
  5172. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5173. {
  5174. if (mode == DRM_MODE_DPMS_ON) {
  5175. encoder->connectors_active = true;
  5176. intel_crtc_update_dpms(encoder->base.crtc);
  5177. } else {
  5178. encoder->connectors_active = false;
  5179. intel_crtc_update_dpms(encoder->base.crtc);
  5180. }
  5181. }
  5182. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5183. * internal consistency). */
  5184. static void intel_connector_check_state(struct intel_connector *connector)
  5185. {
  5186. if (connector->get_hw_state(connector)) {
  5187. struct intel_encoder *encoder = connector->encoder;
  5188. struct drm_crtc *crtc;
  5189. bool encoder_enabled;
  5190. enum pipe pipe;
  5191. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5192. connector->base.base.id,
  5193. connector->base.name);
  5194. /* there is no real hw state for MST connectors */
  5195. if (connector->mst_port)
  5196. return;
  5197. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5198. "wrong connector dpms state\n");
  5199. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5200. "active connector not linked to encoder\n");
  5201. if (encoder) {
  5202. I915_STATE_WARN(!encoder->connectors_active,
  5203. "encoder->connectors_active not set\n");
  5204. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5205. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5206. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5207. return;
  5208. crtc = encoder->base.crtc;
  5209. I915_STATE_WARN(!crtc->state->enable,
  5210. "crtc not enabled\n");
  5211. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5212. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5213. "encoder active on the wrong pipe\n");
  5214. }
  5215. }
  5216. }
  5217. int intel_connector_init(struct intel_connector *connector)
  5218. {
  5219. struct drm_connector_state *connector_state;
  5220. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5221. if (!connector_state)
  5222. return -ENOMEM;
  5223. connector->base.state = connector_state;
  5224. return 0;
  5225. }
  5226. struct intel_connector *intel_connector_alloc(void)
  5227. {
  5228. struct intel_connector *connector;
  5229. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5230. if (!connector)
  5231. return NULL;
  5232. if (intel_connector_init(connector) < 0) {
  5233. kfree(connector);
  5234. return NULL;
  5235. }
  5236. return connector;
  5237. }
  5238. /* Even simpler default implementation, if there's really no special case to
  5239. * consider. */
  5240. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5241. {
  5242. /* All the simple cases only support two dpms states. */
  5243. if (mode != DRM_MODE_DPMS_ON)
  5244. mode = DRM_MODE_DPMS_OFF;
  5245. if (mode == connector->dpms)
  5246. return;
  5247. connector->dpms = mode;
  5248. /* Only need to change hw state when actually enabled */
  5249. if (connector->encoder)
  5250. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5251. intel_modeset_check_state(connector->dev);
  5252. }
  5253. /* Simple connector->get_hw_state implementation for encoders that support only
  5254. * one connector and no cloning and hence the encoder state determines the state
  5255. * of the connector. */
  5256. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5257. {
  5258. enum pipe pipe = 0;
  5259. struct intel_encoder *encoder = connector->encoder;
  5260. return encoder->get_hw_state(encoder, &pipe);
  5261. }
  5262. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5263. {
  5264. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5265. return crtc_state->fdi_lanes;
  5266. return 0;
  5267. }
  5268. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5269. struct intel_crtc_state *pipe_config)
  5270. {
  5271. struct drm_atomic_state *state = pipe_config->base.state;
  5272. struct intel_crtc *other_crtc;
  5273. struct intel_crtc_state *other_crtc_state;
  5274. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5275. pipe_name(pipe), pipe_config->fdi_lanes);
  5276. if (pipe_config->fdi_lanes > 4) {
  5277. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5278. pipe_name(pipe), pipe_config->fdi_lanes);
  5279. return -EINVAL;
  5280. }
  5281. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5282. if (pipe_config->fdi_lanes > 2) {
  5283. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5284. pipe_config->fdi_lanes);
  5285. return -EINVAL;
  5286. } else {
  5287. return 0;
  5288. }
  5289. }
  5290. if (INTEL_INFO(dev)->num_pipes == 2)
  5291. return 0;
  5292. /* Ivybridge 3 pipe is really complicated */
  5293. switch (pipe) {
  5294. case PIPE_A:
  5295. return 0;
  5296. case PIPE_B:
  5297. if (pipe_config->fdi_lanes <= 2)
  5298. return 0;
  5299. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5300. other_crtc_state =
  5301. intel_atomic_get_crtc_state(state, other_crtc);
  5302. if (IS_ERR(other_crtc_state))
  5303. return PTR_ERR(other_crtc_state);
  5304. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5305. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5306. pipe_name(pipe), pipe_config->fdi_lanes);
  5307. return -EINVAL;
  5308. }
  5309. return 0;
  5310. case PIPE_C:
  5311. if (pipe_config->fdi_lanes > 2) {
  5312. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5313. pipe_name(pipe), pipe_config->fdi_lanes);
  5314. return -EINVAL;
  5315. }
  5316. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5317. other_crtc_state =
  5318. intel_atomic_get_crtc_state(state, other_crtc);
  5319. if (IS_ERR(other_crtc_state))
  5320. return PTR_ERR(other_crtc_state);
  5321. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5322. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5323. return -EINVAL;
  5324. }
  5325. return 0;
  5326. default:
  5327. BUG();
  5328. }
  5329. }
  5330. #define RETRY 1
  5331. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5332. struct intel_crtc_state *pipe_config)
  5333. {
  5334. struct drm_device *dev = intel_crtc->base.dev;
  5335. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5336. int lane, link_bw, fdi_dotclock, ret;
  5337. bool needs_recompute = false;
  5338. retry:
  5339. /* FDI is a binary signal running at ~2.7GHz, encoding
  5340. * each output octet as 10 bits. The actual frequency
  5341. * is stored as a divider into a 100MHz clock, and the
  5342. * mode pixel clock is stored in units of 1KHz.
  5343. * Hence the bw of each lane in terms of the mode signal
  5344. * is:
  5345. */
  5346. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5347. fdi_dotclock = adjusted_mode->crtc_clock;
  5348. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5349. pipe_config->pipe_bpp);
  5350. pipe_config->fdi_lanes = lane;
  5351. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5352. link_bw, &pipe_config->fdi_m_n);
  5353. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5354. intel_crtc->pipe, pipe_config);
  5355. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5356. pipe_config->pipe_bpp -= 2*3;
  5357. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5358. pipe_config->pipe_bpp);
  5359. needs_recompute = true;
  5360. pipe_config->bw_constrained = true;
  5361. goto retry;
  5362. }
  5363. if (needs_recompute)
  5364. return RETRY;
  5365. return ret;
  5366. }
  5367. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5368. struct intel_crtc_state *pipe_config)
  5369. {
  5370. pipe_config->ips_enabled = i915.enable_ips &&
  5371. hsw_crtc_supports_ips(crtc) &&
  5372. pipe_config->pipe_bpp <= 24;
  5373. }
  5374. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5375. struct intel_crtc_state *pipe_config)
  5376. {
  5377. struct drm_device *dev = crtc->base.dev;
  5378. struct drm_i915_private *dev_priv = dev->dev_private;
  5379. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5380. int ret;
  5381. /* FIXME should check pixel clock limits on all platforms */
  5382. if (INTEL_INFO(dev)->gen < 4) {
  5383. int clock_limit =
  5384. dev_priv->display.get_display_clock_speed(dev);
  5385. /*
  5386. * Enable pixel doubling when the dot clock
  5387. * is > 90% of the (display) core speed.
  5388. *
  5389. * GDG double wide on either pipe,
  5390. * otherwise pipe A only.
  5391. */
  5392. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5393. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5394. clock_limit *= 2;
  5395. pipe_config->double_wide = true;
  5396. }
  5397. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5398. return -EINVAL;
  5399. }
  5400. /*
  5401. * Pipe horizontal size must be even in:
  5402. * - DVO ganged mode
  5403. * - LVDS dual channel mode
  5404. * - Double wide pipe
  5405. */
  5406. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5407. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5408. pipe_config->pipe_src_w &= ~1;
  5409. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5410. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5411. */
  5412. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5413. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5414. return -EINVAL;
  5415. if (HAS_IPS(dev))
  5416. hsw_compute_ips_config(crtc, pipe_config);
  5417. if (pipe_config->has_pch_encoder)
  5418. return ironlake_fdi_compute_config(crtc, pipe_config);
  5419. /* FIXME: remove below call once atomic mode set is place and all crtc
  5420. * related checks called from atomic_crtc_check function */
  5421. ret = 0;
  5422. DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
  5423. crtc, pipe_config->base.state);
  5424. ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
  5425. return ret;
  5426. }
  5427. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5428. {
  5429. struct drm_i915_private *dev_priv = to_i915(dev);
  5430. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5431. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5432. uint32_t linkrate;
  5433. if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
  5434. WARN(1, "LCPLL1 not enabled\n");
  5435. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5436. }
  5437. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5438. return 540000;
  5439. linkrate = (I915_READ(DPLL_CTRL1) &
  5440. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5441. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5442. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5443. /* vco 8640 */
  5444. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5445. case CDCLK_FREQ_450_432:
  5446. return 432000;
  5447. case CDCLK_FREQ_337_308:
  5448. return 308570;
  5449. case CDCLK_FREQ_675_617:
  5450. return 617140;
  5451. default:
  5452. WARN(1, "Unknown cd freq selection\n");
  5453. }
  5454. } else {
  5455. /* vco 8100 */
  5456. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5457. case CDCLK_FREQ_450_432:
  5458. return 450000;
  5459. case CDCLK_FREQ_337_308:
  5460. return 337500;
  5461. case CDCLK_FREQ_675_617:
  5462. return 675000;
  5463. default:
  5464. WARN(1, "Unknown cd freq selection\n");
  5465. }
  5466. }
  5467. /* error case, do as if DPLL0 isn't enabled */
  5468. return 24000;
  5469. }
  5470. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5471. {
  5472. struct drm_i915_private *dev_priv = dev->dev_private;
  5473. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5474. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5475. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5476. return 800000;
  5477. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5478. return 450000;
  5479. else if (freq == LCPLL_CLK_FREQ_450)
  5480. return 450000;
  5481. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5482. return 540000;
  5483. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5484. return 337500;
  5485. else
  5486. return 675000;
  5487. }
  5488. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5489. {
  5490. struct drm_i915_private *dev_priv = dev->dev_private;
  5491. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5492. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5493. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5494. return 800000;
  5495. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5496. return 450000;
  5497. else if (freq == LCPLL_CLK_FREQ_450)
  5498. return 450000;
  5499. else if (IS_HSW_ULT(dev))
  5500. return 337500;
  5501. else
  5502. return 540000;
  5503. }
  5504. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5505. {
  5506. struct drm_i915_private *dev_priv = dev->dev_private;
  5507. u32 val;
  5508. int divider;
  5509. if (dev_priv->hpll_freq == 0)
  5510. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5511. mutex_lock(&dev_priv->dpio_lock);
  5512. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5513. mutex_unlock(&dev_priv->dpio_lock);
  5514. divider = val & DISPLAY_FREQUENCY_VALUES;
  5515. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5516. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5517. "cdclk change in progress\n");
  5518. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5519. }
  5520. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5521. {
  5522. return 450000;
  5523. }
  5524. static int i945_get_display_clock_speed(struct drm_device *dev)
  5525. {
  5526. return 400000;
  5527. }
  5528. static int i915_get_display_clock_speed(struct drm_device *dev)
  5529. {
  5530. return 333333;
  5531. }
  5532. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5533. {
  5534. return 200000;
  5535. }
  5536. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5537. {
  5538. u16 gcfgc = 0;
  5539. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5540. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5541. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5542. return 266667;
  5543. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5544. return 333333;
  5545. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5546. return 444444;
  5547. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5548. return 200000;
  5549. default:
  5550. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5551. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5552. return 133333;
  5553. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5554. return 166667;
  5555. }
  5556. }
  5557. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5558. {
  5559. u16 gcfgc = 0;
  5560. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5561. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5562. return 133333;
  5563. else {
  5564. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5565. case GC_DISPLAY_CLOCK_333_MHZ:
  5566. return 333333;
  5567. default:
  5568. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5569. return 190000;
  5570. }
  5571. }
  5572. }
  5573. static int i865_get_display_clock_speed(struct drm_device *dev)
  5574. {
  5575. return 266667;
  5576. }
  5577. static int i855_get_display_clock_speed(struct drm_device *dev)
  5578. {
  5579. u16 hpllcc = 0;
  5580. /* Assume that the hardware is in the high speed state. This
  5581. * should be the default.
  5582. */
  5583. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5584. case GC_CLOCK_133_200:
  5585. case GC_CLOCK_100_200:
  5586. return 200000;
  5587. case GC_CLOCK_166_250:
  5588. return 250000;
  5589. case GC_CLOCK_100_133:
  5590. return 133333;
  5591. }
  5592. /* Shouldn't happen */
  5593. return 0;
  5594. }
  5595. static int i830_get_display_clock_speed(struct drm_device *dev)
  5596. {
  5597. return 133333;
  5598. }
  5599. static void
  5600. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5601. {
  5602. while (*num > DATA_LINK_M_N_MASK ||
  5603. *den > DATA_LINK_M_N_MASK) {
  5604. *num >>= 1;
  5605. *den >>= 1;
  5606. }
  5607. }
  5608. static void compute_m_n(unsigned int m, unsigned int n,
  5609. uint32_t *ret_m, uint32_t *ret_n)
  5610. {
  5611. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5612. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5613. intel_reduce_m_n_ratio(ret_m, ret_n);
  5614. }
  5615. void
  5616. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5617. int pixel_clock, int link_clock,
  5618. struct intel_link_m_n *m_n)
  5619. {
  5620. m_n->tu = 64;
  5621. compute_m_n(bits_per_pixel * pixel_clock,
  5622. link_clock * nlanes * 8,
  5623. &m_n->gmch_m, &m_n->gmch_n);
  5624. compute_m_n(pixel_clock, link_clock,
  5625. &m_n->link_m, &m_n->link_n);
  5626. }
  5627. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5628. {
  5629. if (i915.panel_use_ssc >= 0)
  5630. return i915.panel_use_ssc != 0;
  5631. return dev_priv->vbt.lvds_use_ssc
  5632. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5633. }
  5634. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5635. int num_connectors)
  5636. {
  5637. struct drm_device *dev = crtc_state->base.crtc->dev;
  5638. struct drm_i915_private *dev_priv = dev->dev_private;
  5639. int refclk;
  5640. WARN_ON(!crtc_state->base.state);
  5641. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5642. refclk = 100000;
  5643. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5644. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5645. refclk = dev_priv->vbt.lvds_ssc_freq;
  5646. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5647. } else if (!IS_GEN2(dev)) {
  5648. refclk = 96000;
  5649. } else {
  5650. refclk = 48000;
  5651. }
  5652. return refclk;
  5653. }
  5654. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5655. {
  5656. return (1 << dpll->n) << 16 | dpll->m2;
  5657. }
  5658. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5659. {
  5660. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5661. }
  5662. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5663. struct intel_crtc_state *crtc_state,
  5664. intel_clock_t *reduced_clock)
  5665. {
  5666. struct drm_device *dev = crtc->base.dev;
  5667. u32 fp, fp2 = 0;
  5668. if (IS_PINEVIEW(dev)) {
  5669. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5670. if (reduced_clock)
  5671. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5672. } else {
  5673. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5674. if (reduced_clock)
  5675. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5676. }
  5677. crtc_state->dpll_hw_state.fp0 = fp;
  5678. crtc->lowfreq_avail = false;
  5679. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5680. reduced_clock) {
  5681. crtc_state->dpll_hw_state.fp1 = fp2;
  5682. crtc->lowfreq_avail = true;
  5683. } else {
  5684. crtc_state->dpll_hw_state.fp1 = fp;
  5685. }
  5686. }
  5687. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5688. pipe)
  5689. {
  5690. u32 reg_val;
  5691. /*
  5692. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5693. * and set it to a reasonable value instead.
  5694. */
  5695. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5696. reg_val &= 0xffffff00;
  5697. reg_val |= 0x00000030;
  5698. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5699. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5700. reg_val &= 0x8cffffff;
  5701. reg_val = 0x8c000000;
  5702. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5703. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5704. reg_val &= 0xffffff00;
  5705. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5706. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5707. reg_val &= 0x00ffffff;
  5708. reg_val |= 0xb0000000;
  5709. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5710. }
  5711. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5712. struct intel_link_m_n *m_n)
  5713. {
  5714. struct drm_device *dev = crtc->base.dev;
  5715. struct drm_i915_private *dev_priv = dev->dev_private;
  5716. int pipe = crtc->pipe;
  5717. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5718. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5719. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5720. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5721. }
  5722. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5723. struct intel_link_m_n *m_n,
  5724. struct intel_link_m_n *m2_n2)
  5725. {
  5726. struct drm_device *dev = crtc->base.dev;
  5727. struct drm_i915_private *dev_priv = dev->dev_private;
  5728. int pipe = crtc->pipe;
  5729. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5730. if (INTEL_INFO(dev)->gen >= 5) {
  5731. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5732. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5733. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5734. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5735. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5736. * for gen < 8) and if DRRS is supported (to make sure the
  5737. * registers are not unnecessarily accessed).
  5738. */
  5739. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  5740. crtc->config->has_drrs) {
  5741. I915_WRITE(PIPE_DATA_M2(transcoder),
  5742. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5743. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5744. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5745. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5746. }
  5747. } else {
  5748. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5749. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5750. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5751. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5752. }
  5753. }
  5754. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5755. {
  5756. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5757. if (m_n == M1_N1) {
  5758. dp_m_n = &crtc->config->dp_m_n;
  5759. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5760. } else if (m_n == M2_N2) {
  5761. /*
  5762. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5763. * needs to be programmed into M1_N1.
  5764. */
  5765. dp_m_n = &crtc->config->dp_m2_n2;
  5766. } else {
  5767. DRM_ERROR("Unsupported divider value\n");
  5768. return;
  5769. }
  5770. if (crtc->config->has_pch_encoder)
  5771. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5772. else
  5773. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5774. }
  5775. static void vlv_update_pll(struct intel_crtc *crtc,
  5776. struct intel_crtc_state *pipe_config)
  5777. {
  5778. u32 dpll, dpll_md;
  5779. /*
  5780. * Enable DPIO clock input. We should never disable the reference
  5781. * clock for pipe B, since VGA hotplug / manual detection depends
  5782. * on it.
  5783. */
  5784. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  5785. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  5786. /* We should never disable this, set it here for state tracking */
  5787. if (crtc->pipe == PIPE_B)
  5788. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5789. dpll |= DPLL_VCO_ENABLE;
  5790. pipe_config->dpll_hw_state.dpll = dpll;
  5791. dpll_md = (pipe_config->pixel_multiplier - 1)
  5792. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5793. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  5794. }
  5795. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5796. const struct intel_crtc_state *pipe_config)
  5797. {
  5798. struct drm_device *dev = crtc->base.dev;
  5799. struct drm_i915_private *dev_priv = dev->dev_private;
  5800. int pipe = crtc->pipe;
  5801. u32 mdiv;
  5802. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5803. u32 coreclk, reg_val;
  5804. mutex_lock(&dev_priv->dpio_lock);
  5805. bestn = pipe_config->dpll.n;
  5806. bestm1 = pipe_config->dpll.m1;
  5807. bestm2 = pipe_config->dpll.m2;
  5808. bestp1 = pipe_config->dpll.p1;
  5809. bestp2 = pipe_config->dpll.p2;
  5810. /* See eDP HDMI DPIO driver vbios notes doc */
  5811. /* PLL B needs special handling */
  5812. if (pipe == PIPE_B)
  5813. vlv_pllb_recal_opamp(dev_priv, pipe);
  5814. /* Set up Tx target for periodic Rcomp update */
  5815. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5816. /* Disable target IRef on PLL */
  5817. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5818. reg_val &= 0x00ffffff;
  5819. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5820. /* Disable fast lock */
  5821. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5822. /* Set idtafcrecal before PLL is enabled */
  5823. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5824. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5825. mdiv |= ((bestn << DPIO_N_SHIFT));
  5826. mdiv |= (1 << DPIO_K_SHIFT);
  5827. /*
  5828. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5829. * but we don't support that).
  5830. * Note: don't use the DAC post divider as it seems unstable.
  5831. */
  5832. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5833. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5834. mdiv |= DPIO_ENABLE_CALIBRATION;
  5835. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5836. /* Set HBR and RBR LPF coefficients */
  5837. if (pipe_config->port_clock == 162000 ||
  5838. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  5839. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  5840. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5841. 0x009f0003);
  5842. else
  5843. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5844. 0x00d0000f);
  5845. if (pipe_config->has_dp_encoder) {
  5846. /* Use SSC source */
  5847. if (pipe == PIPE_A)
  5848. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5849. 0x0df40000);
  5850. else
  5851. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5852. 0x0df70000);
  5853. } else { /* HDMI or VGA */
  5854. /* Use bend source */
  5855. if (pipe == PIPE_A)
  5856. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5857. 0x0df70000);
  5858. else
  5859. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5860. 0x0df40000);
  5861. }
  5862. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5863. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5864. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5865. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5866. coreclk |= 0x01000000;
  5867. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5868. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5869. mutex_unlock(&dev_priv->dpio_lock);
  5870. }
  5871. static void chv_update_pll(struct intel_crtc *crtc,
  5872. struct intel_crtc_state *pipe_config)
  5873. {
  5874. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5875. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5876. DPLL_VCO_ENABLE;
  5877. if (crtc->pipe != PIPE_A)
  5878. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5879. pipe_config->dpll_hw_state.dpll_md =
  5880. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5881. }
  5882. static void chv_prepare_pll(struct intel_crtc *crtc,
  5883. const struct intel_crtc_state *pipe_config)
  5884. {
  5885. struct drm_device *dev = crtc->base.dev;
  5886. struct drm_i915_private *dev_priv = dev->dev_private;
  5887. int pipe = crtc->pipe;
  5888. int dpll_reg = DPLL(crtc->pipe);
  5889. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5890. u32 loopfilter, tribuf_calcntr;
  5891. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5892. u32 dpio_val;
  5893. int vco;
  5894. bestn = pipe_config->dpll.n;
  5895. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5896. bestm1 = pipe_config->dpll.m1;
  5897. bestm2 = pipe_config->dpll.m2 >> 22;
  5898. bestp1 = pipe_config->dpll.p1;
  5899. bestp2 = pipe_config->dpll.p2;
  5900. vco = pipe_config->dpll.vco;
  5901. dpio_val = 0;
  5902. loopfilter = 0;
  5903. /*
  5904. * Enable Refclk and SSC
  5905. */
  5906. I915_WRITE(dpll_reg,
  5907. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5908. mutex_lock(&dev_priv->dpio_lock);
  5909. /* p1 and p2 divider */
  5910. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5911. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5912. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5913. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5914. 1 << DPIO_CHV_K_DIV_SHIFT);
  5915. /* Feedback post-divider - m2 */
  5916. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5917. /* Feedback refclk divider - n and m1 */
  5918. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5919. DPIO_CHV_M1_DIV_BY_2 |
  5920. 1 << DPIO_CHV_N_DIV_SHIFT);
  5921. /* M2 fraction division */
  5922. if (bestm2_frac)
  5923. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5924. /* M2 fraction division enable */
  5925. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5926. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5927. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5928. if (bestm2_frac)
  5929. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5930. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5931. /* Program digital lock detect threshold */
  5932. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5933. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5934. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5935. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5936. if (!bestm2_frac)
  5937. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5938. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5939. /* Loop filter */
  5940. if (vco == 5400000) {
  5941. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5942. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5943. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5944. tribuf_calcntr = 0x9;
  5945. } else if (vco <= 6200000) {
  5946. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5947. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5948. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5949. tribuf_calcntr = 0x9;
  5950. } else if (vco <= 6480000) {
  5951. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5952. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5953. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5954. tribuf_calcntr = 0x8;
  5955. } else {
  5956. /* Not supported. Apply the same limits as in the max case */
  5957. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5958. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5959. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5960. tribuf_calcntr = 0;
  5961. }
  5962. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5963. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5964. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5965. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5966. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5967. /* AFC Recal */
  5968. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5969. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5970. DPIO_AFC_RECAL);
  5971. mutex_unlock(&dev_priv->dpio_lock);
  5972. }
  5973. /**
  5974. * vlv_force_pll_on - forcibly enable just the PLL
  5975. * @dev_priv: i915 private structure
  5976. * @pipe: pipe PLL to enable
  5977. * @dpll: PLL configuration
  5978. *
  5979. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5980. * in cases where we need the PLL enabled even when @pipe is not going to
  5981. * be enabled.
  5982. */
  5983. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5984. const struct dpll *dpll)
  5985. {
  5986. struct intel_crtc *crtc =
  5987. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5988. struct intel_crtc_state pipe_config = {
  5989. .base.crtc = &crtc->base,
  5990. .pixel_multiplier = 1,
  5991. .dpll = *dpll,
  5992. };
  5993. if (IS_CHERRYVIEW(dev)) {
  5994. chv_update_pll(crtc, &pipe_config);
  5995. chv_prepare_pll(crtc, &pipe_config);
  5996. chv_enable_pll(crtc, &pipe_config);
  5997. } else {
  5998. vlv_update_pll(crtc, &pipe_config);
  5999. vlv_prepare_pll(crtc, &pipe_config);
  6000. vlv_enable_pll(crtc, &pipe_config);
  6001. }
  6002. }
  6003. /**
  6004. * vlv_force_pll_off - forcibly disable just the PLL
  6005. * @dev_priv: i915 private structure
  6006. * @pipe: pipe PLL to disable
  6007. *
  6008. * Disable the PLL for @pipe. To be used in cases where we need
  6009. * the PLL enabled even when @pipe is not going to be enabled.
  6010. */
  6011. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6012. {
  6013. if (IS_CHERRYVIEW(dev))
  6014. chv_disable_pll(to_i915(dev), pipe);
  6015. else
  6016. vlv_disable_pll(to_i915(dev), pipe);
  6017. }
  6018. static void i9xx_update_pll(struct intel_crtc *crtc,
  6019. struct intel_crtc_state *crtc_state,
  6020. intel_clock_t *reduced_clock,
  6021. int num_connectors)
  6022. {
  6023. struct drm_device *dev = crtc->base.dev;
  6024. struct drm_i915_private *dev_priv = dev->dev_private;
  6025. u32 dpll;
  6026. bool is_sdvo;
  6027. struct dpll *clock = &crtc_state->dpll;
  6028. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6029. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6030. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6031. dpll = DPLL_VGA_MODE_DIS;
  6032. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6033. dpll |= DPLLB_MODE_LVDS;
  6034. else
  6035. dpll |= DPLLB_MODE_DAC_SERIAL;
  6036. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6037. dpll |= (crtc_state->pixel_multiplier - 1)
  6038. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6039. }
  6040. if (is_sdvo)
  6041. dpll |= DPLL_SDVO_HIGH_SPEED;
  6042. if (crtc_state->has_dp_encoder)
  6043. dpll |= DPLL_SDVO_HIGH_SPEED;
  6044. /* compute bitmask from p1 value */
  6045. if (IS_PINEVIEW(dev))
  6046. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6047. else {
  6048. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6049. if (IS_G4X(dev) && reduced_clock)
  6050. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6051. }
  6052. switch (clock->p2) {
  6053. case 5:
  6054. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6055. break;
  6056. case 7:
  6057. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6058. break;
  6059. case 10:
  6060. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6061. break;
  6062. case 14:
  6063. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6064. break;
  6065. }
  6066. if (INTEL_INFO(dev)->gen >= 4)
  6067. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6068. if (crtc_state->sdvo_tv_clock)
  6069. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6070. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6071. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6072. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6073. else
  6074. dpll |= PLL_REF_INPUT_DREFCLK;
  6075. dpll |= DPLL_VCO_ENABLE;
  6076. crtc_state->dpll_hw_state.dpll = dpll;
  6077. if (INTEL_INFO(dev)->gen >= 4) {
  6078. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6079. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6080. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6081. }
  6082. }
  6083. static void i8xx_update_pll(struct intel_crtc *crtc,
  6084. struct intel_crtc_state *crtc_state,
  6085. intel_clock_t *reduced_clock,
  6086. int num_connectors)
  6087. {
  6088. struct drm_device *dev = crtc->base.dev;
  6089. struct drm_i915_private *dev_priv = dev->dev_private;
  6090. u32 dpll;
  6091. struct dpll *clock = &crtc_state->dpll;
  6092. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6093. dpll = DPLL_VGA_MODE_DIS;
  6094. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6095. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6096. } else {
  6097. if (clock->p1 == 2)
  6098. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6099. else
  6100. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6101. if (clock->p2 == 4)
  6102. dpll |= PLL_P2_DIVIDE_BY_4;
  6103. }
  6104. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6105. dpll |= DPLL_DVO_2X_MODE;
  6106. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6107. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6108. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6109. else
  6110. dpll |= PLL_REF_INPUT_DREFCLK;
  6111. dpll |= DPLL_VCO_ENABLE;
  6112. crtc_state->dpll_hw_state.dpll = dpll;
  6113. }
  6114. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6115. {
  6116. struct drm_device *dev = intel_crtc->base.dev;
  6117. struct drm_i915_private *dev_priv = dev->dev_private;
  6118. enum pipe pipe = intel_crtc->pipe;
  6119. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6120. struct drm_display_mode *adjusted_mode =
  6121. &intel_crtc->config->base.adjusted_mode;
  6122. uint32_t crtc_vtotal, crtc_vblank_end;
  6123. int vsyncshift = 0;
  6124. /* We need to be careful not to changed the adjusted mode, for otherwise
  6125. * the hw state checker will get angry at the mismatch. */
  6126. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6127. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6128. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6129. /* the chip adds 2 halflines automatically */
  6130. crtc_vtotal -= 1;
  6131. crtc_vblank_end -= 1;
  6132. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6133. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6134. else
  6135. vsyncshift = adjusted_mode->crtc_hsync_start -
  6136. adjusted_mode->crtc_htotal / 2;
  6137. if (vsyncshift < 0)
  6138. vsyncshift += adjusted_mode->crtc_htotal;
  6139. }
  6140. if (INTEL_INFO(dev)->gen > 3)
  6141. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6142. I915_WRITE(HTOTAL(cpu_transcoder),
  6143. (adjusted_mode->crtc_hdisplay - 1) |
  6144. ((adjusted_mode->crtc_htotal - 1) << 16));
  6145. I915_WRITE(HBLANK(cpu_transcoder),
  6146. (adjusted_mode->crtc_hblank_start - 1) |
  6147. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6148. I915_WRITE(HSYNC(cpu_transcoder),
  6149. (adjusted_mode->crtc_hsync_start - 1) |
  6150. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6151. I915_WRITE(VTOTAL(cpu_transcoder),
  6152. (adjusted_mode->crtc_vdisplay - 1) |
  6153. ((crtc_vtotal - 1) << 16));
  6154. I915_WRITE(VBLANK(cpu_transcoder),
  6155. (adjusted_mode->crtc_vblank_start - 1) |
  6156. ((crtc_vblank_end - 1) << 16));
  6157. I915_WRITE(VSYNC(cpu_transcoder),
  6158. (adjusted_mode->crtc_vsync_start - 1) |
  6159. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6160. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6161. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6162. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6163. * bits. */
  6164. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6165. (pipe == PIPE_B || pipe == PIPE_C))
  6166. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6167. /* pipesrc controls the size that is scaled from, which should
  6168. * always be the user's requested size.
  6169. */
  6170. I915_WRITE(PIPESRC(pipe),
  6171. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6172. (intel_crtc->config->pipe_src_h - 1));
  6173. }
  6174. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6175. struct intel_crtc_state *pipe_config)
  6176. {
  6177. struct drm_device *dev = crtc->base.dev;
  6178. struct drm_i915_private *dev_priv = dev->dev_private;
  6179. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6180. uint32_t tmp;
  6181. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6182. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6183. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6184. tmp = I915_READ(HBLANK(cpu_transcoder));
  6185. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6186. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6187. tmp = I915_READ(HSYNC(cpu_transcoder));
  6188. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6189. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6190. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6191. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6192. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6193. tmp = I915_READ(VBLANK(cpu_transcoder));
  6194. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6195. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6196. tmp = I915_READ(VSYNC(cpu_transcoder));
  6197. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6198. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6199. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6200. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6201. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6202. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6203. }
  6204. tmp = I915_READ(PIPESRC(crtc->pipe));
  6205. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6206. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6207. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6208. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6209. }
  6210. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6211. struct intel_crtc_state *pipe_config)
  6212. {
  6213. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6214. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6215. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6216. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6217. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6218. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6219. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6220. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6221. mode->flags = pipe_config->base.adjusted_mode.flags;
  6222. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6223. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6224. }
  6225. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6226. {
  6227. struct drm_device *dev = intel_crtc->base.dev;
  6228. struct drm_i915_private *dev_priv = dev->dev_private;
  6229. uint32_t pipeconf;
  6230. pipeconf = 0;
  6231. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6232. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6233. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6234. if (intel_crtc->config->double_wide)
  6235. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6236. /* only g4x and later have fancy bpc/dither controls */
  6237. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6238. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6239. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6240. pipeconf |= PIPECONF_DITHER_EN |
  6241. PIPECONF_DITHER_TYPE_SP;
  6242. switch (intel_crtc->config->pipe_bpp) {
  6243. case 18:
  6244. pipeconf |= PIPECONF_6BPC;
  6245. break;
  6246. case 24:
  6247. pipeconf |= PIPECONF_8BPC;
  6248. break;
  6249. case 30:
  6250. pipeconf |= PIPECONF_10BPC;
  6251. break;
  6252. default:
  6253. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6254. BUG();
  6255. }
  6256. }
  6257. if (HAS_PIPE_CXSR(dev)) {
  6258. if (intel_crtc->lowfreq_avail) {
  6259. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6260. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6261. } else {
  6262. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6263. }
  6264. }
  6265. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6266. if (INTEL_INFO(dev)->gen < 4 ||
  6267. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6268. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6269. else
  6270. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6271. } else
  6272. pipeconf |= PIPECONF_PROGRESSIVE;
  6273. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6274. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6275. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6276. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6277. }
  6278. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6279. struct intel_crtc_state *crtc_state)
  6280. {
  6281. struct drm_device *dev = crtc->base.dev;
  6282. struct drm_i915_private *dev_priv = dev->dev_private;
  6283. int refclk, num_connectors = 0;
  6284. intel_clock_t clock, reduced_clock;
  6285. bool ok, has_reduced_clock = false;
  6286. bool is_lvds = false, is_dsi = false;
  6287. struct intel_encoder *encoder;
  6288. const intel_limit_t *limit;
  6289. struct drm_atomic_state *state = crtc_state->base.state;
  6290. struct drm_connector_state *connector_state;
  6291. int i;
  6292. for (i = 0; i < state->num_connector; i++) {
  6293. if (!state->connectors[i])
  6294. continue;
  6295. connector_state = state->connector_states[i];
  6296. if (connector_state->crtc != &crtc->base)
  6297. continue;
  6298. encoder = to_intel_encoder(connector_state->best_encoder);
  6299. switch (encoder->type) {
  6300. case INTEL_OUTPUT_LVDS:
  6301. is_lvds = true;
  6302. break;
  6303. case INTEL_OUTPUT_DSI:
  6304. is_dsi = true;
  6305. break;
  6306. default:
  6307. break;
  6308. }
  6309. num_connectors++;
  6310. }
  6311. if (is_dsi)
  6312. return 0;
  6313. if (!crtc_state->clock_set) {
  6314. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6315. /*
  6316. * Returns a set of divisors for the desired target clock with
  6317. * the given refclk, or FALSE. The returned values represent
  6318. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6319. * 2) / p1 / p2.
  6320. */
  6321. limit = intel_limit(crtc_state, refclk);
  6322. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6323. crtc_state->port_clock,
  6324. refclk, NULL, &clock);
  6325. if (!ok) {
  6326. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6327. return -EINVAL;
  6328. }
  6329. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6330. /*
  6331. * Ensure we match the reduced clock's P to the target
  6332. * clock. If the clocks don't match, we can't switch
  6333. * the display clock by using the FP0/FP1. In such case
  6334. * we will disable the LVDS downclock feature.
  6335. */
  6336. has_reduced_clock =
  6337. dev_priv->display.find_dpll(limit, crtc_state,
  6338. dev_priv->lvds_downclock,
  6339. refclk, &clock,
  6340. &reduced_clock);
  6341. }
  6342. /* Compat-code for transition, will disappear. */
  6343. crtc_state->dpll.n = clock.n;
  6344. crtc_state->dpll.m1 = clock.m1;
  6345. crtc_state->dpll.m2 = clock.m2;
  6346. crtc_state->dpll.p1 = clock.p1;
  6347. crtc_state->dpll.p2 = clock.p2;
  6348. }
  6349. if (IS_GEN2(dev)) {
  6350. i8xx_update_pll(crtc, crtc_state,
  6351. has_reduced_clock ? &reduced_clock : NULL,
  6352. num_connectors);
  6353. } else if (IS_CHERRYVIEW(dev)) {
  6354. chv_update_pll(crtc, crtc_state);
  6355. } else if (IS_VALLEYVIEW(dev)) {
  6356. vlv_update_pll(crtc, crtc_state);
  6357. } else {
  6358. i9xx_update_pll(crtc, crtc_state,
  6359. has_reduced_clock ? &reduced_clock : NULL,
  6360. num_connectors);
  6361. }
  6362. return 0;
  6363. }
  6364. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6365. struct intel_crtc_state *pipe_config)
  6366. {
  6367. struct drm_device *dev = crtc->base.dev;
  6368. struct drm_i915_private *dev_priv = dev->dev_private;
  6369. uint32_t tmp;
  6370. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6371. return;
  6372. tmp = I915_READ(PFIT_CONTROL);
  6373. if (!(tmp & PFIT_ENABLE))
  6374. return;
  6375. /* Check whether the pfit is attached to our pipe. */
  6376. if (INTEL_INFO(dev)->gen < 4) {
  6377. if (crtc->pipe != PIPE_B)
  6378. return;
  6379. } else {
  6380. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6381. return;
  6382. }
  6383. pipe_config->gmch_pfit.control = tmp;
  6384. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6385. if (INTEL_INFO(dev)->gen < 5)
  6386. pipe_config->gmch_pfit.lvds_border_bits =
  6387. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6388. }
  6389. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6390. struct intel_crtc_state *pipe_config)
  6391. {
  6392. struct drm_device *dev = crtc->base.dev;
  6393. struct drm_i915_private *dev_priv = dev->dev_private;
  6394. int pipe = pipe_config->cpu_transcoder;
  6395. intel_clock_t clock;
  6396. u32 mdiv;
  6397. int refclk = 100000;
  6398. /* In case of MIPI DPLL will not even be used */
  6399. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6400. return;
  6401. mutex_lock(&dev_priv->dpio_lock);
  6402. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6403. mutex_unlock(&dev_priv->dpio_lock);
  6404. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6405. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6406. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6407. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6408. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6409. vlv_clock(refclk, &clock);
  6410. /* clock.dot is the fast clock */
  6411. pipe_config->port_clock = clock.dot / 5;
  6412. }
  6413. static void
  6414. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6415. struct intel_initial_plane_config *plane_config)
  6416. {
  6417. struct drm_device *dev = crtc->base.dev;
  6418. struct drm_i915_private *dev_priv = dev->dev_private;
  6419. u32 val, base, offset;
  6420. int pipe = crtc->pipe, plane = crtc->plane;
  6421. int fourcc, pixel_format;
  6422. unsigned int aligned_height;
  6423. struct drm_framebuffer *fb;
  6424. struct intel_framebuffer *intel_fb;
  6425. val = I915_READ(DSPCNTR(plane));
  6426. if (!(val & DISPLAY_PLANE_ENABLE))
  6427. return;
  6428. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6429. if (!intel_fb) {
  6430. DRM_DEBUG_KMS("failed to alloc fb\n");
  6431. return;
  6432. }
  6433. fb = &intel_fb->base;
  6434. if (INTEL_INFO(dev)->gen >= 4) {
  6435. if (val & DISPPLANE_TILED) {
  6436. plane_config->tiling = I915_TILING_X;
  6437. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6438. }
  6439. }
  6440. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6441. fourcc = i9xx_format_to_fourcc(pixel_format);
  6442. fb->pixel_format = fourcc;
  6443. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6444. if (INTEL_INFO(dev)->gen >= 4) {
  6445. if (plane_config->tiling)
  6446. offset = I915_READ(DSPTILEOFF(plane));
  6447. else
  6448. offset = I915_READ(DSPLINOFF(plane));
  6449. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6450. } else {
  6451. base = I915_READ(DSPADDR(plane));
  6452. }
  6453. plane_config->base = base;
  6454. val = I915_READ(PIPESRC(pipe));
  6455. fb->width = ((val >> 16) & 0xfff) + 1;
  6456. fb->height = ((val >> 0) & 0xfff) + 1;
  6457. val = I915_READ(DSPSTRIDE(pipe));
  6458. fb->pitches[0] = val & 0xffffffc0;
  6459. aligned_height = intel_fb_align_height(dev, fb->height,
  6460. fb->pixel_format,
  6461. fb->modifier[0]);
  6462. plane_config->size = fb->pitches[0] * aligned_height;
  6463. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6464. pipe_name(pipe), plane, fb->width, fb->height,
  6465. fb->bits_per_pixel, base, fb->pitches[0],
  6466. plane_config->size);
  6467. plane_config->fb = intel_fb;
  6468. }
  6469. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6470. struct intel_crtc_state *pipe_config)
  6471. {
  6472. struct drm_device *dev = crtc->base.dev;
  6473. struct drm_i915_private *dev_priv = dev->dev_private;
  6474. int pipe = pipe_config->cpu_transcoder;
  6475. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6476. intel_clock_t clock;
  6477. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6478. int refclk = 100000;
  6479. mutex_lock(&dev_priv->dpio_lock);
  6480. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6481. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6482. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6483. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6484. mutex_unlock(&dev_priv->dpio_lock);
  6485. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6486. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6487. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6488. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6489. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6490. chv_clock(refclk, &clock);
  6491. /* clock.dot is the fast clock */
  6492. pipe_config->port_clock = clock.dot / 5;
  6493. }
  6494. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6495. struct intel_crtc_state *pipe_config)
  6496. {
  6497. struct drm_device *dev = crtc->base.dev;
  6498. struct drm_i915_private *dev_priv = dev->dev_private;
  6499. uint32_t tmp;
  6500. if (!intel_display_power_is_enabled(dev_priv,
  6501. POWER_DOMAIN_PIPE(crtc->pipe)))
  6502. return false;
  6503. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6504. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6505. tmp = I915_READ(PIPECONF(crtc->pipe));
  6506. if (!(tmp & PIPECONF_ENABLE))
  6507. return false;
  6508. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6509. switch (tmp & PIPECONF_BPC_MASK) {
  6510. case PIPECONF_6BPC:
  6511. pipe_config->pipe_bpp = 18;
  6512. break;
  6513. case PIPECONF_8BPC:
  6514. pipe_config->pipe_bpp = 24;
  6515. break;
  6516. case PIPECONF_10BPC:
  6517. pipe_config->pipe_bpp = 30;
  6518. break;
  6519. default:
  6520. break;
  6521. }
  6522. }
  6523. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6524. pipe_config->limited_color_range = true;
  6525. if (INTEL_INFO(dev)->gen < 4)
  6526. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6527. intel_get_pipe_timings(crtc, pipe_config);
  6528. i9xx_get_pfit_config(crtc, pipe_config);
  6529. if (INTEL_INFO(dev)->gen >= 4) {
  6530. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6531. pipe_config->pixel_multiplier =
  6532. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6533. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6534. pipe_config->dpll_hw_state.dpll_md = tmp;
  6535. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6536. tmp = I915_READ(DPLL(crtc->pipe));
  6537. pipe_config->pixel_multiplier =
  6538. ((tmp & SDVO_MULTIPLIER_MASK)
  6539. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6540. } else {
  6541. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6542. * port and will be fixed up in the encoder->get_config
  6543. * function. */
  6544. pipe_config->pixel_multiplier = 1;
  6545. }
  6546. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6547. if (!IS_VALLEYVIEW(dev)) {
  6548. /*
  6549. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6550. * on 830. Filter it out here so that we don't
  6551. * report errors due to that.
  6552. */
  6553. if (IS_I830(dev))
  6554. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6555. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6556. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6557. } else {
  6558. /* Mask out read-only status bits. */
  6559. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6560. DPLL_PORTC_READY_MASK |
  6561. DPLL_PORTB_READY_MASK);
  6562. }
  6563. if (IS_CHERRYVIEW(dev))
  6564. chv_crtc_clock_get(crtc, pipe_config);
  6565. else if (IS_VALLEYVIEW(dev))
  6566. vlv_crtc_clock_get(crtc, pipe_config);
  6567. else
  6568. i9xx_crtc_clock_get(crtc, pipe_config);
  6569. return true;
  6570. }
  6571. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6572. {
  6573. struct drm_i915_private *dev_priv = dev->dev_private;
  6574. struct intel_encoder *encoder;
  6575. u32 val, final;
  6576. bool has_lvds = false;
  6577. bool has_cpu_edp = false;
  6578. bool has_panel = false;
  6579. bool has_ck505 = false;
  6580. bool can_ssc = false;
  6581. /* We need to take the global config into account */
  6582. for_each_intel_encoder(dev, encoder) {
  6583. switch (encoder->type) {
  6584. case INTEL_OUTPUT_LVDS:
  6585. has_panel = true;
  6586. has_lvds = true;
  6587. break;
  6588. case INTEL_OUTPUT_EDP:
  6589. has_panel = true;
  6590. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6591. has_cpu_edp = true;
  6592. break;
  6593. default:
  6594. break;
  6595. }
  6596. }
  6597. if (HAS_PCH_IBX(dev)) {
  6598. has_ck505 = dev_priv->vbt.display_clock_mode;
  6599. can_ssc = has_ck505;
  6600. } else {
  6601. has_ck505 = false;
  6602. can_ssc = true;
  6603. }
  6604. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6605. has_panel, has_lvds, has_ck505);
  6606. /* Ironlake: try to setup display ref clock before DPLL
  6607. * enabling. This is only under driver's control after
  6608. * PCH B stepping, previous chipset stepping should be
  6609. * ignoring this setting.
  6610. */
  6611. val = I915_READ(PCH_DREF_CONTROL);
  6612. /* As we must carefully and slowly disable/enable each source in turn,
  6613. * compute the final state we want first and check if we need to
  6614. * make any changes at all.
  6615. */
  6616. final = val;
  6617. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6618. if (has_ck505)
  6619. final |= DREF_NONSPREAD_CK505_ENABLE;
  6620. else
  6621. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6622. final &= ~DREF_SSC_SOURCE_MASK;
  6623. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6624. final &= ~DREF_SSC1_ENABLE;
  6625. if (has_panel) {
  6626. final |= DREF_SSC_SOURCE_ENABLE;
  6627. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6628. final |= DREF_SSC1_ENABLE;
  6629. if (has_cpu_edp) {
  6630. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6631. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6632. else
  6633. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6634. } else
  6635. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6636. } else {
  6637. final |= DREF_SSC_SOURCE_DISABLE;
  6638. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6639. }
  6640. if (final == val)
  6641. return;
  6642. /* Always enable nonspread source */
  6643. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6644. if (has_ck505)
  6645. val |= DREF_NONSPREAD_CK505_ENABLE;
  6646. else
  6647. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6648. if (has_panel) {
  6649. val &= ~DREF_SSC_SOURCE_MASK;
  6650. val |= DREF_SSC_SOURCE_ENABLE;
  6651. /* SSC must be turned on before enabling the CPU output */
  6652. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6653. DRM_DEBUG_KMS("Using SSC on panel\n");
  6654. val |= DREF_SSC1_ENABLE;
  6655. } else
  6656. val &= ~DREF_SSC1_ENABLE;
  6657. /* Get SSC going before enabling the outputs */
  6658. I915_WRITE(PCH_DREF_CONTROL, val);
  6659. POSTING_READ(PCH_DREF_CONTROL);
  6660. udelay(200);
  6661. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6662. /* Enable CPU source on CPU attached eDP */
  6663. if (has_cpu_edp) {
  6664. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6665. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6666. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6667. } else
  6668. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6669. } else
  6670. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6671. I915_WRITE(PCH_DREF_CONTROL, val);
  6672. POSTING_READ(PCH_DREF_CONTROL);
  6673. udelay(200);
  6674. } else {
  6675. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6676. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6677. /* Turn off CPU output */
  6678. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6679. I915_WRITE(PCH_DREF_CONTROL, val);
  6680. POSTING_READ(PCH_DREF_CONTROL);
  6681. udelay(200);
  6682. /* Turn off the SSC source */
  6683. val &= ~DREF_SSC_SOURCE_MASK;
  6684. val |= DREF_SSC_SOURCE_DISABLE;
  6685. /* Turn off SSC1 */
  6686. val &= ~DREF_SSC1_ENABLE;
  6687. I915_WRITE(PCH_DREF_CONTROL, val);
  6688. POSTING_READ(PCH_DREF_CONTROL);
  6689. udelay(200);
  6690. }
  6691. BUG_ON(val != final);
  6692. }
  6693. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6694. {
  6695. uint32_t tmp;
  6696. tmp = I915_READ(SOUTH_CHICKEN2);
  6697. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6698. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6699. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6700. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6701. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6702. tmp = I915_READ(SOUTH_CHICKEN2);
  6703. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6704. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6705. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6706. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6707. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6708. }
  6709. /* WaMPhyProgramming:hsw */
  6710. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6711. {
  6712. uint32_t tmp;
  6713. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6714. tmp &= ~(0xFF << 24);
  6715. tmp |= (0x12 << 24);
  6716. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6717. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6718. tmp |= (1 << 11);
  6719. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6720. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6721. tmp |= (1 << 11);
  6722. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6723. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6724. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6725. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6726. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6727. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6728. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6729. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6730. tmp &= ~(7 << 13);
  6731. tmp |= (5 << 13);
  6732. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6733. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6734. tmp &= ~(7 << 13);
  6735. tmp |= (5 << 13);
  6736. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6737. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6738. tmp &= ~0xFF;
  6739. tmp |= 0x1C;
  6740. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6741. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6742. tmp &= ~0xFF;
  6743. tmp |= 0x1C;
  6744. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6745. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6746. tmp &= ~(0xFF << 16);
  6747. tmp |= (0x1C << 16);
  6748. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6749. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6750. tmp &= ~(0xFF << 16);
  6751. tmp |= (0x1C << 16);
  6752. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6753. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6754. tmp |= (1 << 27);
  6755. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6756. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6757. tmp |= (1 << 27);
  6758. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6759. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6760. tmp &= ~(0xF << 28);
  6761. tmp |= (4 << 28);
  6762. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6763. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6764. tmp &= ~(0xF << 28);
  6765. tmp |= (4 << 28);
  6766. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6767. }
  6768. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6769. * Programming" based on the parameters passed:
  6770. * - Sequence to enable CLKOUT_DP
  6771. * - Sequence to enable CLKOUT_DP without spread
  6772. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6773. */
  6774. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  6775. bool with_fdi)
  6776. {
  6777. struct drm_i915_private *dev_priv = dev->dev_private;
  6778. uint32_t reg, tmp;
  6779. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6780. with_spread = true;
  6781. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  6782. with_fdi, "LP PCH doesn't have FDI\n"))
  6783. with_fdi = false;
  6784. mutex_lock(&dev_priv->dpio_lock);
  6785. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6786. tmp &= ~SBI_SSCCTL_DISABLE;
  6787. tmp |= SBI_SSCCTL_PATHALT;
  6788. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6789. udelay(24);
  6790. if (with_spread) {
  6791. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6792. tmp &= ~SBI_SSCCTL_PATHALT;
  6793. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6794. if (with_fdi) {
  6795. lpt_reset_fdi_mphy(dev_priv);
  6796. lpt_program_fdi_mphy(dev_priv);
  6797. }
  6798. }
  6799. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6800. SBI_GEN0 : SBI_DBUFF0;
  6801. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6802. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6803. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6804. mutex_unlock(&dev_priv->dpio_lock);
  6805. }
  6806. /* Sequence to disable CLKOUT_DP */
  6807. static void lpt_disable_clkout_dp(struct drm_device *dev)
  6808. {
  6809. struct drm_i915_private *dev_priv = dev->dev_private;
  6810. uint32_t reg, tmp;
  6811. mutex_lock(&dev_priv->dpio_lock);
  6812. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6813. SBI_GEN0 : SBI_DBUFF0;
  6814. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6815. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6816. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6817. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6818. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6819. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6820. tmp |= SBI_SSCCTL_PATHALT;
  6821. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6822. udelay(32);
  6823. }
  6824. tmp |= SBI_SSCCTL_DISABLE;
  6825. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6826. }
  6827. mutex_unlock(&dev_priv->dpio_lock);
  6828. }
  6829. static void lpt_init_pch_refclk(struct drm_device *dev)
  6830. {
  6831. struct intel_encoder *encoder;
  6832. bool has_vga = false;
  6833. for_each_intel_encoder(dev, encoder) {
  6834. switch (encoder->type) {
  6835. case INTEL_OUTPUT_ANALOG:
  6836. has_vga = true;
  6837. break;
  6838. default:
  6839. break;
  6840. }
  6841. }
  6842. if (has_vga)
  6843. lpt_enable_clkout_dp(dev, true, true);
  6844. else
  6845. lpt_disable_clkout_dp(dev);
  6846. }
  6847. /*
  6848. * Initialize reference clocks when the driver loads
  6849. */
  6850. void intel_init_pch_refclk(struct drm_device *dev)
  6851. {
  6852. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  6853. ironlake_init_pch_refclk(dev);
  6854. else if (HAS_PCH_LPT(dev))
  6855. lpt_init_pch_refclk(dev);
  6856. }
  6857. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  6858. {
  6859. struct drm_device *dev = crtc_state->base.crtc->dev;
  6860. struct drm_i915_private *dev_priv = dev->dev_private;
  6861. struct drm_atomic_state *state = crtc_state->base.state;
  6862. struct drm_connector_state *connector_state;
  6863. struct intel_encoder *encoder;
  6864. int num_connectors = 0, i;
  6865. bool is_lvds = false;
  6866. for (i = 0; i < state->num_connector; i++) {
  6867. if (!state->connectors[i])
  6868. continue;
  6869. connector_state = state->connector_states[i];
  6870. if (connector_state->crtc != crtc_state->base.crtc)
  6871. continue;
  6872. encoder = to_intel_encoder(connector_state->best_encoder);
  6873. switch (encoder->type) {
  6874. case INTEL_OUTPUT_LVDS:
  6875. is_lvds = true;
  6876. break;
  6877. default:
  6878. break;
  6879. }
  6880. num_connectors++;
  6881. }
  6882. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6883. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6884. dev_priv->vbt.lvds_ssc_freq);
  6885. return dev_priv->vbt.lvds_ssc_freq;
  6886. }
  6887. return 120000;
  6888. }
  6889. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6890. {
  6891. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  6892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6893. int pipe = intel_crtc->pipe;
  6894. uint32_t val;
  6895. val = 0;
  6896. switch (intel_crtc->config->pipe_bpp) {
  6897. case 18:
  6898. val |= PIPECONF_6BPC;
  6899. break;
  6900. case 24:
  6901. val |= PIPECONF_8BPC;
  6902. break;
  6903. case 30:
  6904. val |= PIPECONF_10BPC;
  6905. break;
  6906. case 36:
  6907. val |= PIPECONF_12BPC;
  6908. break;
  6909. default:
  6910. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6911. BUG();
  6912. }
  6913. if (intel_crtc->config->dither)
  6914. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6915. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6916. val |= PIPECONF_INTERLACED_ILK;
  6917. else
  6918. val |= PIPECONF_PROGRESSIVE;
  6919. if (intel_crtc->config->limited_color_range)
  6920. val |= PIPECONF_COLOR_RANGE_SELECT;
  6921. I915_WRITE(PIPECONF(pipe), val);
  6922. POSTING_READ(PIPECONF(pipe));
  6923. }
  6924. /*
  6925. * Set up the pipe CSC unit.
  6926. *
  6927. * Currently only full range RGB to limited range RGB conversion
  6928. * is supported, but eventually this should handle various
  6929. * RGB<->YCbCr scenarios as well.
  6930. */
  6931. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6932. {
  6933. struct drm_device *dev = crtc->dev;
  6934. struct drm_i915_private *dev_priv = dev->dev_private;
  6935. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6936. int pipe = intel_crtc->pipe;
  6937. uint16_t coeff = 0x7800; /* 1.0 */
  6938. /*
  6939. * TODO: Check what kind of values actually come out of the pipe
  6940. * with these coeff/postoff values and adjust to get the best
  6941. * accuracy. Perhaps we even need to take the bpc value into
  6942. * consideration.
  6943. */
  6944. if (intel_crtc->config->limited_color_range)
  6945. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6946. /*
  6947. * GY/GU and RY/RU should be the other way around according
  6948. * to BSpec, but reality doesn't agree. Just set them up in
  6949. * a way that results in the correct picture.
  6950. */
  6951. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6952. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6953. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6954. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6955. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6956. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6957. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6958. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6959. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6960. if (INTEL_INFO(dev)->gen > 6) {
  6961. uint16_t postoff = 0;
  6962. if (intel_crtc->config->limited_color_range)
  6963. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6964. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6965. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6966. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6967. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6968. } else {
  6969. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6970. if (intel_crtc->config->limited_color_range)
  6971. mode |= CSC_BLACK_SCREEN_OFFSET;
  6972. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6973. }
  6974. }
  6975. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6976. {
  6977. struct drm_device *dev = crtc->dev;
  6978. struct drm_i915_private *dev_priv = dev->dev_private;
  6979. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6980. enum pipe pipe = intel_crtc->pipe;
  6981. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6982. uint32_t val;
  6983. val = 0;
  6984. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6985. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6986. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6987. val |= PIPECONF_INTERLACED_ILK;
  6988. else
  6989. val |= PIPECONF_PROGRESSIVE;
  6990. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6991. POSTING_READ(PIPECONF(cpu_transcoder));
  6992. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6993. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6994. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6995. val = 0;
  6996. switch (intel_crtc->config->pipe_bpp) {
  6997. case 18:
  6998. val |= PIPEMISC_DITHER_6_BPC;
  6999. break;
  7000. case 24:
  7001. val |= PIPEMISC_DITHER_8_BPC;
  7002. break;
  7003. case 30:
  7004. val |= PIPEMISC_DITHER_10_BPC;
  7005. break;
  7006. case 36:
  7007. val |= PIPEMISC_DITHER_12_BPC;
  7008. break;
  7009. default:
  7010. /* Case prevented by pipe_config_set_bpp. */
  7011. BUG();
  7012. }
  7013. if (intel_crtc->config->dither)
  7014. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7015. I915_WRITE(PIPEMISC(pipe), val);
  7016. }
  7017. }
  7018. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7019. struct intel_crtc_state *crtc_state,
  7020. intel_clock_t *clock,
  7021. bool *has_reduced_clock,
  7022. intel_clock_t *reduced_clock)
  7023. {
  7024. struct drm_device *dev = crtc->dev;
  7025. struct drm_i915_private *dev_priv = dev->dev_private;
  7026. int refclk;
  7027. const intel_limit_t *limit;
  7028. bool ret, is_lvds = false;
  7029. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7030. refclk = ironlake_get_refclk(crtc_state);
  7031. /*
  7032. * Returns a set of divisors for the desired target clock with the given
  7033. * refclk, or FALSE. The returned values represent the clock equation:
  7034. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7035. */
  7036. limit = intel_limit(crtc_state, refclk);
  7037. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7038. crtc_state->port_clock,
  7039. refclk, NULL, clock);
  7040. if (!ret)
  7041. return false;
  7042. if (is_lvds && dev_priv->lvds_downclock_avail) {
  7043. /*
  7044. * Ensure we match the reduced clock's P to the target clock.
  7045. * If the clocks don't match, we can't switch the display clock
  7046. * by using the FP0/FP1. In such case we will disable the LVDS
  7047. * downclock feature.
  7048. */
  7049. *has_reduced_clock =
  7050. dev_priv->display.find_dpll(limit, crtc_state,
  7051. dev_priv->lvds_downclock,
  7052. refclk, clock,
  7053. reduced_clock);
  7054. }
  7055. return true;
  7056. }
  7057. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7058. {
  7059. /*
  7060. * Account for spread spectrum to avoid
  7061. * oversubscribing the link. Max center spread
  7062. * is 2.5%; use 5% for safety's sake.
  7063. */
  7064. u32 bps = target_clock * bpp * 21 / 20;
  7065. return DIV_ROUND_UP(bps, link_bw * 8);
  7066. }
  7067. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7068. {
  7069. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7070. }
  7071. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7072. struct intel_crtc_state *crtc_state,
  7073. u32 *fp,
  7074. intel_clock_t *reduced_clock, u32 *fp2)
  7075. {
  7076. struct drm_crtc *crtc = &intel_crtc->base;
  7077. struct drm_device *dev = crtc->dev;
  7078. struct drm_i915_private *dev_priv = dev->dev_private;
  7079. struct drm_atomic_state *state = crtc_state->base.state;
  7080. struct drm_connector_state *connector_state;
  7081. struct intel_encoder *encoder;
  7082. uint32_t dpll;
  7083. int factor, num_connectors = 0, i;
  7084. bool is_lvds = false, is_sdvo = false;
  7085. for (i = 0; i < state->num_connector; i++) {
  7086. if (!state->connectors[i])
  7087. continue;
  7088. connector_state = state->connector_states[i];
  7089. if (connector_state->crtc != crtc_state->base.crtc)
  7090. continue;
  7091. encoder = to_intel_encoder(connector_state->best_encoder);
  7092. switch (encoder->type) {
  7093. case INTEL_OUTPUT_LVDS:
  7094. is_lvds = true;
  7095. break;
  7096. case INTEL_OUTPUT_SDVO:
  7097. case INTEL_OUTPUT_HDMI:
  7098. is_sdvo = true;
  7099. break;
  7100. default:
  7101. break;
  7102. }
  7103. num_connectors++;
  7104. }
  7105. /* Enable autotuning of the PLL clock (if permissible) */
  7106. factor = 21;
  7107. if (is_lvds) {
  7108. if ((intel_panel_use_ssc(dev_priv) &&
  7109. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7110. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7111. factor = 25;
  7112. } else if (crtc_state->sdvo_tv_clock)
  7113. factor = 20;
  7114. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7115. *fp |= FP_CB_TUNE;
  7116. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7117. *fp2 |= FP_CB_TUNE;
  7118. dpll = 0;
  7119. if (is_lvds)
  7120. dpll |= DPLLB_MODE_LVDS;
  7121. else
  7122. dpll |= DPLLB_MODE_DAC_SERIAL;
  7123. dpll |= (crtc_state->pixel_multiplier - 1)
  7124. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7125. if (is_sdvo)
  7126. dpll |= DPLL_SDVO_HIGH_SPEED;
  7127. if (crtc_state->has_dp_encoder)
  7128. dpll |= DPLL_SDVO_HIGH_SPEED;
  7129. /* compute bitmask from p1 value */
  7130. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7131. /* also FPA1 */
  7132. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7133. switch (crtc_state->dpll.p2) {
  7134. case 5:
  7135. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7136. break;
  7137. case 7:
  7138. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7139. break;
  7140. case 10:
  7141. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7142. break;
  7143. case 14:
  7144. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7145. break;
  7146. }
  7147. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7148. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7149. else
  7150. dpll |= PLL_REF_INPUT_DREFCLK;
  7151. return dpll | DPLL_VCO_ENABLE;
  7152. }
  7153. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7154. struct intel_crtc_state *crtc_state)
  7155. {
  7156. struct drm_device *dev = crtc->base.dev;
  7157. intel_clock_t clock, reduced_clock;
  7158. u32 dpll = 0, fp = 0, fp2 = 0;
  7159. bool ok, has_reduced_clock = false;
  7160. bool is_lvds = false;
  7161. struct intel_shared_dpll *pll;
  7162. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7163. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7164. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7165. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7166. &has_reduced_clock, &reduced_clock);
  7167. if (!ok && !crtc_state->clock_set) {
  7168. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7169. return -EINVAL;
  7170. }
  7171. /* Compat-code for transition, will disappear. */
  7172. if (!crtc_state->clock_set) {
  7173. crtc_state->dpll.n = clock.n;
  7174. crtc_state->dpll.m1 = clock.m1;
  7175. crtc_state->dpll.m2 = clock.m2;
  7176. crtc_state->dpll.p1 = clock.p1;
  7177. crtc_state->dpll.p2 = clock.p2;
  7178. }
  7179. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7180. if (crtc_state->has_pch_encoder) {
  7181. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7182. if (has_reduced_clock)
  7183. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7184. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7185. &fp, &reduced_clock,
  7186. has_reduced_clock ? &fp2 : NULL);
  7187. crtc_state->dpll_hw_state.dpll = dpll;
  7188. crtc_state->dpll_hw_state.fp0 = fp;
  7189. if (has_reduced_clock)
  7190. crtc_state->dpll_hw_state.fp1 = fp2;
  7191. else
  7192. crtc_state->dpll_hw_state.fp1 = fp;
  7193. pll = intel_get_shared_dpll(crtc, crtc_state);
  7194. if (pll == NULL) {
  7195. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7196. pipe_name(crtc->pipe));
  7197. return -EINVAL;
  7198. }
  7199. }
  7200. if (is_lvds && has_reduced_clock)
  7201. crtc->lowfreq_avail = true;
  7202. else
  7203. crtc->lowfreq_avail = false;
  7204. return 0;
  7205. }
  7206. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7207. struct intel_link_m_n *m_n)
  7208. {
  7209. struct drm_device *dev = crtc->base.dev;
  7210. struct drm_i915_private *dev_priv = dev->dev_private;
  7211. enum pipe pipe = crtc->pipe;
  7212. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7213. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7214. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7215. & ~TU_SIZE_MASK;
  7216. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7217. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7218. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7219. }
  7220. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7221. enum transcoder transcoder,
  7222. struct intel_link_m_n *m_n,
  7223. struct intel_link_m_n *m2_n2)
  7224. {
  7225. struct drm_device *dev = crtc->base.dev;
  7226. struct drm_i915_private *dev_priv = dev->dev_private;
  7227. enum pipe pipe = crtc->pipe;
  7228. if (INTEL_INFO(dev)->gen >= 5) {
  7229. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7230. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7231. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7232. & ~TU_SIZE_MASK;
  7233. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7234. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7235. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7236. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7237. * gen < 8) and if DRRS is supported (to make sure the
  7238. * registers are not unnecessarily read).
  7239. */
  7240. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7241. crtc->config->has_drrs) {
  7242. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7243. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7244. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7245. & ~TU_SIZE_MASK;
  7246. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7247. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7248. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7249. }
  7250. } else {
  7251. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7252. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7253. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7254. & ~TU_SIZE_MASK;
  7255. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7256. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7257. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7258. }
  7259. }
  7260. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7261. struct intel_crtc_state *pipe_config)
  7262. {
  7263. if (pipe_config->has_pch_encoder)
  7264. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7265. else
  7266. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7267. &pipe_config->dp_m_n,
  7268. &pipe_config->dp_m2_n2);
  7269. }
  7270. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7271. struct intel_crtc_state *pipe_config)
  7272. {
  7273. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7274. &pipe_config->fdi_m_n, NULL);
  7275. }
  7276. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7277. struct intel_crtc_state *pipe_config)
  7278. {
  7279. struct drm_device *dev = crtc->base.dev;
  7280. struct drm_i915_private *dev_priv = dev->dev_private;
  7281. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7282. uint32_t ps_ctrl = 0;
  7283. int id = -1;
  7284. int i;
  7285. /* find scaler attached to this pipe */
  7286. for (i = 0; i < crtc->num_scalers; i++) {
  7287. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7288. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7289. id = i;
  7290. pipe_config->pch_pfit.enabled = true;
  7291. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7292. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7293. break;
  7294. }
  7295. }
  7296. scaler_state->scaler_id = id;
  7297. if (id >= 0) {
  7298. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7299. } else {
  7300. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7301. }
  7302. }
  7303. static void
  7304. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7305. struct intel_initial_plane_config *plane_config)
  7306. {
  7307. struct drm_device *dev = crtc->base.dev;
  7308. struct drm_i915_private *dev_priv = dev->dev_private;
  7309. u32 val, base, offset, stride_mult, tiling;
  7310. int pipe = crtc->pipe;
  7311. int fourcc, pixel_format;
  7312. unsigned int aligned_height;
  7313. struct drm_framebuffer *fb;
  7314. struct intel_framebuffer *intel_fb;
  7315. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7316. if (!intel_fb) {
  7317. DRM_DEBUG_KMS("failed to alloc fb\n");
  7318. return;
  7319. }
  7320. fb = &intel_fb->base;
  7321. val = I915_READ(PLANE_CTL(pipe, 0));
  7322. if (!(val & PLANE_CTL_ENABLE))
  7323. goto error;
  7324. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7325. fourcc = skl_format_to_fourcc(pixel_format,
  7326. val & PLANE_CTL_ORDER_RGBX,
  7327. val & PLANE_CTL_ALPHA_MASK);
  7328. fb->pixel_format = fourcc;
  7329. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7330. tiling = val & PLANE_CTL_TILED_MASK;
  7331. switch (tiling) {
  7332. case PLANE_CTL_TILED_LINEAR:
  7333. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7334. break;
  7335. case PLANE_CTL_TILED_X:
  7336. plane_config->tiling = I915_TILING_X;
  7337. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7338. break;
  7339. case PLANE_CTL_TILED_Y:
  7340. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7341. break;
  7342. case PLANE_CTL_TILED_YF:
  7343. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7344. break;
  7345. default:
  7346. MISSING_CASE(tiling);
  7347. goto error;
  7348. }
  7349. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7350. plane_config->base = base;
  7351. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7352. val = I915_READ(PLANE_SIZE(pipe, 0));
  7353. fb->height = ((val >> 16) & 0xfff) + 1;
  7354. fb->width = ((val >> 0) & 0x1fff) + 1;
  7355. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7356. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7357. fb->pixel_format);
  7358. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7359. aligned_height = intel_fb_align_height(dev, fb->height,
  7360. fb->pixel_format,
  7361. fb->modifier[0]);
  7362. plane_config->size = fb->pitches[0] * aligned_height;
  7363. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7364. pipe_name(pipe), fb->width, fb->height,
  7365. fb->bits_per_pixel, base, fb->pitches[0],
  7366. plane_config->size);
  7367. plane_config->fb = intel_fb;
  7368. return;
  7369. error:
  7370. kfree(fb);
  7371. }
  7372. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7373. struct intel_crtc_state *pipe_config)
  7374. {
  7375. struct drm_device *dev = crtc->base.dev;
  7376. struct drm_i915_private *dev_priv = dev->dev_private;
  7377. uint32_t tmp;
  7378. tmp = I915_READ(PF_CTL(crtc->pipe));
  7379. if (tmp & PF_ENABLE) {
  7380. pipe_config->pch_pfit.enabled = true;
  7381. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7382. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7383. /* We currently do not free assignements of panel fitters on
  7384. * ivb/hsw (since we don't use the higher upscaling modes which
  7385. * differentiates them) so just WARN about this case for now. */
  7386. if (IS_GEN7(dev)) {
  7387. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7388. PF_PIPE_SEL_IVB(crtc->pipe));
  7389. }
  7390. }
  7391. }
  7392. static void
  7393. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7394. struct intel_initial_plane_config *plane_config)
  7395. {
  7396. struct drm_device *dev = crtc->base.dev;
  7397. struct drm_i915_private *dev_priv = dev->dev_private;
  7398. u32 val, base, offset;
  7399. int pipe = crtc->pipe;
  7400. int fourcc, pixel_format;
  7401. unsigned int aligned_height;
  7402. struct drm_framebuffer *fb;
  7403. struct intel_framebuffer *intel_fb;
  7404. val = I915_READ(DSPCNTR(pipe));
  7405. if (!(val & DISPLAY_PLANE_ENABLE))
  7406. return;
  7407. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7408. if (!intel_fb) {
  7409. DRM_DEBUG_KMS("failed to alloc fb\n");
  7410. return;
  7411. }
  7412. fb = &intel_fb->base;
  7413. if (INTEL_INFO(dev)->gen >= 4) {
  7414. if (val & DISPPLANE_TILED) {
  7415. plane_config->tiling = I915_TILING_X;
  7416. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7417. }
  7418. }
  7419. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7420. fourcc = i9xx_format_to_fourcc(pixel_format);
  7421. fb->pixel_format = fourcc;
  7422. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7423. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7424. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7425. offset = I915_READ(DSPOFFSET(pipe));
  7426. } else {
  7427. if (plane_config->tiling)
  7428. offset = I915_READ(DSPTILEOFF(pipe));
  7429. else
  7430. offset = I915_READ(DSPLINOFF(pipe));
  7431. }
  7432. plane_config->base = base;
  7433. val = I915_READ(PIPESRC(pipe));
  7434. fb->width = ((val >> 16) & 0xfff) + 1;
  7435. fb->height = ((val >> 0) & 0xfff) + 1;
  7436. val = I915_READ(DSPSTRIDE(pipe));
  7437. fb->pitches[0] = val & 0xffffffc0;
  7438. aligned_height = intel_fb_align_height(dev, fb->height,
  7439. fb->pixel_format,
  7440. fb->modifier[0]);
  7441. plane_config->size = fb->pitches[0] * aligned_height;
  7442. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7443. pipe_name(pipe), fb->width, fb->height,
  7444. fb->bits_per_pixel, base, fb->pitches[0],
  7445. plane_config->size);
  7446. plane_config->fb = intel_fb;
  7447. }
  7448. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7449. struct intel_crtc_state *pipe_config)
  7450. {
  7451. struct drm_device *dev = crtc->base.dev;
  7452. struct drm_i915_private *dev_priv = dev->dev_private;
  7453. uint32_t tmp;
  7454. if (!intel_display_power_is_enabled(dev_priv,
  7455. POWER_DOMAIN_PIPE(crtc->pipe)))
  7456. return false;
  7457. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7458. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7459. tmp = I915_READ(PIPECONF(crtc->pipe));
  7460. if (!(tmp & PIPECONF_ENABLE))
  7461. return false;
  7462. switch (tmp & PIPECONF_BPC_MASK) {
  7463. case PIPECONF_6BPC:
  7464. pipe_config->pipe_bpp = 18;
  7465. break;
  7466. case PIPECONF_8BPC:
  7467. pipe_config->pipe_bpp = 24;
  7468. break;
  7469. case PIPECONF_10BPC:
  7470. pipe_config->pipe_bpp = 30;
  7471. break;
  7472. case PIPECONF_12BPC:
  7473. pipe_config->pipe_bpp = 36;
  7474. break;
  7475. default:
  7476. break;
  7477. }
  7478. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7479. pipe_config->limited_color_range = true;
  7480. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7481. struct intel_shared_dpll *pll;
  7482. pipe_config->has_pch_encoder = true;
  7483. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7484. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7485. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7486. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7487. if (HAS_PCH_IBX(dev_priv->dev)) {
  7488. pipe_config->shared_dpll =
  7489. (enum intel_dpll_id) crtc->pipe;
  7490. } else {
  7491. tmp = I915_READ(PCH_DPLL_SEL);
  7492. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7493. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7494. else
  7495. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7496. }
  7497. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7498. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7499. &pipe_config->dpll_hw_state));
  7500. tmp = pipe_config->dpll_hw_state.dpll;
  7501. pipe_config->pixel_multiplier =
  7502. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7503. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7504. ironlake_pch_clock_get(crtc, pipe_config);
  7505. } else {
  7506. pipe_config->pixel_multiplier = 1;
  7507. }
  7508. intel_get_pipe_timings(crtc, pipe_config);
  7509. ironlake_get_pfit_config(crtc, pipe_config);
  7510. return true;
  7511. }
  7512. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7513. {
  7514. struct drm_device *dev = dev_priv->dev;
  7515. struct intel_crtc *crtc;
  7516. for_each_intel_crtc(dev, crtc)
  7517. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7518. pipe_name(crtc->pipe));
  7519. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7520. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7521. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7522. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7523. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7524. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7525. "CPU PWM1 enabled\n");
  7526. if (IS_HASWELL(dev))
  7527. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7528. "CPU PWM2 enabled\n");
  7529. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7530. "PCH PWM1 enabled\n");
  7531. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7532. "Utility pin enabled\n");
  7533. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7534. /*
  7535. * In theory we can still leave IRQs enabled, as long as only the HPD
  7536. * interrupts remain enabled. We used to check for that, but since it's
  7537. * gen-specific and since we only disable LCPLL after we fully disable
  7538. * the interrupts, the check below should be enough.
  7539. */
  7540. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7541. }
  7542. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7543. {
  7544. struct drm_device *dev = dev_priv->dev;
  7545. if (IS_HASWELL(dev))
  7546. return I915_READ(D_COMP_HSW);
  7547. else
  7548. return I915_READ(D_COMP_BDW);
  7549. }
  7550. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7551. {
  7552. struct drm_device *dev = dev_priv->dev;
  7553. if (IS_HASWELL(dev)) {
  7554. mutex_lock(&dev_priv->rps.hw_lock);
  7555. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7556. val))
  7557. DRM_ERROR("Failed to write to D_COMP\n");
  7558. mutex_unlock(&dev_priv->rps.hw_lock);
  7559. } else {
  7560. I915_WRITE(D_COMP_BDW, val);
  7561. POSTING_READ(D_COMP_BDW);
  7562. }
  7563. }
  7564. /*
  7565. * This function implements pieces of two sequences from BSpec:
  7566. * - Sequence for display software to disable LCPLL
  7567. * - Sequence for display software to allow package C8+
  7568. * The steps implemented here are just the steps that actually touch the LCPLL
  7569. * register. Callers should take care of disabling all the display engine
  7570. * functions, doing the mode unset, fixing interrupts, etc.
  7571. */
  7572. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7573. bool switch_to_fclk, bool allow_power_down)
  7574. {
  7575. uint32_t val;
  7576. assert_can_disable_lcpll(dev_priv);
  7577. val = I915_READ(LCPLL_CTL);
  7578. if (switch_to_fclk) {
  7579. val |= LCPLL_CD_SOURCE_FCLK;
  7580. I915_WRITE(LCPLL_CTL, val);
  7581. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7582. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7583. DRM_ERROR("Switching to FCLK failed\n");
  7584. val = I915_READ(LCPLL_CTL);
  7585. }
  7586. val |= LCPLL_PLL_DISABLE;
  7587. I915_WRITE(LCPLL_CTL, val);
  7588. POSTING_READ(LCPLL_CTL);
  7589. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7590. DRM_ERROR("LCPLL still locked\n");
  7591. val = hsw_read_dcomp(dev_priv);
  7592. val |= D_COMP_COMP_DISABLE;
  7593. hsw_write_dcomp(dev_priv, val);
  7594. ndelay(100);
  7595. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7596. 1))
  7597. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7598. if (allow_power_down) {
  7599. val = I915_READ(LCPLL_CTL);
  7600. val |= LCPLL_POWER_DOWN_ALLOW;
  7601. I915_WRITE(LCPLL_CTL, val);
  7602. POSTING_READ(LCPLL_CTL);
  7603. }
  7604. }
  7605. /*
  7606. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7607. * source.
  7608. */
  7609. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7610. {
  7611. uint32_t val;
  7612. val = I915_READ(LCPLL_CTL);
  7613. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7614. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7615. return;
  7616. /*
  7617. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7618. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7619. */
  7620. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7621. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7622. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7623. I915_WRITE(LCPLL_CTL, val);
  7624. POSTING_READ(LCPLL_CTL);
  7625. }
  7626. val = hsw_read_dcomp(dev_priv);
  7627. val |= D_COMP_COMP_FORCE;
  7628. val &= ~D_COMP_COMP_DISABLE;
  7629. hsw_write_dcomp(dev_priv, val);
  7630. val = I915_READ(LCPLL_CTL);
  7631. val &= ~LCPLL_PLL_DISABLE;
  7632. I915_WRITE(LCPLL_CTL, val);
  7633. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7634. DRM_ERROR("LCPLL not locked yet\n");
  7635. if (val & LCPLL_CD_SOURCE_FCLK) {
  7636. val = I915_READ(LCPLL_CTL);
  7637. val &= ~LCPLL_CD_SOURCE_FCLK;
  7638. I915_WRITE(LCPLL_CTL, val);
  7639. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7640. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7641. DRM_ERROR("Switching back to LCPLL failed\n");
  7642. }
  7643. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7644. }
  7645. /*
  7646. * Package states C8 and deeper are really deep PC states that can only be
  7647. * reached when all the devices on the system allow it, so even if the graphics
  7648. * device allows PC8+, it doesn't mean the system will actually get to these
  7649. * states. Our driver only allows PC8+ when going into runtime PM.
  7650. *
  7651. * The requirements for PC8+ are that all the outputs are disabled, the power
  7652. * well is disabled and most interrupts are disabled, and these are also
  7653. * requirements for runtime PM. When these conditions are met, we manually do
  7654. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7655. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7656. * hang the machine.
  7657. *
  7658. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7659. * the state of some registers, so when we come back from PC8+ we need to
  7660. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7661. * need to take care of the registers kept by RC6. Notice that this happens even
  7662. * if we don't put the device in PCI D3 state (which is what currently happens
  7663. * because of the runtime PM support).
  7664. *
  7665. * For more, read "Display Sequences for Package C8" on the hardware
  7666. * documentation.
  7667. */
  7668. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7669. {
  7670. struct drm_device *dev = dev_priv->dev;
  7671. uint32_t val;
  7672. DRM_DEBUG_KMS("Enabling package C8+\n");
  7673. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7674. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7675. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7676. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7677. }
  7678. lpt_disable_clkout_dp(dev);
  7679. hsw_disable_lcpll(dev_priv, true, true);
  7680. }
  7681. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7682. {
  7683. struct drm_device *dev = dev_priv->dev;
  7684. uint32_t val;
  7685. DRM_DEBUG_KMS("Disabling package C8+\n");
  7686. hsw_restore_lcpll(dev_priv);
  7687. lpt_init_pch_refclk(dev);
  7688. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7689. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7690. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7691. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7692. }
  7693. intel_prepare_ddi(dev);
  7694. }
  7695. static void broxton_modeset_global_resources(struct drm_atomic_state *state)
  7696. {
  7697. struct drm_device *dev = state->dev;
  7698. struct drm_i915_private *dev_priv = dev->dev_private;
  7699. int max_pixclk = intel_mode_max_pixclk(state);
  7700. int req_cdclk;
  7701. /* see the comment in valleyview_modeset_global_resources */
  7702. if (WARN_ON(max_pixclk < 0))
  7703. return;
  7704. req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  7705. if (req_cdclk != dev_priv->cdclk_freq)
  7706. broxton_set_cdclk(dev, req_cdclk);
  7707. }
  7708. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7709. struct intel_crtc_state *crtc_state)
  7710. {
  7711. if (!intel_ddi_pll_select(crtc, crtc_state))
  7712. return -EINVAL;
  7713. crtc->lowfreq_avail = false;
  7714. return 0;
  7715. }
  7716. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7717. enum port port,
  7718. struct intel_crtc_state *pipe_config)
  7719. {
  7720. switch (port) {
  7721. case PORT_A:
  7722. pipe_config->ddi_pll_sel = SKL_DPLL0;
  7723. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7724. break;
  7725. case PORT_B:
  7726. pipe_config->ddi_pll_sel = SKL_DPLL1;
  7727. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7728. break;
  7729. case PORT_C:
  7730. pipe_config->ddi_pll_sel = SKL_DPLL2;
  7731. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7732. break;
  7733. default:
  7734. DRM_ERROR("Incorrect port type\n");
  7735. }
  7736. }
  7737. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7738. enum port port,
  7739. struct intel_crtc_state *pipe_config)
  7740. {
  7741. u32 temp, dpll_ctl1;
  7742. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7743. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  7744. switch (pipe_config->ddi_pll_sel) {
  7745. case SKL_DPLL0:
  7746. /*
  7747. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  7748. * of the shared DPLL framework and thus needs to be read out
  7749. * separately
  7750. */
  7751. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  7752. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  7753. break;
  7754. case SKL_DPLL1:
  7755. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7756. break;
  7757. case SKL_DPLL2:
  7758. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7759. break;
  7760. case SKL_DPLL3:
  7761. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7762. break;
  7763. }
  7764. }
  7765. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7766. enum port port,
  7767. struct intel_crtc_state *pipe_config)
  7768. {
  7769. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7770. switch (pipe_config->ddi_pll_sel) {
  7771. case PORT_CLK_SEL_WRPLL1:
  7772. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  7773. break;
  7774. case PORT_CLK_SEL_WRPLL2:
  7775. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  7776. break;
  7777. }
  7778. }
  7779. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7780. struct intel_crtc_state *pipe_config)
  7781. {
  7782. struct drm_device *dev = crtc->base.dev;
  7783. struct drm_i915_private *dev_priv = dev->dev_private;
  7784. struct intel_shared_dpll *pll;
  7785. enum port port;
  7786. uint32_t tmp;
  7787. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7788. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7789. if (IS_SKYLAKE(dev))
  7790. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7791. else if (IS_BROXTON(dev))
  7792. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7793. else
  7794. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7795. if (pipe_config->shared_dpll >= 0) {
  7796. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7797. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7798. &pipe_config->dpll_hw_state));
  7799. }
  7800. /*
  7801. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7802. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7803. * the PCH transcoder is on.
  7804. */
  7805. if (INTEL_INFO(dev)->gen < 9 &&
  7806. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7807. pipe_config->has_pch_encoder = true;
  7808. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7809. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7810. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7811. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7812. }
  7813. }
  7814. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7815. struct intel_crtc_state *pipe_config)
  7816. {
  7817. struct drm_device *dev = crtc->base.dev;
  7818. struct drm_i915_private *dev_priv = dev->dev_private;
  7819. enum intel_display_power_domain pfit_domain;
  7820. uint32_t tmp;
  7821. if (!intel_display_power_is_enabled(dev_priv,
  7822. POWER_DOMAIN_PIPE(crtc->pipe)))
  7823. return false;
  7824. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7825. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7826. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7827. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7828. enum pipe trans_edp_pipe;
  7829. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7830. default:
  7831. WARN(1, "unknown pipe linked to edp transcoder\n");
  7832. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7833. case TRANS_DDI_EDP_INPUT_A_ON:
  7834. trans_edp_pipe = PIPE_A;
  7835. break;
  7836. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7837. trans_edp_pipe = PIPE_B;
  7838. break;
  7839. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7840. trans_edp_pipe = PIPE_C;
  7841. break;
  7842. }
  7843. if (trans_edp_pipe == crtc->pipe)
  7844. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7845. }
  7846. if (!intel_display_power_is_enabled(dev_priv,
  7847. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  7848. return false;
  7849. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7850. if (!(tmp & PIPECONF_ENABLE))
  7851. return false;
  7852. haswell_get_ddi_port_state(crtc, pipe_config);
  7853. intel_get_pipe_timings(crtc, pipe_config);
  7854. if (INTEL_INFO(dev)->gen >= 9) {
  7855. skl_init_scalers(dev, crtc, pipe_config);
  7856. }
  7857. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7858. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  7859. if (INTEL_INFO(dev)->gen == 9)
  7860. skylake_get_pfit_config(crtc, pipe_config);
  7861. else if (INTEL_INFO(dev)->gen < 9)
  7862. ironlake_get_pfit_config(crtc, pipe_config);
  7863. else
  7864. MISSING_CASE(INTEL_INFO(dev)->gen);
  7865. } else {
  7866. pipe_config->scaler_state.scaler_id = -1;
  7867. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7868. }
  7869. if (IS_HASWELL(dev))
  7870. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7871. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7872. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  7873. pipe_config->pixel_multiplier =
  7874. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7875. } else {
  7876. pipe_config->pixel_multiplier = 1;
  7877. }
  7878. return true;
  7879. }
  7880. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  7881. {
  7882. struct drm_device *dev = crtc->dev;
  7883. struct drm_i915_private *dev_priv = dev->dev_private;
  7884. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7885. uint32_t cntl = 0, size = 0;
  7886. if (base) {
  7887. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  7888. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  7889. unsigned int stride = roundup_pow_of_two(width) * 4;
  7890. switch (stride) {
  7891. default:
  7892. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7893. width, stride);
  7894. stride = 256;
  7895. /* fallthrough */
  7896. case 256:
  7897. case 512:
  7898. case 1024:
  7899. case 2048:
  7900. break;
  7901. }
  7902. cntl |= CURSOR_ENABLE |
  7903. CURSOR_GAMMA_ENABLE |
  7904. CURSOR_FORMAT_ARGB |
  7905. CURSOR_STRIDE(stride);
  7906. size = (height << 12) | width;
  7907. }
  7908. if (intel_crtc->cursor_cntl != 0 &&
  7909. (intel_crtc->cursor_base != base ||
  7910. intel_crtc->cursor_size != size ||
  7911. intel_crtc->cursor_cntl != cntl)) {
  7912. /* On these chipsets we can only modify the base/size/stride
  7913. * whilst the cursor is disabled.
  7914. */
  7915. I915_WRITE(_CURACNTR, 0);
  7916. POSTING_READ(_CURACNTR);
  7917. intel_crtc->cursor_cntl = 0;
  7918. }
  7919. if (intel_crtc->cursor_base != base) {
  7920. I915_WRITE(_CURABASE, base);
  7921. intel_crtc->cursor_base = base;
  7922. }
  7923. if (intel_crtc->cursor_size != size) {
  7924. I915_WRITE(CURSIZE, size);
  7925. intel_crtc->cursor_size = size;
  7926. }
  7927. if (intel_crtc->cursor_cntl != cntl) {
  7928. I915_WRITE(_CURACNTR, cntl);
  7929. POSTING_READ(_CURACNTR);
  7930. intel_crtc->cursor_cntl = cntl;
  7931. }
  7932. }
  7933. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  7934. {
  7935. struct drm_device *dev = crtc->dev;
  7936. struct drm_i915_private *dev_priv = dev->dev_private;
  7937. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7938. int pipe = intel_crtc->pipe;
  7939. uint32_t cntl;
  7940. cntl = 0;
  7941. if (base) {
  7942. cntl = MCURSOR_GAMMA_ENABLE;
  7943. switch (intel_crtc->base.cursor->state->crtc_w) {
  7944. case 64:
  7945. cntl |= CURSOR_MODE_64_ARGB_AX;
  7946. break;
  7947. case 128:
  7948. cntl |= CURSOR_MODE_128_ARGB_AX;
  7949. break;
  7950. case 256:
  7951. cntl |= CURSOR_MODE_256_ARGB_AX;
  7952. break;
  7953. default:
  7954. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  7955. return;
  7956. }
  7957. cntl |= pipe << 28; /* Connect to correct pipe */
  7958. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  7959. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7960. }
  7961. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  7962. cntl |= CURSOR_ROTATE_180;
  7963. if (intel_crtc->cursor_cntl != cntl) {
  7964. I915_WRITE(CURCNTR(pipe), cntl);
  7965. POSTING_READ(CURCNTR(pipe));
  7966. intel_crtc->cursor_cntl = cntl;
  7967. }
  7968. /* and commit changes on next vblank */
  7969. I915_WRITE(CURBASE(pipe), base);
  7970. POSTING_READ(CURBASE(pipe));
  7971. intel_crtc->cursor_base = base;
  7972. }
  7973. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7974. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7975. bool on)
  7976. {
  7977. struct drm_device *dev = crtc->dev;
  7978. struct drm_i915_private *dev_priv = dev->dev_private;
  7979. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7980. int pipe = intel_crtc->pipe;
  7981. int x = crtc->cursor_x;
  7982. int y = crtc->cursor_y;
  7983. u32 base = 0, pos = 0;
  7984. if (on)
  7985. base = intel_crtc->cursor_addr;
  7986. if (x >= intel_crtc->config->pipe_src_w)
  7987. base = 0;
  7988. if (y >= intel_crtc->config->pipe_src_h)
  7989. base = 0;
  7990. if (x < 0) {
  7991. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  7992. base = 0;
  7993. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7994. x = -x;
  7995. }
  7996. pos |= x << CURSOR_X_SHIFT;
  7997. if (y < 0) {
  7998. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  7999. base = 0;
  8000. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8001. y = -y;
  8002. }
  8003. pos |= y << CURSOR_Y_SHIFT;
  8004. if (base == 0 && intel_crtc->cursor_base == 0)
  8005. return;
  8006. I915_WRITE(CURPOS(pipe), pos);
  8007. /* ILK+ do this automagically */
  8008. if (HAS_GMCH_DISPLAY(dev) &&
  8009. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8010. base += (intel_crtc->base.cursor->state->crtc_h *
  8011. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8012. }
  8013. if (IS_845G(dev) || IS_I865G(dev))
  8014. i845_update_cursor(crtc, base);
  8015. else
  8016. i9xx_update_cursor(crtc, base);
  8017. }
  8018. static bool cursor_size_ok(struct drm_device *dev,
  8019. uint32_t width, uint32_t height)
  8020. {
  8021. if (width == 0 || height == 0)
  8022. return false;
  8023. /*
  8024. * 845g/865g are special in that they are only limited by
  8025. * the width of their cursors, the height is arbitrary up to
  8026. * the precision of the register. Everything else requires
  8027. * square cursors, limited to a few power-of-two sizes.
  8028. */
  8029. if (IS_845G(dev) || IS_I865G(dev)) {
  8030. if ((width & 63) != 0)
  8031. return false;
  8032. if (width > (IS_845G(dev) ? 64 : 512))
  8033. return false;
  8034. if (height > 1023)
  8035. return false;
  8036. } else {
  8037. switch (width | height) {
  8038. case 256:
  8039. case 128:
  8040. if (IS_GEN2(dev))
  8041. return false;
  8042. case 64:
  8043. break;
  8044. default:
  8045. return false;
  8046. }
  8047. }
  8048. return true;
  8049. }
  8050. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8051. u16 *blue, uint32_t start, uint32_t size)
  8052. {
  8053. int end = (start + size > 256) ? 256 : start + size, i;
  8054. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8055. for (i = start; i < end; i++) {
  8056. intel_crtc->lut_r[i] = red[i] >> 8;
  8057. intel_crtc->lut_g[i] = green[i] >> 8;
  8058. intel_crtc->lut_b[i] = blue[i] >> 8;
  8059. }
  8060. intel_crtc_load_lut(crtc);
  8061. }
  8062. /* VESA 640x480x72Hz mode to set on the pipe */
  8063. static struct drm_display_mode load_detect_mode = {
  8064. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8065. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8066. };
  8067. struct drm_framebuffer *
  8068. __intel_framebuffer_create(struct drm_device *dev,
  8069. struct drm_mode_fb_cmd2 *mode_cmd,
  8070. struct drm_i915_gem_object *obj)
  8071. {
  8072. struct intel_framebuffer *intel_fb;
  8073. int ret;
  8074. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8075. if (!intel_fb) {
  8076. drm_gem_object_unreference(&obj->base);
  8077. return ERR_PTR(-ENOMEM);
  8078. }
  8079. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8080. if (ret)
  8081. goto err;
  8082. return &intel_fb->base;
  8083. err:
  8084. drm_gem_object_unreference(&obj->base);
  8085. kfree(intel_fb);
  8086. return ERR_PTR(ret);
  8087. }
  8088. static struct drm_framebuffer *
  8089. intel_framebuffer_create(struct drm_device *dev,
  8090. struct drm_mode_fb_cmd2 *mode_cmd,
  8091. struct drm_i915_gem_object *obj)
  8092. {
  8093. struct drm_framebuffer *fb;
  8094. int ret;
  8095. ret = i915_mutex_lock_interruptible(dev);
  8096. if (ret)
  8097. return ERR_PTR(ret);
  8098. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8099. mutex_unlock(&dev->struct_mutex);
  8100. return fb;
  8101. }
  8102. static u32
  8103. intel_framebuffer_pitch_for_width(int width, int bpp)
  8104. {
  8105. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8106. return ALIGN(pitch, 64);
  8107. }
  8108. static u32
  8109. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8110. {
  8111. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8112. return PAGE_ALIGN(pitch * mode->vdisplay);
  8113. }
  8114. static struct drm_framebuffer *
  8115. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8116. struct drm_display_mode *mode,
  8117. int depth, int bpp)
  8118. {
  8119. struct drm_i915_gem_object *obj;
  8120. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8121. obj = i915_gem_alloc_object(dev,
  8122. intel_framebuffer_size_for_mode(mode, bpp));
  8123. if (obj == NULL)
  8124. return ERR_PTR(-ENOMEM);
  8125. mode_cmd.width = mode->hdisplay;
  8126. mode_cmd.height = mode->vdisplay;
  8127. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8128. bpp);
  8129. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8130. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8131. }
  8132. static struct drm_framebuffer *
  8133. mode_fits_in_fbdev(struct drm_device *dev,
  8134. struct drm_display_mode *mode)
  8135. {
  8136. #ifdef CONFIG_DRM_I915_FBDEV
  8137. struct drm_i915_private *dev_priv = dev->dev_private;
  8138. struct drm_i915_gem_object *obj;
  8139. struct drm_framebuffer *fb;
  8140. if (!dev_priv->fbdev)
  8141. return NULL;
  8142. if (!dev_priv->fbdev->fb)
  8143. return NULL;
  8144. obj = dev_priv->fbdev->fb->obj;
  8145. BUG_ON(!obj);
  8146. fb = &dev_priv->fbdev->fb->base;
  8147. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8148. fb->bits_per_pixel))
  8149. return NULL;
  8150. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8151. return NULL;
  8152. return fb;
  8153. #else
  8154. return NULL;
  8155. #endif
  8156. }
  8157. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8158. struct drm_display_mode *mode,
  8159. struct intel_load_detect_pipe *old,
  8160. struct drm_modeset_acquire_ctx *ctx)
  8161. {
  8162. struct intel_crtc *intel_crtc;
  8163. struct intel_encoder *intel_encoder =
  8164. intel_attached_encoder(connector);
  8165. struct drm_crtc *possible_crtc;
  8166. struct drm_encoder *encoder = &intel_encoder->base;
  8167. struct drm_crtc *crtc = NULL;
  8168. struct drm_device *dev = encoder->dev;
  8169. struct drm_framebuffer *fb;
  8170. struct drm_mode_config *config = &dev->mode_config;
  8171. struct drm_atomic_state *state = NULL;
  8172. struct drm_connector_state *connector_state;
  8173. int ret, i = -1;
  8174. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8175. connector->base.id, connector->name,
  8176. encoder->base.id, encoder->name);
  8177. retry:
  8178. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8179. if (ret)
  8180. goto fail_unlock;
  8181. /*
  8182. * Algorithm gets a little messy:
  8183. *
  8184. * - if the connector already has an assigned crtc, use it (but make
  8185. * sure it's on first)
  8186. *
  8187. * - try to find the first unused crtc that can drive this connector,
  8188. * and use that if we find one
  8189. */
  8190. /* See if we already have a CRTC for this connector */
  8191. if (encoder->crtc) {
  8192. crtc = encoder->crtc;
  8193. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8194. if (ret)
  8195. goto fail_unlock;
  8196. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8197. if (ret)
  8198. goto fail_unlock;
  8199. old->dpms_mode = connector->dpms;
  8200. old->load_detect_temp = false;
  8201. /* Make sure the crtc and connector are running */
  8202. if (connector->dpms != DRM_MODE_DPMS_ON)
  8203. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8204. return true;
  8205. }
  8206. /* Find an unused one (if possible) */
  8207. for_each_crtc(dev, possible_crtc) {
  8208. i++;
  8209. if (!(encoder->possible_crtcs & (1 << i)))
  8210. continue;
  8211. if (possible_crtc->state->enable)
  8212. continue;
  8213. /* This can occur when applying the pipe A quirk on resume. */
  8214. if (to_intel_crtc(possible_crtc)->new_enabled)
  8215. continue;
  8216. crtc = possible_crtc;
  8217. break;
  8218. }
  8219. /*
  8220. * If we didn't find an unused CRTC, don't use any.
  8221. */
  8222. if (!crtc) {
  8223. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8224. goto fail_unlock;
  8225. }
  8226. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8227. if (ret)
  8228. goto fail_unlock;
  8229. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8230. if (ret)
  8231. goto fail_unlock;
  8232. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8233. to_intel_connector(connector)->new_encoder = intel_encoder;
  8234. intel_crtc = to_intel_crtc(crtc);
  8235. intel_crtc->new_enabled = true;
  8236. old->dpms_mode = connector->dpms;
  8237. old->load_detect_temp = true;
  8238. old->release_fb = NULL;
  8239. state = drm_atomic_state_alloc(dev);
  8240. if (!state)
  8241. return false;
  8242. state->acquire_ctx = ctx;
  8243. connector_state = drm_atomic_get_connector_state(state, connector);
  8244. if (IS_ERR(connector_state)) {
  8245. ret = PTR_ERR(connector_state);
  8246. goto fail;
  8247. }
  8248. connector_state->crtc = crtc;
  8249. connector_state->best_encoder = &intel_encoder->base;
  8250. if (!mode)
  8251. mode = &load_detect_mode;
  8252. /* We need a framebuffer large enough to accommodate all accesses
  8253. * that the plane may generate whilst we perform load detection.
  8254. * We can not rely on the fbcon either being present (we get called
  8255. * during its initialisation to detect all boot displays, or it may
  8256. * not even exist) or that it is large enough to satisfy the
  8257. * requested mode.
  8258. */
  8259. fb = mode_fits_in_fbdev(dev, mode);
  8260. if (fb == NULL) {
  8261. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8262. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8263. old->release_fb = fb;
  8264. } else
  8265. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8266. if (IS_ERR(fb)) {
  8267. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8268. goto fail;
  8269. }
  8270. if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
  8271. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8272. if (old->release_fb)
  8273. old->release_fb->funcs->destroy(old->release_fb);
  8274. goto fail;
  8275. }
  8276. crtc->primary->crtc = crtc;
  8277. /* let the connector get through one full cycle before testing */
  8278. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8279. return true;
  8280. fail:
  8281. intel_crtc->new_enabled = crtc->state->enable;
  8282. fail_unlock:
  8283. if (state) {
  8284. drm_atomic_state_free(state);
  8285. state = NULL;
  8286. }
  8287. if (ret == -EDEADLK) {
  8288. drm_modeset_backoff(ctx);
  8289. goto retry;
  8290. }
  8291. return false;
  8292. }
  8293. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8294. struct intel_load_detect_pipe *old,
  8295. struct drm_modeset_acquire_ctx *ctx)
  8296. {
  8297. struct drm_device *dev = connector->dev;
  8298. struct intel_encoder *intel_encoder =
  8299. intel_attached_encoder(connector);
  8300. struct drm_encoder *encoder = &intel_encoder->base;
  8301. struct drm_crtc *crtc = encoder->crtc;
  8302. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8303. struct drm_atomic_state *state;
  8304. struct drm_connector_state *connector_state;
  8305. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8306. connector->base.id, connector->name,
  8307. encoder->base.id, encoder->name);
  8308. if (old->load_detect_temp) {
  8309. state = drm_atomic_state_alloc(dev);
  8310. if (!state)
  8311. goto fail;
  8312. state->acquire_ctx = ctx;
  8313. connector_state = drm_atomic_get_connector_state(state, connector);
  8314. if (IS_ERR(connector_state))
  8315. goto fail;
  8316. to_intel_connector(connector)->new_encoder = NULL;
  8317. intel_encoder->new_crtc = NULL;
  8318. intel_crtc->new_enabled = false;
  8319. connector_state->best_encoder = NULL;
  8320. connector_state->crtc = NULL;
  8321. intel_set_mode(crtc, NULL, 0, 0, NULL, state);
  8322. drm_atomic_state_free(state);
  8323. if (old->release_fb) {
  8324. drm_framebuffer_unregister_private(old->release_fb);
  8325. drm_framebuffer_unreference(old->release_fb);
  8326. }
  8327. return;
  8328. }
  8329. /* Switch crtc and encoder back off if necessary */
  8330. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8331. connector->funcs->dpms(connector, old->dpms_mode);
  8332. return;
  8333. fail:
  8334. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8335. drm_atomic_state_free(state);
  8336. }
  8337. static int i9xx_pll_refclk(struct drm_device *dev,
  8338. const struct intel_crtc_state *pipe_config)
  8339. {
  8340. struct drm_i915_private *dev_priv = dev->dev_private;
  8341. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8342. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8343. return dev_priv->vbt.lvds_ssc_freq;
  8344. else if (HAS_PCH_SPLIT(dev))
  8345. return 120000;
  8346. else if (!IS_GEN2(dev))
  8347. return 96000;
  8348. else
  8349. return 48000;
  8350. }
  8351. /* Returns the clock of the currently programmed mode of the given pipe. */
  8352. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8353. struct intel_crtc_state *pipe_config)
  8354. {
  8355. struct drm_device *dev = crtc->base.dev;
  8356. struct drm_i915_private *dev_priv = dev->dev_private;
  8357. int pipe = pipe_config->cpu_transcoder;
  8358. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8359. u32 fp;
  8360. intel_clock_t clock;
  8361. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8362. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8363. fp = pipe_config->dpll_hw_state.fp0;
  8364. else
  8365. fp = pipe_config->dpll_hw_state.fp1;
  8366. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8367. if (IS_PINEVIEW(dev)) {
  8368. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8369. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8370. } else {
  8371. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8372. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8373. }
  8374. if (!IS_GEN2(dev)) {
  8375. if (IS_PINEVIEW(dev))
  8376. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8377. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8378. else
  8379. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8380. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8381. switch (dpll & DPLL_MODE_MASK) {
  8382. case DPLLB_MODE_DAC_SERIAL:
  8383. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8384. 5 : 10;
  8385. break;
  8386. case DPLLB_MODE_LVDS:
  8387. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8388. 7 : 14;
  8389. break;
  8390. default:
  8391. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8392. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8393. return;
  8394. }
  8395. if (IS_PINEVIEW(dev))
  8396. pineview_clock(refclk, &clock);
  8397. else
  8398. i9xx_clock(refclk, &clock);
  8399. } else {
  8400. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8401. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8402. if (is_lvds) {
  8403. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8404. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8405. if (lvds & LVDS_CLKB_POWER_UP)
  8406. clock.p2 = 7;
  8407. else
  8408. clock.p2 = 14;
  8409. } else {
  8410. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8411. clock.p1 = 2;
  8412. else {
  8413. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8414. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8415. }
  8416. if (dpll & PLL_P2_DIVIDE_BY_4)
  8417. clock.p2 = 4;
  8418. else
  8419. clock.p2 = 2;
  8420. }
  8421. i9xx_clock(refclk, &clock);
  8422. }
  8423. /*
  8424. * This value includes pixel_multiplier. We will use
  8425. * port_clock to compute adjusted_mode.crtc_clock in the
  8426. * encoder's get_config() function.
  8427. */
  8428. pipe_config->port_clock = clock.dot;
  8429. }
  8430. int intel_dotclock_calculate(int link_freq,
  8431. const struct intel_link_m_n *m_n)
  8432. {
  8433. /*
  8434. * The calculation for the data clock is:
  8435. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8436. * But we want to avoid losing precison if possible, so:
  8437. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8438. *
  8439. * and the link clock is simpler:
  8440. * link_clock = (m * link_clock) / n
  8441. */
  8442. if (!m_n->link_n)
  8443. return 0;
  8444. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8445. }
  8446. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8447. struct intel_crtc_state *pipe_config)
  8448. {
  8449. struct drm_device *dev = crtc->base.dev;
  8450. /* read out port_clock from the DPLL */
  8451. i9xx_crtc_clock_get(crtc, pipe_config);
  8452. /*
  8453. * This value does not include pixel_multiplier.
  8454. * We will check that port_clock and adjusted_mode.crtc_clock
  8455. * agree once we know their relationship in the encoder's
  8456. * get_config() function.
  8457. */
  8458. pipe_config->base.adjusted_mode.crtc_clock =
  8459. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8460. &pipe_config->fdi_m_n);
  8461. }
  8462. /** Returns the currently programmed mode of the given pipe. */
  8463. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8464. struct drm_crtc *crtc)
  8465. {
  8466. struct drm_i915_private *dev_priv = dev->dev_private;
  8467. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8468. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8469. struct drm_display_mode *mode;
  8470. struct intel_crtc_state pipe_config;
  8471. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8472. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8473. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8474. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8475. enum pipe pipe = intel_crtc->pipe;
  8476. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8477. if (!mode)
  8478. return NULL;
  8479. /*
  8480. * Construct a pipe_config sufficient for getting the clock info
  8481. * back out of crtc_clock_get.
  8482. *
  8483. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8484. * to use a real value here instead.
  8485. */
  8486. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8487. pipe_config.pixel_multiplier = 1;
  8488. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8489. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8490. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8491. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8492. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8493. mode->hdisplay = (htot & 0xffff) + 1;
  8494. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8495. mode->hsync_start = (hsync & 0xffff) + 1;
  8496. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8497. mode->vdisplay = (vtot & 0xffff) + 1;
  8498. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8499. mode->vsync_start = (vsync & 0xffff) + 1;
  8500. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8501. drm_mode_set_name(mode);
  8502. return mode;
  8503. }
  8504. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  8505. {
  8506. struct drm_device *dev = crtc->dev;
  8507. struct drm_i915_private *dev_priv = dev->dev_private;
  8508. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8509. if (!HAS_GMCH_DISPLAY(dev))
  8510. return;
  8511. if (!dev_priv->lvds_downclock_avail)
  8512. return;
  8513. /*
  8514. * Since this is called by a timer, we should never get here in
  8515. * the manual case.
  8516. */
  8517. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  8518. int pipe = intel_crtc->pipe;
  8519. int dpll_reg = DPLL(pipe);
  8520. int dpll;
  8521. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  8522. assert_panel_unlocked(dev_priv, pipe);
  8523. dpll = I915_READ(dpll_reg);
  8524. dpll |= DISPLAY_RATE_SELECT_FPA1;
  8525. I915_WRITE(dpll_reg, dpll);
  8526. intel_wait_for_vblank(dev, pipe);
  8527. dpll = I915_READ(dpll_reg);
  8528. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  8529. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  8530. }
  8531. }
  8532. void intel_mark_busy(struct drm_device *dev)
  8533. {
  8534. struct drm_i915_private *dev_priv = dev->dev_private;
  8535. if (dev_priv->mm.busy)
  8536. return;
  8537. intel_runtime_pm_get(dev_priv);
  8538. i915_update_gfx_val(dev_priv);
  8539. if (INTEL_INFO(dev)->gen >= 6)
  8540. gen6_rps_busy(dev_priv);
  8541. dev_priv->mm.busy = true;
  8542. }
  8543. void intel_mark_idle(struct drm_device *dev)
  8544. {
  8545. struct drm_i915_private *dev_priv = dev->dev_private;
  8546. struct drm_crtc *crtc;
  8547. if (!dev_priv->mm.busy)
  8548. return;
  8549. dev_priv->mm.busy = false;
  8550. for_each_crtc(dev, crtc) {
  8551. if (!crtc->primary->fb)
  8552. continue;
  8553. intel_decrease_pllclock(crtc);
  8554. }
  8555. if (INTEL_INFO(dev)->gen >= 6)
  8556. gen6_rps_idle(dev->dev_private);
  8557. intel_runtime_pm_put(dev_priv);
  8558. }
  8559. static void intel_crtc_set_state(struct intel_crtc *crtc,
  8560. struct intel_crtc_state *crtc_state)
  8561. {
  8562. kfree(crtc->config);
  8563. crtc->config = crtc_state;
  8564. crtc->base.state = &crtc_state->base;
  8565. }
  8566. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8567. {
  8568. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8569. struct drm_device *dev = crtc->dev;
  8570. struct intel_unpin_work *work;
  8571. spin_lock_irq(&dev->event_lock);
  8572. work = intel_crtc->unpin_work;
  8573. intel_crtc->unpin_work = NULL;
  8574. spin_unlock_irq(&dev->event_lock);
  8575. if (work) {
  8576. cancel_work_sync(&work->work);
  8577. kfree(work);
  8578. }
  8579. intel_crtc_set_state(intel_crtc, NULL);
  8580. drm_crtc_cleanup(crtc);
  8581. kfree(intel_crtc);
  8582. }
  8583. static void intel_unpin_work_fn(struct work_struct *__work)
  8584. {
  8585. struct intel_unpin_work *work =
  8586. container_of(__work, struct intel_unpin_work, work);
  8587. struct drm_device *dev = work->crtc->dev;
  8588. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  8589. mutex_lock(&dev->struct_mutex);
  8590. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  8591. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8592. intel_fbc_update(dev);
  8593. if (work->flip_queued_req)
  8594. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8595. mutex_unlock(&dev->struct_mutex);
  8596. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8597. drm_framebuffer_unreference(work->old_fb);
  8598. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  8599. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  8600. kfree(work);
  8601. }
  8602. static void do_intel_finish_page_flip(struct drm_device *dev,
  8603. struct drm_crtc *crtc)
  8604. {
  8605. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8606. struct intel_unpin_work *work;
  8607. unsigned long flags;
  8608. /* Ignore early vblank irqs */
  8609. if (intel_crtc == NULL)
  8610. return;
  8611. /*
  8612. * This is called both by irq handlers and the reset code (to complete
  8613. * lost pageflips) so needs the full irqsave spinlocks.
  8614. */
  8615. spin_lock_irqsave(&dev->event_lock, flags);
  8616. work = intel_crtc->unpin_work;
  8617. /* Ensure we don't miss a work->pending update ... */
  8618. smp_rmb();
  8619. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8620. spin_unlock_irqrestore(&dev->event_lock, flags);
  8621. return;
  8622. }
  8623. page_flip_completed(intel_crtc);
  8624. spin_unlock_irqrestore(&dev->event_lock, flags);
  8625. }
  8626. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8627. {
  8628. struct drm_i915_private *dev_priv = dev->dev_private;
  8629. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8630. do_intel_finish_page_flip(dev, crtc);
  8631. }
  8632. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  8633. {
  8634. struct drm_i915_private *dev_priv = dev->dev_private;
  8635. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  8636. do_intel_finish_page_flip(dev, crtc);
  8637. }
  8638. /* Is 'a' after or equal to 'b'? */
  8639. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8640. {
  8641. return !((a - b) & 0x80000000);
  8642. }
  8643. static bool page_flip_finished(struct intel_crtc *crtc)
  8644. {
  8645. struct drm_device *dev = crtc->base.dev;
  8646. struct drm_i915_private *dev_priv = dev->dev_private;
  8647. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  8648. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  8649. return true;
  8650. /*
  8651. * The relevant registers doen't exist on pre-ctg.
  8652. * As the flip done interrupt doesn't trigger for mmio
  8653. * flips on gmch platforms, a flip count check isn't
  8654. * really needed there. But since ctg has the registers,
  8655. * include it in the check anyway.
  8656. */
  8657. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  8658. return true;
  8659. /*
  8660. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8661. * used the same base address. In that case the mmio flip might
  8662. * have completed, but the CS hasn't even executed the flip yet.
  8663. *
  8664. * A flip count check isn't enough as the CS might have updated
  8665. * the base address just after start of vblank, but before we
  8666. * managed to process the interrupt. This means we'd complete the
  8667. * CS flip too soon.
  8668. *
  8669. * Combining both checks should get us a good enough result. It may
  8670. * still happen that the CS flip has been executed, but has not
  8671. * yet actually completed. But in case the base address is the same
  8672. * anyway, we don't really care.
  8673. */
  8674. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8675. crtc->unpin_work->gtt_offset &&
  8676. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  8677. crtc->unpin_work->flip_count);
  8678. }
  8679. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  8680. {
  8681. struct drm_i915_private *dev_priv = dev->dev_private;
  8682. struct intel_crtc *intel_crtc =
  8683. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  8684. unsigned long flags;
  8685. /*
  8686. * This is called both by irq handlers and the reset code (to complete
  8687. * lost pageflips) so needs the full irqsave spinlocks.
  8688. *
  8689. * NB: An MMIO update of the plane base pointer will also
  8690. * generate a page-flip completion irq, i.e. every modeset
  8691. * is also accompanied by a spurious intel_prepare_page_flip().
  8692. */
  8693. spin_lock_irqsave(&dev->event_lock, flags);
  8694. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  8695. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  8696. spin_unlock_irqrestore(&dev->event_lock, flags);
  8697. }
  8698. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  8699. {
  8700. /* Ensure that the work item is consistent when activating it ... */
  8701. smp_wmb();
  8702. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  8703. /* and that it is marked active as soon as the irq could fire. */
  8704. smp_wmb();
  8705. }
  8706. static int intel_gen2_queue_flip(struct drm_device *dev,
  8707. struct drm_crtc *crtc,
  8708. struct drm_framebuffer *fb,
  8709. struct drm_i915_gem_object *obj,
  8710. struct intel_engine_cs *ring,
  8711. uint32_t flags)
  8712. {
  8713. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8714. u32 flip_mask;
  8715. int ret;
  8716. ret = intel_ring_begin(ring, 6);
  8717. if (ret)
  8718. return ret;
  8719. /* Can't queue multiple flips, so wait for the previous
  8720. * one to finish before executing the next.
  8721. */
  8722. if (intel_crtc->plane)
  8723. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8724. else
  8725. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8726. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8727. intel_ring_emit(ring, MI_NOOP);
  8728. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8729. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8730. intel_ring_emit(ring, fb->pitches[0]);
  8731. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8732. intel_ring_emit(ring, 0); /* aux display base address, unused */
  8733. intel_mark_page_flip_active(intel_crtc);
  8734. __intel_ring_advance(ring);
  8735. return 0;
  8736. }
  8737. static int intel_gen3_queue_flip(struct drm_device *dev,
  8738. struct drm_crtc *crtc,
  8739. struct drm_framebuffer *fb,
  8740. struct drm_i915_gem_object *obj,
  8741. struct intel_engine_cs *ring,
  8742. uint32_t flags)
  8743. {
  8744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8745. u32 flip_mask;
  8746. int ret;
  8747. ret = intel_ring_begin(ring, 6);
  8748. if (ret)
  8749. return ret;
  8750. if (intel_crtc->plane)
  8751. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8752. else
  8753. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8754. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8755. intel_ring_emit(ring, MI_NOOP);
  8756. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  8757. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8758. intel_ring_emit(ring, fb->pitches[0]);
  8759. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8760. intel_ring_emit(ring, MI_NOOP);
  8761. intel_mark_page_flip_active(intel_crtc);
  8762. __intel_ring_advance(ring);
  8763. return 0;
  8764. }
  8765. static int intel_gen4_queue_flip(struct drm_device *dev,
  8766. struct drm_crtc *crtc,
  8767. struct drm_framebuffer *fb,
  8768. struct drm_i915_gem_object *obj,
  8769. struct intel_engine_cs *ring,
  8770. uint32_t flags)
  8771. {
  8772. struct drm_i915_private *dev_priv = dev->dev_private;
  8773. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8774. uint32_t pf, pipesrc;
  8775. int ret;
  8776. ret = intel_ring_begin(ring, 4);
  8777. if (ret)
  8778. return ret;
  8779. /* i965+ uses the linear or tiled offsets from the
  8780. * Display Registers (which do not change across a page-flip)
  8781. * so we need only reprogram the base address.
  8782. */
  8783. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8784. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8785. intel_ring_emit(ring, fb->pitches[0]);
  8786. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  8787. obj->tiling_mode);
  8788. /* XXX Enabling the panel-fitter across page-flip is so far
  8789. * untested on non-native modes, so ignore it for now.
  8790. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8791. */
  8792. pf = 0;
  8793. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8794. intel_ring_emit(ring, pf | pipesrc);
  8795. intel_mark_page_flip_active(intel_crtc);
  8796. __intel_ring_advance(ring);
  8797. return 0;
  8798. }
  8799. static int intel_gen6_queue_flip(struct drm_device *dev,
  8800. struct drm_crtc *crtc,
  8801. struct drm_framebuffer *fb,
  8802. struct drm_i915_gem_object *obj,
  8803. struct intel_engine_cs *ring,
  8804. uint32_t flags)
  8805. {
  8806. struct drm_i915_private *dev_priv = dev->dev_private;
  8807. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8808. uint32_t pf, pipesrc;
  8809. int ret;
  8810. ret = intel_ring_begin(ring, 4);
  8811. if (ret)
  8812. return ret;
  8813. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8814. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8815. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  8816. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8817. /* Contrary to the suggestions in the documentation,
  8818. * "Enable Panel Fitter" does not seem to be required when page
  8819. * flipping with a non-native mode, and worse causes a normal
  8820. * modeset to fail.
  8821. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8822. */
  8823. pf = 0;
  8824. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8825. intel_ring_emit(ring, pf | pipesrc);
  8826. intel_mark_page_flip_active(intel_crtc);
  8827. __intel_ring_advance(ring);
  8828. return 0;
  8829. }
  8830. static int intel_gen7_queue_flip(struct drm_device *dev,
  8831. struct drm_crtc *crtc,
  8832. struct drm_framebuffer *fb,
  8833. struct drm_i915_gem_object *obj,
  8834. struct intel_engine_cs *ring,
  8835. uint32_t flags)
  8836. {
  8837. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8838. uint32_t plane_bit = 0;
  8839. int len, ret;
  8840. switch (intel_crtc->plane) {
  8841. case PLANE_A:
  8842. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8843. break;
  8844. case PLANE_B:
  8845. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8846. break;
  8847. case PLANE_C:
  8848. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8849. break;
  8850. default:
  8851. WARN_ONCE(1, "unknown plane in flip command\n");
  8852. return -ENODEV;
  8853. }
  8854. len = 4;
  8855. if (ring->id == RCS) {
  8856. len += 6;
  8857. /*
  8858. * On Gen 8, SRM is now taking an extra dword to accommodate
  8859. * 48bits addresses, and we need a NOOP for the batch size to
  8860. * stay even.
  8861. */
  8862. if (IS_GEN8(dev))
  8863. len += 2;
  8864. }
  8865. /*
  8866. * BSpec MI_DISPLAY_FLIP for IVB:
  8867. * "The full packet must be contained within the same cache line."
  8868. *
  8869. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8870. * cacheline, if we ever start emitting more commands before
  8871. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8872. * then do the cacheline alignment, and finally emit the
  8873. * MI_DISPLAY_FLIP.
  8874. */
  8875. ret = intel_ring_cacheline_align(ring);
  8876. if (ret)
  8877. return ret;
  8878. ret = intel_ring_begin(ring, len);
  8879. if (ret)
  8880. return ret;
  8881. /* Unmask the flip-done completion message. Note that the bspec says that
  8882. * we should do this for both the BCS and RCS, and that we must not unmask
  8883. * more than one flip event at any time (or ensure that one flip message
  8884. * can be sent by waiting for flip-done prior to queueing new flips).
  8885. * Experimentation says that BCS works despite DERRMR masking all
  8886. * flip-done completion events and that unmasking all planes at once
  8887. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8888. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8889. */
  8890. if (ring->id == RCS) {
  8891. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8892. intel_ring_emit(ring, DERRMR);
  8893. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8894. DERRMR_PIPEB_PRI_FLIP_DONE |
  8895. DERRMR_PIPEC_PRI_FLIP_DONE));
  8896. if (IS_GEN8(dev))
  8897. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8898. MI_SRM_LRM_GLOBAL_GTT);
  8899. else
  8900. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8901. MI_SRM_LRM_GLOBAL_GTT);
  8902. intel_ring_emit(ring, DERRMR);
  8903. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8904. if (IS_GEN8(dev)) {
  8905. intel_ring_emit(ring, 0);
  8906. intel_ring_emit(ring, MI_NOOP);
  8907. }
  8908. }
  8909. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8910. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8911. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8912. intel_ring_emit(ring, (MI_NOOP));
  8913. intel_mark_page_flip_active(intel_crtc);
  8914. __intel_ring_advance(ring);
  8915. return 0;
  8916. }
  8917. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8918. struct drm_i915_gem_object *obj)
  8919. {
  8920. /*
  8921. * This is not being used for older platforms, because
  8922. * non-availability of flip done interrupt forces us to use
  8923. * CS flips. Older platforms derive flip done using some clever
  8924. * tricks involving the flip_pending status bits and vblank irqs.
  8925. * So using MMIO flips there would disrupt this mechanism.
  8926. */
  8927. if (ring == NULL)
  8928. return true;
  8929. if (INTEL_INFO(ring->dev)->gen < 5)
  8930. return false;
  8931. if (i915.use_mmio_flip < 0)
  8932. return false;
  8933. else if (i915.use_mmio_flip > 0)
  8934. return true;
  8935. else if (i915.enable_execlists)
  8936. return true;
  8937. else
  8938. return ring != i915_gem_request_get_ring(obj->last_read_req);
  8939. }
  8940. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  8941. {
  8942. struct drm_device *dev = intel_crtc->base.dev;
  8943. struct drm_i915_private *dev_priv = dev->dev_private;
  8944. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8945. const enum pipe pipe = intel_crtc->pipe;
  8946. u32 ctl, stride;
  8947. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8948. ctl &= ~PLANE_CTL_TILED_MASK;
  8949. switch (fb->modifier[0]) {
  8950. case DRM_FORMAT_MOD_NONE:
  8951. break;
  8952. case I915_FORMAT_MOD_X_TILED:
  8953. ctl |= PLANE_CTL_TILED_X;
  8954. break;
  8955. case I915_FORMAT_MOD_Y_TILED:
  8956. ctl |= PLANE_CTL_TILED_Y;
  8957. break;
  8958. case I915_FORMAT_MOD_Yf_TILED:
  8959. ctl |= PLANE_CTL_TILED_YF;
  8960. break;
  8961. default:
  8962. MISSING_CASE(fb->modifier[0]);
  8963. }
  8964. /*
  8965. * The stride is either expressed as a multiple of 64 bytes chunks for
  8966. * linear buffers or in number of tiles for tiled buffers.
  8967. */
  8968. stride = fb->pitches[0] /
  8969. intel_fb_stride_alignment(dev, fb->modifier[0],
  8970. fb->pixel_format);
  8971. /*
  8972. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8973. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8974. */
  8975. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8976. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8977. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  8978. POSTING_READ(PLANE_SURF(pipe, 0));
  8979. }
  8980. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  8981. {
  8982. struct drm_device *dev = intel_crtc->base.dev;
  8983. struct drm_i915_private *dev_priv = dev->dev_private;
  8984. struct intel_framebuffer *intel_fb =
  8985. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8986. struct drm_i915_gem_object *obj = intel_fb->obj;
  8987. u32 dspcntr;
  8988. u32 reg;
  8989. reg = DSPCNTR(intel_crtc->plane);
  8990. dspcntr = I915_READ(reg);
  8991. if (obj->tiling_mode != I915_TILING_NONE)
  8992. dspcntr |= DISPPLANE_TILED;
  8993. else
  8994. dspcntr &= ~DISPPLANE_TILED;
  8995. I915_WRITE(reg, dspcntr);
  8996. I915_WRITE(DSPSURF(intel_crtc->plane),
  8997. intel_crtc->unpin_work->gtt_offset);
  8998. POSTING_READ(DSPSURF(intel_crtc->plane));
  8999. }
  9000. /*
  9001. * XXX: This is the temporary way to update the plane registers until we get
  9002. * around to using the usual plane update functions for MMIO flips
  9003. */
  9004. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9005. {
  9006. struct drm_device *dev = intel_crtc->base.dev;
  9007. bool atomic_update;
  9008. u32 start_vbl_count;
  9009. intel_mark_page_flip_active(intel_crtc);
  9010. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9011. if (INTEL_INFO(dev)->gen >= 9)
  9012. skl_do_mmio_flip(intel_crtc);
  9013. else
  9014. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9015. ilk_do_mmio_flip(intel_crtc);
  9016. if (atomic_update)
  9017. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9018. }
  9019. static void intel_mmio_flip_work_func(struct work_struct *work)
  9020. {
  9021. struct intel_crtc *crtc =
  9022. container_of(work, struct intel_crtc, mmio_flip.work);
  9023. struct intel_mmio_flip *mmio_flip;
  9024. mmio_flip = &crtc->mmio_flip;
  9025. if (mmio_flip->req)
  9026. WARN_ON(__i915_wait_request(mmio_flip->req,
  9027. crtc->reset_counter,
  9028. false, NULL, NULL) != 0);
  9029. intel_do_mmio_flip(crtc);
  9030. if (mmio_flip->req) {
  9031. mutex_lock(&crtc->base.dev->struct_mutex);
  9032. i915_gem_request_assign(&mmio_flip->req, NULL);
  9033. mutex_unlock(&crtc->base.dev->struct_mutex);
  9034. }
  9035. }
  9036. static int intel_queue_mmio_flip(struct drm_device *dev,
  9037. struct drm_crtc *crtc,
  9038. struct drm_framebuffer *fb,
  9039. struct drm_i915_gem_object *obj,
  9040. struct intel_engine_cs *ring,
  9041. uint32_t flags)
  9042. {
  9043. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9044. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  9045. obj->last_write_req);
  9046. schedule_work(&intel_crtc->mmio_flip.work);
  9047. return 0;
  9048. }
  9049. static int intel_default_queue_flip(struct drm_device *dev,
  9050. struct drm_crtc *crtc,
  9051. struct drm_framebuffer *fb,
  9052. struct drm_i915_gem_object *obj,
  9053. struct intel_engine_cs *ring,
  9054. uint32_t flags)
  9055. {
  9056. return -ENODEV;
  9057. }
  9058. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9059. struct drm_crtc *crtc)
  9060. {
  9061. struct drm_i915_private *dev_priv = dev->dev_private;
  9062. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9063. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9064. u32 addr;
  9065. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9066. return true;
  9067. if (!work->enable_stall_check)
  9068. return false;
  9069. if (work->flip_ready_vblank == 0) {
  9070. if (work->flip_queued_req &&
  9071. !i915_gem_request_completed(work->flip_queued_req, true))
  9072. return false;
  9073. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9074. }
  9075. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9076. return false;
  9077. /* Potential stall - if we see that the flip has happened,
  9078. * assume a missed interrupt. */
  9079. if (INTEL_INFO(dev)->gen >= 4)
  9080. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9081. else
  9082. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9083. /* There is a potential issue here with a false positive after a flip
  9084. * to the same address. We could address this by checking for a
  9085. * non-incrementing frame counter.
  9086. */
  9087. return addr == work->gtt_offset;
  9088. }
  9089. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9090. {
  9091. struct drm_i915_private *dev_priv = dev->dev_private;
  9092. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9093. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9094. struct intel_unpin_work *work;
  9095. WARN_ON(!in_interrupt());
  9096. if (crtc == NULL)
  9097. return;
  9098. spin_lock(&dev->event_lock);
  9099. work = intel_crtc->unpin_work;
  9100. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9101. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9102. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9103. page_flip_completed(intel_crtc);
  9104. work = NULL;
  9105. }
  9106. if (work != NULL &&
  9107. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9108. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9109. spin_unlock(&dev->event_lock);
  9110. }
  9111. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9112. struct drm_framebuffer *fb,
  9113. struct drm_pending_vblank_event *event,
  9114. uint32_t page_flip_flags)
  9115. {
  9116. struct drm_device *dev = crtc->dev;
  9117. struct drm_i915_private *dev_priv = dev->dev_private;
  9118. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9119. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9120. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9121. struct drm_plane *primary = crtc->primary;
  9122. enum pipe pipe = intel_crtc->pipe;
  9123. struct intel_unpin_work *work;
  9124. struct intel_engine_cs *ring;
  9125. bool mmio_flip;
  9126. int ret;
  9127. /*
  9128. * drm_mode_page_flip_ioctl() should already catch this, but double
  9129. * check to be safe. In the future we may enable pageflipping from
  9130. * a disabled primary plane.
  9131. */
  9132. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9133. return -EBUSY;
  9134. /* Can't change pixel format via MI display flips. */
  9135. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9136. return -EINVAL;
  9137. /*
  9138. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9139. * Note that pitch changes could also affect these register.
  9140. */
  9141. if (INTEL_INFO(dev)->gen > 3 &&
  9142. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9143. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9144. return -EINVAL;
  9145. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9146. goto out_hang;
  9147. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9148. if (work == NULL)
  9149. return -ENOMEM;
  9150. work->event = event;
  9151. work->crtc = crtc;
  9152. work->old_fb = old_fb;
  9153. INIT_WORK(&work->work, intel_unpin_work_fn);
  9154. ret = drm_crtc_vblank_get(crtc);
  9155. if (ret)
  9156. goto free_work;
  9157. /* We borrow the event spin lock for protecting unpin_work */
  9158. spin_lock_irq(&dev->event_lock);
  9159. if (intel_crtc->unpin_work) {
  9160. /* Before declaring the flip queue wedged, check if
  9161. * the hardware completed the operation behind our backs.
  9162. */
  9163. if (__intel_pageflip_stall_check(dev, crtc)) {
  9164. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9165. page_flip_completed(intel_crtc);
  9166. } else {
  9167. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9168. spin_unlock_irq(&dev->event_lock);
  9169. drm_crtc_vblank_put(crtc);
  9170. kfree(work);
  9171. return -EBUSY;
  9172. }
  9173. }
  9174. intel_crtc->unpin_work = work;
  9175. spin_unlock_irq(&dev->event_lock);
  9176. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9177. flush_workqueue(dev_priv->wq);
  9178. /* Reference the objects for the scheduled work. */
  9179. drm_framebuffer_reference(work->old_fb);
  9180. drm_gem_object_reference(&obj->base);
  9181. crtc->primary->fb = fb;
  9182. update_state_fb(crtc->primary);
  9183. work->pending_flip_obj = obj;
  9184. ret = i915_mutex_lock_interruptible(dev);
  9185. if (ret)
  9186. goto cleanup;
  9187. atomic_inc(&intel_crtc->unpin_work_count);
  9188. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9189. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9190. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9191. if (IS_VALLEYVIEW(dev)) {
  9192. ring = &dev_priv->ring[BCS];
  9193. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9194. /* vlv: DISPLAY_FLIP fails to change tiling */
  9195. ring = NULL;
  9196. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9197. ring = &dev_priv->ring[BCS];
  9198. } else if (INTEL_INFO(dev)->gen >= 7) {
  9199. ring = i915_gem_request_get_ring(obj->last_read_req);
  9200. if (ring == NULL || ring->id != RCS)
  9201. ring = &dev_priv->ring[BCS];
  9202. } else {
  9203. ring = &dev_priv->ring[RCS];
  9204. }
  9205. mmio_flip = use_mmio_flip(ring, obj);
  9206. /* When using CS flips, we want to emit semaphores between rings.
  9207. * However, when using mmio flips we will create a task to do the
  9208. * synchronisation, so all we want here is to pin the framebuffer
  9209. * into the display plane and skip any waits.
  9210. */
  9211. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9212. crtc->primary->state,
  9213. mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
  9214. if (ret)
  9215. goto cleanup_pending;
  9216. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9217. + intel_crtc->dspaddr_offset;
  9218. if (mmio_flip) {
  9219. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9220. page_flip_flags);
  9221. if (ret)
  9222. goto cleanup_unpin;
  9223. i915_gem_request_assign(&work->flip_queued_req,
  9224. obj->last_write_req);
  9225. } else {
  9226. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  9227. page_flip_flags);
  9228. if (ret)
  9229. goto cleanup_unpin;
  9230. i915_gem_request_assign(&work->flip_queued_req,
  9231. intel_ring_get_request(ring));
  9232. }
  9233. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9234. work->enable_stall_check = true;
  9235. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9236. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9237. intel_fbc_disable(dev);
  9238. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9239. mutex_unlock(&dev->struct_mutex);
  9240. trace_i915_flip_request(intel_crtc->plane, obj);
  9241. return 0;
  9242. cleanup_unpin:
  9243. intel_unpin_fb_obj(fb, crtc->primary->state);
  9244. cleanup_pending:
  9245. atomic_dec(&intel_crtc->unpin_work_count);
  9246. mutex_unlock(&dev->struct_mutex);
  9247. cleanup:
  9248. crtc->primary->fb = old_fb;
  9249. update_state_fb(crtc->primary);
  9250. drm_gem_object_unreference_unlocked(&obj->base);
  9251. drm_framebuffer_unreference(work->old_fb);
  9252. spin_lock_irq(&dev->event_lock);
  9253. intel_crtc->unpin_work = NULL;
  9254. spin_unlock_irq(&dev->event_lock);
  9255. drm_crtc_vblank_put(crtc);
  9256. free_work:
  9257. kfree(work);
  9258. if (ret == -EIO) {
  9259. out_hang:
  9260. ret = intel_plane_restore(primary);
  9261. if (ret == 0 && event) {
  9262. spin_lock_irq(&dev->event_lock);
  9263. drm_send_vblank_event(dev, pipe, event);
  9264. spin_unlock_irq(&dev->event_lock);
  9265. }
  9266. }
  9267. return ret;
  9268. }
  9269. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9270. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9271. .load_lut = intel_crtc_load_lut,
  9272. .atomic_begin = intel_begin_crtc_commit,
  9273. .atomic_flush = intel_finish_crtc_commit,
  9274. };
  9275. /**
  9276. * intel_modeset_update_staged_output_state
  9277. *
  9278. * Updates the staged output configuration state, e.g. after we've read out the
  9279. * current hw state.
  9280. */
  9281. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  9282. {
  9283. struct intel_crtc *crtc;
  9284. struct intel_encoder *encoder;
  9285. struct intel_connector *connector;
  9286. for_each_intel_connector(dev, connector) {
  9287. connector->new_encoder =
  9288. to_intel_encoder(connector->base.encoder);
  9289. }
  9290. for_each_intel_encoder(dev, encoder) {
  9291. encoder->new_crtc =
  9292. to_intel_crtc(encoder->base.crtc);
  9293. }
  9294. for_each_intel_crtc(dev, crtc) {
  9295. crtc->new_enabled = crtc->base.state->enable;
  9296. }
  9297. }
  9298. /* Transitional helper to copy current connector/encoder state to
  9299. * connector->state. This is needed so that code that is partially
  9300. * converted to atomic does the right thing.
  9301. */
  9302. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9303. {
  9304. struct intel_connector *connector;
  9305. for_each_intel_connector(dev, connector) {
  9306. if (connector->base.encoder) {
  9307. connector->base.state->best_encoder =
  9308. connector->base.encoder;
  9309. connector->base.state->crtc =
  9310. connector->base.encoder->crtc;
  9311. } else {
  9312. connector->base.state->best_encoder = NULL;
  9313. connector->base.state->crtc = NULL;
  9314. }
  9315. }
  9316. }
  9317. /**
  9318. * intel_modeset_commit_output_state
  9319. *
  9320. * This function copies the stage display pipe configuration to the real one.
  9321. */
  9322. static void intel_modeset_commit_output_state(struct drm_device *dev)
  9323. {
  9324. struct intel_crtc *crtc;
  9325. struct intel_encoder *encoder;
  9326. struct intel_connector *connector;
  9327. for_each_intel_connector(dev, connector) {
  9328. connector->base.encoder = &connector->new_encoder->base;
  9329. }
  9330. for_each_intel_encoder(dev, encoder) {
  9331. encoder->base.crtc = &encoder->new_crtc->base;
  9332. }
  9333. for_each_intel_crtc(dev, crtc) {
  9334. crtc->base.state->enable = crtc->new_enabled;
  9335. crtc->base.enabled = crtc->new_enabled;
  9336. }
  9337. intel_modeset_update_connector_atomic_state(dev);
  9338. }
  9339. static void
  9340. connected_sink_compute_bpp(struct intel_connector *connector,
  9341. struct intel_crtc_state *pipe_config)
  9342. {
  9343. int bpp = pipe_config->pipe_bpp;
  9344. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9345. connector->base.base.id,
  9346. connector->base.name);
  9347. /* Don't use an invalid EDID bpc value */
  9348. if (connector->base.display_info.bpc &&
  9349. connector->base.display_info.bpc * 3 < bpp) {
  9350. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9351. bpp, connector->base.display_info.bpc*3);
  9352. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9353. }
  9354. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9355. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9356. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9357. bpp);
  9358. pipe_config->pipe_bpp = 24;
  9359. }
  9360. }
  9361. static int
  9362. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9363. struct intel_crtc_state *pipe_config)
  9364. {
  9365. struct drm_device *dev = crtc->base.dev;
  9366. struct drm_atomic_state *state;
  9367. struct intel_connector *connector;
  9368. int bpp, i;
  9369. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9370. bpp = 10*3;
  9371. else if (INTEL_INFO(dev)->gen >= 5)
  9372. bpp = 12*3;
  9373. else
  9374. bpp = 8*3;
  9375. pipe_config->pipe_bpp = bpp;
  9376. state = pipe_config->base.state;
  9377. /* Clamp display bpp to EDID value */
  9378. for (i = 0; i < state->num_connector; i++) {
  9379. if (!state->connectors[i])
  9380. continue;
  9381. connector = to_intel_connector(state->connectors[i]);
  9382. if (state->connector_states[i]->crtc != &crtc->base)
  9383. continue;
  9384. connected_sink_compute_bpp(connector, pipe_config);
  9385. }
  9386. return bpp;
  9387. }
  9388. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9389. {
  9390. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9391. "type: 0x%x flags: 0x%x\n",
  9392. mode->crtc_clock,
  9393. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9394. mode->crtc_hsync_end, mode->crtc_htotal,
  9395. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9396. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9397. }
  9398. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9399. struct intel_crtc_state *pipe_config,
  9400. const char *context)
  9401. {
  9402. struct drm_device *dev = crtc->base.dev;
  9403. struct drm_plane *plane;
  9404. struct intel_plane *intel_plane;
  9405. struct intel_plane_state *state;
  9406. struct drm_framebuffer *fb;
  9407. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9408. context, pipe_config, pipe_name(crtc->pipe));
  9409. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9410. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9411. pipe_config->pipe_bpp, pipe_config->dither);
  9412. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9413. pipe_config->has_pch_encoder,
  9414. pipe_config->fdi_lanes,
  9415. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9416. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9417. pipe_config->fdi_m_n.tu);
  9418. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9419. pipe_config->has_dp_encoder,
  9420. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9421. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9422. pipe_config->dp_m_n.tu);
  9423. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9424. pipe_config->has_dp_encoder,
  9425. pipe_config->dp_m2_n2.gmch_m,
  9426. pipe_config->dp_m2_n2.gmch_n,
  9427. pipe_config->dp_m2_n2.link_m,
  9428. pipe_config->dp_m2_n2.link_n,
  9429. pipe_config->dp_m2_n2.tu);
  9430. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9431. pipe_config->has_audio,
  9432. pipe_config->has_infoframe);
  9433. DRM_DEBUG_KMS("requested mode:\n");
  9434. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9435. DRM_DEBUG_KMS("adjusted mode:\n");
  9436. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9437. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9438. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9439. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9440. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9441. DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
  9442. DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
  9443. DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
  9444. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9445. pipe_config->gmch_pfit.control,
  9446. pipe_config->gmch_pfit.pgm_ratios,
  9447. pipe_config->gmch_pfit.lvds_border_bits);
  9448. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9449. pipe_config->pch_pfit.pos,
  9450. pipe_config->pch_pfit.size,
  9451. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  9452. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  9453. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  9454. DRM_DEBUG_KMS("planes on this crtc\n");
  9455. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9456. intel_plane = to_intel_plane(plane);
  9457. if (intel_plane->pipe != crtc->pipe)
  9458. continue;
  9459. state = to_intel_plane_state(plane->state);
  9460. fb = state->base.fb;
  9461. if (!fb) {
  9462. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  9463. "disabled, scaler_id = %d\n",
  9464. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9465. plane->base.id, intel_plane->pipe,
  9466. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  9467. drm_plane_index(plane), state->scaler_id);
  9468. continue;
  9469. }
  9470. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  9471. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9472. plane->base.id, intel_plane->pipe,
  9473. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  9474. drm_plane_index(plane));
  9475. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  9476. fb->base.id, fb->width, fb->height, fb->pixel_format);
  9477. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  9478. state->scaler_id,
  9479. state->src.x1 >> 16, state->src.y1 >> 16,
  9480. drm_rect_width(&state->src) >> 16,
  9481. drm_rect_height(&state->src) >> 16,
  9482. state->dst.x1, state->dst.y1,
  9483. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  9484. }
  9485. }
  9486. static bool encoders_cloneable(const struct intel_encoder *a,
  9487. const struct intel_encoder *b)
  9488. {
  9489. /* masks could be asymmetric, so check both ways */
  9490. return a == b || (a->cloneable & (1 << b->type) &&
  9491. b->cloneable & (1 << a->type));
  9492. }
  9493. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9494. struct intel_crtc *crtc,
  9495. struct intel_encoder *encoder)
  9496. {
  9497. struct intel_encoder *source_encoder;
  9498. struct drm_connector_state *connector_state;
  9499. int i;
  9500. for (i = 0; i < state->num_connector; i++) {
  9501. if (!state->connectors[i])
  9502. continue;
  9503. connector_state = state->connector_states[i];
  9504. if (connector_state->crtc != &crtc->base)
  9505. continue;
  9506. source_encoder =
  9507. to_intel_encoder(connector_state->best_encoder);
  9508. if (!encoders_cloneable(encoder, source_encoder))
  9509. return false;
  9510. }
  9511. return true;
  9512. }
  9513. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9514. struct intel_crtc *crtc)
  9515. {
  9516. struct intel_encoder *encoder;
  9517. struct drm_connector_state *connector_state;
  9518. int i;
  9519. for (i = 0; i < state->num_connector; i++) {
  9520. if (!state->connectors[i])
  9521. continue;
  9522. connector_state = state->connector_states[i];
  9523. if (connector_state->crtc != &crtc->base)
  9524. continue;
  9525. encoder = to_intel_encoder(connector_state->best_encoder);
  9526. if (!check_single_encoder_cloning(state, crtc, encoder))
  9527. return false;
  9528. }
  9529. return true;
  9530. }
  9531. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9532. {
  9533. struct drm_device *dev = state->dev;
  9534. struct intel_encoder *encoder;
  9535. struct drm_connector_state *connector_state;
  9536. unsigned int used_ports = 0;
  9537. int i;
  9538. /*
  9539. * Walk the connector list instead of the encoder
  9540. * list to detect the problem on ddi platforms
  9541. * where there's just one encoder per digital port.
  9542. */
  9543. for (i = 0; i < state->num_connector; i++) {
  9544. if (!state->connectors[i])
  9545. continue;
  9546. connector_state = state->connector_states[i];
  9547. if (!connector_state->best_encoder)
  9548. continue;
  9549. encoder = to_intel_encoder(connector_state->best_encoder);
  9550. WARN_ON(!connector_state->crtc);
  9551. switch (encoder->type) {
  9552. unsigned int port_mask;
  9553. case INTEL_OUTPUT_UNKNOWN:
  9554. if (WARN_ON(!HAS_DDI(dev)))
  9555. break;
  9556. case INTEL_OUTPUT_DISPLAYPORT:
  9557. case INTEL_OUTPUT_HDMI:
  9558. case INTEL_OUTPUT_EDP:
  9559. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9560. /* the same port mustn't appear more than once */
  9561. if (used_ports & port_mask)
  9562. return false;
  9563. used_ports |= port_mask;
  9564. default:
  9565. break;
  9566. }
  9567. }
  9568. return true;
  9569. }
  9570. static void
  9571. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9572. {
  9573. struct drm_crtc_state tmp_state;
  9574. struct intel_crtc_scaler_state scaler_state;
  9575. /* Clear only the intel specific part of the crtc state excluding scalers */
  9576. tmp_state = crtc_state->base;
  9577. scaler_state = crtc_state->scaler_state;
  9578. memset(crtc_state, 0, sizeof *crtc_state);
  9579. crtc_state->base = tmp_state;
  9580. crtc_state->scaler_state = scaler_state;
  9581. }
  9582. static struct intel_crtc_state *
  9583. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9584. struct drm_display_mode *mode,
  9585. struct drm_atomic_state *state)
  9586. {
  9587. struct intel_encoder *encoder;
  9588. struct intel_connector *connector;
  9589. struct drm_connector_state *connector_state;
  9590. struct intel_crtc_state *pipe_config;
  9591. int base_bpp, ret = -EINVAL;
  9592. int i;
  9593. bool retry = true;
  9594. if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
  9595. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9596. return ERR_PTR(-EINVAL);
  9597. }
  9598. if (!check_digital_port_conflicts(state)) {
  9599. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9600. return ERR_PTR(-EINVAL);
  9601. }
  9602. pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
  9603. if (IS_ERR(pipe_config))
  9604. return pipe_config;
  9605. clear_intel_crtc_state(pipe_config);
  9606. pipe_config->base.crtc = crtc;
  9607. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  9608. drm_mode_copy(&pipe_config->base.mode, mode);
  9609. pipe_config->cpu_transcoder =
  9610. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9611. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  9612. /*
  9613. * Sanitize sync polarity flags based on requested ones. If neither
  9614. * positive or negative polarity is requested, treat this as meaning
  9615. * negative polarity.
  9616. */
  9617. if (!(pipe_config->base.adjusted_mode.flags &
  9618. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9619. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9620. if (!(pipe_config->base.adjusted_mode.flags &
  9621. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9622. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9623. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  9624. * plane pixel format and any sink constraints into account. Returns the
  9625. * source plane bpp so that dithering can be selected on mismatches
  9626. * after encoders and crtc also have had their say. */
  9627. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9628. pipe_config);
  9629. if (base_bpp < 0)
  9630. goto fail;
  9631. /*
  9632. * Determine the real pipe dimensions. Note that stereo modes can
  9633. * increase the actual pipe size due to the frame doubling and
  9634. * insertion of additional space for blanks between the frame. This
  9635. * is stored in the crtc timings. We use the requested mode to do this
  9636. * computation to clearly distinguish it from the adjusted mode, which
  9637. * can be changed by the connectors in the below retry loop.
  9638. */
  9639. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  9640. &pipe_config->pipe_src_w,
  9641. &pipe_config->pipe_src_h);
  9642. encoder_retry:
  9643. /* Ensure the port clock defaults are reset when retrying. */
  9644. pipe_config->port_clock = 0;
  9645. pipe_config->pixel_multiplier = 1;
  9646. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9647. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9648. CRTC_STEREO_DOUBLE);
  9649. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9650. * adjust it according to limitations or connector properties, and also
  9651. * a chance to reject the mode entirely.
  9652. */
  9653. for (i = 0; i < state->num_connector; i++) {
  9654. connector = to_intel_connector(state->connectors[i]);
  9655. if (!connector)
  9656. continue;
  9657. connector_state = state->connector_states[i];
  9658. if (connector_state->crtc != crtc)
  9659. continue;
  9660. encoder = to_intel_encoder(connector_state->best_encoder);
  9661. if (!(encoder->compute_config(encoder, pipe_config))) {
  9662. DRM_DEBUG_KMS("Encoder config failure\n");
  9663. goto fail;
  9664. }
  9665. }
  9666. /* Set default port clock if not overwritten by the encoder. Needs to be
  9667. * done afterwards in case the encoder adjusts the mode. */
  9668. if (!pipe_config->port_clock)
  9669. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9670. * pipe_config->pixel_multiplier;
  9671. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9672. if (ret < 0) {
  9673. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9674. goto fail;
  9675. }
  9676. if (ret == RETRY) {
  9677. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9678. ret = -EINVAL;
  9679. goto fail;
  9680. }
  9681. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9682. retry = false;
  9683. goto encoder_retry;
  9684. }
  9685. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  9686. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  9687. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9688. return pipe_config;
  9689. fail:
  9690. return ERR_PTR(ret);
  9691. }
  9692. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  9693. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  9694. static void
  9695. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  9696. unsigned *prepare_pipes, unsigned *disable_pipes)
  9697. {
  9698. struct intel_crtc *intel_crtc;
  9699. struct drm_device *dev = crtc->dev;
  9700. struct intel_encoder *encoder;
  9701. struct intel_connector *connector;
  9702. struct drm_crtc *tmp_crtc;
  9703. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  9704. /* Check which crtcs have changed outputs connected to them, these need
  9705. * to be part of the prepare_pipes mask. We don't (yet) support global
  9706. * modeset across multiple crtcs, so modeset_pipes will only have one
  9707. * bit set at most. */
  9708. for_each_intel_connector(dev, connector) {
  9709. if (connector->base.encoder == &connector->new_encoder->base)
  9710. continue;
  9711. if (connector->base.encoder) {
  9712. tmp_crtc = connector->base.encoder->crtc;
  9713. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  9714. }
  9715. if (connector->new_encoder)
  9716. *prepare_pipes |=
  9717. 1 << connector->new_encoder->new_crtc->pipe;
  9718. }
  9719. for_each_intel_encoder(dev, encoder) {
  9720. if (encoder->base.crtc == &encoder->new_crtc->base)
  9721. continue;
  9722. if (encoder->base.crtc) {
  9723. tmp_crtc = encoder->base.crtc;
  9724. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  9725. }
  9726. if (encoder->new_crtc)
  9727. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  9728. }
  9729. /* Check for pipes that will be enabled/disabled ... */
  9730. for_each_intel_crtc(dev, intel_crtc) {
  9731. if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
  9732. continue;
  9733. if (!intel_crtc->new_enabled)
  9734. *disable_pipes |= 1 << intel_crtc->pipe;
  9735. else
  9736. *prepare_pipes |= 1 << intel_crtc->pipe;
  9737. }
  9738. /* set_mode is also used to update properties on life display pipes. */
  9739. intel_crtc = to_intel_crtc(crtc);
  9740. if (intel_crtc->new_enabled)
  9741. *prepare_pipes |= 1 << intel_crtc->pipe;
  9742. /*
  9743. * For simplicity do a full modeset on any pipe where the output routing
  9744. * changed. We could be more clever, but that would require us to be
  9745. * more careful with calling the relevant encoder->mode_set functions.
  9746. */
  9747. if (*prepare_pipes)
  9748. *modeset_pipes = *prepare_pipes;
  9749. /* ... and mask these out. */
  9750. *modeset_pipes &= ~(*disable_pipes);
  9751. *prepare_pipes &= ~(*disable_pipes);
  9752. /*
  9753. * HACK: We don't (yet) fully support global modesets. intel_set_config
  9754. * obies this rule, but the modeset restore mode of
  9755. * intel_modeset_setup_hw_state does not.
  9756. */
  9757. *modeset_pipes &= 1 << intel_crtc->pipe;
  9758. *prepare_pipes &= 1 << intel_crtc->pipe;
  9759. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  9760. *modeset_pipes, *prepare_pipes, *disable_pipes);
  9761. }
  9762. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  9763. {
  9764. struct drm_encoder *encoder;
  9765. struct drm_device *dev = crtc->dev;
  9766. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  9767. if (encoder->crtc == crtc)
  9768. return true;
  9769. return false;
  9770. }
  9771. static void
  9772. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  9773. {
  9774. struct drm_i915_private *dev_priv = dev->dev_private;
  9775. struct intel_encoder *intel_encoder;
  9776. struct intel_crtc *intel_crtc;
  9777. struct drm_connector *connector;
  9778. intel_shared_dpll_commit(dev_priv);
  9779. for_each_intel_encoder(dev, intel_encoder) {
  9780. if (!intel_encoder->base.crtc)
  9781. continue;
  9782. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  9783. if (prepare_pipes & (1 << intel_crtc->pipe))
  9784. intel_encoder->connectors_active = false;
  9785. }
  9786. intel_modeset_commit_output_state(dev);
  9787. /* Double check state. */
  9788. for_each_intel_crtc(dev, intel_crtc) {
  9789. WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
  9790. }
  9791. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9792. if (!connector->encoder || !connector->encoder->crtc)
  9793. continue;
  9794. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  9795. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  9796. struct drm_property *dpms_property =
  9797. dev->mode_config.dpms_property;
  9798. connector->dpms = DRM_MODE_DPMS_ON;
  9799. drm_object_property_set_value(&connector->base,
  9800. dpms_property,
  9801. DRM_MODE_DPMS_ON);
  9802. intel_encoder = to_intel_encoder(connector->encoder);
  9803. intel_encoder->connectors_active = true;
  9804. }
  9805. }
  9806. }
  9807. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9808. {
  9809. int diff;
  9810. if (clock1 == clock2)
  9811. return true;
  9812. if (!clock1 || !clock2)
  9813. return false;
  9814. diff = abs(clock1 - clock2);
  9815. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9816. return true;
  9817. return false;
  9818. }
  9819. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  9820. list_for_each_entry((intel_crtc), \
  9821. &(dev)->mode_config.crtc_list, \
  9822. base.head) \
  9823. if (mask & (1 <<(intel_crtc)->pipe))
  9824. static bool
  9825. intel_pipe_config_compare(struct drm_device *dev,
  9826. struct intel_crtc_state *current_config,
  9827. struct intel_crtc_state *pipe_config)
  9828. {
  9829. #define PIPE_CONF_CHECK_X(name) \
  9830. if (current_config->name != pipe_config->name) { \
  9831. DRM_ERROR("mismatch in " #name " " \
  9832. "(expected 0x%08x, found 0x%08x)\n", \
  9833. current_config->name, \
  9834. pipe_config->name); \
  9835. return false; \
  9836. }
  9837. #define PIPE_CONF_CHECK_I(name) \
  9838. if (current_config->name != pipe_config->name) { \
  9839. DRM_ERROR("mismatch in " #name " " \
  9840. "(expected %i, found %i)\n", \
  9841. current_config->name, \
  9842. pipe_config->name); \
  9843. return false; \
  9844. }
  9845. /* This is required for BDW+ where there is only one set of registers for
  9846. * switching between high and low RR.
  9847. * This macro can be used whenever a comparison has to be made between one
  9848. * hw state and multiple sw state variables.
  9849. */
  9850. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  9851. if ((current_config->name != pipe_config->name) && \
  9852. (current_config->alt_name != pipe_config->name)) { \
  9853. DRM_ERROR("mismatch in " #name " " \
  9854. "(expected %i or %i, found %i)\n", \
  9855. current_config->name, \
  9856. current_config->alt_name, \
  9857. pipe_config->name); \
  9858. return false; \
  9859. }
  9860. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9861. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9862. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  9863. "(expected %i, found %i)\n", \
  9864. current_config->name & (mask), \
  9865. pipe_config->name & (mask)); \
  9866. return false; \
  9867. }
  9868. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9869. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9870. DRM_ERROR("mismatch in " #name " " \
  9871. "(expected %i, found %i)\n", \
  9872. current_config->name, \
  9873. pipe_config->name); \
  9874. return false; \
  9875. }
  9876. #define PIPE_CONF_QUIRK(quirk) \
  9877. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9878. PIPE_CONF_CHECK_I(cpu_transcoder);
  9879. PIPE_CONF_CHECK_I(has_pch_encoder);
  9880. PIPE_CONF_CHECK_I(fdi_lanes);
  9881. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  9882. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  9883. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  9884. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  9885. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  9886. PIPE_CONF_CHECK_I(has_dp_encoder);
  9887. if (INTEL_INFO(dev)->gen < 8) {
  9888. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  9889. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  9890. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  9891. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  9892. PIPE_CONF_CHECK_I(dp_m_n.tu);
  9893. if (current_config->has_drrs) {
  9894. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  9895. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  9896. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  9897. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  9898. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  9899. }
  9900. } else {
  9901. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  9902. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  9903. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  9904. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  9905. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  9906. }
  9907. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9908. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9909. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9910. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9911. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9912. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9913. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9914. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9915. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9916. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9917. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9918. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9919. PIPE_CONF_CHECK_I(pixel_multiplier);
  9920. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9921. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  9922. IS_VALLEYVIEW(dev))
  9923. PIPE_CONF_CHECK_I(limited_color_range);
  9924. PIPE_CONF_CHECK_I(has_infoframe);
  9925. PIPE_CONF_CHECK_I(has_audio);
  9926. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9927. DRM_MODE_FLAG_INTERLACE);
  9928. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9929. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9930. DRM_MODE_FLAG_PHSYNC);
  9931. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9932. DRM_MODE_FLAG_NHSYNC);
  9933. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9934. DRM_MODE_FLAG_PVSYNC);
  9935. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9936. DRM_MODE_FLAG_NVSYNC);
  9937. }
  9938. PIPE_CONF_CHECK_I(pipe_src_w);
  9939. PIPE_CONF_CHECK_I(pipe_src_h);
  9940. /*
  9941. * FIXME: BIOS likes to set up a cloned config with lvds+external
  9942. * screen. Since we don't yet re-compute the pipe config when moving
  9943. * just the lvds port away to another pipe the sw tracking won't match.
  9944. *
  9945. * Proper atomic modesets with recomputed global state will fix this.
  9946. * Until then just don't check gmch state for inherited modes.
  9947. */
  9948. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  9949. PIPE_CONF_CHECK_I(gmch_pfit.control);
  9950. /* pfit ratios are autocomputed by the hw on gen4+ */
  9951. if (INTEL_INFO(dev)->gen < 4)
  9952. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  9953. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  9954. }
  9955. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9956. if (current_config->pch_pfit.enabled) {
  9957. PIPE_CONF_CHECK_I(pch_pfit.pos);
  9958. PIPE_CONF_CHECK_I(pch_pfit.size);
  9959. }
  9960. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9961. /* BDW+ don't expose a synchronous way to read the state */
  9962. if (IS_HASWELL(dev))
  9963. PIPE_CONF_CHECK_I(ips_enabled);
  9964. PIPE_CONF_CHECK_I(double_wide);
  9965. PIPE_CONF_CHECK_X(ddi_pll_sel);
  9966. PIPE_CONF_CHECK_I(shared_dpll);
  9967. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9968. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9969. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9970. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9971. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9972. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9973. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9974. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9975. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  9976. PIPE_CONF_CHECK_I(pipe_bpp);
  9977. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9978. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9979. #undef PIPE_CONF_CHECK_X
  9980. #undef PIPE_CONF_CHECK_I
  9981. #undef PIPE_CONF_CHECK_I_ALT
  9982. #undef PIPE_CONF_CHECK_FLAGS
  9983. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9984. #undef PIPE_CONF_QUIRK
  9985. return true;
  9986. }
  9987. static void check_wm_state(struct drm_device *dev)
  9988. {
  9989. struct drm_i915_private *dev_priv = dev->dev_private;
  9990. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9991. struct intel_crtc *intel_crtc;
  9992. int plane;
  9993. if (INTEL_INFO(dev)->gen < 9)
  9994. return;
  9995. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9996. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9997. for_each_intel_crtc(dev, intel_crtc) {
  9998. struct skl_ddb_entry *hw_entry, *sw_entry;
  9999. const enum pipe pipe = intel_crtc->pipe;
  10000. if (!intel_crtc->active)
  10001. continue;
  10002. /* planes */
  10003. for_each_plane(dev_priv, pipe, plane) {
  10004. hw_entry = &hw_ddb.plane[pipe][plane];
  10005. sw_entry = &sw_ddb->plane[pipe][plane];
  10006. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10007. continue;
  10008. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10009. "(expected (%u,%u), found (%u,%u))\n",
  10010. pipe_name(pipe), plane + 1,
  10011. sw_entry->start, sw_entry->end,
  10012. hw_entry->start, hw_entry->end);
  10013. }
  10014. /* cursor */
  10015. hw_entry = &hw_ddb.cursor[pipe];
  10016. sw_entry = &sw_ddb->cursor[pipe];
  10017. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10018. continue;
  10019. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10020. "(expected (%u,%u), found (%u,%u))\n",
  10021. pipe_name(pipe),
  10022. sw_entry->start, sw_entry->end,
  10023. hw_entry->start, hw_entry->end);
  10024. }
  10025. }
  10026. static void
  10027. check_connector_state(struct drm_device *dev)
  10028. {
  10029. struct intel_connector *connector;
  10030. for_each_intel_connector(dev, connector) {
  10031. /* This also checks the encoder/connector hw state with the
  10032. * ->get_hw_state callbacks. */
  10033. intel_connector_check_state(connector);
  10034. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10035. "connector's staged encoder doesn't match current encoder\n");
  10036. }
  10037. }
  10038. static void
  10039. check_encoder_state(struct drm_device *dev)
  10040. {
  10041. struct intel_encoder *encoder;
  10042. struct intel_connector *connector;
  10043. for_each_intel_encoder(dev, encoder) {
  10044. bool enabled = false;
  10045. bool active = false;
  10046. enum pipe pipe, tracked_pipe;
  10047. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10048. encoder->base.base.id,
  10049. encoder->base.name);
  10050. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10051. "encoder's stage crtc doesn't match current crtc\n");
  10052. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10053. "encoder's active_connectors set, but no crtc\n");
  10054. for_each_intel_connector(dev, connector) {
  10055. if (connector->base.encoder != &encoder->base)
  10056. continue;
  10057. enabled = true;
  10058. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10059. active = true;
  10060. }
  10061. /*
  10062. * for MST connectors if we unplug the connector is gone
  10063. * away but the encoder is still connected to a crtc
  10064. * until a modeset happens in response to the hotplug.
  10065. */
  10066. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10067. continue;
  10068. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10069. "encoder's enabled state mismatch "
  10070. "(expected %i, found %i)\n",
  10071. !!encoder->base.crtc, enabled);
  10072. I915_STATE_WARN(active && !encoder->base.crtc,
  10073. "active encoder with no crtc\n");
  10074. I915_STATE_WARN(encoder->connectors_active != active,
  10075. "encoder's computed active state doesn't match tracked active state "
  10076. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10077. active = encoder->get_hw_state(encoder, &pipe);
  10078. I915_STATE_WARN(active != encoder->connectors_active,
  10079. "encoder's hw state doesn't match sw tracking "
  10080. "(expected %i, found %i)\n",
  10081. encoder->connectors_active, active);
  10082. if (!encoder->base.crtc)
  10083. continue;
  10084. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10085. I915_STATE_WARN(active && pipe != tracked_pipe,
  10086. "active encoder's pipe doesn't match"
  10087. "(expected %i, found %i)\n",
  10088. tracked_pipe, pipe);
  10089. }
  10090. }
  10091. static void
  10092. check_crtc_state(struct drm_device *dev)
  10093. {
  10094. struct drm_i915_private *dev_priv = dev->dev_private;
  10095. struct intel_crtc *crtc;
  10096. struct intel_encoder *encoder;
  10097. struct intel_crtc_state pipe_config;
  10098. for_each_intel_crtc(dev, crtc) {
  10099. bool enabled = false;
  10100. bool active = false;
  10101. memset(&pipe_config, 0, sizeof(pipe_config));
  10102. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10103. crtc->base.base.id);
  10104. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10105. "active crtc, but not enabled in sw tracking\n");
  10106. for_each_intel_encoder(dev, encoder) {
  10107. if (encoder->base.crtc != &crtc->base)
  10108. continue;
  10109. enabled = true;
  10110. if (encoder->connectors_active)
  10111. active = true;
  10112. }
  10113. I915_STATE_WARN(active != crtc->active,
  10114. "crtc's computed active state doesn't match tracked active state "
  10115. "(expected %i, found %i)\n", active, crtc->active);
  10116. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10117. "crtc's computed enabled state doesn't match tracked enabled state "
  10118. "(expected %i, found %i)\n", enabled,
  10119. crtc->base.state->enable);
  10120. active = dev_priv->display.get_pipe_config(crtc,
  10121. &pipe_config);
  10122. /* hw state is inconsistent with the pipe quirk */
  10123. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10124. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10125. active = crtc->active;
  10126. for_each_intel_encoder(dev, encoder) {
  10127. enum pipe pipe;
  10128. if (encoder->base.crtc != &crtc->base)
  10129. continue;
  10130. if (encoder->get_hw_state(encoder, &pipe))
  10131. encoder->get_config(encoder, &pipe_config);
  10132. }
  10133. I915_STATE_WARN(crtc->active != active,
  10134. "crtc active state doesn't match with hw state "
  10135. "(expected %i, found %i)\n", crtc->active, active);
  10136. if (active &&
  10137. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10138. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10139. intel_dump_pipe_config(crtc, &pipe_config,
  10140. "[hw state]");
  10141. intel_dump_pipe_config(crtc, crtc->config,
  10142. "[sw state]");
  10143. }
  10144. }
  10145. }
  10146. static void
  10147. check_shared_dpll_state(struct drm_device *dev)
  10148. {
  10149. struct drm_i915_private *dev_priv = dev->dev_private;
  10150. struct intel_crtc *crtc;
  10151. struct intel_dpll_hw_state dpll_hw_state;
  10152. int i;
  10153. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10154. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10155. int enabled_crtcs = 0, active_crtcs = 0;
  10156. bool active;
  10157. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10158. DRM_DEBUG_KMS("%s\n", pll->name);
  10159. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10160. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10161. "more active pll users than references: %i vs %i\n",
  10162. pll->active, hweight32(pll->config.crtc_mask));
  10163. I915_STATE_WARN(pll->active && !pll->on,
  10164. "pll in active use but not on in sw tracking\n");
  10165. I915_STATE_WARN(pll->on && !pll->active,
  10166. "pll in on but not on in use in sw tracking\n");
  10167. I915_STATE_WARN(pll->on != active,
  10168. "pll on state mismatch (expected %i, found %i)\n",
  10169. pll->on, active);
  10170. for_each_intel_crtc(dev, crtc) {
  10171. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10172. enabled_crtcs++;
  10173. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10174. active_crtcs++;
  10175. }
  10176. I915_STATE_WARN(pll->active != active_crtcs,
  10177. "pll active crtcs mismatch (expected %i, found %i)\n",
  10178. pll->active, active_crtcs);
  10179. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10180. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10181. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10182. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10183. sizeof(dpll_hw_state)),
  10184. "pll hw state mismatch\n");
  10185. }
  10186. }
  10187. void
  10188. intel_modeset_check_state(struct drm_device *dev)
  10189. {
  10190. check_wm_state(dev);
  10191. check_connector_state(dev);
  10192. check_encoder_state(dev);
  10193. check_crtc_state(dev);
  10194. check_shared_dpll_state(dev);
  10195. }
  10196. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10197. int dotclock)
  10198. {
  10199. /*
  10200. * FDI already provided one idea for the dotclock.
  10201. * Yell if the encoder disagrees.
  10202. */
  10203. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10204. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10205. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10206. }
  10207. static void update_scanline_offset(struct intel_crtc *crtc)
  10208. {
  10209. struct drm_device *dev = crtc->base.dev;
  10210. /*
  10211. * The scanline counter increments at the leading edge of hsync.
  10212. *
  10213. * On most platforms it starts counting from vtotal-1 on the
  10214. * first active line. That means the scanline counter value is
  10215. * always one less than what we would expect. Ie. just after
  10216. * start of vblank, which also occurs at start of hsync (on the
  10217. * last active line), the scanline counter will read vblank_start-1.
  10218. *
  10219. * On gen2 the scanline counter starts counting from 1 instead
  10220. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10221. * to keep the value positive), instead of adding one.
  10222. *
  10223. * On HSW+ the behaviour of the scanline counter depends on the output
  10224. * type. For DP ports it behaves like most other platforms, but on HDMI
  10225. * there's an extra 1 line difference. So we need to add two instead of
  10226. * one to the value.
  10227. */
  10228. if (IS_GEN2(dev)) {
  10229. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10230. int vtotal;
  10231. vtotal = mode->crtc_vtotal;
  10232. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10233. vtotal /= 2;
  10234. crtc->scanline_offset = vtotal - 1;
  10235. } else if (HAS_DDI(dev) &&
  10236. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10237. crtc->scanline_offset = 2;
  10238. } else
  10239. crtc->scanline_offset = 1;
  10240. }
  10241. static struct intel_crtc_state *
  10242. intel_modeset_compute_config(struct drm_crtc *crtc,
  10243. struct drm_display_mode *mode,
  10244. struct drm_atomic_state *state,
  10245. unsigned *modeset_pipes,
  10246. unsigned *prepare_pipes,
  10247. unsigned *disable_pipes)
  10248. {
  10249. struct drm_device *dev = crtc->dev;
  10250. struct intel_crtc_state *pipe_config = NULL;
  10251. struct intel_crtc *intel_crtc;
  10252. int ret = 0;
  10253. ret = drm_atomic_add_affected_connectors(state, crtc);
  10254. if (ret)
  10255. return ERR_PTR(ret);
  10256. intel_modeset_affected_pipes(crtc, modeset_pipes,
  10257. prepare_pipes, disable_pipes);
  10258. for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
  10259. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10260. if (IS_ERR(pipe_config))
  10261. return pipe_config;
  10262. pipe_config->base.enable = false;
  10263. }
  10264. /*
  10265. * Note this needs changes when we start tracking multiple modes
  10266. * and crtcs. At that point we'll need to compute the whole config
  10267. * (i.e. one pipe_config for each crtc) rather than just the one
  10268. * for this crtc.
  10269. */
  10270. for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
  10271. /* FIXME: For now we still expect modeset_pipes has at most
  10272. * one bit set. */
  10273. if (WARN_ON(&intel_crtc->base != crtc))
  10274. continue;
  10275. pipe_config = intel_modeset_pipe_config(crtc, mode, state);
  10276. if (IS_ERR(pipe_config))
  10277. return pipe_config;
  10278. pipe_config->base.enable = true;
  10279. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10280. "[modeset]");
  10281. }
  10282. return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
  10283. }
  10284. static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
  10285. unsigned modeset_pipes,
  10286. unsigned disable_pipes)
  10287. {
  10288. struct drm_device *dev = state->dev;
  10289. struct drm_i915_private *dev_priv = to_i915(dev);
  10290. unsigned clear_pipes = modeset_pipes | disable_pipes;
  10291. struct intel_crtc *intel_crtc;
  10292. int ret = 0;
  10293. if (!dev_priv->display.crtc_compute_clock)
  10294. return 0;
  10295. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  10296. if (ret)
  10297. goto done;
  10298. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  10299. struct intel_crtc_state *crtc_state =
  10300. intel_atomic_get_crtc_state(state, intel_crtc);
  10301. /* Modeset pipes should have a new state by now */
  10302. if (WARN_ON(IS_ERR(crtc_state)))
  10303. continue;
  10304. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10305. crtc_state);
  10306. if (ret) {
  10307. intel_shared_dpll_abort_config(dev_priv);
  10308. goto done;
  10309. }
  10310. }
  10311. done:
  10312. return ret;
  10313. }
  10314. static int __intel_set_mode(struct drm_crtc *crtc,
  10315. struct drm_display_mode *mode,
  10316. int x, int y, struct drm_framebuffer *fb,
  10317. struct intel_crtc_state *pipe_config,
  10318. unsigned modeset_pipes,
  10319. unsigned prepare_pipes,
  10320. unsigned disable_pipes)
  10321. {
  10322. struct drm_device *dev = crtc->dev;
  10323. struct drm_i915_private *dev_priv = dev->dev_private;
  10324. struct drm_display_mode *saved_mode;
  10325. struct drm_atomic_state *state = pipe_config->base.state;
  10326. struct intel_crtc_state *crtc_state_copy = NULL;
  10327. struct intel_crtc *intel_crtc;
  10328. int ret = 0;
  10329. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  10330. if (!saved_mode)
  10331. return -ENOMEM;
  10332. crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
  10333. if (!crtc_state_copy) {
  10334. ret = -ENOMEM;
  10335. goto done;
  10336. }
  10337. *saved_mode = crtc->mode;
  10338. /*
  10339. * See if the config requires any additional preparation, e.g.
  10340. * to adjust global state with pipes off. We need to do this
  10341. * here so we can get the modeset_pipe updated config for the new
  10342. * mode set on this crtc. For other crtcs we need to use the
  10343. * adjusted_mode bits in the crtc directly.
  10344. */
  10345. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  10346. ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
  10347. if (ret)
  10348. goto done;
  10349. /* may have added more to prepare_pipes than we should */
  10350. prepare_pipes &= ~disable_pipes;
  10351. }
  10352. ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
  10353. if (ret)
  10354. goto done;
  10355. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  10356. intel_crtc_disable(&intel_crtc->base);
  10357. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  10358. if (intel_crtc->base.state->enable)
  10359. dev_priv->display.crtc_disable(&intel_crtc->base);
  10360. }
  10361. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  10362. * to set it here already despite that we pass it down the callchain.
  10363. *
  10364. * Note we'll need to fix this up when we start tracking multiple
  10365. * pipes; here we assume a single modeset_pipe and only track the
  10366. * single crtc and mode.
  10367. */
  10368. if (modeset_pipes) {
  10369. crtc->mode = *mode;
  10370. /* mode_set/enable/disable functions rely on a correct pipe
  10371. * config. */
  10372. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  10373. /*
  10374. * Calculate and store various constants which
  10375. * are later needed by vblank and swap-completion
  10376. * timestamping. They are derived from true hwmode.
  10377. */
  10378. drm_calc_timestamping_constants(crtc,
  10379. &pipe_config->base.adjusted_mode);
  10380. }
  10381. /* Only after disabling all output pipelines that will be changed can we
  10382. * update the the output configuration. */
  10383. intel_modeset_update_state(dev, prepare_pipes);
  10384. modeset_update_crtc_power_domains(state);
  10385. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  10386. struct drm_plane *primary = intel_crtc->base.primary;
  10387. int vdisplay, hdisplay;
  10388. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  10389. ret = drm_plane_helper_update(primary, &intel_crtc->base,
  10390. fb, 0, 0,
  10391. hdisplay, vdisplay,
  10392. x << 16, y << 16,
  10393. hdisplay << 16, vdisplay << 16);
  10394. }
  10395. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10396. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  10397. update_scanline_offset(intel_crtc);
  10398. dev_priv->display.crtc_enable(&intel_crtc->base);
  10399. }
  10400. /* FIXME: add subpixel order */
  10401. done:
  10402. if (ret && crtc->state->enable)
  10403. crtc->mode = *saved_mode;
  10404. if (ret == 0 && pipe_config) {
  10405. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10406. /* The pipe_config will be freed with the atomic state, so
  10407. * make a copy. */
  10408. memcpy(crtc_state_copy, intel_crtc->config,
  10409. sizeof *crtc_state_copy);
  10410. intel_crtc->config = crtc_state_copy;
  10411. intel_crtc->base.state = &crtc_state_copy->base;
  10412. } else {
  10413. kfree(crtc_state_copy);
  10414. }
  10415. kfree(saved_mode);
  10416. return ret;
  10417. }
  10418. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  10419. struct drm_display_mode *mode,
  10420. int x, int y, struct drm_framebuffer *fb,
  10421. struct intel_crtc_state *pipe_config,
  10422. unsigned modeset_pipes,
  10423. unsigned prepare_pipes,
  10424. unsigned disable_pipes)
  10425. {
  10426. int ret;
  10427. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  10428. prepare_pipes, disable_pipes);
  10429. if (ret == 0)
  10430. intel_modeset_check_state(crtc->dev);
  10431. return ret;
  10432. }
  10433. static int intel_set_mode(struct drm_crtc *crtc,
  10434. struct drm_display_mode *mode,
  10435. int x, int y, struct drm_framebuffer *fb,
  10436. struct drm_atomic_state *state)
  10437. {
  10438. struct intel_crtc_state *pipe_config;
  10439. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  10440. int ret = 0;
  10441. pipe_config = intel_modeset_compute_config(crtc, mode, state,
  10442. &modeset_pipes,
  10443. &prepare_pipes,
  10444. &disable_pipes);
  10445. if (IS_ERR(pipe_config)) {
  10446. ret = PTR_ERR(pipe_config);
  10447. goto out;
  10448. }
  10449. ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  10450. modeset_pipes, prepare_pipes,
  10451. disable_pipes);
  10452. if (ret)
  10453. goto out;
  10454. out:
  10455. return ret;
  10456. }
  10457. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10458. {
  10459. struct drm_device *dev = crtc->dev;
  10460. struct drm_atomic_state *state;
  10461. struct intel_encoder *encoder;
  10462. struct intel_connector *connector;
  10463. struct drm_connector_state *connector_state;
  10464. state = drm_atomic_state_alloc(dev);
  10465. if (!state) {
  10466. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  10467. crtc->base.id);
  10468. return;
  10469. }
  10470. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10471. /* The force restore path in the HW readout code relies on the staged
  10472. * config still keeping the user requested config while the actual
  10473. * state has been overwritten by the configuration read from HW. We
  10474. * need to copy the staged config to the atomic state, otherwise the
  10475. * mode set will just reapply the state the HW is already in. */
  10476. for_each_intel_encoder(dev, encoder) {
  10477. if (&encoder->new_crtc->base != crtc)
  10478. continue;
  10479. for_each_intel_connector(dev, connector) {
  10480. if (connector->new_encoder != encoder)
  10481. continue;
  10482. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  10483. if (IS_ERR(connector_state)) {
  10484. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  10485. connector->base.base.id,
  10486. connector->base.name,
  10487. PTR_ERR(connector_state));
  10488. continue;
  10489. }
  10490. connector_state->crtc = crtc;
  10491. connector_state->best_encoder = &encoder->base;
  10492. }
  10493. }
  10494. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
  10495. state);
  10496. drm_atomic_state_free(state);
  10497. }
  10498. #undef for_each_intel_crtc_masked
  10499. static void intel_set_config_free(struct intel_set_config *config)
  10500. {
  10501. if (!config)
  10502. return;
  10503. kfree(config->save_connector_encoders);
  10504. kfree(config->save_encoder_crtcs);
  10505. kfree(config->save_crtc_enabled);
  10506. kfree(config);
  10507. }
  10508. static int intel_set_config_save_state(struct drm_device *dev,
  10509. struct intel_set_config *config)
  10510. {
  10511. struct drm_crtc *crtc;
  10512. struct drm_encoder *encoder;
  10513. struct drm_connector *connector;
  10514. int count;
  10515. config->save_crtc_enabled =
  10516. kcalloc(dev->mode_config.num_crtc,
  10517. sizeof(bool), GFP_KERNEL);
  10518. if (!config->save_crtc_enabled)
  10519. return -ENOMEM;
  10520. config->save_encoder_crtcs =
  10521. kcalloc(dev->mode_config.num_encoder,
  10522. sizeof(struct drm_crtc *), GFP_KERNEL);
  10523. if (!config->save_encoder_crtcs)
  10524. return -ENOMEM;
  10525. config->save_connector_encoders =
  10526. kcalloc(dev->mode_config.num_connector,
  10527. sizeof(struct drm_encoder *), GFP_KERNEL);
  10528. if (!config->save_connector_encoders)
  10529. return -ENOMEM;
  10530. /* Copy data. Note that driver private data is not affected.
  10531. * Should anything bad happen only the expected state is
  10532. * restored, not the drivers personal bookkeeping.
  10533. */
  10534. count = 0;
  10535. for_each_crtc(dev, crtc) {
  10536. config->save_crtc_enabled[count++] = crtc->state->enable;
  10537. }
  10538. count = 0;
  10539. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  10540. config->save_encoder_crtcs[count++] = encoder->crtc;
  10541. }
  10542. count = 0;
  10543. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10544. config->save_connector_encoders[count++] = connector->encoder;
  10545. }
  10546. return 0;
  10547. }
  10548. static void intel_set_config_restore_state(struct drm_device *dev,
  10549. struct intel_set_config *config)
  10550. {
  10551. struct intel_crtc *crtc;
  10552. struct intel_encoder *encoder;
  10553. struct intel_connector *connector;
  10554. int count;
  10555. count = 0;
  10556. for_each_intel_crtc(dev, crtc) {
  10557. crtc->new_enabled = config->save_crtc_enabled[count++];
  10558. }
  10559. count = 0;
  10560. for_each_intel_encoder(dev, encoder) {
  10561. encoder->new_crtc =
  10562. to_intel_crtc(config->save_encoder_crtcs[count++]);
  10563. }
  10564. count = 0;
  10565. for_each_intel_connector(dev, connector) {
  10566. connector->new_encoder =
  10567. to_intel_encoder(config->save_connector_encoders[count++]);
  10568. }
  10569. }
  10570. static bool
  10571. is_crtc_connector_off(struct drm_mode_set *set)
  10572. {
  10573. int i;
  10574. if (set->num_connectors == 0)
  10575. return false;
  10576. if (WARN_ON(set->connectors == NULL))
  10577. return false;
  10578. for (i = 0; i < set->num_connectors; i++)
  10579. if (set->connectors[i]->encoder &&
  10580. set->connectors[i]->encoder->crtc == set->crtc &&
  10581. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  10582. return true;
  10583. return false;
  10584. }
  10585. static void
  10586. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  10587. struct intel_set_config *config)
  10588. {
  10589. /* We should be able to check here if the fb has the same properties
  10590. * and then just flip_or_move it */
  10591. if (is_crtc_connector_off(set)) {
  10592. config->mode_changed = true;
  10593. } else if (set->crtc->primary->fb != set->fb) {
  10594. /*
  10595. * If we have no fb, we can only flip as long as the crtc is
  10596. * active, otherwise we need a full mode set. The crtc may
  10597. * be active if we've only disabled the primary plane, or
  10598. * in fastboot situations.
  10599. */
  10600. if (set->crtc->primary->fb == NULL) {
  10601. struct intel_crtc *intel_crtc =
  10602. to_intel_crtc(set->crtc);
  10603. if (intel_crtc->active) {
  10604. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  10605. config->fb_changed = true;
  10606. } else {
  10607. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  10608. config->mode_changed = true;
  10609. }
  10610. } else if (set->fb == NULL) {
  10611. config->mode_changed = true;
  10612. } else if (set->fb->pixel_format !=
  10613. set->crtc->primary->fb->pixel_format) {
  10614. config->mode_changed = true;
  10615. } else {
  10616. config->fb_changed = true;
  10617. }
  10618. }
  10619. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  10620. config->fb_changed = true;
  10621. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  10622. DRM_DEBUG_KMS("modes are different, full mode set\n");
  10623. drm_mode_debug_printmodeline(&set->crtc->mode);
  10624. drm_mode_debug_printmodeline(set->mode);
  10625. config->mode_changed = true;
  10626. }
  10627. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  10628. set->crtc->base.id, config->mode_changed, config->fb_changed);
  10629. }
  10630. static int
  10631. intel_modeset_stage_output_state(struct drm_device *dev,
  10632. struct drm_mode_set *set,
  10633. struct intel_set_config *config,
  10634. struct drm_atomic_state *state)
  10635. {
  10636. struct intel_connector *connector;
  10637. struct drm_connector_state *connector_state;
  10638. struct intel_encoder *encoder;
  10639. struct intel_crtc *crtc;
  10640. int ro;
  10641. /* The upper layers ensure that we either disable a crtc or have a list
  10642. * of connectors. For paranoia, double-check this. */
  10643. WARN_ON(!set->fb && (set->num_connectors != 0));
  10644. WARN_ON(set->fb && (set->num_connectors == 0));
  10645. for_each_intel_connector(dev, connector) {
  10646. /* Otherwise traverse passed in connector list and get encoders
  10647. * for them. */
  10648. for (ro = 0; ro < set->num_connectors; ro++) {
  10649. if (set->connectors[ro] == &connector->base) {
  10650. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  10651. break;
  10652. }
  10653. }
  10654. /* If we disable the crtc, disable all its connectors. Also, if
  10655. * the connector is on the changing crtc but not on the new
  10656. * connector list, disable it. */
  10657. if ((!set->fb || ro == set->num_connectors) &&
  10658. connector->base.encoder &&
  10659. connector->base.encoder->crtc == set->crtc) {
  10660. connector->new_encoder = NULL;
  10661. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  10662. connector->base.base.id,
  10663. connector->base.name);
  10664. }
  10665. if (&connector->new_encoder->base != connector->base.encoder) {
  10666. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
  10667. connector->base.base.id,
  10668. connector->base.name);
  10669. config->mode_changed = true;
  10670. }
  10671. }
  10672. /* connector->new_encoder is now updated for all connectors. */
  10673. /* Update crtc of enabled connectors. */
  10674. for_each_intel_connector(dev, connector) {
  10675. struct drm_crtc *new_crtc;
  10676. if (!connector->new_encoder)
  10677. continue;
  10678. new_crtc = connector->new_encoder->base.crtc;
  10679. for (ro = 0; ro < set->num_connectors; ro++) {
  10680. if (set->connectors[ro] == &connector->base)
  10681. new_crtc = set->crtc;
  10682. }
  10683. /* Make sure the new CRTC will work with the encoder */
  10684. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  10685. new_crtc)) {
  10686. return -EINVAL;
  10687. }
  10688. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  10689. connector_state =
  10690. drm_atomic_get_connector_state(state, &connector->base);
  10691. if (IS_ERR(connector_state))
  10692. return PTR_ERR(connector_state);
  10693. connector_state->crtc = new_crtc;
  10694. connector_state->best_encoder = &connector->new_encoder->base;
  10695. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  10696. connector->base.base.id,
  10697. connector->base.name,
  10698. new_crtc->base.id);
  10699. }
  10700. /* Check for any encoders that needs to be disabled. */
  10701. for_each_intel_encoder(dev, encoder) {
  10702. int num_connectors = 0;
  10703. for_each_intel_connector(dev, connector) {
  10704. if (connector->new_encoder == encoder) {
  10705. WARN_ON(!connector->new_encoder->new_crtc);
  10706. num_connectors++;
  10707. }
  10708. }
  10709. if (num_connectors == 0)
  10710. encoder->new_crtc = NULL;
  10711. else if (num_connectors > 1)
  10712. return -EINVAL;
  10713. /* Only now check for crtc changes so we don't miss encoders
  10714. * that will be disabled. */
  10715. if (&encoder->new_crtc->base != encoder->base.crtc) {
  10716. DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
  10717. encoder->base.base.id,
  10718. encoder->base.name);
  10719. config->mode_changed = true;
  10720. }
  10721. }
  10722. /* Now we've also updated encoder->new_crtc for all encoders. */
  10723. for_each_intel_connector(dev, connector) {
  10724. connector_state =
  10725. drm_atomic_get_connector_state(state, &connector->base);
  10726. if (IS_ERR(connector_state))
  10727. return PTR_ERR(connector_state);
  10728. if (connector->new_encoder) {
  10729. if (connector->new_encoder != connector->encoder)
  10730. connector->encoder = connector->new_encoder;
  10731. } else {
  10732. connector_state->crtc = NULL;
  10733. connector_state->best_encoder = NULL;
  10734. }
  10735. }
  10736. for_each_intel_crtc(dev, crtc) {
  10737. crtc->new_enabled = false;
  10738. for_each_intel_encoder(dev, encoder) {
  10739. if (encoder->new_crtc == crtc) {
  10740. crtc->new_enabled = true;
  10741. break;
  10742. }
  10743. }
  10744. if (crtc->new_enabled != crtc->base.state->enable) {
  10745. DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
  10746. crtc->base.base.id,
  10747. crtc->new_enabled ? "en" : "dis");
  10748. config->mode_changed = true;
  10749. }
  10750. }
  10751. return 0;
  10752. }
  10753. static void disable_crtc_nofb(struct intel_crtc *crtc)
  10754. {
  10755. struct drm_device *dev = crtc->base.dev;
  10756. struct intel_encoder *encoder;
  10757. struct intel_connector *connector;
  10758. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  10759. pipe_name(crtc->pipe));
  10760. for_each_intel_connector(dev, connector) {
  10761. if (connector->new_encoder &&
  10762. connector->new_encoder->new_crtc == crtc)
  10763. connector->new_encoder = NULL;
  10764. }
  10765. for_each_intel_encoder(dev, encoder) {
  10766. if (encoder->new_crtc == crtc)
  10767. encoder->new_crtc = NULL;
  10768. }
  10769. crtc->new_enabled = false;
  10770. }
  10771. static int intel_crtc_set_config(struct drm_mode_set *set)
  10772. {
  10773. struct drm_device *dev;
  10774. struct drm_mode_set save_set;
  10775. struct drm_atomic_state *state = NULL;
  10776. struct intel_set_config *config;
  10777. struct intel_crtc_state *pipe_config;
  10778. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  10779. int ret;
  10780. BUG_ON(!set);
  10781. BUG_ON(!set->crtc);
  10782. BUG_ON(!set->crtc->helper_private);
  10783. /* Enforce sane interface api - has been abused by the fb helper. */
  10784. BUG_ON(!set->mode && set->fb);
  10785. BUG_ON(set->fb && set->num_connectors == 0);
  10786. if (set->fb) {
  10787. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  10788. set->crtc->base.id, set->fb->base.id,
  10789. (int)set->num_connectors, set->x, set->y);
  10790. } else {
  10791. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  10792. }
  10793. dev = set->crtc->dev;
  10794. ret = -ENOMEM;
  10795. config = kzalloc(sizeof(*config), GFP_KERNEL);
  10796. if (!config)
  10797. goto out_config;
  10798. ret = intel_set_config_save_state(dev, config);
  10799. if (ret)
  10800. goto out_config;
  10801. save_set.crtc = set->crtc;
  10802. save_set.mode = &set->crtc->mode;
  10803. save_set.x = set->crtc->x;
  10804. save_set.y = set->crtc->y;
  10805. save_set.fb = set->crtc->primary->fb;
  10806. /* Compute whether we need a full modeset, only an fb base update or no
  10807. * change at all. In the future we might also check whether only the
  10808. * mode changed, e.g. for LVDS where we only change the panel fitter in
  10809. * such cases. */
  10810. intel_set_config_compute_mode_changes(set, config);
  10811. state = drm_atomic_state_alloc(dev);
  10812. if (!state) {
  10813. ret = -ENOMEM;
  10814. goto out_config;
  10815. }
  10816. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10817. ret = intel_modeset_stage_output_state(dev, set, config, state);
  10818. if (ret)
  10819. goto fail;
  10820. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  10821. state,
  10822. &modeset_pipes,
  10823. &prepare_pipes,
  10824. &disable_pipes);
  10825. if (IS_ERR(pipe_config)) {
  10826. ret = PTR_ERR(pipe_config);
  10827. goto fail;
  10828. } else if (pipe_config) {
  10829. if (pipe_config->has_audio !=
  10830. to_intel_crtc(set->crtc)->config->has_audio)
  10831. config->mode_changed = true;
  10832. /*
  10833. * Note we have an issue here with infoframes: current code
  10834. * only updates them on the full mode set path per hw
  10835. * requirements. So here we should be checking for any
  10836. * required changes and forcing a mode set.
  10837. */
  10838. }
  10839. intel_update_pipe_size(to_intel_crtc(set->crtc));
  10840. if (config->mode_changed) {
  10841. ret = intel_set_mode_pipes(set->crtc, set->mode,
  10842. set->x, set->y, set->fb, pipe_config,
  10843. modeset_pipes, prepare_pipes,
  10844. disable_pipes);
  10845. } else if (config->fb_changed) {
  10846. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  10847. struct drm_plane *primary = set->crtc->primary;
  10848. struct intel_plane_state *plane_state =
  10849. to_intel_plane_state(primary->state);
  10850. bool was_visible = plane_state->visible;
  10851. int vdisplay, hdisplay;
  10852. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  10853. ret = drm_plane_helper_update(primary, set->crtc, set->fb,
  10854. 0, 0, hdisplay, vdisplay,
  10855. set->x << 16, set->y << 16,
  10856. hdisplay << 16, vdisplay << 16);
  10857. /*
  10858. * We need to make sure the primary plane is re-enabled if it
  10859. * has previously been turned off.
  10860. */
  10861. plane_state = to_intel_plane_state(primary->state);
  10862. if (ret == 0 && !was_visible && plane_state->visible) {
  10863. WARN_ON(!intel_crtc->active);
  10864. intel_post_enable_primary(set->crtc);
  10865. }
  10866. /*
  10867. * In the fastboot case this may be our only check of the
  10868. * state after boot. It would be better to only do it on
  10869. * the first update, but we don't have a nice way of doing that
  10870. * (and really, set_config isn't used much for high freq page
  10871. * flipping, so increasing its cost here shouldn't be a big
  10872. * deal).
  10873. */
  10874. if (i915.fastboot && ret == 0)
  10875. intel_modeset_check_state(set->crtc->dev);
  10876. }
  10877. if (ret) {
  10878. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  10879. set->crtc->base.id, ret);
  10880. fail:
  10881. intel_set_config_restore_state(dev, config);
  10882. drm_atomic_state_clear(state);
  10883. /*
  10884. * HACK: if the pipe was on, but we didn't have a framebuffer,
  10885. * force the pipe off to avoid oopsing in the modeset code
  10886. * due to fb==NULL. This should only happen during boot since
  10887. * we don't yet reconstruct the FB from the hardware state.
  10888. */
  10889. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  10890. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  10891. /* Try to restore the config */
  10892. if (config->mode_changed &&
  10893. intel_set_mode(save_set.crtc, save_set.mode,
  10894. save_set.x, save_set.y, save_set.fb,
  10895. state))
  10896. DRM_ERROR("failed to restore config after modeset failure\n");
  10897. }
  10898. out_config:
  10899. if (state)
  10900. drm_atomic_state_free(state);
  10901. intel_set_config_free(config);
  10902. return ret;
  10903. }
  10904. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10905. .gamma_set = intel_crtc_gamma_set,
  10906. .set_config = intel_crtc_set_config,
  10907. .destroy = intel_crtc_destroy,
  10908. .page_flip = intel_crtc_page_flip,
  10909. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10910. .atomic_destroy_state = intel_crtc_destroy_state,
  10911. };
  10912. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  10913. struct intel_shared_dpll *pll,
  10914. struct intel_dpll_hw_state *hw_state)
  10915. {
  10916. uint32_t val;
  10917. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  10918. return false;
  10919. val = I915_READ(PCH_DPLL(pll->id));
  10920. hw_state->dpll = val;
  10921. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  10922. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  10923. return val & DPLL_VCO_ENABLE;
  10924. }
  10925. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  10926. struct intel_shared_dpll *pll)
  10927. {
  10928. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  10929. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  10930. }
  10931. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  10932. struct intel_shared_dpll *pll)
  10933. {
  10934. /* PCH refclock must be enabled first */
  10935. ibx_assert_pch_refclk_enabled(dev_priv);
  10936. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10937. /* Wait for the clocks to stabilize. */
  10938. POSTING_READ(PCH_DPLL(pll->id));
  10939. udelay(150);
  10940. /* The pixel multiplier can only be updated once the
  10941. * DPLL is enabled and the clocks are stable.
  10942. *
  10943. * So write it again.
  10944. */
  10945. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10946. POSTING_READ(PCH_DPLL(pll->id));
  10947. udelay(200);
  10948. }
  10949. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  10950. struct intel_shared_dpll *pll)
  10951. {
  10952. struct drm_device *dev = dev_priv->dev;
  10953. struct intel_crtc *crtc;
  10954. /* Make sure no transcoder isn't still depending on us. */
  10955. for_each_intel_crtc(dev, crtc) {
  10956. if (intel_crtc_to_shared_dpll(crtc) == pll)
  10957. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  10958. }
  10959. I915_WRITE(PCH_DPLL(pll->id), 0);
  10960. POSTING_READ(PCH_DPLL(pll->id));
  10961. udelay(200);
  10962. }
  10963. static char *ibx_pch_dpll_names[] = {
  10964. "PCH DPLL A",
  10965. "PCH DPLL B",
  10966. };
  10967. static void ibx_pch_dpll_init(struct drm_device *dev)
  10968. {
  10969. struct drm_i915_private *dev_priv = dev->dev_private;
  10970. int i;
  10971. dev_priv->num_shared_dpll = 2;
  10972. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10973. dev_priv->shared_dplls[i].id = i;
  10974. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  10975. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  10976. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  10977. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  10978. dev_priv->shared_dplls[i].get_hw_state =
  10979. ibx_pch_dpll_get_hw_state;
  10980. }
  10981. }
  10982. static void intel_shared_dpll_init(struct drm_device *dev)
  10983. {
  10984. struct drm_i915_private *dev_priv = dev->dev_private;
  10985. if (HAS_DDI(dev))
  10986. intel_ddi_pll_init(dev);
  10987. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  10988. ibx_pch_dpll_init(dev);
  10989. else
  10990. dev_priv->num_shared_dpll = 0;
  10991. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  10992. }
  10993. /**
  10994. * intel_wm_need_update - Check whether watermarks need updating
  10995. * @plane: drm plane
  10996. * @state: new plane state
  10997. *
  10998. * Check current plane state versus the new one to determine whether
  10999. * watermarks need to be recalculated.
  11000. *
  11001. * Returns true or false.
  11002. */
  11003. bool intel_wm_need_update(struct drm_plane *plane,
  11004. struct drm_plane_state *state)
  11005. {
  11006. /* Update watermarks on tiling changes. */
  11007. if (!plane->state->fb || !state->fb ||
  11008. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  11009. plane->state->rotation != state->rotation)
  11010. return true;
  11011. return false;
  11012. }
  11013. /**
  11014. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11015. * @plane: drm plane to prepare for
  11016. * @fb: framebuffer to prepare for presentation
  11017. *
  11018. * Prepares a framebuffer for usage on a display plane. Generally this
  11019. * involves pinning the underlying object and updating the frontbuffer tracking
  11020. * bits. Some older platforms need special physical address handling for
  11021. * cursor planes.
  11022. *
  11023. * Returns 0 on success, negative error code on failure.
  11024. */
  11025. int
  11026. intel_prepare_plane_fb(struct drm_plane *plane,
  11027. struct drm_framebuffer *fb,
  11028. const struct drm_plane_state *new_state)
  11029. {
  11030. struct drm_device *dev = plane->dev;
  11031. struct intel_plane *intel_plane = to_intel_plane(plane);
  11032. enum pipe pipe = intel_plane->pipe;
  11033. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11034. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11035. unsigned frontbuffer_bits = 0;
  11036. int ret = 0;
  11037. if (!obj)
  11038. return 0;
  11039. switch (plane->type) {
  11040. case DRM_PLANE_TYPE_PRIMARY:
  11041. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11042. break;
  11043. case DRM_PLANE_TYPE_CURSOR:
  11044. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  11045. break;
  11046. case DRM_PLANE_TYPE_OVERLAY:
  11047. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  11048. break;
  11049. }
  11050. mutex_lock(&dev->struct_mutex);
  11051. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11052. INTEL_INFO(dev)->cursor_needs_physical) {
  11053. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11054. ret = i915_gem_object_attach_phys(obj, align);
  11055. if (ret)
  11056. DRM_DEBUG_KMS("failed to attach phys object\n");
  11057. } else {
  11058. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  11059. }
  11060. if (ret == 0)
  11061. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  11062. mutex_unlock(&dev->struct_mutex);
  11063. return ret;
  11064. }
  11065. /**
  11066. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11067. * @plane: drm plane to clean up for
  11068. * @fb: old framebuffer that was on plane
  11069. *
  11070. * Cleans up a framebuffer that has just been removed from a plane.
  11071. */
  11072. void
  11073. intel_cleanup_plane_fb(struct drm_plane *plane,
  11074. struct drm_framebuffer *fb,
  11075. const struct drm_plane_state *old_state)
  11076. {
  11077. struct drm_device *dev = plane->dev;
  11078. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11079. if (WARN_ON(!obj))
  11080. return;
  11081. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11082. !INTEL_INFO(dev)->cursor_needs_physical) {
  11083. mutex_lock(&dev->struct_mutex);
  11084. intel_unpin_fb_obj(fb, old_state);
  11085. mutex_unlock(&dev->struct_mutex);
  11086. }
  11087. }
  11088. int
  11089. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11090. {
  11091. int max_scale;
  11092. struct drm_device *dev;
  11093. struct drm_i915_private *dev_priv;
  11094. int crtc_clock, cdclk;
  11095. if (!intel_crtc || !crtc_state)
  11096. return DRM_PLANE_HELPER_NO_SCALING;
  11097. dev = intel_crtc->base.dev;
  11098. dev_priv = dev->dev_private;
  11099. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11100. cdclk = dev_priv->display.get_display_clock_speed(dev);
  11101. if (!crtc_clock || !cdclk)
  11102. return DRM_PLANE_HELPER_NO_SCALING;
  11103. /*
  11104. * skl max scale is lower of:
  11105. * close to 3 but not 3, -1 is for that purpose
  11106. * or
  11107. * cdclk/crtc_clock
  11108. */
  11109. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11110. return max_scale;
  11111. }
  11112. static int
  11113. intel_check_primary_plane(struct drm_plane *plane,
  11114. struct intel_plane_state *state)
  11115. {
  11116. struct drm_device *dev = plane->dev;
  11117. struct drm_i915_private *dev_priv = dev->dev_private;
  11118. struct drm_crtc *crtc = state->base.crtc;
  11119. struct intel_crtc *intel_crtc;
  11120. struct intel_crtc_state *crtc_state;
  11121. struct drm_framebuffer *fb = state->base.fb;
  11122. struct drm_rect *dest = &state->dst;
  11123. struct drm_rect *src = &state->src;
  11124. const struct drm_rect *clip = &state->clip;
  11125. bool can_position = false;
  11126. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11127. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11128. int ret;
  11129. crtc = crtc ? crtc : plane->crtc;
  11130. intel_crtc = to_intel_crtc(crtc);
  11131. crtc_state = state->base.state ?
  11132. intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
  11133. if (INTEL_INFO(dev)->gen >= 9) {
  11134. min_scale = 1;
  11135. max_scale = skl_max_scale(intel_crtc, crtc_state);
  11136. can_position = true;
  11137. }
  11138. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11139. src, dest, clip,
  11140. min_scale,
  11141. max_scale,
  11142. can_position, true,
  11143. &state->visible);
  11144. if (ret)
  11145. return ret;
  11146. if (intel_crtc->active) {
  11147. struct intel_plane_state *old_state =
  11148. to_intel_plane_state(plane->state);
  11149. intel_crtc->atomic.wait_for_flips = true;
  11150. /*
  11151. * FBC does not work on some platforms for rotated
  11152. * planes, so disable it when rotation is not 0 and
  11153. * update it when rotation is set back to 0.
  11154. *
  11155. * FIXME: This is redundant with the fbc update done in
  11156. * the primary plane enable function except that that
  11157. * one is done too late. We eventually need to unify
  11158. * this.
  11159. */
  11160. if (state->visible &&
  11161. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  11162. dev_priv->fbc.crtc == intel_crtc &&
  11163. state->base.rotation != BIT(DRM_ROTATE_0)) {
  11164. intel_crtc->atomic.disable_fbc = true;
  11165. }
  11166. if (state->visible && !old_state->visible) {
  11167. /*
  11168. * BDW signals flip done immediately if the plane
  11169. * is disabled, even if the plane enable is already
  11170. * armed to occur at the next vblank :(
  11171. */
  11172. if (IS_BROADWELL(dev))
  11173. intel_crtc->atomic.wait_vblank = true;
  11174. }
  11175. intel_crtc->atomic.fb_bits |=
  11176. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  11177. intel_crtc->atomic.update_fbc = true;
  11178. if (intel_wm_need_update(plane, &state->base))
  11179. intel_crtc->atomic.update_wm = true;
  11180. }
  11181. if (INTEL_INFO(dev)->gen >= 9) {
  11182. ret = skl_update_scaler_users(intel_crtc, crtc_state,
  11183. to_intel_plane(plane), state, 0);
  11184. if (ret)
  11185. return ret;
  11186. }
  11187. return 0;
  11188. }
  11189. static void
  11190. intel_commit_primary_plane(struct drm_plane *plane,
  11191. struct intel_plane_state *state)
  11192. {
  11193. struct drm_crtc *crtc = state->base.crtc;
  11194. struct drm_framebuffer *fb = state->base.fb;
  11195. struct drm_device *dev = plane->dev;
  11196. struct drm_i915_private *dev_priv = dev->dev_private;
  11197. struct intel_crtc *intel_crtc;
  11198. struct drm_rect *src = &state->src;
  11199. crtc = crtc ? crtc : plane->crtc;
  11200. intel_crtc = to_intel_crtc(crtc);
  11201. plane->fb = fb;
  11202. crtc->x = src->x1 >> 16;
  11203. crtc->y = src->y1 >> 16;
  11204. if (intel_crtc->active) {
  11205. if (state->visible)
  11206. /* FIXME: kill this fastboot hack */
  11207. intel_update_pipe_size(intel_crtc);
  11208. dev_priv->display.update_primary_plane(crtc, plane->fb,
  11209. crtc->x, crtc->y);
  11210. }
  11211. }
  11212. static void
  11213. intel_disable_primary_plane(struct drm_plane *plane,
  11214. struct drm_crtc *crtc,
  11215. bool force)
  11216. {
  11217. struct drm_device *dev = plane->dev;
  11218. struct drm_i915_private *dev_priv = dev->dev_private;
  11219. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11220. }
  11221. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11222. {
  11223. struct drm_device *dev = crtc->dev;
  11224. struct drm_i915_private *dev_priv = dev->dev_private;
  11225. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11226. struct intel_plane *intel_plane;
  11227. struct drm_plane *p;
  11228. unsigned fb_bits = 0;
  11229. /* Track fb's for any planes being disabled */
  11230. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  11231. intel_plane = to_intel_plane(p);
  11232. if (intel_crtc->atomic.disabled_planes &
  11233. (1 << drm_plane_index(p))) {
  11234. switch (p->type) {
  11235. case DRM_PLANE_TYPE_PRIMARY:
  11236. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  11237. break;
  11238. case DRM_PLANE_TYPE_CURSOR:
  11239. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  11240. break;
  11241. case DRM_PLANE_TYPE_OVERLAY:
  11242. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  11243. break;
  11244. }
  11245. mutex_lock(&dev->struct_mutex);
  11246. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  11247. mutex_unlock(&dev->struct_mutex);
  11248. }
  11249. }
  11250. if (intel_crtc->atomic.wait_for_flips)
  11251. intel_crtc_wait_for_pending_flips(crtc);
  11252. if (intel_crtc->atomic.disable_fbc)
  11253. intel_fbc_disable(dev);
  11254. if (intel_crtc->atomic.pre_disable_primary)
  11255. intel_pre_disable_primary(crtc);
  11256. if (intel_crtc->atomic.update_wm)
  11257. intel_update_watermarks(crtc);
  11258. intel_runtime_pm_get(dev_priv);
  11259. /* Perform vblank evasion around commit operation */
  11260. if (intel_crtc->active)
  11261. intel_crtc->atomic.evade =
  11262. intel_pipe_update_start(intel_crtc,
  11263. &intel_crtc->atomic.start_vbl_count);
  11264. }
  11265. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11266. {
  11267. struct drm_device *dev = crtc->dev;
  11268. struct drm_i915_private *dev_priv = dev->dev_private;
  11269. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11270. struct drm_plane *p;
  11271. if (intel_crtc->atomic.evade)
  11272. intel_pipe_update_end(intel_crtc,
  11273. intel_crtc->atomic.start_vbl_count);
  11274. intel_runtime_pm_put(dev_priv);
  11275. if (intel_crtc->atomic.wait_vblank)
  11276. intel_wait_for_vblank(dev, intel_crtc->pipe);
  11277. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  11278. if (intel_crtc->atomic.update_fbc) {
  11279. mutex_lock(&dev->struct_mutex);
  11280. intel_fbc_update(dev);
  11281. mutex_unlock(&dev->struct_mutex);
  11282. }
  11283. if (intel_crtc->atomic.post_enable_primary)
  11284. intel_post_enable_primary(crtc);
  11285. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  11286. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  11287. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  11288. false, false);
  11289. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  11290. }
  11291. /**
  11292. * intel_plane_destroy - destroy a plane
  11293. * @plane: plane to destroy
  11294. *
  11295. * Common destruction function for all types of planes (primary, cursor,
  11296. * sprite).
  11297. */
  11298. void intel_plane_destroy(struct drm_plane *plane)
  11299. {
  11300. struct intel_plane *intel_plane = to_intel_plane(plane);
  11301. drm_plane_cleanup(plane);
  11302. kfree(intel_plane);
  11303. }
  11304. const struct drm_plane_funcs intel_plane_funcs = {
  11305. .update_plane = drm_atomic_helper_update_plane,
  11306. .disable_plane = drm_atomic_helper_disable_plane,
  11307. .destroy = intel_plane_destroy,
  11308. .set_property = drm_atomic_helper_plane_set_property,
  11309. .atomic_get_property = intel_plane_atomic_get_property,
  11310. .atomic_set_property = intel_plane_atomic_set_property,
  11311. .atomic_duplicate_state = intel_plane_duplicate_state,
  11312. .atomic_destroy_state = intel_plane_destroy_state,
  11313. };
  11314. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11315. int pipe)
  11316. {
  11317. struct intel_plane *primary;
  11318. struct intel_plane_state *state;
  11319. const uint32_t *intel_primary_formats;
  11320. int num_formats;
  11321. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11322. if (primary == NULL)
  11323. return NULL;
  11324. state = intel_create_plane_state(&primary->base);
  11325. if (!state) {
  11326. kfree(primary);
  11327. return NULL;
  11328. }
  11329. primary->base.state = &state->base;
  11330. primary->can_scale = false;
  11331. primary->max_downscale = 1;
  11332. if (INTEL_INFO(dev)->gen >= 9) {
  11333. primary->can_scale = true;
  11334. }
  11335. state->scaler_id = -1;
  11336. primary->pipe = pipe;
  11337. primary->plane = pipe;
  11338. primary->check_plane = intel_check_primary_plane;
  11339. primary->commit_plane = intel_commit_primary_plane;
  11340. primary->disable_plane = intel_disable_primary_plane;
  11341. primary->ckey.flags = I915_SET_COLORKEY_NONE;
  11342. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11343. primary->plane = !pipe;
  11344. if (INTEL_INFO(dev)->gen <= 3) {
  11345. intel_primary_formats = intel_primary_formats_gen2;
  11346. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  11347. } else {
  11348. intel_primary_formats = intel_primary_formats_gen4;
  11349. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  11350. }
  11351. drm_universal_plane_init(dev, &primary->base, 0,
  11352. &intel_plane_funcs,
  11353. intel_primary_formats, num_formats,
  11354. DRM_PLANE_TYPE_PRIMARY);
  11355. if (INTEL_INFO(dev)->gen >= 4)
  11356. intel_create_rotation_property(dev, primary);
  11357. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11358. return &primary->base;
  11359. }
  11360. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11361. {
  11362. if (!dev->mode_config.rotation_property) {
  11363. unsigned long flags = BIT(DRM_ROTATE_0) |
  11364. BIT(DRM_ROTATE_180);
  11365. if (INTEL_INFO(dev)->gen >= 9)
  11366. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11367. dev->mode_config.rotation_property =
  11368. drm_mode_create_rotation_property(dev, flags);
  11369. }
  11370. if (dev->mode_config.rotation_property)
  11371. drm_object_attach_property(&plane->base.base,
  11372. dev->mode_config.rotation_property,
  11373. plane->base.state->rotation);
  11374. }
  11375. static int
  11376. intel_check_cursor_plane(struct drm_plane *plane,
  11377. struct intel_plane_state *state)
  11378. {
  11379. struct drm_crtc *crtc = state->base.crtc;
  11380. struct drm_device *dev = plane->dev;
  11381. struct drm_framebuffer *fb = state->base.fb;
  11382. struct drm_rect *dest = &state->dst;
  11383. struct drm_rect *src = &state->src;
  11384. const struct drm_rect *clip = &state->clip;
  11385. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11386. struct intel_crtc *intel_crtc;
  11387. unsigned stride;
  11388. int ret;
  11389. crtc = crtc ? crtc : plane->crtc;
  11390. intel_crtc = to_intel_crtc(crtc);
  11391. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11392. src, dest, clip,
  11393. DRM_PLANE_HELPER_NO_SCALING,
  11394. DRM_PLANE_HELPER_NO_SCALING,
  11395. true, true, &state->visible);
  11396. if (ret)
  11397. return ret;
  11398. /* if we want to turn off the cursor ignore width and height */
  11399. if (!obj)
  11400. goto finish;
  11401. /* Check for which cursor types we support */
  11402. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  11403. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11404. state->base.crtc_w, state->base.crtc_h);
  11405. return -EINVAL;
  11406. }
  11407. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11408. if (obj->base.size < stride * state->base.crtc_h) {
  11409. DRM_DEBUG_KMS("buffer is too small\n");
  11410. return -ENOMEM;
  11411. }
  11412. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11413. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11414. ret = -EINVAL;
  11415. }
  11416. finish:
  11417. if (intel_crtc->active) {
  11418. if (plane->state->crtc_w != state->base.crtc_w)
  11419. intel_crtc->atomic.update_wm = true;
  11420. intel_crtc->atomic.fb_bits |=
  11421. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  11422. }
  11423. return ret;
  11424. }
  11425. static void
  11426. intel_disable_cursor_plane(struct drm_plane *plane,
  11427. struct drm_crtc *crtc,
  11428. bool force)
  11429. {
  11430. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11431. if (!force) {
  11432. plane->fb = NULL;
  11433. intel_crtc->cursor_bo = NULL;
  11434. intel_crtc->cursor_addr = 0;
  11435. }
  11436. intel_crtc_update_cursor(crtc, false);
  11437. }
  11438. static void
  11439. intel_commit_cursor_plane(struct drm_plane *plane,
  11440. struct intel_plane_state *state)
  11441. {
  11442. struct drm_crtc *crtc = state->base.crtc;
  11443. struct drm_device *dev = plane->dev;
  11444. struct intel_crtc *intel_crtc;
  11445. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11446. uint32_t addr;
  11447. crtc = crtc ? crtc : plane->crtc;
  11448. intel_crtc = to_intel_crtc(crtc);
  11449. plane->fb = state->base.fb;
  11450. crtc->cursor_x = state->base.crtc_x;
  11451. crtc->cursor_y = state->base.crtc_y;
  11452. if (intel_crtc->cursor_bo == obj)
  11453. goto update;
  11454. if (!obj)
  11455. addr = 0;
  11456. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11457. addr = i915_gem_obj_ggtt_offset(obj);
  11458. else
  11459. addr = obj->phys_handle->busaddr;
  11460. intel_crtc->cursor_addr = addr;
  11461. intel_crtc->cursor_bo = obj;
  11462. update:
  11463. if (intel_crtc->active)
  11464. intel_crtc_update_cursor(crtc, state->visible);
  11465. }
  11466. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11467. int pipe)
  11468. {
  11469. struct intel_plane *cursor;
  11470. struct intel_plane_state *state;
  11471. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11472. if (cursor == NULL)
  11473. return NULL;
  11474. state = intel_create_plane_state(&cursor->base);
  11475. if (!state) {
  11476. kfree(cursor);
  11477. return NULL;
  11478. }
  11479. cursor->base.state = &state->base;
  11480. cursor->can_scale = false;
  11481. cursor->max_downscale = 1;
  11482. cursor->pipe = pipe;
  11483. cursor->plane = pipe;
  11484. state->scaler_id = -1;
  11485. cursor->check_plane = intel_check_cursor_plane;
  11486. cursor->commit_plane = intel_commit_cursor_plane;
  11487. cursor->disable_plane = intel_disable_cursor_plane;
  11488. drm_universal_plane_init(dev, &cursor->base, 0,
  11489. &intel_plane_funcs,
  11490. intel_cursor_formats,
  11491. ARRAY_SIZE(intel_cursor_formats),
  11492. DRM_PLANE_TYPE_CURSOR);
  11493. if (INTEL_INFO(dev)->gen >= 4) {
  11494. if (!dev->mode_config.rotation_property)
  11495. dev->mode_config.rotation_property =
  11496. drm_mode_create_rotation_property(dev,
  11497. BIT(DRM_ROTATE_0) |
  11498. BIT(DRM_ROTATE_180));
  11499. if (dev->mode_config.rotation_property)
  11500. drm_object_attach_property(&cursor->base.base,
  11501. dev->mode_config.rotation_property,
  11502. state->base.rotation);
  11503. }
  11504. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11505. return &cursor->base;
  11506. }
  11507. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11508. struct intel_crtc_state *crtc_state)
  11509. {
  11510. int i;
  11511. struct intel_scaler *intel_scaler;
  11512. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11513. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11514. intel_scaler = &scaler_state->scalers[i];
  11515. intel_scaler->in_use = 0;
  11516. intel_scaler->id = i;
  11517. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11518. }
  11519. scaler_state->scaler_id = -1;
  11520. }
  11521. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11522. {
  11523. struct drm_i915_private *dev_priv = dev->dev_private;
  11524. struct intel_crtc *intel_crtc;
  11525. struct intel_crtc_state *crtc_state = NULL;
  11526. struct drm_plane *primary = NULL;
  11527. struct drm_plane *cursor = NULL;
  11528. int i, ret;
  11529. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11530. if (intel_crtc == NULL)
  11531. return;
  11532. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11533. if (!crtc_state)
  11534. goto fail;
  11535. intel_crtc_set_state(intel_crtc, crtc_state);
  11536. crtc_state->base.crtc = &intel_crtc->base;
  11537. /* initialize shared scalers */
  11538. if (INTEL_INFO(dev)->gen >= 9) {
  11539. if (pipe == PIPE_C)
  11540. intel_crtc->num_scalers = 1;
  11541. else
  11542. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11543. skl_init_scalers(dev, intel_crtc, crtc_state);
  11544. }
  11545. primary = intel_primary_plane_create(dev, pipe);
  11546. if (!primary)
  11547. goto fail;
  11548. cursor = intel_cursor_plane_create(dev, pipe);
  11549. if (!cursor)
  11550. goto fail;
  11551. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11552. cursor, &intel_crtc_funcs);
  11553. if (ret)
  11554. goto fail;
  11555. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11556. for (i = 0; i < 256; i++) {
  11557. intel_crtc->lut_r[i] = i;
  11558. intel_crtc->lut_g[i] = i;
  11559. intel_crtc->lut_b[i] = i;
  11560. }
  11561. /*
  11562. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11563. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11564. */
  11565. intel_crtc->pipe = pipe;
  11566. intel_crtc->plane = pipe;
  11567. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11568. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11569. intel_crtc->plane = !pipe;
  11570. }
  11571. intel_crtc->cursor_base = ~0;
  11572. intel_crtc->cursor_cntl = ~0;
  11573. intel_crtc->cursor_size = ~0;
  11574. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11575. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11576. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11577. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11578. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  11579. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11580. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11581. return;
  11582. fail:
  11583. if (primary)
  11584. drm_plane_cleanup(primary);
  11585. if (cursor)
  11586. drm_plane_cleanup(cursor);
  11587. kfree(crtc_state);
  11588. kfree(intel_crtc);
  11589. }
  11590. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11591. {
  11592. struct drm_encoder *encoder = connector->base.encoder;
  11593. struct drm_device *dev = connector->base.dev;
  11594. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11595. if (!encoder || WARN_ON(!encoder->crtc))
  11596. return INVALID_PIPE;
  11597. return to_intel_crtc(encoder->crtc)->pipe;
  11598. }
  11599. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11600. struct drm_file *file)
  11601. {
  11602. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11603. struct drm_crtc *drmmode_crtc;
  11604. struct intel_crtc *crtc;
  11605. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11606. if (!drmmode_crtc) {
  11607. DRM_ERROR("no such CRTC id\n");
  11608. return -ENOENT;
  11609. }
  11610. crtc = to_intel_crtc(drmmode_crtc);
  11611. pipe_from_crtc_id->pipe = crtc->pipe;
  11612. return 0;
  11613. }
  11614. static int intel_encoder_clones(struct intel_encoder *encoder)
  11615. {
  11616. struct drm_device *dev = encoder->base.dev;
  11617. struct intel_encoder *source_encoder;
  11618. int index_mask = 0;
  11619. int entry = 0;
  11620. for_each_intel_encoder(dev, source_encoder) {
  11621. if (encoders_cloneable(encoder, source_encoder))
  11622. index_mask |= (1 << entry);
  11623. entry++;
  11624. }
  11625. return index_mask;
  11626. }
  11627. static bool has_edp_a(struct drm_device *dev)
  11628. {
  11629. struct drm_i915_private *dev_priv = dev->dev_private;
  11630. if (!IS_MOBILE(dev))
  11631. return false;
  11632. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11633. return false;
  11634. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11635. return false;
  11636. return true;
  11637. }
  11638. static bool intel_crt_present(struct drm_device *dev)
  11639. {
  11640. struct drm_i915_private *dev_priv = dev->dev_private;
  11641. if (INTEL_INFO(dev)->gen >= 9)
  11642. return false;
  11643. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11644. return false;
  11645. if (IS_CHERRYVIEW(dev))
  11646. return false;
  11647. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11648. return false;
  11649. return true;
  11650. }
  11651. static void intel_setup_outputs(struct drm_device *dev)
  11652. {
  11653. struct drm_i915_private *dev_priv = dev->dev_private;
  11654. struct intel_encoder *encoder;
  11655. bool dpd_is_edp = false;
  11656. intel_lvds_init(dev);
  11657. if (intel_crt_present(dev))
  11658. intel_crt_init(dev);
  11659. if (IS_BROXTON(dev)) {
  11660. /*
  11661. * FIXME: Broxton doesn't support port detection via the
  11662. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11663. * detect the ports.
  11664. */
  11665. intel_ddi_init(dev, PORT_A);
  11666. intel_ddi_init(dev, PORT_B);
  11667. intel_ddi_init(dev, PORT_C);
  11668. } else if (HAS_DDI(dev)) {
  11669. int found;
  11670. /*
  11671. * Haswell uses DDI functions to detect digital outputs.
  11672. * On SKL pre-D0 the strap isn't connected, so we assume
  11673. * it's there.
  11674. */
  11675. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11676. /* WaIgnoreDDIAStrap: skl */
  11677. if (found ||
  11678. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11679. intel_ddi_init(dev, PORT_A);
  11680. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11681. * register */
  11682. found = I915_READ(SFUSE_STRAP);
  11683. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11684. intel_ddi_init(dev, PORT_B);
  11685. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11686. intel_ddi_init(dev, PORT_C);
  11687. if (found & SFUSE_STRAP_DDID_DETECTED)
  11688. intel_ddi_init(dev, PORT_D);
  11689. } else if (HAS_PCH_SPLIT(dev)) {
  11690. int found;
  11691. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11692. if (has_edp_a(dev))
  11693. intel_dp_init(dev, DP_A, PORT_A);
  11694. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11695. /* PCH SDVOB multiplex with HDMIB */
  11696. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11697. if (!found)
  11698. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11699. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11700. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11701. }
  11702. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11703. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11704. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11705. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11706. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11707. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11708. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11709. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11710. } else if (IS_VALLEYVIEW(dev)) {
  11711. /*
  11712. * The DP_DETECTED bit is the latched state of the DDC
  11713. * SDA pin at boot. However since eDP doesn't require DDC
  11714. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11715. * eDP ports may have been muxed to an alternate function.
  11716. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11717. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11718. * detect eDP ports.
  11719. */
  11720. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11721. !intel_dp_is_edp(dev, PORT_B))
  11722. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11723. PORT_B);
  11724. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11725. intel_dp_is_edp(dev, PORT_B))
  11726. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11727. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11728. !intel_dp_is_edp(dev, PORT_C))
  11729. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11730. PORT_C);
  11731. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11732. intel_dp_is_edp(dev, PORT_C))
  11733. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11734. if (IS_CHERRYVIEW(dev)) {
  11735. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11736. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11737. PORT_D);
  11738. /* eDP not supported on port D, so don't check VBT */
  11739. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11740. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11741. }
  11742. intel_dsi_init(dev);
  11743. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  11744. bool found = false;
  11745. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11746. DRM_DEBUG_KMS("probing SDVOB\n");
  11747. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11748. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  11749. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11750. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11751. }
  11752. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  11753. intel_dp_init(dev, DP_B, PORT_B);
  11754. }
  11755. /* Before G4X SDVOC doesn't have its own detect register */
  11756. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11757. DRM_DEBUG_KMS("probing SDVOC\n");
  11758. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11759. }
  11760. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11761. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  11762. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11763. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11764. }
  11765. if (SUPPORTS_INTEGRATED_DP(dev))
  11766. intel_dp_init(dev, DP_C, PORT_C);
  11767. }
  11768. if (SUPPORTS_INTEGRATED_DP(dev) &&
  11769. (I915_READ(DP_D) & DP_DETECTED))
  11770. intel_dp_init(dev, DP_D, PORT_D);
  11771. } else if (IS_GEN2(dev))
  11772. intel_dvo_init(dev);
  11773. if (SUPPORTS_TV(dev))
  11774. intel_tv_init(dev);
  11775. intel_psr_init(dev);
  11776. for_each_intel_encoder(dev, encoder) {
  11777. encoder->base.possible_crtcs = encoder->crtc_mask;
  11778. encoder->base.possible_clones =
  11779. intel_encoder_clones(encoder);
  11780. }
  11781. intel_init_pch_refclk(dev);
  11782. drm_helper_move_panel_connectors_to_head(dev);
  11783. }
  11784. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11785. {
  11786. struct drm_device *dev = fb->dev;
  11787. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11788. drm_framebuffer_cleanup(fb);
  11789. mutex_lock(&dev->struct_mutex);
  11790. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11791. drm_gem_object_unreference(&intel_fb->obj->base);
  11792. mutex_unlock(&dev->struct_mutex);
  11793. kfree(intel_fb);
  11794. }
  11795. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11796. struct drm_file *file,
  11797. unsigned int *handle)
  11798. {
  11799. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11800. struct drm_i915_gem_object *obj = intel_fb->obj;
  11801. return drm_gem_handle_create(file, &obj->base, handle);
  11802. }
  11803. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11804. .destroy = intel_user_framebuffer_destroy,
  11805. .create_handle = intel_user_framebuffer_create_handle,
  11806. };
  11807. static
  11808. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11809. uint32_t pixel_format)
  11810. {
  11811. u32 gen = INTEL_INFO(dev)->gen;
  11812. if (gen >= 9) {
  11813. /* "The stride in bytes must not exceed the of the size of 8K
  11814. * pixels and 32K bytes."
  11815. */
  11816. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11817. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11818. return 32*1024;
  11819. } else if (gen >= 4) {
  11820. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11821. return 16*1024;
  11822. else
  11823. return 32*1024;
  11824. } else if (gen >= 3) {
  11825. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11826. return 8*1024;
  11827. else
  11828. return 16*1024;
  11829. } else {
  11830. /* XXX DSPC is limited to 4k tiled */
  11831. return 8*1024;
  11832. }
  11833. }
  11834. static int intel_framebuffer_init(struct drm_device *dev,
  11835. struct intel_framebuffer *intel_fb,
  11836. struct drm_mode_fb_cmd2 *mode_cmd,
  11837. struct drm_i915_gem_object *obj)
  11838. {
  11839. unsigned int aligned_height;
  11840. int ret;
  11841. u32 pitch_limit, stride_alignment;
  11842. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11843. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11844. /* Enforce that fb modifier and tiling mode match, but only for
  11845. * X-tiled. This is needed for FBC. */
  11846. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11847. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11848. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11849. return -EINVAL;
  11850. }
  11851. } else {
  11852. if (obj->tiling_mode == I915_TILING_X)
  11853. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11854. else if (obj->tiling_mode == I915_TILING_Y) {
  11855. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11856. return -EINVAL;
  11857. }
  11858. }
  11859. /* Passed in modifier sanity checking. */
  11860. switch (mode_cmd->modifier[0]) {
  11861. case I915_FORMAT_MOD_Y_TILED:
  11862. case I915_FORMAT_MOD_Yf_TILED:
  11863. if (INTEL_INFO(dev)->gen < 9) {
  11864. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11865. mode_cmd->modifier[0]);
  11866. return -EINVAL;
  11867. }
  11868. case DRM_FORMAT_MOD_NONE:
  11869. case I915_FORMAT_MOD_X_TILED:
  11870. break;
  11871. default:
  11872. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11873. mode_cmd->modifier[0]);
  11874. return -EINVAL;
  11875. }
  11876. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11877. mode_cmd->pixel_format);
  11878. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11879. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11880. mode_cmd->pitches[0], stride_alignment);
  11881. return -EINVAL;
  11882. }
  11883. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11884. mode_cmd->pixel_format);
  11885. if (mode_cmd->pitches[0] > pitch_limit) {
  11886. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11887. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11888. "tiled" : "linear",
  11889. mode_cmd->pitches[0], pitch_limit);
  11890. return -EINVAL;
  11891. }
  11892. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11893. mode_cmd->pitches[0] != obj->stride) {
  11894. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11895. mode_cmd->pitches[0], obj->stride);
  11896. return -EINVAL;
  11897. }
  11898. /* Reject formats not supported by any plane early. */
  11899. switch (mode_cmd->pixel_format) {
  11900. case DRM_FORMAT_C8:
  11901. case DRM_FORMAT_RGB565:
  11902. case DRM_FORMAT_XRGB8888:
  11903. case DRM_FORMAT_ARGB8888:
  11904. break;
  11905. case DRM_FORMAT_XRGB1555:
  11906. case DRM_FORMAT_ARGB1555:
  11907. if (INTEL_INFO(dev)->gen > 3) {
  11908. DRM_DEBUG("unsupported pixel format: %s\n",
  11909. drm_get_format_name(mode_cmd->pixel_format));
  11910. return -EINVAL;
  11911. }
  11912. break;
  11913. case DRM_FORMAT_XBGR8888:
  11914. case DRM_FORMAT_ABGR8888:
  11915. case DRM_FORMAT_XRGB2101010:
  11916. case DRM_FORMAT_ARGB2101010:
  11917. case DRM_FORMAT_XBGR2101010:
  11918. case DRM_FORMAT_ABGR2101010:
  11919. if (INTEL_INFO(dev)->gen < 4) {
  11920. DRM_DEBUG("unsupported pixel format: %s\n",
  11921. drm_get_format_name(mode_cmd->pixel_format));
  11922. return -EINVAL;
  11923. }
  11924. break;
  11925. case DRM_FORMAT_YUYV:
  11926. case DRM_FORMAT_UYVY:
  11927. case DRM_FORMAT_YVYU:
  11928. case DRM_FORMAT_VYUY:
  11929. if (INTEL_INFO(dev)->gen < 5) {
  11930. DRM_DEBUG("unsupported pixel format: %s\n",
  11931. drm_get_format_name(mode_cmd->pixel_format));
  11932. return -EINVAL;
  11933. }
  11934. break;
  11935. default:
  11936. DRM_DEBUG("unsupported pixel format: %s\n",
  11937. drm_get_format_name(mode_cmd->pixel_format));
  11938. return -EINVAL;
  11939. }
  11940. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11941. if (mode_cmd->offsets[0] != 0)
  11942. return -EINVAL;
  11943. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11944. mode_cmd->pixel_format,
  11945. mode_cmd->modifier[0]);
  11946. /* FIXME drm helper for size checks (especially planar formats)? */
  11947. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11948. return -EINVAL;
  11949. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11950. intel_fb->obj = obj;
  11951. intel_fb->obj->framebuffer_references++;
  11952. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11953. if (ret) {
  11954. DRM_ERROR("framebuffer init failed %d\n", ret);
  11955. return ret;
  11956. }
  11957. return 0;
  11958. }
  11959. static struct drm_framebuffer *
  11960. intel_user_framebuffer_create(struct drm_device *dev,
  11961. struct drm_file *filp,
  11962. struct drm_mode_fb_cmd2 *mode_cmd)
  11963. {
  11964. struct drm_i915_gem_object *obj;
  11965. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11966. mode_cmd->handles[0]));
  11967. if (&obj->base == NULL)
  11968. return ERR_PTR(-ENOENT);
  11969. return intel_framebuffer_create(dev, mode_cmd, obj);
  11970. }
  11971. #ifndef CONFIG_DRM_I915_FBDEV
  11972. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  11973. {
  11974. }
  11975. #endif
  11976. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11977. .fb_create = intel_user_framebuffer_create,
  11978. .output_poll_changed = intel_fbdev_output_poll_changed,
  11979. .atomic_check = intel_atomic_check,
  11980. .atomic_commit = intel_atomic_commit,
  11981. };
  11982. /* Set up chip specific display functions */
  11983. static void intel_init_display(struct drm_device *dev)
  11984. {
  11985. struct drm_i915_private *dev_priv = dev->dev_private;
  11986. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  11987. dev_priv->display.find_dpll = g4x_find_best_dpll;
  11988. else if (IS_CHERRYVIEW(dev))
  11989. dev_priv->display.find_dpll = chv_find_best_dpll;
  11990. else if (IS_VALLEYVIEW(dev))
  11991. dev_priv->display.find_dpll = vlv_find_best_dpll;
  11992. else if (IS_PINEVIEW(dev))
  11993. dev_priv->display.find_dpll = pnv_find_best_dpll;
  11994. else
  11995. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  11996. if (INTEL_INFO(dev)->gen >= 9) {
  11997. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11998. dev_priv->display.get_initial_plane_config =
  11999. skylake_get_initial_plane_config;
  12000. dev_priv->display.crtc_compute_clock =
  12001. haswell_crtc_compute_clock;
  12002. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12003. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12004. dev_priv->display.off = ironlake_crtc_off;
  12005. dev_priv->display.update_primary_plane =
  12006. skylake_update_primary_plane;
  12007. } else if (HAS_DDI(dev)) {
  12008. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12009. dev_priv->display.get_initial_plane_config =
  12010. ironlake_get_initial_plane_config;
  12011. dev_priv->display.crtc_compute_clock =
  12012. haswell_crtc_compute_clock;
  12013. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12014. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12015. dev_priv->display.off = ironlake_crtc_off;
  12016. dev_priv->display.update_primary_plane =
  12017. ironlake_update_primary_plane;
  12018. } else if (HAS_PCH_SPLIT(dev)) {
  12019. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12020. dev_priv->display.get_initial_plane_config =
  12021. ironlake_get_initial_plane_config;
  12022. dev_priv->display.crtc_compute_clock =
  12023. ironlake_crtc_compute_clock;
  12024. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12025. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12026. dev_priv->display.off = ironlake_crtc_off;
  12027. dev_priv->display.update_primary_plane =
  12028. ironlake_update_primary_plane;
  12029. } else if (IS_VALLEYVIEW(dev)) {
  12030. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12031. dev_priv->display.get_initial_plane_config =
  12032. i9xx_get_initial_plane_config;
  12033. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12034. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12035. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12036. dev_priv->display.off = i9xx_crtc_off;
  12037. dev_priv->display.update_primary_plane =
  12038. i9xx_update_primary_plane;
  12039. } else {
  12040. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12041. dev_priv->display.get_initial_plane_config =
  12042. i9xx_get_initial_plane_config;
  12043. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12044. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12045. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12046. dev_priv->display.off = i9xx_crtc_off;
  12047. dev_priv->display.update_primary_plane =
  12048. i9xx_update_primary_plane;
  12049. }
  12050. /* Returns the core display clock speed */
  12051. if (IS_SKYLAKE(dev))
  12052. dev_priv->display.get_display_clock_speed =
  12053. skylake_get_display_clock_speed;
  12054. else if (IS_BROADWELL(dev))
  12055. dev_priv->display.get_display_clock_speed =
  12056. broadwell_get_display_clock_speed;
  12057. else if (IS_HASWELL(dev))
  12058. dev_priv->display.get_display_clock_speed =
  12059. haswell_get_display_clock_speed;
  12060. else if (IS_VALLEYVIEW(dev))
  12061. dev_priv->display.get_display_clock_speed =
  12062. valleyview_get_display_clock_speed;
  12063. else if (IS_GEN5(dev))
  12064. dev_priv->display.get_display_clock_speed =
  12065. ilk_get_display_clock_speed;
  12066. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12067. IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  12068. dev_priv->display.get_display_clock_speed =
  12069. i945_get_display_clock_speed;
  12070. else if (IS_I915G(dev))
  12071. dev_priv->display.get_display_clock_speed =
  12072. i915_get_display_clock_speed;
  12073. else if (IS_I945GM(dev) || IS_845G(dev))
  12074. dev_priv->display.get_display_clock_speed =
  12075. i9xx_misc_get_display_clock_speed;
  12076. else if (IS_PINEVIEW(dev))
  12077. dev_priv->display.get_display_clock_speed =
  12078. pnv_get_display_clock_speed;
  12079. else if (IS_I915GM(dev))
  12080. dev_priv->display.get_display_clock_speed =
  12081. i915gm_get_display_clock_speed;
  12082. else if (IS_I865G(dev))
  12083. dev_priv->display.get_display_clock_speed =
  12084. i865_get_display_clock_speed;
  12085. else if (IS_I85X(dev))
  12086. dev_priv->display.get_display_clock_speed =
  12087. i855_get_display_clock_speed;
  12088. else /* 852, 830 */
  12089. dev_priv->display.get_display_clock_speed =
  12090. i830_get_display_clock_speed;
  12091. if (IS_GEN5(dev)) {
  12092. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12093. } else if (IS_GEN6(dev)) {
  12094. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12095. } else if (IS_IVYBRIDGE(dev)) {
  12096. /* FIXME: detect B0+ stepping and use auto training */
  12097. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12098. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12099. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12100. } else if (IS_VALLEYVIEW(dev)) {
  12101. dev_priv->display.modeset_global_resources =
  12102. valleyview_modeset_global_resources;
  12103. } else if (IS_BROXTON(dev)) {
  12104. dev_priv->display.modeset_global_resources =
  12105. broxton_modeset_global_resources;
  12106. }
  12107. switch (INTEL_INFO(dev)->gen) {
  12108. case 2:
  12109. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12110. break;
  12111. case 3:
  12112. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12113. break;
  12114. case 4:
  12115. case 5:
  12116. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12117. break;
  12118. case 6:
  12119. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12120. break;
  12121. case 7:
  12122. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12123. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12124. break;
  12125. case 9:
  12126. /* Drop through - unsupported since execlist only. */
  12127. default:
  12128. /* Default just returns -ENODEV to indicate unsupported */
  12129. dev_priv->display.queue_flip = intel_default_queue_flip;
  12130. }
  12131. intel_panel_init_backlight_funcs(dev);
  12132. mutex_init(&dev_priv->pps_mutex);
  12133. }
  12134. /*
  12135. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12136. * resume, or other times. This quirk makes sure that's the case for
  12137. * affected systems.
  12138. */
  12139. static void quirk_pipea_force(struct drm_device *dev)
  12140. {
  12141. struct drm_i915_private *dev_priv = dev->dev_private;
  12142. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12143. DRM_INFO("applying pipe a force quirk\n");
  12144. }
  12145. static void quirk_pipeb_force(struct drm_device *dev)
  12146. {
  12147. struct drm_i915_private *dev_priv = dev->dev_private;
  12148. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12149. DRM_INFO("applying pipe b force quirk\n");
  12150. }
  12151. /*
  12152. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12153. */
  12154. static void quirk_ssc_force_disable(struct drm_device *dev)
  12155. {
  12156. struct drm_i915_private *dev_priv = dev->dev_private;
  12157. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12158. DRM_INFO("applying lvds SSC disable quirk\n");
  12159. }
  12160. /*
  12161. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12162. * brightness value
  12163. */
  12164. static void quirk_invert_brightness(struct drm_device *dev)
  12165. {
  12166. struct drm_i915_private *dev_priv = dev->dev_private;
  12167. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12168. DRM_INFO("applying inverted panel brightness quirk\n");
  12169. }
  12170. /* Some VBT's incorrectly indicate no backlight is present */
  12171. static void quirk_backlight_present(struct drm_device *dev)
  12172. {
  12173. struct drm_i915_private *dev_priv = dev->dev_private;
  12174. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12175. DRM_INFO("applying backlight present quirk\n");
  12176. }
  12177. struct intel_quirk {
  12178. int device;
  12179. int subsystem_vendor;
  12180. int subsystem_device;
  12181. void (*hook)(struct drm_device *dev);
  12182. };
  12183. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12184. struct intel_dmi_quirk {
  12185. void (*hook)(struct drm_device *dev);
  12186. const struct dmi_system_id (*dmi_id_list)[];
  12187. };
  12188. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12189. {
  12190. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12191. return 1;
  12192. }
  12193. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12194. {
  12195. .dmi_id_list = &(const struct dmi_system_id[]) {
  12196. {
  12197. .callback = intel_dmi_reverse_brightness,
  12198. .ident = "NCR Corporation",
  12199. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12200. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12201. },
  12202. },
  12203. { } /* terminating entry */
  12204. },
  12205. .hook = quirk_invert_brightness,
  12206. },
  12207. };
  12208. static struct intel_quirk intel_quirks[] = {
  12209. /* HP Mini needs pipe A force quirk (LP: #322104) */
  12210. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  12211. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12212. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12213. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12214. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12215. /* 830 needs to leave pipe A & dpll A up */
  12216. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12217. /* 830 needs to leave pipe B & dpll B up */
  12218. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12219. /* Lenovo U160 cannot use SSC on LVDS */
  12220. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12221. /* Sony Vaio Y cannot use SSC on LVDS */
  12222. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12223. /* Acer Aspire 5734Z must invert backlight brightness */
  12224. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12225. /* Acer/eMachines G725 */
  12226. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12227. /* Acer/eMachines e725 */
  12228. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12229. /* Acer/Packard Bell NCL20 */
  12230. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12231. /* Acer Aspire 4736Z */
  12232. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12233. /* Acer Aspire 5336 */
  12234. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12235. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12236. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12237. /* Acer C720 Chromebook (Core i3 4005U) */
  12238. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12239. /* Apple Macbook 2,1 (Core 2 T7400) */
  12240. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12241. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12242. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12243. /* HP Chromebook 14 (Celeron 2955U) */
  12244. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12245. /* Dell Chromebook 11 */
  12246. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12247. };
  12248. static void intel_init_quirks(struct drm_device *dev)
  12249. {
  12250. struct pci_dev *d = dev->pdev;
  12251. int i;
  12252. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12253. struct intel_quirk *q = &intel_quirks[i];
  12254. if (d->device == q->device &&
  12255. (d->subsystem_vendor == q->subsystem_vendor ||
  12256. q->subsystem_vendor == PCI_ANY_ID) &&
  12257. (d->subsystem_device == q->subsystem_device ||
  12258. q->subsystem_device == PCI_ANY_ID))
  12259. q->hook(dev);
  12260. }
  12261. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12262. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12263. intel_dmi_quirks[i].hook(dev);
  12264. }
  12265. }
  12266. /* Disable the VGA plane that we never use */
  12267. static void i915_disable_vga(struct drm_device *dev)
  12268. {
  12269. struct drm_i915_private *dev_priv = dev->dev_private;
  12270. u8 sr1;
  12271. u32 vga_reg = i915_vgacntrl_reg(dev);
  12272. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12273. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12274. outb(SR01, VGA_SR_INDEX);
  12275. sr1 = inb(VGA_SR_DATA);
  12276. outb(sr1 | 1<<5, VGA_SR_DATA);
  12277. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12278. udelay(300);
  12279. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12280. POSTING_READ(vga_reg);
  12281. }
  12282. void intel_modeset_init_hw(struct drm_device *dev)
  12283. {
  12284. intel_prepare_ddi(dev);
  12285. if (IS_VALLEYVIEW(dev))
  12286. vlv_update_cdclk(dev);
  12287. intel_init_clock_gating(dev);
  12288. intel_enable_gt_powersave(dev);
  12289. }
  12290. void intel_modeset_init(struct drm_device *dev)
  12291. {
  12292. struct drm_i915_private *dev_priv = dev->dev_private;
  12293. int sprite, ret;
  12294. enum pipe pipe;
  12295. struct intel_crtc *crtc;
  12296. drm_mode_config_init(dev);
  12297. dev->mode_config.min_width = 0;
  12298. dev->mode_config.min_height = 0;
  12299. dev->mode_config.preferred_depth = 24;
  12300. dev->mode_config.prefer_shadow = 1;
  12301. dev->mode_config.allow_fb_modifiers = true;
  12302. dev->mode_config.funcs = &intel_mode_funcs;
  12303. intel_init_quirks(dev);
  12304. intel_init_pm(dev);
  12305. if (INTEL_INFO(dev)->num_pipes == 0)
  12306. return;
  12307. intel_init_display(dev);
  12308. intel_init_audio(dev);
  12309. if (IS_GEN2(dev)) {
  12310. dev->mode_config.max_width = 2048;
  12311. dev->mode_config.max_height = 2048;
  12312. } else if (IS_GEN3(dev)) {
  12313. dev->mode_config.max_width = 4096;
  12314. dev->mode_config.max_height = 4096;
  12315. } else {
  12316. dev->mode_config.max_width = 8192;
  12317. dev->mode_config.max_height = 8192;
  12318. }
  12319. if (IS_845G(dev) || IS_I865G(dev)) {
  12320. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12321. dev->mode_config.cursor_height = 1023;
  12322. } else if (IS_GEN2(dev)) {
  12323. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12324. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12325. } else {
  12326. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12327. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12328. }
  12329. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12330. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12331. INTEL_INFO(dev)->num_pipes,
  12332. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12333. for_each_pipe(dev_priv, pipe) {
  12334. intel_crtc_init(dev, pipe);
  12335. for_each_sprite(dev_priv, pipe, sprite) {
  12336. ret = intel_plane_init(dev, pipe, sprite);
  12337. if (ret)
  12338. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12339. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12340. }
  12341. }
  12342. intel_init_dpio(dev);
  12343. intel_shared_dpll_init(dev);
  12344. /* Just disable it once at startup */
  12345. i915_disable_vga(dev);
  12346. intel_setup_outputs(dev);
  12347. /* Just in case the BIOS is doing something questionable. */
  12348. intel_fbc_disable(dev);
  12349. drm_modeset_lock_all(dev);
  12350. intel_modeset_setup_hw_state(dev, false);
  12351. drm_modeset_unlock_all(dev);
  12352. for_each_intel_crtc(dev, crtc) {
  12353. if (!crtc->active)
  12354. continue;
  12355. /*
  12356. * Note that reserving the BIOS fb up front prevents us
  12357. * from stuffing other stolen allocations like the ring
  12358. * on top. This prevents some ugliness at boot time, and
  12359. * can even allow for smooth boot transitions if the BIOS
  12360. * fb is large enough for the active pipe configuration.
  12361. */
  12362. if (dev_priv->display.get_initial_plane_config) {
  12363. dev_priv->display.get_initial_plane_config(crtc,
  12364. &crtc->plane_config);
  12365. /*
  12366. * If the fb is shared between multiple heads, we'll
  12367. * just get the first one.
  12368. */
  12369. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12370. }
  12371. }
  12372. }
  12373. static void intel_enable_pipe_a(struct drm_device *dev)
  12374. {
  12375. struct intel_connector *connector;
  12376. struct drm_connector *crt = NULL;
  12377. struct intel_load_detect_pipe load_detect_temp;
  12378. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12379. /* We can't just switch on the pipe A, we need to set things up with a
  12380. * proper mode and output configuration. As a gross hack, enable pipe A
  12381. * by enabling the load detect pipe once. */
  12382. for_each_intel_connector(dev, connector) {
  12383. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12384. crt = &connector->base;
  12385. break;
  12386. }
  12387. }
  12388. if (!crt)
  12389. return;
  12390. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12391. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12392. }
  12393. static bool
  12394. intel_check_plane_mapping(struct intel_crtc *crtc)
  12395. {
  12396. struct drm_device *dev = crtc->base.dev;
  12397. struct drm_i915_private *dev_priv = dev->dev_private;
  12398. u32 reg, val;
  12399. if (INTEL_INFO(dev)->num_pipes == 1)
  12400. return true;
  12401. reg = DSPCNTR(!crtc->plane);
  12402. val = I915_READ(reg);
  12403. if ((val & DISPLAY_PLANE_ENABLE) &&
  12404. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12405. return false;
  12406. return true;
  12407. }
  12408. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12409. {
  12410. struct drm_device *dev = crtc->base.dev;
  12411. struct drm_i915_private *dev_priv = dev->dev_private;
  12412. u32 reg;
  12413. /* Clear any frame start delays used for debugging left by the BIOS */
  12414. reg = PIPECONF(crtc->config->cpu_transcoder);
  12415. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12416. /* restore vblank interrupts to correct state */
  12417. drm_crtc_vblank_reset(&crtc->base);
  12418. if (crtc->active) {
  12419. update_scanline_offset(crtc);
  12420. drm_crtc_vblank_on(&crtc->base);
  12421. }
  12422. /* We need to sanitize the plane -> pipe mapping first because this will
  12423. * disable the crtc (and hence change the state) if it is wrong. Note
  12424. * that gen4+ has a fixed plane -> pipe mapping. */
  12425. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12426. struct intel_connector *connector;
  12427. bool plane;
  12428. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12429. crtc->base.base.id);
  12430. /* Pipe has the wrong plane attached and the plane is active.
  12431. * Temporarily change the plane mapping and disable everything
  12432. * ... */
  12433. plane = crtc->plane;
  12434. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12435. crtc->plane = !plane;
  12436. dev_priv->display.crtc_disable(&crtc->base);
  12437. crtc->plane = plane;
  12438. /* ... and break all links. */
  12439. for_each_intel_connector(dev, connector) {
  12440. if (connector->encoder->base.crtc != &crtc->base)
  12441. continue;
  12442. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12443. connector->base.encoder = NULL;
  12444. }
  12445. /* multiple connectors may have the same encoder:
  12446. * handle them and break crtc link separately */
  12447. for_each_intel_connector(dev, connector)
  12448. if (connector->encoder->base.crtc == &crtc->base) {
  12449. connector->encoder->base.crtc = NULL;
  12450. connector->encoder->connectors_active = false;
  12451. }
  12452. WARN_ON(crtc->active);
  12453. crtc->base.state->enable = false;
  12454. crtc->base.enabled = false;
  12455. }
  12456. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12457. crtc->pipe == PIPE_A && !crtc->active) {
  12458. /* BIOS forgot to enable pipe A, this mostly happens after
  12459. * resume. Force-enable the pipe to fix this, the update_dpms
  12460. * call below we restore the pipe to the right state, but leave
  12461. * the required bits on. */
  12462. intel_enable_pipe_a(dev);
  12463. }
  12464. /* Adjust the state of the output pipe according to whether we
  12465. * have active connectors/encoders. */
  12466. intel_crtc_update_dpms(&crtc->base);
  12467. if (crtc->active != crtc->base.state->enable) {
  12468. struct intel_encoder *encoder;
  12469. /* This can happen either due to bugs in the get_hw_state
  12470. * functions or because the pipe is force-enabled due to the
  12471. * pipe A quirk. */
  12472. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12473. crtc->base.base.id,
  12474. crtc->base.state->enable ? "enabled" : "disabled",
  12475. crtc->active ? "enabled" : "disabled");
  12476. crtc->base.state->enable = crtc->active;
  12477. crtc->base.enabled = crtc->active;
  12478. /* Because we only establish the connector -> encoder ->
  12479. * crtc links if something is active, this means the
  12480. * crtc is now deactivated. Break the links. connector
  12481. * -> encoder links are only establish when things are
  12482. * actually up, hence no need to break them. */
  12483. WARN_ON(crtc->active);
  12484. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12485. WARN_ON(encoder->connectors_active);
  12486. encoder->base.crtc = NULL;
  12487. }
  12488. }
  12489. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12490. /*
  12491. * We start out with underrun reporting disabled to avoid races.
  12492. * For correct bookkeeping mark this on active crtcs.
  12493. *
  12494. * Also on gmch platforms we dont have any hardware bits to
  12495. * disable the underrun reporting. Which means we need to start
  12496. * out with underrun reporting disabled also on inactive pipes,
  12497. * since otherwise we'll complain about the garbage we read when
  12498. * e.g. coming up after runtime pm.
  12499. *
  12500. * No protection against concurrent access is required - at
  12501. * worst a fifo underrun happens which also sets this to false.
  12502. */
  12503. crtc->cpu_fifo_underrun_disabled = true;
  12504. crtc->pch_fifo_underrun_disabled = true;
  12505. }
  12506. }
  12507. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12508. {
  12509. struct intel_connector *connector;
  12510. struct drm_device *dev = encoder->base.dev;
  12511. /* We need to check both for a crtc link (meaning that the
  12512. * encoder is active and trying to read from a pipe) and the
  12513. * pipe itself being active. */
  12514. bool has_active_crtc = encoder->base.crtc &&
  12515. to_intel_crtc(encoder->base.crtc)->active;
  12516. if (encoder->connectors_active && !has_active_crtc) {
  12517. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12518. encoder->base.base.id,
  12519. encoder->base.name);
  12520. /* Connector is active, but has no active pipe. This is
  12521. * fallout from our resume register restoring. Disable
  12522. * the encoder manually again. */
  12523. if (encoder->base.crtc) {
  12524. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12525. encoder->base.base.id,
  12526. encoder->base.name);
  12527. encoder->disable(encoder);
  12528. if (encoder->post_disable)
  12529. encoder->post_disable(encoder);
  12530. }
  12531. encoder->base.crtc = NULL;
  12532. encoder->connectors_active = false;
  12533. /* Inconsistent output/port/pipe state happens presumably due to
  12534. * a bug in one of the get_hw_state functions. Or someplace else
  12535. * in our code, like the register restore mess on resume. Clamp
  12536. * things to off as a safer default. */
  12537. for_each_intel_connector(dev, connector) {
  12538. if (connector->encoder != encoder)
  12539. continue;
  12540. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12541. connector->base.encoder = NULL;
  12542. }
  12543. }
  12544. /* Enabled encoders without active connectors will be fixed in
  12545. * the crtc fixup. */
  12546. }
  12547. void i915_redisable_vga_power_on(struct drm_device *dev)
  12548. {
  12549. struct drm_i915_private *dev_priv = dev->dev_private;
  12550. u32 vga_reg = i915_vgacntrl_reg(dev);
  12551. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12552. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12553. i915_disable_vga(dev);
  12554. }
  12555. }
  12556. void i915_redisable_vga(struct drm_device *dev)
  12557. {
  12558. struct drm_i915_private *dev_priv = dev->dev_private;
  12559. /* This function can be called both from intel_modeset_setup_hw_state or
  12560. * at a very early point in our resume sequence, where the power well
  12561. * structures are not yet restored. Since this function is at a very
  12562. * paranoid "someone might have enabled VGA while we were not looking"
  12563. * level, just check if the power well is enabled instead of trying to
  12564. * follow the "don't touch the power well if we don't need it" policy
  12565. * the rest of the driver uses. */
  12566. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12567. return;
  12568. i915_redisable_vga_power_on(dev);
  12569. }
  12570. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12571. {
  12572. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12573. if (!crtc->active)
  12574. return false;
  12575. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  12576. }
  12577. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12578. {
  12579. struct drm_i915_private *dev_priv = dev->dev_private;
  12580. enum pipe pipe;
  12581. struct intel_crtc *crtc;
  12582. struct intel_encoder *encoder;
  12583. struct intel_connector *connector;
  12584. int i;
  12585. for_each_intel_crtc(dev, crtc) {
  12586. struct drm_plane *primary = crtc->base.primary;
  12587. struct intel_plane_state *plane_state;
  12588. memset(crtc->config, 0, sizeof(*crtc->config));
  12589. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12590. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12591. crtc->config);
  12592. crtc->base.state->enable = crtc->active;
  12593. crtc->base.enabled = crtc->active;
  12594. plane_state = to_intel_plane_state(primary->state);
  12595. plane_state->visible = primary_get_hw_state(crtc);
  12596. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12597. crtc->base.base.id,
  12598. crtc->active ? "enabled" : "disabled");
  12599. }
  12600. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12601. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12602. pll->on = pll->get_hw_state(dev_priv, pll,
  12603. &pll->config.hw_state);
  12604. pll->active = 0;
  12605. pll->config.crtc_mask = 0;
  12606. for_each_intel_crtc(dev, crtc) {
  12607. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12608. pll->active++;
  12609. pll->config.crtc_mask |= 1 << crtc->pipe;
  12610. }
  12611. }
  12612. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12613. pll->name, pll->config.crtc_mask, pll->on);
  12614. if (pll->config.crtc_mask)
  12615. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12616. }
  12617. for_each_intel_encoder(dev, encoder) {
  12618. pipe = 0;
  12619. if (encoder->get_hw_state(encoder, &pipe)) {
  12620. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12621. encoder->base.crtc = &crtc->base;
  12622. encoder->get_config(encoder, crtc->config);
  12623. } else {
  12624. encoder->base.crtc = NULL;
  12625. }
  12626. encoder->connectors_active = false;
  12627. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12628. encoder->base.base.id,
  12629. encoder->base.name,
  12630. encoder->base.crtc ? "enabled" : "disabled",
  12631. pipe_name(pipe));
  12632. }
  12633. for_each_intel_connector(dev, connector) {
  12634. if (connector->get_hw_state(connector)) {
  12635. connector->base.dpms = DRM_MODE_DPMS_ON;
  12636. connector->encoder->connectors_active = true;
  12637. connector->base.encoder = &connector->encoder->base;
  12638. } else {
  12639. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12640. connector->base.encoder = NULL;
  12641. }
  12642. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12643. connector->base.base.id,
  12644. connector->base.name,
  12645. connector->base.encoder ? "enabled" : "disabled");
  12646. }
  12647. }
  12648. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12649. * and i915 state tracking structures. */
  12650. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12651. bool force_restore)
  12652. {
  12653. struct drm_i915_private *dev_priv = dev->dev_private;
  12654. enum pipe pipe;
  12655. struct intel_crtc *crtc;
  12656. struct intel_encoder *encoder;
  12657. int i;
  12658. intel_modeset_readout_hw_state(dev);
  12659. /*
  12660. * Now that we have the config, copy it to each CRTC struct
  12661. * Note that this could go away if we move to using crtc_config
  12662. * checking everywhere.
  12663. */
  12664. for_each_intel_crtc(dev, crtc) {
  12665. if (crtc->active && i915.fastboot) {
  12666. intel_mode_from_pipe_config(&crtc->base.mode,
  12667. crtc->config);
  12668. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  12669. crtc->base.base.id);
  12670. drm_mode_debug_printmodeline(&crtc->base.mode);
  12671. }
  12672. }
  12673. /* HW state is read out, now we need to sanitize this mess. */
  12674. for_each_intel_encoder(dev, encoder) {
  12675. intel_sanitize_encoder(encoder);
  12676. }
  12677. for_each_pipe(dev_priv, pipe) {
  12678. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12679. intel_sanitize_crtc(crtc);
  12680. intel_dump_pipe_config(crtc, crtc->config,
  12681. "[setup_hw_state]");
  12682. }
  12683. intel_modeset_update_connector_atomic_state(dev);
  12684. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12685. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12686. if (!pll->on || pll->active)
  12687. continue;
  12688. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12689. pll->disable(dev_priv, pll);
  12690. pll->on = false;
  12691. }
  12692. if (IS_GEN9(dev))
  12693. skl_wm_get_hw_state(dev);
  12694. else if (HAS_PCH_SPLIT(dev))
  12695. ilk_wm_get_hw_state(dev);
  12696. if (force_restore) {
  12697. i915_redisable_vga(dev);
  12698. /*
  12699. * We need to use raw interfaces for restoring state to avoid
  12700. * checking (bogus) intermediate states.
  12701. */
  12702. for_each_pipe(dev_priv, pipe) {
  12703. struct drm_crtc *crtc =
  12704. dev_priv->pipe_to_crtc_mapping[pipe];
  12705. intel_crtc_restore_mode(crtc);
  12706. }
  12707. } else {
  12708. intel_modeset_update_staged_output_state(dev);
  12709. }
  12710. intel_modeset_check_state(dev);
  12711. }
  12712. void intel_modeset_gem_init(struct drm_device *dev)
  12713. {
  12714. struct drm_i915_private *dev_priv = dev->dev_private;
  12715. struct drm_crtc *c;
  12716. struct drm_i915_gem_object *obj;
  12717. int ret;
  12718. mutex_lock(&dev->struct_mutex);
  12719. intel_init_gt_powersave(dev);
  12720. mutex_unlock(&dev->struct_mutex);
  12721. /*
  12722. * There may be no VBT; and if the BIOS enabled SSC we can
  12723. * just keep using it to avoid unnecessary flicker. Whereas if the
  12724. * BIOS isn't using it, don't assume it will work even if the VBT
  12725. * indicates as much.
  12726. */
  12727. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  12728. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12729. DREF_SSC1_ENABLE);
  12730. intel_modeset_init_hw(dev);
  12731. intel_setup_overlay(dev);
  12732. /*
  12733. * Make sure any fbs we allocated at startup are properly
  12734. * pinned & fenced. When we do the allocation it's too early
  12735. * for this.
  12736. */
  12737. for_each_crtc(dev, c) {
  12738. obj = intel_fb_obj(c->primary->fb);
  12739. if (obj == NULL)
  12740. continue;
  12741. mutex_lock(&dev->struct_mutex);
  12742. ret = intel_pin_and_fence_fb_obj(c->primary,
  12743. c->primary->fb,
  12744. c->primary->state,
  12745. NULL);
  12746. mutex_unlock(&dev->struct_mutex);
  12747. if (ret) {
  12748. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12749. to_intel_crtc(c)->pipe);
  12750. drm_framebuffer_unreference(c->primary->fb);
  12751. c->primary->fb = NULL;
  12752. update_state_fb(c->primary);
  12753. }
  12754. }
  12755. intel_backlight_register(dev);
  12756. }
  12757. void intel_connector_unregister(struct intel_connector *intel_connector)
  12758. {
  12759. struct drm_connector *connector = &intel_connector->base;
  12760. intel_panel_destroy_backlight(connector);
  12761. drm_connector_unregister(connector);
  12762. }
  12763. void intel_modeset_cleanup(struct drm_device *dev)
  12764. {
  12765. struct drm_i915_private *dev_priv = dev->dev_private;
  12766. struct drm_connector *connector;
  12767. intel_disable_gt_powersave(dev);
  12768. intel_backlight_unregister(dev);
  12769. /*
  12770. * Interrupts and polling as the first thing to avoid creating havoc.
  12771. * Too much stuff here (turning of connectors, ...) would
  12772. * experience fancy races otherwise.
  12773. */
  12774. intel_irq_uninstall(dev_priv);
  12775. /*
  12776. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12777. * poll handlers. Hence disable polling after hpd handling is shut down.
  12778. */
  12779. drm_kms_helper_poll_fini(dev);
  12780. mutex_lock(&dev->struct_mutex);
  12781. intel_unregister_dsm_handler();
  12782. intel_fbc_disable(dev);
  12783. mutex_unlock(&dev->struct_mutex);
  12784. /* flush any delayed tasks or pending work */
  12785. flush_scheduled_work();
  12786. /* destroy the backlight and sysfs files before encoders/connectors */
  12787. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12788. struct intel_connector *intel_connector;
  12789. intel_connector = to_intel_connector(connector);
  12790. intel_connector->unregister(intel_connector);
  12791. }
  12792. drm_mode_config_cleanup(dev);
  12793. intel_cleanup_overlay(dev);
  12794. mutex_lock(&dev->struct_mutex);
  12795. intel_cleanup_gt_powersave(dev);
  12796. mutex_unlock(&dev->struct_mutex);
  12797. }
  12798. /*
  12799. * Return which encoder is currently attached for connector.
  12800. */
  12801. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12802. {
  12803. return &intel_attached_encoder(connector)->base;
  12804. }
  12805. void intel_connector_attach_encoder(struct intel_connector *connector,
  12806. struct intel_encoder *encoder)
  12807. {
  12808. connector->encoder = encoder;
  12809. drm_mode_connector_attach_encoder(&connector->base,
  12810. &encoder->base);
  12811. }
  12812. /*
  12813. * set vga decode state - true == enable VGA decode
  12814. */
  12815. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12816. {
  12817. struct drm_i915_private *dev_priv = dev->dev_private;
  12818. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12819. u16 gmch_ctrl;
  12820. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12821. DRM_ERROR("failed to read control word\n");
  12822. return -EIO;
  12823. }
  12824. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12825. return 0;
  12826. if (state)
  12827. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12828. else
  12829. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12830. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12831. DRM_ERROR("failed to write control word\n");
  12832. return -EIO;
  12833. }
  12834. return 0;
  12835. }
  12836. struct intel_display_error_state {
  12837. u32 power_well_driver;
  12838. int num_transcoders;
  12839. struct intel_cursor_error_state {
  12840. u32 control;
  12841. u32 position;
  12842. u32 base;
  12843. u32 size;
  12844. } cursor[I915_MAX_PIPES];
  12845. struct intel_pipe_error_state {
  12846. bool power_domain_on;
  12847. u32 source;
  12848. u32 stat;
  12849. } pipe[I915_MAX_PIPES];
  12850. struct intel_plane_error_state {
  12851. u32 control;
  12852. u32 stride;
  12853. u32 size;
  12854. u32 pos;
  12855. u32 addr;
  12856. u32 surface;
  12857. u32 tile_offset;
  12858. } plane[I915_MAX_PIPES];
  12859. struct intel_transcoder_error_state {
  12860. bool power_domain_on;
  12861. enum transcoder cpu_transcoder;
  12862. u32 conf;
  12863. u32 htotal;
  12864. u32 hblank;
  12865. u32 hsync;
  12866. u32 vtotal;
  12867. u32 vblank;
  12868. u32 vsync;
  12869. } transcoder[4];
  12870. };
  12871. struct intel_display_error_state *
  12872. intel_display_capture_error_state(struct drm_device *dev)
  12873. {
  12874. struct drm_i915_private *dev_priv = dev->dev_private;
  12875. struct intel_display_error_state *error;
  12876. int transcoders[] = {
  12877. TRANSCODER_A,
  12878. TRANSCODER_B,
  12879. TRANSCODER_C,
  12880. TRANSCODER_EDP,
  12881. };
  12882. int i;
  12883. if (INTEL_INFO(dev)->num_pipes == 0)
  12884. return NULL;
  12885. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12886. if (error == NULL)
  12887. return NULL;
  12888. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12889. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12890. for_each_pipe(dev_priv, i) {
  12891. error->pipe[i].power_domain_on =
  12892. __intel_display_power_is_enabled(dev_priv,
  12893. POWER_DOMAIN_PIPE(i));
  12894. if (!error->pipe[i].power_domain_on)
  12895. continue;
  12896. error->cursor[i].control = I915_READ(CURCNTR(i));
  12897. error->cursor[i].position = I915_READ(CURPOS(i));
  12898. error->cursor[i].base = I915_READ(CURBASE(i));
  12899. error->plane[i].control = I915_READ(DSPCNTR(i));
  12900. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12901. if (INTEL_INFO(dev)->gen <= 3) {
  12902. error->plane[i].size = I915_READ(DSPSIZE(i));
  12903. error->plane[i].pos = I915_READ(DSPPOS(i));
  12904. }
  12905. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12906. error->plane[i].addr = I915_READ(DSPADDR(i));
  12907. if (INTEL_INFO(dev)->gen >= 4) {
  12908. error->plane[i].surface = I915_READ(DSPSURF(i));
  12909. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12910. }
  12911. error->pipe[i].source = I915_READ(PIPESRC(i));
  12912. if (HAS_GMCH_DISPLAY(dev))
  12913. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12914. }
  12915. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  12916. if (HAS_DDI(dev_priv->dev))
  12917. error->num_transcoders++; /* Account for eDP. */
  12918. for (i = 0; i < error->num_transcoders; i++) {
  12919. enum transcoder cpu_transcoder = transcoders[i];
  12920. error->transcoder[i].power_domain_on =
  12921. __intel_display_power_is_enabled(dev_priv,
  12922. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12923. if (!error->transcoder[i].power_domain_on)
  12924. continue;
  12925. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12926. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12927. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12928. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12929. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12930. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12931. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12932. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12933. }
  12934. return error;
  12935. }
  12936. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12937. void
  12938. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12939. struct drm_device *dev,
  12940. struct intel_display_error_state *error)
  12941. {
  12942. struct drm_i915_private *dev_priv = dev->dev_private;
  12943. int i;
  12944. if (!error)
  12945. return;
  12946. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  12947. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12948. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12949. error->power_well_driver);
  12950. for_each_pipe(dev_priv, i) {
  12951. err_printf(m, "Pipe [%d]:\n", i);
  12952. err_printf(m, " Power: %s\n",
  12953. error->pipe[i].power_domain_on ? "on" : "off");
  12954. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12955. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12956. err_printf(m, "Plane [%d]:\n", i);
  12957. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12958. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12959. if (INTEL_INFO(dev)->gen <= 3) {
  12960. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12961. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12962. }
  12963. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12964. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12965. if (INTEL_INFO(dev)->gen >= 4) {
  12966. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12967. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12968. }
  12969. err_printf(m, "Cursor [%d]:\n", i);
  12970. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12971. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12972. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12973. }
  12974. for (i = 0; i < error->num_transcoders; i++) {
  12975. err_printf(m, "CPU transcoder: %c\n",
  12976. transcoder_name(error->transcoder[i].cpu_transcoder));
  12977. err_printf(m, " Power: %s\n",
  12978. error->transcoder[i].power_domain_on ? "on" : "off");
  12979. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12980. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12981. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12982. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12983. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12984. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12985. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12986. }
  12987. }
  12988. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  12989. {
  12990. struct intel_crtc *crtc;
  12991. for_each_intel_crtc(dev, crtc) {
  12992. struct intel_unpin_work *work;
  12993. spin_lock_irq(&dev->event_lock);
  12994. work = crtc->unpin_work;
  12995. if (work && work->event &&
  12996. work->event->base.file_priv == file) {
  12997. kfree(work->event);
  12998. work->event = NULL;
  12999. }
  13000. spin_unlock_irq(&dev->event_lock);
  13001. }
  13002. }