intel_display.c 430 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543
  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 void intel_crtc_enable_planes(struct drm_crtc *crtc);
  103. static void intel_crtc_disable_planes(struct drm_crtc *crtc);
  104. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  105. {
  106. if (!connector->mst_port)
  107. return connector->encoder;
  108. else
  109. return &connector->mst_port->mst_encoders[pipe]->base;
  110. }
  111. typedef struct {
  112. int min, max;
  113. } intel_range_t;
  114. typedef struct {
  115. int dot_limit;
  116. int p2_slow, p2_fast;
  117. } intel_p2_t;
  118. typedef struct intel_limit intel_limit_t;
  119. struct intel_limit {
  120. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  121. intel_p2_t p2;
  122. };
  123. int
  124. intel_pch_rawclk(struct drm_device *dev)
  125. {
  126. struct drm_i915_private *dev_priv = dev->dev_private;
  127. WARN_ON(!HAS_PCH_SPLIT(dev));
  128. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  129. }
  130. static inline u32 /* units of 100MHz */
  131. intel_fdi_link_freq(struct drm_device *dev)
  132. {
  133. if (IS_GEN5(dev)) {
  134. struct drm_i915_private *dev_priv = dev->dev_private;
  135. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  136. } else
  137. return 27;
  138. }
  139. static const intel_limit_t intel_limits_i8xx_dac = {
  140. .dot = { .min = 25000, .max = 350000 },
  141. .vco = { .min = 908000, .max = 1512000 },
  142. .n = { .min = 2, .max = 16 },
  143. .m = { .min = 96, .max = 140 },
  144. .m1 = { .min = 18, .max = 26 },
  145. .m2 = { .min = 6, .max = 16 },
  146. .p = { .min = 4, .max = 128 },
  147. .p1 = { .min = 2, .max = 33 },
  148. .p2 = { .dot_limit = 165000,
  149. .p2_slow = 4, .p2_fast = 2 },
  150. };
  151. static const intel_limit_t intel_limits_i8xx_dvo = {
  152. .dot = { .min = 25000, .max = 350000 },
  153. .vco = { .min = 908000, .max = 1512000 },
  154. .n = { .min = 2, .max = 16 },
  155. .m = { .min = 96, .max = 140 },
  156. .m1 = { .min = 18, .max = 26 },
  157. .m2 = { .min = 6, .max = 16 },
  158. .p = { .min = 4, .max = 128 },
  159. .p1 = { .min = 2, .max = 33 },
  160. .p2 = { .dot_limit = 165000,
  161. .p2_slow = 4, .p2_fast = 4 },
  162. };
  163. static const intel_limit_t intel_limits_i8xx_lvds = {
  164. .dot = { .min = 25000, .max = 350000 },
  165. .vco = { .min = 908000, .max = 1512000 },
  166. .n = { .min = 2, .max = 16 },
  167. .m = { .min = 96, .max = 140 },
  168. .m1 = { .min = 18, .max = 26 },
  169. .m2 = { .min = 6, .max = 16 },
  170. .p = { .min = 4, .max = 128 },
  171. .p1 = { .min = 1, .max = 6 },
  172. .p2 = { .dot_limit = 165000,
  173. .p2_slow = 14, .p2_fast = 7 },
  174. };
  175. static const intel_limit_t intel_limits_i9xx_sdvo = {
  176. .dot = { .min = 20000, .max = 400000 },
  177. .vco = { .min = 1400000, .max = 2800000 },
  178. .n = { .min = 1, .max = 6 },
  179. .m = { .min = 70, .max = 120 },
  180. .m1 = { .min = 8, .max = 18 },
  181. .m2 = { .min = 3, .max = 7 },
  182. .p = { .min = 5, .max = 80 },
  183. .p1 = { .min = 1, .max = 8 },
  184. .p2 = { .dot_limit = 200000,
  185. .p2_slow = 10, .p2_fast = 5 },
  186. };
  187. static const intel_limit_t intel_limits_i9xx_lvds = {
  188. .dot = { .min = 20000, .max = 400000 },
  189. .vco = { .min = 1400000, .max = 2800000 },
  190. .n = { .min = 1, .max = 6 },
  191. .m = { .min = 70, .max = 120 },
  192. .m1 = { .min = 8, .max = 18 },
  193. .m2 = { .min = 3, .max = 7 },
  194. .p = { .min = 7, .max = 98 },
  195. .p1 = { .min = 1, .max = 8 },
  196. .p2 = { .dot_limit = 112000,
  197. .p2_slow = 14, .p2_fast = 7 },
  198. };
  199. static const intel_limit_t intel_limits_g4x_sdvo = {
  200. .dot = { .min = 25000, .max = 270000 },
  201. .vco = { .min = 1750000, .max = 3500000},
  202. .n = { .min = 1, .max = 4 },
  203. .m = { .min = 104, .max = 138 },
  204. .m1 = { .min = 17, .max = 23 },
  205. .m2 = { .min = 5, .max = 11 },
  206. .p = { .min = 10, .max = 30 },
  207. .p1 = { .min = 1, .max = 3},
  208. .p2 = { .dot_limit = 270000,
  209. .p2_slow = 10,
  210. .p2_fast = 10
  211. },
  212. };
  213. static const intel_limit_t intel_limits_g4x_hdmi = {
  214. .dot = { .min = 22000, .max = 400000 },
  215. .vco = { .min = 1750000, .max = 3500000},
  216. .n = { .min = 1, .max = 4 },
  217. .m = { .min = 104, .max = 138 },
  218. .m1 = { .min = 16, .max = 23 },
  219. .m2 = { .min = 5, .max = 11 },
  220. .p = { .min = 5, .max = 80 },
  221. .p1 = { .min = 1, .max = 8},
  222. .p2 = { .dot_limit = 165000,
  223. .p2_slow = 10, .p2_fast = 5 },
  224. };
  225. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  226. .dot = { .min = 20000, .max = 115000 },
  227. .vco = { .min = 1750000, .max = 3500000 },
  228. .n = { .min = 1, .max = 3 },
  229. .m = { .min = 104, .max = 138 },
  230. .m1 = { .min = 17, .max = 23 },
  231. .m2 = { .min = 5, .max = 11 },
  232. .p = { .min = 28, .max = 112 },
  233. .p1 = { .min = 2, .max = 8 },
  234. .p2 = { .dot_limit = 0,
  235. .p2_slow = 14, .p2_fast = 14
  236. },
  237. };
  238. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  239. .dot = { .min = 80000, .max = 224000 },
  240. .vco = { .min = 1750000, .max = 3500000 },
  241. .n = { .min = 1, .max = 3 },
  242. .m = { .min = 104, .max = 138 },
  243. .m1 = { .min = 17, .max = 23 },
  244. .m2 = { .min = 5, .max = 11 },
  245. .p = { .min = 14, .max = 42 },
  246. .p1 = { .min = 2, .max = 6 },
  247. .p2 = { .dot_limit = 0,
  248. .p2_slow = 7, .p2_fast = 7
  249. },
  250. };
  251. static const intel_limit_t intel_limits_pineview_sdvo = {
  252. .dot = { .min = 20000, .max = 400000},
  253. .vco = { .min = 1700000, .max = 3500000 },
  254. /* Pineview's Ncounter is a ring counter */
  255. .n = { .min = 3, .max = 6 },
  256. .m = { .min = 2, .max = 256 },
  257. /* Pineview only has one combined m divider, which we treat as m2. */
  258. .m1 = { .min = 0, .max = 0 },
  259. .m2 = { .min = 0, .max = 254 },
  260. .p = { .min = 5, .max = 80 },
  261. .p1 = { .min = 1, .max = 8 },
  262. .p2 = { .dot_limit = 200000,
  263. .p2_slow = 10, .p2_fast = 5 },
  264. };
  265. static const intel_limit_t intel_limits_pineview_lvds = {
  266. .dot = { .min = 20000, .max = 400000 },
  267. .vco = { .min = 1700000, .max = 3500000 },
  268. .n = { .min = 3, .max = 6 },
  269. .m = { .min = 2, .max = 256 },
  270. .m1 = { .min = 0, .max = 0 },
  271. .m2 = { .min = 0, .max = 254 },
  272. .p = { .min = 7, .max = 112 },
  273. .p1 = { .min = 1, .max = 8 },
  274. .p2 = { .dot_limit = 112000,
  275. .p2_slow = 14, .p2_fast = 14 },
  276. };
  277. /* Ironlake / Sandybridge
  278. *
  279. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  280. * the range value for them is (actual_value - 2).
  281. */
  282. static const intel_limit_t intel_limits_ironlake_dac = {
  283. .dot = { .min = 25000, .max = 350000 },
  284. .vco = { .min = 1760000, .max = 3510000 },
  285. .n = { .min = 1, .max = 5 },
  286. .m = { .min = 79, .max = 127 },
  287. .m1 = { .min = 12, .max = 22 },
  288. .m2 = { .min = 5, .max = 9 },
  289. .p = { .min = 5, .max = 80 },
  290. .p1 = { .min = 1, .max = 8 },
  291. .p2 = { .dot_limit = 225000,
  292. .p2_slow = 10, .p2_fast = 5 },
  293. };
  294. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  295. .dot = { .min = 25000, .max = 350000 },
  296. .vco = { .min = 1760000, .max = 3510000 },
  297. .n = { .min = 1, .max = 3 },
  298. .m = { .min = 79, .max = 118 },
  299. .m1 = { .min = 12, .max = 22 },
  300. .m2 = { .min = 5, .max = 9 },
  301. .p = { .min = 28, .max = 112 },
  302. .p1 = { .min = 2, .max = 8 },
  303. .p2 = { .dot_limit = 225000,
  304. .p2_slow = 14, .p2_fast = 14 },
  305. };
  306. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  307. .dot = { .min = 25000, .max = 350000 },
  308. .vco = { .min = 1760000, .max = 3510000 },
  309. .n = { .min = 1, .max = 3 },
  310. .m = { .min = 79, .max = 127 },
  311. .m1 = { .min = 12, .max = 22 },
  312. .m2 = { .min = 5, .max = 9 },
  313. .p = { .min = 14, .max = 56 },
  314. .p1 = { .min = 2, .max = 8 },
  315. .p2 = { .dot_limit = 225000,
  316. .p2_slow = 7, .p2_fast = 7 },
  317. };
  318. /* LVDS 100mhz refclk limits. */
  319. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  320. .dot = { .min = 25000, .max = 350000 },
  321. .vco = { .min = 1760000, .max = 3510000 },
  322. .n = { .min = 1, .max = 2 },
  323. .m = { .min = 79, .max = 126 },
  324. .m1 = { .min = 12, .max = 22 },
  325. .m2 = { .min = 5, .max = 9 },
  326. .p = { .min = 28, .max = 112 },
  327. .p1 = { .min = 2, .max = 8 },
  328. .p2 = { .dot_limit = 225000,
  329. .p2_slow = 14, .p2_fast = 14 },
  330. };
  331. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  332. .dot = { .min = 25000, .max = 350000 },
  333. .vco = { .min = 1760000, .max = 3510000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 79, .max = 126 },
  336. .m1 = { .min = 12, .max = 22 },
  337. .m2 = { .min = 5, .max = 9 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 225000,
  341. .p2_slow = 7, .p2_fast = 7 },
  342. };
  343. static const intel_limit_t intel_limits_vlv = {
  344. /*
  345. * These are the data rate limits (measured in fast clocks)
  346. * since those are the strictest limits we have. The fast
  347. * clock and actual rate limits are more relaxed, so checking
  348. * them would make no difference.
  349. */
  350. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  351. .vco = { .min = 4000000, .max = 6000000 },
  352. .n = { .min = 1, .max = 7 },
  353. .m1 = { .min = 2, .max = 3 },
  354. .m2 = { .min = 11, .max = 156 },
  355. .p1 = { .min = 2, .max = 3 },
  356. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  357. };
  358. static const intel_limit_t intel_limits_chv = {
  359. /*
  360. * These are the data rate limits (measured in fast clocks)
  361. * since those are the strictest limits we have. The fast
  362. * clock and actual rate limits are more relaxed, so checking
  363. * them would make no difference.
  364. */
  365. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  366. .vco = { .min = 4800000, .max = 6480000 },
  367. .n = { .min = 1, .max = 1 },
  368. .m1 = { .min = 2, .max = 2 },
  369. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  370. .p1 = { .min = 2, .max = 4 },
  371. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  372. };
  373. static const intel_limit_t intel_limits_bxt = {
  374. /* FIXME: find real dot limits */
  375. .dot = { .min = 0, .max = INT_MAX },
  376. .vco = { .min = 4800000, .max = 6480000 },
  377. .n = { .min = 1, .max = 1 },
  378. .m1 = { .min = 2, .max = 2 },
  379. /* FIXME: find real m2 limits */
  380. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  381. .p1 = { .min = 2, .max = 4 },
  382. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  383. };
  384. static void vlv_clock(int refclk, intel_clock_t *clock)
  385. {
  386. clock->m = clock->m1 * clock->m2;
  387. clock->p = clock->p1 * clock->p2;
  388. if (WARN_ON(clock->n == 0 || clock->p == 0))
  389. return;
  390. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  391. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  392. }
  393. /**
  394. * Returns whether any output on the specified pipe is of the specified type
  395. */
  396. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  397. {
  398. struct drm_device *dev = crtc->base.dev;
  399. struct intel_encoder *encoder;
  400. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  401. if (encoder->type == type)
  402. return true;
  403. return false;
  404. }
  405. /**
  406. * Returns whether any output on the specified pipe will have the specified
  407. * type after a staged modeset is complete, i.e., the same as
  408. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  409. * encoder->crtc.
  410. */
  411. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  412. int type)
  413. {
  414. struct drm_atomic_state *state = crtc_state->base.state;
  415. struct drm_connector_state *connector_state;
  416. struct intel_encoder *encoder;
  417. int i, num_connectors = 0;
  418. for (i = 0; i < state->num_connector; i++) {
  419. if (!state->connectors[i])
  420. continue;
  421. connector_state = state->connector_states[i];
  422. if (connector_state->crtc != crtc_state->base.crtc)
  423. continue;
  424. num_connectors++;
  425. encoder = to_intel_encoder(connector_state->best_encoder);
  426. if (encoder->type == type)
  427. return true;
  428. }
  429. WARN_ON(num_connectors == 0);
  430. return false;
  431. }
  432. static const intel_limit_t *
  433. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  434. {
  435. struct drm_device *dev = crtc_state->base.crtc->dev;
  436. const intel_limit_t *limit;
  437. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  438. if (intel_is_dual_link_lvds(dev)) {
  439. if (refclk == 100000)
  440. limit = &intel_limits_ironlake_dual_lvds_100m;
  441. else
  442. limit = &intel_limits_ironlake_dual_lvds;
  443. } else {
  444. if (refclk == 100000)
  445. limit = &intel_limits_ironlake_single_lvds_100m;
  446. else
  447. limit = &intel_limits_ironlake_single_lvds;
  448. }
  449. } else
  450. limit = &intel_limits_ironlake_dac;
  451. return limit;
  452. }
  453. static const intel_limit_t *
  454. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  455. {
  456. struct drm_device *dev = crtc_state->base.crtc->dev;
  457. const intel_limit_t *limit;
  458. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  459. if (intel_is_dual_link_lvds(dev))
  460. limit = &intel_limits_g4x_dual_channel_lvds;
  461. else
  462. limit = &intel_limits_g4x_single_channel_lvds;
  463. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  464. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  465. limit = &intel_limits_g4x_hdmi;
  466. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  467. limit = &intel_limits_g4x_sdvo;
  468. } else /* The option is for other outputs */
  469. limit = &intel_limits_i9xx_sdvo;
  470. return limit;
  471. }
  472. static const intel_limit_t *
  473. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  474. {
  475. struct drm_device *dev = crtc_state->base.crtc->dev;
  476. const intel_limit_t *limit;
  477. if (IS_BROXTON(dev))
  478. limit = &intel_limits_bxt;
  479. else if (HAS_PCH_SPLIT(dev))
  480. limit = intel_ironlake_limit(crtc_state, refclk);
  481. else if (IS_G4X(dev)) {
  482. limit = intel_g4x_limit(crtc_state);
  483. } else if (IS_PINEVIEW(dev)) {
  484. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  485. limit = &intel_limits_pineview_lvds;
  486. else
  487. limit = &intel_limits_pineview_sdvo;
  488. } else if (IS_CHERRYVIEW(dev)) {
  489. limit = &intel_limits_chv;
  490. } else if (IS_VALLEYVIEW(dev)) {
  491. limit = &intel_limits_vlv;
  492. } else if (!IS_GEN2(dev)) {
  493. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  494. limit = &intel_limits_i9xx_lvds;
  495. else
  496. limit = &intel_limits_i9xx_sdvo;
  497. } else {
  498. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  499. limit = &intel_limits_i8xx_lvds;
  500. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  501. limit = &intel_limits_i8xx_dvo;
  502. else
  503. limit = &intel_limits_i8xx_dac;
  504. }
  505. return limit;
  506. }
  507. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  508. static void pineview_clock(int refclk, intel_clock_t *clock)
  509. {
  510. clock->m = clock->m2 + 2;
  511. clock->p = clock->p1 * clock->p2;
  512. if (WARN_ON(clock->n == 0 || clock->p == 0))
  513. return;
  514. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  515. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  516. }
  517. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  518. {
  519. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  520. }
  521. static void i9xx_clock(int refclk, intel_clock_t *clock)
  522. {
  523. clock->m = i9xx_dpll_compute_m(clock);
  524. clock->p = clock->p1 * clock->p2;
  525. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  526. return;
  527. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  528. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  529. }
  530. static void chv_clock(int refclk, intel_clock_t *clock)
  531. {
  532. clock->m = clock->m1 * clock->m2;
  533. clock->p = clock->p1 * clock->p2;
  534. if (WARN_ON(clock->n == 0 || clock->p == 0))
  535. return;
  536. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  537. clock->n << 22);
  538. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  539. }
  540. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  541. /**
  542. * Returns whether the given set of divisors are valid for a given refclk with
  543. * the given connectors.
  544. */
  545. static bool intel_PLL_is_valid(struct drm_device *dev,
  546. const intel_limit_t *limit,
  547. const intel_clock_t *clock)
  548. {
  549. if (clock->n < limit->n.min || limit->n.max < clock->n)
  550. INTELPllInvalid("n out of range\n");
  551. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  552. INTELPllInvalid("p1 out of range\n");
  553. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  554. INTELPllInvalid("m2 out of range\n");
  555. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  556. INTELPllInvalid("m1 out of range\n");
  557. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  558. if (clock->m1 <= clock->m2)
  559. INTELPllInvalid("m1 <= m2\n");
  560. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  561. if (clock->p < limit->p.min || limit->p.max < clock->p)
  562. INTELPllInvalid("p out of range\n");
  563. if (clock->m < limit->m.min || limit->m.max < clock->m)
  564. INTELPllInvalid("m out of range\n");
  565. }
  566. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  567. INTELPllInvalid("vco out of range\n");
  568. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  569. * connector, etc., rather than just a single range.
  570. */
  571. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  572. INTELPllInvalid("dot out of range\n");
  573. return true;
  574. }
  575. static bool
  576. i9xx_find_best_dpll(const intel_limit_t *limit,
  577. struct intel_crtc_state *crtc_state,
  578. int target, int refclk, intel_clock_t *match_clock,
  579. intel_clock_t *best_clock)
  580. {
  581. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  582. struct drm_device *dev = crtc->base.dev;
  583. intel_clock_t clock;
  584. int err = target;
  585. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  586. /*
  587. * For LVDS just rely on its current settings for dual-channel.
  588. * We haven't figured out how to reliably set up different
  589. * single/dual channel state, if we even can.
  590. */
  591. if (intel_is_dual_link_lvds(dev))
  592. clock.p2 = limit->p2.p2_fast;
  593. else
  594. clock.p2 = limit->p2.p2_slow;
  595. } else {
  596. if (target < limit->p2.dot_limit)
  597. clock.p2 = limit->p2.p2_slow;
  598. else
  599. clock.p2 = limit->p2.p2_fast;
  600. }
  601. memset(best_clock, 0, sizeof(*best_clock));
  602. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  603. clock.m1++) {
  604. for (clock.m2 = limit->m2.min;
  605. clock.m2 <= limit->m2.max; clock.m2++) {
  606. if (clock.m2 >= clock.m1)
  607. break;
  608. for (clock.n = limit->n.min;
  609. clock.n <= limit->n.max; clock.n++) {
  610. for (clock.p1 = limit->p1.min;
  611. clock.p1 <= limit->p1.max; clock.p1++) {
  612. int this_err;
  613. i9xx_clock(refclk, &clock);
  614. if (!intel_PLL_is_valid(dev, limit,
  615. &clock))
  616. continue;
  617. if (match_clock &&
  618. clock.p != match_clock->p)
  619. continue;
  620. this_err = abs(clock.dot - target);
  621. if (this_err < err) {
  622. *best_clock = clock;
  623. err = this_err;
  624. }
  625. }
  626. }
  627. }
  628. }
  629. return (err != target);
  630. }
  631. static bool
  632. pnv_find_best_dpll(const intel_limit_t *limit,
  633. struct intel_crtc_state *crtc_state,
  634. int target, int refclk, intel_clock_t *match_clock,
  635. intel_clock_t *best_clock)
  636. {
  637. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  638. struct drm_device *dev = crtc->base.dev;
  639. intel_clock_t clock;
  640. int err = target;
  641. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  642. /*
  643. * For LVDS just rely on its current settings for dual-channel.
  644. * We haven't figured out how to reliably set up different
  645. * single/dual channel state, if we even can.
  646. */
  647. if (intel_is_dual_link_lvds(dev))
  648. clock.p2 = limit->p2.p2_fast;
  649. else
  650. clock.p2 = limit->p2.p2_slow;
  651. } else {
  652. if (target < limit->p2.dot_limit)
  653. clock.p2 = limit->p2.p2_slow;
  654. else
  655. clock.p2 = limit->p2.p2_fast;
  656. }
  657. memset(best_clock, 0, sizeof(*best_clock));
  658. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  659. clock.m1++) {
  660. for (clock.m2 = limit->m2.min;
  661. clock.m2 <= limit->m2.max; clock.m2++) {
  662. for (clock.n = limit->n.min;
  663. clock.n <= limit->n.max; clock.n++) {
  664. for (clock.p1 = limit->p1.min;
  665. clock.p1 <= limit->p1.max; clock.p1++) {
  666. int this_err;
  667. pineview_clock(refclk, &clock);
  668. if (!intel_PLL_is_valid(dev, limit,
  669. &clock))
  670. continue;
  671. if (match_clock &&
  672. clock.p != match_clock->p)
  673. continue;
  674. this_err = abs(clock.dot - target);
  675. if (this_err < err) {
  676. *best_clock = clock;
  677. err = this_err;
  678. }
  679. }
  680. }
  681. }
  682. }
  683. return (err != target);
  684. }
  685. static bool
  686. g4x_find_best_dpll(const intel_limit_t *limit,
  687. struct intel_crtc_state *crtc_state,
  688. int target, int refclk, intel_clock_t *match_clock,
  689. intel_clock_t *best_clock)
  690. {
  691. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  692. struct drm_device *dev = crtc->base.dev;
  693. intel_clock_t clock;
  694. int max_n;
  695. bool found;
  696. /* approximately equals target * 0.00585 */
  697. int err_most = (target >> 8) + (target >> 9);
  698. found = false;
  699. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  700. if (intel_is_dual_link_lvds(dev))
  701. clock.p2 = limit->p2.p2_fast;
  702. else
  703. clock.p2 = limit->p2.p2_slow;
  704. } else {
  705. if (target < limit->p2.dot_limit)
  706. clock.p2 = limit->p2.p2_slow;
  707. else
  708. clock.p2 = limit->p2.p2_fast;
  709. }
  710. memset(best_clock, 0, sizeof(*best_clock));
  711. max_n = limit->n.max;
  712. /* based on hardware requirement, prefer smaller n to precision */
  713. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  714. /* based on hardware requirement, prefere larger m1,m2 */
  715. for (clock.m1 = limit->m1.max;
  716. clock.m1 >= limit->m1.min; clock.m1--) {
  717. for (clock.m2 = limit->m2.max;
  718. clock.m2 >= limit->m2.min; clock.m2--) {
  719. for (clock.p1 = limit->p1.max;
  720. clock.p1 >= limit->p1.min; clock.p1--) {
  721. int this_err;
  722. i9xx_clock(refclk, &clock);
  723. if (!intel_PLL_is_valid(dev, limit,
  724. &clock))
  725. continue;
  726. this_err = abs(clock.dot - target);
  727. if (this_err < err_most) {
  728. *best_clock = clock;
  729. err_most = this_err;
  730. max_n = clock.n;
  731. found = true;
  732. }
  733. }
  734. }
  735. }
  736. }
  737. return found;
  738. }
  739. /*
  740. * Check if the calculated PLL configuration is more optimal compared to the
  741. * best configuration and error found so far. Return the calculated error.
  742. */
  743. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  744. const intel_clock_t *calculated_clock,
  745. const intel_clock_t *best_clock,
  746. unsigned int best_error_ppm,
  747. unsigned int *error_ppm)
  748. {
  749. /*
  750. * For CHV ignore the error and consider only the P value.
  751. * Prefer a bigger P value based on HW requirements.
  752. */
  753. if (IS_CHERRYVIEW(dev)) {
  754. *error_ppm = 0;
  755. return calculated_clock->p > best_clock->p;
  756. }
  757. if (WARN_ON_ONCE(!target_freq))
  758. return false;
  759. *error_ppm = div_u64(1000000ULL *
  760. abs(target_freq - calculated_clock->dot),
  761. target_freq);
  762. /*
  763. * Prefer a better P value over a better (smaller) error if the error
  764. * is small. Ensure this preference for future configurations too by
  765. * setting the error to 0.
  766. */
  767. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  768. *error_ppm = 0;
  769. return true;
  770. }
  771. return *error_ppm + 10 < best_error_ppm;
  772. }
  773. static bool
  774. vlv_find_best_dpll(const intel_limit_t *limit,
  775. struct intel_crtc_state *crtc_state,
  776. int target, int refclk, intel_clock_t *match_clock,
  777. intel_clock_t *best_clock)
  778. {
  779. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  780. struct drm_device *dev = crtc->base.dev;
  781. intel_clock_t clock;
  782. unsigned int bestppm = 1000000;
  783. /* min update 19.2 MHz */
  784. int max_n = min(limit->n.max, refclk / 19200);
  785. bool found = false;
  786. target *= 5; /* fast clock */
  787. memset(best_clock, 0, sizeof(*best_clock));
  788. /* based on hardware requirement, prefer smaller n to precision */
  789. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  790. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  791. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  792. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  793. clock.p = clock.p1 * clock.p2;
  794. /* based on hardware requirement, prefer bigger m1,m2 values */
  795. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  796. unsigned int ppm;
  797. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  798. refclk * clock.m1);
  799. vlv_clock(refclk, &clock);
  800. if (!intel_PLL_is_valid(dev, limit,
  801. &clock))
  802. continue;
  803. if (!vlv_PLL_is_optimal(dev, target,
  804. &clock,
  805. best_clock,
  806. bestppm, &ppm))
  807. continue;
  808. *best_clock = clock;
  809. bestppm = ppm;
  810. found = true;
  811. }
  812. }
  813. }
  814. }
  815. return found;
  816. }
  817. static bool
  818. chv_find_best_dpll(const intel_limit_t *limit,
  819. struct intel_crtc_state *crtc_state,
  820. int target, int refclk, intel_clock_t *match_clock,
  821. intel_clock_t *best_clock)
  822. {
  823. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  824. struct drm_device *dev = crtc->base.dev;
  825. unsigned int best_error_ppm;
  826. intel_clock_t clock;
  827. uint64_t m2;
  828. int found = false;
  829. memset(best_clock, 0, sizeof(*best_clock));
  830. best_error_ppm = 1000000;
  831. /*
  832. * Based on hardware doc, the n always set to 1, and m1 always
  833. * set to 2. If requires to support 200Mhz refclk, we need to
  834. * revisit this because n may not 1 anymore.
  835. */
  836. clock.n = 1, clock.m1 = 2;
  837. target *= 5; /* fast clock */
  838. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  839. for (clock.p2 = limit->p2.p2_fast;
  840. clock.p2 >= limit->p2.p2_slow;
  841. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  842. unsigned int error_ppm;
  843. clock.p = clock.p1 * clock.p2;
  844. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  845. clock.n) << 22, refclk * clock.m1);
  846. if (m2 > INT_MAX/clock.m1)
  847. continue;
  848. clock.m2 = m2;
  849. chv_clock(refclk, &clock);
  850. if (!intel_PLL_is_valid(dev, limit, &clock))
  851. continue;
  852. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  853. best_error_ppm, &error_ppm))
  854. continue;
  855. *best_clock = clock;
  856. best_error_ppm = error_ppm;
  857. found = true;
  858. }
  859. }
  860. return found;
  861. }
  862. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  863. intel_clock_t *best_clock)
  864. {
  865. int refclk = i9xx_get_refclk(crtc_state, 0);
  866. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  867. target_clock, refclk, NULL, best_clock);
  868. }
  869. bool intel_crtc_active(struct drm_crtc *crtc)
  870. {
  871. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  872. /* Be paranoid as we can arrive here with only partial
  873. * state retrieved from the hardware during setup.
  874. *
  875. * We can ditch the adjusted_mode.crtc_clock check as soon
  876. * as Haswell has gained clock readout/fastboot support.
  877. *
  878. * We can ditch the crtc->primary->fb check as soon as we can
  879. * properly reconstruct framebuffers.
  880. *
  881. * FIXME: The intel_crtc->active here should be switched to
  882. * crtc->state->active once we have proper CRTC states wired up
  883. * for atomic.
  884. */
  885. return intel_crtc->active && crtc->primary->state->fb &&
  886. intel_crtc->config->base.adjusted_mode.crtc_clock;
  887. }
  888. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  889. enum pipe pipe)
  890. {
  891. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  893. return intel_crtc->config->cpu_transcoder;
  894. }
  895. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  896. {
  897. struct drm_i915_private *dev_priv = dev->dev_private;
  898. u32 reg = PIPEDSL(pipe);
  899. u32 line1, line2;
  900. u32 line_mask;
  901. if (IS_GEN2(dev))
  902. line_mask = DSL_LINEMASK_GEN2;
  903. else
  904. line_mask = DSL_LINEMASK_GEN3;
  905. line1 = I915_READ(reg) & line_mask;
  906. mdelay(5);
  907. line2 = I915_READ(reg) & line_mask;
  908. return line1 == line2;
  909. }
  910. /*
  911. * intel_wait_for_pipe_off - wait for pipe to turn off
  912. * @crtc: crtc whose pipe to wait for
  913. *
  914. * After disabling a pipe, we can't wait for vblank in the usual way,
  915. * spinning on the vblank interrupt status bit, since we won't actually
  916. * see an interrupt when the pipe is disabled.
  917. *
  918. * On Gen4 and above:
  919. * wait for the pipe register state bit to turn off
  920. *
  921. * Otherwise:
  922. * wait for the display line value to settle (it usually
  923. * ends up stopping at the start of the next frame).
  924. *
  925. */
  926. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  927. {
  928. struct drm_device *dev = crtc->base.dev;
  929. struct drm_i915_private *dev_priv = dev->dev_private;
  930. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  931. enum pipe pipe = crtc->pipe;
  932. if (INTEL_INFO(dev)->gen >= 4) {
  933. int reg = PIPECONF(cpu_transcoder);
  934. /* Wait for the Pipe State to go off */
  935. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  936. 100))
  937. WARN(1, "pipe_off wait timed out\n");
  938. } else {
  939. /* Wait for the display line to settle */
  940. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  941. WARN(1, "pipe_off wait timed out\n");
  942. }
  943. }
  944. /*
  945. * ibx_digital_port_connected - is the specified port connected?
  946. * @dev_priv: i915 private structure
  947. * @port: the port to test
  948. *
  949. * Returns true if @port is connected, false otherwise.
  950. */
  951. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  952. struct intel_digital_port *port)
  953. {
  954. u32 bit;
  955. if (HAS_PCH_IBX(dev_priv->dev)) {
  956. switch (port->port) {
  957. case PORT_B:
  958. bit = SDE_PORTB_HOTPLUG;
  959. break;
  960. case PORT_C:
  961. bit = SDE_PORTC_HOTPLUG;
  962. break;
  963. case PORT_D:
  964. bit = SDE_PORTD_HOTPLUG;
  965. break;
  966. default:
  967. return true;
  968. }
  969. } else {
  970. switch (port->port) {
  971. case PORT_B:
  972. bit = SDE_PORTB_HOTPLUG_CPT;
  973. break;
  974. case PORT_C:
  975. bit = SDE_PORTC_HOTPLUG_CPT;
  976. break;
  977. case PORT_D:
  978. bit = SDE_PORTD_HOTPLUG_CPT;
  979. break;
  980. default:
  981. return true;
  982. }
  983. }
  984. return I915_READ(SDEISR) & bit;
  985. }
  986. static const char *state_string(bool enabled)
  987. {
  988. return enabled ? "on" : "off";
  989. }
  990. /* Only for pre-ILK configs */
  991. void assert_pll(struct drm_i915_private *dev_priv,
  992. enum pipe pipe, bool state)
  993. {
  994. int reg;
  995. u32 val;
  996. bool cur_state;
  997. reg = DPLL(pipe);
  998. val = I915_READ(reg);
  999. cur_state = !!(val & DPLL_VCO_ENABLE);
  1000. I915_STATE_WARN(cur_state != state,
  1001. "PLL state assertion failure (expected %s, current %s)\n",
  1002. state_string(state), state_string(cur_state));
  1003. }
  1004. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1005. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1006. {
  1007. u32 val;
  1008. bool cur_state;
  1009. mutex_lock(&dev_priv->dpio_lock);
  1010. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1011. mutex_unlock(&dev_priv->dpio_lock);
  1012. cur_state = val & DSI_PLL_VCO_EN;
  1013. I915_STATE_WARN(cur_state != state,
  1014. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1015. state_string(state), state_string(cur_state));
  1016. }
  1017. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1018. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1019. struct intel_shared_dpll *
  1020. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1021. {
  1022. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1023. if (crtc->config->shared_dpll < 0)
  1024. return NULL;
  1025. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1026. }
  1027. /* For ILK+ */
  1028. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1029. struct intel_shared_dpll *pll,
  1030. bool state)
  1031. {
  1032. bool cur_state;
  1033. struct intel_dpll_hw_state hw_state;
  1034. if (WARN (!pll,
  1035. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1036. return;
  1037. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1038. I915_STATE_WARN(cur_state != state,
  1039. "%s assertion failure (expected %s, current %s)\n",
  1040. pll->name, state_string(state), state_string(cur_state));
  1041. }
  1042. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1043. enum pipe pipe, bool state)
  1044. {
  1045. int reg;
  1046. u32 val;
  1047. bool cur_state;
  1048. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1049. pipe);
  1050. if (HAS_DDI(dev_priv->dev)) {
  1051. /* DDI does not have a specific FDI_TX register */
  1052. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1053. val = I915_READ(reg);
  1054. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1055. } else {
  1056. reg = FDI_TX_CTL(pipe);
  1057. val = I915_READ(reg);
  1058. cur_state = !!(val & FDI_TX_ENABLE);
  1059. }
  1060. I915_STATE_WARN(cur_state != state,
  1061. "FDI TX state assertion failure (expected %s, current %s)\n",
  1062. state_string(state), state_string(cur_state));
  1063. }
  1064. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1065. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1066. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1067. enum pipe pipe, bool state)
  1068. {
  1069. int reg;
  1070. u32 val;
  1071. bool cur_state;
  1072. reg = FDI_RX_CTL(pipe);
  1073. val = I915_READ(reg);
  1074. cur_state = !!(val & FDI_RX_ENABLE);
  1075. I915_STATE_WARN(cur_state != state,
  1076. "FDI RX state assertion failure (expected %s, current %s)\n",
  1077. state_string(state), state_string(cur_state));
  1078. }
  1079. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1080. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1081. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1082. enum pipe pipe)
  1083. {
  1084. int reg;
  1085. u32 val;
  1086. /* ILK FDI PLL is always enabled */
  1087. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1088. return;
  1089. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1090. if (HAS_DDI(dev_priv->dev))
  1091. return;
  1092. reg = FDI_TX_CTL(pipe);
  1093. val = I915_READ(reg);
  1094. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1095. }
  1096. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1097. enum pipe pipe, bool state)
  1098. {
  1099. int reg;
  1100. u32 val;
  1101. bool cur_state;
  1102. reg = FDI_RX_CTL(pipe);
  1103. val = I915_READ(reg);
  1104. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1105. I915_STATE_WARN(cur_state != state,
  1106. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1107. state_string(state), state_string(cur_state));
  1108. }
  1109. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1110. enum pipe pipe)
  1111. {
  1112. struct drm_device *dev = dev_priv->dev;
  1113. int pp_reg;
  1114. u32 val;
  1115. enum pipe panel_pipe = PIPE_A;
  1116. bool locked = true;
  1117. if (WARN_ON(HAS_DDI(dev)))
  1118. return;
  1119. if (HAS_PCH_SPLIT(dev)) {
  1120. u32 port_sel;
  1121. pp_reg = PCH_PP_CONTROL;
  1122. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1123. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1124. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1125. panel_pipe = PIPE_B;
  1126. /* XXX: else fix for eDP */
  1127. } else if (IS_VALLEYVIEW(dev)) {
  1128. /* presumably write lock depends on pipe, not port select */
  1129. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1130. panel_pipe = pipe;
  1131. } else {
  1132. pp_reg = PP_CONTROL;
  1133. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1134. panel_pipe = PIPE_B;
  1135. }
  1136. val = I915_READ(pp_reg);
  1137. if (!(val & PANEL_POWER_ON) ||
  1138. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1139. locked = false;
  1140. I915_STATE_WARN(panel_pipe == pipe && locked,
  1141. "panel assertion failure, pipe %c regs locked\n",
  1142. pipe_name(pipe));
  1143. }
  1144. static void assert_cursor(struct drm_i915_private *dev_priv,
  1145. enum pipe pipe, bool state)
  1146. {
  1147. struct drm_device *dev = dev_priv->dev;
  1148. bool cur_state;
  1149. if (IS_845G(dev) || IS_I865G(dev))
  1150. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1151. else
  1152. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1153. I915_STATE_WARN(cur_state != state,
  1154. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1155. pipe_name(pipe), state_string(state), state_string(cur_state));
  1156. }
  1157. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1158. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1159. void assert_pipe(struct drm_i915_private *dev_priv,
  1160. enum pipe pipe, bool state)
  1161. {
  1162. int reg;
  1163. u32 val;
  1164. bool cur_state;
  1165. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1166. pipe);
  1167. /* if we need the pipe quirk it must be always on */
  1168. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1169. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1170. state = true;
  1171. if (!intel_display_power_is_enabled(dev_priv,
  1172. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1173. cur_state = false;
  1174. } else {
  1175. reg = PIPECONF(cpu_transcoder);
  1176. val = I915_READ(reg);
  1177. cur_state = !!(val & PIPECONF_ENABLE);
  1178. }
  1179. I915_STATE_WARN(cur_state != state,
  1180. "pipe %c assertion failure (expected %s, current %s)\n",
  1181. pipe_name(pipe), state_string(state), state_string(cur_state));
  1182. }
  1183. static void assert_plane(struct drm_i915_private *dev_priv,
  1184. enum plane plane, bool state)
  1185. {
  1186. int reg;
  1187. u32 val;
  1188. bool cur_state;
  1189. reg = DSPCNTR(plane);
  1190. val = I915_READ(reg);
  1191. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1192. I915_STATE_WARN(cur_state != state,
  1193. "plane %c assertion failure (expected %s, current %s)\n",
  1194. plane_name(plane), state_string(state), state_string(cur_state));
  1195. }
  1196. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1197. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1198. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe)
  1200. {
  1201. struct drm_device *dev = dev_priv->dev;
  1202. int reg, i;
  1203. u32 val;
  1204. int cur_pipe;
  1205. /* Primary planes are fixed to pipes on gen4+ */
  1206. if (INTEL_INFO(dev)->gen >= 4) {
  1207. reg = DSPCNTR(pipe);
  1208. val = I915_READ(reg);
  1209. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1210. "plane %c assertion failure, should be disabled but not\n",
  1211. plane_name(pipe));
  1212. return;
  1213. }
  1214. /* Need to check both planes against the pipe */
  1215. for_each_pipe(dev_priv, i) {
  1216. reg = DSPCNTR(i);
  1217. val = I915_READ(reg);
  1218. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1219. DISPPLANE_SEL_PIPE_SHIFT;
  1220. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1221. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1222. plane_name(i), pipe_name(pipe));
  1223. }
  1224. }
  1225. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1226. enum pipe pipe)
  1227. {
  1228. struct drm_device *dev = dev_priv->dev;
  1229. int reg, sprite;
  1230. u32 val;
  1231. if (INTEL_INFO(dev)->gen >= 9) {
  1232. for_each_sprite(dev_priv, pipe, sprite) {
  1233. val = I915_READ(PLANE_CTL(pipe, sprite));
  1234. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1235. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1236. sprite, pipe_name(pipe));
  1237. }
  1238. } else if (IS_VALLEYVIEW(dev)) {
  1239. for_each_sprite(dev_priv, pipe, sprite) {
  1240. reg = SPCNTR(pipe, sprite);
  1241. val = I915_READ(reg);
  1242. I915_STATE_WARN(val & SP_ENABLE,
  1243. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1244. sprite_name(pipe, sprite), pipe_name(pipe));
  1245. }
  1246. } else if (INTEL_INFO(dev)->gen >= 7) {
  1247. reg = SPRCTL(pipe);
  1248. val = I915_READ(reg);
  1249. I915_STATE_WARN(val & SPRITE_ENABLE,
  1250. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1251. plane_name(pipe), pipe_name(pipe));
  1252. } else if (INTEL_INFO(dev)->gen >= 5) {
  1253. reg = DVSCNTR(pipe);
  1254. val = I915_READ(reg);
  1255. I915_STATE_WARN(val & DVS_ENABLE,
  1256. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1257. plane_name(pipe), pipe_name(pipe));
  1258. }
  1259. }
  1260. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1261. {
  1262. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1263. drm_crtc_vblank_put(crtc);
  1264. }
  1265. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1266. {
  1267. u32 val;
  1268. bool enabled;
  1269. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1270. val = I915_READ(PCH_DREF_CONTROL);
  1271. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1272. DREF_SUPERSPREAD_SOURCE_MASK));
  1273. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1274. }
  1275. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1276. enum pipe pipe)
  1277. {
  1278. int reg;
  1279. u32 val;
  1280. bool enabled;
  1281. reg = PCH_TRANSCONF(pipe);
  1282. val = I915_READ(reg);
  1283. enabled = !!(val & TRANS_ENABLE);
  1284. I915_STATE_WARN(enabled,
  1285. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1286. pipe_name(pipe));
  1287. }
  1288. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1289. enum pipe pipe, u32 port_sel, u32 val)
  1290. {
  1291. if ((val & DP_PORT_EN) == 0)
  1292. return false;
  1293. if (HAS_PCH_CPT(dev_priv->dev)) {
  1294. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1295. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1296. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1297. return false;
  1298. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1299. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1300. return false;
  1301. } else {
  1302. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1303. return false;
  1304. }
  1305. return true;
  1306. }
  1307. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1308. enum pipe pipe, u32 val)
  1309. {
  1310. if ((val & SDVO_ENABLE) == 0)
  1311. return false;
  1312. if (HAS_PCH_CPT(dev_priv->dev)) {
  1313. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1314. return false;
  1315. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1316. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1317. return false;
  1318. } else {
  1319. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1320. return false;
  1321. }
  1322. return true;
  1323. }
  1324. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1325. enum pipe pipe, u32 val)
  1326. {
  1327. if ((val & LVDS_PORT_EN) == 0)
  1328. return false;
  1329. if (HAS_PCH_CPT(dev_priv->dev)) {
  1330. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1331. return false;
  1332. } else {
  1333. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1334. return false;
  1335. }
  1336. return true;
  1337. }
  1338. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1339. enum pipe pipe, u32 val)
  1340. {
  1341. if ((val & ADPA_DAC_ENABLE) == 0)
  1342. return false;
  1343. if (HAS_PCH_CPT(dev_priv->dev)) {
  1344. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1345. return false;
  1346. } else {
  1347. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1348. return false;
  1349. }
  1350. return true;
  1351. }
  1352. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1353. enum pipe pipe, int reg, u32 port_sel)
  1354. {
  1355. u32 val = I915_READ(reg);
  1356. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1357. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1358. reg, pipe_name(pipe));
  1359. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1360. && (val & DP_PIPEB_SELECT),
  1361. "IBX PCH dp port still using transcoder B\n");
  1362. }
  1363. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1364. enum pipe pipe, int reg)
  1365. {
  1366. u32 val = I915_READ(reg);
  1367. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1368. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1369. reg, pipe_name(pipe));
  1370. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1371. && (val & SDVO_PIPE_B_SELECT),
  1372. "IBX PCH hdmi port still using transcoder B\n");
  1373. }
  1374. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1375. enum pipe pipe)
  1376. {
  1377. int reg;
  1378. u32 val;
  1379. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1380. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1381. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1382. reg = PCH_ADPA;
  1383. val = I915_READ(reg);
  1384. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1385. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1386. pipe_name(pipe));
  1387. reg = PCH_LVDS;
  1388. val = I915_READ(reg);
  1389. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1390. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1391. pipe_name(pipe));
  1392. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1393. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1394. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1395. }
  1396. static void intel_init_dpio(struct drm_device *dev)
  1397. {
  1398. struct drm_i915_private *dev_priv = dev->dev_private;
  1399. if (!IS_VALLEYVIEW(dev))
  1400. return;
  1401. /*
  1402. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1403. * CHV x1 PHY (DP/HDMI D)
  1404. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1405. */
  1406. if (IS_CHERRYVIEW(dev)) {
  1407. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1408. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1409. } else {
  1410. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1411. }
  1412. }
  1413. static void vlv_enable_pll(struct intel_crtc *crtc,
  1414. const struct intel_crtc_state *pipe_config)
  1415. {
  1416. struct drm_device *dev = crtc->base.dev;
  1417. struct drm_i915_private *dev_priv = dev->dev_private;
  1418. int reg = DPLL(crtc->pipe);
  1419. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1420. assert_pipe_disabled(dev_priv, crtc->pipe);
  1421. /* No really, not for ILK+ */
  1422. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1423. /* PLL is protected by panel, make sure we can write it */
  1424. if (IS_MOBILE(dev_priv->dev))
  1425. assert_panel_unlocked(dev_priv, crtc->pipe);
  1426. I915_WRITE(reg, dpll);
  1427. POSTING_READ(reg);
  1428. udelay(150);
  1429. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1430. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1431. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1432. POSTING_READ(DPLL_MD(crtc->pipe));
  1433. /* We do this three times for luck */
  1434. I915_WRITE(reg, dpll);
  1435. POSTING_READ(reg);
  1436. udelay(150); /* wait for warmup */
  1437. I915_WRITE(reg, dpll);
  1438. POSTING_READ(reg);
  1439. udelay(150); /* wait for warmup */
  1440. I915_WRITE(reg, dpll);
  1441. POSTING_READ(reg);
  1442. udelay(150); /* wait for warmup */
  1443. }
  1444. static void chv_enable_pll(struct intel_crtc *crtc,
  1445. const struct intel_crtc_state *pipe_config)
  1446. {
  1447. struct drm_device *dev = crtc->base.dev;
  1448. struct drm_i915_private *dev_priv = dev->dev_private;
  1449. int pipe = crtc->pipe;
  1450. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1451. u32 tmp;
  1452. assert_pipe_disabled(dev_priv, crtc->pipe);
  1453. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1454. mutex_lock(&dev_priv->dpio_lock);
  1455. /* Enable back the 10bit clock to display controller */
  1456. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1457. tmp |= DPIO_DCLKP_EN;
  1458. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1459. /*
  1460. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1461. */
  1462. udelay(1);
  1463. /* Enable PLL */
  1464. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1465. /* Check PLL is locked */
  1466. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1467. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1468. /* not sure when this should be written */
  1469. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1470. POSTING_READ(DPLL_MD(pipe));
  1471. mutex_unlock(&dev_priv->dpio_lock);
  1472. }
  1473. static int intel_num_dvo_pipes(struct drm_device *dev)
  1474. {
  1475. struct intel_crtc *crtc;
  1476. int count = 0;
  1477. for_each_intel_crtc(dev, crtc)
  1478. count += crtc->active &&
  1479. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1480. return count;
  1481. }
  1482. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1483. {
  1484. struct drm_device *dev = crtc->base.dev;
  1485. struct drm_i915_private *dev_priv = dev->dev_private;
  1486. int reg = DPLL(crtc->pipe);
  1487. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1488. assert_pipe_disabled(dev_priv, crtc->pipe);
  1489. /* No really, not for ILK+ */
  1490. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1491. /* PLL is protected by panel, make sure we can write it */
  1492. if (IS_MOBILE(dev) && !IS_I830(dev))
  1493. assert_panel_unlocked(dev_priv, crtc->pipe);
  1494. /* Enable DVO 2x clock on both PLLs if necessary */
  1495. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1496. /*
  1497. * It appears to be important that we don't enable this
  1498. * for the current pipe before otherwise configuring the
  1499. * PLL. No idea how this should be handled if multiple
  1500. * DVO outputs are enabled simultaneosly.
  1501. */
  1502. dpll |= DPLL_DVO_2X_MODE;
  1503. I915_WRITE(DPLL(!crtc->pipe),
  1504. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1505. }
  1506. /* Wait for the clocks to stabilize. */
  1507. POSTING_READ(reg);
  1508. udelay(150);
  1509. if (INTEL_INFO(dev)->gen >= 4) {
  1510. I915_WRITE(DPLL_MD(crtc->pipe),
  1511. crtc->config->dpll_hw_state.dpll_md);
  1512. } else {
  1513. /* The pixel multiplier can only be updated once the
  1514. * DPLL is enabled and the clocks are stable.
  1515. *
  1516. * So write it again.
  1517. */
  1518. I915_WRITE(reg, dpll);
  1519. }
  1520. /* We do this three times for luck */
  1521. I915_WRITE(reg, dpll);
  1522. POSTING_READ(reg);
  1523. udelay(150); /* wait for warmup */
  1524. I915_WRITE(reg, dpll);
  1525. POSTING_READ(reg);
  1526. udelay(150); /* wait for warmup */
  1527. I915_WRITE(reg, dpll);
  1528. POSTING_READ(reg);
  1529. udelay(150); /* wait for warmup */
  1530. }
  1531. /**
  1532. * i9xx_disable_pll - disable a PLL
  1533. * @dev_priv: i915 private structure
  1534. * @pipe: pipe PLL to disable
  1535. *
  1536. * Disable the PLL for @pipe, making sure the pipe is off first.
  1537. *
  1538. * Note! This is for pre-ILK only.
  1539. */
  1540. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1541. {
  1542. struct drm_device *dev = crtc->base.dev;
  1543. struct drm_i915_private *dev_priv = dev->dev_private;
  1544. enum pipe pipe = crtc->pipe;
  1545. /* Disable DVO 2x clock on both PLLs if necessary */
  1546. if (IS_I830(dev) &&
  1547. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1548. intel_num_dvo_pipes(dev) == 1) {
  1549. I915_WRITE(DPLL(PIPE_B),
  1550. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1551. I915_WRITE(DPLL(PIPE_A),
  1552. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1553. }
  1554. /* Don't disable pipe or pipe PLLs if needed */
  1555. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1556. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1557. return;
  1558. /* Make sure the pipe isn't still relying on us */
  1559. assert_pipe_disabled(dev_priv, pipe);
  1560. I915_WRITE(DPLL(pipe), 0);
  1561. POSTING_READ(DPLL(pipe));
  1562. }
  1563. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1564. {
  1565. u32 val = 0;
  1566. /* Make sure the pipe isn't still relying on us */
  1567. assert_pipe_disabled(dev_priv, pipe);
  1568. /*
  1569. * Leave integrated clock source and reference clock enabled for pipe B.
  1570. * The latter is needed for VGA hotplug / manual detection.
  1571. */
  1572. if (pipe == PIPE_B)
  1573. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1574. I915_WRITE(DPLL(pipe), val);
  1575. POSTING_READ(DPLL(pipe));
  1576. }
  1577. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1578. {
  1579. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1580. u32 val;
  1581. /* Make sure the pipe isn't still relying on us */
  1582. assert_pipe_disabled(dev_priv, pipe);
  1583. /* Set PLL en = 0 */
  1584. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1585. if (pipe != PIPE_A)
  1586. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1587. I915_WRITE(DPLL(pipe), val);
  1588. POSTING_READ(DPLL(pipe));
  1589. mutex_lock(&dev_priv->dpio_lock);
  1590. /* Disable 10bit clock to display controller */
  1591. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1592. val &= ~DPIO_DCLKP_EN;
  1593. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1594. /* disable left/right clock distribution */
  1595. if (pipe != PIPE_B) {
  1596. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1597. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1598. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1599. } else {
  1600. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1601. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1602. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1603. }
  1604. mutex_unlock(&dev_priv->dpio_lock);
  1605. }
  1606. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1607. struct intel_digital_port *dport)
  1608. {
  1609. u32 port_mask;
  1610. int dpll_reg;
  1611. switch (dport->port) {
  1612. case PORT_B:
  1613. port_mask = DPLL_PORTB_READY_MASK;
  1614. dpll_reg = DPLL(0);
  1615. break;
  1616. case PORT_C:
  1617. port_mask = DPLL_PORTC_READY_MASK;
  1618. dpll_reg = DPLL(0);
  1619. break;
  1620. case PORT_D:
  1621. port_mask = DPLL_PORTD_READY_MASK;
  1622. dpll_reg = DPIO_PHY_STATUS;
  1623. break;
  1624. default:
  1625. BUG();
  1626. }
  1627. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1628. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1629. port_name(dport->port), I915_READ(dpll_reg));
  1630. }
  1631. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1632. {
  1633. struct drm_device *dev = crtc->base.dev;
  1634. struct drm_i915_private *dev_priv = dev->dev_private;
  1635. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1636. if (WARN_ON(pll == NULL))
  1637. return;
  1638. WARN_ON(!pll->config.crtc_mask);
  1639. if (pll->active == 0) {
  1640. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1641. WARN_ON(pll->on);
  1642. assert_shared_dpll_disabled(dev_priv, pll);
  1643. pll->mode_set(dev_priv, pll);
  1644. }
  1645. }
  1646. /**
  1647. * intel_enable_shared_dpll - enable PCH PLL
  1648. * @dev_priv: i915 private structure
  1649. * @pipe: pipe PLL to enable
  1650. *
  1651. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1652. * drives the transcoder clock.
  1653. */
  1654. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1655. {
  1656. struct drm_device *dev = crtc->base.dev;
  1657. struct drm_i915_private *dev_priv = dev->dev_private;
  1658. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1659. if (WARN_ON(pll == NULL))
  1660. return;
  1661. if (WARN_ON(pll->config.crtc_mask == 0))
  1662. return;
  1663. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1664. pll->name, pll->active, pll->on,
  1665. crtc->base.base.id);
  1666. if (pll->active++) {
  1667. WARN_ON(!pll->on);
  1668. assert_shared_dpll_enabled(dev_priv, pll);
  1669. return;
  1670. }
  1671. WARN_ON(pll->on);
  1672. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1673. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1674. pll->enable(dev_priv, pll);
  1675. pll->on = true;
  1676. }
  1677. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1678. {
  1679. struct drm_device *dev = crtc->base.dev;
  1680. struct drm_i915_private *dev_priv = dev->dev_private;
  1681. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1682. /* PCH only available on ILK+ */
  1683. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1684. if (WARN_ON(pll == NULL))
  1685. return;
  1686. if (WARN_ON(pll->config.crtc_mask == 0))
  1687. return;
  1688. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1689. pll->name, pll->active, pll->on,
  1690. crtc->base.base.id);
  1691. if (WARN_ON(pll->active == 0)) {
  1692. assert_shared_dpll_disabled(dev_priv, pll);
  1693. return;
  1694. }
  1695. assert_shared_dpll_enabled(dev_priv, pll);
  1696. WARN_ON(!pll->on);
  1697. if (--pll->active)
  1698. return;
  1699. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1700. pll->disable(dev_priv, pll);
  1701. pll->on = false;
  1702. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1703. }
  1704. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1705. enum pipe pipe)
  1706. {
  1707. struct drm_device *dev = dev_priv->dev;
  1708. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1709. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1710. uint32_t reg, val, pipeconf_val;
  1711. /* PCH only available on ILK+ */
  1712. BUG_ON(!HAS_PCH_SPLIT(dev));
  1713. /* Make sure PCH DPLL is enabled */
  1714. assert_shared_dpll_enabled(dev_priv,
  1715. intel_crtc_to_shared_dpll(intel_crtc));
  1716. /* FDI must be feeding us bits for PCH ports */
  1717. assert_fdi_tx_enabled(dev_priv, pipe);
  1718. assert_fdi_rx_enabled(dev_priv, pipe);
  1719. if (HAS_PCH_CPT(dev)) {
  1720. /* Workaround: Set the timing override bit before enabling the
  1721. * pch transcoder. */
  1722. reg = TRANS_CHICKEN2(pipe);
  1723. val = I915_READ(reg);
  1724. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1725. I915_WRITE(reg, val);
  1726. }
  1727. reg = PCH_TRANSCONF(pipe);
  1728. val = I915_READ(reg);
  1729. pipeconf_val = I915_READ(PIPECONF(pipe));
  1730. if (HAS_PCH_IBX(dev_priv->dev)) {
  1731. /*
  1732. * make the BPC in transcoder be consistent with
  1733. * that in pipeconf reg.
  1734. */
  1735. val &= ~PIPECONF_BPC_MASK;
  1736. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1737. }
  1738. val &= ~TRANS_INTERLACE_MASK;
  1739. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1740. if (HAS_PCH_IBX(dev_priv->dev) &&
  1741. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1742. val |= TRANS_LEGACY_INTERLACED_ILK;
  1743. else
  1744. val |= TRANS_INTERLACED;
  1745. else
  1746. val |= TRANS_PROGRESSIVE;
  1747. I915_WRITE(reg, val | TRANS_ENABLE);
  1748. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1749. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1750. }
  1751. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1752. enum transcoder cpu_transcoder)
  1753. {
  1754. u32 val, pipeconf_val;
  1755. /* PCH only available on ILK+ */
  1756. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1757. /* FDI must be feeding us bits for PCH ports */
  1758. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1759. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1760. /* Workaround: set timing override bit. */
  1761. val = I915_READ(_TRANSA_CHICKEN2);
  1762. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1763. I915_WRITE(_TRANSA_CHICKEN2, val);
  1764. val = TRANS_ENABLE;
  1765. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1766. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1767. PIPECONF_INTERLACED_ILK)
  1768. val |= TRANS_INTERLACED;
  1769. else
  1770. val |= TRANS_PROGRESSIVE;
  1771. I915_WRITE(LPT_TRANSCONF, val);
  1772. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1773. DRM_ERROR("Failed to enable PCH transcoder\n");
  1774. }
  1775. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1776. enum pipe pipe)
  1777. {
  1778. struct drm_device *dev = dev_priv->dev;
  1779. uint32_t reg, val;
  1780. /* FDI relies on the transcoder */
  1781. assert_fdi_tx_disabled(dev_priv, pipe);
  1782. assert_fdi_rx_disabled(dev_priv, pipe);
  1783. /* Ports must be off as well */
  1784. assert_pch_ports_disabled(dev_priv, pipe);
  1785. reg = PCH_TRANSCONF(pipe);
  1786. val = I915_READ(reg);
  1787. val &= ~TRANS_ENABLE;
  1788. I915_WRITE(reg, val);
  1789. /* wait for PCH transcoder off, transcoder state */
  1790. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1791. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1792. if (!HAS_PCH_IBX(dev)) {
  1793. /* Workaround: Clear the timing override chicken bit again. */
  1794. reg = TRANS_CHICKEN2(pipe);
  1795. val = I915_READ(reg);
  1796. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1797. I915_WRITE(reg, val);
  1798. }
  1799. }
  1800. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1801. {
  1802. u32 val;
  1803. val = I915_READ(LPT_TRANSCONF);
  1804. val &= ~TRANS_ENABLE;
  1805. I915_WRITE(LPT_TRANSCONF, val);
  1806. /* wait for PCH transcoder off, transcoder state */
  1807. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1808. DRM_ERROR("Failed to disable PCH transcoder\n");
  1809. /* Workaround: clear timing override bit. */
  1810. val = I915_READ(_TRANSA_CHICKEN2);
  1811. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1812. I915_WRITE(_TRANSA_CHICKEN2, val);
  1813. }
  1814. /**
  1815. * intel_enable_pipe - enable a pipe, asserting requirements
  1816. * @crtc: crtc responsible for the pipe
  1817. *
  1818. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1819. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1820. */
  1821. static void intel_enable_pipe(struct intel_crtc *crtc)
  1822. {
  1823. struct drm_device *dev = crtc->base.dev;
  1824. struct drm_i915_private *dev_priv = dev->dev_private;
  1825. enum pipe pipe = crtc->pipe;
  1826. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1827. pipe);
  1828. enum pipe pch_transcoder;
  1829. int reg;
  1830. u32 val;
  1831. assert_planes_disabled(dev_priv, pipe);
  1832. assert_cursor_disabled(dev_priv, pipe);
  1833. assert_sprites_disabled(dev_priv, pipe);
  1834. if (HAS_PCH_LPT(dev_priv->dev))
  1835. pch_transcoder = TRANSCODER_A;
  1836. else
  1837. pch_transcoder = pipe;
  1838. /*
  1839. * A pipe without a PLL won't actually be able to drive bits from
  1840. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1841. * need the check.
  1842. */
  1843. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1844. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1845. assert_dsi_pll_enabled(dev_priv);
  1846. else
  1847. assert_pll_enabled(dev_priv, pipe);
  1848. else {
  1849. if (crtc->config->has_pch_encoder) {
  1850. /* if driving the PCH, we need FDI enabled */
  1851. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1852. assert_fdi_tx_pll_enabled(dev_priv,
  1853. (enum pipe) cpu_transcoder);
  1854. }
  1855. /* FIXME: assert CPU port conditions for SNB+ */
  1856. }
  1857. reg = PIPECONF(cpu_transcoder);
  1858. val = I915_READ(reg);
  1859. if (val & PIPECONF_ENABLE) {
  1860. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1861. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1862. return;
  1863. }
  1864. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1865. POSTING_READ(reg);
  1866. }
  1867. /**
  1868. * intel_disable_pipe - disable a pipe, asserting requirements
  1869. * @crtc: crtc whose pipes is to be disabled
  1870. *
  1871. * Disable the pipe of @crtc, making sure that various hardware
  1872. * specific requirements are met, if applicable, e.g. plane
  1873. * disabled, panel fitter off, etc.
  1874. *
  1875. * Will wait until the pipe has shut down before returning.
  1876. */
  1877. static void intel_disable_pipe(struct intel_crtc *crtc)
  1878. {
  1879. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1880. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1881. enum pipe pipe = crtc->pipe;
  1882. int reg;
  1883. u32 val;
  1884. /*
  1885. * Make sure planes won't keep trying to pump pixels to us,
  1886. * or we might hang the display.
  1887. */
  1888. assert_planes_disabled(dev_priv, pipe);
  1889. assert_cursor_disabled(dev_priv, pipe);
  1890. assert_sprites_disabled(dev_priv, pipe);
  1891. reg = PIPECONF(cpu_transcoder);
  1892. val = I915_READ(reg);
  1893. if ((val & PIPECONF_ENABLE) == 0)
  1894. return;
  1895. /*
  1896. * Double wide has implications for planes
  1897. * so best keep it disabled when not needed.
  1898. */
  1899. if (crtc->config->double_wide)
  1900. val &= ~PIPECONF_DOUBLE_WIDE;
  1901. /* Don't disable pipe or pipe PLLs if needed */
  1902. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1903. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1904. val &= ~PIPECONF_ENABLE;
  1905. I915_WRITE(reg, val);
  1906. if ((val & PIPECONF_ENABLE) == 0)
  1907. intel_wait_for_pipe_off(crtc);
  1908. }
  1909. /*
  1910. * Plane regs are double buffered, going from enabled->disabled needs a
  1911. * trigger in order to latch. The display address reg provides this.
  1912. */
  1913. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1914. enum plane plane)
  1915. {
  1916. struct drm_device *dev = dev_priv->dev;
  1917. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1918. I915_WRITE(reg, I915_READ(reg));
  1919. POSTING_READ(reg);
  1920. }
  1921. /**
  1922. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1923. * @plane: plane to be enabled
  1924. * @crtc: crtc for the plane
  1925. *
  1926. * Enable @plane on @crtc, making sure that the pipe is running first.
  1927. */
  1928. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1929. struct drm_crtc *crtc)
  1930. {
  1931. struct drm_device *dev = plane->dev;
  1932. struct drm_i915_private *dev_priv = dev->dev_private;
  1933. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1934. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1935. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1936. to_intel_plane_state(plane->state)->visible = true;
  1937. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1938. crtc->x, crtc->y);
  1939. }
  1940. static bool need_vtd_wa(struct drm_device *dev)
  1941. {
  1942. #ifdef CONFIG_INTEL_IOMMU
  1943. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1944. return true;
  1945. #endif
  1946. return false;
  1947. }
  1948. unsigned int
  1949. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1950. uint64_t fb_format_modifier)
  1951. {
  1952. unsigned int tile_height;
  1953. uint32_t pixel_bytes;
  1954. switch (fb_format_modifier) {
  1955. case DRM_FORMAT_MOD_NONE:
  1956. tile_height = 1;
  1957. break;
  1958. case I915_FORMAT_MOD_X_TILED:
  1959. tile_height = IS_GEN2(dev) ? 16 : 8;
  1960. break;
  1961. case I915_FORMAT_MOD_Y_TILED:
  1962. tile_height = 32;
  1963. break;
  1964. case I915_FORMAT_MOD_Yf_TILED:
  1965. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1966. switch (pixel_bytes) {
  1967. default:
  1968. case 1:
  1969. tile_height = 64;
  1970. break;
  1971. case 2:
  1972. case 4:
  1973. tile_height = 32;
  1974. break;
  1975. case 8:
  1976. tile_height = 16;
  1977. break;
  1978. case 16:
  1979. WARN_ONCE(1,
  1980. "128-bit pixels are not supported for display!");
  1981. tile_height = 16;
  1982. break;
  1983. }
  1984. break;
  1985. default:
  1986. MISSING_CASE(fb_format_modifier);
  1987. tile_height = 1;
  1988. break;
  1989. }
  1990. return tile_height;
  1991. }
  1992. unsigned int
  1993. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1994. uint32_t pixel_format, uint64_t fb_format_modifier)
  1995. {
  1996. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1997. fb_format_modifier));
  1998. }
  1999. static int
  2000. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  2001. const struct drm_plane_state *plane_state)
  2002. {
  2003. struct intel_rotation_info *info = &view->rotation_info;
  2004. *view = i915_ggtt_view_normal;
  2005. if (!plane_state)
  2006. return 0;
  2007. if (!intel_rotation_90_or_270(plane_state->rotation))
  2008. return 0;
  2009. *view = i915_ggtt_view_rotated;
  2010. info->height = fb->height;
  2011. info->pixel_format = fb->pixel_format;
  2012. info->pitch = fb->pitches[0];
  2013. info->fb_modifier = fb->modifier[0];
  2014. return 0;
  2015. }
  2016. int
  2017. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2018. struct drm_framebuffer *fb,
  2019. const struct drm_plane_state *plane_state,
  2020. struct intel_engine_cs *pipelined)
  2021. {
  2022. struct drm_device *dev = fb->dev;
  2023. struct drm_i915_private *dev_priv = dev->dev_private;
  2024. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2025. struct i915_ggtt_view view;
  2026. u32 alignment;
  2027. int ret;
  2028. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2029. switch (fb->modifier[0]) {
  2030. case DRM_FORMAT_MOD_NONE:
  2031. if (INTEL_INFO(dev)->gen >= 9)
  2032. alignment = 256 * 1024;
  2033. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  2034. alignment = 128 * 1024;
  2035. else if (INTEL_INFO(dev)->gen >= 4)
  2036. alignment = 4 * 1024;
  2037. else
  2038. alignment = 64 * 1024;
  2039. break;
  2040. case I915_FORMAT_MOD_X_TILED:
  2041. if (INTEL_INFO(dev)->gen >= 9)
  2042. alignment = 256 * 1024;
  2043. else {
  2044. /* pin() will align the object as required by fence */
  2045. alignment = 0;
  2046. }
  2047. break;
  2048. case I915_FORMAT_MOD_Y_TILED:
  2049. case I915_FORMAT_MOD_Yf_TILED:
  2050. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2051. "Y tiling bo slipped through, driver bug!\n"))
  2052. return -EINVAL;
  2053. alignment = 1 * 1024 * 1024;
  2054. break;
  2055. default:
  2056. MISSING_CASE(fb->modifier[0]);
  2057. return -EINVAL;
  2058. }
  2059. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2060. if (ret)
  2061. return ret;
  2062. /* Note that the w/a also requires 64 PTE of padding following the
  2063. * bo. We currently fill all unused PTE with the shadow page and so
  2064. * we should always have valid PTE following the scanout preventing
  2065. * the VT-d warning.
  2066. */
  2067. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2068. alignment = 256 * 1024;
  2069. /*
  2070. * Global gtt pte registers are special registers which actually forward
  2071. * writes to a chunk of system memory. Which means that there is no risk
  2072. * that the register values disappear as soon as we call
  2073. * intel_runtime_pm_put(), so it is correct to wrap only the
  2074. * pin/unpin/fence and not more.
  2075. */
  2076. intel_runtime_pm_get(dev_priv);
  2077. dev_priv->mm.interruptible = false;
  2078. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2079. &view);
  2080. if (ret)
  2081. goto err_interruptible;
  2082. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2083. * fence, whereas 965+ only requires a fence if using
  2084. * framebuffer compression. For simplicity, we always install
  2085. * a fence as the cost is not that onerous.
  2086. */
  2087. ret = i915_gem_object_get_fence(obj);
  2088. if (ret)
  2089. goto err_unpin;
  2090. i915_gem_object_pin_fence(obj);
  2091. dev_priv->mm.interruptible = true;
  2092. intel_runtime_pm_put(dev_priv);
  2093. return 0;
  2094. err_unpin:
  2095. i915_gem_object_unpin_from_display_plane(obj, &view);
  2096. err_interruptible:
  2097. dev_priv->mm.interruptible = true;
  2098. intel_runtime_pm_put(dev_priv);
  2099. return ret;
  2100. }
  2101. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2102. const struct drm_plane_state *plane_state)
  2103. {
  2104. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2105. struct i915_ggtt_view view;
  2106. int ret;
  2107. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2108. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2109. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2110. i915_gem_object_unpin_fence(obj);
  2111. i915_gem_object_unpin_from_display_plane(obj, &view);
  2112. }
  2113. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2114. * is assumed to be a power-of-two. */
  2115. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2116. unsigned int tiling_mode,
  2117. unsigned int cpp,
  2118. unsigned int pitch)
  2119. {
  2120. if (tiling_mode != I915_TILING_NONE) {
  2121. unsigned int tile_rows, tiles;
  2122. tile_rows = *y / 8;
  2123. *y %= 8;
  2124. tiles = *x / (512/cpp);
  2125. *x %= 512/cpp;
  2126. return tile_rows * pitch * 8 + tiles * 4096;
  2127. } else {
  2128. unsigned int offset;
  2129. offset = *y * pitch + *x * cpp;
  2130. *y = 0;
  2131. *x = (offset & 4095) / cpp;
  2132. return offset & -4096;
  2133. }
  2134. }
  2135. static int i9xx_format_to_fourcc(int format)
  2136. {
  2137. switch (format) {
  2138. case DISPPLANE_8BPP:
  2139. return DRM_FORMAT_C8;
  2140. case DISPPLANE_BGRX555:
  2141. return DRM_FORMAT_XRGB1555;
  2142. case DISPPLANE_BGRX565:
  2143. return DRM_FORMAT_RGB565;
  2144. default:
  2145. case DISPPLANE_BGRX888:
  2146. return DRM_FORMAT_XRGB8888;
  2147. case DISPPLANE_RGBX888:
  2148. return DRM_FORMAT_XBGR8888;
  2149. case DISPPLANE_BGRX101010:
  2150. return DRM_FORMAT_XRGB2101010;
  2151. case DISPPLANE_RGBX101010:
  2152. return DRM_FORMAT_XBGR2101010;
  2153. }
  2154. }
  2155. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2156. {
  2157. switch (format) {
  2158. case PLANE_CTL_FORMAT_RGB_565:
  2159. return DRM_FORMAT_RGB565;
  2160. default:
  2161. case PLANE_CTL_FORMAT_XRGB_8888:
  2162. if (rgb_order) {
  2163. if (alpha)
  2164. return DRM_FORMAT_ABGR8888;
  2165. else
  2166. return DRM_FORMAT_XBGR8888;
  2167. } else {
  2168. if (alpha)
  2169. return DRM_FORMAT_ARGB8888;
  2170. else
  2171. return DRM_FORMAT_XRGB8888;
  2172. }
  2173. case PLANE_CTL_FORMAT_XRGB_2101010:
  2174. if (rgb_order)
  2175. return DRM_FORMAT_XBGR2101010;
  2176. else
  2177. return DRM_FORMAT_XRGB2101010;
  2178. }
  2179. }
  2180. static bool
  2181. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2182. struct intel_initial_plane_config *plane_config)
  2183. {
  2184. struct drm_device *dev = crtc->base.dev;
  2185. struct drm_i915_gem_object *obj = NULL;
  2186. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2187. struct drm_framebuffer *fb = &plane_config->fb->base;
  2188. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2189. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2190. PAGE_SIZE);
  2191. size_aligned -= base_aligned;
  2192. if (plane_config->size == 0)
  2193. return false;
  2194. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2195. base_aligned,
  2196. base_aligned,
  2197. size_aligned);
  2198. if (!obj)
  2199. return false;
  2200. obj->tiling_mode = plane_config->tiling;
  2201. if (obj->tiling_mode == I915_TILING_X)
  2202. obj->stride = fb->pitches[0];
  2203. mode_cmd.pixel_format = fb->pixel_format;
  2204. mode_cmd.width = fb->width;
  2205. mode_cmd.height = fb->height;
  2206. mode_cmd.pitches[0] = fb->pitches[0];
  2207. mode_cmd.modifier[0] = fb->modifier[0];
  2208. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2209. mutex_lock(&dev->struct_mutex);
  2210. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2211. &mode_cmd, obj)) {
  2212. DRM_DEBUG_KMS("intel fb init failed\n");
  2213. goto out_unref_obj;
  2214. }
  2215. mutex_unlock(&dev->struct_mutex);
  2216. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2217. return true;
  2218. out_unref_obj:
  2219. drm_gem_object_unreference(&obj->base);
  2220. mutex_unlock(&dev->struct_mutex);
  2221. return false;
  2222. }
  2223. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2224. static void
  2225. update_state_fb(struct drm_plane *plane)
  2226. {
  2227. if (plane->fb == plane->state->fb)
  2228. return;
  2229. if (plane->state->fb)
  2230. drm_framebuffer_unreference(plane->state->fb);
  2231. plane->state->fb = plane->fb;
  2232. if (plane->state->fb)
  2233. drm_framebuffer_reference(plane->state->fb);
  2234. }
  2235. static void
  2236. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2237. struct intel_initial_plane_config *plane_config)
  2238. {
  2239. struct drm_device *dev = intel_crtc->base.dev;
  2240. struct drm_i915_private *dev_priv = dev->dev_private;
  2241. struct drm_crtc *c;
  2242. struct intel_crtc *i;
  2243. struct drm_i915_gem_object *obj;
  2244. struct drm_plane *primary = intel_crtc->base.primary;
  2245. struct drm_framebuffer *fb;
  2246. if (!plane_config->fb)
  2247. return;
  2248. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2249. fb = &plane_config->fb->base;
  2250. goto valid_fb;
  2251. }
  2252. kfree(plane_config->fb);
  2253. /*
  2254. * Failed to alloc the obj, check to see if we should share
  2255. * an fb with another CRTC instead
  2256. */
  2257. for_each_crtc(dev, c) {
  2258. i = to_intel_crtc(c);
  2259. if (c == &intel_crtc->base)
  2260. continue;
  2261. if (!i->active)
  2262. continue;
  2263. fb = c->primary->fb;
  2264. if (!fb)
  2265. continue;
  2266. obj = intel_fb_obj(fb);
  2267. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2268. drm_framebuffer_reference(fb);
  2269. goto valid_fb;
  2270. }
  2271. }
  2272. return;
  2273. valid_fb:
  2274. obj = intel_fb_obj(fb);
  2275. if (obj->tiling_mode != I915_TILING_NONE)
  2276. dev_priv->preserve_bios_swizzle = true;
  2277. primary->fb = fb;
  2278. primary->state->crtc = &intel_crtc->base;
  2279. primary->crtc = &intel_crtc->base;
  2280. update_state_fb(primary);
  2281. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2282. }
  2283. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2284. struct drm_framebuffer *fb,
  2285. int x, int y)
  2286. {
  2287. struct drm_device *dev = crtc->dev;
  2288. struct drm_i915_private *dev_priv = dev->dev_private;
  2289. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2290. struct drm_plane *primary = crtc->primary;
  2291. bool visible = to_intel_plane_state(primary->state)->visible;
  2292. struct drm_i915_gem_object *obj;
  2293. int plane = intel_crtc->plane;
  2294. unsigned long linear_offset;
  2295. u32 dspcntr;
  2296. u32 reg = DSPCNTR(plane);
  2297. int pixel_size;
  2298. if (!visible || !fb) {
  2299. I915_WRITE(reg, 0);
  2300. if (INTEL_INFO(dev)->gen >= 4)
  2301. I915_WRITE(DSPSURF(plane), 0);
  2302. else
  2303. I915_WRITE(DSPADDR(plane), 0);
  2304. POSTING_READ(reg);
  2305. return;
  2306. }
  2307. obj = intel_fb_obj(fb);
  2308. if (WARN_ON(obj == NULL))
  2309. return;
  2310. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2311. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2312. dspcntr |= DISPLAY_PLANE_ENABLE;
  2313. if (INTEL_INFO(dev)->gen < 4) {
  2314. if (intel_crtc->pipe == PIPE_B)
  2315. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2316. /* pipesrc and dspsize control the size that is scaled from,
  2317. * which should always be the user's requested size.
  2318. */
  2319. I915_WRITE(DSPSIZE(plane),
  2320. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2321. (intel_crtc->config->pipe_src_w - 1));
  2322. I915_WRITE(DSPPOS(plane), 0);
  2323. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2324. I915_WRITE(PRIMSIZE(plane),
  2325. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2326. (intel_crtc->config->pipe_src_w - 1));
  2327. I915_WRITE(PRIMPOS(plane), 0);
  2328. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2329. }
  2330. switch (fb->pixel_format) {
  2331. case DRM_FORMAT_C8:
  2332. dspcntr |= DISPPLANE_8BPP;
  2333. break;
  2334. case DRM_FORMAT_XRGB1555:
  2335. case DRM_FORMAT_ARGB1555:
  2336. dspcntr |= DISPPLANE_BGRX555;
  2337. break;
  2338. case DRM_FORMAT_RGB565:
  2339. dspcntr |= DISPPLANE_BGRX565;
  2340. break;
  2341. case DRM_FORMAT_XRGB8888:
  2342. case DRM_FORMAT_ARGB8888:
  2343. dspcntr |= DISPPLANE_BGRX888;
  2344. break;
  2345. case DRM_FORMAT_XBGR8888:
  2346. case DRM_FORMAT_ABGR8888:
  2347. dspcntr |= DISPPLANE_RGBX888;
  2348. break;
  2349. case DRM_FORMAT_XRGB2101010:
  2350. case DRM_FORMAT_ARGB2101010:
  2351. dspcntr |= DISPPLANE_BGRX101010;
  2352. break;
  2353. case DRM_FORMAT_XBGR2101010:
  2354. case DRM_FORMAT_ABGR2101010:
  2355. dspcntr |= DISPPLANE_RGBX101010;
  2356. break;
  2357. default:
  2358. BUG();
  2359. }
  2360. if (INTEL_INFO(dev)->gen >= 4 &&
  2361. obj->tiling_mode != I915_TILING_NONE)
  2362. dspcntr |= DISPPLANE_TILED;
  2363. if (IS_G4X(dev))
  2364. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2365. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2366. if (INTEL_INFO(dev)->gen >= 4) {
  2367. intel_crtc->dspaddr_offset =
  2368. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2369. pixel_size,
  2370. fb->pitches[0]);
  2371. linear_offset -= intel_crtc->dspaddr_offset;
  2372. } else {
  2373. intel_crtc->dspaddr_offset = linear_offset;
  2374. }
  2375. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2376. dspcntr |= DISPPLANE_ROTATE_180;
  2377. x += (intel_crtc->config->pipe_src_w - 1);
  2378. y += (intel_crtc->config->pipe_src_h - 1);
  2379. /* Finding the last pixel of the last line of the display
  2380. data and adding to linear_offset*/
  2381. linear_offset +=
  2382. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2383. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2384. }
  2385. I915_WRITE(reg, dspcntr);
  2386. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2387. if (INTEL_INFO(dev)->gen >= 4) {
  2388. I915_WRITE(DSPSURF(plane),
  2389. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2390. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2391. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2392. } else
  2393. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2394. POSTING_READ(reg);
  2395. }
  2396. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2397. struct drm_framebuffer *fb,
  2398. int x, int y)
  2399. {
  2400. struct drm_device *dev = crtc->dev;
  2401. struct drm_i915_private *dev_priv = dev->dev_private;
  2402. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2403. struct drm_plane *primary = crtc->primary;
  2404. bool visible = to_intel_plane_state(primary->state)->visible;
  2405. struct drm_i915_gem_object *obj;
  2406. int plane = intel_crtc->plane;
  2407. unsigned long linear_offset;
  2408. u32 dspcntr;
  2409. u32 reg = DSPCNTR(plane);
  2410. int pixel_size;
  2411. if (!visible || !fb) {
  2412. I915_WRITE(reg, 0);
  2413. I915_WRITE(DSPSURF(plane), 0);
  2414. POSTING_READ(reg);
  2415. return;
  2416. }
  2417. obj = intel_fb_obj(fb);
  2418. if (WARN_ON(obj == NULL))
  2419. return;
  2420. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2421. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2422. dspcntr |= DISPLAY_PLANE_ENABLE;
  2423. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2424. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2425. switch (fb->pixel_format) {
  2426. case DRM_FORMAT_C8:
  2427. dspcntr |= DISPPLANE_8BPP;
  2428. break;
  2429. case DRM_FORMAT_RGB565:
  2430. dspcntr |= DISPPLANE_BGRX565;
  2431. break;
  2432. case DRM_FORMAT_XRGB8888:
  2433. case DRM_FORMAT_ARGB8888:
  2434. dspcntr |= DISPPLANE_BGRX888;
  2435. break;
  2436. case DRM_FORMAT_XBGR8888:
  2437. case DRM_FORMAT_ABGR8888:
  2438. dspcntr |= DISPPLANE_RGBX888;
  2439. break;
  2440. case DRM_FORMAT_XRGB2101010:
  2441. case DRM_FORMAT_ARGB2101010:
  2442. dspcntr |= DISPPLANE_BGRX101010;
  2443. break;
  2444. case DRM_FORMAT_XBGR2101010:
  2445. case DRM_FORMAT_ABGR2101010:
  2446. dspcntr |= DISPPLANE_RGBX101010;
  2447. break;
  2448. default:
  2449. BUG();
  2450. }
  2451. if (obj->tiling_mode != I915_TILING_NONE)
  2452. dspcntr |= DISPPLANE_TILED;
  2453. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2454. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2455. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2456. intel_crtc->dspaddr_offset =
  2457. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2458. pixel_size,
  2459. fb->pitches[0]);
  2460. linear_offset -= intel_crtc->dspaddr_offset;
  2461. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2462. dspcntr |= DISPPLANE_ROTATE_180;
  2463. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2464. x += (intel_crtc->config->pipe_src_w - 1);
  2465. y += (intel_crtc->config->pipe_src_h - 1);
  2466. /* Finding the last pixel of the last line of the display
  2467. data and adding to linear_offset*/
  2468. linear_offset +=
  2469. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2470. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2471. }
  2472. }
  2473. I915_WRITE(reg, dspcntr);
  2474. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2475. I915_WRITE(DSPSURF(plane),
  2476. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2477. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2478. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2479. } else {
  2480. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2481. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2482. }
  2483. POSTING_READ(reg);
  2484. }
  2485. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2486. uint32_t pixel_format)
  2487. {
  2488. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2489. /*
  2490. * The stride is either expressed as a multiple of 64 bytes
  2491. * chunks for linear buffers or in number of tiles for tiled
  2492. * buffers.
  2493. */
  2494. switch (fb_modifier) {
  2495. case DRM_FORMAT_MOD_NONE:
  2496. return 64;
  2497. case I915_FORMAT_MOD_X_TILED:
  2498. if (INTEL_INFO(dev)->gen == 2)
  2499. return 128;
  2500. return 512;
  2501. case I915_FORMAT_MOD_Y_TILED:
  2502. /* No need to check for old gens and Y tiling since this is
  2503. * about the display engine and those will be blocked before
  2504. * we get here.
  2505. */
  2506. return 128;
  2507. case I915_FORMAT_MOD_Yf_TILED:
  2508. if (bits_per_pixel == 8)
  2509. return 64;
  2510. else
  2511. return 128;
  2512. default:
  2513. MISSING_CASE(fb_modifier);
  2514. return 64;
  2515. }
  2516. }
  2517. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2518. struct drm_i915_gem_object *obj)
  2519. {
  2520. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2521. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2522. view = &i915_ggtt_view_rotated;
  2523. return i915_gem_obj_ggtt_offset_view(obj, view);
  2524. }
  2525. /*
  2526. * This function detaches (aka. unbinds) unused scalers in hardware
  2527. */
  2528. void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2529. {
  2530. struct drm_device *dev;
  2531. struct drm_i915_private *dev_priv;
  2532. struct intel_crtc_scaler_state *scaler_state;
  2533. int i;
  2534. if (!intel_crtc || !intel_crtc->config)
  2535. return;
  2536. dev = intel_crtc->base.dev;
  2537. dev_priv = dev->dev_private;
  2538. scaler_state = &intel_crtc->config->scaler_state;
  2539. /* loop through and disable scalers that aren't in use */
  2540. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2541. if (!scaler_state->scalers[i].in_use) {
  2542. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2543. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2544. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2545. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2546. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2547. }
  2548. }
  2549. }
  2550. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2551. {
  2552. u32 plane_ctl_format = 0;
  2553. switch (pixel_format) {
  2554. case DRM_FORMAT_RGB565:
  2555. plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
  2556. break;
  2557. case DRM_FORMAT_XBGR8888:
  2558. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2559. break;
  2560. case DRM_FORMAT_XRGB8888:
  2561. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
  2562. break;
  2563. /*
  2564. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2565. * to be already pre-multiplied. We need to add a knob (or a different
  2566. * DRM_FORMAT) for user-space to configure that.
  2567. */
  2568. case DRM_FORMAT_ABGR8888:
  2569. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2570. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2571. break;
  2572. case DRM_FORMAT_ARGB8888:
  2573. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
  2574. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2575. break;
  2576. case DRM_FORMAT_XRGB2101010:
  2577. plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
  2578. break;
  2579. case DRM_FORMAT_XBGR2101010:
  2580. plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2581. break;
  2582. case DRM_FORMAT_YUYV:
  2583. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2584. break;
  2585. case DRM_FORMAT_YVYU:
  2586. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2587. break;
  2588. case DRM_FORMAT_UYVY:
  2589. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2590. break;
  2591. case DRM_FORMAT_VYUY:
  2592. plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2593. break;
  2594. default:
  2595. BUG();
  2596. }
  2597. return plane_ctl_format;
  2598. }
  2599. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2600. {
  2601. u32 plane_ctl_tiling = 0;
  2602. switch (fb_modifier) {
  2603. case DRM_FORMAT_MOD_NONE:
  2604. break;
  2605. case I915_FORMAT_MOD_X_TILED:
  2606. plane_ctl_tiling = PLANE_CTL_TILED_X;
  2607. break;
  2608. case I915_FORMAT_MOD_Y_TILED:
  2609. plane_ctl_tiling = PLANE_CTL_TILED_Y;
  2610. break;
  2611. case I915_FORMAT_MOD_Yf_TILED:
  2612. plane_ctl_tiling = PLANE_CTL_TILED_YF;
  2613. break;
  2614. default:
  2615. MISSING_CASE(fb_modifier);
  2616. }
  2617. return plane_ctl_tiling;
  2618. }
  2619. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2620. {
  2621. u32 plane_ctl_rotation = 0;
  2622. switch (rotation) {
  2623. case BIT(DRM_ROTATE_0):
  2624. break;
  2625. case BIT(DRM_ROTATE_90):
  2626. plane_ctl_rotation = PLANE_CTL_ROTATE_90;
  2627. break;
  2628. case BIT(DRM_ROTATE_180):
  2629. plane_ctl_rotation = PLANE_CTL_ROTATE_180;
  2630. break;
  2631. case BIT(DRM_ROTATE_270):
  2632. plane_ctl_rotation = PLANE_CTL_ROTATE_270;
  2633. break;
  2634. default:
  2635. MISSING_CASE(rotation);
  2636. }
  2637. return plane_ctl_rotation;
  2638. }
  2639. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2640. struct drm_framebuffer *fb,
  2641. int x, int y)
  2642. {
  2643. struct drm_device *dev = crtc->dev;
  2644. struct drm_i915_private *dev_priv = dev->dev_private;
  2645. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2646. struct drm_plane *plane = crtc->primary;
  2647. bool visible = to_intel_plane_state(plane->state)->visible;
  2648. struct drm_i915_gem_object *obj;
  2649. int pipe = intel_crtc->pipe;
  2650. u32 plane_ctl, stride_div, stride;
  2651. u32 tile_height, plane_offset, plane_size;
  2652. unsigned int rotation;
  2653. int x_offset, y_offset;
  2654. unsigned long surf_addr;
  2655. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2656. struct intel_plane_state *plane_state;
  2657. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2658. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2659. int scaler_id = -1;
  2660. plane_state = to_intel_plane_state(plane->state);
  2661. if (!visible || !fb) {
  2662. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2663. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2664. POSTING_READ(PLANE_CTL(pipe, 0));
  2665. return;
  2666. }
  2667. plane_ctl = PLANE_CTL_ENABLE |
  2668. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2669. PLANE_CTL_PIPE_CSC_ENABLE;
  2670. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2671. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2672. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2673. rotation = plane->state->rotation;
  2674. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2675. obj = intel_fb_obj(fb);
  2676. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2677. fb->pixel_format);
  2678. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2679. /*
  2680. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2681. * update_plane helpers are called from legacy paths.
  2682. * Once full atomic crtc is available, below check can be avoided.
  2683. */
  2684. if (drm_rect_width(&plane_state->src)) {
  2685. scaler_id = plane_state->scaler_id;
  2686. src_x = plane_state->src.x1 >> 16;
  2687. src_y = plane_state->src.y1 >> 16;
  2688. src_w = drm_rect_width(&plane_state->src) >> 16;
  2689. src_h = drm_rect_height(&plane_state->src) >> 16;
  2690. dst_x = plane_state->dst.x1;
  2691. dst_y = plane_state->dst.y1;
  2692. dst_w = drm_rect_width(&plane_state->dst);
  2693. dst_h = drm_rect_height(&plane_state->dst);
  2694. WARN_ON(x != src_x || y != src_y);
  2695. } else {
  2696. src_w = intel_crtc->config->pipe_src_w;
  2697. src_h = intel_crtc->config->pipe_src_h;
  2698. }
  2699. if (intel_rotation_90_or_270(rotation)) {
  2700. /* stride = Surface height in tiles */
  2701. tile_height = intel_tile_height(dev, fb->bits_per_pixel,
  2702. fb->modifier[0]);
  2703. stride = DIV_ROUND_UP(fb->height, tile_height);
  2704. x_offset = stride * tile_height - y - src_h;
  2705. y_offset = x;
  2706. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2707. } else {
  2708. stride = fb->pitches[0] / stride_div;
  2709. x_offset = x;
  2710. y_offset = y;
  2711. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2712. }
  2713. plane_offset = y_offset << 16 | x_offset;
  2714. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2715. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2716. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2717. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2718. if (scaler_id >= 0) {
  2719. uint32_t ps_ctrl = 0;
  2720. WARN_ON(!dst_w || !dst_h);
  2721. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2722. crtc_state->scaler_state.scalers[scaler_id].mode;
  2723. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2724. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2725. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2726. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2727. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2728. } else {
  2729. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2730. }
  2731. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2732. POSTING_READ(PLANE_SURF(pipe, 0));
  2733. }
  2734. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2735. static int
  2736. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2737. int x, int y, enum mode_set_atomic state)
  2738. {
  2739. struct drm_device *dev = crtc->dev;
  2740. struct drm_i915_private *dev_priv = dev->dev_private;
  2741. if (dev_priv->display.disable_fbc)
  2742. dev_priv->display.disable_fbc(dev);
  2743. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2744. return 0;
  2745. }
  2746. static void intel_complete_page_flips(struct drm_device *dev)
  2747. {
  2748. struct drm_crtc *crtc;
  2749. for_each_crtc(dev, crtc) {
  2750. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2751. enum plane plane = intel_crtc->plane;
  2752. intel_prepare_page_flip(dev, plane);
  2753. intel_finish_page_flip_plane(dev, plane);
  2754. }
  2755. }
  2756. static void intel_update_primary_planes(struct drm_device *dev)
  2757. {
  2758. struct drm_i915_private *dev_priv = dev->dev_private;
  2759. struct drm_crtc *crtc;
  2760. for_each_crtc(dev, crtc) {
  2761. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2762. drm_modeset_lock(&crtc->mutex, NULL);
  2763. /*
  2764. * FIXME: Once we have proper support for primary planes (and
  2765. * disabling them without disabling the entire crtc) allow again
  2766. * a NULL crtc->primary->fb.
  2767. */
  2768. if (intel_crtc->active && crtc->primary->fb)
  2769. dev_priv->display.update_primary_plane(crtc,
  2770. crtc->primary->fb,
  2771. crtc->x,
  2772. crtc->y);
  2773. drm_modeset_unlock(&crtc->mutex);
  2774. }
  2775. }
  2776. void intel_crtc_reset(struct intel_crtc *crtc)
  2777. {
  2778. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  2779. if (!crtc->active)
  2780. return;
  2781. intel_crtc_disable_planes(&crtc->base);
  2782. dev_priv->display.crtc_disable(&crtc->base);
  2783. dev_priv->display.crtc_enable(&crtc->base);
  2784. intel_crtc_enable_planes(&crtc->base);
  2785. }
  2786. void intel_prepare_reset(struct drm_device *dev)
  2787. {
  2788. struct drm_i915_private *dev_priv = to_i915(dev);
  2789. struct intel_crtc *crtc;
  2790. /* no reset support for gen2 */
  2791. if (IS_GEN2(dev))
  2792. return;
  2793. /* reset doesn't touch the display */
  2794. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2795. return;
  2796. drm_modeset_lock_all(dev);
  2797. /*
  2798. * Disabling the crtcs gracefully seems nicer. Also the
  2799. * g33 docs say we should at least disable all the planes.
  2800. */
  2801. for_each_intel_crtc(dev, crtc) {
  2802. if (!crtc->active)
  2803. continue;
  2804. intel_crtc_disable_planes(&crtc->base);
  2805. dev_priv->display.crtc_disable(&crtc->base);
  2806. }
  2807. }
  2808. void intel_finish_reset(struct drm_device *dev)
  2809. {
  2810. struct drm_i915_private *dev_priv = to_i915(dev);
  2811. /*
  2812. * Flips in the rings will be nuked by the reset,
  2813. * so complete all pending flips so that user space
  2814. * will get its events and not get stuck.
  2815. */
  2816. intel_complete_page_flips(dev);
  2817. /* no reset support for gen2 */
  2818. if (IS_GEN2(dev))
  2819. return;
  2820. /* reset doesn't touch the display */
  2821. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2822. /*
  2823. * Flips in the rings have been nuked by the reset,
  2824. * so update the base address of all primary
  2825. * planes to the the last fb to make sure we're
  2826. * showing the correct fb after a reset.
  2827. */
  2828. intel_update_primary_planes(dev);
  2829. return;
  2830. }
  2831. /*
  2832. * The display has been reset as well,
  2833. * so need a full re-initialization.
  2834. */
  2835. intel_runtime_pm_disable_interrupts(dev_priv);
  2836. intel_runtime_pm_enable_interrupts(dev_priv);
  2837. intel_modeset_init_hw(dev);
  2838. spin_lock_irq(&dev_priv->irq_lock);
  2839. if (dev_priv->display.hpd_irq_setup)
  2840. dev_priv->display.hpd_irq_setup(dev);
  2841. spin_unlock_irq(&dev_priv->irq_lock);
  2842. intel_modeset_setup_hw_state(dev, true);
  2843. intel_hpd_init(dev_priv);
  2844. drm_modeset_unlock_all(dev);
  2845. }
  2846. static int
  2847. intel_finish_fb(struct drm_framebuffer *old_fb)
  2848. {
  2849. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2850. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2851. bool was_interruptible = dev_priv->mm.interruptible;
  2852. int ret;
  2853. /* Big Hammer, we also need to ensure that any pending
  2854. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2855. * current scanout is retired before unpinning the old
  2856. * framebuffer.
  2857. *
  2858. * This should only fail upon a hung GPU, in which case we
  2859. * can safely continue.
  2860. */
  2861. dev_priv->mm.interruptible = false;
  2862. ret = i915_gem_object_finish_gpu(obj);
  2863. dev_priv->mm.interruptible = was_interruptible;
  2864. return ret;
  2865. }
  2866. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2867. {
  2868. struct drm_device *dev = crtc->dev;
  2869. struct drm_i915_private *dev_priv = dev->dev_private;
  2870. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2871. bool pending;
  2872. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2873. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2874. return false;
  2875. spin_lock_irq(&dev->event_lock);
  2876. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2877. spin_unlock_irq(&dev->event_lock);
  2878. return pending;
  2879. }
  2880. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2881. {
  2882. struct drm_device *dev = crtc->base.dev;
  2883. struct drm_i915_private *dev_priv = dev->dev_private;
  2884. const struct drm_display_mode *adjusted_mode;
  2885. if (!i915.fastboot)
  2886. return;
  2887. /*
  2888. * Update pipe size and adjust fitter if needed: the reason for this is
  2889. * that in compute_mode_changes we check the native mode (not the pfit
  2890. * mode) to see if we can flip rather than do a full mode set. In the
  2891. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2892. * pfit state, we'll end up with a big fb scanned out into the wrong
  2893. * sized surface.
  2894. *
  2895. * To fix this properly, we need to hoist the checks up into
  2896. * compute_mode_changes (or above), check the actual pfit state and
  2897. * whether the platform allows pfit disable with pipe active, and only
  2898. * then update the pipesrc and pfit state, even on the flip path.
  2899. */
  2900. adjusted_mode = &crtc->config->base.adjusted_mode;
  2901. I915_WRITE(PIPESRC(crtc->pipe),
  2902. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2903. (adjusted_mode->crtc_vdisplay - 1));
  2904. if (!crtc->config->pch_pfit.enabled &&
  2905. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2906. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2907. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2908. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2909. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2910. }
  2911. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2912. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2913. }
  2914. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2915. {
  2916. struct drm_device *dev = crtc->dev;
  2917. struct drm_i915_private *dev_priv = dev->dev_private;
  2918. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2919. int pipe = intel_crtc->pipe;
  2920. u32 reg, temp;
  2921. /* enable normal train */
  2922. reg = FDI_TX_CTL(pipe);
  2923. temp = I915_READ(reg);
  2924. if (IS_IVYBRIDGE(dev)) {
  2925. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2926. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2927. } else {
  2928. temp &= ~FDI_LINK_TRAIN_NONE;
  2929. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2930. }
  2931. I915_WRITE(reg, temp);
  2932. reg = FDI_RX_CTL(pipe);
  2933. temp = I915_READ(reg);
  2934. if (HAS_PCH_CPT(dev)) {
  2935. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2936. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2937. } else {
  2938. temp &= ~FDI_LINK_TRAIN_NONE;
  2939. temp |= FDI_LINK_TRAIN_NONE;
  2940. }
  2941. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2942. /* wait one idle pattern time */
  2943. POSTING_READ(reg);
  2944. udelay(1000);
  2945. /* IVB wants error correction enabled */
  2946. if (IS_IVYBRIDGE(dev))
  2947. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2948. FDI_FE_ERRC_ENABLE);
  2949. }
  2950. /* The FDI link training functions for ILK/Ibexpeak. */
  2951. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2952. {
  2953. struct drm_device *dev = crtc->dev;
  2954. struct drm_i915_private *dev_priv = dev->dev_private;
  2955. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2956. int pipe = intel_crtc->pipe;
  2957. u32 reg, temp, tries;
  2958. /* FDI needs bits from pipe first */
  2959. assert_pipe_enabled(dev_priv, pipe);
  2960. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2961. for train result */
  2962. reg = FDI_RX_IMR(pipe);
  2963. temp = I915_READ(reg);
  2964. temp &= ~FDI_RX_SYMBOL_LOCK;
  2965. temp &= ~FDI_RX_BIT_LOCK;
  2966. I915_WRITE(reg, temp);
  2967. I915_READ(reg);
  2968. udelay(150);
  2969. /* enable CPU FDI TX and PCH FDI RX */
  2970. reg = FDI_TX_CTL(pipe);
  2971. temp = I915_READ(reg);
  2972. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2973. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2974. temp &= ~FDI_LINK_TRAIN_NONE;
  2975. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2976. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2977. reg = FDI_RX_CTL(pipe);
  2978. temp = I915_READ(reg);
  2979. temp &= ~FDI_LINK_TRAIN_NONE;
  2980. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2981. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2982. POSTING_READ(reg);
  2983. udelay(150);
  2984. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2985. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2986. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2987. FDI_RX_PHASE_SYNC_POINTER_EN);
  2988. reg = FDI_RX_IIR(pipe);
  2989. for (tries = 0; tries < 5; tries++) {
  2990. temp = I915_READ(reg);
  2991. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2992. if ((temp & FDI_RX_BIT_LOCK)) {
  2993. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2994. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2995. break;
  2996. }
  2997. }
  2998. if (tries == 5)
  2999. DRM_ERROR("FDI train 1 fail!\n");
  3000. /* Train 2 */
  3001. reg = FDI_TX_CTL(pipe);
  3002. temp = I915_READ(reg);
  3003. temp &= ~FDI_LINK_TRAIN_NONE;
  3004. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3005. I915_WRITE(reg, temp);
  3006. reg = FDI_RX_CTL(pipe);
  3007. temp = I915_READ(reg);
  3008. temp &= ~FDI_LINK_TRAIN_NONE;
  3009. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3010. I915_WRITE(reg, temp);
  3011. POSTING_READ(reg);
  3012. udelay(150);
  3013. reg = FDI_RX_IIR(pipe);
  3014. for (tries = 0; tries < 5; tries++) {
  3015. temp = I915_READ(reg);
  3016. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3017. if (temp & FDI_RX_SYMBOL_LOCK) {
  3018. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3019. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3020. break;
  3021. }
  3022. }
  3023. if (tries == 5)
  3024. DRM_ERROR("FDI train 2 fail!\n");
  3025. DRM_DEBUG_KMS("FDI train done\n");
  3026. }
  3027. static const int snb_b_fdi_train_param[] = {
  3028. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3029. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3030. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3031. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3032. };
  3033. /* The FDI link training functions for SNB/Cougarpoint. */
  3034. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3035. {
  3036. struct drm_device *dev = crtc->dev;
  3037. struct drm_i915_private *dev_priv = dev->dev_private;
  3038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3039. int pipe = intel_crtc->pipe;
  3040. u32 reg, temp, i, retry;
  3041. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3042. for train result */
  3043. reg = FDI_RX_IMR(pipe);
  3044. temp = I915_READ(reg);
  3045. temp &= ~FDI_RX_SYMBOL_LOCK;
  3046. temp &= ~FDI_RX_BIT_LOCK;
  3047. I915_WRITE(reg, temp);
  3048. POSTING_READ(reg);
  3049. udelay(150);
  3050. /* enable CPU FDI TX and PCH FDI RX */
  3051. reg = FDI_TX_CTL(pipe);
  3052. temp = I915_READ(reg);
  3053. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3054. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3055. temp &= ~FDI_LINK_TRAIN_NONE;
  3056. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3057. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3058. /* SNB-B */
  3059. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3060. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3061. I915_WRITE(FDI_RX_MISC(pipe),
  3062. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3063. reg = FDI_RX_CTL(pipe);
  3064. temp = I915_READ(reg);
  3065. if (HAS_PCH_CPT(dev)) {
  3066. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3067. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3068. } else {
  3069. temp &= ~FDI_LINK_TRAIN_NONE;
  3070. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3071. }
  3072. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3073. POSTING_READ(reg);
  3074. udelay(150);
  3075. for (i = 0; i < 4; i++) {
  3076. reg = FDI_TX_CTL(pipe);
  3077. temp = I915_READ(reg);
  3078. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3079. temp |= snb_b_fdi_train_param[i];
  3080. I915_WRITE(reg, temp);
  3081. POSTING_READ(reg);
  3082. udelay(500);
  3083. for (retry = 0; retry < 5; retry++) {
  3084. reg = FDI_RX_IIR(pipe);
  3085. temp = I915_READ(reg);
  3086. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3087. if (temp & FDI_RX_BIT_LOCK) {
  3088. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3089. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3090. break;
  3091. }
  3092. udelay(50);
  3093. }
  3094. if (retry < 5)
  3095. break;
  3096. }
  3097. if (i == 4)
  3098. DRM_ERROR("FDI train 1 fail!\n");
  3099. /* Train 2 */
  3100. reg = FDI_TX_CTL(pipe);
  3101. temp = I915_READ(reg);
  3102. temp &= ~FDI_LINK_TRAIN_NONE;
  3103. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3104. if (IS_GEN6(dev)) {
  3105. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3106. /* SNB-B */
  3107. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3108. }
  3109. I915_WRITE(reg, temp);
  3110. reg = FDI_RX_CTL(pipe);
  3111. temp = I915_READ(reg);
  3112. if (HAS_PCH_CPT(dev)) {
  3113. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3114. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3115. } else {
  3116. temp &= ~FDI_LINK_TRAIN_NONE;
  3117. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3118. }
  3119. I915_WRITE(reg, temp);
  3120. POSTING_READ(reg);
  3121. udelay(150);
  3122. for (i = 0; i < 4; i++) {
  3123. reg = FDI_TX_CTL(pipe);
  3124. temp = I915_READ(reg);
  3125. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3126. temp |= snb_b_fdi_train_param[i];
  3127. I915_WRITE(reg, temp);
  3128. POSTING_READ(reg);
  3129. udelay(500);
  3130. for (retry = 0; retry < 5; retry++) {
  3131. reg = FDI_RX_IIR(pipe);
  3132. temp = I915_READ(reg);
  3133. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3134. if (temp & FDI_RX_SYMBOL_LOCK) {
  3135. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3136. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3137. break;
  3138. }
  3139. udelay(50);
  3140. }
  3141. if (retry < 5)
  3142. break;
  3143. }
  3144. if (i == 4)
  3145. DRM_ERROR("FDI train 2 fail!\n");
  3146. DRM_DEBUG_KMS("FDI train done.\n");
  3147. }
  3148. /* Manual link training for Ivy Bridge A0 parts */
  3149. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3150. {
  3151. struct drm_device *dev = crtc->dev;
  3152. struct drm_i915_private *dev_priv = dev->dev_private;
  3153. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3154. int pipe = intel_crtc->pipe;
  3155. u32 reg, temp, i, j;
  3156. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3157. for train result */
  3158. reg = FDI_RX_IMR(pipe);
  3159. temp = I915_READ(reg);
  3160. temp &= ~FDI_RX_SYMBOL_LOCK;
  3161. temp &= ~FDI_RX_BIT_LOCK;
  3162. I915_WRITE(reg, temp);
  3163. POSTING_READ(reg);
  3164. udelay(150);
  3165. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3166. I915_READ(FDI_RX_IIR(pipe)));
  3167. /* Try each vswing and preemphasis setting twice before moving on */
  3168. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3169. /* disable first in case we need to retry */
  3170. reg = FDI_TX_CTL(pipe);
  3171. temp = I915_READ(reg);
  3172. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3173. temp &= ~FDI_TX_ENABLE;
  3174. I915_WRITE(reg, temp);
  3175. reg = FDI_RX_CTL(pipe);
  3176. temp = I915_READ(reg);
  3177. temp &= ~FDI_LINK_TRAIN_AUTO;
  3178. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3179. temp &= ~FDI_RX_ENABLE;
  3180. I915_WRITE(reg, temp);
  3181. /* enable CPU FDI TX and PCH FDI RX */
  3182. reg = FDI_TX_CTL(pipe);
  3183. temp = I915_READ(reg);
  3184. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3185. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3186. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3187. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3188. temp |= snb_b_fdi_train_param[j/2];
  3189. temp |= FDI_COMPOSITE_SYNC;
  3190. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3191. I915_WRITE(FDI_RX_MISC(pipe),
  3192. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3193. reg = FDI_RX_CTL(pipe);
  3194. temp = I915_READ(reg);
  3195. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3196. temp |= FDI_COMPOSITE_SYNC;
  3197. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3198. POSTING_READ(reg);
  3199. udelay(1); /* should be 0.5us */
  3200. for (i = 0; i < 4; i++) {
  3201. reg = FDI_RX_IIR(pipe);
  3202. temp = I915_READ(reg);
  3203. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3204. if (temp & FDI_RX_BIT_LOCK ||
  3205. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3206. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3207. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3208. i);
  3209. break;
  3210. }
  3211. udelay(1); /* should be 0.5us */
  3212. }
  3213. if (i == 4) {
  3214. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3215. continue;
  3216. }
  3217. /* Train 2 */
  3218. reg = FDI_TX_CTL(pipe);
  3219. temp = I915_READ(reg);
  3220. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3221. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3222. I915_WRITE(reg, temp);
  3223. reg = FDI_RX_CTL(pipe);
  3224. temp = I915_READ(reg);
  3225. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3226. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3227. I915_WRITE(reg, temp);
  3228. POSTING_READ(reg);
  3229. udelay(2); /* should be 1.5us */
  3230. for (i = 0; i < 4; i++) {
  3231. reg = FDI_RX_IIR(pipe);
  3232. temp = I915_READ(reg);
  3233. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3234. if (temp & FDI_RX_SYMBOL_LOCK ||
  3235. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3236. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3237. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3238. i);
  3239. goto train_done;
  3240. }
  3241. udelay(2); /* should be 1.5us */
  3242. }
  3243. if (i == 4)
  3244. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3245. }
  3246. train_done:
  3247. DRM_DEBUG_KMS("FDI train done.\n");
  3248. }
  3249. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3250. {
  3251. struct drm_device *dev = intel_crtc->base.dev;
  3252. struct drm_i915_private *dev_priv = dev->dev_private;
  3253. int pipe = intel_crtc->pipe;
  3254. u32 reg, temp;
  3255. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3256. reg = FDI_RX_CTL(pipe);
  3257. temp = I915_READ(reg);
  3258. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3259. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3260. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3261. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3262. POSTING_READ(reg);
  3263. udelay(200);
  3264. /* Switch from Rawclk to PCDclk */
  3265. temp = I915_READ(reg);
  3266. I915_WRITE(reg, temp | FDI_PCDCLK);
  3267. POSTING_READ(reg);
  3268. udelay(200);
  3269. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3270. reg = FDI_TX_CTL(pipe);
  3271. temp = I915_READ(reg);
  3272. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3273. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3274. POSTING_READ(reg);
  3275. udelay(100);
  3276. }
  3277. }
  3278. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3279. {
  3280. struct drm_device *dev = intel_crtc->base.dev;
  3281. struct drm_i915_private *dev_priv = dev->dev_private;
  3282. int pipe = intel_crtc->pipe;
  3283. u32 reg, temp;
  3284. /* Switch from PCDclk to Rawclk */
  3285. reg = FDI_RX_CTL(pipe);
  3286. temp = I915_READ(reg);
  3287. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3288. /* Disable CPU FDI TX PLL */
  3289. reg = FDI_TX_CTL(pipe);
  3290. temp = I915_READ(reg);
  3291. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3292. POSTING_READ(reg);
  3293. udelay(100);
  3294. reg = FDI_RX_CTL(pipe);
  3295. temp = I915_READ(reg);
  3296. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3297. /* Wait for the clocks to turn off. */
  3298. POSTING_READ(reg);
  3299. udelay(100);
  3300. }
  3301. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3302. {
  3303. struct drm_device *dev = crtc->dev;
  3304. struct drm_i915_private *dev_priv = dev->dev_private;
  3305. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3306. int pipe = intel_crtc->pipe;
  3307. u32 reg, temp;
  3308. /* disable CPU FDI tx and PCH FDI rx */
  3309. reg = FDI_TX_CTL(pipe);
  3310. temp = I915_READ(reg);
  3311. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3312. POSTING_READ(reg);
  3313. reg = FDI_RX_CTL(pipe);
  3314. temp = I915_READ(reg);
  3315. temp &= ~(0x7 << 16);
  3316. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3317. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3318. POSTING_READ(reg);
  3319. udelay(100);
  3320. /* Ironlake workaround, disable clock pointer after downing FDI */
  3321. if (HAS_PCH_IBX(dev))
  3322. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3323. /* still set train pattern 1 */
  3324. reg = FDI_TX_CTL(pipe);
  3325. temp = I915_READ(reg);
  3326. temp &= ~FDI_LINK_TRAIN_NONE;
  3327. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3328. I915_WRITE(reg, temp);
  3329. reg = FDI_RX_CTL(pipe);
  3330. temp = I915_READ(reg);
  3331. if (HAS_PCH_CPT(dev)) {
  3332. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3333. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3334. } else {
  3335. temp &= ~FDI_LINK_TRAIN_NONE;
  3336. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3337. }
  3338. /* BPC in FDI rx is consistent with that in PIPECONF */
  3339. temp &= ~(0x07 << 16);
  3340. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3341. I915_WRITE(reg, temp);
  3342. POSTING_READ(reg);
  3343. udelay(100);
  3344. }
  3345. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3346. {
  3347. struct intel_crtc *crtc;
  3348. /* Note that we don't need to be called with mode_config.lock here
  3349. * as our list of CRTC objects is static for the lifetime of the
  3350. * device and so cannot disappear as we iterate. Similarly, we can
  3351. * happily treat the predicates as racy, atomic checks as userspace
  3352. * cannot claim and pin a new fb without at least acquring the
  3353. * struct_mutex and so serialising with us.
  3354. */
  3355. for_each_intel_crtc(dev, crtc) {
  3356. if (atomic_read(&crtc->unpin_work_count) == 0)
  3357. continue;
  3358. if (crtc->unpin_work)
  3359. intel_wait_for_vblank(dev, crtc->pipe);
  3360. return true;
  3361. }
  3362. return false;
  3363. }
  3364. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3365. {
  3366. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3367. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3368. /* ensure that the unpin work is consistent wrt ->pending. */
  3369. smp_rmb();
  3370. intel_crtc->unpin_work = NULL;
  3371. if (work->event)
  3372. drm_send_vblank_event(intel_crtc->base.dev,
  3373. intel_crtc->pipe,
  3374. work->event);
  3375. drm_crtc_vblank_put(&intel_crtc->base);
  3376. wake_up_all(&dev_priv->pending_flip_queue);
  3377. queue_work(dev_priv->wq, &work->work);
  3378. trace_i915_flip_complete(intel_crtc->plane,
  3379. work->pending_flip_obj);
  3380. }
  3381. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3382. {
  3383. struct drm_device *dev = crtc->dev;
  3384. struct drm_i915_private *dev_priv = dev->dev_private;
  3385. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3386. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3387. !intel_crtc_has_pending_flip(crtc),
  3388. 60*HZ) == 0)) {
  3389. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3390. spin_lock_irq(&dev->event_lock);
  3391. if (intel_crtc->unpin_work) {
  3392. WARN_ONCE(1, "Removing stuck page flip\n");
  3393. page_flip_completed(intel_crtc);
  3394. }
  3395. spin_unlock_irq(&dev->event_lock);
  3396. }
  3397. if (crtc->primary->fb) {
  3398. mutex_lock(&dev->struct_mutex);
  3399. intel_finish_fb(crtc->primary->fb);
  3400. mutex_unlock(&dev->struct_mutex);
  3401. }
  3402. }
  3403. /* Program iCLKIP clock to the desired frequency */
  3404. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3405. {
  3406. struct drm_device *dev = crtc->dev;
  3407. struct drm_i915_private *dev_priv = dev->dev_private;
  3408. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3409. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3410. u32 temp;
  3411. mutex_lock(&dev_priv->dpio_lock);
  3412. /* It is necessary to ungate the pixclk gate prior to programming
  3413. * the divisors, and gate it back when it is done.
  3414. */
  3415. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3416. /* Disable SSCCTL */
  3417. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3418. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3419. SBI_SSCCTL_DISABLE,
  3420. SBI_ICLK);
  3421. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3422. if (clock == 20000) {
  3423. auxdiv = 1;
  3424. divsel = 0x41;
  3425. phaseinc = 0x20;
  3426. } else {
  3427. /* The iCLK virtual clock root frequency is in MHz,
  3428. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3429. * divisors, it is necessary to divide one by another, so we
  3430. * convert the virtual clock precision to KHz here for higher
  3431. * precision.
  3432. */
  3433. u32 iclk_virtual_root_freq = 172800 * 1000;
  3434. u32 iclk_pi_range = 64;
  3435. u32 desired_divisor, msb_divisor_value, pi_value;
  3436. desired_divisor = (iclk_virtual_root_freq / clock);
  3437. msb_divisor_value = desired_divisor / iclk_pi_range;
  3438. pi_value = desired_divisor % iclk_pi_range;
  3439. auxdiv = 0;
  3440. divsel = msb_divisor_value - 2;
  3441. phaseinc = pi_value;
  3442. }
  3443. /* This should not happen with any sane values */
  3444. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3445. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3446. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3447. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3448. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3449. clock,
  3450. auxdiv,
  3451. divsel,
  3452. phasedir,
  3453. phaseinc);
  3454. /* Program SSCDIVINTPHASE6 */
  3455. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3456. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3457. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3458. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3459. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3460. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3461. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3462. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3463. /* Program SSCAUXDIV */
  3464. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3465. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3466. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3467. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3468. /* Enable modulator and associated divider */
  3469. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3470. temp &= ~SBI_SSCCTL_DISABLE;
  3471. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3472. /* Wait for initialization time */
  3473. udelay(24);
  3474. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3475. mutex_unlock(&dev_priv->dpio_lock);
  3476. }
  3477. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3478. enum pipe pch_transcoder)
  3479. {
  3480. struct drm_device *dev = crtc->base.dev;
  3481. struct drm_i915_private *dev_priv = dev->dev_private;
  3482. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3483. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3484. I915_READ(HTOTAL(cpu_transcoder)));
  3485. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3486. I915_READ(HBLANK(cpu_transcoder)));
  3487. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3488. I915_READ(HSYNC(cpu_transcoder)));
  3489. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3490. I915_READ(VTOTAL(cpu_transcoder)));
  3491. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3492. I915_READ(VBLANK(cpu_transcoder)));
  3493. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3494. I915_READ(VSYNC(cpu_transcoder)));
  3495. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3496. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3497. }
  3498. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3499. {
  3500. struct drm_i915_private *dev_priv = dev->dev_private;
  3501. uint32_t temp;
  3502. temp = I915_READ(SOUTH_CHICKEN1);
  3503. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3504. return;
  3505. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3506. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3507. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3508. if (enable)
  3509. temp |= FDI_BC_BIFURCATION_SELECT;
  3510. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3511. I915_WRITE(SOUTH_CHICKEN1, temp);
  3512. POSTING_READ(SOUTH_CHICKEN1);
  3513. }
  3514. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3515. {
  3516. struct drm_device *dev = intel_crtc->base.dev;
  3517. switch (intel_crtc->pipe) {
  3518. case PIPE_A:
  3519. break;
  3520. case PIPE_B:
  3521. if (intel_crtc->config->fdi_lanes > 2)
  3522. cpt_set_fdi_bc_bifurcation(dev, false);
  3523. else
  3524. cpt_set_fdi_bc_bifurcation(dev, true);
  3525. break;
  3526. case PIPE_C:
  3527. cpt_set_fdi_bc_bifurcation(dev, true);
  3528. break;
  3529. default:
  3530. BUG();
  3531. }
  3532. }
  3533. /*
  3534. * Enable PCH resources required for PCH ports:
  3535. * - PCH PLLs
  3536. * - FDI training & RX/TX
  3537. * - update transcoder timings
  3538. * - DP transcoding bits
  3539. * - transcoder
  3540. */
  3541. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3542. {
  3543. struct drm_device *dev = crtc->dev;
  3544. struct drm_i915_private *dev_priv = dev->dev_private;
  3545. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3546. int pipe = intel_crtc->pipe;
  3547. u32 reg, temp;
  3548. assert_pch_transcoder_disabled(dev_priv, pipe);
  3549. if (IS_IVYBRIDGE(dev))
  3550. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3551. /* Write the TU size bits before fdi link training, so that error
  3552. * detection works. */
  3553. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3554. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3555. /* For PCH output, training FDI link */
  3556. dev_priv->display.fdi_link_train(crtc);
  3557. /* We need to program the right clock selection before writing the pixel
  3558. * mutliplier into the DPLL. */
  3559. if (HAS_PCH_CPT(dev)) {
  3560. u32 sel;
  3561. temp = I915_READ(PCH_DPLL_SEL);
  3562. temp |= TRANS_DPLL_ENABLE(pipe);
  3563. sel = TRANS_DPLLB_SEL(pipe);
  3564. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3565. temp |= sel;
  3566. else
  3567. temp &= ~sel;
  3568. I915_WRITE(PCH_DPLL_SEL, temp);
  3569. }
  3570. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3571. * transcoder, and we actually should do this to not upset any PCH
  3572. * transcoder that already use the clock when we share it.
  3573. *
  3574. * Note that enable_shared_dpll tries to do the right thing, but
  3575. * get_shared_dpll unconditionally resets the pll - we need that to have
  3576. * the right LVDS enable sequence. */
  3577. intel_enable_shared_dpll(intel_crtc);
  3578. /* set transcoder timing, panel must allow it */
  3579. assert_panel_unlocked(dev_priv, pipe);
  3580. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3581. intel_fdi_normal_train(crtc);
  3582. /* For PCH DP, enable TRANS_DP_CTL */
  3583. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3584. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3585. reg = TRANS_DP_CTL(pipe);
  3586. temp = I915_READ(reg);
  3587. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3588. TRANS_DP_SYNC_MASK |
  3589. TRANS_DP_BPC_MASK);
  3590. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3591. TRANS_DP_ENH_FRAMING);
  3592. temp |= bpc << 9; /* same format but at 11:9 */
  3593. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3594. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3595. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3596. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3597. switch (intel_trans_dp_port_sel(crtc)) {
  3598. case PCH_DP_B:
  3599. temp |= TRANS_DP_PORT_SEL_B;
  3600. break;
  3601. case PCH_DP_C:
  3602. temp |= TRANS_DP_PORT_SEL_C;
  3603. break;
  3604. case PCH_DP_D:
  3605. temp |= TRANS_DP_PORT_SEL_D;
  3606. break;
  3607. default:
  3608. BUG();
  3609. }
  3610. I915_WRITE(reg, temp);
  3611. }
  3612. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3613. }
  3614. static void lpt_pch_enable(struct drm_crtc *crtc)
  3615. {
  3616. struct drm_device *dev = crtc->dev;
  3617. struct drm_i915_private *dev_priv = dev->dev_private;
  3618. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3619. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3620. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3621. lpt_program_iclkip(crtc);
  3622. /* Set transcoder timing. */
  3623. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3624. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3625. }
  3626. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3627. {
  3628. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3629. if (pll == NULL)
  3630. return;
  3631. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3632. WARN(1, "bad %s crtc mask\n", pll->name);
  3633. return;
  3634. }
  3635. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3636. if (pll->config.crtc_mask == 0) {
  3637. WARN_ON(pll->on);
  3638. WARN_ON(pll->active);
  3639. }
  3640. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3641. }
  3642. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3643. struct intel_crtc_state *crtc_state)
  3644. {
  3645. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3646. struct intel_shared_dpll *pll;
  3647. enum intel_dpll_id i;
  3648. if (HAS_PCH_IBX(dev_priv->dev)) {
  3649. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3650. i = (enum intel_dpll_id) crtc->pipe;
  3651. pll = &dev_priv->shared_dplls[i];
  3652. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3653. crtc->base.base.id, pll->name);
  3654. WARN_ON(pll->new_config->crtc_mask);
  3655. goto found;
  3656. }
  3657. if (IS_BROXTON(dev_priv->dev)) {
  3658. /* PLL is attached to port in bxt */
  3659. struct intel_encoder *encoder;
  3660. struct intel_digital_port *intel_dig_port;
  3661. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3662. if (WARN_ON(!encoder))
  3663. return NULL;
  3664. intel_dig_port = enc_to_dig_port(&encoder->base);
  3665. /* 1:1 mapping between ports and PLLs */
  3666. i = (enum intel_dpll_id)intel_dig_port->port;
  3667. pll = &dev_priv->shared_dplls[i];
  3668. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3669. crtc->base.base.id, pll->name);
  3670. WARN_ON(pll->new_config->crtc_mask);
  3671. goto found;
  3672. }
  3673. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3674. pll = &dev_priv->shared_dplls[i];
  3675. /* Only want to check enabled timings first */
  3676. if (pll->new_config->crtc_mask == 0)
  3677. continue;
  3678. if (memcmp(&crtc_state->dpll_hw_state,
  3679. &pll->new_config->hw_state,
  3680. sizeof(pll->new_config->hw_state)) == 0) {
  3681. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3682. crtc->base.base.id, pll->name,
  3683. pll->new_config->crtc_mask,
  3684. pll->active);
  3685. goto found;
  3686. }
  3687. }
  3688. /* Ok no matching timings, maybe there's a free one? */
  3689. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3690. pll = &dev_priv->shared_dplls[i];
  3691. if (pll->new_config->crtc_mask == 0) {
  3692. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3693. crtc->base.base.id, pll->name);
  3694. goto found;
  3695. }
  3696. }
  3697. return NULL;
  3698. found:
  3699. if (pll->new_config->crtc_mask == 0)
  3700. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3701. crtc_state->shared_dpll = i;
  3702. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3703. pipe_name(crtc->pipe));
  3704. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3705. return pll;
  3706. }
  3707. /**
  3708. * intel_shared_dpll_start_config - start a new PLL staged config
  3709. * @dev_priv: DRM device
  3710. * @clear_pipes: mask of pipes that will have their PLLs freed
  3711. *
  3712. * Starts a new PLL staged config, copying the current config but
  3713. * releasing the references of pipes specified in clear_pipes.
  3714. */
  3715. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3716. unsigned clear_pipes)
  3717. {
  3718. struct intel_shared_dpll *pll;
  3719. enum intel_dpll_id i;
  3720. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3721. pll = &dev_priv->shared_dplls[i];
  3722. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3723. GFP_KERNEL);
  3724. if (!pll->new_config)
  3725. goto cleanup;
  3726. pll->new_config->crtc_mask &= ~clear_pipes;
  3727. }
  3728. return 0;
  3729. cleanup:
  3730. while (--i >= 0) {
  3731. pll = &dev_priv->shared_dplls[i];
  3732. kfree(pll->new_config);
  3733. pll->new_config = NULL;
  3734. }
  3735. return -ENOMEM;
  3736. }
  3737. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3738. {
  3739. struct intel_shared_dpll *pll;
  3740. enum intel_dpll_id i;
  3741. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3742. pll = &dev_priv->shared_dplls[i];
  3743. WARN_ON(pll->new_config == &pll->config);
  3744. pll->config = *pll->new_config;
  3745. kfree(pll->new_config);
  3746. pll->new_config = NULL;
  3747. }
  3748. }
  3749. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3750. {
  3751. struct intel_shared_dpll *pll;
  3752. enum intel_dpll_id i;
  3753. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3754. pll = &dev_priv->shared_dplls[i];
  3755. WARN_ON(pll->new_config == &pll->config);
  3756. kfree(pll->new_config);
  3757. pll->new_config = NULL;
  3758. }
  3759. }
  3760. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3761. {
  3762. struct drm_i915_private *dev_priv = dev->dev_private;
  3763. int dslreg = PIPEDSL(pipe);
  3764. u32 temp;
  3765. temp = I915_READ(dslreg);
  3766. udelay(500);
  3767. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3768. if (wait_for(I915_READ(dslreg) != temp, 5))
  3769. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3770. }
  3771. }
  3772. /**
  3773. * skl_update_scaler_users - Stages update to crtc's scaler state
  3774. * @intel_crtc: crtc
  3775. * @crtc_state: crtc_state
  3776. * @plane: plane (NULL indicates crtc is requesting update)
  3777. * @plane_state: plane's state
  3778. * @force_detach: request unconditional detachment of scaler
  3779. *
  3780. * This function updates scaler state for requested plane or crtc.
  3781. * To request scaler usage update for a plane, caller shall pass plane pointer.
  3782. * To request scaler usage update for crtc, caller shall pass plane pointer
  3783. * as NULL.
  3784. *
  3785. * Return
  3786. * 0 - scaler_usage updated successfully
  3787. * error - requested scaling cannot be supported or other error condition
  3788. */
  3789. int
  3790. skl_update_scaler_users(
  3791. struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
  3792. struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
  3793. int force_detach)
  3794. {
  3795. int need_scaling;
  3796. int idx;
  3797. int src_w, src_h, dst_w, dst_h;
  3798. int *scaler_id;
  3799. struct drm_framebuffer *fb;
  3800. struct intel_crtc_scaler_state *scaler_state;
  3801. unsigned int rotation;
  3802. if (!intel_crtc || !crtc_state)
  3803. return 0;
  3804. scaler_state = &crtc_state->scaler_state;
  3805. idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
  3806. fb = intel_plane ? plane_state->base.fb : NULL;
  3807. if (intel_plane) {
  3808. src_w = drm_rect_width(&plane_state->src) >> 16;
  3809. src_h = drm_rect_height(&plane_state->src) >> 16;
  3810. dst_w = drm_rect_width(&plane_state->dst);
  3811. dst_h = drm_rect_height(&plane_state->dst);
  3812. scaler_id = &plane_state->scaler_id;
  3813. rotation = plane_state->base.rotation;
  3814. } else {
  3815. struct drm_display_mode *adjusted_mode =
  3816. &crtc_state->base.adjusted_mode;
  3817. src_w = crtc_state->pipe_src_w;
  3818. src_h = crtc_state->pipe_src_h;
  3819. dst_w = adjusted_mode->hdisplay;
  3820. dst_h = adjusted_mode->vdisplay;
  3821. scaler_id = &scaler_state->scaler_id;
  3822. rotation = DRM_ROTATE_0;
  3823. }
  3824. need_scaling = intel_rotation_90_or_270(rotation) ?
  3825. (src_h != dst_w || src_w != dst_h):
  3826. (src_w != dst_w || src_h != dst_h);
  3827. /*
  3828. * if plane is being disabled or scaler is no more required or force detach
  3829. * - free scaler binded to this plane/crtc
  3830. * - in order to do this, update crtc->scaler_usage
  3831. *
  3832. * Here scaler state in crtc_state is set free so that
  3833. * scaler can be assigned to other user. Actual register
  3834. * update to free the scaler is done in plane/panel-fit programming.
  3835. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3836. */
  3837. if (force_detach || !need_scaling || (intel_plane &&
  3838. (!fb || !plane_state->visible))) {
  3839. if (*scaler_id >= 0) {
  3840. scaler_state->scaler_users &= ~(1 << idx);
  3841. scaler_state->scalers[*scaler_id].in_use = 0;
  3842. DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
  3843. "crtc_state = %p scaler_users = 0x%x\n",
  3844. intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
  3845. intel_plane ? intel_plane->base.base.id :
  3846. intel_crtc->base.base.id, crtc_state,
  3847. scaler_state->scaler_users);
  3848. *scaler_id = -1;
  3849. }
  3850. return 0;
  3851. }
  3852. /* range checks */
  3853. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3854. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3855. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3856. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3857. DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
  3858. "size is out of scaler range\n",
  3859. intel_plane ? "PLANE" : "CRTC",
  3860. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3861. intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
  3862. return -EINVAL;
  3863. }
  3864. /* check colorkey */
  3865. if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
  3866. DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
  3867. intel_plane->base.base.id);
  3868. return -EINVAL;
  3869. }
  3870. /* Check src format */
  3871. if (intel_plane) {
  3872. switch (fb->pixel_format) {
  3873. case DRM_FORMAT_RGB565:
  3874. case DRM_FORMAT_XBGR8888:
  3875. case DRM_FORMAT_XRGB8888:
  3876. case DRM_FORMAT_ABGR8888:
  3877. case DRM_FORMAT_ARGB8888:
  3878. case DRM_FORMAT_XRGB2101010:
  3879. case DRM_FORMAT_ARGB2101010:
  3880. case DRM_FORMAT_XBGR2101010:
  3881. case DRM_FORMAT_ABGR2101010:
  3882. case DRM_FORMAT_YUYV:
  3883. case DRM_FORMAT_YVYU:
  3884. case DRM_FORMAT_UYVY:
  3885. case DRM_FORMAT_VYUY:
  3886. break;
  3887. default:
  3888. DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
  3889. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3890. return -EINVAL;
  3891. }
  3892. }
  3893. /* mark this plane as a scaler user in crtc_state */
  3894. scaler_state->scaler_users |= (1 << idx);
  3895. DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
  3896. "crtc_state = %p scaler_users = 0x%x\n",
  3897. intel_plane ? "PLANE" : "CRTC",
  3898. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3899. src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
  3900. return 0;
  3901. }
  3902. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3903. {
  3904. struct drm_device *dev = crtc->base.dev;
  3905. struct drm_i915_private *dev_priv = dev->dev_private;
  3906. int pipe = crtc->pipe;
  3907. struct intel_crtc_scaler_state *scaler_state =
  3908. &crtc->config->scaler_state;
  3909. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3910. /* To update pfit, first update scaler state */
  3911. skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
  3912. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3913. skl_detach_scalers(crtc);
  3914. if (!enable)
  3915. return;
  3916. if (crtc->config->pch_pfit.enabled) {
  3917. int id;
  3918. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3919. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3920. return;
  3921. }
  3922. id = scaler_state->scaler_id;
  3923. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3924. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3925. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3926. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3927. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3928. }
  3929. }
  3930. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3931. {
  3932. struct drm_device *dev = crtc->base.dev;
  3933. struct drm_i915_private *dev_priv = dev->dev_private;
  3934. int pipe = crtc->pipe;
  3935. if (crtc->config->pch_pfit.enabled) {
  3936. /* Force use of hard-coded filter coefficients
  3937. * as some pre-programmed values are broken,
  3938. * e.g. x201.
  3939. */
  3940. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3941. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3942. PF_PIPE_SEL_IVB(pipe));
  3943. else
  3944. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3945. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3946. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3947. }
  3948. }
  3949. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3950. {
  3951. struct drm_device *dev = crtc->dev;
  3952. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3953. struct drm_plane *plane;
  3954. struct intel_plane *intel_plane;
  3955. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3956. intel_plane = to_intel_plane(plane);
  3957. if (intel_plane->pipe == pipe)
  3958. intel_plane_restore(&intel_plane->base);
  3959. }
  3960. }
  3961. void hsw_enable_ips(struct intel_crtc *crtc)
  3962. {
  3963. struct drm_device *dev = crtc->base.dev;
  3964. struct drm_i915_private *dev_priv = dev->dev_private;
  3965. if (!crtc->config->ips_enabled)
  3966. return;
  3967. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3968. intel_wait_for_vblank(dev, crtc->pipe);
  3969. assert_plane_enabled(dev_priv, crtc->plane);
  3970. if (IS_BROADWELL(dev)) {
  3971. mutex_lock(&dev_priv->rps.hw_lock);
  3972. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3973. mutex_unlock(&dev_priv->rps.hw_lock);
  3974. /* Quoting Art Runyan: "its not safe to expect any particular
  3975. * value in IPS_CTL bit 31 after enabling IPS through the
  3976. * mailbox." Moreover, the mailbox may return a bogus state,
  3977. * so we need to just enable it and continue on.
  3978. */
  3979. } else {
  3980. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3981. /* The bit only becomes 1 in the next vblank, so this wait here
  3982. * is essentially intel_wait_for_vblank. If we don't have this
  3983. * and don't wait for vblanks until the end of crtc_enable, then
  3984. * the HW state readout code will complain that the expected
  3985. * IPS_CTL value is not the one we read. */
  3986. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3987. DRM_ERROR("Timed out waiting for IPS enable\n");
  3988. }
  3989. }
  3990. void hsw_disable_ips(struct intel_crtc *crtc)
  3991. {
  3992. struct drm_device *dev = crtc->base.dev;
  3993. struct drm_i915_private *dev_priv = dev->dev_private;
  3994. if (!crtc->config->ips_enabled)
  3995. return;
  3996. assert_plane_enabled(dev_priv, crtc->plane);
  3997. if (IS_BROADWELL(dev)) {
  3998. mutex_lock(&dev_priv->rps.hw_lock);
  3999. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4000. mutex_unlock(&dev_priv->rps.hw_lock);
  4001. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4002. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  4003. DRM_ERROR("Timed out waiting for IPS disable\n");
  4004. } else {
  4005. I915_WRITE(IPS_CTL, 0);
  4006. POSTING_READ(IPS_CTL);
  4007. }
  4008. /* We need to wait for a vblank before we can disable the plane. */
  4009. intel_wait_for_vblank(dev, crtc->pipe);
  4010. }
  4011. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  4012. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  4013. {
  4014. struct drm_device *dev = crtc->dev;
  4015. struct drm_i915_private *dev_priv = dev->dev_private;
  4016. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4017. enum pipe pipe = intel_crtc->pipe;
  4018. int palreg = PALETTE(pipe);
  4019. int i;
  4020. bool reenable_ips = false;
  4021. /* The clocks have to be on to load the palette. */
  4022. if (!crtc->state->enable || !intel_crtc->active)
  4023. return;
  4024. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  4025. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  4026. assert_dsi_pll_enabled(dev_priv);
  4027. else
  4028. assert_pll_enabled(dev_priv, pipe);
  4029. }
  4030. /* use legacy palette for Ironlake */
  4031. if (!HAS_GMCH_DISPLAY(dev))
  4032. palreg = LGC_PALETTE(pipe);
  4033. /* Workaround : Do not read or write the pipe palette/gamma data while
  4034. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  4035. */
  4036. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  4037. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  4038. GAMMA_MODE_MODE_SPLIT)) {
  4039. hsw_disable_ips(intel_crtc);
  4040. reenable_ips = true;
  4041. }
  4042. for (i = 0; i < 256; i++) {
  4043. I915_WRITE(palreg + 4 * i,
  4044. (intel_crtc->lut_r[i] << 16) |
  4045. (intel_crtc->lut_g[i] << 8) |
  4046. intel_crtc->lut_b[i]);
  4047. }
  4048. if (reenable_ips)
  4049. hsw_enable_ips(intel_crtc);
  4050. }
  4051. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4052. {
  4053. if (intel_crtc->overlay) {
  4054. struct drm_device *dev = intel_crtc->base.dev;
  4055. struct drm_i915_private *dev_priv = dev->dev_private;
  4056. mutex_lock(&dev->struct_mutex);
  4057. dev_priv->mm.interruptible = false;
  4058. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4059. dev_priv->mm.interruptible = true;
  4060. mutex_unlock(&dev->struct_mutex);
  4061. }
  4062. /* Let userspace switch the overlay on again. In most cases userspace
  4063. * has to recompute where to put it anyway.
  4064. */
  4065. }
  4066. /**
  4067. * intel_post_enable_primary - Perform operations after enabling primary plane
  4068. * @crtc: the CRTC whose primary plane was just enabled
  4069. *
  4070. * Performs potentially sleeping operations that must be done after the primary
  4071. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4072. * called due to an explicit primary plane update, or due to an implicit
  4073. * re-enable that is caused when a sprite plane is updated to no longer
  4074. * completely hide the primary plane.
  4075. */
  4076. static void
  4077. intel_post_enable_primary(struct drm_crtc *crtc)
  4078. {
  4079. struct drm_device *dev = crtc->dev;
  4080. struct drm_i915_private *dev_priv = dev->dev_private;
  4081. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4082. int pipe = intel_crtc->pipe;
  4083. /*
  4084. * BDW signals flip done immediately if the plane
  4085. * is disabled, even if the plane enable is already
  4086. * armed to occur at the next vblank :(
  4087. */
  4088. if (IS_BROADWELL(dev))
  4089. intel_wait_for_vblank(dev, pipe);
  4090. /*
  4091. * FIXME IPS should be fine as long as one plane is
  4092. * enabled, but in practice it seems to have problems
  4093. * when going from primary only to sprite only and vice
  4094. * versa.
  4095. */
  4096. hsw_enable_ips(intel_crtc);
  4097. mutex_lock(&dev->struct_mutex);
  4098. intel_fbc_update(dev);
  4099. mutex_unlock(&dev->struct_mutex);
  4100. /*
  4101. * Gen2 reports pipe underruns whenever all planes are disabled.
  4102. * So don't enable underrun reporting before at least some planes
  4103. * are enabled.
  4104. * FIXME: Need to fix the logic to work when we turn off all planes
  4105. * but leave the pipe running.
  4106. */
  4107. if (IS_GEN2(dev))
  4108. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4109. /* Underruns don't raise interrupts, so check manually. */
  4110. if (HAS_GMCH_DISPLAY(dev))
  4111. i9xx_check_fifo_underruns(dev_priv);
  4112. }
  4113. /**
  4114. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4115. * @crtc: the CRTC whose primary plane is to be disabled
  4116. *
  4117. * Performs potentially sleeping operations that must be done before the
  4118. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4119. * be called due to an explicit primary plane update, or due to an implicit
  4120. * disable that is caused when a sprite plane completely hides the primary
  4121. * plane.
  4122. */
  4123. static void
  4124. intel_pre_disable_primary(struct drm_crtc *crtc)
  4125. {
  4126. struct drm_device *dev = crtc->dev;
  4127. struct drm_i915_private *dev_priv = dev->dev_private;
  4128. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4129. int pipe = intel_crtc->pipe;
  4130. /*
  4131. * Gen2 reports pipe underruns whenever all planes are disabled.
  4132. * So diasble underrun reporting before all the planes get disabled.
  4133. * FIXME: Need to fix the logic to work when we turn off all planes
  4134. * but leave the pipe running.
  4135. */
  4136. if (IS_GEN2(dev))
  4137. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4138. /*
  4139. * Vblank time updates from the shadow to live plane control register
  4140. * are blocked if the memory self-refresh mode is active at that
  4141. * moment. So to make sure the plane gets truly disabled, disable
  4142. * first the self-refresh mode. The self-refresh enable bit in turn
  4143. * will be checked/applied by the HW only at the next frame start
  4144. * event which is after the vblank start event, so we need to have a
  4145. * wait-for-vblank between disabling the plane and the pipe.
  4146. */
  4147. if (HAS_GMCH_DISPLAY(dev))
  4148. intel_set_memory_cxsr(dev_priv, false);
  4149. mutex_lock(&dev->struct_mutex);
  4150. if (dev_priv->fbc.crtc == intel_crtc)
  4151. intel_fbc_disable(dev);
  4152. mutex_unlock(&dev->struct_mutex);
  4153. /*
  4154. * FIXME IPS should be fine as long as one plane is
  4155. * enabled, but in practice it seems to have problems
  4156. * when going from primary only to sprite only and vice
  4157. * versa.
  4158. */
  4159. hsw_disable_ips(intel_crtc);
  4160. }
  4161. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  4162. {
  4163. intel_enable_primary_hw_plane(crtc->primary, crtc);
  4164. intel_enable_sprite_planes(crtc);
  4165. intel_crtc_update_cursor(crtc, true);
  4166. intel_post_enable_primary(crtc);
  4167. }
  4168. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  4169. {
  4170. struct drm_device *dev = crtc->dev;
  4171. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4172. struct intel_plane *intel_plane;
  4173. int pipe = intel_crtc->pipe;
  4174. intel_crtc_wait_for_pending_flips(crtc);
  4175. intel_pre_disable_primary(crtc);
  4176. intel_crtc_dpms_overlay_disable(intel_crtc);
  4177. for_each_intel_plane(dev, intel_plane) {
  4178. if (intel_plane->pipe == pipe) {
  4179. struct drm_crtc *from = intel_plane->base.crtc;
  4180. intel_plane->disable_plane(&intel_plane->base,
  4181. from ?: crtc, true);
  4182. }
  4183. }
  4184. /*
  4185. * FIXME: Once we grow proper nuclear flip support out of this we need
  4186. * to compute the mask of flip planes precisely. For the time being
  4187. * consider this a flip to a NULL plane.
  4188. */
  4189. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4190. }
  4191. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4192. {
  4193. struct drm_device *dev = crtc->dev;
  4194. struct drm_i915_private *dev_priv = dev->dev_private;
  4195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4196. struct intel_encoder *encoder;
  4197. int pipe = intel_crtc->pipe;
  4198. WARN_ON(!crtc->state->enable);
  4199. if (intel_crtc->active)
  4200. return;
  4201. if (intel_crtc->config->has_pch_encoder)
  4202. intel_prepare_shared_dpll(intel_crtc);
  4203. if (intel_crtc->config->has_dp_encoder)
  4204. intel_dp_set_m_n(intel_crtc, M1_N1);
  4205. intel_set_pipe_timings(intel_crtc);
  4206. if (intel_crtc->config->has_pch_encoder) {
  4207. intel_cpu_transcoder_set_m_n(intel_crtc,
  4208. &intel_crtc->config->fdi_m_n, NULL);
  4209. }
  4210. ironlake_set_pipeconf(crtc);
  4211. intel_crtc->active = true;
  4212. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4213. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4214. for_each_encoder_on_crtc(dev, crtc, encoder)
  4215. if (encoder->pre_enable)
  4216. encoder->pre_enable(encoder);
  4217. if (intel_crtc->config->has_pch_encoder) {
  4218. /* Note: FDI PLL enabling _must_ be done before we enable the
  4219. * cpu pipes, hence this is separate from all the other fdi/pch
  4220. * enabling. */
  4221. ironlake_fdi_pll_enable(intel_crtc);
  4222. } else {
  4223. assert_fdi_tx_disabled(dev_priv, pipe);
  4224. assert_fdi_rx_disabled(dev_priv, pipe);
  4225. }
  4226. ironlake_pfit_enable(intel_crtc);
  4227. /*
  4228. * On ILK+ LUT must be loaded before the pipe is running but with
  4229. * clocks enabled
  4230. */
  4231. intel_crtc_load_lut(crtc);
  4232. intel_update_watermarks(crtc);
  4233. intel_enable_pipe(intel_crtc);
  4234. if (intel_crtc->config->has_pch_encoder)
  4235. ironlake_pch_enable(crtc);
  4236. assert_vblank_disabled(crtc);
  4237. drm_crtc_vblank_on(crtc);
  4238. for_each_encoder_on_crtc(dev, crtc, encoder)
  4239. encoder->enable(encoder);
  4240. if (HAS_PCH_CPT(dev))
  4241. cpt_verify_modeset(dev, intel_crtc->pipe);
  4242. }
  4243. /* IPS only exists on ULT machines and is tied to pipe A. */
  4244. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4245. {
  4246. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4247. }
  4248. /*
  4249. * This implements the workaround described in the "notes" section of the mode
  4250. * set sequence documentation. When going from no pipes or single pipe to
  4251. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  4252. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  4253. */
  4254. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  4255. {
  4256. struct drm_device *dev = crtc->base.dev;
  4257. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  4258. /* We want to get the other_active_crtc only if there's only 1 other
  4259. * active crtc. */
  4260. for_each_intel_crtc(dev, crtc_it) {
  4261. if (!crtc_it->active || crtc_it == crtc)
  4262. continue;
  4263. if (other_active_crtc)
  4264. return;
  4265. other_active_crtc = crtc_it;
  4266. }
  4267. if (!other_active_crtc)
  4268. return;
  4269. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4270. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4271. }
  4272. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4273. {
  4274. struct drm_device *dev = crtc->dev;
  4275. struct drm_i915_private *dev_priv = dev->dev_private;
  4276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4277. struct intel_encoder *encoder;
  4278. int pipe = intel_crtc->pipe;
  4279. WARN_ON(!crtc->state->enable);
  4280. if (intel_crtc->active)
  4281. return;
  4282. if (intel_crtc_to_shared_dpll(intel_crtc))
  4283. intel_enable_shared_dpll(intel_crtc);
  4284. if (intel_crtc->config->has_dp_encoder)
  4285. intel_dp_set_m_n(intel_crtc, M1_N1);
  4286. intel_set_pipe_timings(intel_crtc);
  4287. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4288. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4289. intel_crtc->config->pixel_multiplier - 1);
  4290. }
  4291. if (intel_crtc->config->has_pch_encoder) {
  4292. intel_cpu_transcoder_set_m_n(intel_crtc,
  4293. &intel_crtc->config->fdi_m_n, NULL);
  4294. }
  4295. haswell_set_pipeconf(crtc);
  4296. intel_set_pipe_csc(crtc);
  4297. intel_crtc->active = true;
  4298. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4299. for_each_encoder_on_crtc(dev, crtc, encoder)
  4300. if (encoder->pre_enable)
  4301. encoder->pre_enable(encoder);
  4302. if (intel_crtc->config->has_pch_encoder) {
  4303. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4304. true);
  4305. dev_priv->display.fdi_link_train(crtc);
  4306. }
  4307. intel_ddi_enable_pipe_clock(intel_crtc);
  4308. if (INTEL_INFO(dev)->gen == 9)
  4309. skylake_pfit_update(intel_crtc, 1);
  4310. else if (INTEL_INFO(dev)->gen < 9)
  4311. ironlake_pfit_enable(intel_crtc);
  4312. else
  4313. MISSING_CASE(INTEL_INFO(dev)->gen);
  4314. /*
  4315. * On ILK+ LUT must be loaded before the pipe is running but with
  4316. * clocks enabled
  4317. */
  4318. intel_crtc_load_lut(crtc);
  4319. intel_ddi_set_pipe_settings(crtc);
  4320. intel_ddi_enable_transcoder_func(crtc);
  4321. intel_update_watermarks(crtc);
  4322. intel_enable_pipe(intel_crtc);
  4323. if (intel_crtc->config->has_pch_encoder)
  4324. lpt_pch_enable(crtc);
  4325. if (intel_crtc->config->dp_encoder_is_mst)
  4326. intel_ddi_set_vc_payload_alloc(crtc, true);
  4327. assert_vblank_disabled(crtc);
  4328. drm_crtc_vblank_on(crtc);
  4329. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4330. encoder->enable(encoder);
  4331. intel_opregion_notify_encoder(encoder, true);
  4332. }
  4333. /* If we change the relative order between pipe/planes enabling, we need
  4334. * to change the workaround. */
  4335. haswell_mode_set_planes_workaround(intel_crtc);
  4336. }
  4337. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4338. {
  4339. struct drm_device *dev = crtc->base.dev;
  4340. struct drm_i915_private *dev_priv = dev->dev_private;
  4341. int pipe = crtc->pipe;
  4342. /* To avoid upsetting the power well on haswell only disable the pfit if
  4343. * it's in use. The hw state code will make sure we get this right. */
  4344. if (crtc->config->pch_pfit.enabled) {
  4345. I915_WRITE(PF_CTL(pipe), 0);
  4346. I915_WRITE(PF_WIN_POS(pipe), 0);
  4347. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4348. }
  4349. }
  4350. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4351. {
  4352. struct drm_device *dev = crtc->dev;
  4353. struct drm_i915_private *dev_priv = dev->dev_private;
  4354. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4355. struct intel_encoder *encoder;
  4356. int pipe = intel_crtc->pipe;
  4357. u32 reg, temp;
  4358. if (!intel_crtc->active)
  4359. return;
  4360. for_each_encoder_on_crtc(dev, crtc, encoder)
  4361. encoder->disable(encoder);
  4362. drm_crtc_vblank_off(crtc);
  4363. assert_vblank_disabled(crtc);
  4364. if (intel_crtc->config->has_pch_encoder)
  4365. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4366. intel_disable_pipe(intel_crtc);
  4367. ironlake_pfit_disable(intel_crtc);
  4368. for_each_encoder_on_crtc(dev, crtc, encoder)
  4369. if (encoder->post_disable)
  4370. encoder->post_disable(encoder);
  4371. if (intel_crtc->config->has_pch_encoder) {
  4372. ironlake_fdi_disable(crtc);
  4373. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4374. if (HAS_PCH_CPT(dev)) {
  4375. /* disable TRANS_DP_CTL */
  4376. reg = TRANS_DP_CTL(pipe);
  4377. temp = I915_READ(reg);
  4378. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4379. TRANS_DP_PORT_SEL_MASK);
  4380. temp |= TRANS_DP_PORT_SEL_NONE;
  4381. I915_WRITE(reg, temp);
  4382. /* disable DPLL_SEL */
  4383. temp = I915_READ(PCH_DPLL_SEL);
  4384. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4385. I915_WRITE(PCH_DPLL_SEL, temp);
  4386. }
  4387. /* disable PCH DPLL */
  4388. intel_disable_shared_dpll(intel_crtc);
  4389. ironlake_fdi_pll_disable(intel_crtc);
  4390. }
  4391. intel_crtc->active = false;
  4392. intel_update_watermarks(crtc);
  4393. mutex_lock(&dev->struct_mutex);
  4394. intel_fbc_update(dev);
  4395. mutex_unlock(&dev->struct_mutex);
  4396. }
  4397. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4398. {
  4399. struct drm_device *dev = crtc->dev;
  4400. struct drm_i915_private *dev_priv = dev->dev_private;
  4401. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4402. struct intel_encoder *encoder;
  4403. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4404. if (!intel_crtc->active)
  4405. return;
  4406. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4407. intel_opregion_notify_encoder(encoder, false);
  4408. encoder->disable(encoder);
  4409. }
  4410. drm_crtc_vblank_off(crtc);
  4411. assert_vblank_disabled(crtc);
  4412. if (intel_crtc->config->has_pch_encoder)
  4413. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4414. false);
  4415. intel_disable_pipe(intel_crtc);
  4416. if (intel_crtc->config->dp_encoder_is_mst)
  4417. intel_ddi_set_vc_payload_alloc(crtc, false);
  4418. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4419. if (INTEL_INFO(dev)->gen == 9)
  4420. skylake_pfit_update(intel_crtc, 0);
  4421. else if (INTEL_INFO(dev)->gen < 9)
  4422. ironlake_pfit_disable(intel_crtc);
  4423. else
  4424. MISSING_CASE(INTEL_INFO(dev)->gen);
  4425. intel_ddi_disable_pipe_clock(intel_crtc);
  4426. if (intel_crtc->config->has_pch_encoder) {
  4427. lpt_disable_pch_transcoder(dev_priv);
  4428. intel_ddi_fdi_disable(crtc);
  4429. }
  4430. for_each_encoder_on_crtc(dev, crtc, encoder)
  4431. if (encoder->post_disable)
  4432. encoder->post_disable(encoder);
  4433. intel_crtc->active = false;
  4434. intel_update_watermarks(crtc);
  4435. mutex_lock(&dev->struct_mutex);
  4436. intel_fbc_update(dev);
  4437. mutex_unlock(&dev->struct_mutex);
  4438. if (intel_crtc_to_shared_dpll(intel_crtc))
  4439. intel_disable_shared_dpll(intel_crtc);
  4440. }
  4441. static void ironlake_crtc_off(struct drm_crtc *crtc)
  4442. {
  4443. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4444. intel_put_shared_dpll(intel_crtc);
  4445. }
  4446. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4447. {
  4448. struct drm_device *dev = crtc->base.dev;
  4449. struct drm_i915_private *dev_priv = dev->dev_private;
  4450. struct intel_crtc_state *pipe_config = crtc->config;
  4451. if (!pipe_config->gmch_pfit.control)
  4452. return;
  4453. /*
  4454. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4455. * according to register description and PRM.
  4456. */
  4457. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4458. assert_pipe_disabled(dev_priv, crtc->pipe);
  4459. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4460. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4461. /* Border color in case we don't scale up to the full screen. Black by
  4462. * default, change to something else for debugging. */
  4463. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4464. }
  4465. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4466. {
  4467. switch (port) {
  4468. case PORT_A:
  4469. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4470. case PORT_B:
  4471. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4472. case PORT_C:
  4473. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4474. case PORT_D:
  4475. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4476. default:
  4477. WARN_ON_ONCE(1);
  4478. return POWER_DOMAIN_PORT_OTHER;
  4479. }
  4480. }
  4481. #define for_each_power_domain(domain, mask) \
  4482. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4483. if ((1 << (domain)) & (mask))
  4484. enum intel_display_power_domain
  4485. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4486. {
  4487. struct drm_device *dev = intel_encoder->base.dev;
  4488. struct intel_digital_port *intel_dig_port;
  4489. switch (intel_encoder->type) {
  4490. case INTEL_OUTPUT_UNKNOWN:
  4491. /* Only DDI platforms should ever use this output type */
  4492. WARN_ON_ONCE(!HAS_DDI(dev));
  4493. case INTEL_OUTPUT_DISPLAYPORT:
  4494. case INTEL_OUTPUT_HDMI:
  4495. case INTEL_OUTPUT_EDP:
  4496. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4497. return port_to_power_domain(intel_dig_port->port);
  4498. case INTEL_OUTPUT_DP_MST:
  4499. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4500. return port_to_power_domain(intel_dig_port->port);
  4501. case INTEL_OUTPUT_ANALOG:
  4502. return POWER_DOMAIN_PORT_CRT;
  4503. case INTEL_OUTPUT_DSI:
  4504. return POWER_DOMAIN_PORT_DSI;
  4505. default:
  4506. return POWER_DOMAIN_PORT_OTHER;
  4507. }
  4508. }
  4509. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4510. {
  4511. struct drm_device *dev = crtc->dev;
  4512. struct intel_encoder *intel_encoder;
  4513. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4514. enum pipe pipe = intel_crtc->pipe;
  4515. unsigned long mask;
  4516. enum transcoder transcoder;
  4517. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4518. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4519. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4520. if (intel_crtc->config->pch_pfit.enabled ||
  4521. intel_crtc->config->pch_pfit.force_thru)
  4522. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4523. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4524. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4525. return mask;
  4526. }
  4527. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4528. {
  4529. struct drm_device *dev = state->dev;
  4530. struct drm_i915_private *dev_priv = dev->dev_private;
  4531. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4532. struct intel_crtc *crtc;
  4533. /*
  4534. * First get all needed power domains, then put all unneeded, to avoid
  4535. * any unnecessary toggling of the power wells.
  4536. */
  4537. for_each_intel_crtc(dev, crtc) {
  4538. enum intel_display_power_domain domain;
  4539. if (!crtc->base.state->enable)
  4540. continue;
  4541. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4542. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4543. intel_display_power_get(dev_priv, domain);
  4544. }
  4545. if (dev_priv->display.modeset_global_resources)
  4546. dev_priv->display.modeset_global_resources(state);
  4547. for_each_intel_crtc(dev, crtc) {
  4548. enum intel_display_power_domain domain;
  4549. for_each_power_domain(domain, crtc->enabled_power_domains)
  4550. intel_display_power_put(dev_priv, domain);
  4551. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4552. }
  4553. intel_display_set_init_power(dev_priv, false);
  4554. }
  4555. void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4556. {
  4557. struct drm_i915_private *dev_priv = dev->dev_private;
  4558. uint32_t divider;
  4559. uint32_t ratio;
  4560. uint32_t current_freq;
  4561. int ret;
  4562. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4563. switch (frequency) {
  4564. case 144000:
  4565. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4566. ratio = BXT_DE_PLL_RATIO(60);
  4567. break;
  4568. case 288000:
  4569. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4570. ratio = BXT_DE_PLL_RATIO(60);
  4571. break;
  4572. case 384000:
  4573. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4574. ratio = BXT_DE_PLL_RATIO(60);
  4575. break;
  4576. case 576000:
  4577. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4578. ratio = BXT_DE_PLL_RATIO(60);
  4579. break;
  4580. case 624000:
  4581. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4582. ratio = BXT_DE_PLL_RATIO(65);
  4583. break;
  4584. case 19200:
  4585. /*
  4586. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4587. * to suppress GCC warning.
  4588. */
  4589. ratio = 0;
  4590. divider = 0;
  4591. break;
  4592. default:
  4593. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4594. return;
  4595. }
  4596. mutex_lock(&dev_priv->rps.hw_lock);
  4597. /* Inform power controller of upcoming frequency change */
  4598. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4599. 0x80000000);
  4600. mutex_unlock(&dev_priv->rps.hw_lock);
  4601. if (ret) {
  4602. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4603. ret, frequency);
  4604. return;
  4605. }
  4606. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4607. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4608. current_freq = current_freq * 500 + 1000;
  4609. /*
  4610. * DE PLL has to be disabled when
  4611. * - setting to 19.2MHz (bypass, PLL isn't used)
  4612. * - before setting to 624MHz (PLL needs toggling)
  4613. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4614. */
  4615. if (frequency == 19200 || frequency == 624000 ||
  4616. current_freq == 624000) {
  4617. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4618. /* Timeout 200us */
  4619. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4620. 1))
  4621. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4622. }
  4623. if (frequency != 19200) {
  4624. uint32_t val;
  4625. val = I915_READ(BXT_DE_PLL_CTL);
  4626. val &= ~BXT_DE_PLL_RATIO_MASK;
  4627. val |= ratio;
  4628. I915_WRITE(BXT_DE_PLL_CTL, val);
  4629. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4630. /* Timeout 200us */
  4631. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4632. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4633. val = I915_READ(CDCLK_CTL);
  4634. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4635. val |= divider;
  4636. /*
  4637. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4638. * enable otherwise.
  4639. */
  4640. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4641. if (frequency >= 500000)
  4642. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4643. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4644. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4645. val |= (frequency - 1000) / 500;
  4646. I915_WRITE(CDCLK_CTL, val);
  4647. }
  4648. mutex_lock(&dev_priv->rps.hw_lock);
  4649. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4650. DIV_ROUND_UP(frequency, 25000));
  4651. mutex_unlock(&dev_priv->rps.hw_lock);
  4652. if (ret) {
  4653. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4654. ret, frequency);
  4655. return;
  4656. }
  4657. dev_priv->cdclk_freq = frequency;
  4658. }
  4659. void broxton_init_cdclk(struct drm_device *dev)
  4660. {
  4661. struct drm_i915_private *dev_priv = dev->dev_private;
  4662. uint32_t val;
  4663. /*
  4664. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4665. * or else the reset will hang because there is no PCH to respond.
  4666. * Move the handshake programming to initialization sequence.
  4667. * Previously was left up to BIOS.
  4668. */
  4669. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4670. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4671. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4672. /* Enable PG1 for cdclk */
  4673. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4674. /* check if cd clock is enabled */
  4675. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4676. DRM_DEBUG_KMS("Display already initialized\n");
  4677. return;
  4678. }
  4679. /*
  4680. * FIXME:
  4681. * - The initial CDCLK needs to be read from VBT.
  4682. * Need to make this change after VBT has changes for BXT.
  4683. * - check if setting the max (or any) cdclk freq is really necessary
  4684. * here, it belongs to modeset time
  4685. */
  4686. broxton_set_cdclk(dev, 624000);
  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 enable timeout!\n");
  4692. }
  4693. void broxton_uninit_cdclk(struct drm_device *dev)
  4694. {
  4695. struct drm_i915_private *dev_priv = dev->dev_private;
  4696. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4697. POSTING_READ(DBUF_CTL);
  4698. udelay(10);
  4699. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4700. DRM_ERROR("DBuf power disable timeout!\n");
  4701. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4702. broxton_set_cdclk(dev, 19200);
  4703. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4704. }
  4705. /* returns HPLL frequency in kHz */
  4706. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4707. {
  4708. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4709. /* Obtain SKU information */
  4710. mutex_lock(&dev_priv->dpio_lock);
  4711. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4712. CCK_FUSE_HPLL_FREQ_MASK;
  4713. mutex_unlock(&dev_priv->dpio_lock);
  4714. return vco_freq[hpll_freq] * 1000;
  4715. }
  4716. static void vlv_update_cdclk(struct drm_device *dev)
  4717. {
  4718. struct drm_i915_private *dev_priv = dev->dev_private;
  4719. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4720. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4721. dev_priv->cdclk_freq);
  4722. /*
  4723. * Program the gmbus_freq based on the cdclk frequency.
  4724. * BSpec erroneously claims we should aim for 4MHz, but
  4725. * in fact 1MHz is the correct frequency.
  4726. */
  4727. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4728. }
  4729. /* Adjust CDclk dividers to allow high res or save power if possible */
  4730. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4731. {
  4732. struct drm_i915_private *dev_priv = dev->dev_private;
  4733. u32 val, cmd;
  4734. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4735. != dev_priv->cdclk_freq);
  4736. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4737. cmd = 2;
  4738. else if (cdclk == 266667)
  4739. cmd = 1;
  4740. else
  4741. cmd = 0;
  4742. mutex_lock(&dev_priv->rps.hw_lock);
  4743. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4744. val &= ~DSPFREQGUAR_MASK;
  4745. val |= (cmd << DSPFREQGUAR_SHIFT);
  4746. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4747. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4748. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4749. 50)) {
  4750. DRM_ERROR("timed out waiting for CDclk change\n");
  4751. }
  4752. mutex_unlock(&dev_priv->rps.hw_lock);
  4753. if (cdclk == 400000) {
  4754. u32 divider;
  4755. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4756. mutex_lock(&dev_priv->dpio_lock);
  4757. /* adjust cdclk divider */
  4758. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4759. val &= ~DISPLAY_FREQUENCY_VALUES;
  4760. val |= divider;
  4761. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4762. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4763. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4764. 50))
  4765. DRM_ERROR("timed out waiting for CDclk change\n");
  4766. mutex_unlock(&dev_priv->dpio_lock);
  4767. }
  4768. mutex_lock(&dev_priv->dpio_lock);
  4769. /* adjust self-refresh exit latency value */
  4770. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4771. val &= ~0x7f;
  4772. /*
  4773. * For high bandwidth configs, we set a higher latency in the bunit
  4774. * so that the core display fetch happens in time to avoid underruns.
  4775. */
  4776. if (cdclk == 400000)
  4777. val |= 4500 / 250; /* 4.5 usec */
  4778. else
  4779. val |= 3000 / 250; /* 3.0 usec */
  4780. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4781. mutex_unlock(&dev_priv->dpio_lock);
  4782. vlv_update_cdclk(dev);
  4783. }
  4784. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4785. {
  4786. struct drm_i915_private *dev_priv = dev->dev_private;
  4787. u32 val, cmd;
  4788. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4789. != dev_priv->cdclk_freq);
  4790. switch (cdclk) {
  4791. case 333333:
  4792. case 320000:
  4793. case 266667:
  4794. case 200000:
  4795. break;
  4796. default:
  4797. MISSING_CASE(cdclk);
  4798. return;
  4799. }
  4800. /*
  4801. * Specs are full of misinformation, but testing on actual
  4802. * hardware has shown that we just need to write the desired
  4803. * CCK divider into the Punit register.
  4804. */
  4805. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4806. mutex_lock(&dev_priv->rps.hw_lock);
  4807. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4808. val &= ~DSPFREQGUAR_MASK_CHV;
  4809. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4810. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4811. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4812. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4813. 50)) {
  4814. DRM_ERROR("timed out waiting for CDclk change\n");
  4815. }
  4816. mutex_unlock(&dev_priv->rps.hw_lock);
  4817. vlv_update_cdclk(dev);
  4818. }
  4819. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4820. int max_pixclk)
  4821. {
  4822. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4823. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4824. /*
  4825. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4826. * 200MHz
  4827. * 267MHz
  4828. * 320/333MHz (depends on HPLL freq)
  4829. * 400MHz (VLV only)
  4830. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4831. * of the lower bin and adjust if needed.
  4832. *
  4833. * We seem to get an unstable or solid color picture at 200MHz.
  4834. * Not sure what's wrong. For now use 200MHz only when all pipes
  4835. * are off.
  4836. */
  4837. if (!IS_CHERRYVIEW(dev_priv) &&
  4838. max_pixclk > freq_320*limit/100)
  4839. return 400000;
  4840. else if (max_pixclk > 266667*limit/100)
  4841. return freq_320;
  4842. else if (max_pixclk > 0)
  4843. return 266667;
  4844. else
  4845. return 200000;
  4846. }
  4847. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4848. int max_pixclk)
  4849. {
  4850. /*
  4851. * FIXME:
  4852. * - remove the guardband, it's not needed on BXT
  4853. * - set 19.2MHz bypass frequency if there are no active pipes
  4854. */
  4855. if (max_pixclk > 576000*9/10)
  4856. return 624000;
  4857. else if (max_pixclk > 384000*9/10)
  4858. return 576000;
  4859. else if (max_pixclk > 288000*9/10)
  4860. return 384000;
  4861. else if (max_pixclk > 144000*9/10)
  4862. return 288000;
  4863. else
  4864. return 144000;
  4865. }
  4866. /* compute the max pixel clock for new configuration */
  4867. static int intel_mode_max_pixclk(struct drm_atomic_state *state)
  4868. {
  4869. struct drm_device *dev = state->dev;
  4870. struct intel_crtc *intel_crtc;
  4871. struct intel_crtc_state *crtc_state;
  4872. int max_pixclk = 0;
  4873. for_each_intel_crtc(dev, intel_crtc) {
  4874. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4875. if (IS_ERR(crtc_state))
  4876. return PTR_ERR(crtc_state);
  4877. if (!crtc_state->base.enable)
  4878. continue;
  4879. max_pixclk = max(max_pixclk,
  4880. crtc_state->base.adjusted_mode.crtc_clock);
  4881. }
  4882. return max_pixclk;
  4883. }
  4884. static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
  4885. unsigned *prepare_pipes)
  4886. {
  4887. struct drm_i915_private *dev_priv = to_i915(state->dev);
  4888. struct intel_crtc *intel_crtc;
  4889. int max_pixclk = intel_mode_max_pixclk(state);
  4890. int cdclk;
  4891. if (max_pixclk < 0)
  4892. return max_pixclk;
  4893. if (IS_VALLEYVIEW(dev_priv))
  4894. cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4895. else
  4896. cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  4897. if (cdclk == dev_priv->cdclk_freq)
  4898. return 0;
  4899. /* disable/enable all currently active pipes while we change cdclk */
  4900. for_each_intel_crtc(state->dev, intel_crtc)
  4901. if (intel_crtc->base.state->enable)
  4902. *prepare_pipes |= (1 << intel_crtc->pipe);
  4903. return 0;
  4904. }
  4905. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  4906. {
  4907. unsigned int credits, default_credits;
  4908. if (IS_CHERRYVIEW(dev_priv))
  4909. default_credits = PFI_CREDIT(12);
  4910. else
  4911. default_credits = PFI_CREDIT(8);
  4912. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  4913. /* CHV suggested value is 31 or 63 */
  4914. if (IS_CHERRYVIEW(dev_priv))
  4915. credits = PFI_CREDIT_31;
  4916. else
  4917. credits = PFI_CREDIT(15);
  4918. } else {
  4919. credits = default_credits;
  4920. }
  4921. /*
  4922. * WA - write default credits before re-programming
  4923. * FIXME: should we also set the resend bit here?
  4924. */
  4925. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4926. default_credits);
  4927. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4928. credits | PFI_CREDIT_RESEND);
  4929. /*
  4930. * FIXME is this guaranteed to clear
  4931. * immediately or should we poll for it?
  4932. */
  4933. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  4934. }
  4935. static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
  4936. {
  4937. struct drm_device *dev = state->dev;
  4938. struct drm_i915_private *dev_priv = dev->dev_private;
  4939. int max_pixclk = intel_mode_max_pixclk(state);
  4940. int req_cdclk;
  4941. /* The only reason this can fail is if we fail to add the crtc_state
  4942. * to the atomic state. But that can't happen since the call to
  4943. * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
  4944. * can't have failed otherwise the mode set would be aborted) added all
  4945. * the states already. */
  4946. if (WARN_ON(max_pixclk < 0))
  4947. return;
  4948. req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4949. if (req_cdclk != dev_priv->cdclk_freq) {
  4950. /*
  4951. * FIXME: We can end up here with all power domains off, yet
  4952. * with a CDCLK frequency other than the minimum. To account
  4953. * for this take the PIPE-A power domain, which covers the HW
  4954. * blocks needed for the following programming. This can be
  4955. * removed once it's guaranteed that we get here either with
  4956. * the minimum CDCLK set, or the required power domains
  4957. * enabled.
  4958. */
  4959. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4960. if (IS_CHERRYVIEW(dev))
  4961. cherryview_set_cdclk(dev, req_cdclk);
  4962. else
  4963. valleyview_set_cdclk(dev, req_cdclk);
  4964. vlv_program_pfi_credits(dev_priv);
  4965. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4966. }
  4967. }
  4968. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4969. {
  4970. struct drm_device *dev = crtc->dev;
  4971. struct drm_i915_private *dev_priv = to_i915(dev);
  4972. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4973. struct intel_encoder *encoder;
  4974. int pipe = intel_crtc->pipe;
  4975. bool is_dsi;
  4976. WARN_ON(!crtc->state->enable);
  4977. if (intel_crtc->active)
  4978. return;
  4979. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4980. if (!is_dsi) {
  4981. if (IS_CHERRYVIEW(dev))
  4982. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4983. else
  4984. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4985. }
  4986. if (intel_crtc->config->has_dp_encoder)
  4987. intel_dp_set_m_n(intel_crtc, M1_N1);
  4988. intel_set_pipe_timings(intel_crtc);
  4989. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4990. struct drm_i915_private *dev_priv = dev->dev_private;
  4991. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4992. I915_WRITE(CHV_CANVAS(pipe), 0);
  4993. }
  4994. i9xx_set_pipeconf(intel_crtc);
  4995. intel_crtc->active = true;
  4996. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4997. for_each_encoder_on_crtc(dev, crtc, encoder)
  4998. if (encoder->pre_pll_enable)
  4999. encoder->pre_pll_enable(encoder);
  5000. if (!is_dsi) {
  5001. if (IS_CHERRYVIEW(dev))
  5002. chv_enable_pll(intel_crtc, intel_crtc->config);
  5003. else
  5004. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5005. }
  5006. for_each_encoder_on_crtc(dev, crtc, encoder)
  5007. if (encoder->pre_enable)
  5008. encoder->pre_enable(encoder);
  5009. i9xx_pfit_enable(intel_crtc);
  5010. intel_crtc_load_lut(crtc);
  5011. intel_update_watermarks(crtc);
  5012. intel_enable_pipe(intel_crtc);
  5013. assert_vblank_disabled(crtc);
  5014. drm_crtc_vblank_on(crtc);
  5015. for_each_encoder_on_crtc(dev, crtc, encoder)
  5016. encoder->enable(encoder);
  5017. }
  5018. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5019. {
  5020. struct drm_device *dev = crtc->base.dev;
  5021. struct drm_i915_private *dev_priv = dev->dev_private;
  5022. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5023. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5024. }
  5025. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5026. {
  5027. struct drm_device *dev = crtc->dev;
  5028. struct drm_i915_private *dev_priv = to_i915(dev);
  5029. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5030. struct intel_encoder *encoder;
  5031. int pipe = intel_crtc->pipe;
  5032. WARN_ON(!crtc->state->enable);
  5033. if (intel_crtc->active)
  5034. return;
  5035. i9xx_set_pll_dividers(intel_crtc);
  5036. if (intel_crtc->config->has_dp_encoder)
  5037. intel_dp_set_m_n(intel_crtc, M1_N1);
  5038. intel_set_pipe_timings(intel_crtc);
  5039. i9xx_set_pipeconf(intel_crtc);
  5040. intel_crtc->active = true;
  5041. if (!IS_GEN2(dev))
  5042. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5043. for_each_encoder_on_crtc(dev, crtc, encoder)
  5044. if (encoder->pre_enable)
  5045. encoder->pre_enable(encoder);
  5046. i9xx_enable_pll(intel_crtc);
  5047. i9xx_pfit_enable(intel_crtc);
  5048. intel_crtc_load_lut(crtc);
  5049. intel_update_watermarks(crtc);
  5050. intel_enable_pipe(intel_crtc);
  5051. assert_vblank_disabled(crtc);
  5052. drm_crtc_vblank_on(crtc);
  5053. for_each_encoder_on_crtc(dev, crtc, encoder)
  5054. encoder->enable(encoder);
  5055. }
  5056. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5057. {
  5058. struct drm_device *dev = crtc->base.dev;
  5059. struct drm_i915_private *dev_priv = dev->dev_private;
  5060. if (!crtc->config->gmch_pfit.control)
  5061. return;
  5062. assert_pipe_disabled(dev_priv, crtc->pipe);
  5063. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5064. I915_READ(PFIT_CONTROL));
  5065. I915_WRITE(PFIT_CONTROL, 0);
  5066. }
  5067. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5068. {
  5069. struct drm_device *dev = crtc->dev;
  5070. struct drm_i915_private *dev_priv = dev->dev_private;
  5071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5072. struct intel_encoder *encoder;
  5073. int pipe = intel_crtc->pipe;
  5074. if (!intel_crtc->active)
  5075. return;
  5076. /*
  5077. * On gen2 planes are double buffered but the pipe isn't, so we must
  5078. * wait for planes to fully turn off before disabling the pipe.
  5079. * We also need to wait on all gmch platforms because of the
  5080. * self-refresh mode constraint explained above.
  5081. */
  5082. intel_wait_for_vblank(dev, pipe);
  5083. for_each_encoder_on_crtc(dev, crtc, encoder)
  5084. encoder->disable(encoder);
  5085. drm_crtc_vblank_off(crtc);
  5086. assert_vblank_disabled(crtc);
  5087. intel_disable_pipe(intel_crtc);
  5088. i9xx_pfit_disable(intel_crtc);
  5089. for_each_encoder_on_crtc(dev, crtc, encoder)
  5090. if (encoder->post_disable)
  5091. encoder->post_disable(encoder);
  5092. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5093. if (IS_CHERRYVIEW(dev))
  5094. chv_disable_pll(dev_priv, pipe);
  5095. else if (IS_VALLEYVIEW(dev))
  5096. vlv_disable_pll(dev_priv, pipe);
  5097. else
  5098. i9xx_disable_pll(intel_crtc);
  5099. }
  5100. if (!IS_GEN2(dev))
  5101. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5102. intel_crtc->active = false;
  5103. intel_update_watermarks(crtc);
  5104. mutex_lock(&dev->struct_mutex);
  5105. intel_fbc_update(dev);
  5106. mutex_unlock(&dev->struct_mutex);
  5107. }
  5108. static void i9xx_crtc_off(struct drm_crtc *crtc)
  5109. {
  5110. }
  5111. /* Master function to enable/disable CRTC and corresponding power wells */
  5112. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5113. {
  5114. struct drm_device *dev = crtc->dev;
  5115. struct drm_i915_private *dev_priv = dev->dev_private;
  5116. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5117. enum intel_display_power_domain domain;
  5118. unsigned long domains;
  5119. if (enable) {
  5120. if (!intel_crtc->active) {
  5121. domains = get_crtc_power_domains(crtc);
  5122. for_each_power_domain(domain, domains)
  5123. intel_display_power_get(dev_priv, domain);
  5124. intel_crtc->enabled_power_domains = domains;
  5125. dev_priv->display.crtc_enable(crtc);
  5126. intel_crtc_enable_planes(crtc);
  5127. }
  5128. } else {
  5129. if (intel_crtc->active) {
  5130. intel_crtc_disable_planes(crtc);
  5131. dev_priv->display.crtc_disable(crtc);
  5132. domains = intel_crtc->enabled_power_domains;
  5133. for_each_power_domain(domain, domains)
  5134. intel_display_power_put(dev_priv, domain);
  5135. intel_crtc->enabled_power_domains = 0;
  5136. }
  5137. }
  5138. }
  5139. /**
  5140. * Sets the power management mode of the pipe and plane.
  5141. */
  5142. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5143. {
  5144. struct drm_device *dev = crtc->dev;
  5145. struct intel_encoder *intel_encoder;
  5146. bool enable = false;
  5147. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5148. enable |= intel_encoder->connectors_active;
  5149. intel_crtc_control(crtc, enable);
  5150. }
  5151. static void intel_crtc_disable(struct drm_crtc *crtc)
  5152. {
  5153. struct drm_device *dev = crtc->dev;
  5154. struct drm_connector *connector;
  5155. struct drm_i915_private *dev_priv = dev->dev_private;
  5156. /* crtc should still be enabled when we disable it. */
  5157. WARN_ON(!crtc->state->enable);
  5158. intel_crtc_disable_planes(crtc);
  5159. dev_priv->display.crtc_disable(crtc);
  5160. dev_priv->display.off(crtc);
  5161. drm_plane_helper_disable(crtc->primary);
  5162. /* Update computed state. */
  5163. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  5164. if (!connector->encoder || !connector->encoder->crtc)
  5165. continue;
  5166. if (connector->encoder->crtc != crtc)
  5167. continue;
  5168. connector->dpms = DRM_MODE_DPMS_OFF;
  5169. to_intel_encoder(connector->encoder)->connectors_active = false;
  5170. }
  5171. }
  5172. void intel_encoder_destroy(struct drm_encoder *encoder)
  5173. {
  5174. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5175. drm_encoder_cleanup(encoder);
  5176. kfree(intel_encoder);
  5177. }
  5178. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5179. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5180. * state of the entire output pipe. */
  5181. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5182. {
  5183. if (mode == DRM_MODE_DPMS_ON) {
  5184. encoder->connectors_active = true;
  5185. intel_crtc_update_dpms(encoder->base.crtc);
  5186. } else {
  5187. encoder->connectors_active = false;
  5188. intel_crtc_update_dpms(encoder->base.crtc);
  5189. }
  5190. }
  5191. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5192. * internal consistency). */
  5193. static void intel_connector_check_state(struct intel_connector *connector)
  5194. {
  5195. if (connector->get_hw_state(connector)) {
  5196. struct intel_encoder *encoder = connector->encoder;
  5197. struct drm_crtc *crtc;
  5198. bool encoder_enabled;
  5199. enum pipe pipe;
  5200. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5201. connector->base.base.id,
  5202. connector->base.name);
  5203. /* there is no real hw state for MST connectors */
  5204. if (connector->mst_port)
  5205. return;
  5206. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5207. "wrong connector dpms state\n");
  5208. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5209. "active connector not linked to encoder\n");
  5210. if (encoder) {
  5211. I915_STATE_WARN(!encoder->connectors_active,
  5212. "encoder->connectors_active not set\n");
  5213. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5214. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5215. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5216. return;
  5217. crtc = encoder->base.crtc;
  5218. I915_STATE_WARN(!crtc->state->enable,
  5219. "crtc not enabled\n");
  5220. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5221. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5222. "encoder active on the wrong pipe\n");
  5223. }
  5224. }
  5225. }
  5226. int intel_connector_init(struct intel_connector *connector)
  5227. {
  5228. struct drm_connector_state *connector_state;
  5229. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5230. if (!connector_state)
  5231. return -ENOMEM;
  5232. connector->base.state = connector_state;
  5233. return 0;
  5234. }
  5235. struct intel_connector *intel_connector_alloc(void)
  5236. {
  5237. struct intel_connector *connector;
  5238. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5239. if (!connector)
  5240. return NULL;
  5241. if (intel_connector_init(connector) < 0) {
  5242. kfree(connector);
  5243. return NULL;
  5244. }
  5245. return connector;
  5246. }
  5247. /* Even simpler default implementation, if there's really no special case to
  5248. * consider. */
  5249. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5250. {
  5251. /* All the simple cases only support two dpms states. */
  5252. if (mode != DRM_MODE_DPMS_ON)
  5253. mode = DRM_MODE_DPMS_OFF;
  5254. if (mode == connector->dpms)
  5255. return;
  5256. connector->dpms = mode;
  5257. /* Only need to change hw state when actually enabled */
  5258. if (connector->encoder)
  5259. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5260. intel_modeset_check_state(connector->dev);
  5261. }
  5262. /* Simple connector->get_hw_state implementation for encoders that support only
  5263. * one connector and no cloning and hence the encoder state determines the state
  5264. * of the connector. */
  5265. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5266. {
  5267. enum pipe pipe = 0;
  5268. struct intel_encoder *encoder = connector->encoder;
  5269. return encoder->get_hw_state(encoder, &pipe);
  5270. }
  5271. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5272. {
  5273. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5274. return crtc_state->fdi_lanes;
  5275. return 0;
  5276. }
  5277. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5278. struct intel_crtc_state *pipe_config)
  5279. {
  5280. struct drm_atomic_state *state = pipe_config->base.state;
  5281. struct intel_crtc *other_crtc;
  5282. struct intel_crtc_state *other_crtc_state;
  5283. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5284. pipe_name(pipe), pipe_config->fdi_lanes);
  5285. if (pipe_config->fdi_lanes > 4) {
  5286. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5287. pipe_name(pipe), pipe_config->fdi_lanes);
  5288. return -EINVAL;
  5289. }
  5290. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5291. if (pipe_config->fdi_lanes > 2) {
  5292. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5293. pipe_config->fdi_lanes);
  5294. return -EINVAL;
  5295. } else {
  5296. return 0;
  5297. }
  5298. }
  5299. if (INTEL_INFO(dev)->num_pipes == 2)
  5300. return 0;
  5301. /* Ivybridge 3 pipe is really complicated */
  5302. switch (pipe) {
  5303. case PIPE_A:
  5304. return 0;
  5305. case PIPE_B:
  5306. if (pipe_config->fdi_lanes <= 2)
  5307. return 0;
  5308. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5309. other_crtc_state =
  5310. intel_atomic_get_crtc_state(state, other_crtc);
  5311. if (IS_ERR(other_crtc_state))
  5312. return PTR_ERR(other_crtc_state);
  5313. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5314. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5315. pipe_name(pipe), pipe_config->fdi_lanes);
  5316. return -EINVAL;
  5317. }
  5318. return 0;
  5319. case PIPE_C:
  5320. if (pipe_config->fdi_lanes > 2) {
  5321. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5322. pipe_name(pipe), pipe_config->fdi_lanes);
  5323. return -EINVAL;
  5324. }
  5325. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5326. other_crtc_state =
  5327. intel_atomic_get_crtc_state(state, other_crtc);
  5328. if (IS_ERR(other_crtc_state))
  5329. return PTR_ERR(other_crtc_state);
  5330. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5331. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5332. return -EINVAL;
  5333. }
  5334. return 0;
  5335. default:
  5336. BUG();
  5337. }
  5338. }
  5339. #define RETRY 1
  5340. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5341. struct intel_crtc_state *pipe_config)
  5342. {
  5343. struct drm_device *dev = intel_crtc->base.dev;
  5344. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5345. int lane, link_bw, fdi_dotclock, ret;
  5346. bool needs_recompute = false;
  5347. retry:
  5348. /* FDI is a binary signal running at ~2.7GHz, encoding
  5349. * each output octet as 10 bits. The actual frequency
  5350. * is stored as a divider into a 100MHz clock, and the
  5351. * mode pixel clock is stored in units of 1KHz.
  5352. * Hence the bw of each lane in terms of the mode signal
  5353. * is:
  5354. */
  5355. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5356. fdi_dotclock = adjusted_mode->crtc_clock;
  5357. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5358. pipe_config->pipe_bpp);
  5359. pipe_config->fdi_lanes = lane;
  5360. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5361. link_bw, &pipe_config->fdi_m_n);
  5362. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5363. intel_crtc->pipe, pipe_config);
  5364. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5365. pipe_config->pipe_bpp -= 2*3;
  5366. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5367. pipe_config->pipe_bpp);
  5368. needs_recompute = true;
  5369. pipe_config->bw_constrained = true;
  5370. goto retry;
  5371. }
  5372. if (needs_recompute)
  5373. return RETRY;
  5374. return ret;
  5375. }
  5376. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5377. struct intel_crtc_state *pipe_config)
  5378. {
  5379. pipe_config->ips_enabled = i915.enable_ips &&
  5380. hsw_crtc_supports_ips(crtc) &&
  5381. pipe_config->pipe_bpp <= 24;
  5382. }
  5383. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5384. struct intel_crtc_state *pipe_config)
  5385. {
  5386. struct drm_device *dev = crtc->base.dev;
  5387. struct drm_i915_private *dev_priv = dev->dev_private;
  5388. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5389. int ret;
  5390. /* FIXME should check pixel clock limits on all platforms */
  5391. if (INTEL_INFO(dev)->gen < 4) {
  5392. int clock_limit =
  5393. dev_priv->display.get_display_clock_speed(dev);
  5394. /*
  5395. * Enable pixel doubling when the dot clock
  5396. * is > 90% of the (display) core speed.
  5397. *
  5398. * GDG double wide on either pipe,
  5399. * otherwise pipe A only.
  5400. */
  5401. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5402. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5403. clock_limit *= 2;
  5404. pipe_config->double_wide = true;
  5405. }
  5406. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5407. return -EINVAL;
  5408. }
  5409. /*
  5410. * Pipe horizontal size must be even in:
  5411. * - DVO ganged mode
  5412. * - LVDS dual channel mode
  5413. * - Double wide pipe
  5414. */
  5415. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5416. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5417. pipe_config->pipe_src_w &= ~1;
  5418. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5419. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5420. */
  5421. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5422. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5423. return -EINVAL;
  5424. if (HAS_IPS(dev))
  5425. hsw_compute_ips_config(crtc, pipe_config);
  5426. if (pipe_config->has_pch_encoder)
  5427. return ironlake_fdi_compute_config(crtc, pipe_config);
  5428. /* FIXME: remove below call once atomic mode set is place and all crtc
  5429. * related checks called from atomic_crtc_check function */
  5430. ret = 0;
  5431. DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
  5432. crtc, pipe_config->base.state);
  5433. ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
  5434. return ret;
  5435. }
  5436. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5437. {
  5438. struct drm_i915_private *dev_priv = to_i915(dev);
  5439. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5440. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5441. uint32_t linkrate;
  5442. if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
  5443. WARN(1, "LCPLL1 not enabled\n");
  5444. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5445. }
  5446. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5447. return 540000;
  5448. linkrate = (I915_READ(DPLL_CTRL1) &
  5449. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5450. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5451. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5452. /* vco 8640 */
  5453. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5454. case CDCLK_FREQ_450_432:
  5455. return 432000;
  5456. case CDCLK_FREQ_337_308:
  5457. return 308570;
  5458. case CDCLK_FREQ_675_617:
  5459. return 617140;
  5460. default:
  5461. WARN(1, "Unknown cd freq selection\n");
  5462. }
  5463. } else {
  5464. /* vco 8100 */
  5465. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5466. case CDCLK_FREQ_450_432:
  5467. return 450000;
  5468. case CDCLK_FREQ_337_308:
  5469. return 337500;
  5470. case CDCLK_FREQ_675_617:
  5471. return 675000;
  5472. default:
  5473. WARN(1, "Unknown cd freq selection\n");
  5474. }
  5475. }
  5476. /* error case, do as if DPLL0 isn't enabled */
  5477. return 24000;
  5478. }
  5479. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5480. {
  5481. struct drm_i915_private *dev_priv = dev->dev_private;
  5482. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5483. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5484. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5485. return 800000;
  5486. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5487. return 450000;
  5488. else if (freq == LCPLL_CLK_FREQ_450)
  5489. return 450000;
  5490. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5491. return 540000;
  5492. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5493. return 337500;
  5494. else
  5495. return 675000;
  5496. }
  5497. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5498. {
  5499. struct drm_i915_private *dev_priv = dev->dev_private;
  5500. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5501. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5502. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5503. return 800000;
  5504. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5505. return 450000;
  5506. else if (freq == LCPLL_CLK_FREQ_450)
  5507. return 450000;
  5508. else if (IS_HSW_ULT(dev))
  5509. return 337500;
  5510. else
  5511. return 540000;
  5512. }
  5513. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5514. {
  5515. struct drm_i915_private *dev_priv = dev->dev_private;
  5516. u32 val;
  5517. int divider;
  5518. if (dev_priv->hpll_freq == 0)
  5519. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5520. mutex_lock(&dev_priv->dpio_lock);
  5521. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5522. mutex_unlock(&dev_priv->dpio_lock);
  5523. divider = val & DISPLAY_FREQUENCY_VALUES;
  5524. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5525. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5526. "cdclk change in progress\n");
  5527. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5528. }
  5529. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5530. {
  5531. return 450000;
  5532. }
  5533. static int i945_get_display_clock_speed(struct drm_device *dev)
  5534. {
  5535. return 400000;
  5536. }
  5537. static int i915_get_display_clock_speed(struct drm_device *dev)
  5538. {
  5539. return 333333;
  5540. }
  5541. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5542. {
  5543. return 200000;
  5544. }
  5545. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5546. {
  5547. u16 gcfgc = 0;
  5548. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5549. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5550. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5551. return 266667;
  5552. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5553. return 333333;
  5554. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5555. return 444444;
  5556. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5557. return 200000;
  5558. default:
  5559. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5560. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5561. return 133333;
  5562. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5563. return 166667;
  5564. }
  5565. }
  5566. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5567. {
  5568. u16 gcfgc = 0;
  5569. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5570. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5571. return 133333;
  5572. else {
  5573. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5574. case GC_DISPLAY_CLOCK_333_MHZ:
  5575. return 333333;
  5576. default:
  5577. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5578. return 190000;
  5579. }
  5580. }
  5581. }
  5582. static int i865_get_display_clock_speed(struct drm_device *dev)
  5583. {
  5584. return 266667;
  5585. }
  5586. static int i855_get_display_clock_speed(struct drm_device *dev)
  5587. {
  5588. u16 hpllcc = 0;
  5589. /* Assume that the hardware is in the high speed state. This
  5590. * should be the default.
  5591. */
  5592. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5593. case GC_CLOCK_133_200:
  5594. case GC_CLOCK_100_200:
  5595. return 200000;
  5596. case GC_CLOCK_166_250:
  5597. return 250000;
  5598. case GC_CLOCK_100_133:
  5599. return 133333;
  5600. }
  5601. /* Shouldn't happen */
  5602. return 0;
  5603. }
  5604. static int i830_get_display_clock_speed(struct drm_device *dev)
  5605. {
  5606. return 133333;
  5607. }
  5608. static void
  5609. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5610. {
  5611. while (*num > DATA_LINK_M_N_MASK ||
  5612. *den > DATA_LINK_M_N_MASK) {
  5613. *num >>= 1;
  5614. *den >>= 1;
  5615. }
  5616. }
  5617. static void compute_m_n(unsigned int m, unsigned int n,
  5618. uint32_t *ret_m, uint32_t *ret_n)
  5619. {
  5620. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5621. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5622. intel_reduce_m_n_ratio(ret_m, ret_n);
  5623. }
  5624. void
  5625. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5626. int pixel_clock, int link_clock,
  5627. struct intel_link_m_n *m_n)
  5628. {
  5629. m_n->tu = 64;
  5630. compute_m_n(bits_per_pixel * pixel_clock,
  5631. link_clock * nlanes * 8,
  5632. &m_n->gmch_m, &m_n->gmch_n);
  5633. compute_m_n(pixel_clock, link_clock,
  5634. &m_n->link_m, &m_n->link_n);
  5635. }
  5636. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5637. {
  5638. if (i915.panel_use_ssc >= 0)
  5639. return i915.panel_use_ssc != 0;
  5640. return dev_priv->vbt.lvds_use_ssc
  5641. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5642. }
  5643. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5644. int num_connectors)
  5645. {
  5646. struct drm_device *dev = crtc_state->base.crtc->dev;
  5647. struct drm_i915_private *dev_priv = dev->dev_private;
  5648. int refclk;
  5649. WARN_ON(!crtc_state->base.state);
  5650. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5651. refclk = 100000;
  5652. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5653. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5654. refclk = dev_priv->vbt.lvds_ssc_freq;
  5655. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5656. } else if (!IS_GEN2(dev)) {
  5657. refclk = 96000;
  5658. } else {
  5659. refclk = 48000;
  5660. }
  5661. return refclk;
  5662. }
  5663. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5664. {
  5665. return (1 << dpll->n) << 16 | dpll->m2;
  5666. }
  5667. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5668. {
  5669. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5670. }
  5671. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5672. struct intel_crtc_state *crtc_state,
  5673. intel_clock_t *reduced_clock)
  5674. {
  5675. struct drm_device *dev = crtc->base.dev;
  5676. u32 fp, fp2 = 0;
  5677. if (IS_PINEVIEW(dev)) {
  5678. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5679. if (reduced_clock)
  5680. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5681. } else {
  5682. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5683. if (reduced_clock)
  5684. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5685. }
  5686. crtc_state->dpll_hw_state.fp0 = fp;
  5687. crtc->lowfreq_avail = false;
  5688. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5689. reduced_clock) {
  5690. crtc_state->dpll_hw_state.fp1 = fp2;
  5691. crtc->lowfreq_avail = true;
  5692. } else {
  5693. crtc_state->dpll_hw_state.fp1 = fp;
  5694. }
  5695. }
  5696. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5697. pipe)
  5698. {
  5699. u32 reg_val;
  5700. /*
  5701. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5702. * and set it to a reasonable value instead.
  5703. */
  5704. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5705. reg_val &= 0xffffff00;
  5706. reg_val |= 0x00000030;
  5707. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5708. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5709. reg_val &= 0x8cffffff;
  5710. reg_val = 0x8c000000;
  5711. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5712. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5713. reg_val &= 0xffffff00;
  5714. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5715. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5716. reg_val &= 0x00ffffff;
  5717. reg_val |= 0xb0000000;
  5718. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5719. }
  5720. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5721. struct intel_link_m_n *m_n)
  5722. {
  5723. struct drm_device *dev = crtc->base.dev;
  5724. struct drm_i915_private *dev_priv = dev->dev_private;
  5725. int pipe = crtc->pipe;
  5726. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5727. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5728. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5729. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5730. }
  5731. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5732. struct intel_link_m_n *m_n,
  5733. struct intel_link_m_n *m2_n2)
  5734. {
  5735. struct drm_device *dev = crtc->base.dev;
  5736. struct drm_i915_private *dev_priv = dev->dev_private;
  5737. int pipe = crtc->pipe;
  5738. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5739. if (INTEL_INFO(dev)->gen >= 5) {
  5740. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5741. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5742. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5743. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5744. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5745. * for gen < 8) and if DRRS is supported (to make sure the
  5746. * registers are not unnecessarily accessed).
  5747. */
  5748. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  5749. crtc->config->has_drrs) {
  5750. I915_WRITE(PIPE_DATA_M2(transcoder),
  5751. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5752. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5753. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5754. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5755. }
  5756. } else {
  5757. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5758. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5759. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5760. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5761. }
  5762. }
  5763. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5764. {
  5765. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5766. if (m_n == M1_N1) {
  5767. dp_m_n = &crtc->config->dp_m_n;
  5768. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5769. } else if (m_n == M2_N2) {
  5770. /*
  5771. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5772. * needs to be programmed into M1_N1.
  5773. */
  5774. dp_m_n = &crtc->config->dp_m2_n2;
  5775. } else {
  5776. DRM_ERROR("Unsupported divider value\n");
  5777. return;
  5778. }
  5779. if (crtc->config->has_pch_encoder)
  5780. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5781. else
  5782. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5783. }
  5784. static void vlv_update_pll(struct intel_crtc *crtc,
  5785. struct intel_crtc_state *pipe_config)
  5786. {
  5787. u32 dpll, dpll_md;
  5788. /*
  5789. * Enable DPIO clock input. We should never disable the reference
  5790. * clock for pipe B, since VGA hotplug / manual detection depends
  5791. * on it.
  5792. */
  5793. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  5794. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  5795. /* We should never disable this, set it here for state tracking */
  5796. if (crtc->pipe == PIPE_B)
  5797. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5798. dpll |= DPLL_VCO_ENABLE;
  5799. pipe_config->dpll_hw_state.dpll = dpll;
  5800. dpll_md = (pipe_config->pixel_multiplier - 1)
  5801. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5802. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  5803. }
  5804. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5805. const struct intel_crtc_state *pipe_config)
  5806. {
  5807. struct drm_device *dev = crtc->base.dev;
  5808. struct drm_i915_private *dev_priv = dev->dev_private;
  5809. int pipe = crtc->pipe;
  5810. u32 mdiv;
  5811. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5812. u32 coreclk, reg_val;
  5813. mutex_lock(&dev_priv->dpio_lock);
  5814. bestn = pipe_config->dpll.n;
  5815. bestm1 = pipe_config->dpll.m1;
  5816. bestm2 = pipe_config->dpll.m2;
  5817. bestp1 = pipe_config->dpll.p1;
  5818. bestp2 = pipe_config->dpll.p2;
  5819. /* See eDP HDMI DPIO driver vbios notes doc */
  5820. /* PLL B needs special handling */
  5821. if (pipe == PIPE_B)
  5822. vlv_pllb_recal_opamp(dev_priv, pipe);
  5823. /* Set up Tx target for periodic Rcomp update */
  5824. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5825. /* Disable target IRef on PLL */
  5826. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5827. reg_val &= 0x00ffffff;
  5828. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5829. /* Disable fast lock */
  5830. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5831. /* Set idtafcrecal before PLL is enabled */
  5832. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5833. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5834. mdiv |= ((bestn << DPIO_N_SHIFT));
  5835. mdiv |= (1 << DPIO_K_SHIFT);
  5836. /*
  5837. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5838. * but we don't support that).
  5839. * Note: don't use the DAC post divider as it seems unstable.
  5840. */
  5841. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5842. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5843. mdiv |= DPIO_ENABLE_CALIBRATION;
  5844. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5845. /* Set HBR and RBR LPF coefficients */
  5846. if (pipe_config->port_clock == 162000 ||
  5847. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  5848. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  5849. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5850. 0x009f0003);
  5851. else
  5852. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5853. 0x00d0000f);
  5854. if (pipe_config->has_dp_encoder) {
  5855. /* Use SSC source */
  5856. if (pipe == PIPE_A)
  5857. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5858. 0x0df40000);
  5859. else
  5860. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5861. 0x0df70000);
  5862. } else { /* HDMI or VGA */
  5863. /* Use bend source */
  5864. if (pipe == PIPE_A)
  5865. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5866. 0x0df70000);
  5867. else
  5868. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5869. 0x0df40000);
  5870. }
  5871. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5872. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5873. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5874. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5875. coreclk |= 0x01000000;
  5876. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5877. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5878. mutex_unlock(&dev_priv->dpio_lock);
  5879. }
  5880. static void chv_update_pll(struct intel_crtc *crtc,
  5881. struct intel_crtc_state *pipe_config)
  5882. {
  5883. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5884. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5885. DPLL_VCO_ENABLE;
  5886. if (crtc->pipe != PIPE_A)
  5887. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5888. pipe_config->dpll_hw_state.dpll_md =
  5889. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5890. }
  5891. static void chv_prepare_pll(struct intel_crtc *crtc,
  5892. const struct intel_crtc_state *pipe_config)
  5893. {
  5894. struct drm_device *dev = crtc->base.dev;
  5895. struct drm_i915_private *dev_priv = dev->dev_private;
  5896. int pipe = crtc->pipe;
  5897. int dpll_reg = DPLL(crtc->pipe);
  5898. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5899. u32 loopfilter, tribuf_calcntr;
  5900. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5901. u32 dpio_val;
  5902. int vco;
  5903. bestn = pipe_config->dpll.n;
  5904. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5905. bestm1 = pipe_config->dpll.m1;
  5906. bestm2 = pipe_config->dpll.m2 >> 22;
  5907. bestp1 = pipe_config->dpll.p1;
  5908. bestp2 = pipe_config->dpll.p2;
  5909. vco = pipe_config->dpll.vco;
  5910. dpio_val = 0;
  5911. loopfilter = 0;
  5912. /*
  5913. * Enable Refclk and SSC
  5914. */
  5915. I915_WRITE(dpll_reg,
  5916. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5917. mutex_lock(&dev_priv->dpio_lock);
  5918. /* p1 and p2 divider */
  5919. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5920. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5921. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5922. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5923. 1 << DPIO_CHV_K_DIV_SHIFT);
  5924. /* Feedback post-divider - m2 */
  5925. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5926. /* Feedback refclk divider - n and m1 */
  5927. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5928. DPIO_CHV_M1_DIV_BY_2 |
  5929. 1 << DPIO_CHV_N_DIV_SHIFT);
  5930. /* M2 fraction division */
  5931. if (bestm2_frac)
  5932. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5933. /* M2 fraction division enable */
  5934. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5935. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5936. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5937. if (bestm2_frac)
  5938. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5939. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5940. /* Program digital lock detect threshold */
  5941. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5942. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5943. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5944. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5945. if (!bestm2_frac)
  5946. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5947. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5948. /* Loop filter */
  5949. if (vco == 5400000) {
  5950. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5951. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5952. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5953. tribuf_calcntr = 0x9;
  5954. } else if (vco <= 6200000) {
  5955. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5956. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5957. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5958. tribuf_calcntr = 0x9;
  5959. } else if (vco <= 6480000) {
  5960. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5961. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5962. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5963. tribuf_calcntr = 0x8;
  5964. } else {
  5965. /* Not supported. Apply the same limits as in the max case */
  5966. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5967. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5968. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5969. tribuf_calcntr = 0;
  5970. }
  5971. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5972. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5973. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5974. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5975. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5976. /* AFC Recal */
  5977. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5978. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5979. DPIO_AFC_RECAL);
  5980. mutex_unlock(&dev_priv->dpio_lock);
  5981. }
  5982. /**
  5983. * vlv_force_pll_on - forcibly enable just the PLL
  5984. * @dev_priv: i915 private structure
  5985. * @pipe: pipe PLL to enable
  5986. * @dpll: PLL configuration
  5987. *
  5988. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5989. * in cases where we need the PLL enabled even when @pipe is not going to
  5990. * be enabled.
  5991. */
  5992. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5993. const struct dpll *dpll)
  5994. {
  5995. struct intel_crtc *crtc =
  5996. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5997. struct intel_crtc_state pipe_config = {
  5998. .base.crtc = &crtc->base,
  5999. .pixel_multiplier = 1,
  6000. .dpll = *dpll,
  6001. };
  6002. if (IS_CHERRYVIEW(dev)) {
  6003. chv_update_pll(crtc, &pipe_config);
  6004. chv_prepare_pll(crtc, &pipe_config);
  6005. chv_enable_pll(crtc, &pipe_config);
  6006. } else {
  6007. vlv_update_pll(crtc, &pipe_config);
  6008. vlv_prepare_pll(crtc, &pipe_config);
  6009. vlv_enable_pll(crtc, &pipe_config);
  6010. }
  6011. }
  6012. /**
  6013. * vlv_force_pll_off - forcibly disable just the PLL
  6014. * @dev_priv: i915 private structure
  6015. * @pipe: pipe PLL to disable
  6016. *
  6017. * Disable the PLL for @pipe. To be used in cases where we need
  6018. * the PLL enabled even when @pipe is not going to be enabled.
  6019. */
  6020. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6021. {
  6022. if (IS_CHERRYVIEW(dev))
  6023. chv_disable_pll(to_i915(dev), pipe);
  6024. else
  6025. vlv_disable_pll(to_i915(dev), pipe);
  6026. }
  6027. static void i9xx_update_pll(struct intel_crtc *crtc,
  6028. struct intel_crtc_state *crtc_state,
  6029. intel_clock_t *reduced_clock,
  6030. int num_connectors)
  6031. {
  6032. struct drm_device *dev = crtc->base.dev;
  6033. struct drm_i915_private *dev_priv = dev->dev_private;
  6034. u32 dpll;
  6035. bool is_sdvo;
  6036. struct dpll *clock = &crtc_state->dpll;
  6037. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6038. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6039. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6040. dpll = DPLL_VGA_MODE_DIS;
  6041. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6042. dpll |= DPLLB_MODE_LVDS;
  6043. else
  6044. dpll |= DPLLB_MODE_DAC_SERIAL;
  6045. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6046. dpll |= (crtc_state->pixel_multiplier - 1)
  6047. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6048. }
  6049. if (is_sdvo)
  6050. dpll |= DPLL_SDVO_HIGH_SPEED;
  6051. if (crtc_state->has_dp_encoder)
  6052. dpll |= DPLL_SDVO_HIGH_SPEED;
  6053. /* compute bitmask from p1 value */
  6054. if (IS_PINEVIEW(dev))
  6055. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6056. else {
  6057. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6058. if (IS_G4X(dev) && reduced_clock)
  6059. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6060. }
  6061. switch (clock->p2) {
  6062. case 5:
  6063. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6064. break;
  6065. case 7:
  6066. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6067. break;
  6068. case 10:
  6069. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6070. break;
  6071. case 14:
  6072. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6073. break;
  6074. }
  6075. if (INTEL_INFO(dev)->gen >= 4)
  6076. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6077. if (crtc_state->sdvo_tv_clock)
  6078. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6079. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6080. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6081. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6082. else
  6083. dpll |= PLL_REF_INPUT_DREFCLK;
  6084. dpll |= DPLL_VCO_ENABLE;
  6085. crtc_state->dpll_hw_state.dpll = dpll;
  6086. if (INTEL_INFO(dev)->gen >= 4) {
  6087. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6088. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6089. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6090. }
  6091. }
  6092. static void i8xx_update_pll(struct intel_crtc *crtc,
  6093. struct intel_crtc_state *crtc_state,
  6094. intel_clock_t *reduced_clock,
  6095. int num_connectors)
  6096. {
  6097. struct drm_device *dev = crtc->base.dev;
  6098. struct drm_i915_private *dev_priv = dev->dev_private;
  6099. u32 dpll;
  6100. struct dpll *clock = &crtc_state->dpll;
  6101. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6102. dpll = DPLL_VGA_MODE_DIS;
  6103. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6104. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6105. } else {
  6106. if (clock->p1 == 2)
  6107. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6108. else
  6109. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6110. if (clock->p2 == 4)
  6111. dpll |= PLL_P2_DIVIDE_BY_4;
  6112. }
  6113. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6114. dpll |= DPLL_DVO_2X_MODE;
  6115. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6116. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6117. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6118. else
  6119. dpll |= PLL_REF_INPUT_DREFCLK;
  6120. dpll |= DPLL_VCO_ENABLE;
  6121. crtc_state->dpll_hw_state.dpll = dpll;
  6122. }
  6123. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6124. {
  6125. struct drm_device *dev = intel_crtc->base.dev;
  6126. struct drm_i915_private *dev_priv = dev->dev_private;
  6127. enum pipe pipe = intel_crtc->pipe;
  6128. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6129. struct drm_display_mode *adjusted_mode =
  6130. &intel_crtc->config->base.adjusted_mode;
  6131. uint32_t crtc_vtotal, crtc_vblank_end;
  6132. int vsyncshift = 0;
  6133. /* We need to be careful not to changed the adjusted mode, for otherwise
  6134. * the hw state checker will get angry at the mismatch. */
  6135. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6136. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6137. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6138. /* the chip adds 2 halflines automatically */
  6139. crtc_vtotal -= 1;
  6140. crtc_vblank_end -= 1;
  6141. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6142. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6143. else
  6144. vsyncshift = adjusted_mode->crtc_hsync_start -
  6145. adjusted_mode->crtc_htotal / 2;
  6146. if (vsyncshift < 0)
  6147. vsyncshift += adjusted_mode->crtc_htotal;
  6148. }
  6149. if (INTEL_INFO(dev)->gen > 3)
  6150. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6151. I915_WRITE(HTOTAL(cpu_transcoder),
  6152. (adjusted_mode->crtc_hdisplay - 1) |
  6153. ((adjusted_mode->crtc_htotal - 1) << 16));
  6154. I915_WRITE(HBLANK(cpu_transcoder),
  6155. (adjusted_mode->crtc_hblank_start - 1) |
  6156. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6157. I915_WRITE(HSYNC(cpu_transcoder),
  6158. (adjusted_mode->crtc_hsync_start - 1) |
  6159. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6160. I915_WRITE(VTOTAL(cpu_transcoder),
  6161. (adjusted_mode->crtc_vdisplay - 1) |
  6162. ((crtc_vtotal - 1) << 16));
  6163. I915_WRITE(VBLANK(cpu_transcoder),
  6164. (adjusted_mode->crtc_vblank_start - 1) |
  6165. ((crtc_vblank_end - 1) << 16));
  6166. I915_WRITE(VSYNC(cpu_transcoder),
  6167. (adjusted_mode->crtc_vsync_start - 1) |
  6168. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6169. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6170. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6171. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6172. * bits. */
  6173. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6174. (pipe == PIPE_B || pipe == PIPE_C))
  6175. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6176. /* pipesrc controls the size that is scaled from, which should
  6177. * always be the user's requested size.
  6178. */
  6179. I915_WRITE(PIPESRC(pipe),
  6180. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6181. (intel_crtc->config->pipe_src_h - 1));
  6182. }
  6183. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6184. struct intel_crtc_state *pipe_config)
  6185. {
  6186. struct drm_device *dev = crtc->base.dev;
  6187. struct drm_i915_private *dev_priv = dev->dev_private;
  6188. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6189. uint32_t tmp;
  6190. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6191. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6192. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6193. tmp = I915_READ(HBLANK(cpu_transcoder));
  6194. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6195. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6196. tmp = I915_READ(HSYNC(cpu_transcoder));
  6197. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6198. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6199. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6200. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6201. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6202. tmp = I915_READ(VBLANK(cpu_transcoder));
  6203. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6204. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6205. tmp = I915_READ(VSYNC(cpu_transcoder));
  6206. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6207. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6208. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6209. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6210. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6211. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6212. }
  6213. tmp = I915_READ(PIPESRC(crtc->pipe));
  6214. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6215. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6216. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6217. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6218. }
  6219. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6220. struct intel_crtc_state *pipe_config)
  6221. {
  6222. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6223. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6224. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6225. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6226. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6227. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6228. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6229. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6230. mode->flags = pipe_config->base.adjusted_mode.flags;
  6231. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6232. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6233. }
  6234. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6235. {
  6236. struct drm_device *dev = intel_crtc->base.dev;
  6237. struct drm_i915_private *dev_priv = dev->dev_private;
  6238. uint32_t pipeconf;
  6239. pipeconf = 0;
  6240. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6241. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6242. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6243. if (intel_crtc->config->double_wide)
  6244. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6245. /* only g4x and later have fancy bpc/dither controls */
  6246. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6247. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6248. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6249. pipeconf |= PIPECONF_DITHER_EN |
  6250. PIPECONF_DITHER_TYPE_SP;
  6251. switch (intel_crtc->config->pipe_bpp) {
  6252. case 18:
  6253. pipeconf |= PIPECONF_6BPC;
  6254. break;
  6255. case 24:
  6256. pipeconf |= PIPECONF_8BPC;
  6257. break;
  6258. case 30:
  6259. pipeconf |= PIPECONF_10BPC;
  6260. break;
  6261. default:
  6262. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6263. BUG();
  6264. }
  6265. }
  6266. if (HAS_PIPE_CXSR(dev)) {
  6267. if (intel_crtc->lowfreq_avail) {
  6268. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6269. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6270. } else {
  6271. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6272. }
  6273. }
  6274. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6275. if (INTEL_INFO(dev)->gen < 4 ||
  6276. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6277. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6278. else
  6279. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6280. } else
  6281. pipeconf |= PIPECONF_PROGRESSIVE;
  6282. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6283. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6284. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6285. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6286. }
  6287. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6288. struct intel_crtc_state *crtc_state)
  6289. {
  6290. struct drm_device *dev = crtc->base.dev;
  6291. struct drm_i915_private *dev_priv = dev->dev_private;
  6292. int refclk, num_connectors = 0;
  6293. intel_clock_t clock, reduced_clock;
  6294. bool ok, has_reduced_clock = false;
  6295. bool is_lvds = false, is_dsi = false;
  6296. struct intel_encoder *encoder;
  6297. const intel_limit_t *limit;
  6298. struct drm_atomic_state *state = crtc_state->base.state;
  6299. struct drm_connector_state *connector_state;
  6300. int i;
  6301. for (i = 0; i < state->num_connector; i++) {
  6302. if (!state->connectors[i])
  6303. continue;
  6304. connector_state = state->connector_states[i];
  6305. if (connector_state->crtc != &crtc->base)
  6306. continue;
  6307. encoder = to_intel_encoder(connector_state->best_encoder);
  6308. switch (encoder->type) {
  6309. case INTEL_OUTPUT_LVDS:
  6310. is_lvds = true;
  6311. break;
  6312. case INTEL_OUTPUT_DSI:
  6313. is_dsi = true;
  6314. break;
  6315. default:
  6316. break;
  6317. }
  6318. num_connectors++;
  6319. }
  6320. if (is_dsi)
  6321. return 0;
  6322. if (!crtc_state->clock_set) {
  6323. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6324. /*
  6325. * Returns a set of divisors for the desired target clock with
  6326. * the given refclk, or FALSE. The returned values represent
  6327. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6328. * 2) / p1 / p2.
  6329. */
  6330. limit = intel_limit(crtc_state, refclk);
  6331. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6332. crtc_state->port_clock,
  6333. refclk, NULL, &clock);
  6334. if (!ok) {
  6335. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6336. return -EINVAL;
  6337. }
  6338. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6339. /*
  6340. * Ensure we match the reduced clock's P to the target
  6341. * clock. If the clocks don't match, we can't switch
  6342. * the display clock by using the FP0/FP1. In such case
  6343. * we will disable the LVDS downclock feature.
  6344. */
  6345. has_reduced_clock =
  6346. dev_priv->display.find_dpll(limit, crtc_state,
  6347. dev_priv->lvds_downclock,
  6348. refclk, &clock,
  6349. &reduced_clock);
  6350. }
  6351. /* Compat-code for transition, will disappear. */
  6352. crtc_state->dpll.n = clock.n;
  6353. crtc_state->dpll.m1 = clock.m1;
  6354. crtc_state->dpll.m2 = clock.m2;
  6355. crtc_state->dpll.p1 = clock.p1;
  6356. crtc_state->dpll.p2 = clock.p2;
  6357. }
  6358. if (IS_GEN2(dev)) {
  6359. i8xx_update_pll(crtc, crtc_state,
  6360. has_reduced_clock ? &reduced_clock : NULL,
  6361. num_connectors);
  6362. } else if (IS_CHERRYVIEW(dev)) {
  6363. chv_update_pll(crtc, crtc_state);
  6364. } else if (IS_VALLEYVIEW(dev)) {
  6365. vlv_update_pll(crtc, crtc_state);
  6366. } else {
  6367. i9xx_update_pll(crtc, crtc_state,
  6368. has_reduced_clock ? &reduced_clock : NULL,
  6369. num_connectors);
  6370. }
  6371. return 0;
  6372. }
  6373. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6374. struct intel_crtc_state *pipe_config)
  6375. {
  6376. struct drm_device *dev = crtc->base.dev;
  6377. struct drm_i915_private *dev_priv = dev->dev_private;
  6378. uint32_t tmp;
  6379. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6380. return;
  6381. tmp = I915_READ(PFIT_CONTROL);
  6382. if (!(tmp & PFIT_ENABLE))
  6383. return;
  6384. /* Check whether the pfit is attached to our pipe. */
  6385. if (INTEL_INFO(dev)->gen < 4) {
  6386. if (crtc->pipe != PIPE_B)
  6387. return;
  6388. } else {
  6389. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6390. return;
  6391. }
  6392. pipe_config->gmch_pfit.control = tmp;
  6393. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6394. if (INTEL_INFO(dev)->gen < 5)
  6395. pipe_config->gmch_pfit.lvds_border_bits =
  6396. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6397. }
  6398. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6399. struct intel_crtc_state *pipe_config)
  6400. {
  6401. struct drm_device *dev = crtc->base.dev;
  6402. struct drm_i915_private *dev_priv = dev->dev_private;
  6403. int pipe = pipe_config->cpu_transcoder;
  6404. intel_clock_t clock;
  6405. u32 mdiv;
  6406. int refclk = 100000;
  6407. /* In case of MIPI DPLL will not even be used */
  6408. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6409. return;
  6410. mutex_lock(&dev_priv->dpio_lock);
  6411. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6412. mutex_unlock(&dev_priv->dpio_lock);
  6413. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6414. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6415. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6416. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6417. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6418. vlv_clock(refclk, &clock);
  6419. /* clock.dot is the fast clock */
  6420. pipe_config->port_clock = clock.dot / 5;
  6421. }
  6422. static void
  6423. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6424. struct intel_initial_plane_config *plane_config)
  6425. {
  6426. struct drm_device *dev = crtc->base.dev;
  6427. struct drm_i915_private *dev_priv = dev->dev_private;
  6428. u32 val, base, offset;
  6429. int pipe = crtc->pipe, plane = crtc->plane;
  6430. int fourcc, pixel_format;
  6431. unsigned int aligned_height;
  6432. struct drm_framebuffer *fb;
  6433. struct intel_framebuffer *intel_fb;
  6434. val = I915_READ(DSPCNTR(plane));
  6435. if (!(val & DISPLAY_PLANE_ENABLE))
  6436. return;
  6437. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6438. if (!intel_fb) {
  6439. DRM_DEBUG_KMS("failed to alloc fb\n");
  6440. return;
  6441. }
  6442. fb = &intel_fb->base;
  6443. if (INTEL_INFO(dev)->gen >= 4) {
  6444. if (val & DISPPLANE_TILED) {
  6445. plane_config->tiling = I915_TILING_X;
  6446. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6447. }
  6448. }
  6449. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6450. fourcc = i9xx_format_to_fourcc(pixel_format);
  6451. fb->pixel_format = fourcc;
  6452. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6453. if (INTEL_INFO(dev)->gen >= 4) {
  6454. if (plane_config->tiling)
  6455. offset = I915_READ(DSPTILEOFF(plane));
  6456. else
  6457. offset = I915_READ(DSPLINOFF(plane));
  6458. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6459. } else {
  6460. base = I915_READ(DSPADDR(plane));
  6461. }
  6462. plane_config->base = base;
  6463. val = I915_READ(PIPESRC(pipe));
  6464. fb->width = ((val >> 16) & 0xfff) + 1;
  6465. fb->height = ((val >> 0) & 0xfff) + 1;
  6466. val = I915_READ(DSPSTRIDE(pipe));
  6467. fb->pitches[0] = val & 0xffffffc0;
  6468. aligned_height = intel_fb_align_height(dev, fb->height,
  6469. fb->pixel_format,
  6470. fb->modifier[0]);
  6471. plane_config->size = fb->pitches[0] * aligned_height;
  6472. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6473. pipe_name(pipe), plane, fb->width, fb->height,
  6474. fb->bits_per_pixel, base, fb->pitches[0],
  6475. plane_config->size);
  6476. plane_config->fb = intel_fb;
  6477. }
  6478. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6479. struct intel_crtc_state *pipe_config)
  6480. {
  6481. struct drm_device *dev = crtc->base.dev;
  6482. struct drm_i915_private *dev_priv = dev->dev_private;
  6483. int pipe = pipe_config->cpu_transcoder;
  6484. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6485. intel_clock_t clock;
  6486. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6487. int refclk = 100000;
  6488. mutex_lock(&dev_priv->dpio_lock);
  6489. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6490. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6491. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6492. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6493. mutex_unlock(&dev_priv->dpio_lock);
  6494. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6495. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6496. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6497. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6498. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6499. chv_clock(refclk, &clock);
  6500. /* clock.dot is the fast clock */
  6501. pipe_config->port_clock = clock.dot / 5;
  6502. }
  6503. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6504. struct intel_crtc_state *pipe_config)
  6505. {
  6506. struct drm_device *dev = crtc->base.dev;
  6507. struct drm_i915_private *dev_priv = dev->dev_private;
  6508. uint32_t tmp;
  6509. if (!intel_display_power_is_enabled(dev_priv,
  6510. POWER_DOMAIN_PIPE(crtc->pipe)))
  6511. return false;
  6512. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6513. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6514. tmp = I915_READ(PIPECONF(crtc->pipe));
  6515. if (!(tmp & PIPECONF_ENABLE))
  6516. return false;
  6517. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6518. switch (tmp & PIPECONF_BPC_MASK) {
  6519. case PIPECONF_6BPC:
  6520. pipe_config->pipe_bpp = 18;
  6521. break;
  6522. case PIPECONF_8BPC:
  6523. pipe_config->pipe_bpp = 24;
  6524. break;
  6525. case PIPECONF_10BPC:
  6526. pipe_config->pipe_bpp = 30;
  6527. break;
  6528. default:
  6529. break;
  6530. }
  6531. }
  6532. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6533. pipe_config->limited_color_range = true;
  6534. if (INTEL_INFO(dev)->gen < 4)
  6535. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6536. intel_get_pipe_timings(crtc, pipe_config);
  6537. i9xx_get_pfit_config(crtc, pipe_config);
  6538. if (INTEL_INFO(dev)->gen >= 4) {
  6539. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6540. pipe_config->pixel_multiplier =
  6541. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6542. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6543. pipe_config->dpll_hw_state.dpll_md = tmp;
  6544. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6545. tmp = I915_READ(DPLL(crtc->pipe));
  6546. pipe_config->pixel_multiplier =
  6547. ((tmp & SDVO_MULTIPLIER_MASK)
  6548. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6549. } else {
  6550. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6551. * port and will be fixed up in the encoder->get_config
  6552. * function. */
  6553. pipe_config->pixel_multiplier = 1;
  6554. }
  6555. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6556. if (!IS_VALLEYVIEW(dev)) {
  6557. /*
  6558. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6559. * on 830. Filter it out here so that we don't
  6560. * report errors due to that.
  6561. */
  6562. if (IS_I830(dev))
  6563. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6564. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6565. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6566. } else {
  6567. /* Mask out read-only status bits. */
  6568. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6569. DPLL_PORTC_READY_MASK |
  6570. DPLL_PORTB_READY_MASK);
  6571. }
  6572. if (IS_CHERRYVIEW(dev))
  6573. chv_crtc_clock_get(crtc, pipe_config);
  6574. else if (IS_VALLEYVIEW(dev))
  6575. vlv_crtc_clock_get(crtc, pipe_config);
  6576. else
  6577. i9xx_crtc_clock_get(crtc, pipe_config);
  6578. return true;
  6579. }
  6580. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6581. {
  6582. struct drm_i915_private *dev_priv = dev->dev_private;
  6583. struct intel_encoder *encoder;
  6584. u32 val, final;
  6585. bool has_lvds = false;
  6586. bool has_cpu_edp = false;
  6587. bool has_panel = false;
  6588. bool has_ck505 = false;
  6589. bool can_ssc = false;
  6590. /* We need to take the global config into account */
  6591. for_each_intel_encoder(dev, encoder) {
  6592. switch (encoder->type) {
  6593. case INTEL_OUTPUT_LVDS:
  6594. has_panel = true;
  6595. has_lvds = true;
  6596. break;
  6597. case INTEL_OUTPUT_EDP:
  6598. has_panel = true;
  6599. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6600. has_cpu_edp = true;
  6601. break;
  6602. default:
  6603. break;
  6604. }
  6605. }
  6606. if (HAS_PCH_IBX(dev)) {
  6607. has_ck505 = dev_priv->vbt.display_clock_mode;
  6608. can_ssc = has_ck505;
  6609. } else {
  6610. has_ck505 = false;
  6611. can_ssc = true;
  6612. }
  6613. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6614. has_panel, has_lvds, has_ck505);
  6615. /* Ironlake: try to setup display ref clock before DPLL
  6616. * enabling. This is only under driver's control after
  6617. * PCH B stepping, previous chipset stepping should be
  6618. * ignoring this setting.
  6619. */
  6620. val = I915_READ(PCH_DREF_CONTROL);
  6621. /* As we must carefully and slowly disable/enable each source in turn,
  6622. * compute the final state we want first and check if we need to
  6623. * make any changes at all.
  6624. */
  6625. final = val;
  6626. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6627. if (has_ck505)
  6628. final |= DREF_NONSPREAD_CK505_ENABLE;
  6629. else
  6630. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6631. final &= ~DREF_SSC_SOURCE_MASK;
  6632. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6633. final &= ~DREF_SSC1_ENABLE;
  6634. if (has_panel) {
  6635. final |= DREF_SSC_SOURCE_ENABLE;
  6636. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6637. final |= DREF_SSC1_ENABLE;
  6638. if (has_cpu_edp) {
  6639. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6640. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6641. else
  6642. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6643. } else
  6644. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6645. } else {
  6646. final |= DREF_SSC_SOURCE_DISABLE;
  6647. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6648. }
  6649. if (final == val)
  6650. return;
  6651. /* Always enable nonspread source */
  6652. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6653. if (has_ck505)
  6654. val |= DREF_NONSPREAD_CK505_ENABLE;
  6655. else
  6656. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6657. if (has_panel) {
  6658. val &= ~DREF_SSC_SOURCE_MASK;
  6659. val |= DREF_SSC_SOURCE_ENABLE;
  6660. /* SSC must be turned on before enabling the CPU output */
  6661. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6662. DRM_DEBUG_KMS("Using SSC on panel\n");
  6663. val |= DREF_SSC1_ENABLE;
  6664. } else
  6665. val &= ~DREF_SSC1_ENABLE;
  6666. /* Get SSC going before enabling the outputs */
  6667. I915_WRITE(PCH_DREF_CONTROL, val);
  6668. POSTING_READ(PCH_DREF_CONTROL);
  6669. udelay(200);
  6670. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6671. /* Enable CPU source on CPU attached eDP */
  6672. if (has_cpu_edp) {
  6673. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6674. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6675. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6676. } else
  6677. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6678. } else
  6679. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6680. I915_WRITE(PCH_DREF_CONTROL, val);
  6681. POSTING_READ(PCH_DREF_CONTROL);
  6682. udelay(200);
  6683. } else {
  6684. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6685. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6686. /* Turn off CPU output */
  6687. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6688. I915_WRITE(PCH_DREF_CONTROL, val);
  6689. POSTING_READ(PCH_DREF_CONTROL);
  6690. udelay(200);
  6691. /* Turn off the SSC source */
  6692. val &= ~DREF_SSC_SOURCE_MASK;
  6693. val |= DREF_SSC_SOURCE_DISABLE;
  6694. /* Turn off SSC1 */
  6695. val &= ~DREF_SSC1_ENABLE;
  6696. I915_WRITE(PCH_DREF_CONTROL, val);
  6697. POSTING_READ(PCH_DREF_CONTROL);
  6698. udelay(200);
  6699. }
  6700. BUG_ON(val != final);
  6701. }
  6702. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6703. {
  6704. uint32_t tmp;
  6705. tmp = I915_READ(SOUTH_CHICKEN2);
  6706. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6707. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6708. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6709. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6710. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6711. tmp = I915_READ(SOUTH_CHICKEN2);
  6712. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6713. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6714. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6715. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6716. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6717. }
  6718. /* WaMPhyProgramming:hsw */
  6719. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6720. {
  6721. uint32_t tmp;
  6722. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6723. tmp &= ~(0xFF << 24);
  6724. tmp |= (0x12 << 24);
  6725. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6726. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6727. tmp |= (1 << 11);
  6728. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6729. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6730. tmp |= (1 << 11);
  6731. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6732. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6733. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6734. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6735. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6736. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6737. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6738. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6739. tmp &= ~(7 << 13);
  6740. tmp |= (5 << 13);
  6741. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6742. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6743. tmp &= ~(7 << 13);
  6744. tmp |= (5 << 13);
  6745. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6746. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6747. tmp &= ~0xFF;
  6748. tmp |= 0x1C;
  6749. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6750. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6751. tmp &= ~0xFF;
  6752. tmp |= 0x1C;
  6753. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6754. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6755. tmp &= ~(0xFF << 16);
  6756. tmp |= (0x1C << 16);
  6757. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6758. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6759. tmp &= ~(0xFF << 16);
  6760. tmp |= (0x1C << 16);
  6761. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6762. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6763. tmp |= (1 << 27);
  6764. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6765. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6766. tmp |= (1 << 27);
  6767. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6768. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6769. tmp &= ~(0xF << 28);
  6770. tmp |= (4 << 28);
  6771. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6772. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6773. tmp &= ~(0xF << 28);
  6774. tmp |= (4 << 28);
  6775. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6776. }
  6777. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6778. * Programming" based on the parameters passed:
  6779. * - Sequence to enable CLKOUT_DP
  6780. * - Sequence to enable CLKOUT_DP without spread
  6781. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6782. */
  6783. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  6784. bool with_fdi)
  6785. {
  6786. struct drm_i915_private *dev_priv = dev->dev_private;
  6787. uint32_t reg, tmp;
  6788. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6789. with_spread = true;
  6790. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  6791. with_fdi, "LP PCH doesn't have FDI\n"))
  6792. with_fdi = false;
  6793. mutex_lock(&dev_priv->dpio_lock);
  6794. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6795. tmp &= ~SBI_SSCCTL_DISABLE;
  6796. tmp |= SBI_SSCCTL_PATHALT;
  6797. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6798. udelay(24);
  6799. if (with_spread) {
  6800. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6801. tmp &= ~SBI_SSCCTL_PATHALT;
  6802. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6803. if (with_fdi) {
  6804. lpt_reset_fdi_mphy(dev_priv);
  6805. lpt_program_fdi_mphy(dev_priv);
  6806. }
  6807. }
  6808. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6809. SBI_GEN0 : SBI_DBUFF0;
  6810. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6811. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6812. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6813. mutex_unlock(&dev_priv->dpio_lock);
  6814. }
  6815. /* Sequence to disable CLKOUT_DP */
  6816. static void lpt_disable_clkout_dp(struct drm_device *dev)
  6817. {
  6818. struct drm_i915_private *dev_priv = dev->dev_private;
  6819. uint32_t reg, tmp;
  6820. mutex_lock(&dev_priv->dpio_lock);
  6821. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6822. SBI_GEN0 : SBI_DBUFF0;
  6823. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6824. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6825. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6826. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6827. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6828. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6829. tmp |= SBI_SSCCTL_PATHALT;
  6830. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6831. udelay(32);
  6832. }
  6833. tmp |= SBI_SSCCTL_DISABLE;
  6834. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6835. }
  6836. mutex_unlock(&dev_priv->dpio_lock);
  6837. }
  6838. static void lpt_init_pch_refclk(struct drm_device *dev)
  6839. {
  6840. struct intel_encoder *encoder;
  6841. bool has_vga = false;
  6842. for_each_intel_encoder(dev, encoder) {
  6843. switch (encoder->type) {
  6844. case INTEL_OUTPUT_ANALOG:
  6845. has_vga = true;
  6846. break;
  6847. default:
  6848. break;
  6849. }
  6850. }
  6851. if (has_vga)
  6852. lpt_enable_clkout_dp(dev, true, true);
  6853. else
  6854. lpt_disable_clkout_dp(dev);
  6855. }
  6856. /*
  6857. * Initialize reference clocks when the driver loads
  6858. */
  6859. void intel_init_pch_refclk(struct drm_device *dev)
  6860. {
  6861. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  6862. ironlake_init_pch_refclk(dev);
  6863. else if (HAS_PCH_LPT(dev))
  6864. lpt_init_pch_refclk(dev);
  6865. }
  6866. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  6867. {
  6868. struct drm_device *dev = crtc_state->base.crtc->dev;
  6869. struct drm_i915_private *dev_priv = dev->dev_private;
  6870. struct drm_atomic_state *state = crtc_state->base.state;
  6871. struct drm_connector_state *connector_state;
  6872. struct intel_encoder *encoder;
  6873. int num_connectors = 0, i;
  6874. bool is_lvds = false;
  6875. for (i = 0; i < state->num_connector; i++) {
  6876. if (!state->connectors[i])
  6877. continue;
  6878. connector_state = state->connector_states[i];
  6879. if (connector_state->crtc != crtc_state->base.crtc)
  6880. continue;
  6881. encoder = to_intel_encoder(connector_state->best_encoder);
  6882. switch (encoder->type) {
  6883. case INTEL_OUTPUT_LVDS:
  6884. is_lvds = true;
  6885. break;
  6886. default:
  6887. break;
  6888. }
  6889. num_connectors++;
  6890. }
  6891. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6892. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6893. dev_priv->vbt.lvds_ssc_freq);
  6894. return dev_priv->vbt.lvds_ssc_freq;
  6895. }
  6896. return 120000;
  6897. }
  6898. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6899. {
  6900. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  6901. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6902. int pipe = intel_crtc->pipe;
  6903. uint32_t val;
  6904. val = 0;
  6905. switch (intel_crtc->config->pipe_bpp) {
  6906. case 18:
  6907. val |= PIPECONF_6BPC;
  6908. break;
  6909. case 24:
  6910. val |= PIPECONF_8BPC;
  6911. break;
  6912. case 30:
  6913. val |= PIPECONF_10BPC;
  6914. break;
  6915. case 36:
  6916. val |= PIPECONF_12BPC;
  6917. break;
  6918. default:
  6919. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6920. BUG();
  6921. }
  6922. if (intel_crtc->config->dither)
  6923. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6924. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6925. val |= PIPECONF_INTERLACED_ILK;
  6926. else
  6927. val |= PIPECONF_PROGRESSIVE;
  6928. if (intel_crtc->config->limited_color_range)
  6929. val |= PIPECONF_COLOR_RANGE_SELECT;
  6930. I915_WRITE(PIPECONF(pipe), val);
  6931. POSTING_READ(PIPECONF(pipe));
  6932. }
  6933. /*
  6934. * Set up the pipe CSC unit.
  6935. *
  6936. * Currently only full range RGB to limited range RGB conversion
  6937. * is supported, but eventually this should handle various
  6938. * RGB<->YCbCr scenarios as well.
  6939. */
  6940. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6941. {
  6942. struct drm_device *dev = crtc->dev;
  6943. struct drm_i915_private *dev_priv = dev->dev_private;
  6944. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6945. int pipe = intel_crtc->pipe;
  6946. uint16_t coeff = 0x7800; /* 1.0 */
  6947. /*
  6948. * TODO: Check what kind of values actually come out of the pipe
  6949. * with these coeff/postoff values and adjust to get the best
  6950. * accuracy. Perhaps we even need to take the bpc value into
  6951. * consideration.
  6952. */
  6953. if (intel_crtc->config->limited_color_range)
  6954. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6955. /*
  6956. * GY/GU and RY/RU should be the other way around according
  6957. * to BSpec, but reality doesn't agree. Just set them up in
  6958. * a way that results in the correct picture.
  6959. */
  6960. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6961. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6962. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6963. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6964. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6965. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6966. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6967. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6968. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6969. if (INTEL_INFO(dev)->gen > 6) {
  6970. uint16_t postoff = 0;
  6971. if (intel_crtc->config->limited_color_range)
  6972. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6973. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6974. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6975. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6976. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6977. } else {
  6978. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6979. if (intel_crtc->config->limited_color_range)
  6980. mode |= CSC_BLACK_SCREEN_OFFSET;
  6981. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6982. }
  6983. }
  6984. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6985. {
  6986. struct drm_device *dev = crtc->dev;
  6987. struct drm_i915_private *dev_priv = dev->dev_private;
  6988. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6989. enum pipe pipe = intel_crtc->pipe;
  6990. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6991. uint32_t val;
  6992. val = 0;
  6993. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6994. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6995. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6996. val |= PIPECONF_INTERLACED_ILK;
  6997. else
  6998. val |= PIPECONF_PROGRESSIVE;
  6999. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7000. POSTING_READ(PIPECONF(cpu_transcoder));
  7001. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7002. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7003. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7004. val = 0;
  7005. switch (intel_crtc->config->pipe_bpp) {
  7006. case 18:
  7007. val |= PIPEMISC_DITHER_6_BPC;
  7008. break;
  7009. case 24:
  7010. val |= PIPEMISC_DITHER_8_BPC;
  7011. break;
  7012. case 30:
  7013. val |= PIPEMISC_DITHER_10_BPC;
  7014. break;
  7015. case 36:
  7016. val |= PIPEMISC_DITHER_12_BPC;
  7017. break;
  7018. default:
  7019. /* Case prevented by pipe_config_set_bpp. */
  7020. BUG();
  7021. }
  7022. if (intel_crtc->config->dither)
  7023. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7024. I915_WRITE(PIPEMISC(pipe), val);
  7025. }
  7026. }
  7027. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7028. struct intel_crtc_state *crtc_state,
  7029. intel_clock_t *clock,
  7030. bool *has_reduced_clock,
  7031. intel_clock_t *reduced_clock)
  7032. {
  7033. struct drm_device *dev = crtc->dev;
  7034. struct drm_i915_private *dev_priv = dev->dev_private;
  7035. int refclk;
  7036. const intel_limit_t *limit;
  7037. bool ret, is_lvds = false;
  7038. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7039. refclk = ironlake_get_refclk(crtc_state);
  7040. /*
  7041. * Returns a set of divisors for the desired target clock with the given
  7042. * refclk, or FALSE. The returned values represent the clock equation:
  7043. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7044. */
  7045. limit = intel_limit(crtc_state, refclk);
  7046. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7047. crtc_state->port_clock,
  7048. refclk, NULL, clock);
  7049. if (!ret)
  7050. return false;
  7051. if (is_lvds && dev_priv->lvds_downclock_avail) {
  7052. /*
  7053. * Ensure we match the reduced clock's P to the target clock.
  7054. * If the clocks don't match, we can't switch the display clock
  7055. * by using the FP0/FP1. In such case we will disable the LVDS
  7056. * downclock feature.
  7057. */
  7058. *has_reduced_clock =
  7059. dev_priv->display.find_dpll(limit, crtc_state,
  7060. dev_priv->lvds_downclock,
  7061. refclk, clock,
  7062. reduced_clock);
  7063. }
  7064. return true;
  7065. }
  7066. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7067. {
  7068. /*
  7069. * Account for spread spectrum to avoid
  7070. * oversubscribing the link. Max center spread
  7071. * is 2.5%; use 5% for safety's sake.
  7072. */
  7073. u32 bps = target_clock * bpp * 21 / 20;
  7074. return DIV_ROUND_UP(bps, link_bw * 8);
  7075. }
  7076. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7077. {
  7078. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7079. }
  7080. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7081. struct intel_crtc_state *crtc_state,
  7082. u32 *fp,
  7083. intel_clock_t *reduced_clock, u32 *fp2)
  7084. {
  7085. struct drm_crtc *crtc = &intel_crtc->base;
  7086. struct drm_device *dev = crtc->dev;
  7087. struct drm_i915_private *dev_priv = dev->dev_private;
  7088. struct drm_atomic_state *state = crtc_state->base.state;
  7089. struct drm_connector_state *connector_state;
  7090. struct intel_encoder *encoder;
  7091. uint32_t dpll;
  7092. int factor, num_connectors = 0, i;
  7093. bool is_lvds = false, is_sdvo = false;
  7094. for (i = 0; i < state->num_connector; i++) {
  7095. if (!state->connectors[i])
  7096. continue;
  7097. connector_state = state->connector_states[i];
  7098. if (connector_state->crtc != crtc_state->base.crtc)
  7099. continue;
  7100. encoder = to_intel_encoder(connector_state->best_encoder);
  7101. switch (encoder->type) {
  7102. case INTEL_OUTPUT_LVDS:
  7103. is_lvds = true;
  7104. break;
  7105. case INTEL_OUTPUT_SDVO:
  7106. case INTEL_OUTPUT_HDMI:
  7107. is_sdvo = true;
  7108. break;
  7109. default:
  7110. break;
  7111. }
  7112. num_connectors++;
  7113. }
  7114. /* Enable autotuning of the PLL clock (if permissible) */
  7115. factor = 21;
  7116. if (is_lvds) {
  7117. if ((intel_panel_use_ssc(dev_priv) &&
  7118. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7119. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7120. factor = 25;
  7121. } else if (crtc_state->sdvo_tv_clock)
  7122. factor = 20;
  7123. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7124. *fp |= FP_CB_TUNE;
  7125. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7126. *fp2 |= FP_CB_TUNE;
  7127. dpll = 0;
  7128. if (is_lvds)
  7129. dpll |= DPLLB_MODE_LVDS;
  7130. else
  7131. dpll |= DPLLB_MODE_DAC_SERIAL;
  7132. dpll |= (crtc_state->pixel_multiplier - 1)
  7133. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7134. if (is_sdvo)
  7135. dpll |= DPLL_SDVO_HIGH_SPEED;
  7136. if (crtc_state->has_dp_encoder)
  7137. dpll |= DPLL_SDVO_HIGH_SPEED;
  7138. /* compute bitmask from p1 value */
  7139. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7140. /* also FPA1 */
  7141. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7142. switch (crtc_state->dpll.p2) {
  7143. case 5:
  7144. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7145. break;
  7146. case 7:
  7147. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7148. break;
  7149. case 10:
  7150. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7151. break;
  7152. case 14:
  7153. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7154. break;
  7155. }
  7156. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7157. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7158. else
  7159. dpll |= PLL_REF_INPUT_DREFCLK;
  7160. return dpll | DPLL_VCO_ENABLE;
  7161. }
  7162. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7163. struct intel_crtc_state *crtc_state)
  7164. {
  7165. struct drm_device *dev = crtc->base.dev;
  7166. intel_clock_t clock, reduced_clock;
  7167. u32 dpll = 0, fp = 0, fp2 = 0;
  7168. bool ok, has_reduced_clock = false;
  7169. bool is_lvds = false;
  7170. struct intel_shared_dpll *pll;
  7171. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7172. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7173. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7174. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7175. &has_reduced_clock, &reduced_clock);
  7176. if (!ok && !crtc_state->clock_set) {
  7177. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7178. return -EINVAL;
  7179. }
  7180. /* Compat-code for transition, will disappear. */
  7181. if (!crtc_state->clock_set) {
  7182. crtc_state->dpll.n = clock.n;
  7183. crtc_state->dpll.m1 = clock.m1;
  7184. crtc_state->dpll.m2 = clock.m2;
  7185. crtc_state->dpll.p1 = clock.p1;
  7186. crtc_state->dpll.p2 = clock.p2;
  7187. }
  7188. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7189. if (crtc_state->has_pch_encoder) {
  7190. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7191. if (has_reduced_clock)
  7192. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7193. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7194. &fp, &reduced_clock,
  7195. has_reduced_clock ? &fp2 : NULL);
  7196. crtc_state->dpll_hw_state.dpll = dpll;
  7197. crtc_state->dpll_hw_state.fp0 = fp;
  7198. if (has_reduced_clock)
  7199. crtc_state->dpll_hw_state.fp1 = fp2;
  7200. else
  7201. crtc_state->dpll_hw_state.fp1 = fp;
  7202. pll = intel_get_shared_dpll(crtc, crtc_state);
  7203. if (pll == NULL) {
  7204. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7205. pipe_name(crtc->pipe));
  7206. return -EINVAL;
  7207. }
  7208. }
  7209. if (is_lvds && has_reduced_clock)
  7210. crtc->lowfreq_avail = true;
  7211. else
  7212. crtc->lowfreq_avail = false;
  7213. return 0;
  7214. }
  7215. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7216. struct intel_link_m_n *m_n)
  7217. {
  7218. struct drm_device *dev = crtc->base.dev;
  7219. struct drm_i915_private *dev_priv = dev->dev_private;
  7220. enum pipe pipe = crtc->pipe;
  7221. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7222. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7223. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7224. & ~TU_SIZE_MASK;
  7225. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7226. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7227. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7228. }
  7229. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7230. enum transcoder transcoder,
  7231. struct intel_link_m_n *m_n,
  7232. struct intel_link_m_n *m2_n2)
  7233. {
  7234. struct drm_device *dev = crtc->base.dev;
  7235. struct drm_i915_private *dev_priv = dev->dev_private;
  7236. enum pipe pipe = crtc->pipe;
  7237. if (INTEL_INFO(dev)->gen >= 5) {
  7238. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7239. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7240. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7241. & ~TU_SIZE_MASK;
  7242. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7243. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7244. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7245. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7246. * gen < 8) and if DRRS is supported (to make sure the
  7247. * registers are not unnecessarily read).
  7248. */
  7249. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7250. crtc->config->has_drrs) {
  7251. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7252. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7253. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7254. & ~TU_SIZE_MASK;
  7255. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7256. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7257. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7258. }
  7259. } else {
  7260. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7261. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7262. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7263. & ~TU_SIZE_MASK;
  7264. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7265. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7266. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7267. }
  7268. }
  7269. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7270. struct intel_crtc_state *pipe_config)
  7271. {
  7272. if (pipe_config->has_pch_encoder)
  7273. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7274. else
  7275. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7276. &pipe_config->dp_m_n,
  7277. &pipe_config->dp_m2_n2);
  7278. }
  7279. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7280. struct intel_crtc_state *pipe_config)
  7281. {
  7282. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7283. &pipe_config->fdi_m_n, NULL);
  7284. }
  7285. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7286. struct intel_crtc_state *pipe_config)
  7287. {
  7288. struct drm_device *dev = crtc->base.dev;
  7289. struct drm_i915_private *dev_priv = dev->dev_private;
  7290. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7291. uint32_t ps_ctrl = 0;
  7292. int id = -1;
  7293. int i;
  7294. /* find scaler attached to this pipe */
  7295. for (i = 0; i < crtc->num_scalers; i++) {
  7296. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7297. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7298. id = i;
  7299. pipe_config->pch_pfit.enabled = true;
  7300. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7301. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7302. break;
  7303. }
  7304. }
  7305. scaler_state->scaler_id = id;
  7306. if (id >= 0) {
  7307. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7308. } else {
  7309. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7310. }
  7311. }
  7312. static void
  7313. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7314. struct intel_initial_plane_config *plane_config)
  7315. {
  7316. struct drm_device *dev = crtc->base.dev;
  7317. struct drm_i915_private *dev_priv = dev->dev_private;
  7318. u32 val, base, offset, stride_mult, tiling;
  7319. int pipe = crtc->pipe;
  7320. int fourcc, pixel_format;
  7321. unsigned int aligned_height;
  7322. struct drm_framebuffer *fb;
  7323. struct intel_framebuffer *intel_fb;
  7324. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7325. if (!intel_fb) {
  7326. DRM_DEBUG_KMS("failed to alloc fb\n");
  7327. return;
  7328. }
  7329. fb = &intel_fb->base;
  7330. val = I915_READ(PLANE_CTL(pipe, 0));
  7331. if (!(val & PLANE_CTL_ENABLE))
  7332. goto error;
  7333. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7334. fourcc = skl_format_to_fourcc(pixel_format,
  7335. val & PLANE_CTL_ORDER_RGBX,
  7336. val & PLANE_CTL_ALPHA_MASK);
  7337. fb->pixel_format = fourcc;
  7338. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7339. tiling = val & PLANE_CTL_TILED_MASK;
  7340. switch (tiling) {
  7341. case PLANE_CTL_TILED_LINEAR:
  7342. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7343. break;
  7344. case PLANE_CTL_TILED_X:
  7345. plane_config->tiling = I915_TILING_X;
  7346. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7347. break;
  7348. case PLANE_CTL_TILED_Y:
  7349. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7350. break;
  7351. case PLANE_CTL_TILED_YF:
  7352. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7353. break;
  7354. default:
  7355. MISSING_CASE(tiling);
  7356. goto error;
  7357. }
  7358. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7359. plane_config->base = base;
  7360. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7361. val = I915_READ(PLANE_SIZE(pipe, 0));
  7362. fb->height = ((val >> 16) & 0xfff) + 1;
  7363. fb->width = ((val >> 0) & 0x1fff) + 1;
  7364. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7365. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7366. fb->pixel_format);
  7367. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7368. aligned_height = intel_fb_align_height(dev, fb->height,
  7369. fb->pixel_format,
  7370. fb->modifier[0]);
  7371. plane_config->size = fb->pitches[0] * aligned_height;
  7372. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7373. pipe_name(pipe), fb->width, fb->height,
  7374. fb->bits_per_pixel, base, fb->pitches[0],
  7375. plane_config->size);
  7376. plane_config->fb = intel_fb;
  7377. return;
  7378. error:
  7379. kfree(fb);
  7380. }
  7381. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7382. struct intel_crtc_state *pipe_config)
  7383. {
  7384. struct drm_device *dev = crtc->base.dev;
  7385. struct drm_i915_private *dev_priv = dev->dev_private;
  7386. uint32_t tmp;
  7387. tmp = I915_READ(PF_CTL(crtc->pipe));
  7388. if (tmp & PF_ENABLE) {
  7389. pipe_config->pch_pfit.enabled = true;
  7390. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7391. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7392. /* We currently do not free assignements of panel fitters on
  7393. * ivb/hsw (since we don't use the higher upscaling modes which
  7394. * differentiates them) so just WARN about this case for now. */
  7395. if (IS_GEN7(dev)) {
  7396. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7397. PF_PIPE_SEL_IVB(crtc->pipe));
  7398. }
  7399. }
  7400. }
  7401. static void
  7402. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7403. struct intel_initial_plane_config *plane_config)
  7404. {
  7405. struct drm_device *dev = crtc->base.dev;
  7406. struct drm_i915_private *dev_priv = dev->dev_private;
  7407. u32 val, base, offset;
  7408. int pipe = crtc->pipe;
  7409. int fourcc, pixel_format;
  7410. unsigned int aligned_height;
  7411. struct drm_framebuffer *fb;
  7412. struct intel_framebuffer *intel_fb;
  7413. val = I915_READ(DSPCNTR(pipe));
  7414. if (!(val & DISPLAY_PLANE_ENABLE))
  7415. return;
  7416. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7417. if (!intel_fb) {
  7418. DRM_DEBUG_KMS("failed to alloc fb\n");
  7419. return;
  7420. }
  7421. fb = &intel_fb->base;
  7422. if (INTEL_INFO(dev)->gen >= 4) {
  7423. if (val & DISPPLANE_TILED) {
  7424. plane_config->tiling = I915_TILING_X;
  7425. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7426. }
  7427. }
  7428. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7429. fourcc = i9xx_format_to_fourcc(pixel_format);
  7430. fb->pixel_format = fourcc;
  7431. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7432. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7433. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7434. offset = I915_READ(DSPOFFSET(pipe));
  7435. } else {
  7436. if (plane_config->tiling)
  7437. offset = I915_READ(DSPTILEOFF(pipe));
  7438. else
  7439. offset = I915_READ(DSPLINOFF(pipe));
  7440. }
  7441. plane_config->base = base;
  7442. val = I915_READ(PIPESRC(pipe));
  7443. fb->width = ((val >> 16) & 0xfff) + 1;
  7444. fb->height = ((val >> 0) & 0xfff) + 1;
  7445. val = I915_READ(DSPSTRIDE(pipe));
  7446. fb->pitches[0] = val & 0xffffffc0;
  7447. aligned_height = intel_fb_align_height(dev, fb->height,
  7448. fb->pixel_format,
  7449. fb->modifier[0]);
  7450. plane_config->size = fb->pitches[0] * aligned_height;
  7451. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7452. pipe_name(pipe), fb->width, fb->height,
  7453. fb->bits_per_pixel, base, fb->pitches[0],
  7454. plane_config->size);
  7455. plane_config->fb = intel_fb;
  7456. }
  7457. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7458. struct intel_crtc_state *pipe_config)
  7459. {
  7460. struct drm_device *dev = crtc->base.dev;
  7461. struct drm_i915_private *dev_priv = dev->dev_private;
  7462. uint32_t tmp;
  7463. if (!intel_display_power_is_enabled(dev_priv,
  7464. POWER_DOMAIN_PIPE(crtc->pipe)))
  7465. return false;
  7466. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7467. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7468. tmp = I915_READ(PIPECONF(crtc->pipe));
  7469. if (!(tmp & PIPECONF_ENABLE))
  7470. return false;
  7471. switch (tmp & PIPECONF_BPC_MASK) {
  7472. case PIPECONF_6BPC:
  7473. pipe_config->pipe_bpp = 18;
  7474. break;
  7475. case PIPECONF_8BPC:
  7476. pipe_config->pipe_bpp = 24;
  7477. break;
  7478. case PIPECONF_10BPC:
  7479. pipe_config->pipe_bpp = 30;
  7480. break;
  7481. case PIPECONF_12BPC:
  7482. pipe_config->pipe_bpp = 36;
  7483. break;
  7484. default:
  7485. break;
  7486. }
  7487. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7488. pipe_config->limited_color_range = true;
  7489. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7490. struct intel_shared_dpll *pll;
  7491. pipe_config->has_pch_encoder = true;
  7492. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7493. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7494. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7495. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7496. if (HAS_PCH_IBX(dev_priv->dev)) {
  7497. pipe_config->shared_dpll =
  7498. (enum intel_dpll_id) crtc->pipe;
  7499. } else {
  7500. tmp = I915_READ(PCH_DPLL_SEL);
  7501. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7502. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7503. else
  7504. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7505. }
  7506. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7507. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7508. &pipe_config->dpll_hw_state));
  7509. tmp = pipe_config->dpll_hw_state.dpll;
  7510. pipe_config->pixel_multiplier =
  7511. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7512. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7513. ironlake_pch_clock_get(crtc, pipe_config);
  7514. } else {
  7515. pipe_config->pixel_multiplier = 1;
  7516. }
  7517. intel_get_pipe_timings(crtc, pipe_config);
  7518. ironlake_get_pfit_config(crtc, pipe_config);
  7519. return true;
  7520. }
  7521. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7522. {
  7523. struct drm_device *dev = dev_priv->dev;
  7524. struct intel_crtc *crtc;
  7525. for_each_intel_crtc(dev, crtc)
  7526. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7527. pipe_name(crtc->pipe));
  7528. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7529. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7530. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7531. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7532. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7533. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7534. "CPU PWM1 enabled\n");
  7535. if (IS_HASWELL(dev))
  7536. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7537. "CPU PWM2 enabled\n");
  7538. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7539. "PCH PWM1 enabled\n");
  7540. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7541. "Utility pin enabled\n");
  7542. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7543. /*
  7544. * In theory we can still leave IRQs enabled, as long as only the HPD
  7545. * interrupts remain enabled. We used to check for that, but since it's
  7546. * gen-specific and since we only disable LCPLL after we fully disable
  7547. * the interrupts, the check below should be enough.
  7548. */
  7549. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7550. }
  7551. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7552. {
  7553. struct drm_device *dev = dev_priv->dev;
  7554. if (IS_HASWELL(dev))
  7555. return I915_READ(D_COMP_HSW);
  7556. else
  7557. return I915_READ(D_COMP_BDW);
  7558. }
  7559. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7560. {
  7561. struct drm_device *dev = dev_priv->dev;
  7562. if (IS_HASWELL(dev)) {
  7563. mutex_lock(&dev_priv->rps.hw_lock);
  7564. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7565. val))
  7566. DRM_ERROR("Failed to write to D_COMP\n");
  7567. mutex_unlock(&dev_priv->rps.hw_lock);
  7568. } else {
  7569. I915_WRITE(D_COMP_BDW, val);
  7570. POSTING_READ(D_COMP_BDW);
  7571. }
  7572. }
  7573. /*
  7574. * This function implements pieces of two sequences from BSpec:
  7575. * - Sequence for display software to disable LCPLL
  7576. * - Sequence for display software to allow package C8+
  7577. * The steps implemented here are just the steps that actually touch the LCPLL
  7578. * register. Callers should take care of disabling all the display engine
  7579. * functions, doing the mode unset, fixing interrupts, etc.
  7580. */
  7581. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7582. bool switch_to_fclk, bool allow_power_down)
  7583. {
  7584. uint32_t val;
  7585. assert_can_disable_lcpll(dev_priv);
  7586. val = I915_READ(LCPLL_CTL);
  7587. if (switch_to_fclk) {
  7588. val |= LCPLL_CD_SOURCE_FCLK;
  7589. I915_WRITE(LCPLL_CTL, val);
  7590. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7591. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7592. DRM_ERROR("Switching to FCLK failed\n");
  7593. val = I915_READ(LCPLL_CTL);
  7594. }
  7595. val |= LCPLL_PLL_DISABLE;
  7596. I915_WRITE(LCPLL_CTL, val);
  7597. POSTING_READ(LCPLL_CTL);
  7598. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7599. DRM_ERROR("LCPLL still locked\n");
  7600. val = hsw_read_dcomp(dev_priv);
  7601. val |= D_COMP_COMP_DISABLE;
  7602. hsw_write_dcomp(dev_priv, val);
  7603. ndelay(100);
  7604. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7605. 1))
  7606. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7607. if (allow_power_down) {
  7608. val = I915_READ(LCPLL_CTL);
  7609. val |= LCPLL_POWER_DOWN_ALLOW;
  7610. I915_WRITE(LCPLL_CTL, val);
  7611. POSTING_READ(LCPLL_CTL);
  7612. }
  7613. }
  7614. /*
  7615. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7616. * source.
  7617. */
  7618. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7619. {
  7620. uint32_t val;
  7621. val = I915_READ(LCPLL_CTL);
  7622. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7623. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7624. return;
  7625. /*
  7626. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7627. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7628. */
  7629. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7630. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7631. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7632. I915_WRITE(LCPLL_CTL, val);
  7633. POSTING_READ(LCPLL_CTL);
  7634. }
  7635. val = hsw_read_dcomp(dev_priv);
  7636. val |= D_COMP_COMP_FORCE;
  7637. val &= ~D_COMP_COMP_DISABLE;
  7638. hsw_write_dcomp(dev_priv, val);
  7639. val = I915_READ(LCPLL_CTL);
  7640. val &= ~LCPLL_PLL_DISABLE;
  7641. I915_WRITE(LCPLL_CTL, val);
  7642. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7643. DRM_ERROR("LCPLL not locked yet\n");
  7644. if (val & LCPLL_CD_SOURCE_FCLK) {
  7645. val = I915_READ(LCPLL_CTL);
  7646. val &= ~LCPLL_CD_SOURCE_FCLK;
  7647. I915_WRITE(LCPLL_CTL, val);
  7648. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7649. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7650. DRM_ERROR("Switching back to LCPLL failed\n");
  7651. }
  7652. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7653. }
  7654. /*
  7655. * Package states C8 and deeper are really deep PC states that can only be
  7656. * reached when all the devices on the system allow it, so even if the graphics
  7657. * device allows PC8+, it doesn't mean the system will actually get to these
  7658. * states. Our driver only allows PC8+ when going into runtime PM.
  7659. *
  7660. * The requirements for PC8+ are that all the outputs are disabled, the power
  7661. * well is disabled and most interrupts are disabled, and these are also
  7662. * requirements for runtime PM. When these conditions are met, we manually do
  7663. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7664. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7665. * hang the machine.
  7666. *
  7667. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7668. * the state of some registers, so when we come back from PC8+ we need to
  7669. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7670. * need to take care of the registers kept by RC6. Notice that this happens even
  7671. * if we don't put the device in PCI D3 state (which is what currently happens
  7672. * because of the runtime PM support).
  7673. *
  7674. * For more, read "Display Sequences for Package C8" on the hardware
  7675. * documentation.
  7676. */
  7677. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7678. {
  7679. struct drm_device *dev = dev_priv->dev;
  7680. uint32_t val;
  7681. DRM_DEBUG_KMS("Enabling package C8+\n");
  7682. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7683. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7684. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7685. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7686. }
  7687. lpt_disable_clkout_dp(dev);
  7688. hsw_disable_lcpll(dev_priv, true, true);
  7689. }
  7690. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7691. {
  7692. struct drm_device *dev = dev_priv->dev;
  7693. uint32_t val;
  7694. DRM_DEBUG_KMS("Disabling package C8+\n");
  7695. hsw_restore_lcpll(dev_priv);
  7696. lpt_init_pch_refclk(dev);
  7697. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7698. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7699. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7700. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7701. }
  7702. intel_prepare_ddi(dev);
  7703. }
  7704. static void broxton_modeset_global_resources(struct drm_atomic_state *state)
  7705. {
  7706. struct drm_device *dev = state->dev;
  7707. struct drm_i915_private *dev_priv = dev->dev_private;
  7708. int max_pixclk = intel_mode_max_pixclk(state);
  7709. int req_cdclk;
  7710. /* see the comment in valleyview_modeset_global_resources */
  7711. if (WARN_ON(max_pixclk < 0))
  7712. return;
  7713. req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  7714. if (req_cdclk != dev_priv->cdclk_freq)
  7715. broxton_set_cdclk(dev, req_cdclk);
  7716. }
  7717. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7718. struct intel_crtc_state *crtc_state)
  7719. {
  7720. if (!intel_ddi_pll_select(crtc, crtc_state))
  7721. return -EINVAL;
  7722. crtc->lowfreq_avail = false;
  7723. return 0;
  7724. }
  7725. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7726. enum port port,
  7727. struct intel_crtc_state *pipe_config)
  7728. {
  7729. switch (port) {
  7730. case PORT_A:
  7731. pipe_config->ddi_pll_sel = SKL_DPLL0;
  7732. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7733. break;
  7734. case PORT_B:
  7735. pipe_config->ddi_pll_sel = SKL_DPLL1;
  7736. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7737. break;
  7738. case PORT_C:
  7739. pipe_config->ddi_pll_sel = SKL_DPLL2;
  7740. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7741. break;
  7742. default:
  7743. DRM_ERROR("Incorrect port type\n");
  7744. }
  7745. }
  7746. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7747. enum port port,
  7748. struct intel_crtc_state *pipe_config)
  7749. {
  7750. u32 temp, dpll_ctl1;
  7751. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7752. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  7753. switch (pipe_config->ddi_pll_sel) {
  7754. case SKL_DPLL0:
  7755. /*
  7756. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  7757. * of the shared DPLL framework and thus needs to be read out
  7758. * separately
  7759. */
  7760. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  7761. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  7762. break;
  7763. case SKL_DPLL1:
  7764. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7765. break;
  7766. case SKL_DPLL2:
  7767. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7768. break;
  7769. case SKL_DPLL3:
  7770. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7771. break;
  7772. }
  7773. }
  7774. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7775. enum port port,
  7776. struct intel_crtc_state *pipe_config)
  7777. {
  7778. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7779. switch (pipe_config->ddi_pll_sel) {
  7780. case PORT_CLK_SEL_WRPLL1:
  7781. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  7782. break;
  7783. case PORT_CLK_SEL_WRPLL2:
  7784. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  7785. break;
  7786. }
  7787. }
  7788. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7789. struct intel_crtc_state *pipe_config)
  7790. {
  7791. struct drm_device *dev = crtc->base.dev;
  7792. struct drm_i915_private *dev_priv = dev->dev_private;
  7793. struct intel_shared_dpll *pll;
  7794. enum port port;
  7795. uint32_t tmp;
  7796. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7797. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7798. if (IS_SKYLAKE(dev))
  7799. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7800. else if (IS_BROXTON(dev))
  7801. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7802. else
  7803. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7804. if (pipe_config->shared_dpll >= 0) {
  7805. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7806. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7807. &pipe_config->dpll_hw_state));
  7808. }
  7809. /*
  7810. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7811. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7812. * the PCH transcoder is on.
  7813. */
  7814. if (INTEL_INFO(dev)->gen < 9 &&
  7815. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7816. pipe_config->has_pch_encoder = true;
  7817. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7818. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7819. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7820. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7821. }
  7822. }
  7823. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7824. struct intel_crtc_state *pipe_config)
  7825. {
  7826. struct drm_device *dev = crtc->base.dev;
  7827. struct drm_i915_private *dev_priv = dev->dev_private;
  7828. enum intel_display_power_domain pfit_domain;
  7829. uint32_t tmp;
  7830. if (!intel_display_power_is_enabled(dev_priv,
  7831. POWER_DOMAIN_PIPE(crtc->pipe)))
  7832. return false;
  7833. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7834. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7835. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7836. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7837. enum pipe trans_edp_pipe;
  7838. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7839. default:
  7840. WARN(1, "unknown pipe linked to edp transcoder\n");
  7841. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7842. case TRANS_DDI_EDP_INPUT_A_ON:
  7843. trans_edp_pipe = PIPE_A;
  7844. break;
  7845. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7846. trans_edp_pipe = PIPE_B;
  7847. break;
  7848. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7849. trans_edp_pipe = PIPE_C;
  7850. break;
  7851. }
  7852. if (trans_edp_pipe == crtc->pipe)
  7853. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7854. }
  7855. if (!intel_display_power_is_enabled(dev_priv,
  7856. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  7857. return false;
  7858. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7859. if (!(tmp & PIPECONF_ENABLE))
  7860. return false;
  7861. haswell_get_ddi_port_state(crtc, pipe_config);
  7862. intel_get_pipe_timings(crtc, pipe_config);
  7863. if (INTEL_INFO(dev)->gen >= 9) {
  7864. skl_init_scalers(dev, crtc, pipe_config);
  7865. }
  7866. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7867. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  7868. if (INTEL_INFO(dev)->gen == 9)
  7869. skylake_get_pfit_config(crtc, pipe_config);
  7870. else if (INTEL_INFO(dev)->gen < 9)
  7871. ironlake_get_pfit_config(crtc, pipe_config);
  7872. else
  7873. MISSING_CASE(INTEL_INFO(dev)->gen);
  7874. } else {
  7875. pipe_config->scaler_state.scaler_id = -1;
  7876. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7877. }
  7878. if (IS_HASWELL(dev))
  7879. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7880. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7881. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  7882. pipe_config->pixel_multiplier =
  7883. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7884. } else {
  7885. pipe_config->pixel_multiplier = 1;
  7886. }
  7887. return true;
  7888. }
  7889. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  7890. {
  7891. struct drm_device *dev = crtc->dev;
  7892. struct drm_i915_private *dev_priv = dev->dev_private;
  7893. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7894. uint32_t cntl = 0, size = 0;
  7895. if (base) {
  7896. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  7897. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  7898. unsigned int stride = roundup_pow_of_two(width) * 4;
  7899. switch (stride) {
  7900. default:
  7901. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7902. width, stride);
  7903. stride = 256;
  7904. /* fallthrough */
  7905. case 256:
  7906. case 512:
  7907. case 1024:
  7908. case 2048:
  7909. break;
  7910. }
  7911. cntl |= CURSOR_ENABLE |
  7912. CURSOR_GAMMA_ENABLE |
  7913. CURSOR_FORMAT_ARGB |
  7914. CURSOR_STRIDE(stride);
  7915. size = (height << 12) | width;
  7916. }
  7917. if (intel_crtc->cursor_cntl != 0 &&
  7918. (intel_crtc->cursor_base != base ||
  7919. intel_crtc->cursor_size != size ||
  7920. intel_crtc->cursor_cntl != cntl)) {
  7921. /* On these chipsets we can only modify the base/size/stride
  7922. * whilst the cursor is disabled.
  7923. */
  7924. I915_WRITE(_CURACNTR, 0);
  7925. POSTING_READ(_CURACNTR);
  7926. intel_crtc->cursor_cntl = 0;
  7927. }
  7928. if (intel_crtc->cursor_base != base) {
  7929. I915_WRITE(_CURABASE, base);
  7930. intel_crtc->cursor_base = base;
  7931. }
  7932. if (intel_crtc->cursor_size != size) {
  7933. I915_WRITE(CURSIZE, size);
  7934. intel_crtc->cursor_size = size;
  7935. }
  7936. if (intel_crtc->cursor_cntl != cntl) {
  7937. I915_WRITE(_CURACNTR, cntl);
  7938. POSTING_READ(_CURACNTR);
  7939. intel_crtc->cursor_cntl = cntl;
  7940. }
  7941. }
  7942. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  7943. {
  7944. struct drm_device *dev = crtc->dev;
  7945. struct drm_i915_private *dev_priv = dev->dev_private;
  7946. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7947. int pipe = intel_crtc->pipe;
  7948. uint32_t cntl;
  7949. cntl = 0;
  7950. if (base) {
  7951. cntl = MCURSOR_GAMMA_ENABLE;
  7952. switch (intel_crtc->base.cursor->state->crtc_w) {
  7953. case 64:
  7954. cntl |= CURSOR_MODE_64_ARGB_AX;
  7955. break;
  7956. case 128:
  7957. cntl |= CURSOR_MODE_128_ARGB_AX;
  7958. break;
  7959. case 256:
  7960. cntl |= CURSOR_MODE_256_ARGB_AX;
  7961. break;
  7962. default:
  7963. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  7964. return;
  7965. }
  7966. cntl |= pipe << 28; /* Connect to correct pipe */
  7967. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  7968. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7969. }
  7970. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  7971. cntl |= CURSOR_ROTATE_180;
  7972. if (intel_crtc->cursor_cntl != cntl) {
  7973. I915_WRITE(CURCNTR(pipe), cntl);
  7974. POSTING_READ(CURCNTR(pipe));
  7975. intel_crtc->cursor_cntl = cntl;
  7976. }
  7977. /* and commit changes on next vblank */
  7978. I915_WRITE(CURBASE(pipe), base);
  7979. POSTING_READ(CURBASE(pipe));
  7980. intel_crtc->cursor_base = base;
  7981. }
  7982. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7983. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7984. bool on)
  7985. {
  7986. struct drm_device *dev = crtc->dev;
  7987. struct drm_i915_private *dev_priv = dev->dev_private;
  7988. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7989. int pipe = intel_crtc->pipe;
  7990. int x = crtc->cursor_x;
  7991. int y = crtc->cursor_y;
  7992. u32 base = 0, pos = 0;
  7993. if (on)
  7994. base = intel_crtc->cursor_addr;
  7995. if (x >= intel_crtc->config->pipe_src_w)
  7996. base = 0;
  7997. if (y >= intel_crtc->config->pipe_src_h)
  7998. base = 0;
  7999. if (x < 0) {
  8000. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8001. base = 0;
  8002. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8003. x = -x;
  8004. }
  8005. pos |= x << CURSOR_X_SHIFT;
  8006. if (y < 0) {
  8007. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8008. base = 0;
  8009. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8010. y = -y;
  8011. }
  8012. pos |= y << CURSOR_Y_SHIFT;
  8013. if (base == 0 && intel_crtc->cursor_base == 0)
  8014. return;
  8015. I915_WRITE(CURPOS(pipe), pos);
  8016. /* ILK+ do this automagically */
  8017. if (HAS_GMCH_DISPLAY(dev) &&
  8018. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8019. base += (intel_crtc->base.cursor->state->crtc_h *
  8020. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8021. }
  8022. if (IS_845G(dev) || IS_I865G(dev))
  8023. i845_update_cursor(crtc, base);
  8024. else
  8025. i9xx_update_cursor(crtc, base);
  8026. }
  8027. static bool cursor_size_ok(struct drm_device *dev,
  8028. uint32_t width, uint32_t height)
  8029. {
  8030. if (width == 0 || height == 0)
  8031. return false;
  8032. /*
  8033. * 845g/865g are special in that they are only limited by
  8034. * the width of their cursors, the height is arbitrary up to
  8035. * the precision of the register. Everything else requires
  8036. * square cursors, limited to a few power-of-two sizes.
  8037. */
  8038. if (IS_845G(dev) || IS_I865G(dev)) {
  8039. if ((width & 63) != 0)
  8040. return false;
  8041. if (width > (IS_845G(dev) ? 64 : 512))
  8042. return false;
  8043. if (height > 1023)
  8044. return false;
  8045. } else {
  8046. switch (width | height) {
  8047. case 256:
  8048. case 128:
  8049. if (IS_GEN2(dev))
  8050. return false;
  8051. case 64:
  8052. break;
  8053. default:
  8054. return false;
  8055. }
  8056. }
  8057. return true;
  8058. }
  8059. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8060. u16 *blue, uint32_t start, uint32_t size)
  8061. {
  8062. int end = (start + size > 256) ? 256 : start + size, i;
  8063. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8064. for (i = start; i < end; i++) {
  8065. intel_crtc->lut_r[i] = red[i] >> 8;
  8066. intel_crtc->lut_g[i] = green[i] >> 8;
  8067. intel_crtc->lut_b[i] = blue[i] >> 8;
  8068. }
  8069. intel_crtc_load_lut(crtc);
  8070. }
  8071. /* VESA 640x480x72Hz mode to set on the pipe */
  8072. static struct drm_display_mode load_detect_mode = {
  8073. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8074. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8075. };
  8076. struct drm_framebuffer *
  8077. __intel_framebuffer_create(struct drm_device *dev,
  8078. struct drm_mode_fb_cmd2 *mode_cmd,
  8079. struct drm_i915_gem_object *obj)
  8080. {
  8081. struct intel_framebuffer *intel_fb;
  8082. int ret;
  8083. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8084. if (!intel_fb) {
  8085. drm_gem_object_unreference(&obj->base);
  8086. return ERR_PTR(-ENOMEM);
  8087. }
  8088. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8089. if (ret)
  8090. goto err;
  8091. return &intel_fb->base;
  8092. err:
  8093. drm_gem_object_unreference(&obj->base);
  8094. kfree(intel_fb);
  8095. return ERR_PTR(ret);
  8096. }
  8097. static struct drm_framebuffer *
  8098. intel_framebuffer_create(struct drm_device *dev,
  8099. struct drm_mode_fb_cmd2 *mode_cmd,
  8100. struct drm_i915_gem_object *obj)
  8101. {
  8102. struct drm_framebuffer *fb;
  8103. int ret;
  8104. ret = i915_mutex_lock_interruptible(dev);
  8105. if (ret)
  8106. return ERR_PTR(ret);
  8107. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8108. mutex_unlock(&dev->struct_mutex);
  8109. return fb;
  8110. }
  8111. static u32
  8112. intel_framebuffer_pitch_for_width(int width, int bpp)
  8113. {
  8114. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8115. return ALIGN(pitch, 64);
  8116. }
  8117. static u32
  8118. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8119. {
  8120. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8121. return PAGE_ALIGN(pitch * mode->vdisplay);
  8122. }
  8123. static struct drm_framebuffer *
  8124. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8125. struct drm_display_mode *mode,
  8126. int depth, int bpp)
  8127. {
  8128. struct drm_i915_gem_object *obj;
  8129. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8130. obj = i915_gem_alloc_object(dev,
  8131. intel_framebuffer_size_for_mode(mode, bpp));
  8132. if (obj == NULL)
  8133. return ERR_PTR(-ENOMEM);
  8134. mode_cmd.width = mode->hdisplay;
  8135. mode_cmd.height = mode->vdisplay;
  8136. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8137. bpp);
  8138. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8139. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8140. }
  8141. static struct drm_framebuffer *
  8142. mode_fits_in_fbdev(struct drm_device *dev,
  8143. struct drm_display_mode *mode)
  8144. {
  8145. #ifdef CONFIG_DRM_I915_FBDEV
  8146. struct drm_i915_private *dev_priv = dev->dev_private;
  8147. struct drm_i915_gem_object *obj;
  8148. struct drm_framebuffer *fb;
  8149. if (!dev_priv->fbdev)
  8150. return NULL;
  8151. if (!dev_priv->fbdev->fb)
  8152. return NULL;
  8153. obj = dev_priv->fbdev->fb->obj;
  8154. BUG_ON(!obj);
  8155. fb = &dev_priv->fbdev->fb->base;
  8156. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8157. fb->bits_per_pixel))
  8158. return NULL;
  8159. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8160. return NULL;
  8161. return fb;
  8162. #else
  8163. return NULL;
  8164. #endif
  8165. }
  8166. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8167. struct drm_display_mode *mode,
  8168. struct intel_load_detect_pipe *old,
  8169. struct drm_modeset_acquire_ctx *ctx)
  8170. {
  8171. struct intel_crtc *intel_crtc;
  8172. struct intel_encoder *intel_encoder =
  8173. intel_attached_encoder(connector);
  8174. struct drm_crtc *possible_crtc;
  8175. struct drm_encoder *encoder = &intel_encoder->base;
  8176. struct drm_crtc *crtc = NULL;
  8177. struct drm_device *dev = encoder->dev;
  8178. struct drm_framebuffer *fb;
  8179. struct drm_mode_config *config = &dev->mode_config;
  8180. struct drm_atomic_state *state = NULL;
  8181. struct drm_connector_state *connector_state;
  8182. int ret, i = -1;
  8183. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8184. connector->base.id, connector->name,
  8185. encoder->base.id, encoder->name);
  8186. retry:
  8187. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8188. if (ret)
  8189. goto fail_unlock;
  8190. /*
  8191. * Algorithm gets a little messy:
  8192. *
  8193. * - if the connector already has an assigned crtc, use it (but make
  8194. * sure it's on first)
  8195. *
  8196. * - try to find the first unused crtc that can drive this connector,
  8197. * and use that if we find one
  8198. */
  8199. /* See if we already have a CRTC for this connector */
  8200. if (encoder->crtc) {
  8201. crtc = encoder->crtc;
  8202. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8203. if (ret)
  8204. goto fail_unlock;
  8205. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8206. if (ret)
  8207. goto fail_unlock;
  8208. old->dpms_mode = connector->dpms;
  8209. old->load_detect_temp = false;
  8210. /* Make sure the crtc and connector are running */
  8211. if (connector->dpms != DRM_MODE_DPMS_ON)
  8212. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8213. return true;
  8214. }
  8215. /* Find an unused one (if possible) */
  8216. for_each_crtc(dev, possible_crtc) {
  8217. i++;
  8218. if (!(encoder->possible_crtcs & (1 << i)))
  8219. continue;
  8220. if (possible_crtc->state->enable)
  8221. continue;
  8222. /* This can occur when applying the pipe A quirk on resume. */
  8223. if (to_intel_crtc(possible_crtc)->new_enabled)
  8224. continue;
  8225. crtc = possible_crtc;
  8226. break;
  8227. }
  8228. /*
  8229. * If we didn't find an unused CRTC, don't use any.
  8230. */
  8231. if (!crtc) {
  8232. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8233. goto fail_unlock;
  8234. }
  8235. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8236. if (ret)
  8237. goto fail_unlock;
  8238. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8239. if (ret)
  8240. goto fail_unlock;
  8241. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8242. to_intel_connector(connector)->new_encoder = intel_encoder;
  8243. intel_crtc = to_intel_crtc(crtc);
  8244. intel_crtc->new_enabled = true;
  8245. old->dpms_mode = connector->dpms;
  8246. old->load_detect_temp = true;
  8247. old->release_fb = NULL;
  8248. state = drm_atomic_state_alloc(dev);
  8249. if (!state)
  8250. return false;
  8251. state->acquire_ctx = ctx;
  8252. connector_state = drm_atomic_get_connector_state(state, connector);
  8253. if (IS_ERR(connector_state)) {
  8254. ret = PTR_ERR(connector_state);
  8255. goto fail;
  8256. }
  8257. connector_state->crtc = crtc;
  8258. connector_state->best_encoder = &intel_encoder->base;
  8259. if (!mode)
  8260. mode = &load_detect_mode;
  8261. /* We need a framebuffer large enough to accommodate all accesses
  8262. * that the plane may generate whilst we perform load detection.
  8263. * We can not rely on the fbcon either being present (we get called
  8264. * during its initialisation to detect all boot displays, or it may
  8265. * not even exist) or that it is large enough to satisfy the
  8266. * requested mode.
  8267. */
  8268. fb = mode_fits_in_fbdev(dev, mode);
  8269. if (fb == NULL) {
  8270. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8271. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8272. old->release_fb = fb;
  8273. } else
  8274. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8275. if (IS_ERR(fb)) {
  8276. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8277. goto fail;
  8278. }
  8279. if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
  8280. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8281. if (old->release_fb)
  8282. old->release_fb->funcs->destroy(old->release_fb);
  8283. goto fail;
  8284. }
  8285. crtc->primary->crtc = crtc;
  8286. /* let the connector get through one full cycle before testing */
  8287. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8288. return true;
  8289. fail:
  8290. intel_crtc->new_enabled = crtc->state->enable;
  8291. fail_unlock:
  8292. if (state) {
  8293. drm_atomic_state_free(state);
  8294. state = NULL;
  8295. }
  8296. if (ret == -EDEADLK) {
  8297. drm_modeset_backoff(ctx);
  8298. goto retry;
  8299. }
  8300. return false;
  8301. }
  8302. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8303. struct intel_load_detect_pipe *old,
  8304. struct drm_modeset_acquire_ctx *ctx)
  8305. {
  8306. struct drm_device *dev = connector->dev;
  8307. struct intel_encoder *intel_encoder =
  8308. intel_attached_encoder(connector);
  8309. struct drm_encoder *encoder = &intel_encoder->base;
  8310. struct drm_crtc *crtc = encoder->crtc;
  8311. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8312. struct drm_atomic_state *state;
  8313. struct drm_connector_state *connector_state;
  8314. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8315. connector->base.id, connector->name,
  8316. encoder->base.id, encoder->name);
  8317. if (old->load_detect_temp) {
  8318. state = drm_atomic_state_alloc(dev);
  8319. if (!state)
  8320. goto fail;
  8321. state->acquire_ctx = ctx;
  8322. connector_state = drm_atomic_get_connector_state(state, connector);
  8323. if (IS_ERR(connector_state))
  8324. goto fail;
  8325. to_intel_connector(connector)->new_encoder = NULL;
  8326. intel_encoder->new_crtc = NULL;
  8327. intel_crtc->new_enabled = false;
  8328. connector_state->best_encoder = NULL;
  8329. connector_state->crtc = NULL;
  8330. intel_set_mode(crtc, NULL, 0, 0, NULL, state);
  8331. drm_atomic_state_free(state);
  8332. if (old->release_fb) {
  8333. drm_framebuffer_unregister_private(old->release_fb);
  8334. drm_framebuffer_unreference(old->release_fb);
  8335. }
  8336. return;
  8337. }
  8338. /* Switch crtc and encoder back off if necessary */
  8339. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8340. connector->funcs->dpms(connector, old->dpms_mode);
  8341. return;
  8342. fail:
  8343. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8344. drm_atomic_state_free(state);
  8345. }
  8346. static int i9xx_pll_refclk(struct drm_device *dev,
  8347. const struct intel_crtc_state *pipe_config)
  8348. {
  8349. struct drm_i915_private *dev_priv = dev->dev_private;
  8350. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8351. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8352. return dev_priv->vbt.lvds_ssc_freq;
  8353. else if (HAS_PCH_SPLIT(dev))
  8354. return 120000;
  8355. else if (!IS_GEN2(dev))
  8356. return 96000;
  8357. else
  8358. return 48000;
  8359. }
  8360. /* Returns the clock of the currently programmed mode of the given pipe. */
  8361. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8362. struct intel_crtc_state *pipe_config)
  8363. {
  8364. struct drm_device *dev = crtc->base.dev;
  8365. struct drm_i915_private *dev_priv = dev->dev_private;
  8366. int pipe = pipe_config->cpu_transcoder;
  8367. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8368. u32 fp;
  8369. intel_clock_t clock;
  8370. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8371. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8372. fp = pipe_config->dpll_hw_state.fp0;
  8373. else
  8374. fp = pipe_config->dpll_hw_state.fp1;
  8375. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8376. if (IS_PINEVIEW(dev)) {
  8377. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8378. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8379. } else {
  8380. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8381. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8382. }
  8383. if (!IS_GEN2(dev)) {
  8384. if (IS_PINEVIEW(dev))
  8385. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8386. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8387. else
  8388. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8389. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8390. switch (dpll & DPLL_MODE_MASK) {
  8391. case DPLLB_MODE_DAC_SERIAL:
  8392. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8393. 5 : 10;
  8394. break;
  8395. case DPLLB_MODE_LVDS:
  8396. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8397. 7 : 14;
  8398. break;
  8399. default:
  8400. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8401. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8402. return;
  8403. }
  8404. if (IS_PINEVIEW(dev))
  8405. pineview_clock(refclk, &clock);
  8406. else
  8407. i9xx_clock(refclk, &clock);
  8408. } else {
  8409. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8410. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8411. if (is_lvds) {
  8412. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8413. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8414. if (lvds & LVDS_CLKB_POWER_UP)
  8415. clock.p2 = 7;
  8416. else
  8417. clock.p2 = 14;
  8418. } else {
  8419. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8420. clock.p1 = 2;
  8421. else {
  8422. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8423. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8424. }
  8425. if (dpll & PLL_P2_DIVIDE_BY_4)
  8426. clock.p2 = 4;
  8427. else
  8428. clock.p2 = 2;
  8429. }
  8430. i9xx_clock(refclk, &clock);
  8431. }
  8432. /*
  8433. * This value includes pixel_multiplier. We will use
  8434. * port_clock to compute adjusted_mode.crtc_clock in the
  8435. * encoder's get_config() function.
  8436. */
  8437. pipe_config->port_clock = clock.dot;
  8438. }
  8439. int intel_dotclock_calculate(int link_freq,
  8440. const struct intel_link_m_n *m_n)
  8441. {
  8442. /*
  8443. * The calculation for the data clock is:
  8444. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8445. * But we want to avoid losing precison if possible, so:
  8446. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8447. *
  8448. * and the link clock is simpler:
  8449. * link_clock = (m * link_clock) / n
  8450. */
  8451. if (!m_n->link_n)
  8452. return 0;
  8453. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8454. }
  8455. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8456. struct intel_crtc_state *pipe_config)
  8457. {
  8458. struct drm_device *dev = crtc->base.dev;
  8459. /* read out port_clock from the DPLL */
  8460. i9xx_crtc_clock_get(crtc, pipe_config);
  8461. /*
  8462. * This value does not include pixel_multiplier.
  8463. * We will check that port_clock and adjusted_mode.crtc_clock
  8464. * agree once we know their relationship in the encoder's
  8465. * get_config() function.
  8466. */
  8467. pipe_config->base.adjusted_mode.crtc_clock =
  8468. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8469. &pipe_config->fdi_m_n);
  8470. }
  8471. /** Returns the currently programmed mode of the given pipe. */
  8472. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8473. struct drm_crtc *crtc)
  8474. {
  8475. struct drm_i915_private *dev_priv = dev->dev_private;
  8476. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8477. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8478. struct drm_display_mode *mode;
  8479. struct intel_crtc_state pipe_config;
  8480. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8481. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8482. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8483. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8484. enum pipe pipe = intel_crtc->pipe;
  8485. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8486. if (!mode)
  8487. return NULL;
  8488. /*
  8489. * Construct a pipe_config sufficient for getting the clock info
  8490. * back out of crtc_clock_get.
  8491. *
  8492. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8493. * to use a real value here instead.
  8494. */
  8495. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8496. pipe_config.pixel_multiplier = 1;
  8497. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8498. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8499. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8500. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8501. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8502. mode->hdisplay = (htot & 0xffff) + 1;
  8503. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8504. mode->hsync_start = (hsync & 0xffff) + 1;
  8505. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8506. mode->vdisplay = (vtot & 0xffff) + 1;
  8507. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8508. mode->vsync_start = (vsync & 0xffff) + 1;
  8509. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8510. drm_mode_set_name(mode);
  8511. return mode;
  8512. }
  8513. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  8514. {
  8515. struct drm_device *dev = crtc->dev;
  8516. struct drm_i915_private *dev_priv = dev->dev_private;
  8517. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8518. if (!HAS_GMCH_DISPLAY(dev))
  8519. return;
  8520. if (!dev_priv->lvds_downclock_avail)
  8521. return;
  8522. /*
  8523. * Since this is called by a timer, we should never get here in
  8524. * the manual case.
  8525. */
  8526. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  8527. int pipe = intel_crtc->pipe;
  8528. int dpll_reg = DPLL(pipe);
  8529. int dpll;
  8530. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  8531. assert_panel_unlocked(dev_priv, pipe);
  8532. dpll = I915_READ(dpll_reg);
  8533. dpll |= DISPLAY_RATE_SELECT_FPA1;
  8534. I915_WRITE(dpll_reg, dpll);
  8535. intel_wait_for_vblank(dev, pipe);
  8536. dpll = I915_READ(dpll_reg);
  8537. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  8538. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  8539. }
  8540. }
  8541. void intel_mark_busy(struct drm_device *dev)
  8542. {
  8543. struct drm_i915_private *dev_priv = dev->dev_private;
  8544. if (dev_priv->mm.busy)
  8545. return;
  8546. intel_runtime_pm_get(dev_priv);
  8547. i915_update_gfx_val(dev_priv);
  8548. if (INTEL_INFO(dev)->gen >= 6)
  8549. gen6_rps_busy(dev_priv);
  8550. dev_priv->mm.busy = true;
  8551. }
  8552. void intel_mark_idle(struct drm_device *dev)
  8553. {
  8554. struct drm_i915_private *dev_priv = dev->dev_private;
  8555. struct drm_crtc *crtc;
  8556. if (!dev_priv->mm.busy)
  8557. return;
  8558. dev_priv->mm.busy = false;
  8559. for_each_crtc(dev, crtc) {
  8560. if (!crtc->primary->fb)
  8561. continue;
  8562. intel_decrease_pllclock(crtc);
  8563. }
  8564. if (INTEL_INFO(dev)->gen >= 6)
  8565. gen6_rps_idle(dev->dev_private);
  8566. intel_runtime_pm_put(dev_priv);
  8567. }
  8568. static void intel_crtc_set_state(struct intel_crtc *crtc,
  8569. struct intel_crtc_state *crtc_state)
  8570. {
  8571. kfree(crtc->config);
  8572. crtc->config = crtc_state;
  8573. crtc->base.state = &crtc_state->base;
  8574. }
  8575. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8576. {
  8577. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8578. struct drm_device *dev = crtc->dev;
  8579. struct intel_unpin_work *work;
  8580. spin_lock_irq(&dev->event_lock);
  8581. work = intel_crtc->unpin_work;
  8582. intel_crtc->unpin_work = NULL;
  8583. spin_unlock_irq(&dev->event_lock);
  8584. if (work) {
  8585. cancel_work_sync(&work->work);
  8586. kfree(work);
  8587. }
  8588. intel_crtc_set_state(intel_crtc, NULL);
  8589. drm_crtc_cleanup(crtc);
  8590. kfree(intel_crtc);
  8591. }
  8592. static void intel_unpin_work_fn(struct work_struct *__work)
  8593. {
  8594. struct intel_unpin_work *work =
  8595. container_of(__work, struct intel_unpin_work, work);
  8596. struct drm_device *dev = work->crtc->dev;
  8597. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  8598. mutex_lock(&dev->struct_mutex);
  8599. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  8600. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8601. intel_fbc_update(dev);
  8602. if (work->flip_queued_req)
  8603. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8604. mutex_unlock(&dev->struct_mutex);
  8605. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8606. drm_framebuffer_unreference(work->old_fb);
  8607. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  8608. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  8609. kfree(work);
  8610. }
  8611. static void do_intel_finish_page_flip(struct drm_device *dev,
  8612. struct drm_crtc *crtc)
  8613. {
  8614. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8615. struct intel_unpin_work *work;
  8616. unsigned long flags;
  8617. /* Ignore early vblank irqs */
  8618. if (intel_crtc == NULL)
  8619. return;
  8620. /*
  8621. * This is called both by irq handlers and the reset code (to complete
  8622. * lost pageflips) so needs the full irqsave spinlocks.
  8623. */
  8624. spin_lock_irqsave(&dev->event_lock, flags);
  8625. work = intel_crtc->unpin_work;
  8626. /* Ensure we don't miss a work->pending update ... */
  8627. smp_rmb();
  8628. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8629. spin_unlock_irqrestore(&dev->event_lock, flags);
  8630. return;
  8631. }
  8632. page_flip_completed(intel_crtc);
  8633. spin_unlock_irqrestore(&dev->event_lock, flags);
  8634. }
  8635. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8636. {
  8637. struct drm_i915_private *dev_priv = dev->dev_private;
  8638. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8639. do_intel_finish_page_flip(dev, crtc);
  8640. }
  8641. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  8642. {
  8643. struct drm_i915_private *dev_priv = dev->dev_private;
  8644. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  8645. do_intel_finish_page_flip(dev, crtc);
  8646. }
  8647. /* Is 'a' after or equal to 'b'? */
  8648. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8649. {
  8650. return !((a - b) & 0x80000000);
  8651. }
  8652. static bool page_flip_finished(struct intel_crtc *crtc)
  8653. {
  8654. struct drm_device *dev = crtc->base.dev;
  8655. struct drm_i915_private *dev_priv = dev->dev_private;
  8656. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  8657. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  8658. return true;
  8659. /*
  8660. * The relevant registers doen't exist on pre-ctg.
  8661. * As the flip done interrupt doesn't trigger for mmio
  8662. * flips on gmch platforms, a flip count check isn't
  8663. * really needed there. But since ctg has the registers,
  8664. * include it in the check anyway.
  8665. */
  8666. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  8667. return true;
  8668. /*
  8669. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8670. * used the same base address. In that case the mmio flip might
  8671. * have completed, but the CS hasn't even executed the flip yet.
  8672. *
  8673. * A flip count check isn't enough as the CS might have updated
  8674. * the base address just after start of vblank, but before we
  8675. * managed to process the interrupt. This means we'd complete the
  8676. * CS flip too soon.
  8677. *
  8678. * Combining both checks should get us a good enough result. It may
  8679. * still happen that the CS flip has been executed, but has not
  8680. * yet actually completed. But in case the base address is the same
  8681. * anyway, we don't really care.
  8682. */
  8683. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8684. crtc->unpin_work->gtt_offset &&
  8685. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  8686. crtc->unpin_work->flip_count);
  8687. }
  8688. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  8689. {
  8690. struct drm_i915_private *dev_priv = dev->dev_private;
  8691. struct intel_crtc *intel_crtc =
  8692. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  8693. unsigned long flags;
  8694. /*
  8695. * This is called both by irq handlers and the reset code (to complete
  8696. * lost pageflips) so needs the full irqsave spinlocks.
  8697. *
  8698. * NB: An MMIO update of the plane base pointer will also
  8699. * generate a page-flip completion irq, i.e. every modeset
  8700. * is also accompanied by a spurious intel_prepare_page_flip().
  8701. */
  8702. spin_lock_irqsave(&dev->event_lock, flags);
  8703. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  8704. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  8705. spin_unlock_irqrestore(&dev->event_lock, flags);
  8706. }
  8707. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  8708. {
  8709. /* Ensure that the work item is consistent when activating it ... */
  8710. smp_wmb();
  8711. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  8712. /* and that it is marked active as soon as the irq could fire. */
  8713. smp_wmb();
  8714. }
  8715. static int intel_gen2_queue_flip(struct drm_device *dev,
  8716. struct drm_crtc *crtc,
  8717. struct drm_framebuffer *fb,
  8718. struct drm_i915_gem_object *obj,
  8719. struct intel_engine_cs *ring,
  8720. uint32_t flags)
  8721. {
  8722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8723. u32 flip_mask;
  8724. int ret;
  8725. ret = intel_ring_begin(ring, 6);
  8726. if (ret)
  8727. return ret;
  8728. /* Can't queue multiple flips, so wait for the previous
  8729. * one to finish before executing the next.
  8730. */
  8731. if (intel_crtc->plane)
  8732. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8733. else
  8734. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8735. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8736. intel_ring_emit(ring, MI_NOOP);
  8737. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8738. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8739. intel_ring_emit(ring, fb->pitches[0]);
  8740. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8741. intel_ring_emit(ring, 0); /* aux display base address, unused */
  8742. intel_mark_page_flip_active(intel_crtc);
  8743. __intel_ring_advance(ring);
  8744. return 0;
  8745. }
  8746. static int intel_gen3_queue_flip(struct drm_device *dev,
  8747. struct drm_crtc *crtc,
  8748. struct drm_framebuffer *fb,
  8749. struct drm_i915_gem_object *obj,
  8750. struct intel_engine_cs *ring,
  8751. uint32_t flags)
  8752. {
  8753. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8754. u32 flip_mask;
  8755. int ret;
  8756. ret = intel_ring_begin(ring, 6);
  8757. if (ret)
  8758. return ret;
  8759. if (intel_crtc->plane)
  8760. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8761. else
  8762. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8763. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8764. intel_ring_emit(ring, MI_NOOP);
  8765. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  8766. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8767. intel_ring_emit(ring, fb->pitches[0]);
  8768. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8769. intel_ring_emit(ring, MI_NOOP);
  8770. intel_mark_page_flip_active(intel_crtc);
  8771. __intel_ring_advance(ring);
  8772. return 0;
  8773. }
  8774. static int intel_gen4_queue_flip(struct drm_device *dev,
  8775. struct drm_crtc *crtc,
  8776. struct drm_framebuffer *fb,
  8777. struct drm_i915_gem_object *obj,
  8778. struct intel_engine_cs *ring,
  8779. uint32_t flags)
  8780. {
  8781. struct drm_i915_private *dev_priv = dev->dev_private;
  8782. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8783. uint32_t pf, pipesrc;
  8784. int ret;
  8785. ret = intel_ring_begin(ring, 4);
  8786. if (ret)
  8787. return ret;
  8788. /* i965+ uses the linear or tiled offsets from the
  8789. * Display Registers (which do not change across a page-flip)
  8790. * so we need only reprogram the base address.
  8791. */
  8792. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8793. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8794. intel_ring_emit(ring, fb->pitches[0]);
  8795. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  8796. obj->tiling_mode);
  8797. /* XXX Enabling the panel-fitter across page-flip is so far
  8798. * untested on non-native modes, so ignore it for now.
  8799. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8800. */
  8801. pf = 0;
  8802. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8803. intel_ring_emit(ring, pf | pipesrc);
  8804. intel_mark_page_flip_active(intel_crtc);
  8805. __intel_ring_advance(ring);
  8806. return 0;
  8807. }
  8808. static int intel_gen6_queue_flip(struct drm_device *dev,
  8809. struct drm_crtc *crtc,
  8810. struct drm_framebuffer *fb,
  8811. struct drm_i915_gem_object *obj,
  8812. struct intel_engine_cs *ring,
  8813. uint32_t flags)
  8814. {
  8815. struct drm_i915_private *dev_priv = dev->dev_private;
  8816. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8817. uint32_t pf, pipesrc;
  8818. int ret;
  8819. ret = intel_ring_begin(ring, 4);
  8820. if (ret)
  8821. return ret;
  8822. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8823. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8824. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  8825. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8826. /* Contrary to the suggestions in the documentation,
  8827. * "Enable Panel Fitter" does not seem to be required when page
  8828. * flipping with a non-native mode, and worse causes a normal
  8829. * modeset to fail.
  8830. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8831. */
  8832. pf = 0;
  8833. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8834. intel_ring_emit(ring, pf | pipesrc);
  8835. intel_mark_page_flip_active(intel_crtc);
  8836. __intel_ring_advance(ring);
  8837. return 0;
  8838. }
  8839. static int intel_gen7_queue_flip(struct drm_device *dev,
  8840. struct drm_crtc *crtc,
  8841. struct drm_framebuffer *fb,
  8842. struct drm_i915_gem_object *obj,
  8843. struct intel_engine_cs *ring,
  8844. uint32_t flags)
  8845. {
  8846. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8847. uint32_t plane_bit = 0;
  8848. int len, ret;
  8849. switch (intel_crtc->plane) {
  8850. case PLANE_A:
  8851. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8852. break;
  8853. case PLANE_B:
  8854. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8855. break;
  8856. case PLANE_C:
  8857. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8858. break;
  8859. default:
  8860. WARN_ONCE(1, "unknown plane in flip command\n");
  8861. return -ENODEV;
  8862. }
  8863. len = 4;
  8864. if (ring->id == RCS) {
  8865. len += 6;
  8866. /*
  8867. * On Gen 8, SRM is now taking an extra dword to accommodate
  8868. * 48bits addresses, and we need a NOOP for the batch size to
  8869. * stay even.
  8870. */
  8871. if (IS_GEN8(dev))
  8872. len += 2;
  8873. }
  8874. /*
  8875. * BSpec MI_DISPLAY_FLIP for IVB:
  8876. * "The full packet must be contained within the same cache line."
  8877. *
  8878. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8879. * cacheline, if we ever start emitting more commands before
  8880. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8881. * then do the cacheline alignment, and finally emit the
  8882. * MI_DISPLAY_FLIP.
  8883. */
  8884. ret = intel_ring_cacheline_align(ring);
  8885. if (ret)
  8886. return ret;
  8887. ret = intel_ring_begin(ring, len);
  8888. if (ret)
  8889. return ret;
  8890. /* Unmask the flip-done completion message. Note that the bspec says that
  8891. * we should do this for both the BCS and RCS, and that we must not unmask
  8892. * more than one flip event at any time (or ensure that one flip message
  8893. * can be sent by waiting for flip-done prior to queueing new flips).
  8894. * Experimentation says that BCS works despite DERRMR masking all
  8895. * flip-done completion events and that unmasking all planes at once
  8896. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8897. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8898. */
  8899. if (ring->id == RCS) {
  8900. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8901. intel_ring_emit(ring, DERRMR);
  8902. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8903. DERRMR_PIPEB_PRI_FLIP_DONE |
  8904. DERRMR_PIPEC_PRI_FLIP_DONE));
  8905. if (IS_GEN8(dev))
  8906. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8907. MI_SRM_LRM_GLOBAL_GTT);
  8908. else
  8909. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8910. MI_SRM_LRM_GLOBAL_GTT);
  8911. intel_ring_emit(ring, DERRMR);
  8912. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8913. if (IS_GEN8(dev)) {
  8914. intel_ring_emit(ring, 0);
  8915. intel_ring_emit(ring, MI_NOOP);
  8916. }
  8917. }
  8918. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8919. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8920. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8921. intel_ring_emit(ring, (MI_NOOP));
  8922. intel_mark_page_flip_active(intel_crtc);
  8923. __intel_ring_advance(ring);
  8924. return 0;
  8925. }
  8926. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8927. struct drm_i915_gem_object *obj)
  8928. {
  8929. /*
  8930. * This is not being used for older platforms, because
  8931. * non-availability of flip done interrupt forces us to use
  8932. * CS flips. Older platforms derive flip done using some clever
  8933. * tricks involving the flip_pending status bits and vblank irqs.
  8934. * So using MMIO flips there would disrupt this mechanism.
  8935. */
  8936. if (ring == NULL)
  8937. return true;
  8938. if (INTEL_INFO(ring->dev)->gen < 5)
  8939. return false;
  8940. if (i915.use_mmio_flip < 0)
  8941. return false;
  8942. else if (i915.use_mmio_flip > 0)
  8943. return true;
  8944. else if (i915.enable_execlists)
  8945. return true;
  8946. else
  8947. return ring != i915_gem_request_get_ring(obj->last_read_req);
  8948. }
  8949. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  8950. {
  8951. struct drm_device *dev = intel_crtc->base.dev;
  8952. struct drm_i915_private *dev_priv = dev->dev_private;
  8953. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8954. const enum pipe pipe = intel_crtc->pipe;
  8955. u32 ctl, stride;
  8956. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8957. ctl &= ~PLANE_CTL_TILED_MASK;
  8958. switch (fb->modifier[0]) {
  8959. case DRM_FORMAT_MOD_NONE:
  8960. break;
  8961. case I915_FORMAT_MOD_X_TILED:
  8962. ctl |= PLANE_CTL_TILED_X;
  8963. break;
  8964. case I915_FORMAT_MOD_Y_TILED:
  8965. ctl |= PLANE_CTL_TILED_Y;
  8966. break;
  8967. case I915_FORMAT_MOD_Yf_TILED:
  8968. ctl |= PLANE_CTL_TILED_YF;
  8969. break;
  8970. default:
  8971. MISSING_CASE(fb->modifier[0]);
  8972. }
  8973. /*
  8974. * The stride is either expressed as a multiple of 64 bytes chunks for
  8975. * linear buffers or in number of tiles for tiled buffers.
  8976. */
  8977. stride = fb->pitches[0] /
  8978. intel_fb_stride_alignment(dev, fb->modifier[0],
  8979. fb->pixel_format);
  8980. /*
  8981. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8982. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8983. */
  8984. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8985. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8986. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  8987. POSTING_READ(PLANE_SURF(pipe, 0));
  8988. }
  8989. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  8990. {
  8991. struct drm_device *dev = intel_crtc->base.dev;
  8992. struct drm_i915_private *dev_priv = dev->dev_private;
  8993. struct intel_framebuffer *intel_fb =
  8994. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8995. struct drm_i915_gem_object *obj = intel_fb->obj;
  8996. u32 dspcntr;
  8997. u32 reg;
  8998. reg = DSPCNTR(intel_crtc->plane);
  8999. dspcntr = I915_READ(reg);
  9000. if (obj->tiling_mode != I915_TILING_NONE)
  9001. dspcntr |= DISPPLANE_TILED;
  9002. else
  9003. dspcntr &= ~DISPPLANE_TILED;
  9004. I915_WRITE(reg, dspcntr);
  9005. I915_WRITE(DSPSURF(intel_crtc->plane),
  9006. intel_crtc->unpin_work->gtt_offset);
  9007. POSTING_READ(DSPSURF(intel_crtc->plane));
  9008. }
  9009. /*
  9010. * XXX: This is the temporary way to update the plane registers until we get
  9011. * around to using the usual plane update functions for MMIO flips
  9012. */
  9013. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9014. {
  9015. struct drm_device *dev = intel_crtc->base.dev;
  9016. bool atomic_update;
  9017. u32 start_vbl_count;
  9018. intel_mark_page_flip_active(intel_crtc);
  9019. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9020. if (INTEL_INFO(dev)->gen >= 9)
  9021. skl_do_mmio_flip(intel_crtc);
  9022. else
  9023. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9024. ilk_do_mmio_flip(intel_crtc);
  9025. if (atomic_update)
  9026. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9027. }
  9028. static void intel_mmio_flip_work_func(struct work_struct *work)
  9029. {
  9030. struct intel_crtc *crtc =
  9031. container_of(work, struct intel_crtc, mmio_flip.work);
  9032. struct intel_mmio_flip *mmio_flip;
  9033. mmio_flip = &crtc->mmio_flip;
  9034. if (mmio_flip->req)
  9035. WARN_ON(__i915_wait_request(mmio_flip->req,
  9036. crtc->reset_counter,
  9037. false, NULL, NULL) != 0);
  9038. intel_do_mmio_flip(crtc);
  9039. if (mmio_flip->req) {
  9040. mutex_lock(&crtc->base.dev->struct_mutex);
  9041. i915_gem_request_assign(&mmio_flip->req, NULL);
  9042. mutex_unlock(&crtc->base.dev->struct_mutex);
  9043. }
  9044. }
  9045. static int intel_queue_mmio_flip(struct drm_device *dev,
  9046. struct drm_crtc *crtc,
  9047. struct drm_framebuffer *fb,
  9048. struct drm_i915_gem_object *obj,
  9049. struct intel_engine_cs *ring,
  9050. uint32_t flags)
  9051. {
  9052. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9053. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  9054. obj->last_write_req);
  9055. schedule_work(&intel_crtc->mmio_flip.work);
  9056. return 0;
  9057. }
  9058. static int intel_default_queue_flip(struct drm_device *dev,
  9059. struct drm_crtc *crtc,
  9060. struct drm_framebuffer *fb,
  9061. struct drm_i915_gem_object *obj,
  9062. struct intel_engine_cs *ring,
  9063. uint32_t flags)
  9064. {
  9065. return -ENODEV;
  9066. }
  9067. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9068. struct drm_crtc *crtc)
  9069. {
  9070. struct drm_i915_private *dev_priv = dev->dev_private;
  9071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9072. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9073. u32 addr;
  9074. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9075. return true;
  9076. if (!work->enable_stall_check)
  9077. return false;
  9078. if (work->flip_ready_vblank == 0) {
  9079. if (work->flip_queued_req &&
  9080. !i915_gem_request_completed(work->flip_queued_req, true))
  9081. return false;
  9082. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9083. }
  9084. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9085. return false;
  9086. /* Potential stall - if we see that the flip has happened,
  9087. * assume a missed interrupt. */
  9088. if (INTEL_INFO(dev)->gen >= 4)
  9089. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9090. else
  9091. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9092. /* There is a potential issue here with a false positive after a flip
  9093. * to the same address. We could address this by checking for a
  9094. * non-incrementing frame counter.
  9095. */
  9096. return addr == work->gtt_offset;
  9097. }
  9098. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9099. {
  9100. struct drm_i915_private *dev_priv = dev->dev_private;
  9101. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9102. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9103. struct intel_unpin_work *work;
  9104. WARN_ON(!in_interrupt());
  9105. if (crtc == NULL)
  9106. return;
  9107. spin_lock(&dev->event_lock);
  9108. work = intel_crtc->unpin_work;
  9109. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9110. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9111. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9112. page_flip_completed(intel_crtc);
  9113. work = NULL;
  9114. }
  9115. if (work != NULL &&
  9116. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9117. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9118. spin_unlock(&dev->event_lock);
  9119. }
  9120. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9121. struct drm_framebuffer *fb,
  9122. struct drm_pending_vblank_event *event,
  9123. uint32_t page_flip_flags)
  9124. {
  9125. struct drm_device *dev = crtc->dev;
  9126. struct drm_i915_private *dev_priv = dev->dev_private;
  9127. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9128. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9129. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9130. struct drm_plane *primary = crtc->primary;
  9131. enum pipe pipe = intel_crtc->pipe;
  9132. struct intel_unpin_work *work;
  9133. struct intel_engine_cs *ring;
  9134. bool mmio_flip;
  9135. int ret;
  9136. /*
  9137. * drm_mode_page_flip_ioctl() should already catch this, but double
  9138. * check to be safe. In the future we may enable pageflipping from
  9139. * a disabled primary plane.
  9140. */
  9141. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9142. return -EBUSY;
  9143. /* Can't change pixel format via MI display flips. */
  9144. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9145. return -EINVAL;
  9146. /*
  9147. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9148. * Note that pitch changes could also affect these register.
  9149. */
  9150. if (INTEL_INFO(dev)->gen > 3 &&
  9151. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9152. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9153. return -EINVAL;
  9154. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9155. goto out_hang;
  9156. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9157. if (work == NULL)
  9158. return -ENOMEM;
  9159. work->event = event;
  9160. work->crtc = crtc;
  9161. work->old_fb = old_fb;
  9162. INIT_WORK(&work->work, intel_unpin_work_fn);
  9163. ret = drm_crtc_vblank_get(crtc);
  9164. if (ret)
  9165. goto free_work;
  9166. /* We borrow the event spin lock for protecting unpin_work */
  9167. spin_lock_irq(&dev->event_lock);
  9168. if (intel_crtc->unpin_work) {
  9169. /* Before declaring the flip queue wedged, check if
  9170. * the hardware completed the operation behind our backs.
  9171. */
  9172. if (__intel_pageflip_stall_check(dev, crtc)) {
  9173. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9174. page_flip_completed(intel_crtc);
  9175. } else {
  9176. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9177. spin_unlock_irq(&dev->event_lock);
  9178. drm_crtc_vblank_put(crtc);
  9179. kfree(work);
  9180. return -EBUSY;
  9181. }
  9182. }
  9183. intel_crtc->unpin_work = work;
  9184. spin_unlock_irq(&dev->event_lock);
  9185. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9186. flush_workqueue(dev_priv->wq);
  9187. /* Reference the objects for the scheduled work. */
  9188. drm_framebuffer_reference(work->old_fb);
  9189. drm_gem_object_reference(&obj->base);
  9190. crtc->primary->fb = fb;
  9191. update_state_fb(crtc->primary);
  9192. work->pending_flip_obj = obj;
  9193. ret = i915_mutex_lock_interruptible(dev);
  9194. if (ret)
  9195. goto cleanup;
  9196. atomic_inc(&intel_crtc->unpin_work_count);
  9197. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9198. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9199. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9200. if (IS_VALLEYVIEW(dev)) {
  9201. ring = &dev_priv->ring[BCS];
  9202. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9203. /* vlv: DISPLAY_FLIP fails to change tiling */
  9204. ring = NULL;
  9205. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9206. ring = &dev_priv->ring[BCS];
  9207. } else if (INTEL_INFO(dev)->gen >= 7) {
  9208. ring = i915_gem_request_get_ring(obj->last_read_req);
  9209. if (ring == NULL || ring->id != RCS)
  9210. ring = &dev_priv->ring[BCS];
  9211. } else {
  9212. ring = &dev_priv->ring[RCS];
  9213. }
  9214. mmio_flip = use_mmio_flip(ring, obj);
  9215. /* When using CS flips, we want to emit semaphores between rings.
  9216. * However, when using mmio flips we will create a task to do the
  9217. * synchronisation, so all we want here is to pin the framebuffer
  9218. * into the display plane and skip any waits.
  9219. */
  9220. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9221. crtc->primary->state,
  9222. mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
  9223. if (ret)
  9224. goto cleanup_pending;
  9225. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9226. + intel_crtc->dspaddr_offset;
  9227. if (mmio_flip) {
  9228. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9229. page_flip_flags);
  9230. if (ret)
  9231. goto cleanup_unpin;
  9232. i915_gem_request_assign(&work->flip_queued_req,
  9233. obj->last_write_req);
  9234. } else {
  9235. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  9236. page_flip_flags);
  9237. if (ret)
  9238. goto cleanup_unpin;
  9239. i915_gem_request_assign(&work->flip_queued_req,
  9240. intel_ring_get_request(ring));
  9241. }
  9242. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9243. work->enable_stall_check = true;
  9244. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9245. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9246. intel_fbc_disable(dev);
  9247. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9248. mutex_unlock(&dev->struct_mutex);
  9249. trace_i915_flip_request(intel_crtc->plane, obj);
  9250. return 0;
  9251. cleanup_unpin:
  9252. intel_unpin_fb_obj(fb, crtc->primary->state);
  9253. cleanup_pending:
  9254. atomic_dec(&intel_crtc->unpin_work_count);
  9255. mutex_unlock(&dev->struct_mutex);
  9256. cleanup:
  9257. crtc->primary->fb = old_fb;
  9258. update_state_fb(crtc->primary);
  9259. drm_gem_object_unreference_unlocked(&obj->base);
  9260. drm_framebuffer_unreference(work->old_fb);
  9261. spin_lock_irq(&dev->event_lock);
  9262. intel_crtc->unpin_work = NULL;
  9263. spin_unlock_irq(&dev->event_lock);
  9264. drm_crtc_vblank_put(crtc);
  9265. free_work:
  9266. kfree(work);
  9267. if (ret == -EIO) {
  9268. out_hang:
  9269. ret = intel_plane_restore(primary);
  9270. if (ret == 0 && event) {
  9271. spin_lock_irq(&dev->event_lock);
  9272. drm_send_vblank_event(dev, pipe, event);
  9273. spin_unlock_irq(&dev->event_lock);
  9274. }
  9275. }
  9276. return ret;
  9277. }
  9278. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9279. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9280. .load_lut = intel_crtc_load_lut,
  9281. .atomic_begin = intel_begin_crtc_commit,
  9282. .atomic_flush = intel_finish_crtc_commit,
  9283. };
  9284. /**
  9285. * intel_modeset_update_staged_output_state
  9286. *
  9287. * Updates the staged output configuration state, e.g. after we've read out the
  9288. * current hw state.
  9289. */
  9290. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  9291. {
  9292. struct intel_crtc *crtc;
  9293. struct intel_encoder *encoder;
  9294. struct intel_connector *connector;
  9295. for_each_intel_connector(dev, connector) {
  9296. connector->new_encoder =
  9297. to_intel_encoder(connector->base.encoder);
  9298. }
  9299. for_each_intel_encoder(dev, encoder) {
  9300. encoder->new_crtc =
  9301. to_intel_crtc(encoder->base.crtc);
  9302. }
  9303. for_each_intel_crtc(dev, crtc) {
  9304. crtc->new_enabled = crtc->base.state->enable;
  9305. }
  9306. }
  9307. /* Transitional helper to copy current connector/encoder state to
  9308. * connector->state. This is needed so that code that is partially
  9309. * converted to atomic does the right thing.
  9310. */
  9311. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9312. {
  9313. struct intel_connector *connector;
  9314. for_each_intel_connector(dev, connector) {
  9315. if (connector->base.encoder) {
  9316. connector->base.state->best_encoder =
  9317. connector->base.encoder;
  9318. connector->base.state->crtc =
  9319. connector->base.encoder->crtc;
  9320. } else {
  9321. connector->base.state->best_encoder = NULL;
  9322. connector->base.state->crtc = NULL;
  9323. }
  9324. }
  9325. }
  9326. /**
  9327. * intel_modeset_commit_output_state
  9328. *
  9329. * This function copies the stage display pipe configuration to the real one.
  9330. */
  9331. static void intel_modeset_commit_output_state(struct drm_device *dev)
  9332. {
  9333. struct intel_crtc *crtc;
  9334. struct intel_encoder *encoder;
  9335. struct intel_connector *connector;
  9336. for_each_intel_connector(dev, connector) {
  9337. connector->base.encoder = &connector->new_encoder->base;
  9338. }
  9339. for_each_intel_encoder(dev, encoder) {
  9340. encoder->base.crtc = &encoder->new_crtc->base;
  9341. }
  9342. for_each_intel_crtc(dev, crtc) {
  9343. crtc->base.state->enable = crtc->new_enabled;
  9344. crtc->base.enabled = crtc->new_enabled;
  9345. }
  9346. intel_modeset_update_connector_atomic_state(dev);
  9347. }
  9348. static void
  9349. connected_sink_compute_bpp(struct intel_connector *connector,
  9350. struct intel_crtc_state *pipe_config)
  9351. {
  9352. int bpp = pipe_config->pipe_bpp;
  9353. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9354. connector->base.base.id,
  9355. connector->base.name);
  9356. /* Don't use an invalid EDID bpc value */
  9357. if (connector->base.display_info.bpc &&
  9358. connector->base.display_info.bpc * 3 < bpp) {
  9359. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9360. bpp, connector->base.display_info.bpc*3);
  9361. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9362. }
  9363. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9364. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9365. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9366. bpp);
  9367. pipe_config->pipe_bpp = 24;
  9368. }
  9369. }
  9370. static int
  9371. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9372. struct intel_crtc_state *pipe_config)
  9373. {
  9374. struct drm_device *dev = crtc->base.dev;
  9375. struct drm_atomic_state *state;
  9376. struct intel_connector *connector;
  9377. int bpp, i;
  9378. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9379. bpp = 10*3;
  9380. else if (INTEL_INFO(dev)->gen >= 5)
  9381. bpp = 12*3;
  9382. else
  9383. bpp = 8*3;
  9384. pipe_config->pipe_bpp = bpp;
  9385. state = pipe_config->base.state;
  9386. /* Clamp display bpp to EDID value */
  9387. for (i = 0; i < state->num_connector; i++) {
  9388. if (!state->connectors[i])
  9389. continue;
  9390. connector = to_intel_connector(state->connectors[i]);
  9391. if (state->connector_states[i]->crtc != &crtc->base)
  9392. continue;
  9393. connected_sink_compute_bpp(connector, pipe_config);
  9394. }
  9395. return bpp;
  9396. }
  9397. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9398. {
  9399. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9400. "type: 0x%x flags: 0x%x\n",
  9401. mode->crtc_clock,
  9402. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9403. mode->crtc_hsync_end, mode->crtc_htotal,
  9404. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9405. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9406. }
  9407. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9408. struct intel_crtc_state *pipe_config,
  9409. const char *context)
  9410. {
  9411. struct drm_device *dev = crtc->base.dev;
  9412. struct drm_plane *plane;
  9413. struct intel_plane *intel_plane;
  9414. struct intel_plane_state *state;
  9415. struct drm_framebuffer *fb;
  9416. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9417. context, pipe_config, pipe_name(crtc->pipe));
  9418. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9419. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9420. pipe_config->pipe_bpp, pipe_config->dither);
  9421. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9422. pipe_config->has_pch_encoder,
  9423. pipe_config->fdi_lanes,
  9424. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9425. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9426. pipe_config->fdi_m_n.tu);
  9427. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9428. pipe_config->has_dp_encoder,
  9429. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9430. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9431. pipe_config->dp_m_n.tu);
  9432. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9433. pipe_config->has_dp_encoder,
  9434. pipe_config->dp_m2_n2.gmch_m,
  9435. pipe_config->dp_m2_n2.gmch_n,
  9436. pipe_config->dp_m2_n2.link_m,
  9437. pipe_config->dp_m2_n2.link_n,
  9438. pipe_config->dp_m2_n2.tu);
  9439. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9440. pipe_config->has_audio,
  9441. pipe_config->has_infoframe);
  9442. DRM_DEBUG_KMS("requested mode:\n");
  9443. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9444. DRM_DEBUG_KMS("adjusted mode:\n");
  9445. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9446. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9447. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9448. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9449. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9450. DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
  9451. DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
  9452. DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
  9453. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9454. pipe_config->gmch_pfit.control,
  9455. pipe_config->gmch_pfit.pgm_ratios,
  9456. pipe_config->gmch_pfit.lvds_border_bits);
  9457. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9458. pipe_config->pch_pfit.pos,
  9459. pipe_config->pch_pfit.size,
  9460. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  9461. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  9462. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  9463. DRM_DEBUG_KMS("planes on this crtc\n");
  9464. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9465. intel_plane = to_intel_plane(plane);
  9466. if (intel_plane->pipe != crtc->pipe)
  9467. continue;
  9468. state = to_intel_plane_state(plane->state);
  9469. fb = state->base.fb;
  9470. if (!fb) {
  9471. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  9472. "disabled, scaler_id = %d\n",
  9473. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9474. plane->base.id, intel_plane->pipe,
  9475. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  9476. drm_plane_index(plane), state->scaler_id);
  9477. continue;
  9478. }
  9479. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  9480. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9481. plane->base.id, intel_plane->pipe,
  9482. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  9483. drm_plane_index(plane));
  9484. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  9485. fb->base.id, fb->width, fb->height, fb->pixel_format);
  9486. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  9487. state->scaler_id,
  9488. state->src.x1 >> 16, state->src.y1 >> 16,
  9489. drm_rect_width(&state->src) >> 16,
  9490. drm_rect_height(&state->src) >> 16,
  9491. state->dst.x1, state->dst.y1,
  9492. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  9493. }
  9494. }
  9495. static bool encoders_cloneable(const struct intel_encoder *a,
  9496. const struct intel_encoder *b)
  9497. {
  9498. /* masks could be asymmetric, so check both ways */
  9499. return a == b || (a->cloneable & (1 << b->type) &&
  9500. b->cloneable & (1 << a->type));
  9501. }
  9502. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9503. struct intel_crtc *crtc,
  9504. struct intel_encoder *encoder)
  9505. {
  9506. struct intel_encoder *source_encoder;
  9507. struct drm_connector_state *connector_state;
  9508. int i;
  9509. for (i = 0; i < state->num_connector; i++) {
  9510. if (!state->connectors[i])
  9511. continue;
  9512. connector_state = state->connector_states[i];
  9513. if (connector_state->crtc != &crtc->base)
  9514. continue;
  9515. source_encoder =
  9516. to_intel_encoder(connector_state->best_encoder);
  9517. if (!encoders_cloneable(encoder, source_encoder))
  9518. return false;
  9519. }
  9520. return true;
  9521. }
  9522. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9523. struct intel_crtc *crtc)
  9524. {
  9525. struct intel_encoder *encoder;
  9526. struct drm_connector_state *connector_state;
  9527. int i;
  9528. for (i = 0; i < state->num_connector; i++) {
  9529. if (!state->connectors[i])
  9530. continue;
  9531. connector_state = state->connector_states[i];
  9532. if (connector_state->crtc != &crtc->base)
  9533. continue;
  9534. encoder = to_intel_encoder(connector_state->best_encoder);
  9535. if (!check_single_encoder_cloning(state, crtc, encoder))
  9536. return false;
  9537. }
  9538. return true;
  9539. }
  9540. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9541. {
  9542. struct drm_device *dev = state->dev;
  9543. struct intel_encoder *encoder;
  9544. struct drm_connector_state *connector_state;
  9545. unsigned int used_ports = 0;
  9546. int i;
  9547. /*
  9548. * Walk the connector list instead of the encoder
  9549. * list to detect the problem on ddi platforms
  9550. * where there's just one encoder per digital port.
  9551. */
  9552. for (i = 0; i < state->num_connector; i++) {
  9553. if (!state->connectors[i])
  9554. continue;
  9555. connector_state = state->connector_states[i];
  9556. if (!connector_state->best_encoder)
  9557. continue;
  9558. encoder = to_intel_encoder(connector_state->best_encoder);
  9559. WARN_ON(!connector_state->crtc);
  9560. switch (encoder->type) {
  9561. unsigned int port_mask;
  9562. case INTEL_OUTPUT_UNKNOWN:
  9563. if (WARN_ON(!HAS_DDI(dev)))
  9564. break;
  9565. case INTEL_OUTPUT_DISPLAYPORT:
  9566. case INTEL_OUTPUT_HDMI:
  9567. case INTEL_OUTPUT_EDP:
  9568. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9569. /* the same port mustn't appear more than once */
  9570. if (used_ports & port_mask)
  9571. return false;
  9572. used_ports |= port_mask;
  9573. default:
  9574. break;
  9575. }
  9576. }
  9577. return true;
  9578. }
  9579. static void
  9580. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9581. {
  9582. struct drm_crtc_state tmp_state;
  9583. struct intel_crtc_scaler_state scaler_state;
  9584. /* Clear only the intel specific part of the crtc state excluding scalers */
  9585. tmp_state = crtc_state->base;
  9586. scaler_state = crtc_state->scaler_state;
  9587. memset(crtc_state, 0, sizeof *crtc_state);
  9588. crtc_state->base = tmp_state;
  9589. crtc_state->scaler_state = scaler_state;
  9590. }
  9591. static struct intel_crtc_state *
  9592. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9593. struct drm_display_mode *mode,
  9594. struct drm_atomic_state *state)
  9595. {
  9596. struct intel_encoder *encoder;
  9597. struct intel_connector *connector;
  9598. struct drm_connector_state *connector_state;
  9599. struct intel_crtc_state *pipe_config;
  9600. int base_bpp, ret = -EINVAL;
  9601. int i;
  9602. bool retry = true;
  9603. if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
  9604. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9605. return ERR_PTR(-EINVAL);
  9606. }
  9607. if (!check_digital_port_conflicts(state)) {
  9608. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  9609. return ERR_PTR(-EINVAL);
  9610. }
  9611. pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
  9612. if (IS_ERR(pipe_config))
  9613. return pipe_config;
  9614. clear_intel_crtc_state(pipe_config);
  9615. pipe_config->base.crtc = crtc;
  9616. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  9617. drm_mode_copy(&pipe_config->base.mode, mode);
  9618. pipe_config->cpu_transcoder =
  9619. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9620. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  9621. /*
  9622. * Sanitize sync polarity flags based on requested ones. If neither
  9623. * positive or negative polarity is requested, treat this as meaning
  9624. * negative polarity.
  9625. */
  9626. if (!(pipe_config->base.adjusted_mode.flags &
  9627. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9628. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9629. if (!(pipe_config->base.adjusted_mode.flags &
  9630. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9631. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9632. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  9633. * plane pixel format and any sink constraints into account. Returns the
  9634. * source plane bpp so that dithering can be selected on mismatches
  9635. * after encoders and crtc also have had their say. */
  9636. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9637. pipe_config);
  9638. if (base_bpp < 0)
  9639. goto fail;
  9640. /*
  9641. * Determine the real pipe dimensions. Note that stereo modes can
  9642. * increase the actual pipe size due to the frame doubling and
  9643. * insertion of additional space for blanks between the frame. This
  9644. * is stored in the crtc timings. We use the requested mode to do this
  9645. * computation to clearly distinguish it from the adjusted mode, which
  9646. * can be changed by the connectors in the below retry loop.
  9647. */
  9648. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  9649. &pipe_config->pipe_src_w,
  9650. &pipe_config->pipe_src_h);
  9651. encoder_retry:
  9652. /* Ensure the port clock defaults are reset when retrying. */
  9653. pipe_config->port_clock = 0;
  9654. pipe_config->pixel_multiplier = 1;
  9655. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9656. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9657. CRTC_STEREO_DOUBLE);
  9658. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9659. * adjust it according to limitations or connector properties, and also
  9660. * a chance to reject the mode entirely.
  9661. */
  9662. for (i = 0; i < state->num_connector; i++) {
  9663. connector = to_intel_connector(state->connectors[i]);
  9664. if (!connector)
  9665. continue;
  9666. connector_state = state->connector_states[i];
  9667. if (connector_state->crtc != crtc)
  9668. continue;
  9669. encoder = to_intel_encoder(connector_state->best_encoder);
  9670. if (!(encoder->compute_config(encoder, pipe_config))) {
  9671. DRM_DEBUG_KMS("Encoder config failure\n");
  9672. goto fail;
  9673. }
  9674. }
  9675. /* Set default port clock if not overwritten by the encoder. Needs to be
  9676. * done afterwards in case the encoder adjusts the mode. */
  9677. if (!pipe_config->port_clock)
  9678. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9679. * pipe_config->pixel_multiplier;
  9680. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9681. if (ret < 0) {
  9682. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9683. goto fail;
  9684. }
  9685. if (ret == RETRY) {
  9686. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9687. ret = -EINVAL;
  9688. goto fail;
  9689. }
  9690. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9691. retry = false;
  9692. goto encoder_retry;
  9693. }
  9694. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  9695. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  9696. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9697. return pipe_config;
  9698. fail:
  9699. return ERR_PTR(ret);
  9700. }
  9701. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  9702. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  9703. static void
  9704. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  9705. unsigned *prepare_pipes, unsigned *disable_pipes)
  9706. {
  9707. struct intel_crtc *intel_crtc;
  9708. struct drm_device *dev = crtc->dev;
  9709. struct intel_encoder *encoder;
  9710. struct intel_connector *connector;
  9711. struct drm_crtc *tmp_crtc;
  9712. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  9713. /* Check which crtcs have changed outputs connected to them, these need
  9714. * to be part of the prepare_pipes mask. We don't (yet) support global
  9715. * modeset across multiple crtcs, so modeset_pipes will only have one
  9716. * bit set at most. */
  9717. for_each_intel_connector(dev, connector) {
  9718. if (connector->base.encoder == &connector->new_encoder->base)
  9719. continue;
  9720. if (connector->base.encoder) {
  9721. tmp_crtc = connector->base.encoder->crtc;
  9722. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  9723. }
  9724. if (connector->new_encoder)
  9725. *prepare_pipes |=
  9726. 1 << connector->new_encoder->new_crtc->pipe;
  9727. }
  9728. for_each_intel_encoder(dev, encoder) {
  9729. if (encoder->base.crtc == &encoder->new_crtc->base)
  9730. continue;
  9731. if (encoder->base.crtc) {
  9732. tmp_crtc = encoder->base.crtc;
  9733. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  9734. }
  9735. if (encoder->new_crtc)
  9736. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  9737. }
  9738. /* Check for pipes that will be enabled/disabled ... */
  9739. for_each_intel_crtc(dev, intel_crtc) {
  9740. if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
  9741. continue;
  9742. if (!intel_crtc->new_enabled)
  9743. *disable_pipes |= 1 << intel_crtc->pipe;
  9744. else
  9745. *prepare_pipes |= 1 << intel_crtc->pipe;
  9746. }
  9747. /* set_mode is also used to update properties on life display pipes. */
  9748. intel_crtc = to_intel_crtc(crtc);
  9749. if (intel_crtc->new_enabled)
  9750. *prepare_pipes |= 1 << intel_crtc->pipe;
  9751. /*
  9752. * For simplicity do a full modeset on any pipe where the output routing
  9753. * changed. We could be more clever, but that would require us to be
  9754. * more careful with calling the relevant encoder->mode_set functions.
  9755. */
  9756. if (*prepare_pipes)
  9757. *modeset_pipes = *prepare_pipes;
  9758. /* ... and mask these out. */
  9759. *modeset_pipes &= ~(*disable_pipes);
  9760. *prepare_pipes &= ~(*disable_pipes);
  9761. /*
  9762. * HACK: We don't (yet) fully support global modesets. intel_set_config
  9763. * obies this rule, but the modeset restore mode of
  9764. * intel_modeset_setup_hw_state does not.
  9765. */
  9766. *modeset_pipes &= 1 << intel_crtc->pipe;
  9767. *prepare_pipes &= 1 << intel_crtc->pipe;
  9768. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  9769. *modeset_pipes, *prepare_pipes, *disable_pipes);
  9770. }
  9771. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  9772. {
  9773. struct drm_encoder *encoder;
  9774. struct drm_device *dev = crtc->dev;
  9775. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  9776. if (encoder->crtc == crtc)
  9777. return true;
  9778. return false;
  9779. }
  9780. static void
  9781. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  9782. {
  9783. struct drm_i915_private *dev_priv = dev->dev_private;
  9784. struct intel_encoder *intel_encoder;
  9785. struct intel_crtc *intel_crtc;
  9786. struct drm_connector *connector;
  9787. intel_shared_dpll_commit(dev_priv);
  9788. for_each_intel_encoder(dev, intel_encoder) {
  9789. if (!intel_encoder->base.crtc)
  9790. continue;
  9791. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  9792. if (prepare_pipes & (1 << intel_crtc->pipe))
  9793. intel_encoder->connectors_active = false;
  9794. }
  9795. intel_modeset_commit_output_state(dev);
  9796. /* Double check state. */
  9797. for_each_intel_crtc(dev, intel_crtc) {
  9798. WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
  9799. }
  9800. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9801. if (!connector->encoder || !connector->encoder->crtc)
  9802. continue;
  9803. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  9804. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  9805. struct drm_property *dpms_property =
  9806. dev->mode_config.dpms_property;
  9807. connector->dpms = DRM_MODE_DPMS_ON;
  9808. drm_object_property_set_value(&connector->base,
  9809. dpms_property,
  9810. DRM_MODE_DPMS_ON);
  9811. intel_encoder = to_intel_encoder(connector->encoder);
  9812. intel_encoder->connectors_active = true;
  9813. }
  9814. }
  9815. }
  9816. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9817. {
  9818. int diff;
  9819. if (clock1 == clock2)
  9820. return true;
  9821. if (!clock1 || !clock2)
  9822. return false;
  9823. diff = abs(clock1 - clock2);
  9824. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9825. return true;
  9826. return false;
  9827. }
  9828. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  9829. list_for_each_entry((intel_crtc), \
  9830. &(dev)->mode_config.crtc_list, \
  9831. base.head) \
  9832. if (mask & (1 <<(intel_crtc)->pipe))
  9833. static bool
  9834. intel_pipe_config_compare(struct drm_device *dev,
  9835. struct intel_crtc_state *current_config,
  9836. struct intel_crtc_state *pipe_config)
  9837. {
  9838. #define PIPE_CONF_CHECK_X(name) \
  9839. if (current_config->name != pipe_config->name) { \
  9840. DRM_ERROR("mismatch in " #name " " \
  9841. "(expected 0x%08x, found 0x%08x)\n", \
  9842. current_config->name, \
  9843. pipe_config->name); \
  9844. return false; \
  9845. }
  9846. #define PIPE_CONF_CHECK_I(name) \
  9847. if (current_config->name != pipe_config->name) { \
  9848. DRM_ERROR("mismatch in " #name " " \
  9849. "(expected %i, found %i)\n", \
  9850. current_config->name, \
  9851. pipe_config->name); \
  9852. return false; \
  9853. }
  9854. /* This is required for BDW+ where there is only one set of registers for
  9855. * switching between high and low RR.
  9856. * This macro can be used whenever a comparison has to be made between one
  9857. * hw state and multiple sw state variables.
  9858. */
  9859. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  9860. if ((current_config->name != pipe_config->name) && \
  9861. (current_config->alt_name != pipe_config->name)) { \
  9862. DRM_ERROR("mismatch in " #name " " \
  9863. "(expected %i or %i, found %i)\n", \
  9864. current_config->name, \
  9865. current_config->alt_name, \
  9866. pipe_config->name); \
  9867. return false; \
  9868. }
  9869. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9870. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9871. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  9872. "(expected %i, found %i)\n", \
  9873. current_config->name & (mask), \
  9874. pipe_config->name & (mask)); \
  9875. return false; \
  9876. }
  9877. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9878. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9879. DRM_ERROR("mismatch in " #name " " \
  9880. "(expected %i, found %i)\n", \
  9881. current_config->name, \
  9882. pipe_config->name); \
  9883. return false; \
  9884. }
  9885. #define PIPE_CONF_QUIRK(quirk) \
  9886. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9887. PIPE_CONF_CHECK_I(cpu_transcoder);
  9888. PIPE_CONF_CHECK_I(has_pch_encoder);
  9889. PIPE_CONF_CHECK_I(fdi_lanes);
  9890. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  9891. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  9892. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  9893. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  9894. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  9895. PIPE_CONF_CHECK_I(has_dp_encoder);
  9896. if (INTEL_INFO(dev)->gen < 8) {
  9897. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  9898. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  9899. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  9900. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  9901. PIPE_CONF_CHECK_I(dp_m_n.tu);
  9902. if (current_config->has_drrs) {
  9903. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  9904. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  9905. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  9906. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  9907. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  9908. }
  9909. } else {
  9910. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  9911. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  9912. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  9913. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  9914. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  9915. }
  9916. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9917. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9918. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9919. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9920. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9921. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9922. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9923. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9924. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9925. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9926. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9927. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9928. PIPE_CONF_CHECK_I(pixel_multiplier);
  9929. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9930. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  9931. IS_VALLEYVIEW(dev))
  9932. PIPE_CONF_CHECK_I(limited_color_range);
  9933. PIPE_CONF_CHECK_I(has_infoframe);
  9934. PIPE_CONF_CHECK_I(has_audio);
  9935. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9936. DRM_MODE_FLAG_INTERLACE);
  9937. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9938. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9939. DRM_MODE_FLAG_PHSYNC);
  9940. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9941. DRM_MODE_FLAG_NHSYNC);
  9942. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9943. DRM_MODE_FLAG_PVSYNC);
  9944. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9945. DRM_MODE_FLAG_NVSYNC);
  9946. }
  9947. PIPE_CONF_CHECK_I(pipe_src_w);
  9948. PIPE_CONF_CHECK_I(pipe_src_h);
  9949. /*
  9950. * FIXME: BIOS likes to set up a cloned config with lvds+external
  9951. * screen. Since we don't yet re-compute the pipe config when moving
  9952. * just the lvds port away to another pipe the sw tracking won't match.
  9953. *
  9954. * Proper atomic modesets with recomputed global state will fix this.
  9955. * Until then just don't check gmch state for inherited modes.
  9956. */
  9957. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  9958. PIPE_CONF_CHECK_I(gmch_pfit.control);
  9959. /* pfit ratios are autocomputed by the hw on gen4+ */
  9960. if (INTEL_INFO(dev)->gen < 4)
  9961. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  9962. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  9963. }
  9964. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9965. if (current_config->pch_pfit.enabled) {
  9966. PIPE_CONF_CHECK_I(pch_pfit.pos);
  9967. PIPE_CONF_CHECK_I(pch_pfit.size);
  9968. }
  9969. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9970. /* BDW+ don't expose a synchronous way to read the state */
  9971. if (IS_HASWELL(dev))
  9972. PIPE_CONF_CHECK_I(ips_enabled);
  9973. PIPE_CONF_CHECK_I(double_wide);
  9974. PIPE_CONF_CHECK_X(ddi_pll_sel);
  9975. PIPE_CONF_CHECK_I(shared_dpll);
  9976. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9977. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9978. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9979. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9980. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9981. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9982. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9983. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9984. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  9985. PIPE_CONF_CHECK_I(pipe_bpp);
  9986. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9987. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9988. #undef PIPE_CONF_CHECK_X
  9989. #undef PIPE_CONF_CHECK_I
  9990. #undef PIPE_CONF_CHECK_I_ALT
  9991. #undef PIPE_CONF_CHECK_FLAGS
  9992. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9993. #undef PIPE_CONF_QUIRK
  9994. return true;
  9995. }
  9996. static void check_wm_state(struct drm_device *dev)
  9997. {
  9998. struct drm_i915_private *dev_priv = dev->dev_private;
  9999. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10000. struct intel_crtc *intel_crtc;
  10001. int plane;
  10002. if (INTEL_INFO(dev)->gen < 9)
  10003. return;
  10004. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10005. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10006. for_each_intel_crtc(dev, intel_crtc) {
  10007. struct skl_ddb_entry *hw_entry, *sw_entry;
  10008. const enum pipe pipe = intel_crtc->pipe;
  10009. if (!intel_crtc->active)
  10010. continue;
  10011. /* planes */
  10012. for_each_plane(dev_priv, pipe, plane) {
  10013. hw_entry = &hw_ddb.plane[pipe][plane];
  10014. sw_entry = &sw_ddb->plane[pipe][plane];
  10015. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10016. continue;
  10017. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10018. "(expected (%u,%u), found (%u,%u))\n",
  10019. pipe_name(pipe), plane + 1,
  10020. sw_entry->start, sw_entry->end,
  10021. hw_entry->start, hw_entry->end);
  10022. }
  10023. /* cursor */
  10024. hw_entry = &hw_ddb.cursor[pipe];
  10025. sw_entry = &sw_ddb->cursor[pipe];
  10026. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10027. continue;
  10028. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10029. "(expected (%u,%u), found (%u,%u))\n",
  10030. pipe_name(pipe),
  10031. sw_entry->start, sw_entry->end,
  10032. hw_entry->start, hw_entry->end);
  10033. }
  10034. }
  10035. static void
  10036. check_connector_state(struct drm_device *dev)
  10037. {
  10038. struct intel_connector *connector;
  10039. for_each_intel_connector(dev, connector) {
  10040. /* This also checks the encoder/connector hw state with the
  10041. * ->get_hw_state callbacks. */
  10042. intel_connector_check_state(connector);
  10043. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10044. "connector's staged encoder doesn't match current encoder\n");
  10045. }
  10046. }
  10047. static void
  10048. check_encoder_state(struct drm_device *dev)
  10049. {
  10050. struct intel_encoder *encoder;
  10051. struct intel_connector *connector;
  10052. for_each_intel_encoder(dev, encoder) {
  10053. bool enabled = false;
  10054. bool active = false;
  10055. enum pipe pipe, tracked_pipe;
  10056. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10057. encoder->base.base.id,
  10058. encoder->base.name);
  10059. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10060. "encoder's stage crtc doesn't match current crtc\n");
  10061. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10062. "encoder's active_connectors set, but no crtc\n");
  10063. for_each_intel_connector(dev, connector) {
  10064. if (connector->base.encoder != &encoder->base)
  10065. continue;
  10066. enabled = true;
  10067. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10068. active = true;
  10069. }
  10070. /*
  10071. * for MST connectors if we unplug the connector is gone
  10072. * away but the encoder is still connected to a crtc
  10073. * until a modeset happens in response to the hotplug.
  10074. */
  10075. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10076. continue;
  10077. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10078. "encoder's enabled state mismatch "
  10079. "(expected %i, found %i)\n",
  10080. !!encoder->base.crtc, enabled);
  10081. I915_STATE_WARN(active && !encoder->base.crtc,
  10082. "active encoder with no crtc\n");
  10083. I915_STATE_WARN(encoder->connectors_active != active,
  10084. "encoder's computed active state doesn't match tracked active state "
  10085. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10086. active = encoder->get_hw_state(encoder, &pipe);
  10087. I915_STATE_WARN(active != encoder->connectors_active,
  10088. "encoder's hw state doesn't match sw tracking "
  10089. "(expected %i, found %i)\n",
  10090. encoder->connectors_active, active);
  10091. if (!encoder->base.crtc)
  10092. continue;
  10093. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10094. I915_STATE_WARN(active && pipe != tracked_pipe,
  10095. "active encoder's pipe doesn't match"
  10096. "(expected %i, found %i)\n",
  10097. tracked_pipe, pipe);
  10098. }
  10099. }
  10100. static void
  10101. check_crtc_state(struct drm_device *dev)
  10102. {
  10103. struct drm_i915_private *dev_priv = dev->dev_private;
  10104. struct intel_crtc *crtc;
  10105. struct intel_encoder *encoder;
  10106. struct intel_crtc_state pipe_config;
  10107. for_each_intel_crtc(dev, crtc) {
  10108. bool enabled = false;
  10109. bool active = false;
  10110. memset(&pipe_config, 0, sizeof(pipe_config));
  10111. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10112. crtc->base.base.id);
  10113. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10114. "active crtc, but not enabled in sw tracking\n");
  10115. for_each_intel_encoder(dev, encoder) {
  10116. if (encoder->base.crtc != &crtc->base)
  10117. continue;
  10118. enabled = true;
  10119. if (encoder->connectors_active)
  10120. active = true;
  10121. }
  10122. I915_STATE_WARN(active != crtc->active,
  10123. "crtc's computed active state doesn't match tracked active state "
  10124. "(expected %i, found %i)\n", active, crtc->active);
  10125. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10126. "crtc's computed enabled state doesn't match tracked enabled state "
  10127. "(expected %i, found %i)\n", enabled,
  10128. crtc->base.state->enable);
  10129. active = dev_priv->display.get_pipe_config(crtc,
  10130. &pipe_config);
  10131. /* hw state is inconsistent with the pipe quirk */
  10132. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10133. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10134. active = crtc->active;
  10135. for_each_intel_encoder(dev, encoder) {
  10136. enum pipe pipe;
  10137. if (encoder->base.crtc != &crtc->base)
  10138. continue;
  10139. if (encoder->get_hw_state(encoder, &pipe))
  10140. encoder->get_config(encoder, &pipe_config);
  10141. }
  10142. I915_STATE_WARN(crtc->active != active,
  10143. "crtc active state doesn't match with hw state "
  10144. "(expected %i, found %i)\n", crtc->active, active);
  10145. if (active &&
  10146. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10147. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10148. intel_dump_pipe_config(crtc, &pipe_config,
  10149. "[hw state]");
  10150. intel_dump_pipe_config(crtc, crtc->config,
  10151. "[sw state]");
  10152. }
  10153. }
  10154. }
  10155. static void
  10156. check_shared_dpll_state(struct drm_device *dev)
  10157. {
  10158. struct drm_i915_private *dev_priv = dev->dev_private;
  10159. struct intel_crtc *crtc;
  10160. struct intel_dpll_hw_state dpll_hw_state;
  10161. int i;
  10162. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10163. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10164. int enabled_crtcs = 0, active_crtcs = 0;
  10165. bool active;
  10166. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10167. DRM_DEBUG_KMS("%s\n", pll->name);
  10168. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10169. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10170. "more active pll users than references: %i vs %i\n",
  10171. pll->active, hweight32(pll->config.crtc_mask));
  10172. I915_STATE_WARN(pll->active && !pll->on,
  10173. "pll in active use but not on in sw tracking\n");
  10174. I915_STATE_WARN(pll->on && !pll->active,
  10175. "pll in on but not on in use in sw tracking\n");
  10176. I915_STATE_WARN(pll->on != active,
  10177. "pll on state mismatch (expected %i, found %i)\n",
  10178. pll->on, active);
  10179. for_each_intel_crtc(dev, crtc) {
  10180. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10181. enabled_crtcs++;
  10182. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10183. active_crtcs++;
  10184. }
  10185. I915_STATE_WARN(pll->active != active_crtcs,
  10186. "pll active crtcs mismatch (expected %i, found %i)\n",
  10187. pll->active, active_crtcs);
  10188. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10189. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10190. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10191. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10192. sizeof(dpll_hw_state)),
  10193. "pll hw state mismatch\n");
  10194. }
  10195. }
  10196. void
  10197. intel_modeset_check_state(struct drm_device *dev)
  10198. {
  10199. check_wm_state(dev);
  10200. check_connector_state(dev);
  10201. check_encoder_state(dev);
  10202. check_crtc_state(dev);
  10203. check_shared_dpll_state(dev);
  10204. }
  10205. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10206. int dotclock)
  10207. {
  10208. /*
  10209. * FDI already provided one idea for the dotclock.
  10210. * Yell if the encoder disagrees.
  10211. */
  10212. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10213. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10214. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10215. }
  10216. static void update_scanline_offset(struct intel_crtc *crtc)
  10217. {
  10218. struct drm_device *dev = crtc->base.dev;
  10219. /*
  10220. * The scanline counter increments at the leading edge of hsync.
  10221. *
  10222. * On most platforms it starts counting from vtotal-1 on the
  10223. * first active line. That means the scanline counter value is
  10224. * always one less than what we would expect. Ie. just after
  10225. * start of vblank, which also occurs at start of hsync (on the
  10226. * last active line), the scanline counter will read vblank_start-1.
  10227. *
  10228. * On gen2 the scanline counter starts counting from 1 instead
  10229. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10230. * to keep the value positive), instead of adding one.
  10231. *
  10232. * On HSW+ the behaviour of the scanline counter depends on the output
  10233. * type. For DP ports it behaves like most other platforms, but on HDMI
  10234. * there's an extra 1 line difference. So we need to add two instead of
  10235. * one to the value.
  10236. */
  10237. if (IS_GEN2(dev)) {
  10238. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10239. int vtotal;
  10240. vtotal = mode->crtc_vtotal;
  10241. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10242. vtotal /= 2;
  10243. crtc->scanline_offset = vtotal - 1;
  10244. } else if (HAS_DDI(dev) &&
  10245. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10246. crtc->scanline_offset = 2;
  10247. } else
  10248. crtc->scanline_offset = 1;
  10249. }
  10250. static struct intel_crtc_state *
  10251. intel_modeset_compute_config(struct drm_crtc *crtc,
  10252. struct drm_display_mode *mode,
  10253. struct drm_atomic_state *state,
  10254. unsigned *modeset_pipes,
  10255. unsigned *prepare_pipes,
  10256. unsigned *disable_pipes)
  10257. {
  10258. struct drm_device *dev = crtc->dev;
  10259. struct intel_crtc_state *pipe_config = NULL;
  10260. struct intel_crtc *intel_crtc;
  10261. int ret = 0;
  10262. ret = drm_atomic_add_affected_connectors(state, crtc);
  10263. if (ret)
  10264. return ERR_PTR(ret);
  10265. intel_modeset_affected_pipes(crtc, modeset_pipes,
  10266. prepare_pipes, disable_pipes);
  10267. for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
  10268. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10269. if (IS_ERR(pipe_config))
  10270. return pipe_config;
  10271. pipe_config->base.enable = false;
  10272. }
  10273. /*
  10274. * Note this needs changes when we start tracking multiple modes
  10275. * and crtcs. At that point we'll need to compute the whole config
  10276. * (i.e. one pipe_config for each crtc) rather than just the one
  10277. * for this crtc.
  10278. */
  10279. for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
  10280. /* FIXME: For now we still expect modeset_pipes has at most
  10281. * one bit set. */
  10282. if (WARN_ON(&intel_crtc->base != crtc))
  10283. continue;
  10284. pipe_config = intel_modeset_pipe_config(crtc, mode, state);
  10285. if (IS_ERR(pipe_config))
  10286. return pipe_config;
  10287. pipe_config->base.enable = true;
  10288. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10289. "[modeset]");
  10290. }
  10291. return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
  10292. }
  10293. static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
  10294. unsigned modeset_pipes,
  10295. unsigned disable_pipes)
  10296. {
  10297. struct drm_device *dev = state->dev;
  10298. struct drm_i915_private *dev_priv = to_i915(dev);
  10299. unsigned clear_pipes = modeset_pipes | disable_pipes;
  10300. struct intel_crtc *intel_crtc;
  10301. int ret = 0;
  10302. if (!dev_priv->display.crtc_compute_clock)
  10303. return 0;
  10304. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  10305. if (ret)
  10306. goto done;
  10307. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  10308. struct intel_crtc_state *crtc_state =
  10309. intel_atomic_get_crtc_state(state, intel_crtc);
  10310. /* Modeset pipes should have a new state by now */
  10311. if (WARN_ON(IS_ERR(crtc_state)))
  10312. continue;
  10313. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10314. crtc_state);
  10315. if (ret) {
  10316. intel_shared_dpll_abort_config(dev_priv);
  10317. goto done;
  10318. }
  10319. }
  10320. done:
  10321. return ret;
  10322. }
  10323. static int __intel_set_mode(struct drm_crtc *crtc,
  10324. struct drm_display_mode *mode,
  10325. int x, int y, struct drm_framebuffer *fb,
  10326. struct intel_crtc_state *pipe_config,
  10327. unsigned modeset_pipes,
  10328. unsigned prepare_pipes,
  10329. unsigned disable_pipes)
  10330. {
  10331. struct drm_device *dev = crtc->dev;
  10332. struct drm_i915_private *dev_priv = dev->dev_private;
  10333. struct drm_display_mode *saved_mode;
  10334. struct drm_atomic_state *state = pipe_config->base.state;
  10335. struct intel_crtc_state *crtc_state_copy = NULL;
  10336. struct intel_crtc *intel_crtc;
  10337. int ret = 0;
  10338. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  10339. if (!saved_mode)
  10340. return -ENOMEM;
  10341. crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
  10342. if (!crtc_state_copy) {
  10343. ret = -ENOMEM;
  10344. goto done;
  10345. }
  10346. *saved_mode = crtc->mode;
  10347. /*
  10348. * See if the config requires any additional preparation, e.g.
  10349. * to adjust global state with pipes off. We need to do this
  10350. * here so we can get the modeset_pipe updated config for the new
  10351. * mode set on this crtc. For other crtcs we need to use the
  10352. * adjusted_mode bits in the crtc directly.
  10353. */
  10354. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  10355. ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
  10356. if (ret)
  10357. goto done;
  10358. /* may have added more to prepare_pipes than we should */
  10359. prepare_pipes &= ~disable_pipes;
  10360. }
  10361. ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
  10362. if (ret)
  10363. goto done;
  10364. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  10365. intel_crtc_disable(&intel_crtc->base);
  10366. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  10367. if (intel_crtc->base.state->enable) {
  10368. intel_crtc_disable_planes(&intel_crtc->base);
  10369. dev_priv->display.crtc_disable(&intel_crtc->base);
  10370. }
  10371. }
  10372. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  10373. * to set it here already despite that we pass it down the callchain.
  10374. *
  10375. * Note we'll need to fix this up when we start tracking multiple
  10376. * pipes; here we assume a single modeset_pipe and only track the
  10377. * single crtc and mode.
  10378. */
  10379. if (modeset_pipes) {
  10380. crtc->mode = *mode;
  10381. /* mode_set/enable/disable functions rely on a correct pipe
  10382. * config. */
  10383. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  10384. /*
  10385. * Calculate and store various constants which
  10386. * are later needed by vblank and swap-completion
  10387. * timestamping. They are derived from true hwmode.
  10388. */
  10389. drm_calc_timestamping_constants(crtc,
  10390. &pipe_config->base.adjusted_mode);
  10391. }
  10392. /* Only after disabling all output pipelines that will be changed can we
  10393. * update the the output configuration. */
  10394. intel_modeset_update_state(dev, prepare_pipes);
  10395. modeset_update_crtc_power_domains(state);
  10396. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  10397. struct drm_plane *primary = intel_crtc->base.primary;
  10398. int vdisplay, hdisplay;
  10399. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  10400. ret = drm_plane_helper_update(primary, &intel_crtc->base,
  10401. fb, 0, 0,
  10402. hdisplay, vdisplay,
  10403. x << 16, y << 16,
  10404. hdisplay << 16, vdisplay << 16);
  10405. }
  10406. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10407. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  10408. update_scanline_offset(intel_crtc);
  10409. dev_priv->display.crtc_enable(&intel_crtc->base);
  10410. intel_crtc_enable_planes(&intel_crtc->base);
  10411. }
  10412. /* FIXME: add subpixel order */
  10413. done:
  10414. if (ret && crtc->state->enable)
  10415. crtc->mode = *saved_mode;
  10416. if (ret == 0 && pipe_config) {
  10417. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10418. /* The pipe_config will be freed with the atomic state, so
  10419. * make a copy. */
  10420. memcpy(crtc_state_copy, intel_crtc->config,
  10421. sizeof *crtc_state_copy);
  10422. intel_crtc->config = crtc_state_copy;
  10423. intel_crtc->base.state = &crtc_state_copy->base;
  10424. } else {
  10425. kfree(crtc_state_copy);
  10426. }
  10427. kfree(saved_mode);
  10428. return ret;
  10429. }
  10430. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  10431. struct drm_display_mode *mode,
  10432. int x, int y, struct drm_framebuffer *fb,
  10433. struct intel_crtc_state *pipe_config,
  10434. unsigned modeset_pipes,
  10435. unsigned prepare_pipes,
  10436. unsigned disable_pipes)
  10437. {
  10438. int ret;
  10439. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  10440. prepare_pipes, disable_pipes);
  10441. if (ret == 0)
  10442. intel_modeset_check_state(crtc->dev);
  10443. return ret;
  10444. }
  10445. static int intel_set_mode(struct drm_crtc *crtc,
  10446. struct drm_display_mode *mode,
  10447. int x, int y, struct drm_framebuffer *fb,
  10448. struct drm_atomic_state *state)
  10449. {
  10450. struct intel_crtc_state *pipe_config;
  10451. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  10452. int ret = 0;
  10453. pipe_config = intel_modeset_compute_config(crtc, mode, state,
  10454. &modeset_pipes,
  10455. &prepare_pipes,
  10456. &disable_pipes);
  10457. if (IS_ERR(pipe_config)) {
  10458. ret = PTR_ERR(pipe_config);
  10459. goto out;
  10460. }
  10461. ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  10462. modeset_pipes, prepare_pipes,
  10463. disable_pipes);
  10464. if (ret)
  10465. goto out;
  10466. out:
  10467. return ret;
  10468. }
  10469. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10470. {
  10471. struct drm_device *dev = crtc->dev;
  10472. struct drm_atomic_state *state;
  10473. struct intel_encoder *encoder;
  10474. struct intel_connector *connector;
  10475. struct drm_connector_state *connector_state;
  10476. state = drm_atomic_state_alloc(dev);
  10477. if (!state) {
  10478. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  10479. crtc->base.id);
  10480. return;
  10481. }
  10482. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10483. /* The force restore path in the HW readout code relies on the staged
  10484. * config still keeping the user requested config while the actual
  10485. * state has been overwritten by the configuration read from HW. We
  10486. * need to copy the staged config to the atomic state, otherwise the
  10487. * mode set will just reapply the state the HW is already in. */
  10488. for_each_intel_encoder(dev, encoder) {
  10489. if (&encoder->new_crtc->base != crtc)
  10490. continue;
  10491. for_each_intel_connector(dev, connector) {
  10492. if (connector->new_encoder != encoder)
  10493. continue;
  10494. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  10495. if (IS_ERR(connector_state)) {
  10496. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  10497. connector->base.base.id,
  10498. connector->base.name,
  10499. PTR_ERR(connector_state));
  10500. continue;
  10501. }
  10502. connector_state->crtc = crtc;
  10503. connector_state->best_encoder = &encoder->base;
  10504. }
  10505. }
  10506. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
  10507. state);
  10508. drm_atomic_state_free(state);
  10509. }
  10510. #undef for_each_intel_crtc_masked
  10511. static void intel_set_config_free(struct intel_set_config *config)
  10512. {
  10513. if (!config)
  10514. return;
  10515. kfree(config->save_connector_encoders);
  10516. kfree(config->save_encoder_crtcs);
  10517. kfree(config->save_crtc_enabled);
  10518. kfree(config);
  10519. }
  10520. static int intel_set_config_save_state(struct drm_device *dev,
  10521. struct intel_set_config *config)
  10522. {
  10523. struct drm_crtc *crtc;
  10524. struct drm_encoder *encoder;
  10525. struct drm_connector *connector;
  10526. int count;
  10527. config->save_crtc_enabled =
  10528. kcalloc(dev->mode_config.num_crtc,
  10529. sizeof(bool), GFP_KERNEL);
  10530. if (!config->save_crtc_enabled)
  10531. return -ENOMEM;
  10532. config->save_encoder_crtcs =
  10533. kcalloc(dev->mode_config.num_encoder,
  10534. sizeof(struct drm_crtc *), GFP_KERNEL);
  10535. if (!config->save_encoder_crtcs)
  10536. return -ENOMEM;
  10537. config->save_connector_encoders =
  10538. kcalloc(dev->mode_config.num_connector,
  10539. sizeof(struct drm_encoder *), GFP_KERNEL);
  10540. if (!config->save_connector_encoders)
  10541. return -ENOMEM;
  10542. /* Copy data. Note that driver private data is not affected.
  10543. * Should anything bad happen only the expected state is
  10544. * restored, not the drivers personal bookkeeping.
  10545. */
  10546. count = 0;
  10547. for_each_crtc(dev, crtc) {
  10548. config->save_crtc_enabled[count++] = crtc->state->enable;
  10549. }
  10550. count = 0;
  10551. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  10552. config->save_encoder_crtcs[count++] = encoder->crtc;
  10553. }
  10554. count = 0;
  10555. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10556. config->save_connector_encoders[count++] = connector->encoder;
  10557. }
  10558. return 0;
  10559. }
  10560. static void intel_set_config_restore_state(struct drm_device *dev,
  10561. struct intel_set_config *config)
  10562. {
  10563. struct intel_crtc *crtc;
  10564. struct intel_encoder *encoder;
  10565. struct intel_connector *connector;
  10566. int count;
  10567. count = 0;
  10568. for_each_intel_crtc(dev, crtc) {
  10569. crtc->new_enabled = config->save_crtc_enabled[count++];
  10570. }
  10571. count = 0;
  10572. for_each_intel_encoder(dev, encoder) {
  10573. encoder->new_crtc =
  10574. to_intel_crtc(config->save_encoder_crtcs[count++]);
  10575. }
  10576. count = 0;
  10577. for_each_intel_connector(dev, connector) {
  10578. connector->new_encoder =
  10579. to_intel_encoder(config->save_connector_encoders[count++]);
  10580. }
  10581. }
  10582. static bool
  10583. is_crtc_connector_off(struct drm_mode_set *set)
  10584. {
  10585. int i;
  10586. if (set->num_connectors == 0)
  10587. return false;
  10588. if (WARN_ON(set->connectors == NULL))
  10589. return false;
  10590. for (i = 0; i < set->num_connectors; i++)
  10591. if (set->connectors[i]->encoder &&
  10592. set->connectors[i]->encoder->crtc == set->crtc &&
  10593. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  10594. return true;
  10595. return false;
  10596. }
  10597. static void
  10598. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  10599. struct intel_set_config *config)
  10600. {
  10601. /* We should be able to check here if the fb has the same properties
  10602. * and then just flip_or_move it */
  10603. if (is_crtc_connector_off(set)) {
  10604. config->mode_changed = true;
  10605. } else if (set->crtc->primary->fb != set->fb) {
  10606. /*
  10607. * If we have no fb, we can only flip as long as the crtc is
  10608. * active, otherwise we need a full mode set. The crtc may
  10609. * be active if we've only disabled the primary plane, or
  10610. * in fastboot situations.
  10611. */
  10612. if (set->crtc->primary->fb == NULL) {
  10613. struct intel_crtc *intel_crtc =
  10614. to_intel_crtc(set->crtc);
  10615. if (intel_crtc->active) {
  10616. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  10617. config->fb_changed = true;
  10618. } else {
  10619. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  10620. config->mode_changed = true;
  10621. }
  10622. } else if (set->fb == NULL) {
  10623. config->mode_changed = true;
  10624. } else if (set->fb->pixel_format !=
  10625. set->crtc->primary->fb->pixel_format) {
  10626. config->mode_changed = true;
  10627. } else {
  10628. config->fb_changed = true;
  10629. }
  10630. }
  10631. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  10632. config->fb_changed = true;
  10633. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  10634. DRM_DEBUG_KMS("modes are different, full mode set\n");
  10635. drm_mode_debug_printmodeline(&set->crtc->mode);
  10636. drm_mode_debug_printmodeline(set->mode);
  10637. config->mode_changed = true;
  10638. }
  10639. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  10640. set->crtc->base.id, config->mode_changed, config->fb_changed);
  10641. }
  10642. static int
  10643. intel_modeset_stage_output_state(struct drm_device *dev,
  10644. struct drm_mode_set *set,
  10645. struct intel_set_config *config,
  10646. struct drm_atomic_state *state)
  10647. {
  10648. struct intel_connector *connector;
  10649. struct drm_connector_state *connector_state;
  10650. struct intel_encoder *encoder;
  10651. struct intel_crtc *crtc;
  10652. int ro;
  10653. /* The upper layers ensure that we either disable a crtc or have a list
  10654. * of connectors. For paranoia, double-check this. */
  10655. WARN_ON(!set->fb && (set->num_connectors != 0));
  10656. WARN_ON(set->fb && (set->num_connectors == 0));
  10657. for_each_intel_connector(dev, connector) {
  10658. /* Otherwise traverse passed in connector list and get encoders
  10659. * for them. */
  10660. for (ro = 0; ro < set->num_connectors; ro++) {
  10661. if (set->connectors[ro] == &connector->base) {
  10662. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  10663. break;
  10664. }
  10665. }
  10666. /* If we disable the crtc, disable all its connectors. Also, if
  10667. * the connector is on the changing crtc but not on the new
  10668. * connector list, disable it. */
  10669. if ((!set->fb || ro == set->num_connectors) &&
  10670. connector->base.encoder &&
  10671. connector->base.encoder->crtc == set->crtc) {
  10672. connector->new_encoder = NULL;
  10673. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  10674. connector->base.base.id,
  10675. connector->base.name);
  10676. }
  10677. if (&connector->new_encoder->base != connector->base.encoder) {
  10678. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
  10679. connector->base.base.id,
  10680. connector->base.name);
  10681. config->mode_changed = true;
  10682. }
  10683. }
  10684. /* connector->new_encoder is now updated for all connectors. */
  10685. /* Update crtc of enabled connectors. */
  10686. for_each_intel_connector(dev, connector) {
  10687. struct drm_crtc *new_crtc;
  10688. if (!connector->new_encoder)
  10689. continue;
  10690. new_crtc = connector->new_encoder->base.crtc;
  10691. for (ro = 0; ro < set->num_connectors; ro++) {
  10692. if (set->connectors[ro] == &connector->base)
  10693. new_crtc = set->crtc;
  10694. }
  10695. /* Make sure the new CRTC will work with the encoder */
  10696. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  10697. new_crtc)) {
  10698. return -EINVAL;
  10699. }
  10700. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  10701. connector_state =
  10702. drm_atomic_get_connector_state(state, &connector->base);
  10703. if (IS_ERR(connector_state))
  10704. return PTR_ERR(connector_state);
  10705. connector_state->crtc = new_crtc;
  10706. connector_state->best_encoder = &connector->new_encoder->base;
  10707. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  10708. connector->base.base.id,
  10709. connector->base.name,
  10710. new_crtc->base.id);
  10711. }
  10712. /* Check for any encoders that needs to be disabled. */
  10713. for_each_intel_encoder(dev, encoder) {
  10714. int num_connectors = 0;
  10715. for_each_intel_connector(dev, connector) {
  10716. if (connector->new_encoder == encoder) {
  10717. WARN_ON(!connector->new_encoder->new_crtc);
  10718. num_connectors++;
  10719. }
  10720. }
  10721. if (num_connectors == 0)
  10722. encoder->new_crtc = NULL;
  10723. else if (num_connectors > 1)
  10724. return -EINVAL;
  10725. /* Only now check for crtc changes so we don't miss encoders
  10726. * that will be disabled. */
  10727. if (&encoder->new_crtc->base != encoder->base.crtc) {
  10728. DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
  10729. encoder->base.base.id,
  10730. encoder->base.name);
  10731. config->mode_changed = true;
  10732. }
  10733. }
  10734. /* Now we've also updated encoder->new_crtc for all encoders. */
  10735. for_each_intel_connector(dev, connector) {
  10736. connector_state =
  10737. drm_atomic_get_connector_state(state, &connector->base);
  10738. if (IS_ERR(connector_state))
  10739. return PTR_ERR(connector_state);
  10740. if (connector->new_encoder) {
  10741. if (connector->new_encoder != connector->encoder)
  10742. connector->encoder = connector->new_encoder;
  10743. } else {
  10744. connector_state->crtc = NULL;
  10745. connector_state->best_encoder = NULL;
  10746. }
  10747. }
  10748. for_each_intel_crtc(dev, crtc) {
  10749. crtc->new_enabled = false;
  10750. for_each_intel_encoder(dev, encoder) {
  10751. if (encoder->new_crtc == crtc) {
  10752. crtc->new_enabled = true;
  10753. break;
  10754. }
  10755. }
  10756. if (crtc->new_enabled != crtc->base.state->enable) {
  10757. DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
  10758. crtc->base.base.id,
  10759. crtc->new_enabled ? "en" : "dis");
  10760. config->mode_changed = true;
  10761. }
  10762. }
  10763. return 0;
  10764. }
  10765. static void disable_crtc_nofb(struct intel_crtc *crtc)
  10766. {
  10767. struct drm_device *dev = crtc->base.dev;
  10768. struct intel_encoder *encoder;
  10769. struct intel_connector *connector;
  10770. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  10771. pipe_name(crtc->pipe));
  10772. for_each_intel_connector(dev, connector) {
  10773. if (connector->new_encoder &&
  10774. connector->new_encoder->new_crtc == crtc)
  10775. connector->new_encoder = NULL;
  10776. }
  10777. for_each_intel_encoder(dev, encoder) {
  10778. if (encoder->new_crtc == crtc)
  10779. encoder->new_crtc = NULL;
  10780. }
  10781. crtc->new_enabled = false;
  10782. }
  10783. static int intel_crtc_set_config(struct drm_mode_set *set)
  10784. {
  10785. struct drm_device *dev;
  10786. struct drm_mode_set save_set;
  10787. struct drm_atomic_state *state = NULL;
  10788. struct intel_set_config *config;
  10789. struct intel_crtc_state *pipe_config;
  10790. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  10791. int ret;
  10792. BUG_ON(!set);
  10793. BUG_ON(!set->crtc);
  10794. BUG_ON(!set->crtc->helper_private);
  10795. /* Enforce sane interface api - has been abused by the fb helper. */
  10796. BUG_ON(!set->mode && set->fb);
  10797. BUG_ON(set->fb && set->num_connectors == 0);
  10798. if (set->fb) {
  10799. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  10800. set->crtc->base.id, set->fb->base.id,
  10801. (int)set->num_connectors, set->x, set->y);
  10802. } else {
  10803. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  10804. }
  10805. dev = set->crtc->dev;
  10806. ret = -ENOMEM;
  10807. config = kzalloc(sizeof(*config), GFP_KERNEL);
  10808. if (!config)
  10809. goto out_config;
  10810. ret = intel_set_config_save_state(dev, config);
  10811. if (ret)
  10812. goto out_config;
  10813. save_set.crtc = set->crtc;
  10814. save_set.mode = &set->crtc->mode;
  10815. save_set.x = set->crtc->x;
  10816. save_set.y = set->crtc->y;
  10817. save_set.fb = set->crtc->primary->fb;
  10818. /* Compute whether we need a full modeset, only an fb base update or no
  10819. * change at all. In the future we might also check whether only the
  10820. * mode changed, e.g. for LVDS where we only change the panel fitter in
  10821. * such cases. */
  10822. intel_set_config_compute_mode_changes(set, config);
  10823. state = drm_atomic_state_alloc(dev);
  10824. if (!state) {
  10825. ret = -ENOMEM;
  10826. goto out_config;
  10827. }
  10828. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10829. ret = intel_modeset_stage_output_state(dev, set, config, state);
  10830. if (ret)
  10831. goto fail;
  10832. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  10833. state,
  10834. &modeset_pipes,
  10835. &prepare_pipes,
  10836. &disable_pipes);
  10837. if (IS_ERR(pipe_config)) {
  10838. ret = PTR_ERR(pipe_config);
  10839. goto fail;
  10840. } else if (pipe_config) {
  10841. if (pipe_config->has_audio !=
  10842. to_intel_crtc(set->crtc)->config->has_audio)
  10843. config->mode_changed = true;
  10844. /*
  10845. * Note we have an issue here with infoframes: current code
  10846. * only updates them on the full mode set path per hw
  10847. * requirements. So here we should be checking for any
  10848. * required changes and forcing a mode set.
  10849. */
  10850. }
  10851. intel_update_pipe_size(to_intel_crtc(set->crtc));
  10852. if (config->mode_changed) {
  10853. ret = intel_set_mode_pipes(set->crtc, set->mode,
  10854. set->x, set->y, set->fb, pipe_config,
  10855. modeset_pipes, prepare_pipes,
  10856. disable_pipes);
  10857. } else if (config->fb_changed) {
  10858. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  10859. struct drm_plane *primary = set->crtc->primary;
  10860. struct intel_plane_state *plane_state =
  10861. to_intel_plane_state(primary->state);
  10862. bool was_visible = plane_state->visible;
  10863. int vdisplay, hdisplay;
  10864. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  10865. ret = drm_plane_helper_update(primary, set->crtc, set->fb,
  10866. 0, 0, hdisplay, vdisplay,
  10867. set->x << 16, set->y << 16,
  10868. hdisplay << 16, vdisplay << 16);
  10869. /*
  10870. * We need to make sure the primary plane is re-enabled if it
  10871. * has previously been turned off.
  10872. */
  10873. plane_state = to_intel_plane_state(primary->state);
  10874. if (ret == 0 && !was_visible && plane_state->visible) {
  10875. WARN_ON(!intel_crtc->active);
  10876. intel_post_enable_primary(set->crtc);
  10877. }
  10878. /*
  10879. * In the fastboot case this may be our only check of the
  10880. * state after boot. It would be better to only do it on
  10881. * the first update, but we don't have a nice way of doing that
  10882. * (and really, set_config isn't used much for high freq page
  10883. * flipping, so increasing its cost here shouldn't be a big
  10884. * deal).
  10885. */
  10886. if (i915.fastboot && ret == 0)
  10887. intel_modeset_check_state(set->crtc->dev);
  10888. }
  10889. if (ret) {
  10890. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  10891. set->crtc->base.id, ret);
  10892. fail:
  10893. intel_set_config_restore_state(dev, config);
  10894. drm_atomic_state_clear(state);
  10895. /*
  10896. * HACK: if the pipe was on, but we didn't have a framebuffer,
  10897. * force the pipe off to avoid oopsing in the modeset code
  10898. * due to fb==NULL. This should only happen during boot since
  10899. * we don't yet reconstruct the FB from the hardware state.
  10900. */
  10901. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  10902. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  10903. /* Try to restore the config */
  10904. if (config->mode_changed &&
  10905. intel_set_mode(save_set.crtc, save_set.mode,
  10906. save_set.x, save_set.y, save_set.fb,
  10907. state))
  10908. DRM_ERROR("failed to restore config after modeset failure\n");
  10909. }
  10910. out_config:
  10911. if (state)
  10912. drm_atomic_state_free(state);
  10913. intel_set_config_free(config);
  10914. return ret;
  10915. }
  10916. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10917. .gamma_set = intel_crtc_gamma_set,
  10918. .set_config = intel_crtc_set_config,
  10919. .destroy = intel_crtc_destroy,
  10920. .page_flip = intel_crtc_page_flip,
  10921. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10922. .atomic_destroy_state = intel_crtc_destroy_state,
  10923. };
  10924. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  10925. struct intel_shared_dpll *pll,
  10926. struct intel_dpll_hw_state *hw_state)
  10927. {
  10928. uint32_t val;
  10929. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  10930. return false;
  10931. val = I915_READ(PCH_DPLL(pll->id));
  10932. hw_state->dpll = val;
  10933. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  10934. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  10935. return val & DPLL_VCO_ENABLE;
  10936. }
  10937. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  10938. struct intel_shared_dpll *pll)
  10939. {
  10940. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  10941. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  10942. }
  10943. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  10944. struct intel_shared_dpll *pll)
  10945. {
  10946. /* PCH refclock must be enabled first */
  10947. ibx_assert_pch_refclk_enabled(dev_priv);
  10948. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10949. /* Wait for the clocks to stabilize. */
  10950. POSTING_READ(PCH_DPLL(pll->id));
  10951. udelay(150);
  10952. /* The pixel multiplier can only be updated once the
  10953. * DPLL is enabled and the clocks are stable.
  10954. *
  10955. * So write it again.
  10956. */
  10957. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10958. POSTING_READ(PCH_DPLL(pll->id));
  10959. udelay(200);
  10960. }
  10961. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  10962. struct intel_shared_dpll *pll)
  10963. {
  10964. struct drm_device *dev = dev_priv->dev;
  10965. struct intel_crtc *crtc;
  10966. /* Make sure no transcoder isn't still depending on us. */
  10967. for_each_intel_crtc(dev, crtc) {
  10968. if (intel_crtc_to_shared_dpll(crtc) == pll)
  10969. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  10970. }
  10971. I915_WRITE(PCH_DPLL(pll->id), 0);
  10972. POSTING_READ(PCH_DPLL(pll->id));
  10973. udelay(200);
  10974. }
  10975. static char *ibx_pch_dpll_names[] = {
  10976. "PCH DPLL A",
  10977. "PCH DPLL B",
  10978. };
  10979. static void ibx_pch_dpll_init(struct drm_device *dev)
  10980. {
  10981. struct drm_i915_private *dev_priv = dev->dev_private;
  10982. int i;
  10983. dev_priv->num_shared_dpll = 2;
  10984. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10985. dev_priv->shared_dplls[i].id = i;
  10986. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  10987. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  10988. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  10989. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  10990. dev_priv->shared_dplls[i].get_hw_state =
  10991. ibx_pch_dpll_get_hw_state;
  10992. }
  10993. }
  10994. static void intel_shared_dpll_init(struct drm_device *dev)
  10995. {
  10996. struct drm_i915_private *dev_priv = dev->dev_private;
  10997. if (HAS_DDI(dev))
  10998. intel_ddi_pll_init(dev);
  10999. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11000. ibx_pch_dpll_init(dev);
  11001. else
  11002. dev_priv->num_shared_dpll = 0;
  11003. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11004. }
  11005. /**
  11006. * intel_wm_need_update - Check whether watermarks need updating
  11007. * @plane: drm plane
  11008. * @state: new plane state
  11009. *
  11010. * Check current plane state versus the new one to determine whether
  11011. * watermarks need to be recalculated.
  11012. *
  11013. * Returns true or false.
  11014. */
  11015. bool intel_wm_need_update(struct drm_plane *plane,
  11016. struct drm_plane_state *state)
  11017. {
  11018. /* Update watermarks on tiling changes. */
  11019. if (!plane->state->fb || !state->fb ||
  11020. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  11021. plane->state->rotation != state->rotation)
  11022. return true;
  11023. return false;
  11024. }
  11025. /**
  11026. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11027. * @plane: drm plane to prepare for
  11028. * @fb: framebuffer to prepare for presentation
  11029. *
  11030. * Prepares a framebuffer for usage on a display plane. Generally this
  11031. * involves pinning the underlying object and updating the frontbuffer tracking
  11032. * bits. Some older platforms need special physical address handling for
  11033. * cursor planes.
  11034. *
  11035. * Returns 0 on success, negative error code on failure.
  11036. */
  11037. int
  11038. intel_prepare_plane_fb(struct drm_plane *plane,
  11039. struct drm_framebuffer *fb,
  11040. const struct drm_plane_state *new_state)
  11041. {
  11042. struct drm_device *dev = plane->dev;
  11043. struct intel_plane *intel_plane = to_intel_plane(plane);
  11044. enum pipe pipe = intel_plane->pipe;
  11045. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11046. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11047. unsigned frontbuffer_bits = 0;
  11048. int ret = 0;
  11049. if (!obj)
  11050. return 0;
  11051. switch (plane->type) {
  11052. case DRM_PLANE_TYPE_PRIMARY:
  11053. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11054. break;
  11055. case DRM_PLANE_TYPE_CURSOR:
  11056. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  11057. break;
  11058. case DRM_PLANE_TYPE_OVERLAY:
  11059. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  11060. break;
  11061. }
  11062. mutex_lock(&dev->struct_mutex);
  11063. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11064. INTEL_INFO(dev)->cursor_needs_physical) {
  11065. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11066. ret = i915_gem_object_attach_phys(obj, align);
  11067. if (ret)
  11068. DRM_DEBUG_KMS("failed to attach phys object\n");
  11069. } else {
  11070. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  11071. }
  11072. if (ret == 0)
  11073. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  11074. mutex_unlock(&dev->struct_mutex);
  11075. return ret;
  11076. }
  11077. /**
  11078. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11079. * @plane: drm plane to clean up for
  11080. * @fb: old framebuffer that was on plane
  11081. *
  11082. * Cleans up a framebuffer that has just been removed from a plane.
  11083. */
  11084. void
  11085. intel_cleanup_plane_fb(struct drm_plane *plane,
  11086. struct drm_framebuffer *fb,
  11087. const struct drm_plane_state *old_state)
  11088. {
  11089. struct drm_device *dev = plane->dev;
  11090. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11091. if (WARN_ON(!obj))
  11092. return;
  11093. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11094. !INTEL_INFO(dev)->cursor_needs_physical) {
  11095. mutex_lock(&dev->struct_mutex);
  11096. intel_unpin_fb_obj(fb, old_state);
  11097. mutex_unlock(&dev->struct_mutex);
  11098. }
  11099. }
  11100. int
  11101. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11102. {
  11103. int max_scale;
  11104. struct drm_device *dev;
  11105. struct drm_i915_private *dev_priv;
  11106. int crtc_clock, cdclk;
  11107. if (!intel_crtc || !crtc_state)
  11108. return DRM_PLANE_HELPER_NO_SCALING;
  11109. dev = intel_crtc->base.dev;
  11110. dev_priv = dev->dev_private;
  11111. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11112. cdclk = dev_priv->display.get_display_clock_speed(dev);
  11113. if (!crtc_clock || !cdclk)
  11114. return DRM_PLANE_HELPER_NO_SCALING;
  11115. /*
  11116. * skl max scale is lower of:
  11117. * close to 3 but not 3, -1 is for that purpose
  11118. * or
  11119. * cdclk/crtc_clock
  11120. */
  11121. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11122. return max_scale;
  11123. }
  11124. static int
  11125. intel_check_primary_plane(struct drm_plane *plane,
  11126. struct intel_plane_state *state)
  11127. {
  11128. struct drm_device *dev = plane->dev;
  11129. struct drm_i915_private *dev_priv = dev->dev_private;
  11130. struct drm_crtc *crtc = state->base.crtc;
  11131. struct intel_crtc *intel_crtc;
  11132. struct intel_crtc_state *crtc_state;
  11133. struct drm_framebuffer *fb = state->base.fb;
  11134. struct drm_rect *dest = &state->dst;
  11135. struct drm_rect *src = &state->src;
  11136. const struct drm_rect *clip = &state->clip;
  11137. bool can_position = false;
  11138. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11139. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11140. int ret;
  11141. crtc = crtc ? crtc : plane->crtc;
  11142. intel_crtc = to_intel_crtc(crtc);
  11143. crtc_state = state->base.state ?
  11144. intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
  11145. if (INTEL_INFO(dev)->gen >= 9) {
  11146. min_scale = 1;
  11147. max_scale = skl_max_scale(intel_crtc, crtc_state);
  11148. can_position = true;
  11149. }
  11150. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11151. src, dest, clip,
  11152. min_scale,
  11153. max_scale,
  11154. can_position, true,
  11155. &state->visible);
  11156. if (ret)
  11157. return ret;
  11158. if (intel_crtc->active) {
  11159. struct intel_plane_state *old_state =
  11160. to_intel_plane_state(plane->state);
  11161. intel_crtc->atomic.wait_for_flips = true;
  11162. /*
  11163. * FBC does not work on some platforms for rotated
  11164. * planes, so disable it when rotation is not 0 and
  11165. * update it when rotation is set back to 0.
  11166. *
  11167. * FIXME: This is redundant with the fbc update done in
  11168. * the primary plane enable function except that that
  11169. * one is done too late. We eventually need to unify
  11170. * this.
  11171. */
  11172. if (state->visible &&
  11173. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  11174. dev_priv->fbc.crtc == intel_crtc &&
  11175. state->base.rotation != BIT(DRM_ROTATE_0)) {
  11176. intel_crtc->atomic.disable_fbc = true;
  11177. }
  11178. if (state->visible && !old_state->visible) {
  11179. /*
  11180. * BDW signals flip done immediately if the plane
  11181. * is disabled, even if the plane enable is already
  11182. * armed to occur at the next vblank :(
  11183. */
  11184. if (IS_BROADWELL(dev))
  11185. intel_crtc->atomic.wait_vblank = true;
  11186. }
  11187. intel_crtc->atomic.fb_bits |=
  11188. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  11189. intel_crtc->atomic.update_fbc = true;
  11190. if (intel_wm_need_update(plane, &state->base))
  11191. intel_crtc->atomic.update_wm = true;
  11192. }
  11193. if (INTEL_INFO(dev)->gen >= 9) {
  11194. ret = skl_update_scaler_users(intel_crtc, crtc_state,
  11195. to_intel_plane(plane), state, 0);
  11196. if (ret)
  11197. return ret;
  11198. }
  11199. return 0;
  11200. }
  11201. static void
  11202. intel_commit_primary_plane(struct drm_plane *plane,
  11203. struct intel_plane_state *state)
  11204. {
  11205. struct drm_crtc *crtc = state->base.crtc;
  11206. struct drm_framebuffer *fb = state->base.fb;
  11207. struct drm_device *dev = plane->dev;
  11208. struct drm_i915_private *dev_priv = dev->dev_private;
  11209. struct intel_crtc *intel_crtc;
  11210. struct drm_rect *src = &state->src;
  11211. crtc = crtc ? crtc : plane->crtc;
  11212. intel_crtc = to_intel_crtc(crtc);
  11213. plane->fb = fb;
  11214. crtc->x = src->x1 >> 16;
  11215. crtc->y = src->y1 >> 16;
  11216. if (intel_crtc->active) {
  11217. if (state->visible)
  11218. /* FIXME: kill this fastboot hack */
  11219. intel_update_pipe_size(intel_crtc);
  11220. dev_priv->display.update_primary_plane(crtc, plane->fb,
  11221. crtc->x, crtc->y);
  11222. }
  11223. }
  11224. static void
  11225. intel_disable_primary_plane(struct drm_plane *plane,
  11226. struct drm_crtc *crtc,
  11227. bool force)
  11228. {
  11229. struct drm_device *dev = plane->dev;
  11230. struct drm_i915_private *dev_priv = dev->dev_private;
  11231. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11232. }
  11233. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11234. {
  11235. struct drm_device *dev = crtc->dev;
  11236. struct drm_i915_private *dev_priv = dev->dev_private;
  11237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11238. struct intel_plane *intel_plane;
  11239. struct drm_plane *p;
  11240. unsigned fb_bits = 0;
  11241. /* Track fb's for any planes being disabled */
  11242. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  11243. intel_plane = to_intel_plane(p);
  11244. if (intel_crtc->atomic.disabled_planes &
  11245. (1 << drm_plane_index(p))) {
  11246. switch (p->type) {
  11247. case DRM_PLANE_TYPE_PRIMARY:
  11248. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  11249. break;
  11250. case DRM_PLANE_TYPE_CURSOR:
  11251. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  11252. break;
  11253. case DRM_PLANE_TYPE_OVERLAY:
  11254. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  11255. break;
  11256. }
  11257. mutex_lock(&dev->struct_mutex);
  11258. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  11259. mutex_unlock(&dev->struct_mutex);
  11260. }
  11261. }
  11262. if (intel_crtc->atomic.wait_for_flips)
  11263. intel_crtc_wait_for_pending_flips(crtc);
  11264. if (intel_crtc->atomic.disable_fbc)
  11265. intel_fbc_disable(dev);
  11266. if (intel_crtc->atomic.pre_disable_primary)
  11267. intel_pre_disable_primary(crtc);
  11268. if (intel_crtc->atomic.update_wm)
  11269. intel_update_watermarks(crtc);
  11270. intel_runtime_pm_get(dev_priv);
  11271. /* Perform vblank evasion around commit operation */
  11272. if (intel_crtc->active)
  11273. intel_crtc->atomic.evade =
  11274. intel_pipe_update_start(intel_crtc,
  11275. &intel_crtc->atomic.start_vbl_count);
  11276. }
  11277. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11278. {
  11279. struct drm_device *dev = crtc->dev;
  11280. struct drm_i915_private *dev_priv = dev->dev_private;
  11281. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11282. struct drm_plane *p;
  11283. if (intel_crtc->atomic.evade)
  11284. intel_pipe_update_end(intel_crtc,
  11285. intel_crtc->atomic.start_vbl_count);
  11286. intel_runtime_pm_put(dev_priv);
  11287. if (intel_crtc->atomic.wait_vblank)
  11288. intel_wait_for_vblank(dev, intel_crtc->pipe);
  11289. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  11290. if (intel_crtc->atomic.update_fbc) {
  11291. mutex_lock(&dev->struct_mutex);
  11292. intel_fbc_update(dev);
  11293. mutex_unlock(&dev->struct_mutex);
  11294. }
  11295. if (intel_crtc->atomic.post_enable_primary)
  11296. intel_post_enable_primary(crtc);
  11297. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  11298. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  11299. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  11300. false, false);
  11301. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  11302. }
  11303. /**
  11304. * intel_plane_destroy - destroy a plane
  11305. * @plane: plane to destroy
  11306. *
  11307. * Common destruction function for all types of planes (primary, cursor,
  11308. * sprite).
  11309. */
  11310. void intel_plane_destroy(struct drm_plane *plane)
  11311. {
  11312. struct intel_plane *intel_plane = to_intel_plane(plane);
  11313. drm_plane_cleanup(plane);
  11314. kfree(intel_plane);
  11315. }
  11316. const struct drm_plane_funcs intel_plane_funcs = {
  11317. .update_plane = drm_atomic_helper_update_plane,
  11318. .disable_plane = drm_atomic_helper_disable_plane,
  11319. .destroy = intel_plane_destroy,
  11320. .set_property = drm_atomic_helper_plane_set_property,
  11321. .atomic_get_property = intel_plane_atomic_get_property,
  11322. .atomic_set_property = intel_plane_atomic_set_property,
  11323. .atomic_duplicate_state = intel_plane_duplicate_state,
  11324. .atomic_destroy_state = intel_plane_destroy_state,
  11325. };
  11326. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11327. int pipe)
  11328. {
  11329. struct intel_plane *primary;
  11330. struct intel_plane_state *state;
  11331. const uint32_t *intel_primary_formats;
  11332. int num_formats;
  11333. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11334. if (primary == NULL)
  11335. return NULL;
  11336. state = intel_create_plane_state(&primary->base);
  11337. if (!state) {
  11338. kfree(primary);
  11339. return NULL;
  11340. }
  11341. primary->base.state = &state->base;
  11342. primary->can_scale = false;
  11343. primary->max_downscale = 1;
  11344. if (INTEL_INFO(dev)->gen >= 9) {
  11345. primary->can_scale = true;
  11346. }
  11347. state->scaler_id = -1;
  11348. primary->pipe = pipe;
  11349. primary->plane = pipe;
  11350. primary->check_plane = intel_check_primary_plane;
  11351. primary->commit_plane = intel_commit_primary_plane;
  11352. primary->disable_plane = intel_disable_primary_plane;
  11353. primary->ckey.flags = I915_SET_COLORKEY_NONE;
  11354. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11355. primary->plane = !pipe;
  11356. if (INTEL_INFO(dev)->gen <= 3) {
  11357. intel_primary_formats = intel_primary_formats_gen2;
  11358. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  11359. } else {
  11360. intel_primary_formats = intel_primary_formats_gen4;
  11361. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  11362. }
  11363. drm_universal_plane_init(dev, &primary->base, 0,
  11364. &intel_plane_funcs,
  11365. intel_primary_formats, num_formats,
  11366. DRM_PLANE_TYPE_PRIMARY);
  11367. if (INTEL_INFO(dev)->gen >= 4)
  11368. intel_create_rotation_property(dev, primary);
  11369. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11370. return &primary->base;
  11371. }
  11372. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11373. {
  11374. if (!dev->mode_config.rotation_property) {
  11375. unsigned long flags = BIT(DRM_ROTATE_0) |
  11376. BIT(DRM_ROTATE_180);
  11377. if (INTEL_INFO(dev)->gen >= 9)
  11378. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11379. dev->mode_config.rotation_property =
  11380. drm_mode_create_rotation_property(dev, flags);
  11381. }
  11382. if (dev->mode_config.rotation_property)
  11383. drm_object_attach_property(&plane->base.base,
  11384. dev->mode_config.rotation_property,
  11385. plane->base.state->rotation);
  11386. }
  11387. static int
  11388. intel_check_cursor_plane(struct drm_plane *plane,
  11389. struct intel_plane_state *state)
  11390. {
  11391. struct drm_crtc *crtc = state->base.crtc;
  11392. struct drm_device *dev = plane->dev;
  11393. struct drm_framebuffer *fb = state->base.fb;
  11394. struct drm_rect *dest = &state->dst;
  11395. struct drm_rect *src = &state->src;
  11396. const struct drm_rect *clip = &state->clip;
  11397. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11398. struct intel_crtc *intel_crtc;
  11399. unsigned stride;
  11400. int ret;
  11401. crtc = crtc ? crtc : plane->crtc;
  11402. intel_crtc = to_intel_crtc(crtc);
  11403. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11404. src, dest, clip,
  11405. DRM_PLANE_HELPER_NO_SCALING,
  11406. DRM_PLANE_HELPER_NO_SCALING,
  11407. true, true, &state->visible);
  11408. if (ret)
  11409. return ret;
  11410. /* if we want to turn off the cursor ignore width and height */
  11411. if (!obj)
  11412. goto finish;
  11413. /* Check for which cursor types we support */
  11414. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  11415. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11416. state->base.crtc_w, state->base.crtc_h);
  11417. return -EINVAL;
  11418. }
  11419. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11420. if (obj->base.size < stride * state->base.crtc_h) {
  11421. DRM_DEBUG_KMS("buffer is too small\n");
  11422. return -ENOMEM;
  11423. }
  11424. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11425. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11426. ret = -EINVAL;
  11427. }
  11428. finish:
  11429. if (intel_crtc->active) {
  11430. if (plane->state->crtc_w != state->base.crtc_w)
  11431. intel_crtc->atomic.update_wm = true;
  11432. intel_crtc->atomic.fb_bits |=
  11433. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  11434. }
  11435. return ret;
  11436. }
  11437. static void
  11438. intel_disable_cursor_plane(struct drm_plane *plane,
  11439. struct drm_crtc *crtc,
  11440. bool force)
  11441. {
  11442. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11443. if (!force) {
  11444. plane->fb = NULL;
  11445. intel_crtc->cursor_bo = NULL;
  11446. intel_crtc->cursor_addr = 0;
  11447. }
  11448. intel_crtc_update_cursor(crtc, false);
  11449. }
  11450. static void
  11451. intel_commit_cursor_plane(struct drm_plane *plane,
  11452. struct intel_plane_state *state)
  11453. {
  11454. struct drm_crtc *crtc = state->base.crtc;
  11455. struct drm_device *dev = plane->dev;
  11456. struct intel_crtc *intel_crtc;
  11457. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11458. uint32_t addr;
  11459. crtc = crtc ? crtc : plane->crtc;
  11460. intel_crtc = to_intel_crtc(crtc);
  11461. plane->fb = state->base.fb;
  11462. crtc->cursor_x = state->base.crtc_x;
  11463. crtc->cursor_y = state->base.crtc_y;
  11464. if (intel_crtc->cursor_bo == obj)
  11465. goto update;
  11466. if (!obj)
  11467. addr = 0;
  11468. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11469. addr = i915_gem_obj_ggtt_offset(obj);
  11470. else
  11471. addr = obj->phys_handle->busaddr;
  11472. intel_crtc->cursor_addr = addr;
  11473. intel_crtc->cursor_bo = obj;
  11474. update:
  11475. if (intel_crtc->active)
  11476. intel_crtc_update_cursor(crtc, state->visible);
  11477. }
  11478. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11479. int pipe)
  11480. {
  11481. struct intel_plane *cursor;
  11482. struct intel_plane_state *state;
  11483. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11484. if (cursor == NULL)
  11485. return NULL;
  11486. state = intel_create_plane_state(&cursor->base);
  11487. if (!state) {
  11488. kfree(cursor);
  11489. return NULL;
  11490. }
  11491. cursor->base.state = &state->base;
  11492. cursor->can_scale = false;
  11493. cursor->max_downscale = 1;
  11494. cursor->pipe = pipe;
  11495. cursor->plane = pipe;
  11496. state->scaler_id = -1;
  11497. cursor->check_plane = intel_check_cursor_plane;
  11498. cursor->commit_plane = intel_commit_cursor_plane;
  11499. cursor->disable_plane = intel_disable_cursor_plane;
  11500. drm_universal_plane_init(dev, &cursor->base, 0,
  11501. &intel_plane_funcs,
  11502. intel_cursor_formats,
  11503. ARRAY_SIZE(intel_cursor_formats),
  11504. DRM_PLANE_TYPE_CURSOR);
  11505. if (INTEL_INFO(dev)->gen >= 4) {
  11506. if (!dev->mode_config.rotation_property)
  11507. dev->mode_config.rotation_property =
  11508. drm_mode_create_rotation_property(dev,
  11509. BIT(DRM_ROTATE_0) |
  11510. BIT(DRM_ROTATE_180));
  11511. if (dev->mode_config.rotation_property)
  11512. drm_object_attach_property(&cursor->base.base,
  11513. dev->mode_config.rotation_property,
  11514. state->base.rotation);
  11515. }
  11516. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11517. return &cursor->base;
  11518. }
  11519. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11520. struct intel_crtc_state *crtc_state)
  11521. {
  11522. int i;
  11523. struct intel_scaler *intel_scaler;
  11524. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11525. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11526. intel_scaler = &scaler_state->scalers[i];
  11527. intel_scaler->in_use = 0;
  11528. intel_scaler->id = i;
  11529. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11530. }
  11531. scaler_state->scaler_id = -1;
  11532. }
  11533. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11534. {
  11535. struct drm_i915_private *dev_priv = dev->dev_private;
  11536. struct intel_crtc *intel_crtc;
  11537. struct intel_crtc_state *crtc_state = NULL;
  11538. struct drm_plane *primary = NULL;
  11539. struct drm_plane *cursor = NULL;
  11540. int i, ret;
  11541. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11542. if (intel_crtc == NULL)
  11543. return;
  11544. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11545. if (!crtc_state)
  11546. goto fail;
  11547. intel_crtc_set_state(intel_crtc, crtc_state);
  11548. crtc_state->base.crtc = &intel_crtc->base;
  11549. /* initialize shared scalers */
  11550. if (INTEL_INFO(dev)->gen >= 9) {
  11551. if (pipe == PIPE_C)
  11552. intel_crtc->num_scalers = 1;
  11553. else
  11554. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11555. skl_init_scalers(dev, intel_crtc, crtc_state);
  11556. }
  11557. primary = intel_primary_plane_create(dev, pipe);
  11558. if (!primary)
  11559. goto fail;
  11560. cursor = intel_cursor_plane_create(dev, pipe);
  11561. if (!cursor)
  11562. goto fail;
  11563. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11564. cursor, &intel_crtc_funcs);
  11565. if (ret)
  11566. goto fail;
  11567. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11568. for (i = 0; i < 256; i++) {
  11569. intel_crtc->lut_r[i] = i;
  11570. intel_crtc->lut_g[i] = i;
  11571. intel_crtc->lut_b[i] = i;
  11572. }
  11573. /*
  11574. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11575. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11576. */
  11577. intel_crtc->pipe = pipe;
  11578. intel_crtc->plane = pipe;
  11579. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11580. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11581. intel_crtc->plane = !pipe;
  11582. }
  11583. intel_crtc->cursor_base = ~0;
  11584. intel_crtc->cursor_cntl = ~0;
  11585. intel_crtc->cursor_size = ~0;
  11586. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11587. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11588. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11589. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11590. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  11591. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11592. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11593. return;
  11594. fail:
  11595. if (primary)
  11596. drm_plane_cleanup(primary);
  11597. if (cursor)
  11598. drm_plane_cleanup(cursor);
  11599. kfree(crtc_state);
  11600. kfree(intel_crtc);
  11601. }
  11602. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11603. {
  11604. struct drm_encoder *encoder = connector->base.encoder;
  11605. struct drm_device *dev = connector->base.dev;
  11606. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11607. if (!encoder || WARN_ON(!encoder->crtc))
  11608. return INVALID_PIPE;
  11609. return to_intel_crtc(encoder->crtc)->pipe;
  11610. }
  11611. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11612. struct drm_file *file)
  11613. {
  11614. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11615. struct drm_crtc *drmmode_crtc;
  11616. struct intel_crtc *crtc;
  11617. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11618. if (!drmmode_crtc) {
  11619. DRM_ERROR("no such CRTC id\n");
  11620. return -ENOENT;
  11621. }
  11622. crtc = to_intel_crtc(drmmode_crtc);
  11623. pipe_from_crtc_id->pipe = crtc->pipe;
  11624. return 0;
  11625. }
  11626. static int intel_encoder_clones(struct intel_encoder *encoder)
  11627. {
  11628. struct drm_device *dev = encoder->base.dev;
  11629. struct intel_encoder *source_encoder;
  11630. int index_mask = 0;
  11631. int entry = 0;
  11632. for_each_intel_encoder(dev, source_encoder) {
  11633. if (encoders_cloneable(encoder, source_encoder))
  11634. index_mask |= (1 << entry);
  11635. entry++;
  11636. }
  11637. return index_mask;
  11638. }
  11639. static bool has_edp_a(struct drm_device *dev)
  11640. {
  11641. struct drm_i915_private *dev_priv = dev->dev_private;
  11642. if (!IS_MOBILE(dev))
  11643. return false;
  11644. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11645. return false;
  11646. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11647. return false;
  11648. return true;
  11649. }
  11650. static bool intel_crt_present(struct drm_device *dev)
  11651. {
  11652. struct drm_i915_private *dev_priv = dev->dev_private;
  11653. if (INTEL_INFO(dev)->gen >= 9)
  11654. return false;
  11655. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11656. return false;
  11657. if (IS_CHERRYVIEW(dev))
  11658. return false;
  11659. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11660. return false;
  11661. return true;
  11662. }
  11663. static void intel_setup_outputs(struct drm_device *dev)
  11664. {
  11665. struct drm_i915_private *dev_priv = dev->dev_private;
  11666. struct intel_encoder *encoder;
  11667. bool dpd_is_edp = false;
  11668. intel_lvds_init(dev);
  11669. if (intel_crt_present(dev))
  11670. intel_crt_init(dev);
  11671. if (IS_BROXTON(dev)) {
  11672. /*
  11673. * FIXME: Broxton doesn't support port detection via the
  11674. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11675. * detect the ports.
  11676. */
  11677. intel_ddi_init(dev, PORT_A);
  11678. intel_ddi_init(dev, PORT_B);
  11679. intel_ddi_init(dev, PORT_C);
  11680. } else if (HAS_DDI(dev)) {
  11681. int found;
  11682. /*
  11683. * Haswell uses DDI functions to detect digital outputs.
  11684. * On SKL pre-D0 the strap isn't connected, so we assume
  11685. * it's there.
  11686. */
  11687. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11688. /* WaIgnoreDDIAStrap: skl */
  11689. if (found ||
  11690. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11691. intel_ddi_init(dev, PORT_A);
  11692. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11693. * register */
  11694. found = I915_READ(SFUSE_STRAP);
  11695. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11696. intel_ddi_init(dev, PORT_B);
  11697. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11698. intel_ddi_init(dev, PORT_C);
  11699. if (found & SFUSE_STRAP_DDID_DETECTED)
  11700. intel_ddi_init(dev, PORT_D);
  11701. } else if (HAS_PCH_SPLIT(dev)) {
  11702. int found;
  11703. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11704. if (has_edp_a(dev))
  11705. intel_dp_init(dev, DP_A, PORT_A);
  11706. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11707. /* PCH SDVOB multiplex with HDMIB */
  11708. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11709. if (!found)
  11710. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11711. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11712. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11713. }
  11714. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11715. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11716. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11717. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11718. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11719. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11720. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11721. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11722. } else if (IS_VALLEYVIEW(dev)) {
  11723. /*
  11724. * The DP_DETECTED bit is the latched state of the DDC
  11725. * SDA pin at boot. However since eDP doesn't require DDC
  11726. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11727. * eDP ports may have been muxed to an alternate function.
  11728. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11729. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11730. * detect eDP ports.
  11731. */
  11732. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11733. !intel_dp_is_edp(dev, PORT_B))
  11734. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11735. PORT_B);
  11736. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11737. intel_dp_is_edp(dev, PORT_B))
  11738. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11739. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11740. !intel_dp_is_edp(dev, PORT_C))
  11741. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11742. PORT_C);
  11743. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11744. intel_dp_is_edp(dev, PORT_C))
  11745. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11746. if (IS_CHERRYVIEW(dev)) {
  11747. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11748. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11749. PORT_D);
  11750. /* eDP not supported on port D, so don't check VBT */
  11751. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11752. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11753. }
  11754. intel_dsi_init(dev);
  11755. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  11756. bool found = false;
  11757. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11758. DRM_DEBUG_KMS("probing SDVOB\n");
  11759. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11760. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  11761. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11762. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11763. }
  11764. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  11765. intel_dp_init(dev, DP_B, PORT_B);
  11766. }
  11767. /* Before G4X SDVOC doesn't have its own detect register */
  11768. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11769. DRM_DEBUG_KMS("probing SDVOC\n");
  11770. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11771. }
  11772. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11773. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  11774. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11775. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11776. }
  11777. if (SUPPORTS_INTEGRATED_DP(dev))
  11778. intel_dp_init(dev, DP_C, PORT_C);
  11779. }
  11780. if (SUPPORTS_INTEGRATED_DP(dev) &&
  11781. (I915_READ(DP_D) & DP_DETECTED))
  11782. intel_dp_init(dev, DP_D, PORT_D);
  11783. } else if (IS_GEN2(dev))
  11784. intel_dvo_init(dev);
  11785. if (SUPPORTS_TV(dev))
  11786. intel_tv_init(dev);
  11787. intel_psr_init(dev);
  11788. for_each_intel_encoder(dev, encoder) {
  11789. encoder->base.possible_crtcs = encoder->crtc_mask;
  11790. encoder->base.possible_clones =
  11791. intel_encoder_clones(encoder);
  11792. }
  11793. intel_init_pch_refclk(dev);
  11794. drm_helper_move_panel_connectors_to_head(dev);
  11795. }
  11796. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11797. {
  11798. struct drm_device *dev = fb->dev;
  11799. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11800. drm_framebuffer_cleanup(fb);
  11801. mutex_lock(&dev->struct_mutex);
  11802. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11803. drm_gem_object_unreference(&intel_fb->obj->base);
  11804. mutex_unlock(&dev->struct_mutex);
  11805. kfree(intel_fb);
  11806. }
  11807. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11808. struct drm_file *file,
  11809. unsigned int *handle)
  11810. {
  11811. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11812. struct drm_i915_gem_object *obj = intel_fb->obj;
  11813. return drm_gem_handle_create(file, &obj->base, handle);
  11814. }
  11815. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11816. .destroy = intel_user_framebuffer_destroy,
  11817. .create_handle = intel_user_framebuffer_create_handle,
  11818. };
  11819. static
  11820. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11821. uint32_t pixel_format)
  11822. {
  11823. u32 gen = INTEL_INFO(dev)->gen;
  11824. if (gen >= 9) {
  11825. /* "The stride in bytes must not exceed the of the size of 8K
  11826. * pixels and 32K bytes."
  11827. */
  11828. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11829. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11830. return 32*1024;
  11831. } else if (gen >= 4) {
  11832. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11833. return 16*1024;
  11834. else
  11835. return 32*1024;
  11836. } else if (gen >= 3) {
  11837. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11838. return 8*1024;
  11839. else
  11840. return 16*1024;
  11841. } else {
  11842. /* XXX DSPC is limited to 4k tiled */
  11843. return 8*1024;
  11844. }
  11845. }
  11846. static int intel_framebuffer_init(struct drm_device *dev,
  11847. struct intel_framebuffer *intel_fb,
  11848. struct drm_mode_fb_cmd2 *mode_cmd,
  11849. struct drm_i915_gem_object *obj)
  11850. {
  11851. unsigned int aligned_height;
  11852. int ret;
  11853. u32 pitch_limit, stride_alignment;
  11854. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11855. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11856. /* Enforce that fb modifier and tiling mode match, but only for
  11857. * X-tiled. This is needed for FBC. */
  11858. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11859. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11860. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11861. return -EINVAL;
  11862. }
  11863. } else {
  11864. if (obj->tiling_mode == I915_TILING_X)
  11865. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11866. else if (obj->tiling_mode == I915_TILING_Y) {
  11867. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11868. return -EINVAL;
  11869. }
  11870. }
  11871. /* Passed in modifier sanity checking. */
  11872. switch (mode_cmd->modifier[0]) {
  11873. case I915_FORMAT_MOD_Y_TILED:
  11874. case I915_FORMAT_MOD_Yf_TILED:
  11875. if (INTEL_INFO(dev)->gen < 9) {
  11876. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11877. mode_cmd->modifier[0]);
  11878. return -EINVAL;
  11879. }
  11880. case DRM_FORMAT_MOD_NONE:
  11881. case I915_FORMAT_MOD_X_TILED:
  11882. break;
  11883. default:
  11884. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11885. mode_cmd->modifier[0]);
  11886. return -EINVAL;
  11887. }
  11888. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11889. mode_cmd->pixel_format);
  11890. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11891. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11892. mode_cmd->pitches[0], stride_alignment);
  11893. return -EINVAL;
  11894. }
  11895. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11896. mode_cmd->pixel_format);
  11897. if (mode_cmd->pitches[0] > pitch_limit) {
  11898. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11899. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11900. "tiled" : "linear",
  11901. mode_cmd->pitches[0], pitch_limit);
  11902. return -EINVAL;
  11903. }
  11904. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11905. mode_cmd->pitches[0] != obj->stride) {
  11906. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11907. mode_cmd->pitches[0], obj->stride);
  11908. return -EINVAL;
  11909. }
  11910. /* Reject formats not supported by any plane early. */
  11911. switch (mode_cmd->pixel_format) {
  11912. case DRM_FORMAT_C8:
  11913. case DRM_FORMAT_RGB565:
  11914. case DRM_FORMAT_XRGB8888:
  11915. case DRM_FORMAT_ARGB8888:
  11916. break;
  11917. case DRM_FORMAT_XRGB1555:
  11918. case DRM_FORMAT_ARGB1555:
  11919. if (INTEL_INFO(dev)->gen > 3) {
  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_XBGR8888:
  11926. case DRM_FORMAT_ABGR8888:
  11927. case DRM_FORMAT_XRGB2101010:
  11928. case DRM_FORMAT_ARGB2101010:
  11929. case DRM_FORMAT_XBGR2101010:
  11930. case DRM_FORMAT_ABGR2101010:
  11931. if (INTEL_INFO(dev)->gen < 4) {
  11932. DRM_DEBUG("unsupported pixel format: %s\n",
  11933. drm_get_format_name(mode_cmd->pixel_format));
  11934. return -EINVAL;
  11935. }
  11936. break;
  11937. case DRM_FORMAT_YUYV:
  11938. case DRM_FORMAT_UYVY:
  11939. case DRM_FORMAT_YVYU:
  11940. case DRM_FORMAT_VYUY:
  11941. if (INTEL_INFO(dev)->gen < 5) {
  11942. DRM_DEBUG("unsupported pixel format: %s\n",
  11943. drm_get_format_name(mode_cmd->pixel_format));
  11944. return -EINVAL;
  11945. }
  11946. break;
  11947. default:
  11948. DRM_DEBUG("unsupported pixel format: %s\n",
  11949. drm_get_format_name(mode_cmd->pixel_format));
  11950. return -EINVAL;
  11951. }
  11952. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11953. if (mode_cmd->offsets[0] != 0)
  11954. return -EINVAL;
  11955. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11956. mode_cmd->pixel_format,
  11957. mode_cmd->modifier[0]);
  11958. /* FIXME drm helper for size checks (especially planar formats)? */
  11959. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11960. return -EINVAL;
  11961. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11962. intel_fb->obj = obj;
  11963. intel_fb->obj->framebuffer_references++;
  11964. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11965. if (ret) {
  11966. DRM_ERROR("framebuffer init failed %d\n", ret);
  11967. return ret;
  11968. }
  11969. return 0;
  11970. }
  11971. static struct drm_framebuffer *
  11972. intel_user_framebuffer_create(struct drm_device *dev,
  11973. struct drm_file *filp,
  11974. struct drm_mode_fb_cmd2 *mode_cmd)
  11975. {
  11976. struct drm_i915_gem_object *obj;
  11977. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11978. mode_cmd->handles[0]));
  11979. if (&obj->base == NULL)
  11980. return ERR_PTR(-ENOENT);
  11981. return intel_framebuffer_create(dev, mode_cmd, obj);
  11982. }
  11983. #ifndef CONFIG_DRM_I915_FBDEV
  11984. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  11985. {
  11986. }
  11987. #endif
  11988. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11989. .fb_create = intel_user_framebuffer_create,
  11990. .output_poll_changed = intel_fbdev_output_poll_changed,
  11991. .atomic_check = intel_atomic_check,
  11992. .atomic_commit = intel_atomic_commit,
  11993. };
  11994. /* Set up chip specific display functions */
  11995. static void intel_init_display(struct drm_device *dev)
  11996. {
  11997. struct drm_i915_private *dev_priv = dev->dev_private;
  11998. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  11999. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12000. else if (IS_CHERRYVIEW(dev))
  12001. dev_priv->display.find_dpll = chv_find_best_dpll;
  12002. else if (IS_VALLEYVIEW(dev))
  12003. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12004. else if (IS_PINEVIEW(dev))
  12005. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12006. else
  12007. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12008. if (INTEL_INFO(dev)->gen >= 9) {
  12009. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12010. dev_priv->display.get_initial_plane_config =
  12011. skylake_get_initial_plane_config;
  12012. dev_priv->display.crtc_compute_clock =
  12013. haswell_crtc_compute_clock;
  12014. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12015. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12016. dev_priv->display.off = ironlake_crtc_off;
  12017. dev_priv->display.update_primary_plane =
  12018. skylake_update_primary_plane;
  12019. } else if (HAS_DDI(dev)) {
  12020. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12021. dev_priv->display.get_initial_plane_config =
  12022. ironlake_get_initial_plane_config;
  12023. dev_priv->display.crtc_compute_clock =
  12024. haswell_crtc_compute_clock;
  12025. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12026. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12027. dev_priv->display.off = ironlake_crtc_off;
  12028. dev_priv->display.update_primary_plane =
  12029. ironlake_update_primary_plane;
  12030. } else if (HAS_PCH_SPLIT(dev)) {
  12031. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12032. dev_priv->display.get_initial_plane_config =
  12033. ironlake_get_initial_plane_config;
  12034. dev_priv->display.crtc_compute_clock =
  12035. ironlake_crtc_compute_clock;
  12036. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12037. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12038. dev_priv->display.off = ironlake_crtc_off;
  12039. dev_priv->display.update_primary_plane =
  12040. ironlake_update_primary_plane;
  12041. } else if (IS_VALLEYVIEW(dev)) {
  12042. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12043. dev_priv->display.get_initial_plane_config =
  12044. i9xx_get_initial_plane_config;
  12045. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12046. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12047. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12048. dev_priv->display.off = i9xx_crtc_off;
  12049. dev_priv->display.update_primary_plane =
  12050. i9xx_update_primary_plane;
  12051. } else {
  12052. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12053. dev_priv->display.get_initial_plane_config =
  12054. i9xx_get_initial_plane_config;
  12055. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12056. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12057. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12058. dev_priv->display.off = i9xx_crtc_off;
  12059. dev_priv->display.update_primary_plane =
  12060. i9xx_update_primary_plane;
  12061. }
  12062. /* Returns the core display clock speed */
  12063. if (IS_SKYLAKE(dev))
  12064. dev_priv->display.get_display_clock_speed =
  12065. skylake_get_display_clock_speed;
  12066. else if (IS_BROADWELL(dev))
  12067. dev_priv->display.get_display_clock_speed =
  12068. broadwell_get_display_clock_speed;
  12069. else if (IS_HASWELL(dev))
  12070. dev_priv->display.get_display_clock_speed =
  12071. haswell_get_display_clock_speed;
  12072. else if (IS_VALLEYVIEW(dev))
  12073. dev_priv->display.get_display_clock_speed =
  12074. valleyview_get_display_clock_speed;
  12075. else if (IS_GEN5(dev))
  12076. dev_priv->display.get_display_clock_speed =
  12077. ilk_get_display_clock_speed;
  12078. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12079. IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  12080. dev_priv->display.get_display_clock_speed =
  12081. i945_get_display_clock_speed;
  12082. else if (IS_I915G(dev))
  12083. dev_priv->display.get_display_clock_speed =
  12084. i915_get_display_clock_speed;
  12085. else if (IS_I945GM(dev) || IS_845G(dev))
  12086. dev_priv->display.get_display_clock_speed =
  12087. i9xx_misc_get_display_clock_speed;
  12088. else if (IS_PINEVIEW(dev))
  12089. dev_priv->display.get_display_clock_speed =
  12090. pnv_get_display_clock_speed;
  12091. else if (IS_I915GM(dev))
  12092. dev_priv->display.get_display_clock_speed =
  12093. i915gm_get_display_clock_speed;
  12094. else if (IS_I865G(dev))
  12095. dev_priv->display.get_display_clock_speed =
  12096. i865_get_display_clock_speed;
  12097. else if (IS_I85X(dev))
  12098. dev_priv->display.get_display_clock_speed =
  12099. i855_get_display_clock_speed;
  12100. else /* 852, 830 */
  12101. dev_priv->display.get_display_clock_speed =
  12102. i830_get_display_clock_speed;
  12103. if (IS_GEN5(dev)) {
  12104. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12105. } else if (IS_GEN6(dev)) {
  12106. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12107. } else if (IS_IVYBRIDGE(dev)) {
  12108. /* FIXME: detect B0+ stepping and use auto training */
  12109. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12110. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12111. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12112. } else if (IS_VALLEYVIEW(dev)) {
  12113. dev_priv->display.modeset_global_resources =
  12114. valleyview_modeset_global_resources;
  12115. } else if (IS_BROXTON(dev)) {
  12116. dev_priv->display.modeset_global_resources =
  12117. broxton_modeset_global_resources;
  12118. }
  12119. switch (INTEL_INFO(dev)->gen) {
  12120. case 2:
  12121. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12122. break;
  12123. case 3:
  12124. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12125. break;
  12126. case 4:
  12127. case 5:
  12128. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12129. break;
  12130. case 6:
  12131. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12132. break;
  12133. case 7:
  12134. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12135. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12136. break;
  12137. case 9:
  12138. /* Drop through - unsupported since execlist only. */
  12139. default:
  12140. /* Default just returns -ENODEV to indicate unsupported */
  12141. dev_priv->display.queue_flip = intel_default_queue_flip;
  12142. }
  12143. intel_panel_init_backlight_funcs(dev);
  12144. mutex_init(&dev_priv->pps_mutex);
  12145. }
  12146. /*
  12147. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12148. * resume, or other times. This quirk makes sure that's the case for
  12149. * affected systems.
  12150. */
  12151. static void quirk_pipea_force(struct drm_device *dev)
  12152. {
  12153. struct drm_i915_private *dev_priv = dev->dev_private;
  12154. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12155. DRM_INFO("applying pipe a force quirk\n");
  12156. }
  12157. static void quirk_pipeb_force(struct drm_device *dev)
  12158. {
  12159. struct drm_i915_private *dev_priv = dev->dev_private;
  12160. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12161. DRM_INFO("applying pipe b force quirk\n");
  12162. }
  12163. /*
  12164. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12165. */
  12166. static void quirk_ssc_force_disable(struct drm_device *dev)
  12167. {
  12168. struct drm_i915_private *dev_priv = dev->dev_private;
  12169. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12170. DRM_INFO("applying lvds SSC disable quirk\n");
  12171. }
  12172. /*
  12173. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12174. * brightness value
  12175. */
  12176. static void quirk_invert_brightness(struct drm_device *dev)
  12177. {
  12178. struct drm_i915_private *dev_priv = dev->dev_private;
  12179. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12180. DRM_INFO("applying inverted panel brightness quirk\n");
  12181. }
  12182. /* Some VBT's incorrectly indicate no backlight is present */
  12183. static void quirk_backlight_present(struct drm_device *dev)
  12184. {
  12185. struct drm_i915_private *dev_priv = dev->dev_private;
  12186. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12187. DRM_INFO("applying backlight present quirk\n");
  12188. }
  12189. struct intel_quirk {
  12190. int device;
  12191. int subsystem_vendor;
  12192. int subsystem_device;
  12193. void (*hook)(struct drm_device *dev);
  12194. };
  12195. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12196. struct intel_dmi_quirk {
  12197. void (*hook)(struct drm_device *dev);
  12198. const struct dmi_system_id (*dmi_id_list)[];
  12199. };
  12200. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12201. {
  12202. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12203. return 1;
  12204. }
  12205. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12206. {
  12207. .dmi_id_list = &(const struct dmi_system_id[]) {
  12208. {
  12209. .callback = intel_dmi_reverse_brightness,
  12210. .ident = "NCR Corporation",
  12211. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12212. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12213. },
  12214. },
  12215. { } /* terminating entry */
  12216. },
  12217. .hook = quirk_invert_brightness,
  12218. },
  12219. };
  12220. static struct intel_quirk intel_quirks[] = {
  12221. /* HP Mini needs pipe A force quirk (LP: #322104) */
  12222. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  12223. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12224. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12225. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12226. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12227. /* 830 needs to leave pipe A & dpll A up */
  12228. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12229. /* 830 needs to leave pipe B & dpll B up */
  12230. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12231. /* Lenovo U160 cannot use SSC on LVDS */
  12232. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12233. /* Sony Vaio Y cannot use SSC on LVDS */
  12234. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12235. /* Acer Aspire 5734Z must invert backlight brightness */
  12236. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12237. /* Acer/eMachines G725 */
  12238. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12239. /* Acer/eMachines e725 */
  12240. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12241. /* Acer/Packard Bell NCL20 */
  12242. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12243. /* Acer Aspire 4736Z */
  12244. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12245. /* Acer Aspire 5336 */
  12246. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12247. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12248. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12249. /* Acer C720 Chromebook (Core i3 4005U) */
  12250. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12251. /* Apple Macbook 2,1 (Core 2 T7400) */
  12252. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12253. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12254. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12255. /* HP Chromebook 14 (Celeron 2955U) */
  12256. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12257. /* Dell Chromebook 11 */
  12258. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12259. };
  12260. static void intel_init_quirks(struct drm_device *dev)
  12261. {
  12262. struct pci_dev *d = dev->pdev;
  12263. int i;
  12264. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12265. struct intel_quirk *q = &intel_quirks[i];
  12266. if (d->device == q->device &&
  12267. (d->subsystem_vendor == q->subsystem_vendor ||
  12268. q->subsystem_vendor == PCI_ANY_ID) &&
  12269. (d->subsystem_device == q->subsystem_device ||
  12270. q->subsystem_device == PCI_ANY_ID))
  12271. q->hook(dev);
  12272. }
  12273. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12274. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12275. intel_dmi_quirks[i].hook(dev);
  12276. }
  12277. }
  12278. /* Disable the VGA plane that we never use */
  12279. static void i915_disable_vga(struct drm_device *dev)
  12280. {
  12281. struct drm_i915_private *dev_priv = dev->dev_private;
  12282. u8 sr1;
  12283. u32 vga_reg = i915_vgacntrl_reg(dev);
  12284. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12285. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12286. outb(SR01, VGA_SR_INDEX);
  12287. sr1 = inb(VGA_SR_DATA);
  12288. outb(sr1 | 1<<5, VGA_SR_DATA);
  12289. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12290. udelay(300);
  12291. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12292. POSTING_READ(vga_reg);
  12293. }
  12294. void intel_modeset_init_hw(struct drm_device *dev)
  12295. {
  12296. intel_prepare_ddi(dev);
  12297. if (IS_VALLEYVIEW(dev))
  12298. vlv_update_cdclk(dev);
  12299. intel_init_clock_gating(dev);
  12300. intel_enable_gt_powersave(dev);
  12301. }
  12302. void intel_modeset_init(struct drm_device *dev)
  12303. {
  12304. struct drm_i915_private *dev_priv = dev->dev_private;
  12305. int sprite, ret;
  12306. enum pipe pipe;
  12307. struct intel_crtc *crtc;
  12308. drm_mode_config_init(dev);
  12309. dev->mode_config.min_width = 0;
  12310. dev->mode_config.min_height = 0;
  12311. dev->mode_config.preferred_depth = 24;
  12312. dev->mode_config.prefer_shadow = 1;
  12313. dev->mode_config.allow_fb_modifiers = true;
  12314. dev->mode_config.funcs = &intel_mode_funcs;
  12315. intel_init_quirks(dev);
  12316. intel_init_pm(dev);
  12317. if (INTEL_INFO(dev)->num_pipes == 0)
  12318. return;
  12319. intel_init_display(dev);
  12320. intel_init_audio(dev);
  12321. if (IS_GEN2(dev)) {
  12322. dev->mode_config.max_width = 2048;
  12323. dev->mode_config.max_height = 2048;
  12324. } else if (IS_GEN3(dev)) {
  12325. dev->mode_config.max_width = 4096;
  12326. dev->mode_config.max_height = 4096;
  12327. } else {
  12328. dev->mode_config.max_width = 8192;
  12329. dev->mode_config.max_height = 8192;
  12330. }
  12331. if (IS_845G(dev) || IS_I865G(dev)) {
  12332. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12333. dev->mode_config.cursor_height = 1023;
  12334. } else if (IS_GEN2(dev)) {
  12335. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12336. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12337. } else {
  12338. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12339. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12340. }
  12341. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12342. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12343. INTEL_INFO(dev)->num_pipes,
  12344. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12345. for_each_pipe(dev_priv, pipe) {
  12346. intel_crtc_init(dev, pipe);
  12347. for_each_sprite(dev_priv, pipe, sprite) {
  12348. ret = intel_plane_init(dev, pipe, sprite);
  12349. if (ret)
  12350. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12351. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12352. }
  12353. }
  12354. intel_init_dpio(dev);
  12355. intel_shared_dpll_init(dev);
  12356. /* Just disable it once at startup */
  12357. i915_disable_vga(dev);
  12358. intel_setup_outputs(dev);
  12359. /* Just in case the BIOS is doing something questionable. */
  12360. intel_fbc_disable(dev);
  12361. drm_modeset_lock_all(dev);
  12362. intel_modeset_setup_hw_state(dev, false);
  12363. drm_modeset_unlock_all(dev);
  12364. for_each_intel_crtc(dev, crtc) {
  12365. if (!crtc->active)
  12366. continue;
  12367. /*
  12368. * Note that reserving the BIOS fb up front prevents us
  12369. * from stuffing other stolen allocations like the ring
  12370. * on top. This prevents some ugliness at boot time, and
  12371. * can even allow for smooth boot transitions if the BIOS
  12372. * fb is large enough for the active pipe configuration.
  12373. */
  12374. if (dev_priv->display.get_initial_plane_config) {
  12375. dev_priv->display.get_initial_plane_config(crtc,
  12376. &crtc->plane_config);
  12377. /*
  12378. * If the fb is shared between multiple heads, we'll
  12379. * just get the first one.
  12380. */
  12381. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12382. }
  12383. }
  12384. }
  12385. static void intel_enable_pipe_a(struct drm_device *dev)
  12386. {
  12387. struct intel_connector *connector;
  12388. struct drm_connector *crt = NULL;
  12389. struct intel_load_detect_pipe load_detect_temp;
  12390. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12391. /* We can't just switch on the pipe A, we need to set things up with a
  12392. * proper mode and output configuration. As a gross hack, enable pipe A
  12393. * by enabling the load detect pipe once. */
  12394. for_each_intel_connector(dev, connector) {
  12395. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12396. crt = &connector->base;
  12397. break;
  12398. }
  12399. }
  12400. if (!crt)
  12401. return;
  12402. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12403. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12404. }
  12405. static bool
  12406. intel_check_plane_mapping(struct intel_crtc *crtc)
  12407. {
  12408. struct drm_device *dev = crtc->base.dev;
  12409. struct drm_i915_private *dev_priv = dev->dev_private;
  12410. u32 reg, val;
  12411. if (INTEL_INFO(dev)->num_pipes == 1)
  12412. return true;
  12413. reg = DSPCNTR(!crtc->plane);
  12414. val = I915_READ(reg);
  12415. if ((val & DISPLAY_PLANE_ENABLE) &&
  12416. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12417. return false;
  12418. return true;
  12419. }
  12420. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12421. {
  12422. struct drm_device *dev = crtc->base.dev;
  12423. struct drm_i915_private *dev_priv = dev->dev_private;
  12424. u32 reg;
  12425. /* Clear any frame start delays used for debugging left by the BIOS */
  12426. reg = PIPECONF(crtc->config->cpu_transcoder);
  12427. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12428. /* restore vblank interrupts to correct state */
  12429. drm_crtc_vblank_reset(&crtc->base);
  12430. if (crtc->active) {
  12431. update_scanline_offset(crtc);
  12432. drm_crtc_vblank_on(&crtc->base);
  12433. }
  12434. /* We need to sanitize the plane -> pipe mapping first because this will
  12435. * disable the crtc (and hence change the state) if it is wrong. Note
  12436. * that gen4+ has a fixed plane -> pipe mapping. */
  12437. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12438. struct intel_connector *connector;
  12439. bool plane;
  12440. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12441. crtc->base.base.id);
  12442. /* Pipe has the wrong plane attached and the plane is active.
  12443. * Temporarily change the plane mapping and disable everything
  12444. * ... */
  12445. plane = crtc->plane;
  12446. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12447. crtc->plane = !plane;
  12448. intel_crtc_disable_planes(&crtc->base);
  12449. dev_priv->display.crtc_disable(&crtc->base);
  12450. crtc->plane = plane;
  12451. /* ... and break all links. */
  12452. for_each_intel_connector(dev, connector) {
  12453. if (connector->encoder->base.crtc != &crtc->base)
  12454. continue;
  12455. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12456. connector->base.encoder = NULL;
  12457. }
  12458. /* multiple connectors may have the same encoder:
  12459. * handle them and break crtc link separately */
  12460. for_each_intel_connector(dev, connector)
  12461. if (connector->encoder->base.crtc == &crtc->base) {
  12462. connector->encoder->base.crtc = NULL;
  12463. connector->encoder->connectors_active = false;
  12464. }
  12465. WARN_ON(crtc->active);
  12466. crtc->base.state->enable = false;
  12467. crtc->base.enabled = false;
  12468. }
  12469. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12470. crtc->pipe == PIPE_A && !crtc->active) {
  12471. /* BIOS forgot to enable pipe A, this mostly happens after
  12472. * resume. Force-enable the pipe to fix this, the update_dpms
  12473. * call below we restore the pipe to the right state, but leave
  12474. * the required bits on. */
  12475. intel_enable_pipe_a(dev);
  12476. }
  12477. /* Adjust the state of the output pipe according to whether we
  12478. * have active connectors/encoders. */
  12479. intel_crtc_update_dpms(&crtc->base);
  12480. if (crtc->active != crtc->base.state->enable) {
  12481. struct intel_encoder *encoder;
  12482. /* This can happen either due to bugs in the get_hw_state
  12483. * functions or because the pipe is force-enabled due to the
  12484. * pipe A quirk. */
  12485. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12486. crtc->base.base.id,
  12487. crtc->base.state->enable ? "enabled" : "disabled",
  12488. crtc->active ? "enabled" : "disabled");
  12489. crtc->base.state->enable = crtc->active;
  12490. crtc->base.enabled = crtc->active;
  12491. /* Because we only establish the connector -> encoder ->
  12492. * crtc links if something is active, this means the
  12493. * crtc is now deactivated. Break the links. connector
  12494. * -> encoder links are only establish when things are
  12495. * actually up, hence no need to break them. */
  12496. WARN_ON(crtc->active);
  12497. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12498. WARN_ON(encoder->connectors_active);
  12499. encoder->base.crtc = NULL;
  12500. }
  12501. }
  12502. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12503. /*
  12504. * We start out with underrun reporting disabled to avoid races.
  12505. * For correct bookkeeping mark this on active crtcs.
  12506. *
  12507. * Also on gmch platforms we dont have any hardware bits to
  12508. * disable the underrun reporting. Which means we need to start
  12509. * out with underrun reporting disabled also on inactive pipes,
  12510. * since otherwise we'll complain about the garbage we read when
  12511. * e.g. coming up after runtime pm.
  12512. *
  12513. * No protection against concurrent access is required - at
  12514. * worst a fifo underrun happens which also sets this to false.
  12515. */
  12516. crtc->cpu_fifo_underrun_disabled = true;
  12517. crtc->pch_fifo_underrun_disabled = true;
  12518. }
  12519. }
  12520. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12521. {
  12522. struct intel_connector *connector;
  12523. struct drm_device *dev = encoder->base.dev;
  12524. /* We need to check both for a crtc link (meaning that the
  12525. * encoder is active and trying to read from a pipe) and the
  12526. * pipe itself being active. */
  12527. bool has_active_crtc = encoder->base.crtc &&
  12528. to_intel_crtc(encoder->base.crtc)->active;
  12529. if (encoder->connectors_active && !has_active_crtc) {
  12530. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12531. encoder->base.base.id,
  12532. encoder->base.name);
  12533. /* Connector is active, but has no active pipe. This is
  12534. * fallout from our resume register restoring. Disable
  12535. * the encoder manually again. */
  12536. if (encoder->base.crtc) {
  12537. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12538. encoder->base.base.id,
  12539. encoder->base.name);
  12540. encoder->disable(encoder);
  12541. if (encoder->post_disable)
  12542. encoder->post_disable(encoder);
  12543. }
  12544. encoder->base.crtc = NULL;
  12545. encoder->connectors_active = false;
  12546. /* Inconsistent output/port/pipe state happens presumably due to
  12547. * a bug in one of the get_hw_state functions. Or someplace else
  12548. * in our code, like the register restore mess on resume. Clamp
  12549. * things to off as a safer default. */
  12550. for_each_intel_connector(dev, connector) {
  12551. if (connector->encoder != encoder)
  12552. continue;
  12553. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12554. connector->base.encoder = NULL;
  12555. }
  12556. }
  12557. /* Enabled encoders without active connectors will be fixed in
  12558. * the crtc fixup. */
  12559. }
  12560. void i915_redisable_vga_power_on(struct drm_device *dev)
  12561. {
  12562. struct drm_i915_private *dev_priv = dev->dev_private;
  12563. u32 vga_reg = i915_vgacntrl_reg(dev);
  12564. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12565. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12566. i915_disable_vga(dev);
  12567. }
  12568. }
  12569. void i915_redisable_vga(struct drm_device *dev)
  12570. {
  12571. struct drm_i915_private *dev_priv = dev->dev_private;
  12572. /* This function can be called both from intel_modeset_setup_hw_state or
  12573. * at a very early point in our resume sequence, where the power well
  12574. * structures are not yet restored. Since this function is at a very
  12575. * paranoid "someone might have enabled VGA while we were not looking"
  12576. * level, just check if the power well is enabled instead of trying to
  12577. * follow the "don't touch the power well if we don't need it" policy
  12578. * the rest of the driver uses. */
  12579. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12580. return;
  12581. i915_redisable_vga_power_on(dev);
  12582. }
  12583. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12584. {
  12585. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12586. if (!crtc->active)
  12587. return false;
  12588. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  12589. }
  12590. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12591. {
  12592. struct drm_i915_private *dev_priv = dev->dev_private;
  12593. enum pipe pipe;
  12594. struct intel_crtc *crtc;
  12595. struct intel_encoder *encoder;
  12596. struct intel_connector *connector;
  12597. int i;
  12598. for_each_intel_crtc(dev, crtc) {
  12599. struct drm_plane *primary = crtc->base.primary;
  12600. struct intel_plane_state *plane_state;
  12601. memset(crtc->config, 0, sizeof(*crtc->config));
  12602. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12603. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12604. crtc->config);
  12605. crtc->base.state->enable = crtc->active;
  12606. crtc->base.enabled = crtc->active;
  12607. plane_state = to_intel_plane_state(primary->state);
  12608. plane_state->visible = primary_get_hw_state(crtc);
  12609. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12610. crtc->base.base.id,
  12611. crtc->active ? "enabled" : "disabled");
  12612. }
  12613. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12614. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12615. pll->on = pll->get_hw_state(dev_priv, pll,
  12616. &pll->config.hw_state);
  12617. pll->active = 0;
  12618. pll->config.crtc_mask = 0;
  12619. for_each_intel_crtc(dev, crtc) {
  12620. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12621. pll->active++;
  12622. pll->config.crtc_mask |= 1 << crtc->pipe;
  12623. }
  12624. }
  12625. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12626. pll->name, pll->config.crtc_mask, pll->on);
  12627. if (pll->config.crtc_mask)
  12628. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12629. }
  12630. for_each_intel_encoder(dev, encoder) {
  12631. pipe = 0;
  12632. if (encoder->get_hw_state(encoder, &pipe)) {
  12633. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12634. encoder->base.crtc = &crtc->base;
  12635. encoder->get_config(encoder, crtc->config);
  12636. } else {
  12637. encoder->base.crtc = NULL;
  12638. }
  12639. encoder->connectors_active = false;
  12640. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12641. encoder->base.base.id,
  12642. encoder->base.name,
  12643. encoder->base.crtc ? "enabled" : "disabled",
  12644. pipe_name(pipe));
  12645. }
  12646. for_each_intel_connector(dev, connector) {
  12647. if (connector->get_hw_state(connector)) {
  12648. connector->base.dpms = DRM_MODE_DPMS_ON;
  12649. connector->encoder->connectors_active = true;
  12650. connector->base.encoder = &connector->encoder->base;
  12651. } else {
  12652. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12653. connector->base.encoder = NULL;
  12654. }
  12655. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12656. connector->base.base.id,
  12657. connector->base.name,
  12658. connector->base.encoder ? "enabled" : "disabled");
  12659. }
  12660. }
  12661. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12662. * and i915 state tracking structures. */
  12663. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12664. bool force_restore)
  12665. {
  12666. struct drm_i915_private *dev_priv = dev->dev_private;
  12667. enum pipe pipe;
  12668. struct intel_crtc *crtc;
  12669. struct intel_encoder *encoder;
  12670. int i;
  12671. intel_modeset_readout_hw_state(dev);
  12672. /*
  12673. * Now that we have the config, copy it to each CRTC struct
  12674. * Note that this could go away if we move to using crtc_config
  12675. * checking everywhere.
  12676. */
  12677. for_each_intel_crtc(dev, crtc) {
  12678. if (crtc->active && i915.fastboot) {
  12679. intel_mode_from_pipe_config(&crtc->base.mode,
  12680. crtc->config);
  12681. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  12682. crtc->base.base.id);
  12683. drm_mode_debug_printmodeline(&crtc->base.mode);
  12684. }
  12685. }
  12686. /* HW state is read out, now we need to sanitize this mess. */
  12687. for_each_intel_encoder(dev, encoder) {
  12688. intel_sanitize_encoder(encoder);
  12689. }
  12690. for_each_pipe(dev_priv, pipe) {
  12691. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12692. intel_sanitize_crtc(crtc);
  12693. intel_dump_pipe_config(crtc, crtc->config,
  12694. "[setup_hw_state]");
  12695. }
  12696. intel_modeset_update_connector_atomic_state(dev);
  12697. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12698. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12699. if (!pll->on || pll->active)
  12700. continue;
  12701. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12702. pll->disable(dev_priv, pll);
  12703. pll->on = false;
  12704. }
  12705. if (IS_GEN9(dev))
  12706. skl_wm_get_hw_state(dev);
  12707. else if (HAS_PCH_SPLIT(dev))
  12708. ilk_wm_get_hw_state(dev);
  12709. if (force_restore) {
  12710. i915_redisable_vga(dev);
  12711. /*
  12712. * We need to use raw interfaces for restoring state to avoid
  12713. * checking (bogus) intermediate states.
  12714. */
  12715. for_each_pipe(dev_priv, pipe) {
  12716. struct drm_crtc *crtc =
  12717. dev_priv->pipe_to_crtc_mapping[pipe];
  12718. intel_crtc_restore_mode(crtc);
  12719. }
  12720. } else {
  12721. intel_modeset_update_staged_output_state(dev);
  12722. }
  12723. intel_modeset_check_state(dev);
  12724. }
  12725. void intel_modeset_gem_init(struct drm_device *dev)
  12726. {
  12727. struct drm_i915_private *dev_priv = dev->dev_private;
  12728. struct drm_crtc *c;
  12729. struct drm_i915_gem_object *obj;
  12730. int ret;
  12731. mutex_lock(&dev->struct_mutex);
  12732. intel_init_gt_powersave(dev);
  12733. mutex_unlock(&dev->struct_mutex);
  12734. /*
  12735. * There may be no VBT; and if the BIOS enabled SSC we can
  12736. * just keep using it to avoid unnecessary flicker. Whereas if the
  12737. * BIOS isn't using it, don't assume it will work even if the VBT
  12738. * indicates as much.
  12739. */
  12740. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  12741. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12742. DREF_SSC1_ENABLE);
  12743. intel_modeset_init_hw(dev);
  12744. intel_setup_overlay(dev);
  12745. /*
  12746. * Make sure any fbs we allocated at startup are properly
  12747. * pinned & fenced. When we do the allocation it's too early
  12748. * for this.
  12749. */
  12750. for_each_crtc(dev, c) {
  12751. obj = intel_fb_obj(c->primary->fb);
  12752. if (obj == NULL)
  12753. continue;
  12754. mutex_lock(&dev->struct_mutex);
  12755. ret = intel_pin_and_fence_fb_obj(c->primary,
  12756. c->primary->fb,
  12757. c->primary->state,
  12758. NULL);
  12759. mutex_unlock(&dev->struct_mutex);
  12760. if (ret) {
  12761. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12762. to_intel_crtc(c)->pipe);
  12763. drm_framebuffer_unreference(c->primary->fb);
  12764. c->primary->fb = NULL;
  12765. update_state_fb(c->primary);
  12766. }
  12767. }
  12768. intel_backlight_register(dev);
  12769. }
  12770. void intel_connector_unregister(struct intel_connector *intel_connector)
  12771. {
  12772. struct drm_connector *connector = &intel_connector->base;
  12773. intel_panel_destroy_backlight(connector);
  12774. drm_connector_unregister(connector);
  12775. }
  12776. void intel_modeset_cleanup(struct drm_device *dev)
  12777. {
  12778. struct drm_i915_private *dev_priv = dev->dev_private;
  12779. struct drm_connector *connector;
  12780. intel_disable_gt_powersave(dev);
  12781. intel_backlight_unregister(dev);
  12782. /*
  12783. * Interrupts and polling as the first thing to avoid creating havoc.
  12784. * Too much stuff here (turning of connectors, ...) would
  12785. * experience fancy races otherwise.
  12786. */
  12787. intel_irq_uninstall(dev_priv);
  12788. /*
  12789. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12790. * poll handlers. Hence disable polling after hpd handling is shut down.
  12791. */
  12792. drm_kms_helper_poll_fini(dev);
  12793. mutex_lock(&dev->struct_mutex);
  12794. intel_unregister_dsm_handler();
  12795. intel_fbc_disable(dev);
  12796. mutex_unlock(&dev->struct_mutex);
  12797. /* flush any delayed tasks or pending work */
  12798. flush_scheduled_work();
  12799. /* destroy the backlight and sysfs files before encoders/connectors */
  12800. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12801. struct intel_connector *intel_connector;
  12802. intel_connector = to_intel_connector(connector);
  12803. intel_connector->unregister(intel_connector);
  12804. }
  12805. drm_mode_config_cleanup(dev);
  12806. intel_cleanup_overlay(dev);
  12807. mutex_lock(&dev->struct_mutex);
  12808. intel_cleanup_gt_powersave(dev);
  12809. mutex_unlock(&dev->struct_mutex);
  12810. }
  12811. /*
  12812. * Return which encoder is currently attached for connector.
  12813. */
  12814. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12815. {
  12816. return &intel_attached_encoder(connector)->base;
  12817. }
  12818. void intel_connector_attach_encoder(struct intel_connector *connector,
  12819. struct intel_encoder *encoder)
  12820. {
  12821. connector->encoder = encoder;
  12822. drm_mode_connector_attach_encoder(&connector->base,
  12823. &encoder->base);
  12824. }
  12825. /*
  12826. * set vga decode state - true == enable VGA decode
  12827. */
  12828. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12829. {
  12830. struct drm_i915_private *dev_priv = dev->dev_private;
  12831. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12832. u16 gmch_ctrl;
  12833. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12834. DRM_ERROR("failed to read control word\n");
  12835. return -EIO;
  12836. }
  12837. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12838. return 0;
  12839. if (state)
  12840. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12841. else
  12842. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12843. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12844. DRM_ERROR("failed to write control word\n");
  12845. return -EIO;
  12846. }
  12847. return 0;
  12848. }
  12849. struct intel_display_error_state {
  12850. u32 power_well_driver;
  12851. int num_transcoders;
  12852. struct intel_cursor_error_state {
  12853. u32 control;
  12854. u32 position;
  12855. u32 base;
  12856. u32 size;
  12857. } cursor[I915_MAX_PIPES];
  12858. struct intel_pipe_error_state {
  12859. bool power_domain_on;
  12860. u32 source;
  12861. u32 stat;
  12862. } pipe[I915_MAX_PIPES];
  12863. struct intel_plane_error_state {
  12864. u32 control;
  12865. u32 stride;
  12866. u32 size;
  12867. u32 pos;
  12868. u32 addr;
  12869. u32 surface;
  12870. u32 tile_offset;
  12871. } plane[I915_MAX_PIPES];
  12872. struct intel_transcoder_error_state {
  12873. bool power_domain_on;
  12874. enum transcoder cpu_transcoder;
  12875. u32 conf;
  12876. u32 htotal;
  12877. u32 hblank;
  12878. u32 hsync;
  12879. u32 vtotal;
  12880. u32 vblank;
  12881. u32 vsync;
  12882. } transcoder[4];
  12883. };
  12884. struct intel_display_error_state *
  12885. intel_display_capture_error_state(struct drm_device *dev)
  12886. {
  12887. struct drm_i915_private *dev_priv = dev->dev_private;
  12888. struct intel_display_error_state *error;
  12889. int transcoders[] = {
  12890. TRANSCODER_A,
  12891. TRANSCODER_B,
  12892. TRANSCODER_C,
  12893. TRANSCODER_EDP,
  12894. };
  12895. int i;
  12896. if (INTEL_INFO(dev)->num_pipes == 0)
  12897. return NULL;
  12898. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12899. if (error == NULL)
  12900. return NULL;
  12901. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12902. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12903. for_each_pipe(dev_priv, i) {
  12904. error->pipe[i].power_domain_on =
  12905. __intel_display_power_is_enabled(dev_priv,
  12906. POWER_DOMAIN_PIPE(i));
  12907. if (!error->pipe[i].power_domain_on)
  12908. continue;
  12909. error->cursor[i].control = I915_READ(CURCNTR(i));
  12910. error->cursor[i].position = I915_READ(CURPOS(i));
  12911. error->cursor[i].base = I915_READ(CURBASE(i));
  12912. error->plane[i].control = I915_READ(DSPCNTR(i));
  12913. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  12914. if (INTEL_INFO(dev)->gen <= 3) {
  12915. error->plane[i].size = I915_READ(DSPSIZE(i));
  12916. error->plane[i].pos = I915_READ(DSPPOS(i));
  12917. }
  12918. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12919. error->plane[i].addr = I915_READ(DSPADDR(i));
  12920. if (INTEL_INFO(dev)->gen >= 4) {
  12921. error->plane[i].surface = I915_READ(DSPSURF(i));
  12922. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  12923. }
  12924. error->pipe[i].source = I915_READ(PIPESRC(i));
  12925. if (HAS_GMCH_DISPLAY(dev))
  12926. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  12927. }
  12928. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  12929. if (HAS_DDI(dev_priv->dev))
  12930. error->num_transcoders++; /* Account for eDP. */
  12931. for (i = 0; i < error->num_transcoders; i++) {
  12932. enum transcoder cpu_transcoder = transcoders[i];
  12933. error->transcoder[i].power_domain_on =
  12934. __intel_display_power_is_enabled(dev_priv,
  12935. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  12936. if (!error->transcoder[i].power_domain_on)
  12937. continue;
  12938. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  12939. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  12940. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  12941. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  12942. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  12943. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  12944. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  12945. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  12946. }
  12947. return error;
  12948. }
  12949. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  12950. void
  12951. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  12952. struct drm_device *dev,
  12953. struct intel_display_error_state *error)
  12954. {
  12955. struct drm_i915_private *dev_priv = dev->dev_private;
  12956. int i;
  12957. if (!error)
  12958. return;
  12959. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  12960. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12961. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  12962. error->power_well_driver);
  12963. for_each_pipe(dev_priv, i) {
  12964. err_printf(m, "Pipe [%d]:\n", i);
  12965. err_printf(m, " Power: %s\n",
  12966. error->pipe[i].power_domain_on ? "on" : "off");
  12967. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  12968. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  12969. err_printf(m, "Plane [%d]:\n", i);
  12970. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  12971. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  12972. if (INTEL_INFO(dev)->gen <= 3) {
  12973. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  12974. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  12975. }
  12976. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  12977. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  12978. if (INTEL_INFO(dev)->gen >= 4) {
  12979. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  12980. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  12981. }
  12982. err_printf(m, "Cursor [%d]:\n", i);
  12983. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  12984. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  12985. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  12986. }
  12987. for (i = 0; i < error->num_transcoders; i++) {
  12988. err_printf(m, "CPU transcoder: %c\n",
  12989. transcoder_name(error->transcoder[i].cpu_transcoder));
  12990. err_printf(m, " Power: %s\n",
  12991. error->transcoder[i].power_domain_on ? "on" : "off");
  12992. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  12993. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  12994. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  12995. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  12996. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  12997. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  12998. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  12999. }
  13000. }
  13001. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13002. {
  13003. struct intel_crtc *crtc;
  13004. for_each_intel_crtc(dev, crtc) {
  13005. struct intel_unpin_work *work;
  13006. spin_lock_irq(&dev->event_lock);
  13007. work = crtc->unpin_work;
  13008. if (work && work->event &&
  13009. work->event->base.file_priv == file) {
  13010. kfree(work->event);
  13011. work->event = NULL;
  13012. }
  13013. spin_unlock_irq(&dev->event_lock);
  13014. }
  13015. }