intel_display.c 432 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. /* Primary plane formats for gen <= 3 */
  47. static const uint32_t i8xx_primary_formats[] = {
  48. DRM_FORMAT_C8,
  49. DRM_FORMAT_RGB565,
  50. DRM_FORMAT_XRGB1555,
  51. DRM_FORMAT_XRGB8888,
  52. };
  53. /* Primary plane formats for gen >= 4 */
  54. static const uint32_t i965_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB8888,
  58. DRM_FORMAT_XBGR8888,
  59. DRM_FORMAT_XRGB2101010,
  60. DRM_FORMAT_XBGR2101010,
  61. };
  62. static const uint32_t skl_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_ARGB8888,
  68. DRM_FORMAT_ABGR8888,
  69. DRM_FORMAT_XRGB2101010,
  70. DRM_FORMAT_XBGR2101010,
  71. DRM_FORMAT_YUYV,
  72. DRM_FORMAT_YVYU,
  73. DRM_FORMAT_UYVY,
  74. DRM_FORMAT_VYUY,
  75. };
  76. /* Cursor formats */
  77. static const uint32_t intel_cursor_formats[] = {
  78. DRM_FORMAT_ARGB8888,
  79. };
  80. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  81. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  82. struct intel_crtc_state *pipe_config);
  83. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  84. struct intel_crtc_state *pipe_config);
  85. static int intel_framebuffer_init(struct drm_device *dev,
  86. struct intel_framebuffer *ifb,
  87. struct drm_mode_fb_cmd2 *mode_cmd,
  88. struct drm_i915_gem_object *obj);
  89. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  90. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  91. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  92. struct intel_link_m_n *m_n,
  93. struct intel_link_m_n *m2_n2);
  94. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  95. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  96. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  97. static void vlv_prepare_pll(struct intel_crtc *crtc,
  98. const struct intel_crtc_state *pipe_config);
  99. static void chv_prepare_pll(struct intel_crtc *crtc,
  100. const struct intel_crtc_state *pipe_config);
  101. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  102. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  103. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  104. struct intel_crtc_state *crtc_state);
  105. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  106. int num_connectors);
  107. static void skylake_pfit_enable(struct intel_crtc *crtc);
  108. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  109. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  110. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  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. /* hrawclock is 1/4 the FSB frequency */
  131. int intel_hrawclk(struct drm_device *dev)
  132. {
  133. struct drm_i915_private *dev_priv = dev->dev_private;
  134. uint32_t clkcfg;
  135. /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
  136. if (IS_VALLEYVIEW(dev))
  137. return 200;
  138. clkcfg = I915_READ(CLKCFG);
  139. switch (clkcfg & CLKCFG_FSB_MASK) {
  140. case CLKCFG_FSB_400:
  141. return 100;
  142. case CLKCFG_FSB_533:
  143. return 133;
  144. case CLKCFG_FSB_667:
  145. return 166;
  146. case CLKCFG_FSB_800:
  147. return 200;
  148. case CLKCFG_FSB_1067:
  149. return 266;
  150. case CLKCFG_FSB_1333:
  151. return 333;
  152. /* these two are just a guess; one of them might be right */
  153. case CLKCFG_FSB_1600:
  154. case CLKCFG_FSB_1600_ALT:
  155. return 400;
  156. default:
  157. return 133;
  158. }
  159. }
  160. static inline u32 /* units of 100MHz */
  161. intel_fdi_link_freq(struct drm_device *dev)
  162. {
  163. if (IS_GEN5(dev)) {
  164. struct drm_i915_private *dev_priv = dev->dev_private;
  165. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  166. } else
  167. return 27;
  168. }
  169. static const intel_limit_t intel_limits_i8xx_dac = {
  170. .dot = { .min = 25000, .max = 350000 },
  171. .vco = { .min = 908000, .max = 1512000 },
  172. .n = { .min = 2, .max = 16 },
  173. .m = { .min = 96, .max = 140 },
  174. .m1 = { .min = 18, .max = 26 },
  175. .m2 = { .min = 6, .max = 16 },
  176. .p = { .min = 4, .max = 128 },
  177. .p1 = { .min = 2, .max = 33 },
  178. .p2 = { .dot_limit = 165000,
  179. .p2_slow = 4, .p2_fast = 2 },
  180. };
  181. static const intel_limit_t intel_limits_i8xx_dvo = {
  182. .dot = { .min = 25000, .max = 350000 },
  183. .vco = { .min = 908000, .max = 1512000 },
  184. .n = { .min = 2, .max = 16 },
  185. .m = { .min = 96, .max = 140 },
  186. .m1 = { .min = 18, .max = 26 },
  187. .m2 = { .min = 6, .max = 16 },
  188. .p = { .min = 4, .max = 128 },
  189. .p1 = { .min = 2, .max = 33 },
  190. .p2 = { .dot_limit = 165000,
  191. .p2_slow = 4, .p2_fast = 4 },
  192. };
  193. static const intel_limit_t intel_limits_i8xx_lvds = {
  194. .dot = { .min = 25000, .max = 350000 },
  195. .vco = { .min = 908000, .max = 1512000 },
  196. .n = { .min = 2, .max = 16 },
  197. .m = { .min = 96, .max = 140 },
  198. .m1 = { .min = 18, .max = 26 },
  199. .m2 = { .min = 6, .max = 16 },
  200. .p = { .min = 4, .max = 128 },
  201. .p1 = { .min = 1, .max = 6 },
  202. .p2 = { .dot_limit = 165000,
  203. .p2_slow = 14, .p2_fast = 7 },
  204. };
  205. static const intel_limit_t intel_limits_i9xx_sdvo = {
  206. .dot = { .min = 20000, .max = 400000 },
  207. .vco = { .min = 1400000, .max = 2800000 },
  208. .n = { .min = 1, .max = 6 },
  209. .m = { .min = 70, .max = 120 },
  210. .m1 = { .min = 8, .max = 18 },
  211. .m2 = { .min = 3, .max = 7 },
  212. .p = { .min = 5, .max = 80 },
  213. .p1 = { .min = 1, .max = 8 },
  214. .p2 = { .dot_limit = 200000,
  215. .p2_slow = 10, .p2_fast = 5 },
  216. };
  217. static const intel_limit_t intel_limits_i9xx_lvds = {
  218. .dot = { .min = 20000, .max = 400000 },
  219. .vco = { .min = 1400000, .max = 2800000 },
  220. .n = { .min = 1, .max = 6 },
  221. .m = { .min = 70, .max = 120 },
  222. .m1 = { .min = 8, .max = 18 },
  223. .m2 = { .min = 3, .max = 7 },
  224. .p = { .min = 7, .max = 98 },
  225. .p1 = { .min = 1, .max = 8 },
  226. .p2 = { .dot_limit = 112000,
  227. .p2_slow = 14, .p2_fast = 7 },
  228. };
  229. static const intel_limit_t intel_limits_g4x_sdvo = {
  230. .dot = { .min = 25000, .max = 270000 },
  231. .vco = { .min = 1750000, .max = 3500000},
  232. .n = { .min = 1, .max = 4 },
  233. .m = { .min = 104, .max = 138 },
  234. .m1 = { .min = 17, .max = 23 },
  235. .m2 = { .min = 5, .max = 11 },
  236. .p = { .min = 10, .max = 30 },
  237. .p1 = { .min = 1, .max = 3},
  238. .p2 = { .dot_limit = 270000,
  239. .p2_slow = 10,
  240. .p2_fast = 10
  241. },
  242. };
  243. static const intel_limit_t intel_limits_g4x_hdmi = {
  244. .dot = { .min = 22000, .max = 400000 },
  245. .vco = { .min = 1750000, .max = 3500000},
  246. .n = { .min = 1, .max = 4 },
  247. .m = { .min = 104, .max = 138 },
  248. .m1 = { .min = 16, .max = 23 },
  249. .m2 = { .min = 5, .max = 11 },
  250. .p = { .min = 5, .max = 80 },
  251. .p1 = { .min = 1, .max = 8},
  252. .p2 = { .dot_limit = 165000,
  253. .p2_slow = 10, .p2_fast = 5 },
  254. };
  255. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  256. .dot = { .min = 20000, .max = 115000 },
  257. .vco = { .min = 1750000, .max = 3500000 },
  258. .n = { .min = 1, .max = 3 },
  259. .m = { .min = 104, .max = 138 },
  260. .m1 = { .min = 17, .max = 23 },
  261. .m2 = { .min = 5, .max = 11 },
  262. .p = { .min = 28, .max = 112 },
  263. .p1 = { .min = 2, .max = 8 },
  264. .p2 = { .dot_limit = 0,
  265. .p2_slow = 14, .p2_fast = 14
  266. },
  267. };
  268. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  269. .dot = { .min = 80000, .max = 224000 },
  270. .vco = { .min = 1750000, .max = 3500000 },
  271. .n = { .min = 1, .max = 3 },
  272. .m = { .min = 104, .max = 138 },
  273. .m1 = { .min = 17, .max = 23 },
  274. .m2 = { .min = 5, .max = 11 },
  275. .p = { .min = 14, .max = 42 },
  276. .p1 = { .min = 2, .max = 6 },
  277. .p2 = { .dot_limit = 0,
  278. .p2_slow = 7, .p2_fast = 7
  279. },
  280. };
  281. static const intel_limit_t intel_limits_pineview_sdvo = {
  282. .dot = { .min = 20000, .max = 400000},
  283. .vco = { .min = 1700000, .max = 3500000 },
  284. /* Pineview's Ncounter is a ring counter */
  285. .n = { .min = 3, .max = 6 },
  286. .m = { .min = 2, .max = 256 },
  287. /* Pineview only has one combined m divider, which we treat as m2. */
  288. .m1 = { .min = 0, .max = 0 },
  289. .m2 = { .min = 0, .max = 254 },
  290. .p = { .min = 5, .max = 80 },
  291. .p1 = { .min = 1, .max = 8 },
  292. .p2 = { .dot_limit = 200000,
  293. .p2_slow = 10, .p2_fast = 5 },
  294. };
  295. static const intel_limit_t intel_limits_pineview_lvds = {
  296. .dot = { .min = 20000, .max = 400000 },
  297. .vco = { .min = 1700000, .max = 3500000 },
  298. .n = { .min = 3, .max = 6 },
  299. .m = { .min = 2, .max = 256 },
  300. .m1 = { .min = 0, .max = 0 },
  301. .m2 = { .min = 0, .max = 254 },
  302. .p = { .min = 7, .max = 112 },
  303. .p1 = { .min = 1, .max = 8 },
  304. .p2 = { .dot_limit = 112000,
  305. .p2_slow = 14, .p2_fast = 14 },
  306. };
  307. /* Ironlake / Sandybridge
  308. *
  309. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  310. * the range value for them is (actual_value - 2).
  311. */
  312. static const intel_limit_t intel_limits_ironlake_dac = {
  313. .dot = { .min = 25000, .max = 350000 },
  314. .vco = { .min = 1760000, .max = 3510000 },
  315. .n = { .min = 1, .max = 5 },
  316. .m = { .min = 79, .max = 127 },
  317. .m1 = { .min = 12, .max = 22 },
  318. .m2 = { .min = 5, .max = 9 },
  319. .p = { .min = 5, .max = 80 },
  320. .p1 = { .min = 1, .max = 8 },
  321. .p2 = { .dot_limit = 225000,
  322. .p2_slow = 10, .p2_fast = 5 },
  323. };
  324. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  325. .dot = { .min = 25000, .max = 350000 },
  326. .vco = { .min = 1760000, .max = 3510000 },
  327. .n = { .min = 1, .max = 3 },
  328. .m = { .min = 79, .max = 118 },
  329. .m1 = { .min = 12, .max = 22 },
  330. .m2 = { .min = 5, .max = 9 },
  331. .p = { .min = 28, .max = 112 },
  332. .p1 = { .min = 2, .max = 8 },
  333. .p2 = { .dot_limit = 225000,
  334. .p2_slow = 14, .p2_fast = 14 },
  335. };
  336. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  337. .dot = { .min = 25000, .max = 350000 },
  338. .vco = { .min = 1760000, .max = 3510000 },
  339. .n = { .min = 1, .max = 3 },
  340. .m = { .min = 79, .max = 127 },
  341. .m1 = { .min = 12, .max = 22 },
  342. .m2 = { .min = 5, .max = 9 },
  343. .p = { .min = 14, .max = 56 },
  344. .p1 = { .min = 2, .max = 8 },
  345. .p2 = { .dot_limit = 225000,
  346. .p2_slow = 7, .p2_fast = 7 },
  347. };
  348. /* LVDS 100mhz refclk limits. */
  349. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  350. .dot = { .min = 25000, .max = 350000 },
  351. .vco = { .min = 1760000, .max = 3510000 },
  352. .n = { .min = 1, .max = 2 },
  353. .m = { .min = 79, .max = 126 },
  354. .m1 = { .min = 12, .max = 22 },
  355. .m2 = { .min = 5, .max = 9 },
  356. .p = { .min = 28, .max = 112 },
  357. .p1 = { .min = 2, .max = 8 },
  358. .p2 = { .dot_limit = 225000,
  359. .p2_slow = 14, .p2_fast = 14 },
  360. };
  361. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  362. .dot = { .min = 25000, .max = 350000 },
  363. .vco = { .min = 1760000, .max = 3510000 },
  364. .n = { .min = 1, .max = 3 },
  365. .m = { .min = 79, .max = 126 },
  366. .m1 = { .min = 12, .max = 22 },
  367. .m2 = { .min = 5, .max = 9 },
  368. .p = { .min = 14, .max = 42 },
  369. .p1 = { .min = 2, .max = 6 },
  370. .p2 = { .dot_limit = 225000,
  371. .p2_slow = 7, .p2_fast = 7 },
  372. };
  373. static const intel_limit_t intel_limits_vlv = {
  374. /*
  375. * These are the data rate limits (measured in fast clocks)
  376. * since those are the strictest limits we have. The fast
  377. * clock and actual rate limits are more relaxed, so checking
  378. * them would make no difference.
  379. */
  380. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  381. .vco = { .min = 4000000, .max = 6000000 },
  382. .n = { .min = 1, .max = 7 },
  383. .m1 = { .min = 2, .max = 3 },
  384. .m2 = { .min = 11, .max = 156 },
  385. .p1 = { .min = 2, .max = 3 },
  386. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  387. };
  388. static const intel_limit_t intel_limits_chv = {
  389. /*
  390. * These are the data rate limits (measured in fast clocks)
  391. * since those are the strictest limits we have. The fast
  392. * clock and actual rate limits are more relaxed, so checking
  393. * them would make no difference.
  394. */
  395. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  396. .vco = { .min = 4800000, .max = 6480000 },
  397. .n = { .min = 1, .max = 1 },
  398. .m1 = { .min = 2, .max = 2 },
  399. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  400. .p1 = { .min = 2, .max = 4 },
  401. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  402. };
  403. static const intel_limit_t intel_limits_bxt = {
  404. /* FIXME: find real dot limits */
  405. .dot = { .min = 0, .max = INT_MAX },
  406. .vco = { .min = 4800000, .max = 6700000 },
  407. .n = { .min = 1, .max = 1 },
  408. .m1 = { .min = 2, .max = 2 },
  409. /* FIXME: find real m2 limits */
  410. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  411. .p1 = { .min = 2, .max = 4 },
  412. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  413. };
  414. static bool
  415. needs_modeset(struct drm_crtc_state *state)
  416. {
  417. return drm_atomic_crtc_needs_modeset(state);
  418. }
  419. /**
  420. * Returns whether any output on the specified pipe is of the specified type
  421. */
  422. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  423. {
  424. struct drm_device *dev = crtc->base.dev;
  425. struct intel_encoder *encoder;
  426. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  427. if (encoder->type == type)
  428. return true;
  429. return false;
  430. }
  431. /**
  432. * Returns whether any output on the specified pipe will have the specified
  433. * type after a staged modeset is complete, i.e., the same as
  434. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  435. * encoder->crtc.
  436. */
  437. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  438. int type)
  439. {
  440. struct drm_atomic_state *state = crtc_state->base.state;
  441. struct drm_connector *connector;
  442. struct drm_connector_state *connector_state;
  443. struct intel_encoder *encoder;
  444. int i, num_connectors = 0;
  445. for_each_connector_in_state(state, connector, connector_state, i) {
  446. if (connector_state->crtc != crtc_state->base.crtc)
  447. continue;
  448. num_connectors++;
  449. encoder = to_intel_encoder(connector_state->best_encoder);
  450. if (encoder->type == type)
  451. return true;
  452. }
  453. WARN_ON(num_connectors == 0);
  454. return false;
  455. }
  456. static const intel_limit_t *
  457. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  458. {
  459. struct drm_device *dev = crtc_state->base.crtc->dev;
  460. const intel_limit_t *limit;
  461. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  462. if (intel_is_dual_link_lvds(dev)) {
  463. if (refclk == 100000)
  464. limit = &intel_limits_ironlake_dual_lvds_100m;
  465. else
  466. limit = &intel_limits_ironlake_dual_lvds;
  467. } else {
  468. if (refclk == 100000)
  469. limit = &intel_limits_ironlake_single_lvds_100m;
  470. else
  471. limit = &intel_limits_ironlake_single_lvds;
  472. }
  473. } else
  474. limit = &intel_limits_ironlake_dac;
  475. return limit;
  476. }
  477. static const intel_limit_t *
  478. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  479. {
  480. struct drm_device *dev = crtc_state->base.crtc->dev;
  481. const intel_limit_t *limit;
  482. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  483. if (intel_is_dual_link_lvds(dev))
  484. limit = &intel_limits_g4x_dual_channel_lvds;
  485. else
  486. limit = &intel_limits_g4x_single_channel_lvds;
  487. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  488. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  489. limit = &intel_limits_g4x_hdmi;
  490. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  491. limit = &intel_limits_g4x_sdvo;
  492. } else /* The option is for other outputs */
  493. limit = &intel_limits_i9xx_sdvo;
  494. return limit;
  495. }
  496. static const intel_limit_t *
  497. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  498. {
  499. struct drm_device *dev = crtc_state->base.crtc->dev;
  500. const intel_limit_t *limit;
  501. if (IS_BROXTON(dev))
  502. limit = &intel_limits_bxt;
  503. else if (HAS_PCH_SPLIT(dev))
  504. limit = intel_ironlake_limit(crtc_state, refclk);
  505. else if (IS_G4X(dev)) {
  506. limit = intel_g4x_limit(crtc_state);
  507. } else if (IS_PINEVIEW(dev)) {
  508. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  509. limit = &intel_limits_pineview_lvds;
  510. else
  511. limit = &intel_limits_pineview_sdvo;
  512. } else if (IS_CHERRYVIEW(dev)) {
  513. limit = &intel_limits_chv;
  514. } else if (IS_VALLEYVIEW(dev)) {
  515. limit = &intel_limits_vlv;
  516. } else if (!IS_GEN2(dev)) {
  517. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  518. limit = &intel_limits_i9xx_lvds;
  519. else
  520. limit = &intel_limits_i9xx_sdvo;
  521. } else {
  522. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  523. limit = &intel_limits_i8xx_lvds;
  524. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  525. limit = &intel_limits_i8xx_dvo;
  526. else
  527. limit = &intel_limits_i8xx_dac;
  528. }
  529. return limit;
  530. }
  531. /*
  532. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  533. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  534. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  535. * The helpers' return value is the rate of the clock that is fed to the
  536. * display engine's pipe which can be the above fast dot clock rate or a
  537. * divided-down version of it.
  538. */
  539. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  540. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  541. {
  542. clock->m = clock->m2 + 2;
  543. clock->p = clock->p1 * clock->p2;
  544. if (WARN_ON(clock->n == 0 || clock->p == 0))
  545. return 0;
  546. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  547. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  548. return clock->dot;
  549. }
  550. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  551. {
  552. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  553. }
  554. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  555. {
  556. clock->m = i9xx_dpll_compute_m(clock);
  557. clock->p = clock->p1 * clock->p2;
  558. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  559. return 0;
  560. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  561. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  562. return clock->dot;
  563. }
  564. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  565. {
  566. clock->m = clock->m1 * clock->m2;
  567. clock->p = clock->p1 * clock->p2;
  568. if (WARN_ON(clock->n == 0 || clock->p == 0))
  569. return 0;
  570. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  571. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  572. return clock->dot / 5;
  573. }
  574. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  575. {
  576. clock->m = clock->m1 * clock->m2;
  577. clock->p = clock->p1 * clock->p2;
  578. if (WARN_ON(clock->n == 0 || clock->p == 0))
  579. return 0;
  580. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  581. clock->n << 22);
  582. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  583. return clock->dot / 5;
  584. }
  585. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  586. /**
  587. * Returns whether the given set of divisors are valid for a given refclk with
  588. * the given connectors.
  589. */
  590. static bool intel_PLL_is_valid(struct drm_device *dev,
  591. const intel_limit_t *limit,
  592. const intel_clock_t *clock)
  593. {
  594. if (clock->n < limit->n.min || limit->n.max < clock->n)
  595. INTELPllInvalid("n out of range\n");
  596. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  597. INTELPllInvalid("p1 out of range\n");
  598. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  599. INTELPllInvalid("m2 out of range\n");
  600. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  601. INTELPllInvalid("m1 out of range\n");
  602. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  603. if (clock->m1 <= clock->m2)
  604. INTELPllInvalid("m1 <= m2\n");
  605. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  606. if (clock->p < limit->p.min || limit->p.max < clock->p)
  607. INTELPllInvalid("p out of range\n");
  608. if (clock->m < limit->m.min || limit->m.max < clock->m)
  609. INTELPllInvalid("m out of range\n");
  610. }
  611. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  612. INTELPllInvalid("vco out of range\n");
  613. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  614. * connector, etc., rather than just a single range.
  615. */
  616. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  617. INTELPllInvalid("dot out of range\n");
  618. return true;
  619. }
  620. static int
  621. i9xx_select_p2_div(const intel_limit_t *limit,
  622. const struct intel_crtc_state *crtc_state,
  623. int target)
  624. {
  625. struct drm_device *dev = crtc_state->base.crtc->dev;
  626. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  627. /*
  628. * For LVDS just rely on its current settings for dual-channel.
  629. * We haven't figured out how to reliably set up different
  630. * single/dual channel state, if we even can.
  631. */
  632. if (intel_is_dual_link_lvds(dev))
  633. return limit->p2.p2_fast;
  634. else
  635. return limit->p2.p2_slow;
  636. } else {
  637. if (target < limit->p2.dot_limit)
  638. return limit->p2.p2_slow;
  639. else
  640. return limit->p2.p2_fast;
  641. }
  642. }
  643. static bool
  644. i9xx_find_best_dpll(const intel_limit_t *limit,
  645. struct intel_crtc_state *crtc_state,
  646. int target, int refclk, intel_clock_t *match_clock,
  647. intel_clock_t *best_clock)
  648. {
  649. struct drm_device *dev = crtc_state->base.crtc->dev;
  650. intel_clock_t clock;
  651. int err = target;
  652. memset(best_clock, 0, sizeof(*best_clock));
  653. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  654. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  655. clock.m1++) {
  656. for (clock.m2 = limit->m2.min;
  657. clock.m2 <= limit->m2.max; clock.m2++) {
  658. if (clock.m2 >= clock.m1)
  659. break;
  660. for (clock.n = limit->n.min;
  661. clock.n <= limit->n.max; clock.n++) {
  662. for (clock.p1 = limit->p1.min;
  663. clock.p1 <= limit->p1.max; clock.p1++) {
  664. int this_err;
  665. i9xx_calc_dpll_params(refclk, &clock);
  666. if (!intel_PLL_is_valid(dev, limit,
  667. &clock))
  668. continue;
  669. if (match_clock &&
  670. clock.p != match_clock->p)
  671. continue;
  672. this_err = abs(clock.dot - target);
  673. if (this_err < err) {
  674. *best_clock = clock;
  675. err = this_err;
  676. }
  677. }
  678. }
  679. }
  680. }
  681. return (err != target);
  682. }
  683. static bool
  684. pnv_find_best_dpll(const intel_limit_t *limit,
  685. struct intel_crtc_state *crtc_state,
  686. int target, int refclk, intel_clock_t *match_clock,
  687. intel_clock_t *best_clock)
  688. {
  689. struct drm_device *dev = crtc_state->base.crtc->dev;
  690. intel_clock_t clock;
  691. int err = target;
  692. memset(best_clock, 0, sizeof(*best_clock));
  693. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  694. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  695. clock.m1++) {
  696. for (clock.m2 = limit->m2.min;
  697. clock.m2 <= limit->m2.max; clock.m2++) {
  698. for (clock.n = limit->n.min;
  699. clock.n <= limit->n.max; clock.n++) {
  700. for (clock.p1 = limit->p1.min;
  701. clock.p1 <= limit->p1.max; clock.p1++) {
  702. int this_err;
  703. pnv_calc_dpll_params(refclk, &clock);
  704. if (!intel_PLL_is_valid(dev, limit,
  705. &clock))
  706. continue;
  707. if (match_clock &&
  708. clock.p != match_clock->p)
  709. continue;
  710. this_err = abs(clock.dot - target);
  711. if (this_err < err) {
  712. *best_clock = clock;
  713. err = this_err;
  714. }
  715. }
  716. }
  717. }
  718. }
  719. return (err != target);
  720. }
  721. static bool
  722. g4x_find_best_dpll(const intel_limit_t *limit,
  723. struct intel_crtc_state *crtc_state,
  724. int target, int refclk, intel_clock_t *match_clock,
  725. intel_clock_t *best_clock)
  726. {
  727. struct drm_device *dev = crtc_state->base.crtc->dev;
  728. intel_clock_t clock;
  729. int max_n;
  730. bool found = false;
  731. /* approximately equals target * 0.00585 */
  732. int err_most = (target >> 8) + (target >> 9);
  733. memset(best_clock, 0, sizeof(*best_clock));
  734. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  735. max_n = limit->n.max;
  736. /* based on hardware requirement, prefer smaller n to precision */
  737. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  738. /* based on hardware requirement, prefere larger m1,m2 */
  739. for (clock.m1 = limit->m1.max;
  740. clock.m1 >= limit->m1.min; clock.m1--) {
  741. for (clock.m2 = limit->m2.max;
  742. clock.m2 >= limit->m2.min; clock.m2--) {
  743. for (clock.p1 = limit->p1.max;
  744. clock.p1 >= limit->p1.min; clock.p1--) {
  745. int this_err;
  746. i9xx_calc_dpll_params(refclk, &clock);
  747. if (!intel_PLL_is_valid(dev, limit,
  748. &clock))
  749. continue;
  750. this_err = abs(clock.dot - target);
  751. if (this_err < err_most) {
  752. *best_clock = clock;
  753. err_most = this_err;
  754. max_n = clock.n;
  755. found = true;
  756. }
  757. }
  758. }
  759. }
  760. }
  761. return found;
  762. }
  763. /*
  764. * Check if the calculated PLL configuration is more optimal compared to the
  765. * best configuration and error found so far. Return the calculated error.
  766. */
  767. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  768. const intel_clock_t *calculated_clock,
  769. const intel_clock_t *best_clock,
  770. unsigned int best_error_ppm,
  771. unsigned int *error_ppm)
  772. {
  773. /*
  774. * For CHV ignore the error and consider only the P value.
  775. * Prefer a bigger P value based on HW requirements.
  776. */
  777. if (IS_CHERRYVIEW(dev)) {
  778. *error_ppm = 0;
  779. return calculated_clock->p > best_clock->p;
  780. }
  781. if (WARN_ON_ONCE(!target_freq))
  782. return false;
  783. *error_ppm = div_u64(1000000ULL *
  784. abs(target_freq - calculated_clock->dot),
  785. target_freq);
  786. /*
  787. * Prefer a better P value over a better (smaller) error if the error
  788. * is small. Ensure this preference for future configurations too by
  789. * setting the error to 0.
  790. */
  791. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  792. *error_ppm = 0;
  793. return true;
  794. }
  795. return *error_ppm + 10 < best_error_ppm;
  796. }
  797. static bool
  798. vlv_find_best_dpll(const intel_limit_t *limit,
  799. struct intel_crtc_state *crtc_state,
  800. int target, int refclk, intel_clock_t *match_clock,
  801. intel_clock_t *best_clock)
  802. {
  803. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  804. struct drm_device *dev = crtc->base.dev;
  805. intel_clock_t clock;
  806. unsigned int bestppm = 1000000;
  807. /* min update 19.2 MHz */
  808. int max_n = min(limit->n.max, refclk / 19200);
  809. bool found = false;
  810. target *= 5; /* fast clock */
  811. memset(best_clock, 0, sizeof(*best_clock));
  812. /* based on hardware requirement, prefer smaller n to precision */
  813. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  814. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  815. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  816. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  817. clock.p = clock.p1 * clock.p2;
  818. /* based on hardware requirement, prefer bigger m1,m2 values */
  819. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  820. unsigned int ppm;
  821. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  822. refclk * clock.m1);
  823. vlv_calc_dpll_params(refclk, &clock);
  824. if (!intel_PLL_is_valid(dev, limit,
  825. &clock))
  826. continue;
  827. if (!vlv_PLL_is_optimal(dev, target,
  828. &clock,
  829. best_clock,
  830. bestppm, &ppm))
  831. continue;
  832. *best_clock = clock;
  833. bestppm = ppm;
  834. found = true;
  835. }
  836. }
  837. }
  838. }
  839. return found;
  840. }
  841. static bool
  842. chv_find_best_dpll(const intel_limit_t *limit,
  843. struct intel_crtc_state *crtc_state,
  844. int target, int refclk, intel_clock_t *match_clock,
  845. intel_clock_t *best_clock)
  846. {
  847. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  848. struct drm_device *dev = crtc->base.dev;
  849. unsigned int best_error_ppm;
  850. intel_clock_t clock;
  851. uint64_t m2;
  852. int found = false;
  853. memset(best_clock, 0, sizeof(*best_clock));
  854. best_error_ppm = 1000000;
  855. /*
  856. * Based on hardware doc, the n always set to 1, and m1 always
  857. * set to 2. If requires to support 200Mhz refclk, we need to
  858. * revisit this because n may not 1 anymore.
  859. */
  860. clock.n = 1, clock.m1 = 2;
  861. target *= 5; /* fast clock */
  862. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  863. for (clock.p2 = limit->p2.p2_fast;
  864. clock.p2 >= limit->p2.p2_slow;
  865. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  866. unsigned int error_ppm;
  867. clock.p = clock.p1 * clock.p2;
  868. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  869. clock.n) << 22, refclk * clock.m1);
  870. if (m2 > INT_MAX/clock.m1)
  871. continue;
  872. clock.m2 = m2;
  873. chv_calc_dpll_params(refclk, &clock);
  874. if (!intel_PLL_is_valid(dev, limit, &clock))
  875. continue;
  876. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  877. best_error_ppm, &error_ppm))
  878. continue;
  879. *best_clock = clock;
  880. best_error_ppm = error_ppm;
  881. found = true;
  882. }
  883. }
  884. return found;
  885. }
  886. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  887. intel_clock_t *best_clock)
  888. {
  889. int refclk = i9xx_get_refclk(crtc_state, 0);
  890. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  891. target_clock, refclk, NULL, best_clock);
  892. }
  893. bool intel_crtc_active(struct drm_crtc *crtc)
  894. {
  895. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  896. /* Be paranoid as we can arrive here with only partial
  897. * state retrieved from the hardware during setup.
  898. *
  899. * We can ditch the adjusted_mode.crtc_clock check as soon
  900. * as Haswell has gained clock readout/fastboot support.
  901. *
  902. * We can ditch the crtc->primary->fb check as soon as we can
  903. * properly reconstruct framebuffers.
  904. *
  905. * FIXME: The intel_crtc->active here should be switched to
  906. * crtc->state->active once we have proper CRTC states wired up
  907. * for atomic.
  908. */
  909. return intel_crtc->active && crtc->primary->state->fb &&
  910. intel_crtc->config->base.adjusted_mode.crtc_clock;
  911. }
  912. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  913. enum pipe pipe)
  914. {
  915. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  916. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  917. return intel_crtc->config->cpu_transcoder;
  918. }
  919. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  920. {
  921. struct drm_i915_private *dev_priv = dev->dev_private;
  922. u32 reg = PIPEDSL(pipe);
  923. u32 line1, line2;
  924. u32 line_mask;
  925. if (IS_GEN2(dev))
  926. line_mask = DSL_LINEMASK_GEN2;
  927. else
  928. line_mask = DSL_LINEMASK_GEN3;
  929. line1 = I915_READ(reg) & line_mask;
  930. msleep(5);
  931. line2 = I915_READ(reg) & line_mask;
  932. return line1 == line2;
  933. }
  934. /*
  935. * intel_wait_for_pipe_off - wait for pipe to turn off
  936. * @crtc: crtc whose pipe to wait for
  937. *
  938. * After disabling a pipe, we can't wait for vblank in the usual way,
  939. * spinning on the vblank interrupt status bit, since we won't actually
  940. * see an interrupt when the pipe is disabled.
  941. *
  942. * On Gen4 and above:
  943. * wait for the pipe register state bit to turn off
  944. *
  945. * Otherwise:
  946. * wait for the display line value to settle (it usually
  947. * ends up stopping at the start of the next frame).
  948. *
  949. */
  950. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  951. {
  952. struct drm_device *dev = crtc->base.dev;
  953. struct drm_i915_private *dev_priv = dev->dev_private;
  954. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  955. enum pipe pipe = crtc->pipe;
  956. if (INTEL_INFO(dev)->gen >= 4) {
  957. int reg = PIPECONF(cpu_transcoder);
  958. /* Wait for the Pipe State to go off */
  959. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  960. 100))
  961. WARN(1, "pipe_off wait timed out\n");
  962. } else {
  963. /* Wait for the display line to settle */
  964. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  965. WARN(1, "pipe_off wait timed out\n");
  966. }
  967. }
  968. static const char *state_string(bool enabled)
  969. {
  970. return enabled ? "on" : "off";
  971. }
  972. /* Only for pre-ILK configs */
  973. void assert_pll(struct drm_i915_private *dev_priv,
  974. enum pipe pipe, bool state)
  975. {
  976. int reg;
  977. u32 val;
  978. bool cur_state;
  979. reg = DPLL(pipe);
  980. val = I915_READ(reg);
  981. cur_state = !!(val & DPLL_VCO_ENABLE);
  982. I915_STATE_WARN(cur_state != state,
  983. "PLL state assertion failure (expected %s, current %s)\n",
  984. state_string(state), state_string(cur_state));
  985. }
  986. /* XXX: the dsi pll is shared between MIPI DSI ports */
  987. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  988. {
  989. u32 val;
  990. bool cur_state;
  991. mutex_lock(&dev_priv->sb_lock);
  992. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  993. mutex_unlock(&dev_priv->sb_lock);
  994. cur_state = val & DSI_PLL_VCO_EN;
  995. I915_STATE_WARN(cur_state != state,
  996. "DSI PLL state assertion failure (expected %s, current %s)\n",
  997. state_string(state), state_string(cur_state));
  998. }
  999. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1000. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1001. struct intel_shared_dpll *
  1002. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1003. {
  1004. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1005. if (crtc->config->shared_dpll < 0)
  1006. return NULL;
  1007. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1008. }
  1009. /* For ILK+ */
  1010. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1011. struct intel_shared_dpll *pll,
  1012. bool state)
  1013. {
  1014. bool cur_state;
  1015. struct intel_dpll_hw_state hw_state;
  1016. if (WARN (!pll,
  1017. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1018. return;
  1019. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1020. I915_STATE_WARN(cur_state != state,
  1021. "%s assertion failure (expected %s, current %s)\n",
  1022. pll->name, state_string(state), state_string(cur_state));
  1023. }
  1024. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1025. enum pipe pipe, bool state)
  1026. {
  1027. int reg;
  1028. u32 val;
  1029. bool cur_state;
  1030. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1031. pipe);
  1032. if (HAS_DDI(dev_priv->dev)) {
  1033. /* DDI does not have a specific FDI_TX register */
  1034. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1035. val = I915_READ(reg);
  1036. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1037. } else {
  1038. reg = FDI_TX_CTL(pipe);
  1039. val = I915_READ(reg);
  1040. cur_state = !!(val & FDI_TX_ENABLE);
  1041. }
  1042. I915_STATE_WARN(cur_state != state,
  1043. "FDI TX state assertion failure (expected %s, current %s)\n",
  1044. state_string(state), state_string(cur_state));
  1045. }
  1046. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1047. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1048. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1049. enum pipe pipe, bool state)
  1050. {
  1051. int reg;
  1052. u32 val;
  1053. bool cur_state;
  1054. reg = FDI_RX_CTL(pipe);
  1055. val = I915_READ(reg);
  1056. cur_state = !!(val & FDI_RX_ENABLE);
  1057. I915_STATE_WARN(cur_state != state,
  1058. "FDI RX state assertion failure (expected %s, current %s)\n",
  1059. state_string(state), state_string(cur_state));
  1060. }
  1061. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1062. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1063. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1064. enum pipe pipe)
  1065. {
  1066. int reg;
  1067. u32 val;
  1068. /* ILK FDI PLL is always enabled */
  1069. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1070. return;
  1071. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1072. if (HAS_DDI(dev_priv->dev))
  1073. return;
  1074. reg = FDI_TX_CTL(pipe);
  1075. val = I915_READ(reg);
  1076. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1077. }
  1078. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1079. enum pipe pipe, bool state)
  1080. {
  1081. int reg;
  1082. u32 val;
  1083. bool cur_state;
  1084. reg = FDI_RX_CTL(pipe);
  1085. val = I915_READ(reg);
  1086. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1087. I915_STATE_WARN(cur_state != state,
  1088. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1089. state_string(state), state_string(cur_state));
  1090. }
  1091. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1092. enum pipe pipe)
  1093. {
  1094. struct drm_device *dev = dev_priv->dev;
  1095. int pp_reg;
  1096. u32 val;
  1097. enum pipe panel_pipe = PIPE_A;
  1098. bool locked = true;
  1099. if (WARN_ON(HAS_DDI(dev)))
  1100. return;
  1101. if (HAS_PCH_SPLIT(dev)) {
  1102. u32 port_sel;
  1103. pp_reg = PCH_PP_CONTROL;
  1104. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1105. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1106. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1107. panel_pipe = PIPE_B;
  1108. /* XXX: else fix for eDP */
  1109. } else if (IS_VALLEYVIEW(dev)) {
  1110. /* presumably write lock depends on pipe, not port select */
  1111. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1112. panel_pipe = pipe;
  1113. } else {
  1114. pp_reg = PP_CONTROL;
  1115. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1116. panel_pipe = PIPE_B;
  1117. }
  1118. val = I915_READ(pp_reg);
  1119. if (!(val & PANEL_POWER_ON) ||
  1120. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1121. locked = false;
  1122. I915_STATE_WARN(panel_pipe == pipe && locked,
  1123. "panel assertion failure, pipe %c regs locked\n",
  1124. pipe_name(pipe));
  1125. }
  1126. static void assert_cursor(struct drm_i915_private *dev_priv,
  1127. enum pipe pipe, bool state)
  1128. {
  1129. struct drm_device *dev = dev_priv->dev;
  1130. bool cur_state;
  1131. if (IS_845G(dev) || IS_I865G(dev))
  1132. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1133. else
  1134. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1135. I915_STATE_WARN(cur_state != state,
  1136. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1137. pipe_name(pipe), state_string(state), state_string(cur_state));
  1138. }
  1139. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1140. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1141. void assert_pipe(struct drm_i915_private *dev_priv,
  1142. enum pipe pipe, bool state)
  1143. {
  1144. int reg;
  1145. u32 val;
  1146. bool cur_state;
  1147. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1148. pipe);
  1149. /* if we need the pipe quirk it must be always on */
  1150. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1151. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1152. state = true;
  1153. if (!intel_display_power_is_enabled(dev_priv,
  1154. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1155. cur_state = false;
  1156. } else {
  1157. reg = PIPECONF(cpu_transcoder);
  1158. val = I915_READ(reg);
  1159. cur_state = !!(val & PIPECONF_ENABLE);
  1160. }
  1161. I915_STATE_WARN(cur_state != state,
  1162. "pipe %c assertion failure (expected %s, current %s)\n",
  1163. pipe_name(pipe), state_string(state), state_string(cur_state));
  1164. }
  1165. static void assert_plane(struct drm_i915_private *dev_priv,
  1166. enum plane plane, bool state)
  1167. {
  1168. int reg;
  1169. u32 val;
  1170. bool cur_state;
  1171. reg = DSPCNTR(plane);
  1172. val = I915_READ(reg);
  1173. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1174. I915_STATE_WARN(cur_state != state,
  1175. "plane %c assertion failure (expected %s, current %s)\n",
  1176. plane_name(plane), state_string(state), state_string(cur_state));
  1177. }
  1178. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1179. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1180. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1181. enum pipe pipe)
  1182. {
  1183. struct drm_device *dev = dev_priv->dev;
  1184. int reg, i;
  1185. u32 val;
  1186. int cur_pipe;
  1187. /* Primary planes are fixed to pipes on gen4+ */
  1188. if (INTEL_INFO(dev)->gen >= 4) {
  1189. reg = DSPCNTR(pipe);
  1190. val = I915_READ(reg);
  1191. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1192. "plane %c assertion failure, should be disabled but not\n",
  1193. plane_name(pipe));
  1194. return;
  1195. }
  1196. /* Need to check both planes against the pipe */
  1197. for_each_pipe(dev_priv, i) {
  1198. reg = DSPCNTR(i);
  1199. val = I915_READ(reg);
  1200. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1201. DISPPLANE_SEL_PIPE_SHIFT;
  1202. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1203. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1204. plane_name(i), pipe_name(pipe));
  1205. }
  1206. }
  1207. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1208. enum pipe pipe)
  1209. {
  1210. struct drm_device *dev = dev_priv->dev;
  1211. int reg, sprite;
  1212. u32 val;
  1213. if (INTEL_INFO(dev)->gen >= 9) {
  1214. for_each_sprite(dev_priv, pipe, sprite) {
  1215. val = I915_READ(PLANE_CTL(pipe, sprite));
  1216. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1217. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1218. sprite, pipe_name(pipe));
  1219. }
  1220. } else if (IS_VALLEYVIEW(dev)) {
  1221. for_each_sprite(dev_priv, pipe, sprite) {
  1222. reg = SPCNTR(pipe, sprite);
  1223. val = I915_READ(reg);
  1224. I915_STATE_WARN(val & SP_ENABLE,
  1225. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1226. sprite_name(pipe, sprite), pipe_name(pipe));
  1227. }
  1228. } else if (INTEL_INFO(dev)->gen >= 7) {
  1229. reg = SPRCTL(pipe);
  1230. val = I915_READ(reg);
  1231. I915_STATE_WARN(val & SPRITE_ENABLE,
  1232. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1233. plane_name(pipe), pipe_name(pipe));
  1234. } else if (INTEL_INFO(dev)->gen >= 5) {
  1235. reg = DVSCNTR(pipe);
  1236. val = I915_READ(reg);
  1237. I915_STATE_WARN(val & DVS_ENABLE,
  1238. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1239. plane_name(pipe), pipe_name(pipe));
  1240. }
  1241. }
  1242. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1243. {
  1244. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1245. drm_crtc_vblank_put(crtc);
  1246. }
  1247. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1248. {
  1249. u32 val;
  1250. bool enabled;
  1251. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1252. val = I915_READ(PCH_DREF_CONTROL);
  1253. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1254. DREF_SUPERSPREAD_SOURCE_MASK));
  1255. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1256. }
  1257. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1258. enum pipe pipe)
  1259. {
  1260. int reg;
  1261. u32 val;
  1262. bool enabled;
  1263. reg = PCH_TRANSCONF(pipe);
  1264. val = I915_READ(reg);
  1265. enabled = !!(val & TRANS_ENABLE);
  1266. I915_STATE_WARN(enabled,
  1267. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1268. pipe_name(pipe));
  1269. }
  1270. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1271. enum pipe pipe, u32 port_sel, u32 val)
  1272. {
  1273. if ((val & DP_PORT_EN) == 0)
  1274. return false;
  1275. if (HAS_PCH_CPT(dev_priv->dev)) {
  1276. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1277. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1278. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1279. return false;
  1280. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1281. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1282. return false;
  1283. } else {
  1284. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1285. return false;
  1286. }
  1287. return true;
  1288. }
  1289. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1290. enum pipe pipe, u32 val)
  1291. {
  1292. if ((val & SDVO_ENABLE) == 0)
  1293. return false;
  1294. if (HAS_PCH_CPT(dev_priv->dev)) {
  1295. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1296. return false;
  1297. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1298. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1299. return false;
  1300. } else {
  1301. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1302. return false;
  1303. }
  1304. return true;
  1305. }
  1306. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1307. enum pipe pipe, u32 val)
  1308. {
  1309. if ((val & LVDS_PORT_EN) == 0)
  1310. return false;
  1311. if (HAS_PCH_CPT(dev_priv->dev)) {
  1312. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1313. return false;
  1314. } else {
  1315. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1316. return false;
  1317. }
  1318. return true;
  1319. }
  1320. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1321. enum pipe pipe, u32 val)
  1322. {
  1323. if ((val & ADPA_DAC_ENABLE) == 0)
  1324. return false;
  1325. if (HAS_PCH_CPT(dev_priv->dev)) {
  1326. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1327. return false;
  1328. } else {
  1329. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1330. return false;
  1331. }
  1332. return true;
  1333. }
  1334. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1335. enum pipe pipe, int reg, u32 port_sel)
  1336. {
  1337. u32 val = I915_READ(reg);
  1338. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1339. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1340. reg, pipe_name(pipe));
  1341. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1342. && (val & DP_PIPEB_SELECT),
  1343. "IBX PCH dp port still using transcoder B\n");
  1344. }
  1345. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1346. enum pipe pipe, int reg)
  1347. {
  1348. u32 val = I915_READ(reg);
  1349. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1350. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1351. reg, pipe_name(pipe));
  1352. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1353. && (val & SDVO_PIPE_B_SELECT),
  1354. "IBX PCH hdmi port still using transcoder B\n");
  1355. }
  1356. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1357. enum pipe pipe)
  1358. {
  1359. int reg;
  1360. u32 val;
  1361. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1362. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1363. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1364. reg = PCH_ADPA;
  1365. val = I915_READ(reg);
  1366. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1367. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1368. pipe_name(pipe));
  1369. reg = PCH_LVDS;
  1370. val = I915_READ(reg);
  1371. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1372. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1373. pipe_name(pipe));
  1374. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1375. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1376. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1377. }
  1378. static void vlv_enable_pll(struct intel_crtc *crtc,
  1379. const struct intel_crtc_state *pipe_config)
  1380. {
  1381. struct drm_device *dev = crtc->base.dev;
  1382. struct drm_i915_private *dev_priv = dev->dev_private;
  1383. int reg = DPLL(crtc->pipe);
  1384. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1385. assert_pipe_disabled(dev_priv, crtc->pipe);
  1386. /* No really, not for ILK+ */
  1387. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1388. /* PLL is protected by panel, make sure we can write it */
  1389. if (IS_MOBILE(dev_priv->dev))
  1390. assert_panel_unlocked(dev_priv, crtc->pipe);
  1391. I915_WRITE(reg, dpll);
  1392. POSTING_READ(reg);
  1393. udelay(150);
  1394. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1395. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1396. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1397. POSTING_READ(DPLL_MD(crtc->pipe));
  1398. /* We do this three times for luck */
  1399. I915_WRITE(reg, dpll);
  1400. POSTING_READ(reg);
  1401. udelay(150); /* wait for warmup */
  1402. I915_WRITE(reg, dpll);
  1403. POSTING_READ(reg);
  1404. udelay(150); /* wait for warmup */
  1405. I915_WRITE(reg, dpll);
  1406. POSTING_READ(reg);
  1407. udelay(150); /* wait for warmup */
  1408. }
  1409. static void chv_enable_pll(struct intel_crtc *crtc,
  1410. const struct intel_crtc_state *pipe_config)
  1411. {
  1412. struct drm_device *dev = crtc->base.dev;
  1413. struct drm_i915_private *dev_priv = dev->dev_private;
  1414. int pipe = crtc->pipe;
  1415. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1416. u32 tmp;
  1417. assert_pipe_disabled(dev_priv, crtc->pipe);
  1418. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1419. mutex_lock(&dev_priv->sb_lock);
  1420. /* Enable back the 10bit clock to display controller */
  1421. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1422. tmp |= DPIO_DCLKP_EN;
  1423. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1424. mutex_unlock(&dev_priv->sb_lock);
  1425. /*
  1426. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1427. */
  1428. udelay(1);
  1429. /* Enable PLL */
  1430. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1431. /* Check PLL is locked */
  1432. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1433. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1434. /* not sure when this should be written */
  1435. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1436. POSTING_READ(DPLL_MD(pipe));
  1437. }
  1438. static int intel_num_dvo_pipes(struct drm_device *dev)
  1439. {
  1440. struct intel_crtc *crtc;
  1441. int count = 0;
  1442. for_each_intel_crtc(dev, crtc)
  1443. count += crtc->base.state->active &&
  1444. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1445. return count;
  1446. }
  1447. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1448. {
  1449. struct drm_device *dev = crtc->base.dev;
  1450. struct drm_i915_private *dev_priv = dev->dev_private;
  1451. int reg = DPLL(crtc->pipe);
  1452. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1453. assert_pipe_disabled(dev_priv, crtc->pipe);
  1454. /* No really, not for ILK+ */
  1455. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1456. /* PLL is protected by panel, make sure we can write it */
  1457. if (IS_MOBILE(dev) && !IS_I830(dev))
  1458. assert_panel_unlocked(dev_priv, crtc->pipe);
  1459. /* Enable DVO 2x clock on both PLLs if necessary */
  1460. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1461. /*
  1462. * It appears to be important that we don't enable this
  1463. * for the current pipe before otherwise configuring the
  1464. * PLL. No idea how this should be handled if multiple
  1465. * DVO outputs are enabled simultaneosly.
  1466. */
  1467. dpll |= DPLL_DVO_2X_MODE;
  1468. I915_WRITE(DPLL(!crtc->pipe),
  1469. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1470. }
  1471. /* Wait for the clocks to stabilize. */
  1472. POSTING_READ(reg);
  1473. udelay(150);
  1474. if (INTEL_INFO(dev)->gen >= 4) {
  1475. I915_WRITE(DPLL_MD(crtc->pipe),
  1476. crtc->config->dpll_hw_state.dpll_md);
  1477. } else {
  1478. /* The pixel multiplier can only be updated once the
  1479. * DPLL is enabled and the clocks are stable.
  1480. *
  1481. * So write it again.
  1482. */
  1483. I915_WRITE(reg, dpll);
  1484. }
  1485. /* We do this three times for luck */
  1486. I915_WRITE(reg, dpll);
  1487. POSTING_READ(reg);
  1488. udelay(150); /* wait for warmup */
  1489. I915_WRITE(reg, dpll);
  1490. POSTING_READ(reg);
  1491. udelay(150); /* wait for warmup */
  1492. I915_WRITE(reg, dpll);
  1493. POSTING_READ(reg);
  1494. udelay(150); /* wait for warmup */
  1495. }
  1496. /**
  1497. * i9xx_disable_pll - disable a PLL
  1498. * @dev_priv: i915 private structure
  1499. * @pipe: pipe PLL to disable
  1500. *
  1501. * Disable the PLL for @pipe, making sure the pipe is off first.
  1502. *
  1503. * Note! This is for pre-ILK only.
  1504. */
  1505. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1506. {
  1507. struct drm_device *dev = crtc->base.dev;
  1508. struct drm_i915_private *dev_priv = dev->dev_private;
  1509. enum pipe pipe = crtc->pipe;
  1510. /* Disable DVO 2x clock on both PLLs if necessary */
  1511. if (IS_I830(dev) &&
  1512. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1513. !intel_num_dvo_pipes(dev)) {
  1514. I915_WRITE(DPLL(PIPE_B),
  1515. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1516. I915_WRITE(DPLL(PIPE_A),
  1517. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1518. }
  1519. /* Don't disable pipe or pipe PLLs if needed */
  1520. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1521. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1522. return;
  1523. /* Make sure the pipe isn't still relying on us */
  1524. assert_pipe_disabled(dev_priv, pipe);
  1525. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1526. POSTING_READ(DPLL(pipe));
  1527. }
  1528. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1529. {
  1530. u32 val;
  1531. /* Make sure the pipe isn't still relying on us */
  1532. assert_pipe_disabled(dev_priv, pipe);
  1533. /*
  1534. * Leave integrated clock source and reference clock enabled for pipe B.
  1535. * The latter is needed for VGA hotplug / manual detection.
  1536. */
  1537. val = DPLL_VGA_MODE_DIS;
  1538. if (pipe == PIPE_B)
  1539. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1540. I915_WRITE(DPLL(pipe), val);
  1541. POSTING_READ(DPLL(pipe));
  1542. }
  1543. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1544. {
  1545. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1546. u32 val;
  1547. /* Make sure the pipe isn't still relying on us */
  1548. assert_pipe_disabled(dev_priv, pipe);
  1549. /* Set PLL en = 0 */
  1550. val = DPLL_SSC_REF_CLK_CHV |
  1551. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1552. if (pipe != PIPE_A)
  1553. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1554. I915_WRITE(DPLL(pipe), val);
  1555. POSTING_READ(DPLL(pipe));
  1556. mutex_lock(&dev_priv->sb_lock);
  1557. /* Disable 10bit clock to display controller */
  1558. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1559. val &= ~DPIO_DCLKP_EN;
  1560. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1561. mutex_unlock(&dev_priv->sb_lock);
  1562. }
  1563. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1564. struct intel_digital_port *dport,
  1565. unsigned int expected_mask)
  1566. {
  1567. u32 port_mask;
  1568. int dpll_reg;
  1569. switch (dport->port) {
  1570. case PORT_B:
  1571. port_mask = DPLL_PORTB_READY_MASK;
  1572. dpll_reg = DPLL(0);
  1573. break;
  1574. case PORT_C:
  1575. port_mask = DPLL_PORTC_READY_MASK;
  1576. dpll_reg = DPLL(0);
  1577. expected_mask <<= 4;
  1578. break;
  1579. case PORT_D:
  1580. port_mask = DPLL_PORTD_READY_MASK;
  1581. dpll_reg = DPIO_PHY_STATUS;
  1582. break;
  1583. default:
  1584. BUG();
  1585. }
  1586. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1587. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1588. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1589. }
  1590. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1591. {
  1592. struct drm_device *dev = crtc->base.dev;
  1593. struct drm_i915_private *dev_priv = dev->dev_private;
  1594. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1595. if (WARN_ON(pll == NULL))
  1596. return;
  1597. WARN_ON(!pll->config.crtc_mask);
  1598. if (pll->active == 0) {
  1599. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1600. WARN_ON(pll->on);
  1601. assert_shared_dpll_disabled(dev_priv, pll);
  1602. pll->mode_set(dev_priv, pll);
  1603. }
  1604. }
  1605. /**
  1606. * intel_enable_shared_dpll - enable PCH PLL
  1607. * @dev_priv: i915 private structure
  1608. * @pipe: pipe PLL to enable
  1609. *
  1610. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1611. * drives the transcoder clock.
  1612. */
  1613. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1614. {
  1615. struct drm_device *dev = crtc->base.dev;
  1616. struct drm_i915_private *dev_priv = dev->dev_private;
  1617. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1618. if (WARN_ON(pll == NULL))
  1619. return;
  1620. if (WARN_ON(pll->config.crtc_mask == 0))
  1621. return;
  1622. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1623. pll->name, pll->active, pll->on,
  1624. crtc->base.base.id);
  1625. if (pll->active++) {
  1626. WARN_ON(!pll->on);
  1627. assert_shared_dpll_enabled(dev_priv, pll);
  1628. return;
  1629. }
  1630. WARN_ON(pll->on);
  1631. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1632. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1633. pll->enable(dev_priv, pll);
  1634. pll->on = true;
  1635. }
  1636. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1637. {
  1638. struct drm_device *dev = crtc->base.dev;
  1639. struct drm_i915_private *dev_priv = dev->dev_private;
  1640. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1641. /* PCH only available on ILK+ */
  1642. if (INTEL_INFO(dev)->gen < 5)
  1643. return;
  1644. if (pll == NULL)
  1645. return;
  1646. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1647. return;
  1648. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1649. pll->name, pll->active, pll->on,
  1650. crtc->base.base.id);
  1651. if (WARN_ON(pll->active == 0)) {
  1652. assert_shared_dpll_disabled(dev_priv, pll);
  1653. return;
  1654. }
  1655. assert_shared_dpll_enabled(dev_priv, pll);
  1656. WARN_ON(!pll->on);
  1657. if (--pll->active)
  1658. return;
  1659. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1660. pll->disable(dev_priv, pll);
  1661. pll->on = false;
  1662. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1663. }
  1664. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1665. enum pipe pipe)
  1666. {
  1667. struct drm_device *dev = dev_priv->dev;
  1668. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1669. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1670. uint32_t reg, val, pipeconf_val;
  1671. /* PCH only available on ILK+ */
  1672. BUG_ON(!HAS_PCH_SPLIT(dev));
  1673. /* Make sure PCH DPLL is enabled */
  1674. assert_shared_dpll_enabled(dev_priv,
  1675. intel_crtc_to_shared_dpll(intel_crtc));
  1676. /* FDI must be feeding us bits for PCH ports */
  1677. assert_fdi_tx_enabled(dev_priv, pipe);
  1678. assert_fdi_rx_enabled(dev_priv, pipe);
  1679. if (HAS_PCH_CPT(dev)) {
  1680. /* Workaround: Set the timing override bit before enabling the
  1681. * pch transcoder. */
  1682. reg = TRANS_CHICKEN2(pipe);
  1683. val = I915_READ(reg);
  1684. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1685. I915_WRITE(reg, val);
  1686. }
  1687. reg = PCH_TRANSCONF(pipe);
  1688. val = I915_READ(reg);
  1689. pipeconf_val = I915_READ(PIPECONF(pipe));
  1690. if (HAS_PCH_IBX(dev_priv->dev)) {
  1691. /*
  1692. * Make the BPC in transcoder be consistent with
  1693. * that in pipeconf reg. For HDMI we must use 8bpc
  1694. * here for both 8bpc and 12bpc.
  1695. */
  1696. val &= ~PIPECONF_BPC_MASK;
  1697. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1698. val |= PIPECONF_8BPC;
  1699. else
  1700. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1701. }
  1702. val &= ~TRANS_INTERLACE_MASK;
  1703. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1704. if (HAS_PCH_IBX(dev_priv->dev) &&
  1705. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1706. val |= TRANS_LEGACY_INTERLACED_ILK;
  1707. else
  1708. val |= TRANS_INTERLACED;
  1709. else
  1710. val |= TRANS_PROGRESSIVE;
  1711. I915_WRITE(reg, val | TRANS_ENABLE);
  1712. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1713. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1714. }
  1715. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1716. enum transcoder cpu_transcoder)
  1717. {
  1718. u32 val, pipeconf_val;
  1719. /* PCH only available on ILK+ */
  1720. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1721. /* FDI must be feeding us bits for PCH ports */
  1722. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1723. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1724. /* Workaround: set timing override bit. */
  1725. val = I915_READ(_TRANSA_CHICKEN2);
  1726. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1727. I915_WRITE(_TRANSA_CHICKEN2, val);
  1728. val = TRANS_ENABLE;
  1729. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1730. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1731. PIPECONF_INTERLACED_ILK)
  1732. val |= TRANS_INTERLACED;
  1733. else
  1734. val |= TRANS_PROGRESSIVE;
  1735. I915_WRITE(LPT_TRANSCONF, val);
  1736. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1737. DRM_ERROR("Failed to enable PCH transcoder\n");
  1738. }
  1739. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1740. enum pipe pipe)
  1741. {
  1742. struct drm_device *dev = dev_priv->dev;
  1743. uint32_t reg, val;
  1744. /* FDI relies on the transcoder */
  1745. assert_fdi_tx_disabled(dev_priv, pipe);
  1746. assert_fdi_rx_disabled(dev_priv, pipe);
  1747. /* Ports must be off as well */
  1748. assert_pch_ports_disabled(dev_priv, pipe);
  1749. reg = PCH_TRANSCONF(pipe);
  1750. val = I915_READ(reg);
  1751. val &= ~TRANS_ENABLE;
  1752. I915_WRITE(reg, val);
  1753. /* wait for PCH transcoder off, transcoder state */
  1754. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1755. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1756. if (!HAS_PCH_IBX(dev)) {
  1757. /* Workaround: Clear the timing override chicken bit again. */
  1758. reg = TRANS_CHICKEN2(pipe);
  1759. val = I915_READ(reg);
  1760. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1761. I915_WRITE(reg, val);
  1762. }
  1763. }
  1764. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1765. {
  1766. u32 val;
  1767. val = I915_READ(LPT_TRANSCONF);
  1768. val &= ~TRANS_ENABLE;
  1769. I915_WRITE(LPT_TRANSCONF, val);
  1770. /* wait for PCH transcoder off, transcoder state */
  1771. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1772. DRM_ERROR("Failed to disable PCH transcoder\n");
  1773. /* Workaround: clear timing override bit. */
  1774. val = I915_READ(_TRANSA_CHICKEN2);
  1775. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1776. I915_WRITE(_TRANSA_CHICKEN2, val);
  1777. }
  1778. /**
  1779. * intel_enable_pipe - enable a pipe, asserting requirements
  1780. * @crtc: crtc responsible for the pipe
  1781. *
  1782. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1783. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1784. */
  1785. static void intel_enable_pipe(struct intel_crtc *crtc)
  1786. {
  1787. struct drm_device *dev = crtc->base.dev;
  1788. struct drm_i915_private *dev_priv = dev->dev_private;
  1789. enum pipe pipe = crtc->pipe;
  1790. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1791. pipe);
  1792. enum pipe pch_transcoder;
  1793. int reg;
  1794. u32 val;
  1795. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1796. assert_planes_disabled(dev_priv, pipe);
  1797. assert_cursor_disabled(dev_priv, pipe);
  1798. assert_sprites_disabled(dev_priv, pipe);
  1799. if (HAS_PCH_LPT(dev_priv->dev))
  1800. pch_transcoder = TRANSCODER_A;
  1801. else
  1802. pch_transcoder = pipe;
  1803. /*
  1804. * A pipe without a PLL won't actually be able to drive bits from
  1805. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1806. * need the check.
  1807. */
  1808. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1809. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1810. assert_dsi_pll_enabled(dev_priv);
  1811. else
  1812. assert_pll_enabled(dev_priv, pipe);
  1813. else {
  1814. if (crtc->config->has_pch_encoder) {
  1815. /* if driving the PCH, we need FDI enabled */
  1816. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1817. assert_fdi_tx_pll_enabled(dev_priv,
  1818. (enum pipe) cpu_transcoder);
  1819. }
  1820. /* FIXME: assert CPU port conditions for SNB+ */
  1821. }
  1822. reg = PIPECONF(cpu_transcoder);
  1823. val = I915_READ(reg);
  1824. if (val & PIPECONF_ENABLE) {
  1825. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1826. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1827. return;
  1828. }
  1829. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1830. POSTING_READ(reg);
  1831. }
  1832. /**
  1833. * intel_disable_pipe - disable a pipe, asserting requirements
  1834. * @crtc: crtc whose pipes is to be disabled
  1835. *
  1836. * Disable the pipe of @crtc, making sure that various hardware
  1837. * specific requirements are met, if applicable, e.g. plane
  1838. * disabled, panel fitter off, etc.
  1839. *
  1840. * Will wait until the pipe has shut down before returning.
  1841. */
  1842. static void intel_disable_pipe(struct intel_crtc *crtc)
  1843. {
  1844. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1845. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1846. enum pipe pipe = crtc->pipe;
  1847. int reg;
  1848. u32 val;
  1849. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1850. /*
  1851. * Make sure planes won't keep trying to pump pixels to us,
  1852. * or we might hang the display.
  1853. */
  1854. assert_planes_disabled(dev_priv, pipe);
  1855. assert_cursor_disabled(dev_priv, pipe);
  1856. assert_sprites_disabled(dev_priv, pipe);
  1857. reg = PIPECONF(cpu_transcoder);
  1858. val = I915_READ(reg);
  1859. if ((val & PIPECONF_ENABLE) == 0)
  1860. return;
  1861. /*
  1862. * Double wide has implications for planes
  1863. * so best keep it disabled when not needed.
  1864. */
  1865. if (crtc->config->double_wide)
  1866. val &= ~PIPECONF_DOUBLE_WIDE;
  1867. /* Don't disable pipe or pipe PLLs if needed */
  1868. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1869. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1870. val &= ~PIPECONF_ENABLE;
  1871. I915_WRITE(reg, val);
  1872. if ((val & PIPECONF_ENABLE) == 0)
  1873. intel_wait_for_pipe_off(crtc);
  1874. }
  1875. static bool need_vtd_wa(struct drm_device *dev)
  1876. {
  1877. #ifdef CONFIG_INTEL_IOMMU
  1878. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1879. return true;
  1880. #endif
  1881. return false;
  1882. }
  1883. unsigned int
  1884. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1885. uint64_t fb_format_modifier, unsigned int plane)
  1886. {
  1887. unsigned int tile_height;
  1888. uint32_t pixel_bytes;
  1889. switch (fb_format_modifier) {
  1890. case DRM_FORMAT_MOD_NONE:
  1891. tile_height = 1;
  1892. break;
  1893. case I915_FORMAT_MOD_X_TILED:
  1894. tile_height = IS_GEN2(dev) ? 16 : 8;
  1895. break;
  1896. case I915_FORMAT_MOD_Y_TILED:
  1897. tile_height = 32;
  1898. break;
  1899. case I915_FORMAT_MOD_Yf_TILED:
  1900. pixel_bytes = drm_format_plane_cpp(pixel_format, plane);
  1901. switch (pixel_bytes) {
  1902. default:
  1903. case 1:
  1904. tile_height = 64;
  1905. break;
  1906. case 2:
  1907. case 4:
  1908. tile_height = 32;
  1909. break;
  1910. case 8:
  1911. tile_height = 16;
  1912. break;
  1913. case 16:
  1914. WARN_ONCE(1,
  1915. "128-bit pixels are not supported for display!");
  1916. tile_height = 16;
  1917. break;
  1918. }
  1919. break;
  1920. default:
  1921. MISSING_CASE(fb_format_modifier);
  1922. tile_height = 1;
  1923. break;
  1924. }
  1925. return tile_height;
  1926. }
  1927. unsigned int
  1928. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1929. uint32_t pixel_format, uint64_t fb_format_modifier)
  1930. {
  1931. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1932. fb_format_modifier, 0));
  1933. }
  1934. static int
  1935. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1936. const struct drm_plane_state *plane_state)
  1937. {
  1938. struct intel_rotation_info *info = &view->rotation_info;
  1939. unsigned int tile_height, tile_pitch;
  1940. *view = i915_ggtt_view_normal;
  1941. if (!plane_state)
  1942. return 0;
  1943. if (!intel_rotation_90_or_270(plane_state->rotation))
  1944. return 0;
  1945. *view = i915_ggtt_view_rotated;
  1946. info->height = fb->height;
  1947. info->pixel_format = fb->pixel_format;
  1948. info->pitch = fb->pitches[0];
  1949. info->uv_offset = fb->offsets[1];
  1950. info->fb_modifier = fb->modifier[0];
  1951. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1952. fb->modifier[0], 0);
  1953. tile_pitch = PAGE_SIZE / tile_height;
  1954. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1955. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1956. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1957. if (info->pixel_format == DRM_FORMAT_NV12) {
  1958. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1959. fb->modifier[0], 1);
  1960. tile_pitch = PAGE_SIZE / tile_height;
  1961. info->width_pages_uv = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1962. info->height_pages_uv = DIV_ROUND_UP(fb->height / 2,
  1963. tile_height);
  1964. info->size_uv = info->width_pages_uv * info->height_pages_uv *
  1965. PAGE_SIZE;
  1966. }
  1967. return 0;
  1968. }
  1969. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1970. {
  1971. if (INTEL_INFO(dev_priv)->gen >= 9)
  1972. return 256 * 1024;
  1973. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1974. IS_VALLEYVIEW(dev_priv))
  1975. return 128 * 1024;
  1976. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1977. return 4 * 1024;
  1978. else
  1979. return 0;
  1980. }
  1981. int
  1982. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1983. struct drm_framebuffer *fb,
  1984. const struct drm_plane_state *plane_state,
  1985. struct intel_engine_cs *pipelined,
  1986. struct drm_i915_gem_request **pipelined_request)
  1987. {
  1988. struct drm_device *dev = fb->dev;
  1989. struct drm_i915_private *dev_priv = dev->dev_private;
  1990. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1991. struct i915_ggtt_view view;
  1992. u32 alignment;
  1993. int ret;
  1994. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1995. switch (fb->modifier[0]) {
  1996. case DRM_FORMAT_MOD_NONE:
  1997. alignment = intel_linear_alignment(dev_priv);
  1998. break;
  1999. case I915_FORMAT_MOD_X_TILED:
  2000. if (INTEL_INFO(dev)->gen >= 9)
  2001. alignment = 256 * 1024;
  2002. else {
  2003. /* pin() will align the object as required by fence */
  2004. alignment = 0;
  2005. }
  2006. break;
  2007. case I915_FORMAT_MOD_Y_TILED:
  2008. case I915_FORMAT_MOD_Yf_TILED:
  2009. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2010. "Y tiling bo slipped through, driver bug!\n"))
  2011. return -EINVAL;
  2012. alignment = 1 * 1024 * 1024;
  2013. break;
  2014. default:
  2015. MISSING_CASE(fb->modifier[0]);
  2016. return -EINVAL;
  2017. }
  2018. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2019. if (ret)
  2020. return ret;
  2021. /* Note that the w/a also requires 64 PTE of padding following the
  2022. * bo. We currently fill all unused PTE with the shadow page and so
  2023. * we should always have valid PTE following the scanout preventing
  2024. * the VT-d warning.
  2025. */
  2026. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2027. alignment = 256 * 1024;
  2028. /*
  2029. * Global gtt pte registers are special registers which actually forward
  2030. * writes to a chunk of system memory. Which means that there is no risk
  2031. * that the register values disappear as soon as we call
  2032. * intel_runtime_pm_put(), so it is correct to wrap only the
  2033. * pin/unpin/fence and not more.
  2034. */
  2035. intel_runtime_pm_get(dev_priv);
  2036. dev_priv->mm.interruptible = false;
  2037. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2038. pipelined_request, &view);
  2039. if (ret)
  2040. goto err_interruptible;
  2041. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2042. * fence, whereas 965+ only requires a fence if using
  2043. * framebuffer compression. For simplicity, we always install
  2044. * a fence as the cost is not that onerous.
  2045. */
  2046. ret = i915_gem_object_get_fence(obj);
  2047. if (ret == -EDEADLK) {
  2048. /*
  2049. * -EDEADLK means there are no free fences
  2050. * no pending flips.
  2051. *
  2052. * This is propagated to atomic, but it uses
  2053. * -EDEADLK to force a locking recovery, so
  2054. * change the returned error to -EBUSY.
  2055. */
  2056. ret = -EBUSY;
  2057. goto err_unpin;
  2058. } else if (ret)
  2059. goto err_unpin;
  2060. i915_gem_object_pin_fence(obj);
  2061. dev_priv->mm.interruptible = true;
  2062. intel_runtime_pm_put(dev_priv);
  2063. return 0;
  2064. err_unpin:
  2065. i915_gem_object_unpin_from_display_plane(obj, &view);
  2066. err_interruptible:
  2067. dev_priv->mm.interruptible = true;
  2068. intel_runtime_pm_put(dev_priv);
  2069. return ret;
  2070. }
  2071. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2072. const struct drm_plane_state *plane_state)
  2073. {
  2074. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2075. struct i915_ggtt_view view;
  2076. int ret;
  2077. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2078. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2079. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2080. i915_gem_object_unpin_fence(obj);
  2081. i915_gem_object_unpin_from_display_plane(obj, &view);
  2082. }
  2083. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2084. * is assumed to be a power-of-two. */
  2085. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2086. int *x, int *y,
  2087. unsigned int tiling_mode,
  2088. unsigned int cpp,
  2089. unsigned int pitch)
  2090. {
  2091. if (tiling_mode != I915_TILING_NONE) {
  2092. unsigned int tile_rows, tiles;
  2093. tile_rows = *y / 8;
  2094. *y %= 8;
  2095. tiles = *x / (512/cpp);
  2096. *x %= 512/cpp;
  2097. return tile_rows * pitch * 8 + tiles * 4096;
  2098. } else {
  2099. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2100. unsigned int offset;
  2101. offset = *y * pitch + *x * cpp;
  2102. *y = (offset & alignment) / pitch;
  2103. *x = ((offset & alignment) - *y * pitch) / cpp;
  2104. return offset & ~alignment;
  2105. }
  2106. }
  2107. static int i9xx_format_to_fourcc(int format)
  2108. {
  2109. switch (format) {
  2110. case DISPPLANE_8BPP:
  2111. return DRM_FORMAT_C8;
  2112. case DISPPLANE_BGRX555:
  2113. return DRM_FORMAT_XRGB1555;
  2114. case DISPPLANE_BGRX565:
  2115. return DRM_FORMAT_RGB565;
  2116. default:
  2117. case DISPPLANE_BGRX888:
  2118. return DRM_FORMAT_XRGB8888;
  2119. case DISPPLANE_RGBX888:
  2120. return DRM_FORMAT_XBGR8888;
  2121. case DISPPLANE_BGRX101010:
  2122. return DRM_FORMAT_XRGB2101010;
  2123. case DISPPLANE_RGBX101010:
  2124. return DRM_FORMAT_XBGR2101010;
  2125. }
  2126. }
  2127. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2128. {
  2129. switch (format) {
  2130. case PLANE_CTL_FORMAT_RGB_565:
  2131. return DRM_FORMAT_RGB565;
  2132. default:
  2133. case PLANE_CTL_FORMAT_XRGB_8888:
  2134. if (rgb_order) {
  2135. if (alpha)
  2136. return DRM_FORMAT_ABGR8888;
  2137. else
  2138. return DRM_FORMAT_XBGR8888;
  2139. } else {
  2140. if (alpha)
  2141. return DRM_FORMAT_ARGB8888;
  2142. else
  2143. return DRM_FORMAT_XRGB8888;
  2144. }
  2145. case PLANE_CTL_FORMAT_XRGB_2101010:
  2146. if (rgb_order)
  2147. return DRM_FORMAT_XBGR2101010;
  2148. else
  2149. return DRM_FORMAT_XRGB2101010;
  2150. }
  2151. }
  2152. static bool
  2153. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2154. struct intel_initial_plane_config *plane_config)
  2155. {
  2156. struct drm_device *dev = crtc->base.dev;
  2157. struct drm_i915_gem_object *obj = NULL;
  2158. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2159. struct drm_framebuffer *fb = &plane_config->fb->base;
  2160. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2161. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2162. PAGE_SIZE);
  2163. size_aligned -= base_aligned;
  2164. if (plane_config->size == 0)
  2165. return false;
  2166. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2167. base_aligned,
  2168. base_aligned,
  2169. size_aligned);
  2170. if (!obj)
  2171. return false;
  2172. obj->tiling_mode = plane_config->tiling;
  2173. if (obj->tiling_mode == I915_TILING_X)
  2174. obj->stride = fb->pitches[0];
  2175. mode_cmd.pixel_format = fb->pixel_format;
  2176. mode_cmd.width = fb->width;
  2177. mode_cmd.height = fb->height;
  2178. mode_cmd.pitches[0] = fb->pitches[0];
  2179. mode_cmd.modifier[0] = fb->modifier[0];
  2180. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2181. mutex_lock(&dev->struct_mutex);
  2182. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2183. &mode_cmd, obj)) {
  2184. DRM_DEBUG_KMS("intel fb init failed\n");
  2185. goto out_unref_obj;
  2186. }
  2187. mutex_unlock(&dev->struct_mutex);
  2188. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2189. return true;
  2190. out_unref_obj:
  2191. drm_gem_object_unreference(&obj->base);
  2192. mutex_unlock(&dev->struct_mutex);
  2193. return false;
  2194. }
  2195. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2196. static void
  2197. update_state_fb(struct drm_plane *plane)
  2198. {
  2199. if (plane->fb == plane->state->fb)
  2200. return;
  2201. if (plane->state->fb)
  2202. drm_framebuffer_unreference(plane->state->fb);
  2203. plane->state->fb = plane->fb;
  2204. if (plane->state->fb)
  2205. drm_framebuffer_reference(plane->state->fb);
  2206. }
  2207. static void
  2208. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2209. struct intel_initial_plane_config *plane_config)
  2210. {
  2211. struct drm_device *dev = intel_crtc->base.dev;
  2212. struct drm_i915_private *dev_priv = dev->dev_private;
  2213. struct drm_crtc *c;
  2214. struct intel_crtc *i;
  2215. struct drm_i915_gem_object *obj;
  2216. struct drm_plane *primary = intel_crtc->base.primary;
  2217. struct drm_plane_state *plane_state = primary->state;
  2218. struct drm_framebuffer *fb;
  2219. if (!plane_config->fb)
  2220. return;
  2221. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2222. fb = &plane_config->fb->base;
  2223. goto valid_fb;
  2224. }
  2225. kfree(plane_config->fb);
  2226. /*
  2227. * Failed to alloc the obj, check to see if we should share
  2228. * an fb with another CRTC instead
  2229. */
  2230. for_each_crtc(dev, c) {
  2231. i = to_intel_crtc(c);
  2232. if (c == &intel_crtc->base)
  2233. continue;
  2234. if (!i->active)
  2235. continue;
  2236. fb = c->primary->fb;
  2237. if (!fb)
  2238. continue;
  2239. obj = intel_fb_obj(fb);
  2240. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2241. drm_framebuffer_reference(fb);
  2242. goto valid_fb;
  2243. }
  2244. }
  2245. return;
  2246. valid_fb:
  2247. plane_state->src_x = plane_state->src_y = 0;
  2248. plane_state->src_w = fb->width << 16;
  2249. plane_state->src_h = fb->height << 16;
  2250. plane_state->crtc_x = plane_state->src_y = 0;
  2251. plane_state->crtc_w = fb->width;
  2252. plane_state->crtc_h = fb->height;
  2253. obj = intel_fb_obj(fb);
  2254. if (obj->tiling_mode != I915_TILING_NONE)
  2255. dev_priv->preserve_bios_swizzle = true;
  2256. drm_framebuffer_reference(fb);
  2257. primary->fb = primary->state->fb = fb;
  2258. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2259. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2260. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2261. }
  2262. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2263. struct drm_framebuffer *fb,
  2264. int x, int y)
  2265. {
  2266. struct drm_device *dev = crtc->dev;
  2267. struct drm_i915_private *dev_priv = dev->dev_private;
  2268. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2269. struct drm_plane *primary = crtc->primary;
  2270. bool visible = to_intel_plane_state(primary->state)->visible;
  2271. struct drm_i915_gem_object *obj;
  2272. int plane = intel_crtc->plane;
  2273. unsigned long linear_offset;
  2274. u32 dspcntr;
  2275. u32 reg = DSPCNTR(plane);
  2276. int pixel_size;
  2277. if (!visible || !fb) {
  2278. I915_WRITE(reg, 0);
  2279. if (INTEL_INFO(dev)->gen >= 4)
  2280. I915_WRITE(DSPSURF(plane), 0);
  2281. else
  2282. I915_WRITE(DSPADDR(plane), 0);
  2283. POSTING_READ(reg);
  2284. return;
  2285. }
  2286. obj = intel_fb_obj(fb);
  2287. if (WARN_ON(obj == NULL))
  2288. return;
  2289. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2290. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2291. dspcntr |= DISPLAY_PLANE_ENABLE;
  2292. if (INTEL_INFO(dev)->gen < 4) {
  2293. if (intel_crtc->pipe == PIPE_B)
  2294. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2295. /* pipesrc and dspsize control the size that is scaled from,
  2296. * which should always be the user's requested size.
  2297. */
  2298. I915_WRITE(DSPSIZE(plane),
  2299. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2300. (intel_crtc->config->pipe_src_w - 1));
  2301. I915_WRITE(DSPPOS(plane), 0);
  2302. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2303. I915_WRITE(PRIMSIZE(plane),
  2304. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2305. (intel_crtc->config->pipe_src_w - 1));
  2306. I915_WRITE(PRIMPOS(plane), 0);
  2307. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2308. }
  2309. switch (fb->pixel_format) {
  2310. case DRM_FORMAT_C8:
  2311. dspcntr |= DISPPLANE_8BPP;
  2312. break;
  2313. case DRM_FORMAT_XRGB1555:
  2314. dspcntr |= DISPPLANE_BGRX555;
  2315. break;
  2316. case DRM_FORMAT_RGB565:
  2317. dspcntr |= DISPPLANE_BGRX565;
  2318. break;
  2319. case DRM_FORMAT_XRGB8888:
  2320. dspcntr |= DISPPLANE_BGRX888;
  2321. break;
  2322. case DRM_FORMAT_XBGR8888:
  2323. dspcntr |= DISPPLANE_RGBX888;
  2324. break;
  2325. case DRM_FORMAT_XRGB2101010:
  2326. dspcntr |= DISPPLANE_BGRX101010;
  2327. break;
  2328. case DRM_FORMAT_XBGR2101010:
  2329. dspcntr |= DISPPLANE_RGBX101010;
  2330. break;
  2331. default:
  2332. BUG();
  2333. }
  2334. if (INTEL_INFO(dev)->gen >= 4 &&
  2335. obj->tiling_mode != I915_TILING_NONE)
  2336. dspcntr |= DISPPLANE_TILED;
  2337. if (IS_G4X(dev))
  2338. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2339. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2340. if (INTEL_INFO(dev)->gen >= 4) {
  2341. intel_crtc->dspaddr_offset =
  2342. intel_gen4_compute_page_offset(dev_priv,
  2343. &x, &y, obj->tiling_mode,
  2344. pixel_size,
  2345. fb->pitches[0]);
  2346. linear_offset -= intel_crtc->dspaddr_offset;
  2347. } else {
  2348. intel_crtc->dspaddr_offset = linear_offset;
  2349. }
  2350. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2351. dspcntr |= DISPPLANE_ROTATE_180;
  2352. x += (intel_crtc->config->pipe_src_w - 1);
  2353. y += (intel_crtc->config->pipe_src_h - 1);
  2354. /* Finding the last pixel of the last line of the display
  2355. data and adding to linear_offset*/
  2356. linear_offset +=
  2357. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2358. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2359. }
  2360. intel_crtc->adjusted_x = x;
  2361. intel_crtc->adjusted_y = y;
  2362. I915_WRITE(reg, dspcntr);
  2363. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2364. if (INTEL_INFO(dev)->gen >= 4) {
  2365. I915_WRITE(DSPSURF(plane),
  2366. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2367. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2368. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2369. } else
  2370. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2371. POSTING_READ(reg);
  2372. }
  2373. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2374. struct drm_framebuffer *fb,
  2375. int x, int y)
  2376. {
  2377. struct drm_device *dev = crtc->dev;
  2378. struct drm_i915_private *dev_priv = dev->dev_private;
  2379. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2380. struct drm_plane *primary = crtc->primary;
  2381. bool visible = to_intel_plane_state(primary->state)->visible;
  2382. struct drm_i915_gem_object *obj;
  2383. int plane = intel_crtc->plane;
  2384. unsigned long linear_offset;
  2385. u32 dspcntr;
  2386. u32 reg = DSPCNTR(plane);
  2387. int pixel_size;
  2388. if (!visible || !fb) {
  2389. I915_WRITE(reg, 0);
  2390. I915_WRITE(DSPSURF(plane), 0);
  2391. POSTING_READ(reg);
  2392. return;
  2393. }
  2394. obj = intel_fb_obj(fb);
  2395. if (WARN_ON(obj == NULL))
  2396. return;
  2397. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2398. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2399. dspcntr |= DISPLAY_PLANE_ENABLE;
  2400. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2401. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2402. switch (fb->pixel_format) {
  2403. case DRM_FORMAT_C8:
  2404. dspcntr |= DISPPLANE_8BPP;
  2405. break;
  2406. case DRM_FORMAT_RGB565:
  2407. dspcntr |= DISPPLANE_BGRX565;
  2408. break;
  2409. case DRM_FORMAT_XRGB8888:
  2410. dspcntr |= DISPPLANE_BGRX888;
  2411. break;
  2412. case DRM_FORMAT_XBGR8888:
  2413. dspcntr |= DISPPLANE_RGBX888;
  2414. break;
  2415. case DRM_FORMAT_XRGB2101010:
  2416. dspcntr |= DISPPLANE_BGRX101010;
  2417. break;
  2418. case DRM_FORMAT_XBGR2101010:
  2419. dspcntr |= DISPPLANE_RGBX101010;
  2420. break;
  2421. default:
  2422. BUG();
  2423. }
  2424. if (obj->tiling_mode != I915_TILING_NONE)
  2425. dspcntr |= DISPPLANE_TILED;
  2426. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2427. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2428. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2429. intel_crtc->dspaddr_offset =
  2430. intel_gen4_compute_page_offset(dev_priv,
  2431. &x, &y, obj->tiling_mode,
  2432. pixel_size,
  2433. fb->pitches[0]);
  2434. linear_offset -= intel_crtc->dspaddr_offset;
  2435. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2436. dspcntr |= DISPPLANE_ROTATE_180;
  2437. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2438. x += (intel_crtc->config->pipe_src_w - 1);
  2439. y += (intel_crtc->config->pipe_src_h - 1);
  2440. /* Finding the last pixel of the last line of the display
  2441. data and adding to linear_offset*/
  2442. linear_offset +=
  2443. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2444. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2445. }
  2446. }
  2447. intel_crtc->adjusted_x = x;
  2448. intel_crtc->adjusted_y = y;
  2449. I915_WRITE(reg, dspcntr);
  2450. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2451. I915_WRITE(DSPSURF(plane),
  2452. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2453. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2454. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2455. } else {
  2456. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2457. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2458. }
  2459. POSTING_READ(reg);
  2460. }
  2461. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2462. uint32_t pixel_format)
  2463. {
  2464. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2465. /*
  2466. * The stride is either expressed as a multiple of 64 bytes
  2467. * chunks for linear buffers or in number of tiles for tiled
  2468. * buffers.
  2469. */
  2470. switch (fb_modifier) {
  2471. case DRM_FORMAT_MOD_NONE:
  2472. return 64;
  2473. case I915_FORMAT_MOD_X_TILED:
  2474. if (INTEL_INFO(dev)->gen == 2)
  2475. return 128;
  2476. return 512;
  2477. case I915_FORMAT_MOD_Y_TILED:
  2478. /* No need to check for old gens and Y tiling since this is
  2479. * about the display engine and those will be blocked before
  2480. * we get here.
  2481. */
  2482. return 128;
  2483. case I915_FORMAT_MOD_Yf_TILED:
  2484. if (bits_per_pixel == 8)
  2485. return 64;
  2486. else
  2487. return 128;
  2488. default:
  2489. MISSING_CASE(fb_modifier);
  2490. return 64;
  2491. }
  2492. }
  2493. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2494. struct drm_i915_gem_object *obj,
  2495. unsigned int plane)
  2496. {
  2497. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2498. struct i915_vma *vma;
  2499. unsigned char *offset;
  2500. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2501. view = &i915_ggtt_view_rotated;
  2502. vma = i915_gem_obj_to_ggtt_view(obj, view);
  2503. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2504. view->type))
  2505. return -1;
  2506. offset = (unsigned char *)vma->node.start;
  2507. if (plane == 1) {
  2508. offset += vma->ggtt_view.rotation_info.uv_start_page *
  2509. PAGE_SIZE;
  2510. }
  2511. return (unsigned long)offset;
  2512. }
  2513. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2514. {
  2515. struct drm_device *dev = intel_crtc->base.dev;
  2516. struct drm_i915_private *dev_priv = dev->dev_private;
  2517. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2518. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2519. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2520. }
  2521. /*
  2522. * This function detaches (aka. unbinds) unused scalers in hardware
  2523. */
  2524. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2525. {
  2526. struct intel_crtc_scaler_state *scaler_state;
  2527. int i;
  2528. scaler_state = &intel_crtc->config->scaler_state;
  2529. /* loop through and disable scalers that aren't in use */
  2530. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2531. if (!scaler_state->scalers[i].in_use)
  2532. skl_detach_scaler(intel_crtc, i);
  2533. }
  2534. }
  2535. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2536. {
  2537. switch (pixel_format) {
  2538. case DRM_FORMAT_C8:
  2539. return PLANE_CTL_FORMAT_INDEXED;
  2540. case DRM_FORMAT_RGB565:
  2541. return PLANE_CTL_FORMAT_RGB_565;
  2542. case DRM_FORMAT_XBGR8888:
  2543. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2544. case DRM_FORMAT_XRGB8888:
  2545. return PLANE_CTL_FORMAT_XRGB_8888;
  2546. /*
  2547. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2548. * to be already pre-multiplied. We need to add a knob (or a different
  2549. * DRM_FORMAT) for user-space to configure that.
  2550. */
  2551. case DRM_FORMAT_ABGR8888:
  2552. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2553. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2554. case DRM_FORMAT_ARGB8888:
  2555. return PLANE_CTL_FORMAT_XRGB_8888 |
  2556. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2557. case DRM_FORMAT_XRGB2101010:
  2558. return PLANE_CTL_FORMAT_XRGB_2101010;
  2559. case DRM_FORMAT_XBGR2101010:
  2560. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2561. case DRM_FORMAT_YUYV:
  2562. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2563. case DRM_FORMAT_YVYU:
  2564. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2565. case DRM_FORMAT_UYVY:
  2566. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2567. case DRM_FORMAT_VYUY:
  2568. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2569. default:
  2570. MISSING_CASE(pixel_format);
  2571. }
  2572. return 0;
  2573. }
  2574. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2575. {
  2576. switch (fb_modifier) {
  2577. case DRM_FORMAT_MOD_NONE:
  2578. break;
  2579. case I915_FORMAT_MOD_X_TILED:
  2580. return PLANE_CTL_TILED_X;
  2581. case I915_FORMAT_MOD_Y_TILED:
  2582. return PLANE_CTL_TILED_Y;
  2583. case I915_FORMAT_MOD_Yf_TILED:
  2584. return PLANE_CTL_TILED_YF;
  2585. default:
  2586. MISSING_CASE(fb_modifier);
  2587. }
  2588. return 0;
  2589. }
  2590. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2591. {
  2592. switch (rotation) {
  2593. case BIT(DRM_ROTATE_0):
  2594. break;
  2595. /*
  2596. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2597. * while i915 HW rotation is clockwise, thats why this swapping.
  2598. */
  2599. case BIT(DRM_ROTATE_90):
  2600. return PLANE_CTL_ROTATE_270;
  2601. case BIT(DRM_ROTATE_180):
  2602. return PLANE_CTL_ROTATE_180;
  2603. case BIT(DRM_ROTATE_270):
  2604. return PLANE_CTL_ROTATE_90;
  2605. default:
  2606. MISSING_CASE(rotation);
  2607. }
  2608. return 0;
  2609. }
  2610. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2611. struct drm_framebuffer *fb,
  2612. int x, int y)
  2613. {
  2614. struct drm_device *dev = crtc->dev;
  2615. struct drm_i915_private *dev_priv = dev->dev_private;
  2616. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2617. struct drm_plane *plane = crtc->primary;
  2618. bool visible = to_intel_plane_state(plane->state)->visible;
  2619. struct drm_i915_gem_object *obj;
  2620. int pipe = intel_crtc->pipe;
  2621. u32 plane_ctl, stride_div, stride;
  2622. u32 tile_height, plane_offset, plane_size;
  2623. unsigned int rotation;
  2624. int x_offset, y_offset;
  2625. unsigned long surf_addr;
  2626. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2627. struct intel_plane_state *plane_state;
  2628. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2629. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2630. int scaler_id = -1;
  2631. plane_state = to_intel_plane_state(plane->state);
  2632. if (!visible || !fb) {
  2633. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2634. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2635. POSTING_READ(PLANE_CTL(pipe, 0));
  2636. return;
  2637. }
  2638. plane_ctl = PLANE_CTL_ENABLE |
  2639. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2640. PLANE_CTL_PIPE_CSC_ENABLE;
  2641. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2642. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2643. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2644. rotation = plane->state->rotation;
  2645. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2646. obj = intel_fb_obj(fb);
  2647. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2648. fb->pixel_format);
  2649. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2650. /*
  2651. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2652. * update_plane helpers are called from legacy paths.
  2653. * Once full atomic crtc is available, below check can be avoided.
  2654. */
  2655. if (drm_rect_width(&plane_state->src)) {
  2656. scaler_id = plane_state->scaler_id;
  2657. src_x = plane_state->src.x1 >> 16;
  2658. src_y = plane_state->src.y1 >> 16;
  2659. src_w = drm_rect_width(&plane_state->src) >> 16;
  2660. src_h = drm_rect_height(&plane_state->src) >> 16;
  2661. dst_x = plane_state->dst.x1;
  2662. dst_y = plane_state->dst.y1;
  2663. dst_w = drm_rect_width(&plane_state->dst);
  2664. dst_h = drm_rect_height(&plane_state->dst);
  2665. WARN_ON(x != src_x || y != src_y);
  2666. } else {
  2667. src_w = intel_crtc->config->pipe_src_w;
  2668. src_h = intel_crtc->config->pipe_src_h;
  2669. }
  2670. if (intel_rotation_90_or_270(rotation)) {
  2671. /* stride = Surface height in tiles */
  2672. tile_height = intel_tile_height(dev, fb->pixel_format,
  2673. fb->modifier[0], 0);
  2674. stride = DIV_ROUND_UP(fb->height, tile_height);
  2675. x_offset = stride * tile_height - y - src_h;
  2676. y_offset = x;
  2677. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2678. } else {
  2679. stride = fb->pitches[0] / stride_div;
  2680. x_offset = x;
  2681. y_offset = y;
  2682. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2683. }
  2684. plane_offset = y_offset << 16 | x_offset;
  2685. intel_crtc->adjusted_x = x_offset;
  2686. intel_crtc->adjusted_y = y_offset;
  2687. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2688. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2689. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2690. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2691. if (scaler_id >= 0) {
  2692. uint32_t ps_ctrl = 0;
  2693. WARN_ON(!dst_w || !dst_h);
  2694. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2695. crtc_state->scaler_state.scalers[scaler_id].mode;
  2696. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2697. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2698. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2699. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2700. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2701. } else {
  2702. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2703. }
  2704. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2705. POSTING_READ(PLANE_SURF(pipe, 0));
  2706. }
  2707. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2708. static int
  2709. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2710. int x, int y, enum mode_set_atomic state)
  2711. {
  2712. struct drm_device *dev = crtc->dev;
  2713. struct drm_i915_private *dev_priv = dev->dev_private;
  2714. if (dev_priv->fbc.disable_fbc)
  2715. dev_priv->fbc.disable_fbc(dev_priv);
  2716. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2717. return 0;
  2718. }
  2719. static void intel_complete_page_flips(struct drm_device *dev)
  2720. {
  2721. struct drm_crtc *crtc;
  2722. for_each_crtc(dev, crtc) {
  2723. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2724. enum plane plane = intel_crtc->plane;
  2725. intel_prepare_page_flip(dev, plane);
  2726. intel_finish_page_flip_plane(dev, plane);
  2727. }
  2728. }
  2729. static void intel_update_primary_planes(struct drm_device *dev)
  2730. {
  2731. struct drm_crtc *crtc;
  2732. for_each_crtc(dev, crtc) {
  2733. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2734. struct intel_plane_state *plane_state;
  2735. drm_modeset_lock_crtc(crtc, &plane->base);
  2736. plane_state = to_intel_plane_state(plane->base.state);
  2737. if (plane_state->base.fb)
  2738. plane->commit_plane(&plane->base, plane_state);
  2739. drm_modeset_unlock_crtc(crtc);
  2740. }
  2741. }
  2742. void intel_prepare_reset(struct drm_device *dev)
  2743. {
  2744. /* no reset support for gen2 */
  2745. if (IS_GEN2(dev))
  2746. return;
  2747. /* reset doesn't touch the display */
  2748. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2749. return;
  2750. drm_modeset_lock_all(dev);
  2751. /*
  2752. * Disabling the crtcs gracefully seems nicer. Also the
  2753. * g33 docs say we should at least disable all the planes.
  2754. */
  2755. intel_display_suspend(dev);
  2756. }
  2757. void intel_finish_reset(struct drm_device *dev)
  2758. {
  2759. struct drm_i915_private *dev_priv = to_i915(dev);
  2760. /*
  2761. * Flips in the rings will be nuked by the reset,
  2762. * so complete all pending flips so that user space
  2763. * will get its events and not get stuck.
  2764. */
  2765. intel_complete_page_flips(dev);
  2766. /* no reset support for gen2 */
  2767. if (IS_GEN2(dev))
  2768. return;
  2769. /* reset doesn't touch the display */
  2770. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2771. /*
  2772. * Flips in the rings have been nuked by the reset,
  2773. * so update the base address of all primary
  2774. * planes to the the last fb to make sure we're
  2775. * showing the correct fb after a reset.
  2776. *
  2777. * FIXME: Atomic will make this obsolete since we won't schedule
  2778. * CS-based flips (which might get lost in gpu resets) any more.
  2779. */
  2780. intel_update_primary_planes(dev);
  2781. return;
  2782. }
  2783. /*
  2784. * The display has been reset as well,
  2785. * so need a full re-initialization.
  2786. */
  2787. intel_runtime_pm_disable_interrupts(dev_priv);
  2788. intel_runtime_pm_enable_interrupts(dev_priv);
  2789. intel_modeset_init_hw(dev);
  2790. spin_lock_irq(&dev_priv->irq_lock);
  2791. if (dev_priv->display.hpd_irq_setup)
  2792. dev_priv->display.hpd_irq_setup(dev);
  2793. spin_unlock_irq(&dev_priv->irq_lock);
  2794. intel_display_resume(dev);
  2795. intel_hpd_init(dev_priv);
  2796. drm_modeset_unlock_all(dev);
  2797. }
  2798. static void
  2799. intel_finish_fb(struct drm_framebuffer *old_fb)
  2800. {
  2801. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2802. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2803. bool was_interruptible = dev_priv->mm.interruptible;
  2804. int ret;
  2805. /* Big Hammer, we also need to ensure that any pending
  2806. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2807. * current scanout is retired before unpinning the old
  2808. * framebuffer. Note that we rely on userspace rendering
  2809. * into the buffer attached to the pipe they are waiting
  2810. * on. If not, userspace generates a GPU hang with IPEHR
  2811. * point to the MI_WAIT_FOR_EVENT.
  2812. *
  2813. * This should only fail upon a hung GPU, in which case we
  2814. * can safely continue.
  2815. */
  2816. dev_priv->mm.interruptible = false;
  2817. ret = i915_gem_object_wait_rendering(obj, true);
  2818. dev_priv->mm.interruptible = was_interruptible;
  2819. WARN_ON(ret);
  2820. }
  2821. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2822. {
  2823. struct drm_device *dev = crtc->dev;
  2824. struct drm_i915_private *dev_priv = dev->dev_private;
  2825. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2826. bool pending;
  2827. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2828. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2829. return false;
  2830. spin_lock_irq(&dev->event_lock);
  2831. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2832. spin_unlock_irq(&dev->event_lock);
  2833. return pending;
  2834. }
  2835. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2836. struct intel_crtc_state *old_crtc_state)
  2837. {
  2838. struct drm_device *dev = crtc->base.dev;
  2839. struct drm_i915_private *dev_priv = dev->dev_private;
  2840. struct intel_crtc_state *pipe_config =
  2841. to_intel_crtc_state(crtc->base.state);
  2842. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2843. crtc->base.mode = crtc->base.state->mode;
  2844. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2845. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2846. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2847. if (HAS_DDI(dev))
  2848. intel_set_pipe_csc(&crtc->base);
  2849. /*
  2850. * Update pipe size and adjust fitter if needed: the reason for this is
  2851. * that in compute_mode_changes we check the native mode (not the pfit
  2852. * mode) to see if we can flip rather than do a full mode set. In the
  2853. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2854. * pfit state, we'll end up with a big fb scanned out into the wrong
  2855. * sized surface.
  2856. */
  2857. I915_WRITE(PIPESRC(crtc->pipe),
  2858. ((pipe_config->pipe_src_w - 1) << 16) |
  2859. (pipe_config->pipe_src_h - 1));
  2860. /* on skylake this is done by detaching scalers */
  2861. if (INTEL_INFO(dev)->gen >= 9) {
  2862. skl_detach_scalers(crtc);
  2863. if (pipe_config->pch_pfit.enabled)
  2864. skylake_pfit_enable(crtc);
  2865. } else if (HAS_PCH_SPLIT(dev)) {
  2866. if (pipe_config->pch_pfit.enabled)
  2867. ironlake_pfit_enable(crtc);
  2868. else if (old_crtc_state->pch_pfit.enabled)
  2869. ironlake_pfit_disable(crtc, true);
  2870. }
  2871. }
  2872. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2873. {
  2874. struct drm_device *dev = crtc->dev;
  2875. struct drm_i915_private *dev_priv = dev->dev_private;
  2876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2877. int pipe = intel_crtc->pipe;
  2878. u32 reg, temp;
  2879. /* enable normal train */
  2880. reg = FDI_TX_CTL(pipe);
  2881. temp = I915_READ(reg);
  2882. if (IS_IVYBRIDGE(dev)) {
  2883. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2884. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2885. } else {
  2886. temp &= ~FDI_LINK_TRAIN_NONE;
  2887. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2888. }
  2889. I915_WRITE(reg, temp);
  2890. reg = FDI_RX_CTL(pipe);
  2891. temp = I915_READ(reg);
  2892. if (HAS_PCH_CPT(dev)) {
  2893. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2894. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2895. } else {
  2896. temp &= ~FDI_LINK_TRAIN_NONE;
  2897. temp |= FDI_LINK_TRAIN_NONE;
  2898. }
  2899. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2900. /* wait one idle pattern time */
  2901. POSTING_READ(reg);
  2902. udelay(1000);
  2903. /* IVB wants error correction enabled */
  2904. if (IS_IVYBRIDGE(dev))
  2905. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2906. FDI_FE_ERRC_ENABLE);
  2907. }
  2908. /* The FDI link training functions for ILK/Ibexpeak. */
  2909. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2910. {
  2911. struct drm_device *dev = crtc->dev;
  2912. struct drm_i915_private *dev_priv = dev->dev_private;
  2913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2914. int pipe = intel_crtc->pipe;
  2915. u32 reg, temp, tries;
  2916. /* FDI needs bits from pipe first */
  2917. assert_pipe_enabled(dev_priv, pipe);
  2918. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2919. for train result */
  2920. reg = FDI_RX_IMR(pipe);
  2921. temp = I915_READ(reg);
  2922. temp &= ~FDI_RX_SYMBOL_LOCK;
  2923. temp &= ~FDI_RX_BIT_LOCK;
  2924. I915_WRITE(reg, temp);
  2925. I915_READ(reg);
  2926. udelay(150);
  2927. /* enable CPU FDI TX and PCH FDI RX */
  2928. reg = FDI_TX_CTL(pipe);
  2929. temp = I915_READ(reg);
  2930. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2931. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2932. temp &= ~FDI_LINK_TRAIN_NONE;
  2933. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2934. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2935. reg = FDI_RX_CTL(pipe);
  2936. temp = I915_READ(reg);
  2937. temp &= ~FDI_LINK_TRAIN_NONE;
  2938. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2939. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2940. POSTING_READ(reg);
  2941. udelay(150);
  2942. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2943. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2944. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2945. FDI_RX_PHASE_SYNC_POINTER_EN);
  2946. reg = FDI_RX_IIR(pipe);
  2947. for (tries = 0; tries < 5; tries++) {
  2948. temp = I915_READ(reg);
  2949. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2950. if ((temp & FDI_RX_BIT_LOCK)) {
  2951. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2952. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2953. break;
  2954. }
  2955. }
  2956. if (tries == 5)
  2957. DRM_ERROR("FDI train 1 fail!\n");
  2958. /* Train 2 */
  2959. reg = FDI_TX_CTL(pipe);
  2960. temp = I915_READ(reg);
  2961. temp &= ~FDI_LINK_TRAIN_NONE;
  2962. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2963. I915_WRITE(reg, temp);
  2964. reg = FDI_RX_CTL(pipe);
  2965. temp = I915_READ(reg);
  2966. temp &= ~FDI_LINK_TRAIN_NONE;
  2967. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2968. I915_WRITE(reg, temp);
  2969. POSTING_READ(reg);
  2970. udelay(150);
  2971. reg = FDI_RX_IIR(pipe);
  2972. for (tries = 0; tries < 5; tries++) {
  2973. temp = I915_READ(reg);
  2974. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2975. if (temp & FDI_RX_SYMBOL_LOCK) {
  2976. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2977. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2978. break;
  2979. }
  2980. }
  2981. if (tries == 5)
  2982. DRM_ERROR("FDI train 2 fail!\n");
  2983. DRM_DEBUG_KMS("FDI train done\n");
  2984. }
  2985. static const int snb_b_fdi_train_param[] = {
  2986. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2987. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2988. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2989. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2990. };
  2991. /* The FDI link training functions for SNB/Cougarpoint. */
  2992. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2993. {
  2994. struct drm_device *dev = crtc->dev;
  2995. struct drm_i915_private *dev_priv = dev->dev_private;
  2996. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2997. int pipe = intel_crtc->pipe;
  2998. u32 reg, temp, i, retry;
  2999. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3000. for train result */
  3001. reg = FDI_RX_IMR(pipe);
  3002. temp = I915_READ(reg);
  3003. temp &= ~FDI_RX_SYMBOL_LOCK;
  3004. temp &= ~FDI_RX_BIT_LOCK;
  3005. I915_WRITE(reg, temp);
  3006. POSTING_READ(reg);
  3007. udelay(150);
  3008. /* enable CPU FDI TX and PCH FDI RX */
  3009. reg = FDI_TX_CTL(pipe);
  3010. temp = I915_READ(reg);
  3011. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3012. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3013. temp &= ~FDI_LINK_TRAIN_NONE;
  3014. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3015. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3016. /* SNB-B */
  3017. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3018. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3019. I915_WRITE(FDI_RX_MISC(pipe),
  3020. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3021. reg = FDI_RX_CTL(pipe);
  3022. temp = I915_READ(reg);
  3023. if (HAS_PCH_CPT(dev)) {
  3024. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3025. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3026. } else {
  3027. temp &= ~FDI_LINK_TRAIN_NONE;
  3028. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3029. }
  3030. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3031. POSTING_READ(reg);
  3032. udelay(150);
  3033. for (i = 0; i < 4; i++) {
  3034. reg = FDI_TX_CTL(pipe);
  3035. temp = I915_READ(reg);
  3036. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3037. temp |= snb_b_fdi_train_param[i];
  3038. I915_WRITE(reg, temp);
  3039. POSTING_READ(reg);
  3040. udelay(500);
  3041. for (retry = 0; retry < 5; retry++) {
  3042. reg = FDI_RX_IIR(pipe);
  3043. temp = I915_READ(reg);
  3044. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3045. if (temp & FDI_RX_BIT_LOCK) {
  3046. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3047. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3048. break;
  3049. }
  3050. udelay(50);
  3051. }
  3052. if (retry < 5)
  3053. break;
  3054. }
  3055. if (i == 4)
  3056. DRM_ERROR("FDI train 1 fail!\n");
  3057. /* Train 2 */
  3058. reg = FDI_TX_CTL(pipe);
  3059. temp = I915_READ(reg);
  3060. temp &= ~FDI_LINK_TRAIN_NONE;
  3061. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3062. if (IS_GEN6(dev)) {
  3063. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3064. /* SNB-B */
  3065. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3066. }
  3067. I915_WRITE(reg, temp);
  3068. reg = FDI_RX_CTL(pipe);
  3069. temp = I915_READ(reg);
  3070. if (HAS_PCH_CPT(dev)) {
  3071. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3072. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3073. } else {
  3074. temp &= ~FDI_LINK_TRAIN_NONE;
  3075. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3076. }
  3077. I915_WRITE(reg, temp);
  3078. POSTING_READ(reg);
  3079. udelay(150);
  3080. for (i = 0; i < 4; i++) {
  3081. reg = FDI_TX_CTL(pipe);
  3082. temp = I915_READ(reg);
  3083. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3084. temp |= snb_b_fdi_train_param[i];
  3085. I915_WRITE(reg, temp);
  3086. POSTING_READ(reg);
  3087. udelay(500);
  3088. for (retry = 0; retry < 5; retry++) {
  3089. reg = FDI_RX_IIR(pipe);
  3090. temp = I915_READ(reg);
  3091. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3092. if (temp & FDI_RX_SYMBOL_LOCK) {
  3093. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3094. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3095. break;
  3096. }
  3097. udelay(50);
  3098. }
  3099. if (retry < 5)
  3100. break;
  3101. }
  3102. if (i == 4)
  3103. DRM_ERROR("FDI train 2 fail!\n");
  3104. DRM_DEBUG_KMS("FDI train done.\n");
  3105. }
  3106. /* Manual link training for Ivy Bridge A0 parts */
  3107. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3108. {
  3109. struct drm_device *dev = crtc->dev;
  3110. struct drm_i915_private *dev_priv = dev->dev_private;
  3111. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3112. int pipe = intel_crtc->pipe;
  3113. u32 reg, temp, i, j;
  3114. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3115. for train result */
  3116. reg = FDI_RX_IMR(pipe);
  3117. temp = I915_READ(reg);
  3118. temp &= ~FDI_RX_SYMBOL_LOCK;
  3119. temp &= ~FDI_RX_BIT_LOCK;
  3120. I915_WRITE(reg, temp);
  3121. POSTING_READ(reg);
  3122. udelay(150);
  3123. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3124. I915_READ(FDI_RX_IIR(pipe)));
  3125. /* Try each vswing and preemphasis setting twice before moving on */
  3126. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3127. /* disable first in case we need to retry */
  3128. reg = FDI_TX_CTL(pipe);
  3129. temp = I915_READ(reg);
  3130. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3131. temp &= ~FDI_TX_ENABLE;
  3132. I915_WRITE(reg, temp);
  3133. reg = FDI_RX_CTL(pipe);
  3134. temp = I915_READ(reg);
  3135. temp &= ~FDI_LINK_TRAIN_AUTO;
  3136. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3137. temp &= ~FDI_RX_ENABLE;
  3138. I915_WRITE(reg, temp);
  3139. /* enable CPU FDI TX and PCH FDI RX */
  3140. reg = FDI_TX_CTL(pipe);
  3141. temp = I915_READ(reg);
  3142. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3143. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3144. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3145. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3146. temp |= snb_b_fdi_train_param[j/2];
  3147. temp |= FDI_COMPOSITE_SYNC;
  3148. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3149. I915_WRITE(FDI_RX_MISC(pipe),
  3150. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3151. reg = FDI_RX_CTL(pipe);
  3152. temp = I915_READ(reg);
  3153. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3154. temp |= FDI_COMPOSITE_SYNC;
  3155. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3156. POSTING_READ(reg);
  3157. udelay(1); /* should be 0.5us */
  3158. for (i = 0; i < 4; i++) {
  3159. reg = FDI_RX_IIR(pipe);
  3160. temp = I915_READ(reg);
  3161. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3162. if (temp & FDI_RX_BIT_LOCK ||
  3163. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3164. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3165. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3166. i);
  3167. break;
  3168. }
  3169. udelay(1); /* should be 0.5us */
  3170. }
  3171. if (i == 4) {
  3172. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3173. continue;
  3174. }
  3175. /* Train 2 */
  3176. reg = FDI_TX_CTL(pipe);
  3177. temp = I915_READ(reg);
  3178. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3179. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3180. I915_WRITE(reg, temp);
  3181. reg = FDI_RX_CTL(pipe);
  3182. temp = I915_READ(reg);
  3183. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3184. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3185. I915_WRITE(reg, temp);
  3186. POSTING_READ(reg);
  3187. udelay(2); /* should be 1.5us */
  3188. for (i = 0; i < 4; i++) {
  3189. reg = FDI_RX_IIR(pipe);
  3190. temp = I915_READ(reg);
  3191. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3192. if (temp & FDI_RX_SYMBOL_LOCK ||
  3193. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3194. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3195. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3196. i);
  3197. goto train_done;
  3198. }
  3199. udelay(2); /* should be 1.5us */
  3200. }
  3201. if (i == 4)
  3202. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3203. }
  3204. train_done:
  3205. DRM_DEBUG_KMS("FDI train done.\n");
  3206. }
  3207. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3208. {
  3209. struct drm_device *dev = intel_crtc->base.dev;
  3210. struct drm_i915_private *dev_priv = dev->dev_private;
  3211. int pipe = intel_crtc->pipe;
  3212. u32 reg, temp;
  3213. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3214. reg = FDI_RX_CTL(pipe);
  3215. temp = I915_READ(reg);
  3216. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3217. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3218. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3219. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3220. POSTING_READ(reg);
  3221. udelay(200);
  3222. /* Switch from Rawclk to PCDclk */
  3223. temp = I915_READ(reg);
  3224. I915_WRITE(reg, temp | FDI_PCDCLK);
  3225. POSTING_READ(reg);
  3226. udelay(200);
  3227. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3228. reg = FDI_TX_CTL(pipe);
  3229. temp = I915_READ(reg);
  3230. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3231. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3232. POSTING_READ(reg);
  3233. udelay(100);
  3234. }
  3235. }
  3236. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3237. {
  3238. struct drm_device *dev = intel_crtc->base.dev;
  3239. struct drm_i915_private *dev_priv = dev->dev_private;
  3240. int pipe = intel_crtc->pipe;
  3241. u32 reg, temp;
  3242. /* Switch from PCDclk to Rawclk */
  3243. reg = FDI_RX_CTL(pipe);
  3244. temp = I915_READ(reg);
  3245. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3246. /* Disable CPU FDI TX PLL */
  3247. reg = FDI_TX_CTL(pipe);
  3248. temp = I915_READ(reg);
  3249. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3250. POSTING_READ(reg);
  3251. udelay(100);
  3252. reg = FDI_RX_CTL(pipe);
  3253. temp = I915_READ(reg);
  3254. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3255. /* Wait for the clocks to turn off. */
  3256. POSTING_READ(reg);
  3257. udelay(100);
  3258. }
  3259. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3260. {
  3261. struct drm_device *dev = crtc->dev;
  3262. struct drm_i915_private *dev_priv = dev->dev_private;
  3263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3264. int pipe = intel_crtc->pipe;
  3265. u32 reg, temp;
  3266. /* disable CPU FDI tx and PCH FDI rx */
  3267. reg = FDI_TX_CTL(pipe);
  3268. temp = I915_READ(reg);
  3269. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3270. POSTING_READ(reg);
  3271. reg = FDI_RX_CTL(pipe);
  3272. temp = I915_READ(reg);
  3273. temp &= ~(0x7 << 16);
  3274. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3275. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3276. POSTING_READ(reg);
  3277. udelay(100);
  3278. /* Ironlake workaround, disable clock pointer after downing FDI */
  3279. if (HAS_PCH_IBX(dev))
  3280. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3281. /* still set train pattern 1 */
  3282. reg = FDI_TX_CTL(pipe);
  3283. temp = I915_READ(reg);
  3284. temp &= ~FDI_LINK_TRAIN_NONE;
  3285. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3286. I915_WRITE(reg, temp);
  3287. reg = FDI_RX_CTL(pipe);
  3288. temp = I915_READ(reg);
  3289. if (HAS_PCH_CPT(dev)) {
  3290. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3291. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3292. } else {
  3293. temp &= ~FDI_LINK_TRAIN_NONE;
  3294. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3295. }
  3296. /* BPC in FDI rx is consistent with that in PIPECONF */
  3297. temp &= ~(0x07 << 16);
  3298. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3299. I915_WRITE(reg, temp);
  3300. POSTING_READ(reg);
  3301. udelay(100);
  3302. }
  3303. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3304. {
  3305. struct intel_crtc *crtc;
  3306. /* Note that we don't need to be called with mode_config.lock here
  3307. * as our list of CRTC objects is static for the lifetime of the
  3308. * device and so cannot disappear as we iterate. Similarly, we can
  3309. * happily treat the predicates as racy, atomic checks as userspace
  3310. * cannot claim and pin a new fb without at least acquring the
  3311. * struct_mutex and so serialising with us.
  3312. */
  3313. for_each_intel_crtc(dev, crtc) {
  3314. if (atomic_read(&crtc->unpin_work_count) == 0)
  3315. continue;
  3316. if (crtc->unpin_work)
  3317. intel_wait_for_vblank(dev, crtc->pipe);
  3318. return true;
  3319. }
  3320. return false;
  3321. }
  3322. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3323. {
  3324. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3325. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3326. /* ensure that the unpin work is consistent wrt ->pending. */
  3327. smp_rmb();
  3328. intel_crtc->unpin_work = NULL;
  3329. if (work->event)
  3330. drm_send_vblank_event(intel_crtc->base.dev,
  3331. intel_crtc->pipe,
  3332. work->event);
  3333. drm_crtc_vblank_put(&intel_crtc->base);
  3334. wake_up_all(&dev_priv->pending_flip_queue);
  3335. queue_work(dev_priv->wq, &work->work);
  3336. trace_i915_flip_complete(intel_crtc->plane,
  3337. work->pending_flip_obj);
  3338. }
  3339. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3340. {
  3341. struct drm_device *dev = crtc->dev;
  3342. struct drm_i915_private *dev_priv = dev->dev_private;
  3343. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3344. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3345. !intel_crtc_has_pending_flip(crtc),
  3346. 60*HZ) == 0)) {
  3347. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3348. spin_lock_irq(&dev->event_lock);
  3349. if (intel_crtc->unpin_work) {
  3350. WARN_ONCE(1, "Removing stuck page flip\n");
  3351. page_flip_completed(intel_crtc);
  3352. }
  3353. spin_unlock_irq(&dev->event_lock);
  3354. }
  3355. if (crtc->primary->fb) {
  3356. mutex_lock(&dev->struct_mutex);
  3357. intel_finish_fb(crtc->primary->fb);
  3358. mutex_unlock(&dev->struct_mutex);
  3359. }
  3360. }
  3361. /* Program iCLKIP clock to the desired frequency */
  3362. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3363. {
  3364. struct drm_device *dev = crtc->dev;
  3365. struct drm_i915_private *dev_priv = dev->dev_private;
  3366. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3367. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3368. u32 temp;
  3369. mutex_lock(&dev_priv->sb_lock);
  3370. /* It is necessary to ungate the pixclk gate prior to programming
  3371. * the divisors, and gate it back when it is done.
  3372. */
  3373. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3374. /* Disable SSCCTL */
  3375. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3376. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3377. SBI_SSCCTL_DISABLE,
  3378. SBI_ICLK);
  3379. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3380. if (clock == 20000) {
  3381. auxdiv = 1;
  3382. divsel = 0x41;
  3383. phaseinc = 0x20;
  3384. } else {
  3385. /* The iCLK virtual clock root frequency is in MHz,
  3386. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3387. * divisors, it is necessary to divide one by another, so we
  3388. * convert the virtual clock precision to KHz here for higher
  3389. * precision.
  3390. */
  3391. u32 iclk_virtual_root_freq = 172800 * 1000;
  3392. u32 iclk_pi_range = 64;
  3393. u32 desired_divisor, msb_divisor_value, pi_value;
  3394. desired_divisor = (iclk_virtual_root_freq / clock);
  3395. msb_divisor_value = desired_divisor / iclk_pi_range;
  3396. pi_value = desired_divisor % iclk_pi_range;
  3397. auxdiv = 0;
  3398. divsel = msb_divisor_value - 2;
  3399. phaseinc = pi_value;
  3400. }
  3401. /* This should not happen with any sane values */
  3402. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3403. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3404. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3405. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3406. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3407. clock,
  3408. auxdiv,
  3409. divsel,
  3410. phasedir,
  3411. phaseinc);
  3412. /* Program SSCDIVINTPHASE6 */
  3413. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3414. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3415. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3416. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3417. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3418. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3419. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3420. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3421. /* Program SSCAUXDIV */
  3422. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3423. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3424. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3425. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3426. /* Enable modulator and associated divider */
  3427. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3428. temp &= ~SBI_SSCCTL_DISABLE;
  3429. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3430. /* Wait for initialization time */
  3431. udelay(24);
  3432. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3433. mutex_unlock(&dev_priv->sb_lock);
  3434. }
  3435. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3436. enum pipe pch_transcoder)
  3437. {
  3438. struct drm_device *dev = crtc->base.dev;
  3439. struct drm_i915_private *dev_priv = dev->dev_private;
  3440. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3441. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3442. I915_READ(HTOTAL(cpu_transcoder)));
  3443. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3444. I915_READ(HBLANK(cpu_transcoder)));
  3445. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3446. I915_READ(HSYNC(cpu_transcoder)));
  3447. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3448. I915_READ(VTOTAL(cpu_transcoder)));
  3449. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3450. I915_READ(VBLANK(cpu_transcoder)));
  3451. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3452. I915_READ(VSYNC(cpu_transcoder)));
  3453. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3454. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3455. }
  3456. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3457. {
  3458. struct drm_i915_private *dev_priv = dev->dev_private;
  3459. uint32_t temp;
  3460. temp = I915_READ(SOUTH_CHICKEN1);
  3461. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3462. return;
  3463. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3464. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3465. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3466. if (enable)
  3467. temp |= FDI_BC_BIFURCATION_SELECT;
  3468. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3469. I915_WRITE(SOUTH_CHICKEN1, temp);
  3470. POSTING_READ(SOUTH_CHICKEN1);
  3471. }
  3472. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3473. {
  3474. struct drm_device *dev = intel_crtc->base.dev;
  3475. switch (intel_crtc->pipe) {
  3476. case PIPE_A:
  3477. break;
  3478. case PIPE_B:
  3479. if (intel_crtc->config->fdi_lanes > 2)
  3480. cpt_set_fdi_bc_bifurcation(dev, false);
  3481. else
  3482. cpt_set_fdi_bc_bifurcation(dev, true);
  3483. break;
  3484. case PIPE_C:
  3485. cpt_set_fdi_bc_bifurcation(dev, true);
  3486. break;
  3487. default:
  3488. BUG();
  3489. }
  3490. }
  3491. /*
  3492. * Enable PCH resources required for PCH ports:
  3493. * - PCH PLLs
  3494. * - FDI training & RX/TX
  3495. * - update transcoder timings
  3496. * - DP transcoding bits
  3497. * - transcoder
  3498. */
  3499. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3500. {
  3501. struct drm_device *dev = crtc->dev;
  3502. struct drm_i915_private *dev_priv = dev->dev_private;
  3503. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3504. int pipe = intel_crtc->pipe;
  3505. u32 reg, temp;
  3506. assert_pch_transcoder_disabled(dev_priv, pipe);
  3507. if (IS_IVYBRIDGE(dev))
  3508. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3509. /* Write the TU size bits before fdi link training, so that error
  3510. * detection works. */
  3511. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3512. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3513. /* For PCH output, training FDI link */
  3514. dev_priv->display.fdi_link_train(crtc);
  3515. /* We need to program the right clock selection before writing the pixel
  3516. * mutliplier into the DPLL. */
  3517. if (HAS_PCH_CPT(dev)) {
  3518. u32 sel;
  3519. temp = I915_READ(PCH_DPLL_SEL);
  3520. temp |= TRANS_DPLL_ENABLE(pipe);
  3521. sel = TRANS_DPLLB_SEL(pipe);
  3522. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3523. temp |= sel;
  3524. else
  3525. temp &= ~sel;
  3526. I915_WRITE(PCH_DPLL_SEL, temp);
  3527. }
  3528. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3529. * transcoder, and we actually should do this to not upset any PCH
  3530. * transcoder that already use the clock when we share it.
  3531. *
  3532. * Note that enable_shared_dpll tries to do the right thing, but
  3533. * get_shared_dpll unconditionally resets the pll - we need that to have
  3534. * the right LVDS enable sequence. */
  3535. intel_enable_shared_dpll(intel_crtc);
  3536. /* set transcoder timing, panel must allow it */
  3537. assert_panel_unlocked(dev_priv, pipe);
  3538. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3539. intel_fdi_normal_train(crtc);
  3540. /* For PCH DP, enable TRANS_DP_CTL */
  3541. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3542. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3543. reg = TRANS_DP_CTL(pipe);
  3544. temp = I915_READ(reg);
  3545. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3546. TRANS_DP_SYNC_MASK |
  3547. TRANS_DP_BPC_MASK);
  3548. temp |= TRANS_DP_OUTPUT_ENABLE;
  3549. temp |= bpc << 9; /* same format but at 11:9 */
  3550. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3551. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3552. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3553. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3554. switch (intel_trans_dp_port_sel(crtc)) {
  3555. case PCH_DP_B:
  3556. temp |= TRANS_DP_PORT_SEL_B;
  3557. break;
  3558. case PCH_DP_C:
  3559. temp |= TRANS_DP_PORT_SEL_C;
  3560. break;
  3561. case PCH_DP_D:
  3562. temp |= TRANS_DP_PORT_SEL_D;
  3563. break;
  3564. default:
  3565. BUG();
  3566. }
  3567. I915_WRITE(reg, temp);
  3568. }
  3569. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3570. }
  3571. static void lpt_pch_enable(struct drm_crtc *crtc)
  3572. {
  3573. struct drm_device *dev = crtc->dev;
  3574. struct drm_i915_private *dev_priv = dev->dev_private;
  3575. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3576. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3577. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3578. lpt_program_iclkip(crtc);
  3579. /* Set transcoder timing. */
  3580. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3581. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3582. }
  3583. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3584. struct intel_crtc_state *crtc_state)
  3585. {
  3586. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3587. struct intel_shared_dpll *pll;
  3588. struct intel_shared_dpll_config *shared_dpll;
  3589. enum intel_dpll_id i;
  3590. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3591. if (HAS_PCH_IBX(dev_priv->dev)) {
  3592. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3593. i = (enum intel_dpll_id) crtc->pipe;
  3594. pll = &dev_priv->shared_dplls[i];
  3595. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3596. crtc->base.base.id, pll->name);
  3597. WARN_ON(shared_dpll[i].crtc_mask);
  3598. goto found;
  3599. }
  3600. if (IS_BROXTON(dev_priv->dev)) {
  3601. /* PLL is attached to port in bxt */
  3602. struct intel_encoder *encoder;
  3603. struct intel_digital_port *intel_dig_port;
  3604. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3605. if (WARN_ON(!encoder))
  3606. return NULL;
  3607. intel_dig_port = enc_to_dig_port(&encoder->base);
  3608. /* 1:1 mapping between ports and PLLs */
  3609. i = (enum intel_dpll_id)intel_dig_port->port;
  3610. pll = &dev_priv->shared_dplls[i];
  3611. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3612. crtc->base.base.id, pll->name);
  3613. WARN_ON(shared_dpll[i].crtc_mask);
  3614. goto found;
  3615. }
  3616. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3617. pll = &dev_priv->shared_dplls[i];
  3618. /* Only want to check enabled timings first */
  3619. if (shared_dpll[i].crtc_mask == 0)
  3620. continue;
  3621. if (memcmp(&crtc_state->dpll_hw_state,
  3622. &shared_dpll[i].hw_state,
  3623. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3624. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3625. crtc->base.base.id, pll->name,
  3626. shared_dpll[i].crtc_mask,
  3627. pll->active);
  3628. goto found;
  3629. }
  3630. }
  3631. /* Ok no matching timings, maybe there's a free one? */
  3632. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3633. pll = &dev_priv->shared_dplls[i];
  3634. if (shared_dpll[i].crtc_mask == 0) {
  3635. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3636. crtc->base.base.id, pll->name);
  3637. goto found;
  3638. }
  3639. }
  3640. return NULL;
  3641. found:
  3642. if (shared_dpll[i].crtc_mask == 0)
  3643. shared_dpll[i].hw_state =
  3644. crtc_state->dpll_hw_state;
  3645. crtc_state->shared_dpll = i;
  3646. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3647. pipe_name(crtc->pipe));
  3648. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3649. return pll;
  3650. }
  3651. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3652. {
  3653. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3654. struct intel_shared_dpll_config *shared_dpll;
  3655. struct intel_shared_dpll *pll;
  3656. enum intel_dpll_id i;
  3657. if (!to_intel_atomic_state(state)->dpll_set)
  3658. return;
  3659. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3660. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3661. pll = &dev_priv->shared_dplls[i];
  3662. pll->config = shared_dpll[i];
  3663. }
  3664. }
  3665. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3666. {
  3667. struct drm_i915_private *dev_priv = dev->dev_private;
  3668. int dslreg = PIPEDSL(pipe);
  3669. u32 temp;
  3670. temp = I915_READ(dslreg);
  3671. udelay(500);
  3672. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3673. if (wait_for(I915_READ(dslreg) != temp, 5))
  3674. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3675. }
  3676. }
  3677. static int
  3678. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3679. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3680. int src_w, int src_h, int dst_w, int dst_h)
  3681. {
  3682. struct intel_crtc_scaler_state *scaler_state =
  3683. &crtc_state->scaler_state;
  3684. struct intel_crtc *intel_crtc =
  3685. to_intel_crtc(crtc_state->base.crtc);
  3686. int need_scaling;
  3687. need_scaling = intel_rotation_90_or_270(rotation) ?
  3688. (src_h != dst_w || src_w != dst_h):
  3689. (src_w != dst_w || src_h != dst_h);
  3690. /*
  3691. * if plane is being disabled or scaler is no more required or force detach
  3692. * - free scaler binded to this plane/crtc
  3693. * - in order to do this, update crtc->scaler_usage
  3694. *
  3695. * Here scaler state in crtc_state is set free so that
  3696. * scaler can be assigned to other user. Actual register
  3697. * update to free the scaler is done in plane/panel-fit programming.
  3698. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3699. */
  3700. if (force_detach || !need_scaling) {
  3701. if (*scaler_id >= 0) {
  3702. scaler_state->scaler_users &= ~(1 << scaler_user);
  3703. scaler_state->scalers[*scaler_id].in_use = 0;
  3704. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3705. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3706. intel_crtc->pipe, scaler_user, *scaler_id,
  3707. scaler_state->scaler_users);
  3708. *scaler_id = -1;
  3709. }
  3710. return 0;
  3711. }
  3712. /* range checks */
  3713. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3714. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3715. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3716. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3717. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3718. "size is out of scaler range\n",
  3719. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3720. return -EINVAL;
  3721. }
  3722. /* mark this plane as a scaler user in crtc_state */
  3723. scaler_state->scaler_users |= (1 << scaler_user);
  3724. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3725. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3726. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3727. scaler_state->scaler_users);
  3728. return 0;
  3729. }
  3730. /**
  3731. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3732. *
  3733. * @state: crtc's scaler state
  3734. *
  3735. * Return
  3736. * 0 - scaler_usage updated successfully
  3737. * error - requested scaling cannot be supported or other error condition
  3738. */
  3739. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3740. {
  3741. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3742. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3743. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3744. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3745. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3746. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3747. state->pipe_src_w, state->pipe_src_h,
  3748. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3749. }
  3750. /**
  3751. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3752. *
  3753. * @state: crtc's scaler state
  3754. * @plane_state: atomic plane state to update
  3755. *
  3756. * Return
  3757. * 0 - scaler_usage updated successfully
  3758. * error - requested scaling cannot be supported or other error condition
  3759. */
  3760. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3761. struct intel_plane_state *plane_state)
  3762. {
  3763. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3764. struct intel_plane *intel_plane =
  3765. to_intel_plane(plane_state->base.plane);
  3766. struct drm_framebuffer *fb = plane_state->base.fb;
  3767. int ret;
  3768. bool force_detach = !fb || !plane_state->visible;
  3769. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3770. intel_plane->base.base.id, intel_crtc->pipe,
  3771. drm_plane_index(&intel_plane->base));
  3772. ret = skl_update_scaler(crtc_state, force_detach,
  3773. drm_plane_index(&intel_plane->base),
  3774. &plane_state->scaler_id,
  3775. plane_state->base.rotation,
  3776. drm_rect_width(&plane_state->src) >> 16,
  3777. drm_rect_height(&plane_state->src) >> 16,
  3778. drm_rect_width(&plane_state->dst),
  3779. drm_rect_height(&plane_state->dst));
  3780. if (ret || plane_state->scaler_id < 0)
  3781. return ret;
  3782. /* check colorkey */
  3783. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3784. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3785. intel_plane->base.base.id);
  3786. return -EINVAL;
  3787. }
  3788. /* Check src format */
  3789. switch (fb->pixel_format) {
  3790. case DRM_FORMAT_RGB565:
  3791. case DRM_FORMAT_XBGR8888:
  3792. case DRM_FORMAT_XRGB8888:
  3793. case DRM_FORMAT_ABGR8888:
  3794. case DRM_FORMAT_ARGB8888:
  3795. case DRM_FORMAT_XRGB2101010:
  3796. case DRM_FORMAT_XBGR2101010:
  3797. case DRM_FORMAT_YUYV:
  3798. case DRM_FORMAT_YVYU:
  3799. case DRM_FORMAT_UYVY:
  3800. case DRM_FORMAT_VYUY:
  3801. break;
  3802. default:
  3803. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3804. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3805. return -EINVAL;
  3806. }
  3807. return 0;
  3808. }
  3809. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3810. {
  3811. int i;
  3812. for (i = 0; i < crtc->num_scalers; i++)
  3813. skl_detach_scaler(crtc, i);
  3814. }
  3815. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3816. {
  3817. struct drm_device *dev = crtc->base.dev;
  3818. struct drm_i915_private *dev_priv = dev->dev_private;
  3819. int pipe = crtc->pipe;
  3820. struct intel_crtc_scaler_state *scaler_state =
  3821. &crtc->config->scaler_state;
  3822. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3823. if (crtc->config->pch_pfit.enabled) {
  3824. int id;
  3825. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3826. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3827. return;
  3828. }
  3829. id = scaler_state->scaler_id;
  3830. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3831. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3832. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3833. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3834. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3835. }
  3836. }
  3837. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3838. {
  3839. struct drm_device *dev = crtc->base.dev;
  3840. struct drm_i915_private *dev_priv = dev->dev_private;
  3841. int pipe = crtc->pipe;
  3842. if (crtc->config->pch_pfit.enabled) {
  3843. /* Force use of hard-coded filter coefficients
  3844. * as some pre-programmed values are broken,
  3845. * e.g. x201.
  3846. */
  3847. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3848. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3849. PF_PIPE_SEL_IVB(pipe));
  3850. else
  3851. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3852. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3853. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3854. }
  3855. }
  3856. void hsw_enable_ips(struct intel_crtc *crtc)
  3857. {
  3858. struct drm_device *dev = crtc->base.dev;
  3859. struct drm_i915_private *dev_priv = dev->dev_private;
  3860. if (!crtc->config->ips_enabled)
  3861. return;
  3862. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3863. intel_wait_for_vblank(dev, crtc->pipe);
  3864. assert_plane_enabled(dev_priv, crtc->plane);
  3865. if (IS_BROADWELL(dev)) {
  3866. mutex_lock(&dev_priv->rps.hw_lock);
  3867. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3868. mutex_unlock(&dev_priv->rps.hw_lock);
  3869. /* Quoting Art Runyan: "its not safe to expect any particular
  3870. * value in IPS_CTL bit 31 after enabling IPS through the
  3871. * mailbox." Moreover, the mailbox may return a bogus state,
  3872. * so we need to just enable it and continue on.
  3873. */
  3874. } else {
  3875. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3876. /* The bit only becomes 1 in the next vblank, so this wait here
  3877. * is essentially intel_wait_for_vblank. If we don't have this
  3878. * and don't wait for vblanks until the end of crtc_enable, then
  3879. * the HW state readout code will complain that the expected
  3880. * IPS_CTL value is not the one we read. */
  3881. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3882. DRM_ERROR("Timed out waiting for IPS enable\n");
  3883. }
  3884. }
  3885. void hsw_disable_ips(struct intel_crtc *crtc)
  3886. {
  3887. struct drm_device *dev = crtc->base.dev;
  3888. struct drm_i915_private *dev_priv = dev->dev_private;
  3889. if (!crtc->config->ips_enabled)
  3890. return;
  3891. assert_plane_enabled(dev_priv, crtc->plane);
  3892. if (IS_BROADWELL(dev)) {
  3893. mutex_lock(&dev_priv->rps.hw_lock);
  3894. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3895. mutex_unlock(&dev_priv->rps.hw_lock);
  3896. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3897. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3898. DRM_ERROR("Timed out waiting for IPS disable\n");
  3899. } else {
  3900. I915_WRITE(IPS_CTL, 0);
  3901. POSTING_READ(IPS_CTL);
  3902. }
  3903. /* We need to wait for a vblank before we can disable the plane. */
  3904. intel_wait_for_vblank(dev, crtc->pipe);
  3905. }
  3906. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3907. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3908. {
  3909. struct drm_device *dev = crtc->dev;
  3910. struct drm_i915_private *dev_priv = dev->dev_private;
  3911. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3912. enum pipe pipe = intel_crtc->pipe;
  3913. int i;
  3914. bool reenable_ips = false;
  3915. /* The clocks have to be on to load the palette. */
  3916. if (!crtc->state->active)
  3917. return;
  3918. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3919. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3920. assert_dsi_pll_enabled(dev_priv);
  3921. else
  3922. assert_pll_enabled(dev_priv, pipe);
  3923. }
  3924. /* Workaround : Do not read or write the pipe palette/gamma data while
  3925. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3926. */
  3927. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3928. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3929. GAMMA_MODE_MODE_SPLIT)) {
  3930. hsw_disable_ips(intel_crtc);
  3931. reenable_ips = true;
  3932. }
  3933. for (i = 0; i < 256; i++) {
  3934. u32 palreg;
  3935. if (HAS_GMCH_DISPLAY(dev))
  3936. palreg = PALETTE(pipe, i);
  3937. else
  3938. palreg = LGC_PALETTE(pipe, i);
  3939. I915_WRITE(palreg,
  3940. (intel_crtc->lut_r[i] << 16) |
  3941. (intel_crtc->lut_g[i] << 8) |
  3942. intel_crtc->lut_b[i]);
  3943. }
  3944. if (reenable_ips)
  3945. hsw_enable_ips(intel_crtc);
  3946. }
  3947. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3948. {
  3949. if (intel_crtc->overlay) {
  3950. struct drm_device *dev = intel_crtc->base.dev;
  3951. struct drm_i915_private *dev_priv = dev->dev_private;
  3952. mutex_lock(&dev->struct_mutex);
  3953. dev_priv->mm.interruptible = false;
  3954. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3955. dev_priv->mm.interruptible = true;
  3956. mutex_unlock(&dev->struct_mutex);
  3957. }
  3958. /* Let userspace switch the overlay on again. In most cases userspace
  3959. * has to recompute where to put it anyway.
  3960. */
  3961. }
  3962. /**
  3963. * intel_post_enable_primary - Perform operations after enabling primary plane
  3964. * @crtc: the CRTC whose primary plane was just enabled
  3965. *
  3966. * Performs potentially sleeping operations that must be done after the primary
  3967. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3968. * called due to an explicit primary plane update, or due to an implicit
  3969. * re-enable that is caused when a sprite plane is updated to no longer
  3970. * completely hide the primary plane.
  3971. */
  3972. static void
  3973. intel_post_enable_primary(struct drm_crtc *crtc)
  3974. {
  3975. struct drm_device *dev = crtc->dev;
  3976. struct drm_i915_private *dev_priv = dev->dev_private;
  3977. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3978. int pipe = intel_crtc->pipe;
  3979. /*
  3980. * BDW signals flip done immediately if the plane
  3981. * is disabled, even if the plane enable is already
  3982. * armed to occur at the next vblank :(
  3983. */
  3984. if (IS_BROADWELL(dev))
  3985. intel_wait_for_vblank(dev, pipe);
  3986. /*
  3987. * FIXME IPS should be fine as long as one plane is
  3988. * enabled, but in practice it seems to have problems
  3989. * when going from primary only to sprite only and vice
  3990. * versa.
  3991. */
  3992. hsw_enable_ips(intel_crtc);
  3993. /*
  3994. * Gen2 reports pipe underruns whenever all planes are disabled.
  3995. * So don't enable underrun reporting before at least some planes
  3996. * are enabled.
  3997. * FIXME: Need to fix the logic to work when we turn off all planes
  3998. * but leave the pipe running.
  3999. */
  4000. if (IS_GEN2(dev))
  4001. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4002. /* Underruns don't raise interrupts, so check manually. */
  4003. if (HAS_GMCH_DISPLAY(dev))
  4004. i9xx_check_fifo_underruns(dev_priv);
  4005. }
  4006. /**
  4007. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4008. * @crtc: the CRTC whose primary plane is to be disabled
  4009. *
  4010. * Performs potentially sleeping operations that must be done before the
  4011. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4012. * be called due to an explicit primary plane update, or due to an implicit
  4013. * disable that is caused when a sprite plane completely hides the primary
  4014. * plane.
  4015. */
  4016. static void
  4017. intel_pre_disable_primary(struct drm_crtc *crtc)
  4018. {
  4019. struct drm_device *dev = crtc->dev;
  4020. struct drm_i915_private *dev_priv = dev->dev_private;
  4021. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4022. int pipe = intel_crtc->pipe;
  4023. /*
  4024. * Gen2 reports pipe underruns whenever all planes are disabled.
  4025. * So diasble underrun reporting before all the planes get disabled.
  4026. * FIXME: Need to fix the logic to work when we turn off all planes
  4027. * but leave the pipe running.
  4028. */
  4029. if (IS_GEN2(dev))
  4030. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4031. /*
  4032. * Vblank time updates from the shadow to live plane control register
  4033. * are blocked if the memory self-refresh mode is active at that
  4034. * moment. So to make sure the plane gets truly disabled, disable
  4035. * first the self-refresh mode. The self-refresh enable bit in turn
  4036. * will be checked/applied by the HW only at the next frame start
  4037. * event which is after the vblank start event, so we need to have a
  4038. * wait-for-vblank between disabling the plane and the pipe.
  4039. */
  4040. if (HAS_GMCH_DISPLAY(dev)) {
  4041. intel_set_memory_cxsr(dev_priv, false);
  4042. dev_priv->wm.vlv.cxsr = false;
  4043. intel_wait_for_vblank(dev, pipe);
  4044. }
  4045. /*
  4046. * FIXME IPS should be fine as long as one plane is
  4047. * enabled, but in practice it seems to have problems
  4048. * when going from primary only to sprite only and vice
  4049. * versa.
  4050. */
  4051. hsw_disable_ips(intel_crtc);
  4052. }
  4053. static void intel_post_plane_update(struct intel_crtc *crtc)
  4054. {
  4055. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4056. struct drm_device *dev = crtc->base.dev;
  4057. struct drm_i915_private *dev_priv = dev->dev_private;
  4058. struct drm_plane *plane;
  4059. if (atomic->wait_vblank)
  4060. intel_wait_for_vblank(dev, crtc->pipe);
  4061. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4062. if (atomic->disable_cxsr)
  4063. crtc->wm.cxsr_allowed = true;
  4064. if (crtc->atomic.update_wm_post)
  4065. intel_update_watermarks(&crtc->base);
  4066. if (atomic->update_fbc)
  4067. intel_fbc_update(dev_priv);
  4068. if (atomic->post_enable_primary)
  4069. intel_post_enable_primary(&crtc->base);
  4070. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4071. intel_update_sprite_watermarks(plane, &crtc->base,
  4072. 0, 0, 0, false, false);
  4073. memset(atomic, 0, sizeof(*atomic));
  4074. }
  4075. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4076. {
  4077. struct drm_device *dev = crtc->base.dev;
  4078. struct drm_i915_private *dev_priv = dev->dev_private;
  4079. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4080. struct drm_plane *p;
  4081. /* Track fb's for any planes being disabled */
  4082. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4083. struct intel_plane *plane = to_intel_plane(p);
  4084. mutex_lock(&dev->struct_mutex);
  4085. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
  4086. plane->frontbuffer_bit);
  4087. mutex_unlock(&dev->struct_mutex);
  4088. }
  4089. if (atomic->wait_for_flips)
  4090. intel_crtc_wait_for_pending_flips(&crtc->base);
  4091. if (atomic->disable_fbc)
  4092. intel_fbc_disable_crtc(crtc);
  4093. if (crtc->atomic.disable_ips)
  4094. hsw_disable_ips(crtc);
  4095. if (atomic->pre_disable_primary)
  4096. intel_pre_disable_primary(&crtc->base);
  4097. if (atomic->disable_cxsr) {
  4098. crtc->wm.cxsr_allowed = false;
  4099. intel_set_memory_cxsr(dev_priv, false);
  4100. }
  4101. }
  4102. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4103. {
  4104. struct drm_device *dev = crtc->dev;
  4105. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4106. struct drm_plane *p;
  4107. int pipe = intel_crtc->pipe;
  4108. intel_crtc_dpms_overlay_disable(intel_crtc);
  4109. drm_for_each_plane_mask(p, dev, plane_mask)
  4110. to_intel_plane(p)->disable_plane(p, crtc);
  4111. /*
  4112. * FIXME: Once we grow proper nuclear flip support out of this we need
  4113. * to compute the mask of flip planes precisely. For the time being
  4114. * consider this a flip to a NULL plane.
  4115. */
  4116. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4117. }
  4118. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4119. {
  4120. struct drm_device *dev = crtc->dev;
  4121. struct drm_i915_private *dev_priv = dev->dev_private;
  4122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4123. struct intel_encoder *encoder;
  4124. int pipe = intel_crtc->pipe;
  4125. if (WARN_ON(intel_crtc->active))
  4126. return;
  4127. if (intel_crtc->config->has_pch_encoder)
  4128. intel_prepare_shared_dpll(intel_crtc);
  4129. if (intel_crtc->config->has_dp_encoder)
  4130. intel_dp_set_m_n(intel_crtc, M1_N1);
  4131. intel_set_pipe_timings(intel_crtc);
  4132. if (intel_crtc->config->has_pch_encoder) {
  4133. intel_cpu_transcoder_set_m_n(intel_crtc,
  4134. &intel_crtc->config->fdi_m_n, NULL);
  4135. }
  4136. ironlake_set_pipeconf(crtc);
  4137. intel_crtc->active = true;
  4138. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4139. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4140. for_each_encoder_on_crtc(dev, crtc, encoder)
  4141. if (encoder->pre_enable)
  4142. encoder->pre_enable(encoder);
  4143. if (intel_crtc->config->has_pch_encoder) {
  4144. /* Note: FDI PLL enabling _must_ be done before we enable the
  4145. * cpu pipes, hence this is separate from all the other fdi/pch
  4146. * enabling. */
  4147. ironlake_fdi_pll_enable(intel_crtc);
  4148. } else {
  4149. assert_fdi_tx_disabled(dev_priv, pipe);
  4150. assert_fdi_rx_disabled(dev_priv, pipe);
  4151. }
  4152. ironlake_pfit_enable(intel_crtc);
  4153. /*
  4154. * On ILK+ LUT must be loaded before the pipe is running but with
  4155. * clocks enabled
  4156. */
  4157. intel_crtc_load_lut(crtc);
  4158. intel_update_watermarks(crtc);
  4159. intel_enable_pipe(intel_crtc);
  4160. if (intel_crtc->config->has_pch_encoder)
  4161. ironlake_pch_enable(crtc);
  4162. assert_vblank_disabled(crtc);
  4163. drm_crtc_vblank_on(crtc);
  4164. for_each_encoder_on_crtc(dev, crtc, encoder)
  4165. encoder->enable(encoder);
  4166. if (HAS_PCH_CPT(dev))
  4167. cpt_verify_modeset(dev, intel_crtc->pipe);
  4168. }
  4169. /* IPS only exists on ULT machines and is tied to pipe A. */
  4170. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4171. {
  4172. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4173. }
  4174. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4175. {
  4176. struct drm_device *dev = crtc->dev;
  4177. struct drm_i915_private *dev_priv = dev->dev_private;
  4178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4179. struct intel_encoder *encoder;
  4180. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4181. struct intel_crtc_state *pipe_config =
  4182. to_intel_crtc_state(crtc->state);
  4183. if (WARN_ON(intel_crtc->active))
  4184. return;
  4185. if (intel_crtc_to_shared_dpll(intel_crtc))
  4186. intel_enable_shared_dpll(intel_crtc);
  4187. if (intel_crtc->config->has_dp_encoder)
  4188. intel_dp_set_m_n(intel_crtc, M1_N1);
  4189. intel_set_pipe_timings(intel_crtc);
  4190. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4191. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4192. intel_crtc->config->pixel_multiplier - 1);
  4193. }
  4194. if (intel_crtc->config->has_pch_encoder) {
  4195. intel_cpu_transcoder_set_m_n(intel_crtc,
  4196. &intel_crtc->config->fdi_m_n, NULL);
  4197. }
  4198. haswell_set_pipeconf(crtc);
  4199. intel_set_pipe_csc(crtc);
  4200. intel_crtc->active = true;
  4201. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4202. for_each_encoder_on_crtc(dev, crtc, encoder)
  4203. if (encoder->pre_enable)
  4204. encoder->pre_enable(encoder);
  4205. if (intel_crtc->config->has_pch_encoder) {
  4206. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4207. true);
  4208. dev_priv->display.fdi_link_train(crtc);
  4209. }
  4210. intel_ddi_enable_pipe_clock(intel_crtc);
  4211. if (INTEL_INFO(dev)->gen >= 9)
  4212. skylake_pfit_enable(intel_crtc);
  4213. else
  4214. ironlake_pfit_enable(intel_crtc);
  4215. /*
  4216. * On ILK+ LUT must be loaded before the pipe is running but with
  4217. * clocks enabled
  4218. */
  4219. intel_crtc_load_lut(crtc);
  4220. intel_ddi_set_pipe_settings(crtc);
  4221. intel_ddi_enable_transcoder_func(crtc);
  4222. intel_update_watermarks(crtc);
  4223. intel_enable_pipe(intel_crtc);
  4224. if (intel_crtc->config->has_pch_encoder)
  4225. lpt_pch_enable(crtc);
  4226. if (intel_crtc->config->dp_encoder_is_mst)
  4227. intel_ddi_set_vc_payload_alloc(crtc, true);
  4228. assert_vblank_disabled(crtc);
  4229. drm_crtc_vblank_on(crtc);
  4230. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4231. encoder->enable(encoder);
  4232. intel_opregion_notify_encoder(encoder, true);
  4233. }
  4234. /* If we change the relative order between pipe/planes enabling, we need
  4235. * to change the workaround. */
  4236. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4237. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4238. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4239. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4240. }
  4241. }
  4242. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4243. {
  4244. struct drm_device *dev = crtc->base.dev;
  4245. struct drm_i915_private *dev_priv = dev->dev_private;
  4246. int pipe = crtc->pipe;
  4247. /* To avoid upsetting the power well on haswell only disable the pfit if
  4248. * it's in use. The hw state code will make sure we get this right. */
  4249. if (force || crtc->config->pch_pfit.enabled) {
  4250. I915_WRITE(PF_CTL(pipe), 0);
  4251. I915_WRITE(PF_WIN_POS(pipe), 0);
  4252. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4253. }
  4254. }
  4255. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4256. {
  4257. struct drm_device *dev = crtc->dev;
  4258. struct drm_i915_private *dev_priv = dev->dev_private;
  4259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4260. struct intel_encoder *encoder;
  4261. int pipe = intel_crtc->pipe;
  4262. u32 reg, temp;
  4263. for_each_encoder_on_crtc(dev, crtc, encoder)
  4264. encoder->disable(encoder);
  4265. drm_crtc_vblank_off(crtc);
  4266. assert_vblank_disabled(crtc);
  4267. if (intel_crtc->config->has_pch_encoder)
  4268. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4269. intel_disable_pipe(intel_crtc);
  4270. ironlake_pfit_disable(intel_crtc, false);
  4271. if (intel_crtc->config->has_pch_encoder)
  4272. ironlake_fdi_disable(crtc);
  4273. for_each_encoder_on_crtc(dev, crtc, encoder)
  4274. if (encoder->post_disable)
  4275. encoder->post_disable(encoder);
  4276. if (intel_crtc->config->has_pch_encoder) {
  4277. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4278. if (HAS_PCH_CPT(dev)) {
  4279. /* disable TRANS_DP_CTL */
  4280. reg = TRANS_DP_CTL(pipe);
  4281. temp = I915_READ(reg);
  4282. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4283. TRANS_DP_PORT_SEL_MASK);
  4284. temp |= TRANS_DP_PORT_SEL_NONE;
  4285. I915_WRITE(reg, temp);
  4286. /* disable DPLL_SEL */
  4287. temp = I915_READ(PCH_DPLL_SEL);
  4288. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4289. I915_WRITE(PCH_DPLL_SEL, temp);
  4290. }
  4291. ironlake_fdi_pll_disable(intel_crtc);
  4292. }
  4293. intel_crtc->active = false;
  4294. intel_update_watermarks(crtc);
  4295. }
  4296. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4297. {
  4298. struct drm_device *dev = crtc->dev;
  4299. struct drm_i915_private *dev_priv = dev->dev_private;
  4300. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4301. struct intel_encoder *encoder;
  4302. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4303. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4304. intel_opregion_notify_encoder(encoder, false);
  4305. encoder->disable(encoder);
  4306. }
  4307. drm_crtc_vblank_off(crtc);
  4308. assert_vblank_disabled(crtc);
  4309. if (intel_crtc->config->has_pch_encoder)
  4310. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4311. false);
  4312. intel_disable_pipe(intel_crtc);
  4313. if (intel_crtc->config->dp_encoder_is_mst)
  4314. intel_ddi_set_vc_payload_alloc(crtc, false);
  4315. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4316. if (INTEL_INFO(dev)->gen >= 9)
  4317. skylake_scaler_disable(intel_crtc);
  4318. else
  4319. ironlake_pfit_disable(intel_crtc, false);
  4320. intel_ddi_disable_pipe_clock(intel_crtc);
  4321. if (intel_crtc->config->has_pch_encoder) {
  4322. lpt_disable_pch_transcoder(dev_priv);
  4323. intel_ddi_fdi_disable(crtc);
  4324. }
  4325. for_each_encoder_on_crtc(dev, crtc, encoder)
  4326. if (encoder->post_disable)
  4327. encoder->post_disable(encoder);
  4328. intel_crtc->active = false;
  4329. intel_update_watermarks(crtc);
  4330. }
  4331. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4332. {
  4333. struct drm_device *dev = crtc->base.dev;
  4334. struct drm_i915_private *dev_priv = dev->dev_private;
  4335. struct intel_crtc_state *pipe_config = crtc->config;
  4336. if (!pipe_config->gmch_pfit.control)
  4337. return;
  4338. /*
  4339. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4340. * according to register description and PRM.
  4341. */
  4342. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4343. assert_pipe_disabled(dev_priv, crtc->pipe);
  4344. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4345. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4346. /* Border color in case we don't scale up to the full screen. Black by
  4347. * default, change to something else for debugging. */
  4348. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4349. }
  4350. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4351. {
  4352. switch (port) {
  4353. case PORT_A:
  4354. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4355. case PORT_B:
  4356. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4357. case PORT_C:
  4358. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4359. case PORT_D:
  4360. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4361. case PORT_E:
  4362. return POWER_DOMAIN_PORT_DDI_E_2_LANES;
  4363. default:
  4364. WARN_ON_ONCE(1);
  4365. return POWER_DOMAIN_PORT_OTHER;
  4366. }
  4367. }
  4368. #define for_each_power_domain(domain, mask) \
  4369. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4370. if ((1 << (domain)) & (mask))
  4371. enum intel_display_power_domain
  4372. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4373. {
  4374. struct drm_device *dev = intel_encoder->base.dev;
  4375. struct intel_digital_port *intel_dig_port;
  4376. switch (intel_encoder->type) {
  4377. case INTEL_OUTPUT_UNKNOWN:
  4378. /* Only DDI platforms should ever use this output type */
  4379. WARN_ON_ONCE(!HAS_DDI(dev));
  4380. case INTEL_OUTPUT_DISPLAYPORT:
  4381. case INTEL_OUTPUT_HDMI:
  4382. case INTEL_OUTPUT_EDP:
  4383. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4384. return port_to_power_domain(intel_dig_port->port);
  4385. case INTEL_OUTPUT_DP_MST:
  4386. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4387. return port_to_power_domain(intel_dig_port->port);
  4388. case INTEL_OUTPUT_ANALOG:
  4389. return POWER_DOMAIN_PORT_CRT;
  4390. case INTEL_OUTPUT_DSI:
  4391. return POWER_DOMAIN_PORT_DSI;
  4392. default:
  4393. return POWER_DOMAIN_PORT_OTHER;
  4394. }
  4395. }
  4396. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4397. {
  4398. struct drm_device *dev = crtc->dev;
  4399. struct intel_encoder *intel_encoder;
  4400. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4401. enum pipe pipe = intel_crtc->pipe;
  4402. unsigned long mask;
  4403. enum transcoder transcoder;
  4404. if (!crtc->state->active)
  4405. return 0;
  4406. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4407. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4408. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4409. if (intel_crtc->config->pch_pfit.enabled ||
  4410. intel_crtc->config->pch_pfit.force_thru)
  4411. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4412. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4413. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4414. return mask;
  4415. }
  4416. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4417. {
  4418. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4419. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4420. enum intel_display_power_domain domain;
  4421. unsigned long domains, new_domains, old_domains;
  4422. old_domains = intel_crtc->enabled_power_domains;
  4423. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4424. domains = new_domains & ~old_domains;
  4425. for_each_power_domain(domain, domains)
  4426. intel_display_power_get(dev_priv, domain);
  4427. return old_domains & ~new_domains;
  4428. }
  4429. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4430. unsigned long domains)
  4431. {
  4432. enum intel_display_power_domain domain;
  4433. for_each_power_domain(domain, domains)
  4434. intel_display_power_put(dev_priv, domain);
  4435. }
  4436. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4437. {
  4438. struct drm_device *dev = state->dev;
  4439. struct drm_i915_private *dev_priv = dev->dev_private;
  4440. unsigned long put_domains[I915_MAX_PIPES] = {};
  4441. struct drm_crtc_state *crtc_state;
  4442. struct drm_crtc *crtc;
  4443. int i;
  4444. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4445. if (needs_modeset(crtc->state))
  4446. put_domains[to_intel_crtc(crtc)->pipe] =
  4447. modeset_get_crtc_power_domains(crtc);
  4448. }
  4449. if (dev_priv->display.modeset_commit_cdclk) {
  4450. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4451. if (cdclk != dev_priv->cdclk_freq &&
  4452. !WARN_ON(!state->allow_modeset))
  4453. dev_priv->display.modeset_commit_cdclk(state);
  4454. }
  4455. for (i = 0; i < I915_MAX_PIPES; i++)
  4456. if (put_domains[i])
  4457. modeset_put_power_domains(dev_priv, put_domains[i]);
  4458. }
  4459. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4460. {
  4461. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4462. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4463. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4464. return max_cdclk_freq;
  4465. else if (IS_CHERRYVIEW(dev_priv))
  4466. return max_cdclk_freq*95/100;
  4467. else if (INTEL_INFO(dev_priv)->gen < 4)
  4468. return 2*max_cdclk_freq*90/100;
  4469. else
  4470. return max_cdclk_freq*90/100;
  4471. }
  4472. static void intel_update_max_cdclk(struct drm_device *dev)
  4473. {
  4474. struct drm_i915_private *dev_priv = dev->dev_private;
  4475. if (IS_SKYLAKE(dev)) {
  4476. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4477. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4478. dev_priv->max_cdclk_freq = 675000;
  4479. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4480. dev_priv->max_cdclk_freq = 540000;
  4481. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4482. dev_priv->max_cdclk_freq = 450000;
  4483. else
  4484. dev_priv->max_cdclk_freq = 337500;
  4485. } else if (IS_BROADWELL(dev)) {
  4486. /*
  4487. * FIXME with extra cooling we can allow
  4488. * 540 MHz for ULX and 675 Mhz for ULT.
  4489. * How can we know if extra cooling is
  4490. * available? PCI ID, VTB, something else?
  4491. */
  4492. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4493. dev_priv->max_cdclk_freq = 450000;
  4494. else if (IS_BDW_ULX(dev))
  4495. dev_priv->max_cdclk_freq = 450000;
  4496. else if (IS_BDW_ULT(dev))
  4497. dev_priv->max_cdclk_freq = 540000;
  4498. else
  4499. dev_priv->max_cdclk_freq = 675000;
  4500. } else if (IS_CHERRYVIEW(dev)) {
  4501. dev_priv->max_cdclk_freq = 320000;
  4502. } else if (IS_VALLEYVIEW(dev)) {
  4503. dev_priv->max_cdclk_freq = 400000;
  4504. } else {
  4505. /* otherwise assume cdclk is fixed */
  4506. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4507. }
  4508. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4509. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4510. dev_priv->max_cdclk_freq);
  4511. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4512. dev_priv->max_dotclk_freq);
  4513. }
  4514. static void intel_update_cdclk(struct drm_device *dev)
  4515. {
  4516. struct drm_i915_private *dev_priv = dev->dev_private;
  4517. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4518. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4519. dev_priv->cdclk_freq);
  4520. /*
  4521. * Program the gmbus_freq based on the cdclk frequency.
  4522. * BSpec erroneously claims we should aim for 4MHz, but
  4523. * in fact 1MHz is the correct frequency.
  4524. */
  4525. if (IS_VALLEYVIEW(dev)) {
  4526. /*
  4527. * Program the gmbus_freq based on the cdclk frequency.
  4528. * BSpec erroneously claims we should aim for 4MHz, but
  4529. * in fact 1MHz is the correct frequency.
  4530. */
  4531. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4532. }
  4533. if (dev_priv->max_cdclk_freq == 0)
  4534. intel_update_max_cdclk(dev);
  4535. }
  4536. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4537. {
  4538. struct drm_i915_private *dev_priv = dev->dev_private;
  4539. uint32_t divider;
  4540. uint32_t ratio;
  4541. uint32_t current_freq;
  4542. int ret;
  4543. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4544. switch (frequency) {
  4545. case 144000:
  4546. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4547. ratio = BXT_DE_PLL_RATIO(60);
  4548. break;
  4549. case 288000:
  4550. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4551. ratio = BXT_DE_PLL_RATIO(60);
  4552. break;
  4553. case 384000:
  4554. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4555. ratio = BXT_DE_PLL_RATIO(60);
  4556. break;
  4557. case 576000:
  4558. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4559. ratio = BXT_DE_PLL_RATIO(60);
  4560. break;
  4561. case 624000:
  4562. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4563. ratio = BXT_DE_PLL_RATIO(65);
  4564. break;
  4565. case 19200:
  4566. /*
  4567. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4568. * to suppress GCC warning.
  4569. */
  4570. ratio = 0;
  4571. divider = 0;
  4572. break;
  4573. default:
  4574. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4575. return;
  4576. }
  4577. mutex_lock(&dev_priv->rps.hw_lock);
  4578. /* Inform power controller of upcoming frequency change */
  4579. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4580. 0x80000000);
  4581. mutex_unlock(&dev_priv->rps.hw_lock);
  4582. if (ret) {
  4583. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4584. ret, frequency);
  4585. return;
  4586. }
  4587. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4588. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4589. current_freq = current_freq * 500 + 1000;
  4590. /*
  4591. * DE PLL has to be disabled when
  4592. * - setting to 19.2MHz (bypass, PLL isn't used)
  4593. * - before setting to 624MHz (PLL needs toggling)
  4594. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4595. */
  4596. if (frequency == 19200 || frequency == 624000 ||
  4597. current_freq == 624000) {
  4598. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4599. /* Timeout 200us */
  4600. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4601. 1))
  4602. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4603. }
  4604. if (frequency != 19200) {
  4605. uint32_t val;
  4606. val = I915_READ(BXT_DE_PLL_CTL);
  4607. val &= ~BXT_DE_PLL_RATIO_MASK;
  4608. val |= ratio;
  4609. I915_WRITE(BXT_DE_PLL_CTL, val);
  4610. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4611. /* Timeout 200us */
  4612. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4613. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4614. val = I915_READ(CDCLK_CTL);
  4615. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4616. val |= divider;
  4617. /*
  4618. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4619. * enable otherwise.
  4620. */
  4621. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4622. if (frequency >= 500000)
  4623. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4624. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4625. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4626. val |= (frequency - 1000) / 500;
  4627. I915_WRITE(CDCLK_CTL, val);
  4628. }
  4629. mutex_lock(&dev_priv->rps.hw_lock);
  4630. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4631. DIV_ROUND_UP(frequency, 25000));
  4632. mutex_unlock(&dev_priv->rps.hw_lock);
  4633. if (ret) {
  4634. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4635. ret, frequency);
  4636. return;
  4637. }
  4638. intel_update_cdclk(dev);
  4639. }
  4640. void broxton_init_cdclk(struct drm_device *dev)
  4641. {
  4642. struct drm_i915_private *dev_priv = dev->dev_private;
  4643. uint32_t val;
  4644. /*
  4645. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4646. * or else the reset will hang because there is no PCH to respond.
  4647. * Move the handshake programming to initialization sequence.
  4648. * Previously was left up to BIOS.
  4649. */
  4650. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4651. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4652. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4653. /* Enable PG1 for cdclk */
  4654. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4655. /* check if cd clock is enabled */
  4656. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4657. DRM_DEBUG_KMS("Display already initialized\n");
  4658. return;
  4659. }
  4660. /*
  4661. * FIXME:
  4662. * - The initial CDCLK needs to be read from VBT.
  4663. * Need to make this change after VBT has changes for BXT.
  4664. * - check if setting the max (or any) cdclk freq is really necessary
  4665. * here, it belongs to modeset time
  4666. */
  4667. broxton_set_cdclk(dev, 624000);
  4668. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4669. POSTING_READ(DBUF_CTL);
  4670. udelay(10);
  4671. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4672. DRM_ERROR("DBuf power enable timeout!\n");
  4673. }
  4674. void broxton_uninit_cdclk(struct drm_device *dev)
  4675. {
  4676. struct drm_i915_private *dev_priv = dev->dev_private;
  4677. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4678. POSTING_READ(DBUF_CTL);
  4679. udelay(10);
  4680. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4681. DRM_ERROR("DBuf power disable timeout!\n");
  4682. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4683. broxton_set_cdclk(dev, 19200);
  4684. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4685. }
  4686. static const struct skl_cdclk_entry {
  4687. unsigned int freq;
  4688. unsigned int vco;
  4689. } skl_cdclk_frequencies[] = {
  4690. { .freq = 308570, .vco = 8640 },
  4691. { .freq = 337500, .vco = 8100 },
  4692. { .freq = 432000, .vco = 8640 },
  4693. { .freq = 450000, .vco = 8100 },
  4694. { .freq = 540000, .vco = 8100 },
  4695. { .freq = 617140, .vco = 8640 },
  4696. { .freq = 675000, .vco = 8100 },
  4697. };
  4698. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4699. {
  4700. return (freq - 1000) / 500;
  4701. }
  4702. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4703. {
  4704. unsigned int i;
  4705. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4706. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4707. if (e->freq == freq)
  4708. return e->vco;
  4709. }
  4710. return 8100;
  4711. }
  4712. static void
  4713. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4714. {
  4715. unsigned int min_freq;
  4716. u32 val;
  4717. /* select the minimum CDCLK before enabling DPLL 0 */
  4718. val = I915_READ(CDCLK_CTL);
  4719. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4720. val |= CDCLK_FREQ_337_308;
  4721. if (required_vco == 8640)
  4722. min_freq = 308570;
  4723. else
  4724. min_freq = 337500;
  4725. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4726. I915_WRITE(CDCLK_CTL, val);
  4727. POSTING_READ(CDCLK_CTL);
  4728. /*
  4729. * We always enable DPLL0 with the lowest link rate possible, but still
  4730. * taking into account the VCO required to operate the eDP panel at the
  4731. * desired frequency. The usual DP link rates operate with a VCO of
  4732. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4733. * The modeset code is responsible for the selection of the exact link
  4734. * rate later on, with the constraint of choosing a frequency that
  4735. * works with required_vco.
  4736. */
  4737. val = I915_READ(DPLL_CTRL1);
  4738. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4739. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4740. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4741. if (required_vco == 8640)
  4742. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4743. SKL_DPLL0);
  4744. else
  4745. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4746. SKL_DPLL0);
  4747. I915_WRITE(DPLL_CTRL1, val);
  4748. POSTING_READ(DPLL_CTRL1);
  4749. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4750. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4751. DRM_ERROR("DPLL0 not locked\n");
  4752. }
  4753. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4754. {
  4755. int ret;
  4756. u32 val;
  4757. /* inform PCU we want to change CDCLK */
  4758. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4759. mutex_lock(&dev_priv->rps.hw_lock);
  4760. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4761. mutex_unlock(&dev_priv->rps.hw_lock);
  4762. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4763. }
  4764. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4765. {
  4766. unsigned int i;
  4767. for (i = 0; i < 15; i++) {
  4768. if (skl_cdclk_pcu_ready(dev_priv))
  4769. return true;
  4770. udelay(10);
  4771. }
  4772. return false;
  4773. }
  4774. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4775. {
  4776. struct drm_device *dev = dev_priv->dev;
  4777. u32 freq_select, pcu_ack;
  4778. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4779. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4780. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4781. return;
  4782. }
  4783. /* set CDCLK_CTL */
  4784. switch(freq) {
  4785. case 450000:
  4786. case 432000:
  4787. freq_select = CDCLK_FREQ_450_432;
  4788. pcu_ack = 1;
  4789. break;
  4790. case 540000:
  4791. freq_select = CDCLK_FREQ_540;
  4792. pcu_ack = 2;
  4793. break;
  4794. case 308570:
  4795. case 337500:
  4796. default:
  4797. freq_select = CDCLK_FREQ_337_308;
  4798. pcu_ack = 0;
  4799. break;
  4800. case 617140:
  4801. case 675000:
  4802. freq_select = CDCLK_FREQ_675_617;
  4803. pcu_ack = 3;
  4804. break;
  4805. }
  4806. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4807. POSTING_READ(CDCLK_CTL);
  4808. /* inform PCU of the change */
  4809. mutex_lock(&dev_priv->rps.hw_lock);
  4810. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4811. mutex_unlock(&dev_priv->rps.hw_lock);
  4812. intel_update_cdclk(dev);
  4813. }
  4814. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4815. {
  4816. /* disable DBUF power */
  4817. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4818. POSTING_READ(DBUF_CTL);
  4819. udelay(10);
  4820. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4821. DRM_ERROR("DBuf power disable timeout\n");
  4822. /*
  4823. * DMC assumes ownership of LCPLL and will get confused if we touch it.
  4824. */
  4825. if (dev_priv->csr.dmc_payload) {
  4826. /* disable DPLL0 */
  4827. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) &
  4828. ~LCPLL_PLL_ENABLE);
  4829. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4830. DRM_ERROR("Couldn't disable DPLL0\n");
  4831. }
  4832. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4833. }
  4834. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4835. {
  4836. u32 val;
  4837. unsigned int required_vco;
  4838. /* enable PCH reset handshake */
  4839. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4840. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4841. /* enable PG1 and Misc I/O */
  4842. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4843. /* DPLL0 not enabled (happens on early BIOS versions) */
  4844. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4845. /* enable DPLL0 */
  4846. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4847. skl_dpll0_enable(dev_priv, required_vco);
  4848. }
  4849. /* set CDCLK to the frequency the BIOS chose */
  4850. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4851. /* enable DBUF power */
  4852. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4853. POSTING_READ(DBUF_CTL);
  4854. udelay(10);
  4855. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4856. DRM_ERROR("DBuf power enable timeout\n");
  4857. }
  4858. /* returns HPLL frequency in kHz */
  4859. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4860. {
  4861. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4862. /* Obtain SKU information */
  4863. mutex_lock(&dev_priv->sb_lock);
  4864. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4865. CCK_FUSE_HPLL_FREQ_MASK;
  4866. mutex_unlock(&dev_priv->sb_lock);
  4867. return vco_freq[hpll_freq] * 1000;
  4868. }
  4869. /* Adjust CDclk dividers to allow high res or save power if possible */
  4870. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4871. {
  4872. struct drm_i915_private *dev_priv = dev->dev_private;
  4873. u32 val, cmd;
  4874. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4875. != dev_priv->cdclk_freq);
  4876. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4877. cmd = 2;
  4878. else if (cdclk == 266667)
  4879. cmd = 1;
  4880. else
  4881. cmd = 0;
  4882. mutex_lock(&dev_priv->rps.hw_lock);
  4883. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4884. val &= ~DSPFREQGUAR_MASK;
  4885. val |= (cmd << DSPFREQGUAR_SHIFT);
  4886. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4887. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4888. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4889. 50)) {
  4890. DRM_ERROR("timed out waiting for CDclk change\n");
  4891. }
  4892. mutex_unlock(&dev_priv->rps.hw_lock);
  4893. mutex_lock(&dev_priv->sb_lock);
  4894. if (cdclk == 400000) {
  4895. u32 divider;
  4896. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4897. /* adjust cdclk divider */
  4898. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4899. val &= ~DISPLAY_FREQUENCY_VALUES;
  4900. val |= divider;
  4901. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4902. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4903. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4904. 50))
  4905. DRM_ERROR("timed out waiting for CDclk change\n");
  4906. }
  4907. /* adjust self-refresh exit latency value */
  4908. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4909. val &= ~0x7f;
  4910. /*
  4911. * For high bandwidth configs, we set a higher latency in the bunit
  4912. * so that the core display fetch happens in time to avoid underruns.
  4913. */
  4914. if (cdclk == 400000)
  4915. val |= 4500 / 250; /* 4.5 usec */
  4916. else
  4917. val |= 3000 / 250; /* 3.0 usec */
  4918. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4919. mutex_unlock(&dev_priv->sb_lock);
  4920. intel_update_cdclk(dev);
  4921. }
  4922. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4923. {
  4924. struct drm_i915_private *dev_priv = dev->dev_private;
  4925. u32 val, cmd;
  4926. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4927. != dev_priv->cdclk_freq);
  4928. switch (cdclk) {
  4929. case 333333:
  4930. case 320000:
  4931. case 266667:
  4932. case 200000:
  4933. break;
  4934. default:
  4935. MISSING_CASE(cdclk);
  4936. return;
  4937. }
  4938. /*
  4939. * Specs are full of misinformation, but testing on actual
  4940. * hardware has shown that we just need to write the desired
  4941. * CCK divider into the Punit register.
  4942. */
  4943. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4944. mutex_lock(&dev_priv->rps.hw_lock);
  4945. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4946. val &= ~DSPFREQGUAR_MASK_CHV;
  4947. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4948. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4949. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4950. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4951. 50)) {
  4952. DRM_ERROR("timed out waiting for CDclk change\n");
  4953. }
  4954. mutex_unlock(&dev_priv->rps.hw_lock);
  4955. intel_update_cdclk(dev);
  4956. }
  4957. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4958. int max_pixclk)
  4959. {
  4960. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4961. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4962. /*
  4963. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4964. * 200MHz
  4965. * 267MHz
  4966. * 320/333MHz (depends on HPLL freq)
  4967. * 400MHz (VLV only)
  4968. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4969. * of the lower bin and adjust if needed.
  4970. *
  4971. * We seem to get an unstable or solid color picture at 200MHz.
  4972. * Not sure what's wrong. For now use 200MHz only when all pipes
  4973. * are off.
  4974. */
  4975. if (!IS_CHERRYVIEW(dev_priv) &&
  4976. max_pixclk > freq_320*limit/100)
  4977. return 400000;
  4978. else if (max_pixclk > 266667*limit/100)
  4979. return freq_320;
  4980. else if (max_pixclk > 0)
  4981. return 266667;
  4982. else
  4983. return 200000;
  4984. }
  4985. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4986. int max_pixclk)
  4987. {
  4988. /*
  4989. * FIXME:
  4990. * - remove the guardband, it's not needed on BXT
  4991. * - set 19.2MHz bypass frequency if there are no active pipes
  4992. */
  4993. if (max_pixclk > 576000*9/10)
  4994. return 624000;
  4995. else if (max_pixclk > 384000*9/10)
  4996. return 576000;
  4997. else if (max_pixclk > 288000*9/10)
  4998. return 384000;
  4999. else if (max_pixclk > 144000*9/10)
  5000. return 288000;
  5001. else
  5002. return 144000;
  5003. }
  5004. /* Compute the max pixel clock for new configuration. Uses atomic state if
  5005. * that's non-NULL, look at current state otherwise. */
  5006. static int intel_mode_max_pixclk(struct drm_device *dev,
  5007. struct drm_atomic_state *state)
  5008. {
  5009. struct intel_crtc *intel_crtc;
  5010. struct intel_crtc_state *crtc_state;
  5011. int max_pixclk = 0;
  5012. for_each_intel_crtc(dev, intel_crtc) {
  5013. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5014. if (IS_ERR(crtc_state))
  5015. return PTR_ERR(crtc_state);
  5016. if (!crtc_state->base.enable)
  5017. continue;
  5018. max_pixclk = max(max_pixclk,
  5019. crtc_state->base.adjusted_mode.crtc_clock);
  5020. }
  5021. return max_pixclk;
  5022. }
  5023. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5024. {
  5025. struct drm_device *dev = state->dev;
  5026. struct drm_i915_private *dev_priv = dev->dev_private;
  5027. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5028. if (max_pixclk < 0)
  5029. return max_pixclk;
  5030. to_intel_atomic_state(state)->cdclk =
  5031. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5032. return 0;
  5033. }
  5034. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5035. {
  5036. struct drm_device *dev = state->dev;
  5037. struct drm_i915_private *dev_priv = dev->dev_private;
  5038. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5039. if (max_pixclk < 0)
  5040. return max_pixclk;
  5041. to_intel_atomic_state(state)->cdclk =
  5042. broxton_calc_cdclk(dev_priv, max_pixclk);
  5043. return 0;
  5044. }
  5045. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5046. {
  5047. unsigned int credits, default_credits;
  5048. if (IS_CHERRYVIEW(dev_priv))
  5049. default_credits = PFI_CREDIT(12);
  5050. else
  5051. default_credits = PFI_CREDIT(8);
  5052. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5053. /* CHV suggested value is 31 or 63 */
  5054. if (IS_CHERRYVIEW(dev_priv))
  5055. credits = PFI_CREDIT_63;
  5056. else
  5057. credits = PFI_CREDIT(15);
  5058. } else {
  5059. credits = default_credits;
  5060. }
  5061. /*
  5062. * WA - write default credits before re-programming
  5063. * FIXME: should we also set the resend bit here?
  5064. */
  5065. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5066. default_credits);
  5067. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5068. credits | PFI_CREDIT_RESEND);
  5069. /*
  5070. * FIXME is this guaranteed to clear
  5071. * immediately or should we poll for it?
  5072. */
  5073. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5074. }
  5075. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5076. {
  5077. struct drm_device *dev = old_state->dev;
  5078. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5079. struct drm_i915_private *dev_priv = dev->dev_private;
  5080. /*
  5081. * FIXME: We can end up here with all power domains off, yet
  5082. * with a CDCLK frequency other than the minimum. To account
  5083. * for this take the PIPE-A power domain, which covers the HW
  5084. * blocks needed for the following programming. This can be
  5085. * removed once it's guaranteed that we get here either with
  5086. * the minimum CDCLK set, or the required power domains
  5087. * enabled.
  5088. */
  5089. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5090. if (IS_CHERRYVIEW(dev))
  5091. cherryview_set_cdclk(dev, req_cdclk);
  5092. else
  5093. valleyview_set_cdclk(dev, req_cdclk);
  5094. vlv_program_pfi_credits(dev_priv);
  5095. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5096. }
  5097. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5098. {
  5099. struct drm_device *dev = crtc->dev;
  5100. struct drm_i915_private *dev_priv = to_i915(dev);
  5101. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5102. struct intel_encoder *encoder;
  5103. int pipe = intel_crtc->pipe;
  5104. bool is_dsi;
  5105. if (WARN_ON(intel_crtc->active))
  5106. return;
  5107. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5108. if (intel_crtc->config->has_dp_encoder)
  5109. intel_dp_set_m_n(intel_crtc, M1_N1);
  5110. intel_set_pipe_timings(intel_crtc);
  5111. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5112. struct drm_i915_private *dev_priv = dev->dev_private;
  5113. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5114. I915_WRITE(CHV_CANVAS(pipe), 0);
  5115. }
  5116. i9xx_set_pipeconf(intel_crtc);
  5117. intel_crtc->active = true;
  5118. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5119. for_each_encoder_on_crtc(dev, crtc, encoder)
  5120. if (encoder->pre_pll_enable)
  5121. encoder->pre_pll_enable(encoder);
  5122. if (!is_dsi) {
  5123. if (IS_CHERRYVIEW(dev)) {
  5124. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5125. chv_enable_pll(intel_crtc, intel_crtc->config);
  5126. } else {
  5127. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5128. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5129. }
  5130. }
  5131. for_each_encoder_on_crtc(dev, crtc, encoder)
  5132. if (encoder->pre_enable)
  5133. encoder->pre_enable(encoder);
  5134. i9xx_pfit_enable(intel_crtc);
  5135. intel_crtc_load_lut(crtc);
  5136. intel_enable_pipe(intel_crtc);
  5137. assert_vblank_disabled(crtc);
  5138. drm_crtc_vblank_on(crtc);
  5139. for_each_encoder_on_crtc(dev, crtc, encoder)
  5140. encoder->enable(encoder);
  5141. }
  5142. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5143. {
  5144. struct drm_device *dev = crtc->base.dev;
  5145. struct drm_i915_private *dev_priv = dev->dev_private;
  5146. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5147. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5148. }
  5149. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5150. {
  5151. struct drm_device *dev = crtc->dev;
  5152. struct drm_i915_private *dev_priv = to_i915(dev);
  5153. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5154. struct intel_encoder *encoder;
  5155. int pipe = intel_crtc->pipe;
  5156. if (WARN_ON(intel_crtc->active))
  5157. return;
  5158. i9xx_set_pll_dividers(intel_crtc);
  5159. if (intel_crtc->config->has_dp_encoder)
  5160. intel_dp_set_m_n(intel_crtc, M1_N1);
  5161. intel_set_pipe_timings(intel_crtc);
  5162. i9xx_set_pipeconf(intel_crtc);
  5163. intel_crtc->active = true;
  5164. if (!IS_GEN2(dev))
  5165. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5166. for_each_encoder_on_crtc(dev, crtc, encoder)
  5167. if (encoder->pre_enable)
  5168. encoder->pre_enable(encoder);
  5169. i9xx_enable_pll(intel_crtc);
  5170. i9xx_pfit_enable(intel_crtc);
  5171. intel_crtc_load_lut(crtc);
  5172. intel_update_watermarks(crtc);
  5173. intel_enable_pipe(intel_crtc);
  5174. assert_vblank_disabled(crtc);
  5175. drm_crtc_vblank_on(crtc);
  5176. for_each_encoder_on_crtc(dev, crtc, encoder)
  5177. encoder->enable(encoder);
  5178. }
  5179. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5180. {
  5181. struct drm_device *dev = crtc->base.dev;
  5182. struct drm_i915_private *dev_priv = dev->dev_private;
  5183. if (!crtc->config->gmch_pfit.control)
  5184. return;
  5185. assert_pipe_disabled(dev_priv, crtc->pipe);
  5186. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5187. I915_READ(PFIT_CONTROL));
  5188. I915_WRITE(PFIT_CONTROL, 0);
  5189. }
  5190. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5191. {
  5192. struct drm_device *dev = crtc->dev;
  5193. struct drm_i915_private *dev_priv = dev->dev_private;
  5194. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5195. struct intel_encoder *encoder;
  5196. int pipe = intel_crtc->pipe;
  5197. /*
  5198. * On gen2 planes are double buffered but the pipe isn't, so we must
  5199. * wait for planes to fully turn off before disabling the pipe.
  5200. * We also need to wait on all gmch platforms because of the
  5201. * self-refresh mode constraint explained above.
  5202. */
  5203. intel_wait_for_vblank(dev, pipe);
  5204. for_each_encoder_on_crtc(dev, crtc, encoder)
  5205. encoder->disable(encoder);
  5206. drm_crtc_vblank_off(crtc);
  5207. assert_vblank_disabled(crtc);
  5208. intel_disable_pipe(intel_crtc);
  5209. i9xx_pfit_disable(intel_crtc);
  5210. for_each_encoder_on_crtc(dev, crtc, encoder)
  5211. if (encoder->post_disable)
  5212. encoder->post_disable(encoder);
  5213. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5214. if (IS_CHERRYVIEW(dev))
  5215. chv_disable_pll(dev_priv, pipe);
  5216. else if (IS_VALLEYVIEW(dev))
  5217. vlv_disable_pll(dev_priv, pipe);
  5218. else
  5219. i9xx_disable_pll(intel_crtc);
  5220. }
  5221. for_each_encoder_on_crtc(dev, crtc, encoder)
  5222. if (encoder->post_pll_disable)
  5223. encoder->post_pll_disable(encoder);
  5224. if (!IS_GEN2(dev))
  5225. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5226. intel_crtc->active = false;
  5227. intel_update_watermarks(crtc);
  5228. }
  5229. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5230. {
  5231. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5232. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5233. enum intel_display_power_domain domain;
  5234. unsigned long domains;
  5235. if (!intel_crtc->active)
  5236. return;
  5237. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5238. intel_crtc_wait_for_pending_flips(crtc);
  5239. intel_pre_disable_primary(crtc);
  5240. }
  5241. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5242. dev_priv->display.crtc_disable(crtc);
  5243. intel_disable_shared_dpll(intel_crtc);
  5244. domains = intel_crtc->enabled_power_domains;
  5245. for_each_power_domain(domain, domains)
  5246. intel_display_power_put(dev_priv, domain);
  5247. intel_crtc->enabled_power_domains = 0;
  5248. }
  5249. /*
  5250. * turn all crtc's off, but do not adjust state
  5251. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5252. */
  5253. int intel_display_suspend(struct drm_device *dev)
  5254. {
  5255. struct drm_mode_config *config = &dev->mode_config;
  5256. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5257. struct drm_atomic_state *state;
  5258. struct drm_crtc *crtc;
  5259. unsigned crtc_mask = 0;
  5260. int ret = 0;
  5261. if (WARN_ON(!ctx))
  5262. return 0;
  5263. lockdep_assert_held(&ctx->ww_ctx);
  5264. state = drm_atomic_state_alloc(dev);
  5265. if (WARN_ON(!state))
  5266. return -ENOMEM;
  5267. state->acquire_ctx = ctx;
  5268. state->allow_modeset = true;
  5269. for_each_crtc(dev, crtc) {
  5270. struct drm_crtc_state *crtc_state =
  5271. drm_atomic_get_crtc_state(state, crtc);
  5272. ret = PTR_ERR_OR_ZERO(crtc_state);
  5273. if (ret)
  5274. goto free;
  5275. if (!crtc_state->active)
  5276. continue;
  5277. crtc_state->active = false;
  5278. crtc_mask |= 1 << drm_crtc_index(crtc);
  5279. }
  5280. if (crtc_mask) {
  5281. ret = drm_atomic_commit(state);
  5282. if (!ret) {
  5283. for_each_crtc(dev, crtc)
  5284. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5285. crtc->state->active = true;
  5286. return ret;
  5287. }
  5288. }
  5289. free:
  5290. if (ret)
  5291. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5292. drm_atomic_state_free(state);
  5293. return ret;
  5294. }
  5295. void intel_encoder_destroy(struct drm_encoder *encoder)
  5296. {
  5297. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5298. drm_encoder_cleanup(encoder);
  5299. kfree(intel_encoder);
  5300. }
  5301. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5302. * internal consistency). */
  5303. static void intel_connector_check_state(struct intel_connector *connector)
  5304. {
  5305. struct drm_crtc *crtc = connector->base.state->crtc;
  5306. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5307. connector->base.base.id,
  5308. connector->base.name);
  5309. if (connector->get_hw_state(connector)) {
  5310. struct intel_encoder *encoder = connector->encoder;
  5311. struct drm_connector_state *conn_state = connector->base.state;
  5312. I915_STATE_WARN(!crtc,
  5313. "connector enabled without attached crtc\n");
  5314. if (!crtc)
  5315. return;
  5316. I915_STATE_WARN(!crtc->state->active,
  5317. "connector is active, but attached crtc isn't\n");
  5318. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5319. return;
  5320. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5321. "atomic encoder doesn't match attached encoder\n");
  5322. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5323. "attached encoder crtc differs from connector crtc\n");
  5324. } else {
  5325. I915_STATE_WARN(crtc && crtc->state->active,
  5326. "attached crtc is active, but connector isn't\n");
  5327. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5328. "best encoder set without crtc!\n");
  5329. }
  5330. }
  5331. int intel_connector_init(struct intel_connector *connector)
  5332. {
  5333. struct drm_connector_state *connector_state;
  5334. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5335. if (!connector_state)
  5336. return -ENOMEM;
  5337. connector->base.state = connector_state;
  5338. return 0;
  5339. }
  5340. struct intel_connector *intel_connector_alloc(void)
  5341. {
  5342. struct intel_connector *connector;
  5343. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5344. if (!connector)
  5345. return NULL;
  5346. if (intel_connector_init(connector) < 0) {
  5347. kfree(connector);
  5348. return NULL;
  5349. }
  5350. return connector;
  5351. }
  5352. /* Simple connector->get_hw_state implementation for encoders that support only
  5353. * one connector and no cloning and hence the encoder state determines the state
  5354. * of the connector. */
  5355. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5356. {
  5357. enum pipe pipe = 0;
  5358. struct intel_encoder *encoder = connector->encoder;
  5359. return encoder->get_hw_state(encoder, &pipe);
  5360. }
  5361. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5362. {
  5363. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5364. return crtc_state->fdi_lanes;
  5365. return 0;
  5366. }
  5367. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5368. struct intel_crtc_state *pipe_config)
  5369. {
  5370. struct drm_atomic_state *state = pipe_config->base.state;
  5371. struct intel_crtc *other_crtc;
  5372. struct intel_crtc_state *other_crtc_state;
  5373. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5374. pipe_name(pipe), pipe_config->fdi_lanes);
  5375. if (pipe_config->fdi_lanes > 4) {
  5376. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5377. pipe_name(pipe), pipe_config->fdi_lanes);
  5378. return -EINVAL;
  5379. }
  5380. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5381. if (pipe_config->fdi_lanes > 2) {
  5382. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5383. pipe_config->fdi_lanes);
  5384. return -EINVAL;
  5385. } else {
  5386. return 0;
  5387. }
  5388. }
  5389. if (INTEL_INFO(dev)->num_pipes == 2)
  5390. return 0;
  5391. /* Ivybridge 3 pipe is really complicated */
  5392. switch (pipe) {
  5393. case PIPE_A:
  5394. return 0;
  5395. case PIPE_B:
  5396. if (pipe_config->fdi_lanes <= 2)
  5397. return 0;
  5398. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5399. other_crtc_state =
  5400. intel_atomic_get_crtc_state(state, other_crtc);
  5401. if (IS_ERR(other_crtc_state))
  5402. return PTR_ERR(other_crtc_state);
  5403. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5404. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5405. pipe_name(pipe), pipe_config->fdi_lanes);
  5406. return -EINVAL;
  5407. }
  5408. return 0;
  5409. case PIPE_C:
  5410. if (pipe_config->fdi_lanes > 2) {
  5411. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5412. pipe_name(pipe), pipe_config->fdi_lanes);
  5413. return -EINVAL;
  5414. }
  5415. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5416. other_crtc_state =
  5417. intel_atomic_get_crtc_state(state, other_crtc);
  5418. if (IS_ERR(other_crtc_state))
  5419. return PTR_ERR(other_crtc_state);
  5420. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5421. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5422. return -EINVAL;
  5423. }
  5424. return 0;
  5425. default:
  5426. BUG();
  5427. }
  5428. }
  5429. #define RETRY 1
  5430. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5431. struct intel_crtc_state *pipe_config)
  5432. {
  5433. struct drm_device *dev = intel_crtc->base.dev;
  5434. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5435. int lane, link_bw, fdi_dotclock, ret;
  5436. bool needs_recompute = false;
  5437. retry:
  5438. /* FDI is a binary signal running at ~2.7GHz, encoding
  5439. * each output octet as 10 bits. The actual frequency
  5440. * is stored as a divider into a 100MHz clock, and the
  5441. * mode pixel clock is stored in units of 1KHz.
  5442. * Hence the bw of each lane in terms of the mode signal
  5443. * is:
  5444. */
  5445. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5446. fdi_dotclock = adjusted_mode->crtc_clock;
  5447. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5448. pipe_config->pipe_bpp);
  5449. pipe_config->fdi_lanes = lane;
  5450. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5451. link_bw, &pipe_config->fdi_m_n);
  5452. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5453. intel_crtc->pipe, pipe_config);
  5454. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5455. pipe_config->pipe_bpp -= 2*3;
  5456. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5457. pipe_config->pipe_bpp);
  5458. needs_recompute = true;
  5459. pipe_config->bw_constrained = true;
  5460. goto retry;
  5461. }
  5462. if (needs_recompute)
  5463. return RETRY;
  5464. return ret;
  5465. }
  5466. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5467. struct intel_crtc_state *pipe_config)
  5468. {
  5469. if (pipe_config->pipe_bpp > 24)
  5470. return false;
  5471. /* HSW can handle pixel rate up to cdclk? */
  5472. if (IS_HASWELL(dev_priv->dev))
  5473. return true;
  5474. /*
  5475. * We compare against max which means we must take
  5476. * the increased cdclk requirement into account when
  5477. * calculating the new cdclk.
  5478. *
  5479. * Should measure whether using a lower cdclk w/o IPS
  5480. */
  5481. return ilk_pipe_pixel_rate(pipe_config) <=
  5482. dev_priv->max_cdclk_freq * 95 / 100;
  5483. }
  5484. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5485. struct intel_crtc_state *pipe_config)
  5486. {
  5487. struct drm_device *dev = crtc->base.dev;
  5488. struct drm_i915_private *dev_priv = dev->dev_private;
  5489. pipe_config->ips_enabled = i915.enable_ips &&
  5490. hsw_crtc_supports_ips(crtc) &&
  5491. pipe_config_supports_ips(dev_priv, pipe_config);
  5492. }
  5493. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5494. struct intel_crtc_state *pipe_config)
  5495. {
  5496. struct drm_device *dev = crtc->base.dev;
  5497. struct drm_i915_private *dev_priv = dev->dev_private;
  5498. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5499. /* FIXME should check pixel clock limits on all platforms */
  5500. if (INTEL_INFO(dev)->gen < 4) {
  5501. int clock_limit = dev_priv->max_cdclk_freq;
  5502. /*
  5503. * Enable pixel doubling when the dot clock
  5504. * is > 90% of the (display) core speed.
  5505. *
  5506. * GDG double wide on either pipe,
  5507. * otherwise pipe A only.
  5508. */
  5509. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5510. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5511. clock_limit *= 2;
  5512. pipe_config->double_wide = true;
  5513. }
  5514. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5515. return -EINVAL;
  5516. }
  5517. /*
  5518. * Pipe horizontal size must be even in:
  5519. * - DVO ganged mode
  5520. * - LVDS dual channel mode
  5521. * - Double wide pipe
  5522. */
  5523. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5524. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5525. pipe_config->pipe_src_w &= ~1;
  5526. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5527. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5528. */
  5529. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5530. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5531. return -EINVAL;
  5532. if (HAS_IPS(dev))
  5533. hsw_compute_ips_config(crtc, pipe_config);
  5534. if (pipe_config->has_pch_encoder)
  5535. return ironlake_fdi_compute_config(crtc, pipe_config);
  5536. return 0;
  5537. }
  5538. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5539. {
  5540. struct drm_i915_private *dev_priv = to_i915(dev);
  5541. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5542. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5543. uint32_t linkrate;
  5544. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5545. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5546. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5547. return 540000;
  5548. linkrate = (I915_READ(DPLL_CTRL1) &
  5549. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5550. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5551. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5552. /* vco 8640 */
  5553. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5554. case CDCLK_FREQ_450_432:
  5555. return 432000;
  5556. case CDCLK_FREQ_337_308:
  5557. return 308570;
  5558. case CDCLK_FREQ_675_617:
  5559. return 617140;
  5560. default:
  5561. WARN(1, "Unknown cd freq selection\n");
  5562. }
  5563. } else {
  5564. /* vco 8100 */
  5565. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5566. case CDCLK_FREQ_450_432:
  5567. return 450000;
  5568. case CDCLK_FREQ_337_308:
  5569. return 337500;
  5570. case CDCLK_FREQ_675_617:
  5571. return 675000;
  5572. default:
  5573. WARN(1, "Unknown cd freq selection\n");
  5574. }
  5575. }
  5576. /* error case, do as if DPLL0 isn't enabled */
  5577. return 24000;
  5578. }
  5579. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5580. {
  5581. struct drm_i915_private *dev_priv = to_i915(dev);
  5582. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5583. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5584. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5585. int cdclk;
  5586. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5587. return 19200;
  5588. cdclk = 19200 * pll_ratio / 2;
  5589. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5590. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5591. return cdclk; /* 576MHz or 624MHz */
  5592. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5593. return cdclk * 2 / 3; /* 384MHz */
  5594. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5595. return cdclk / 2; /* 288MHz */
  5596. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5597. return cdclk / 4; /* 144MHz */
  5598. }
  5599. /* error case, do as if DE PLL isn't enabled */
  5600. return 19200;
  5601. }
  5602. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5603. {
  5604. struct drm_i915_private *dev_priv = dev->dev_private;
  5605. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5606. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5607. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5608. return 800000;
  5609. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5610. return 450000;
  5611. else if (freq == LCPLL_CLK_FREQ_450)
  5612. return 450000;
  5613. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5614. return 540000;
  5615. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5616. return 337500;
  5617. else
  5618. return 675000;
  5619. }
  5620. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5621. {
  5622. struct drm_i915_private *dev_priv = dev->dev_private;
  5623. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5624. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5625. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5626. return 800000;
  5627. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5628. return 450000;
  5629. else if (freq == LCPLL_CLK_FREQ_450)
  5630. return 450000;
  5631. else if (IS_HSW_ULT(dev))
  5632. return 337500;
  5633. else
  5634. return 540000;
  5635. }
  5636. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5637. {
  5638. struct drm_i915_private *dev_priv = dev->dev_private;
  5639. u32 val;
  5640. int divider;
  5641. if (dev_priv->hpll_freq == 0)
  5642. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5643. mutex_lock(&dev_priv->sb_lock);
  5644. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5645. mutex_unlock(&dev_priv->sb_lock);
  5646. divider = val & DISPLAY_FREQUENCY_VALUES;
  5647. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5648. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5649. "cdclk change in progress\n");
  5650. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5651. }
  5652. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5653. {
  5654. return 450000;
  5655. }
  5656. static int i945_get_display_clock_speed(struct drm_device *dev)
  5657. {
  5658. return 400000;
  5659. }
  5660. static int i915_get_display_clock_speed(struct drm_device *dev)
  5661. {
  5662. return 333333;
  5663. }
  5664. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5665. {
  5666. return 200000;
  5667. }
  5668. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5669. {
  5670. u16 gcfgc = 0;
  5671. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5672. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5673. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5674. return 266667;
  5675. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5676. return 333333;
  5677. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5678. return 444444;
  5679. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5680. return 200000;
  5681. default:
  5682. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5683. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5684. return 133333;
  5685. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5686. return 166667;
  5687. }
  5688. }
  5689. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5690. {
  5691. u16 gcfgc = 0;
  5692. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5693. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5694. return 133333;
  5695. else {
  5696. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5697. case GC_DISPLAY_CLOCK_333_MHZ:
  5698. return 333333;
  5699. default:
  5700. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5701. return 190000;
  5702. }
  5703. }
  5704. }
  5705. static int i865_get_display_clock_speed(struct drm_device *dev)
  5706. {
  5707. return 266667;
  5708. }
  5709. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5710. {
  5711. u16 hpllcc = 0;
  5712. /*
  5713. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5714. * encoding is different :(
  5715. * FIXME is this the right way to detect 852GM/852GMV?
  5716. */
  5717. if (dev->pdev->revision == 0x1)
  5718. return 133333;
  5719. pci_bus_read_config_word(dev->pdev->bus,
  5720. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5721. /* Assume that the hardware is in the high speed state. This
  5722. * should be the default.
  5723. */
  5724. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5725. case GC_CLOCK_133_200:
  5726. case GC_CLOCK_133_200_2:
  5727. case GC_CLOCK_100_200:
  5728. return 200000;
  5729. case GC_CLOCK_166_250:
  5730. return 250000;
  5731. case GC_CLOCK_100_133:
  5732. return 133333;
  5733. case GC_CLOCK_133_266:
  5734. case GC_CLOCK_133_266_2:
  5735. case GC_CLOCK_166_266:
  5736. return 266667;
  5737. }
  5738. /* Shouldn't happen */
  5739. return 0;
  5740. }
  5741. static int i830_get_display_clock_speed(struct drm_device *dev)
  5742. {
  5743. return 133333;
  5744. }
  5745. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5746. {
  5747. struct drm_i915_private *dev_priv = dev->dev_private;
  5748. static const unsigned int blb_vco[8] = {
  5749. [0] = 3200000,
  5750. [1] = 4000000,
  5751. [2] = 5333333,
  5752. [3] = 4800000,
  5753. [4] = 6400000,
  5754. };
  5755. static const unsigned int pnv_vco[8] = {
  5756. [0] = 3200000,
  5757. [1] = 4000000,
  5758. [2] = 5333333,
  5759. [3] = 4800000,
  5760. [4] = 2666667,
  5761. };
  5762. static const unsigned int cl_vco[8] = {
  5763. [0] = 3200000,
  5764. [1] = 4000000,
  5765. [2] = 5333333,
  5766. [3] = 6400000,
  5767. [4] = 3333333,
  5768. [5] = 3566667,
  5769. [6] = 4266667,
  5770. };
  5771. static const unsigned int elk_vco[8] = {
  5772. [0] = 3200000,
  5773. [1] = 4000000,
  5774. [2] = 5333333,
  5775. [3] = 4800000,
  5776. };
  5777. static const unsigned int ctg_vco[8] = {
  5778. [0] = 3200000,
  5779. [1] = 4000000,
  5780. [2] = 5333333,
  5781. [3] = 6400000,
  5782. [4] = 2666667,
  5783. [5] = 4266667,
  5784. };
  5785. const unsigned int *vco_table;
  5786. unsigned int vco;
  5787. uint8_t tmp = 0;
  5788. /* FIXME other chipsets? */
  5789. if (IS_GM45(dev))
  5790. vco_table = ctg_vco;
  5791. else if (IS_G4X(dev))
  5792. vco_table = elk_vco;
  5793. else if (IS_CRESTLINE(dev))
  5794. vco_table = cl_vco;
  5795. else if (IS_PINEVIEW(dev))
  5796. vco_table = pnv_vco;
  5797. else if (IS_G33(dev))
  5798. vco_table = blb_vco;
  5799. else
  5800. return 0;
  5801. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5802. vco = vco_table[tmp & 0x7];
  5803. if (vco == 0)
  5804. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5805. else
  5806. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5807. return vco;
  5808. }
  5809. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5810. {
  5811. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5812. uint16_t tmp = 0;
  5813. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5814. cdclk_sel = (tmp >> 12) & 0x1;
  5815. switch (vco) {
  5816. case 2666667:
  5817. case 4000000:
  5818. case 5333333:
  5819. return cdclk_sel ? 333333 : 222222;
  5820. case 3200000:
  5821. return cdclk_sel ? 320000 : 228571;
  5822. default:
  5823. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5824. return 222222;
  5825. }
  5826. }
  5827. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5828. {
  5829. static const uint8_t div_3200[] = { 16, 10, 8 };
  5830. static const uint8_t div_4000[] = { 20, 12, 10 };
  5831. static const uint8_t div_5333[] = { 24, 16, 14 };
  5832. const uint8_t *div_table;
  5833. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5834. uint16_t tmp = 0;
  5835. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5836. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5837. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5838. goto fail;
  5839. switch (vco) {
  5840. case 3200000:
  5841. div_table = div_3200;
  5842. break;
  5843. case 4000000:
  5844. div_table = div_4000;
  5845. break;
  5846. case 5333333:
  5847. div_table = div_5333;
  5848. break;
  5849. default:
  5850. goto fail;
  5851. }
  5852. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5853. fail:
  5854. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5855. return 200000;
  5856. }
  5857. static int g33_get_display_clock_speed(struct drm_device *dev)
  5858. {
  5859. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5860. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5861. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5862. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5863. const uint8_t *div_table;
  5864. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5865. uint16_t tmp = 0;
  5866. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5867. cdclk_sel = (tmp >> 4) & 0x7;
  5868. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5869. goto fail;
  5870. switch (vco) {
  5871. case 3200000:
  5872. div_table = div_3200;
  5873. break;
  5874. case 4000000:
  5875. div_table = div_4000;
  5876. break;
  5877. case 4800000:
  5878. div_table = div_4800;
  5879. break;
  5880. case 5333333:
  5881. div_table = div_5333;
  5882. break;
  5883. default:
  5884. goto fail;
  5885. }
  5886. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5887. fail:
  5888. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5889. return 190476;
  5890. }
  5891. static void
  5892. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5893. {
  5894. while (*num > DATA_LINK_M_N_MASK ||
  5895. *den > DATA_LINK_M_N_MASK) {
  5896. *num >>= 1;
  5897. *den >>= 1;
  5898. }
  5899. }
  5900. static void compute_m_n(unsigned int m, unsigned int n,
  5901. uint32_t *ret_m, uint32_t *ret_n)
  5902. {
  5903. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5904. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5905. intel_reduce_m_n_ratio(ret_m, ret_n);
  5906. }
  5907. void
  5908. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5909. int pixel_clock, int link_clock,
  5910. struct intel_link_m_n *m_n)
  5911. {
  5912. m_n->tu = 64;
  5913. compute_m_n(bits_per_pixel * pixel_clock,
  5914. link_clock * nlanes * 8,
  5915. &m_n->gmch_m, &m_n->gmch_n);
  5916. compute_m_n(pixel_clock, link_clock,
  5917. &m_n->link_m, &m_n->link_n);
  5918. }
  5919. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5920. {
  5921. if (i915.panel_use_ssc >= 0)
  5922. return i915.panel_use_ssc != 0;
  5923. return dev_priv->vbt.lvds_use_ssc
  5924. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5925. }
  5926. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5927. int num_connectors)
  5928. {
  5929. struct drm_device *dev = crtc_state->base.crtc->dev;
  5930. struct drm_i915_private *dev_priv = dev->dev_private;
  5931. int refclk;
  5932. WARN_ON(!crtc_state->base.state);
  5933. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5934. refclk = 100000;
  5935. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5936. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5937. refclk = dev_priv->vbt.lvds_ssc_freq;
  5938. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5939. } else if (!IS_GEN2(dev)) {
  5940. refclk = 96000;
  5941. } else {
  5942. refclk = 48000;
  5943. }
  5944. return refclk;
  5945. }
  5946. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5947. {
  5948. return (1 << dpll->n) << 16 | dpll->m2;
  5949. }
  5950. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5951. {
  5952. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5953. }
  5954. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5955. struct intel_crtc_state *crtc_state,
  5956. intel_clock_t *reduced_clock)
  5957. {
  5958. struct drm_device *dev = crtc->base.dev;
  5959. u32 fp, fp2 = 0;
  5960. if (IS_PINEVIEW(dev)) {
  5961. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5962. if (reduced_clock)
  5963. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5964. } else {
  5965. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5966. if (reduced_clock)
  5967. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5968. }
  5969. crtc_state->dpll_hw_state.fp0 = fp;
  5970. crtc->lowfreq_avail = false;
  5971. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5972. reduced_clock) {
  5973. crtc_state->dpll_hw_state.fp1 = fp2;
  5974. crtc->lowfreq_avail = true;
  5975. } else {
  5976. crtc_state->dpll_hw_state.fp1 = fp;
  5977. }
  5978. }
  5979. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5980. pipe)
  5981. {
  5982. u32 reg_val;
  5983. /*
  5984. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5985. * and set it to a reasonable value instead.
  5986. */
  5987. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5988. reg_val &= 0xffffff00;
  5989. reg_val |= 0x00000030;
  5990. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5991. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5992. reg_val &= 0x8cffffff;
  5993. reg_val = 0x8c000000;
  5994. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5995. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5996. reg_val &= 0xffffff00;
  5997. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5998. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5999. reg_val &= 0x00ffffff;
  6000. reg_val |= 0xb0000000;
  6001. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6002. }
  6003. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6004. struct intel_link_m_n *m_n)
  6005. {
  6006. struct drm_device *dev = crtc->base.dev;
  6007. struct drm_i915_private *dev_priv = dev->dev_private;
  6008. int pipe = crtc->pipe;
  6009. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6010. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6011. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6012. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6013. }
  6014. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6015. struct intel_link_m_n *m_n,
  6016. struct intel_link_m_n *m2_n2)
  6017. {
  6018. struct drm_device *dev = crtc->base.dev;
  6019. struct drm_i915_private *dev_priv = dev->dev_private;
  6020. int pipe = crtc->pipe;
  6021. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6022. if (INTEL_INFO(dev)->gen >= 5) {
  6023. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6024. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6025. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6026. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6027. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6028. * for gen < 8) and if DRRS is supported (to make sure the
  6029. * registers are not unnecessarily accessed).
  6030. */
  6031. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6032. crtc->config->has_drrs) {
  6033. I915_WRITE(PIPE_DATA_M2(transcoder),
  6034. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6035. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6036. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6037. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6038. }
  6039. } else {
  6040. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6041. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6042. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6043. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6044. }
  6045. }
  6046. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6047. {
  6048. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6049. if (m_n == M1_N1) {
  6050. dp_m_n = &crtc->config->dp_m_n;
  6051. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6052. } else if (m_n == M2_N2) {
  6053. /*
  6054. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6055. * needs to be programmed into M1_N1.
  6056. */
  6057. dp_m_n = &crtc->config->dp_m2_n2;
  6058. } else {
  6059. DRM_ERROR("Unsupported divider value\n");
  6060. return;
  6061. }
  6062. if (crtc->config->has_pch_encoder)
  6063. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6064. else
  6065. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6066. }
  6067. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6068. struct intel_crtc_state *pipe_config)
  6069. {
  6070. u32 dpll, dpll_md;
  6071. /*
  6072. * Enable DPIO clock input. We should never disable the reference
  6073. * clock for pipe B, since VGA hotplug / manual detection depends
  6074. * on it.
  6075. */
  6076. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6077. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6078. /* We should never disable this, set it here for state tracking */
  6079. if (crtc->pipe == PIPE_B)
  6080. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6081. dpll |= DPLL_VCO_ENABLE;
  6082. pipe_config->dpll_hw_state.dpll = dpll;
  6083. dpll_md = (pipe_config->pixel_multiplier - 1)
  6084. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6085. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6086. }
  6087. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6088. const struct intel_crtc_state *pipe_config)
  6089. {
  6090. struct drm_device *dev = crtc->base.dev;
  6091. struct drm_i915_private *dev_priv = dev->dev_private;
  6092. int pipe = crtc->pipe;
  6093. u32 mdiv;
  6094. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6095. u32 coreclk, reg_val;
  6096. mutex_lock(&dev_priv->sb_lock);
  6097. bestn = pipe_config->dpll.n;
  6098. bestm1 = pipe_config->dpll.m1;
  6099. bestm2 = pipe_config->dpll.m2;
  6100. bestp1 = pipe_config->dpll.p1;
  6101. bestp2 = pipe_config->dpll.p2;
  6102. /* See eDP HDMI DPIO driver vbios notes doc */
  6103. /* PLL B needs special handling */
  6104. if (pipe == PIPE_B)
  6105. vlv_pllb_recal_opamp(dev_priv, pipe);
  6106. /* Set up Tx target for periodic Rcomp update */
  6107. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6108. /* Disable target IRef on PLL */
  6109. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6110. reg_val &= 0x00ffffff;
  6111. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6112. /* Disable fast lock */
  6113. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6114. /* Set idtafcrecal before PLL is enabled */
  6115. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6116. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6117. mdiv |= ((bestn << DPIO_N_SHIFT));
  6118. mdiv |= (1 << DPIO_K_SHIFT);
  6119. /*
  6120. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6121. * but we don't support that).
  6122. * Note: don't use the DAC post divider as it seems unstable.
  6123. */
  6124. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6125. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6126. mdiv |= DPIO_ENABLE_CALIBRATION;
  6127. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6128. /* Set HBR and RBR LPF coefficients */
  6129. if (pipe_config->port_clock == 162000 ||
  6130. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6131. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6132. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6133. 0x009f0003);
  6134. else
  6135. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6136. 0x00d0000f);
  6137. if (pipe_config->has_dp_encoder) {
  6138. /* Use SSC source */
  6139. if (pipe == PIPE_A)
  6140. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6141. 0x0df40000);
  6142. else
  6143. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6144. 0x0df70000);
  6145. } else { /* HDMI or VGA */
  6146. /* Use bend source */
  6147. if (pipe == PIPE_A)
  6148. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6149. 0x0df70000);
  6150. else
  6151. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6152. 0x0df40000);
  6153. }
  6154. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6155. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6156. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6157. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6158. coreclk |= 0x01000000;
  6159. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6160. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6161. mutex_unlock(&dev_priv->sb_lock);
  6162. }
  6163. static void chv_compute_dpll(struct intel_crtc *crtc,
  6164. struct intel_crtc_state *pipe_config)
  6165. {
  6166. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6167. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6168. DPLL_VCO_ENABLE;
  6169. if (crtc->pipe != PIPE_A)
  6170. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6171. pipe_config->dpll_hw_state.dpll_md =
  6172. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6173. }
  6174. static void chv_prepare_pll(struct intel_crtc *crtc,
  6175. const struct intel_crtc_state *pipe_config)
  6176. {
  6177. struct drm_device *dev = crtc->base.dev;
  6178. struct drm_i915_private *dev_priv = dev->dev_private;
  6179. int pipe = crtc->pipe;
  6180. int dpll_reg = DPLL(crtc->pipe);
  6181. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6182. u32 loopfilter, tribuf_calcntr;
  6183. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6184. u32 dpio_val;
  6185. int vco;
  6186. bestn = pipe_config->dpll.n;
  6187. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6188. bestm1 = pipe_config->dpll.m1;
  6189. bestm2 = pipe_config->dpll.m2 >> 22;
  6190. bestp1 = pipe_config->dpll.p1;
  6191. bestp2 = pipe_config->dpll.p2;
  6192. vco = pipe_config->dpll.vco;
  6193. dpio_val = 0;
  6194. loopfilter = 0;
  6195. /*
  6196. * Enable Refclk and SSC
  6197. */
  6198. I915_WRITE(dpll_reg,
  6199. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6200. mutex_lock(&dev_priv->sb_lock);
  6201. /* p1 and p2 divider */
  6202. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6203. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6204. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6205. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6206. 1 << DPIO_CHV_K_DIV_SHIFT);
  6207. /* Feedback post-divider - m2 */
  6208. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6209. /* Feedback refclk divider - n and m1 */
  6210. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6211. DPIO_CHV_M1_DIV_BY_2 |
  6212. 1 << DPIO_CHV_N_DIV_SHIFT);
  6213. /* M2 fraction division */
  6214. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6215. /* M2 fraction division enable */
  6216. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6217. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6218. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6219. if (bestm2_frac)
  6220. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6221. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6222. /* Program digital lock detect threshold */
  6223. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6224. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6225. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6226. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6227. if (!bestm2_frac)
  6228. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6229. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6230. /* Loop filter */
  6231. if (vco == 5400000) {
  6232. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6233. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6234. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6235. tribuf_calcntr = 0x9;
  6236. } else if (vco <= 6200000) {
  6237. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6238. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6239. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6240. tribuf_calcntr = 0x9;
  6241. } else if (vco <= 6480000) {
  6242. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6243. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6244. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6245. tribuf_calcntr = 0x8;
  6246. } else {
  6247. /* Not supported. Apply the same limits as in the max case */
  6248. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6249. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6250. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6251. tribuf_calcntr = 0;
  6252. }
  6253. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6254. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6255. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6256. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6257. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6258. /* AFC Recal */
  6259. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6260. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6261. DPIO_AFC_RECAL);
  6262. mutex_unlock(&dev_priv->sb_lock);
  6263. }
  6264. /**
  6265. * vlv_force_pll_on - forcibly enable just the PLL
  6266. * @dev_priv: i915 private structure
  6267. * @pipe: pipe PLL to enable
  6268. * @dpll: PLL configuration
  6269. *
  6270. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6271. * in cases where we need the PLL enabled even when @pipe is not going to
  6272. * be enabled.
  6273. */
  6274. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6275. const struct dpll *dpll)
  6276. {
  6277. struct intel_crtc *crtc =
  6278. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6279. struct intel_crtc_state pipe_config = {
  6280. .base.crtc = &crtc->base,
  6281. .pixel_multiplier = 1,
  6282. .dpll = *dpll,
  6283. };
  6284. if (IS_CHERRYVIEW(dev)) {
  6285. chv_compute_dpll(crtc, &pipe_config);
  6286. chv_prepare_pll(crtc, &pipe_config);
  6287. chv_enable_pll(crtc, &pipe_config);
  6288. } else {
  6289. vlv_compute_dpll(crtc, &pipe_config);
  6290. vlv_prepare_pll(crtc, &pipe_config);
  6291. vlv_enable_pll(crtc, &pipe_config);
  6292. }
  6293. }
  6294. /**
  6295. * vlv_force_pll_off - forcibly disable just the PLL
  6296. * @dev_priv: i915 private structure
  6297. * @pipe: pipe PLL to disable
  6298. *
  6299. * Disable the PLL for @pipe. To be used in cases where we need
  6300. * the PLL enabled even when @pipe is not going to be enabled.
  6301. */
  6302. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6303. {
  6304. if (IS_CHERRYVIEW(dev))
  6305. chv_disable_pll(to_i915(dev), pipe);
  6306. else
  6307. vlv_disable_pll(to_i915(dev), pipe);
  6308. }
  6309. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6310. struct intel_crtc_state *crtc_state,
  6311. intel_clock_t *reduced_clock,
  6312. int num_connectors)
  6313. {
  6314. struct drm_device *dev = crtc->base.dev;
  6315. struct drm_i915_private *dev_priv = dev->dev_private;
  6316. u32 dpll;
  6317. bool is_sdvo;
  6318. struct dpll *clock = &crtc_state->dpll;
  6319. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6320. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6321. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6322. dpll = DPLL_VGA_MODE_DIS;
  6323. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6324. dpll |= DPLLB_MODE_LVDS;
  6325. else
  6326. dpll |= DPLLB_MODE_DAC_SERIAL;
  6327. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6328. dpll |= (crtc_state->pixel_multiplier - 1)
  6329. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6330. }
  6331. if (is_sdvo)
  6332. dpll |= DPLL_SDVO_HIGH_SPEED;
  6333. if (crtc_state->has_dp_encoder)
  6334. dpll |= DPLL_SDVO_HIGH_SPEED;
  6335. /* compute bitmask from p1 value */
  6336. if (IS_PINEVIEW(dev))
  6337. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6338. else {
  6339. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6340. if (IS_G4X(dev) && reduced_clock)
  6341. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6342. }
  6343. switch (clock->p2) {
  6344. case 5:
  6345. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6346. break;
  6347. case 7:
  6348. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6349. break;
  6350. case 10:
  6351. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6352. break;
  6353. case 14:
  6354. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6355. break;
  6356. }
  6357. if (INTEL_INFO(dev)->gen >= 4)
  6358. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6359. if (crtc_state->sdvo_tv_clock)
  6360. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6361. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6362. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6363. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6364. else
  6365. dpll |= PLL_REF_INPUT_DREFCLK;
  6366. dpll |= DPLL_VCO_ENABLE;
  6367. crtc_state->dpll_hw_state.dpll = dpll;
  6368. if (INTEL_INFO(dev)->gen >= 4) {
  6369. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6370. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6371. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6372. }
  6373. }
  6374. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6375. struct intel_crtc_state *crtc_state,
  6376. intel_clock_t *reduced_clock,
  6377. int num_connectors)
  6378. {
  6379. struct drm_device *dev = crtc->base.dev;
  6380. struct drm_i915_private *dev_priv = dev->dev_private;
  6381. u32 dpll;
  6382. struct dpll *clock = &crtc_state->dpll;
  6383. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6384. dpll = DPLL_VGA_MODE_DIS;
  6385. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6386. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6387. } else {
  6388. if (clock->p1 == 2)
  6389. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6390. else
  6391. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6392. if (clock->p2 == 4)
  6393. dpll |= PLL_P2_DIVIDE_BY_4;
  6394. }
  6395. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6396. dpll |= DPLL_DVO_2X_MODE;
  6397. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6398. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6399. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6400. else
  6401. dpll |= PLL_REF_INPUT_DREFCLK;
  6402. dpll |= DPLL_VCO_ENABLE;
  6403. crtc_state->dpll_hw_state.dpll = dpll;
  6404. }
  6405. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6406. {
  6407. struct drm_device *dev = intel_crtc->base.dev;
  6408. struct drm_i915_private *dev_priv = dev->dev_private;
  6409. enum pipe pipe = intel_crtc->pipe;
  6410. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6411. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6412. uint32_t crtc_vtotal, crtc_vblank_end;
  6413. int vsyncshift = 0;
  6414. /* We need to be careful not to changed the adjusted mode, for otherwise
  6415. * the hw state checker will get angry at the mismatch. */
  6416. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6417. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6418. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6419. /* the chip adds 2 halflines automatically */
  6420. crtc_vtotal -= 1;
  6421. crtc_vblank_end -= 1;
  6422. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6423. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6424. else
  6425. vsyncshift = adjusted_mode->crtc_hsync_start -
  6426. adjusted_mode->crtc_htotal / 2;
  6427. if (vsyncshift < 0)
  6428. vsyncshift += adjusted_mode->crtc_htotal;
  6429. }
  6430. if (INTEL_INFO(dev)->gen > 3)
  6431. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6432. I915_WRITE(HTOTAL(cpu_transcoder),
  6433. (adjusted_mode->crtc_hdisplay - 1) |
  6434. ((adjusted_mode->crtc_htotal - 1) << 16));
  6435. I915_WRITE(HBLANK(cpu_transcoder),
  6436. (adjusted_mode->crtc_hblank_start - 1) |
  6437. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6438. I915_WRITE(HSYNC(cpu_transcoder),
  6439. (adjusted_mode->crtc_hsync_start - 1) |
  6440. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6441. I915_WRITE(VTOTAL(cpu_transcoder),
  6442. (adjusted_mode->crtc_vdisplay - 1) |
  6443. ((crtc_vtotal - 1) << 16));
  6444. I915_WRITE(VBLANK(cpu_transcoder),
  6445. (adjusted_mode->crtc_vblank_start - 1) |
  6446. ((crtc_vblank_end - 1) << 16));
  6447. I915_WRITE(VSYNC(cpu_transcoder),
  6448. (adjusted_mode->crtc_vsync_start - 1) |
  6449. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6450. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6451. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6452. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6453. * bits. */
  6454. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6455. (pipe == PIPE_B || pipe == PIPE_C))
  6456. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6457. /* pipesrc controls the size that is scaled from, which should
  6458. * always be the user's requested size.
  6459. */
  6460. I915_WRITE(PIPESRC(pipe),
  6461. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6462. (intel_crtc->config->pipe_src_h - 1));
  6463. }
  6464. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6465. struct intel_crtc_state *pipe_config)
  6466. {
  6467. struct drm_device *dev = crtc->base.dev;
  6468. struct drm_i915_private *dev_priv = dev->dev_private;
  6469. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6470. uint32_t tmp;
  6471. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6472. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6473. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6474. tmp = I915_READ(HBLANK(cpu_transcoder));
  6475. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6476. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6477. tmp = I915_READ(HSYNC(cpu_transcoder));
  6478. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6479. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6480. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6481. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6482. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6483. tmp = I915_READ(VBLANK(cpu_transcoder));
  6484. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6485. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6486. tmp = I915_READ(VSYNC(cpu_transcoder));
  6487. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6488. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6489. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6490. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6491. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6492. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6493. }
  6494. tmp = I915_READ(PIPESRC(crtc->pipe));
  6495. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6496. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6497. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6498. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6499. }
  6500. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6501. struct intel_crtc_state *pipe_config)
  6502. {
  6503. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6504. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6505. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6506. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6507. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6508. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6509. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6510. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6511. mode->flags = pipe_config->base.adjusted_mode.flags;
  6512. mode->type = DRM_MODE_TYPE_DRIVER;
  6513. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6514. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6515. mode->hsync = drm_mode_hsync(mode);
  6516. mode->vrefresh = drm_mode_vrefresh(mode);
  6517. drm_mode_set_name(mode);
  6518. }
  6519. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6520. {
  6521. struct drm_device *dev = intel_crtc->base.dev;
  6522. struct drm_i915_private *dev_priv = dev->dev_private;
  6523. uint32_t pipeconf;
  6524. pipeconf = 0;
  6525. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6526. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6527. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6528. if (intel_crtc->config->double_wide)
  6529. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6530. /* only g4x and later have fancy bpc/dither controls */
  6531. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6532. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6533. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6534. pipeconf |= PIPECONF_DITHER_EN |
  6535. PIPECONF_DITHER_TYPE_SP;
  6536. switch (intel_crtc->config->pipe_bpp) {
  6537. case 18:
  6538. pipeconf |= PIPECONF_6BPC;
  6539. break;
  6540. case 24:
  6541. pipeconf |= PIPECONF_8BPC;
  6542. break;
  6543. case 30:
  6544. pipeconf |= PIPECONF_10BPC;
  6545. break;
  6546. default:
  6547. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6548. BUG();
  6549. }
  6550. }
  6551. if (HAS_PIPE_CXSR(dev)) {
  6552. if (intel_crtc->lowfreq_avail) {
  6553. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6554. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6555. } else {
  6556. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6557. }
  6558. }
  6559. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6560. if (INTEL_INFO(dev)->gen < 4 ||
  6561. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6562. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6563. else
  6564. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6565. } else
  6566. pipeconf |= PIPECONF_PROGRESSIVE;
  6567. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6568. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6569. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6570. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6571. }
  6572. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6573. struct intel_crtc_state *crtc_state)
  6574. {
  6575. struct drm_device *dev = crtc->base.dev;
  6576. struct drm_i915_private *dev_priv = dev->dev_private;
  6577. int refclk, num_connectors = 0;
  6578. intel_clock_t clock;
  6579. bool ok;
  6580. bool is_dsi = false;
  6581. struct intel_encoder *encoder;
  6582. const intel_limit_t *limit;
  6583. struct drm_atomic_state *state = crtc_state->base.state;
  6584. struct drm_connector *connector;
  6585. struct drm_connector_state *connector_state;
  6586. int i;
  6587. memset(&crtc_state->dpll_hw_state, 0,
  6588. sizeof(crtc_state->dpll_hw_state));
  6589. for_each_connector_in_state(state, connector, connector_state, i) {
  6590. if (connector_state->crtc != &crtc->base)
  6591. continue;
  6592. encoder = to_intel_encoder(connector_state->best_encoder);
  6593. switch (encoder->type) {
  6594. case INTEL_OUTPUT_DSI:
  6595. is_dsi = true;
  6596. break;
  6597. default:
  6598. break;
  6599. }
  6600. num_connectors++;
  6601. }
  6602. if (is_dsi)
  6603. return 0;
  6604. if (!crtc_state->clock_set) {
  6605. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6606. /*
  6607. * Returns a set of divisors for the desired target clock with
  6608. * the given refclk, or FALSE. The returned values represent
  6609. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6610. * 2) / p1 / p2.
  6611. */
  6612. limit = intel_limit(crtc_state, refclk);
  6613. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6614. crtc_state->port_clock,
  6615. refclk, NULL, &clock);
  6616. if (!ok) {
  6617. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6618. return -EINVAL;
  6619. }
  6620. /* Compat-code for transition, will disappear. */
  6621. crtc_state->dpll.n = clock.n;
  6622. crtc_state->dpll.m1 = clock.m1;
  6623. crtc_state->dpll.m2 = clock.m2;
  6624. crtc_state->dpll.p1 = clock.p1;
  6625. crtc_state->dpll.p2 = clock.p2;
  6626. }
  6627. if (IS_GEN2(dev)) {
  6628. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6629. num_connectors);
  6630. } else if (IS_CHERRYVIEW(dev)) {
  6631. chv_compute_dpll(crtc, crtc_state);
  6632. } else if (IS_VALLEYVIEW(dev)) {
  6633. vlv_compute_dpll(crtc, crtc_state);
  6634. } else {
  6635. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6636. num_connectors);
  6637. }
  6638. return 0;
  6639. }
  6640. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6641. struct intel_crtc_state *pipe_config)
  6642. {
  6643. struct drm_device *dev = crtc->base.dev;
  6644. struct drm_i915_private *dev_priv = dev->dev_private;
  6645. uint32_t tmp;
  6646. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6647. return;
  6648. tmp = I915_READ(PFIT_CONTROL);
  6649. if (!(tmp & PFIT_ENABLE))
  6650. return;
  6651. /* Check whether the pfit is attached to our pipe. */
  6652. if (INTEL_INFO(dev)->gen < 4) {
  6653. if (crtc->pipe != PIPE_B)
  6654. return;
  6655. } else {
  6656. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6657. return;
  6658. }
  6659. pipe_config->gmch_pfit.control = tmp;
  6660. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6661. if (INTEL_INFO(dev)->gen < 5)
  6662. pipe_config->gmch_pfit.lvds_border_bits =
  6663. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6664. }
  6665. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6666. struct intel_crtc_state *pipe_config)
  6667. {
  6668. struct drm_device *dev = crtc->base.dev;
  6669. struct drm_i915_private *dev_priv = dev->dev_private;
  6670. int pipe = pipe_config->cpu_transcoder;
  6671. intel_clock_t clock;
  6672. u32 mdiv;
  6673. int refclk = 100000;
  6674. /* In case of MIPI DPLL will not even be used */
  6675. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6676. return;
  6677. mutex_lock(&dev_priv->sb_lock);
  6678. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6679. mutex_unlock(&dev_priv->sb_lock);
  6680. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6681. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6682. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6683. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6684. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6685. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6686. }
  6687. static void
  6688. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6689. struct intel_initial_plane_config *plane_config)
  6690. {
  6691. struct drm_device *dev = crtc->base.dev;
  6692. struct drm_i915_private *dev_priv = dev->dev_private;
  6693. u32 val, base, offset;
  6694. int pipe = crtc->pipe, plane = crtc->plane;
  6695. int fourcc, pixel_format;
  6696. unsigned int aligned_height;
  6697. struct drm_framebuffer *fb;
  6698. struct intel_framebuffer *intel_fb;
  6699. val = I915_READ(DSPCNTR(plane));
  6700. if (!(val & DISPLAY_PLANE_ENABLE))
  6701. return;
  6702. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6703. if (!intel_fb) {
  6704. DRM_DEBUG_KMS("failed to alloc fb\n");
  6705. return;
  6706. }
  6707. fb = &intel_fb->base;
  6708. if (INTEL_INFO(dev)->gen >= 4) {
  6709. if (val & DISPPLANE_TILED) {
  6710. plane_config->tiling = I915_TILING_X;
  6711. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6712. }
  6713. }
  6714. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6715. fourcc = i9xx_format_to_fourcc(pixel_format);
  6716. fb->pixel_format = fourcc;
  6717. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6718. if (INTEL_INFO(dev)->gen >= 4) {
  6719. if (plane_config->tiling)
  6720. offset = I915_READ(DSPTILEOFF(plane));
  6721. else
  6722. offset = I915_READ(DSPLINOFF(plane));
  6723. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6724. } else {
  6725. base = I915_READ(DSPADDR(plane));
  6726. }
  6727. plane_config->base = base;
  6728. val = I915_READ(PIPESRC(pipe));
  6729. fb->width = ((val >> 16) & 0xfff) + 1;
  6730. fb->height = ((val >> 0) & 0xfff) + 1;
  6731. val = I915_READ(DSPSTRIDE(pipe));
  6732. fb->pitches[0] = val & 0xffffffc0;
  6733. aligned_height = intel_fb_align_height(dev, fb->height,
  6734. fb->pixel_format,
  6735. fb->modifier[0]);
  6736. plane_config->size = fb->pitches[0] * aligned_height;
  6737. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6738. pipe_name(pipe), plane, fb->width, fb->height,
  6739. fb->bits_per_pixel, base, fb->pitches[0],
  6740. plane_config->size);
  6741. plane_config->fb = intel_fb;
  6742. }
  6743. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6744. struct intel_crtc_state *pipe_config)
  6745. {
  6746. struct drm_device *dev = crtc->base.dev;
  6747. struct drm_i915_private *dev_priv = dev->dev_private;
  6748. int pipe = pipe_config->cpu_transcoder;
  6749. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6750. intel_clock_t clock;
  6751. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6752. int refclk = 100000;
  6753. mutex_lock(&dev_priv->sb_lock);
  6754. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6755. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6756. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6757. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6758. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6759. mutex_unlock(&dev_priv->sb_lock);
  6760. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6761. clock.m2 = (pll_dw0 & 0xff) << 22;
  6762. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6763. clock.m2 |= pll_dw2 & 0x3fffff;
  6764. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6765. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6766. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6767. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6768. }
  6769. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6770. struct intel_crtc_state *pipe_config)
  6771. {
  6772. struct drm_device *dev = crtc->base.dev;
  6773. struct drm_i915_private *dev_priv = dev->dev_private;
  6774. uint32_t tmp;
  6775. if (!intel_display_power_is_enabled(dev_priv,
  6776. POWER_DOMAIN_PIPE(crtc->pipe)))
  6777. return false;
  6778. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6779. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6780. tmp = I915_READ(PIPECONF(crtc->pipe));
  6781. if (!(tmp & PIPECONF_ENABLE))
  6782. return false;
  6783. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6784. switch (tmp & PIPECONF_BPC_MASK) {
  6785. case PIPECONF_6BPC:
  6786. pipe_config->pipe_bpp = 18;
  6787. break;
  6788. case PIPECONF_8BPC:
  6789. pipe_config->pipe_bpp = 24;
  6790. break;
  6791. case PIPECONF_10BPC:
  6792. pipe_config->pipe_bpp = 30;
  6793. break;
  6794. default:
  6795. break;
  6796. }
  6797. }
  6798. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6799. pipe_config->limited_color_range = true;
  6800. if (INTEL_INFO(dev)->gen < 4)
  6801. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6802. intel_get_pipe_timings(crtc, pipe_config);
  6803. i9xx_get_pfit_config(crtc, pipe_config);
  6804. if (INTEL_INFO(dev)->gen >= 4) {
  6805. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6806. pipe_config->pixel_multiplier =
  6807. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6808. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6809. pipe_config->dpll_hw_state.dpll_md = tmp;
  6810. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6811. tmp = I915_READ(DPLL(crtc->pipe));
  6812. pipe_config->pixel_multiplier =
  6813. ((tmp & SDVO_MULTIPLIER_MASK)
  6814. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6815. } else {
  6816. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6817. * port and will be fixed up in the encoder->get_config
  6818. * function. */
  6819. pipe_config->pixel_multiplier = 1;
  6820. }
  6821. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6822. if (!IS_VALLEYVIEW(dev)) {
  6823. /*
  6824. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6825. * on 830. Filter it out here so that we don't
  6826. * report errors due to that.
  6827. */
  6828. if (IS_I830(dev))
  6829. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6830. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6831. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6832. } else {
  6833. /* Mask out read-only status bits. */
  6834. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6835. DPLL_PORTC_READY_MASK |
  6836. DPLL_PORTB_READY_MASK);
  6837. }
  6838. if (IS_CHERRYVIEW(dev))
  6839. chv_crtc_clock_get(crtc, pipe_config);
  6840. else if (IS_VALLEYVIEW(dev))
  6841. vlv_crtc_clock_get(crtc, pipe_config);
  6842. else
  6843. i9xx_crtc_clock_get(crtc, pipe_config);
  6844. /*
  6845. * Normally the dotclock is filled in by the encoder .get_config()
  6846. * but in case the pipe is enabled w/o any ports we need a sane
  6847. * default.
  6848. */
  6849. pipe_config->base.adjusted_mode.crtc_clock =
  6850. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6851. return true;
  6852. }
  6853. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6854. {
  6855. struct drm_i915_private *dev_priv = dev->dev_private;
  6856. struct intel_encoder *encoder;
  6857. u32 val, final;
  6858. bool has_lvds = false;
  6859. bool has_cpu_edp = false;
  6860. bool has_panel = false;
  6861. bool has_ck505 = false;
  6862. bool can_ssc = false;
  6863. /* We need to take the global config into account */
  6864. for_each_intel_encoder(dev, encoder) {
  6865. switch (encoder->type) {
  6866. case INTEL_OUTPUT_LVDS:
  6867. has_panel = true;
  6868. has_lvds = true;
  6869. break;
  6870. case INTEL_OUTPUT_EDP:
  6871. has_panel = true;
  6872. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6873. has_cpu_edp = true;
  6874. break;
  6875. default:
  6876. break;
  6877. }
  6878. }
  6879. if (HAS_PCH_IBX(dev)) {
  6880. has_ck505 = dev_priv->vbt.display_clock_mode;
  6881. can_ssc = has_ck505;
  6882. } else {
  6883. has_ck505 = false;
  6884. can_ssc = true;
  6885. }
  6886. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6887. has_panel, has_lvds, has_ck505);
  6888. /* Ironlake: try to setup display ref clock before DPLL
  6889. * enabling. This is only under driver's control after
  6890. * PCH B stepping, previous chipset stepping should be
  6891. * ignoring this setting.
  6892. */
  6893. val = I915_READ(PCH_DREF_CONTROL);
  6894. /* As we must carefully and slowly disable/enable each source in turn,
  6895. * compute the final state we want first and check if we need to
  6896. * make any changes at all.
  6897. */
  6898. final = val;
  6899. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6900. if (has_ck505)
  6901. final |= DREF_NONSPREAD_CK505_ENABLE;
  6902. else
  6903. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6904. final &= ~DREF_SSC_SOURCE_MASK;
  6905. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6906. final &= ~DREF_SSC1_ENABLE;
  6907. if (has_panel) {
  6908. final |= DREF_SSC_SOURCE_ENABLE;
  6909. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6910. final |= DREF_SSC1_ENABLE;
  6911. if (has_cpu_edp) {
  6912. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6913. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6914. else
  6915. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6916. } else
  6917. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6918. } else {
  6919. final |= DREF_SSC_SOURCE_DISABLE;
  6920. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6921. }
  6922. if (final == val)
  6923. return;
  6924. /* Always enable nonspread source */
  6925. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6926. if (has_ck505)
  6927. val |= DREF_NONSPREAD_CK505_ENABLE;
  6928. else
  6929. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6930. if (has_panel) {
  6931. val &= ~DREF_SSC_SOURCE_MASK;
  6932. val |= DREF_SSC_SOURCE_ENABLE;
  6933. /* SSC must be turned on before enabling the CPU output */
  6934. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6935. DRM_DEBUG_KMS("Using SSC on panel\n");
  6936. val |= DREF_SSC1_ENABLE;
  6937. } else
  6938. val &= ~DREF_SSC1_ENABLE;
  6939. /* Get SSC going before enabling the outputs */
  6940. I915_WRITE(PCH_DREF_CONTROL, val);
  6941. POSTING_READ(PCH_DREF_CONTROL);
  6942. udelay(200);
  6943. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6944. /* Enable CPU source on CPU attached eDP */
  6945. if (has_cpu_edp) {
  6946. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6947. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6948. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6949. } else
  6950. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6951. } else
  6952. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6953. I915_WRITE(PCH_DREF_CONTROL, val);
  6954. POSTING_READ(PCH_DREF_CONTROL);
  6955. udelay(200);
  6956. } else {
  6957. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6958. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6959. /* Turn off CPU output */
  6960. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6961. I915_WRITE(PCH_DREF_CONTROL, val);
  6962. POSTING_READ(PCH_DREF_CONTROL);
  6963. udelay(200);
  6964. /* Turn off the SSC source */
  6965. val &= ~DREF_SSC_SOURCE_MASK;
  6966. val |= DREF_SSC_SOURCE_DISABLE;
  6967. /* Turn off SSC1 */
  6968. val &= ~DREF_SSC1_ENABLE;
  6969. I915_WRITE(PCH_DREF_CONTROL, val);
  6970. POSTING_READ(PCH_DREF_CONTROL);
  6971. udelay(200);
  6972. }
  6973. BUG_ON(val != final);
  6974. }
  6975. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6976. {
  6977. uint32_t tmp;
  6978. tmp = I915_READ(SOUTH_CHICKEN2);
  6979. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6980. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6981. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6982. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6983. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6984. tmp = I915_READ(SOUTH_CHICKEN2);
  6985. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6986. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6987. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6988. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6989. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6990. }
  6991. /* WaMPhyProgramming:hsw */
  6992. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6993. {
  6994. uint32_t tmp;
  6995. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6996. tmp &= ~(0xFF << 24);
  6997. tmp |= (0x12 << 24);
  6998. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6999. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7000. tmp |= (1 << 11);
  7001. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7002. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7003. tmp |= (1 << 11);
  7004. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7005. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7006. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7007. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7008. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7009. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7010. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7011. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7012. tmp &= ~(7 << 13);
  7013. tmp |= (5 << 13);
  7014. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7015. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7016. tmp &= ~(7 << 13);
  7017. tmp |= (5 << 13);
  7018. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7019. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7020. tmp &= ~0xFF;
  7021. tmp |= 0x1C;
  7022. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7023. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7024. tmp &= ~0xFF;
  7025. tmp |= 0x1C;
  7026. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7027. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7028. tmp &= ~(0xFF << 16);
  7029. tmp |= (0x1C << 16);
  7030. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7031. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7032. tmp &= ~(0xFF << 16);
  7033. tmp |= (0x1C << 16);
  7034. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7035. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7036. tmp |= (1 << 27);
  7037. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7038. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7039. tmp |= (1 << 27);
  7040. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7041. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7042. tmp &= ~(0xF << 28);
  7043. tmp |= (4 << 28);
  7044. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7045. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7046. tmp &= ~(0xF << 28);
  7047. tmp |= (4 << 28);
  7048. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7049. }
  7050. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7051. * Programming" based on the parameters passed:
  7052. * - Sequence to enable CLKOUT_DP
  7053. * - Sequence to enable CLKOUT_DP without spread
  7054. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7055. */
  7056. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7057. bool with_fdi)
  7058. {
  7059. struct drm_i915_private *dev_priv = dev->dev_private;
  7060. uint32_t reg, tmp;
  7061. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7062. with_spread = true;
  7063. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7064. with_fdi = false;
  7065. mutex_lock(&dev_priv->sb_lock);
  7066. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7067. tmp &= ~SBI_SSCCTL_DISABLE;
  7068. tmp |= SBI_SSCCTL_PATHALT;
  7069. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7070. udelay(24);
  7071. if (with_spread) {
  7072. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7073. tmp &= ~SBI_SSCCTL_PATHALT;
  7074. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7075. if (with_fdi) {
  7076. lpt_reset_fdi_mphy(dev_priv);
  7077. lpt_program_fdi_mphy(dev_priv);
  7078. }
  7079. }
  7080. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7081. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7082. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7083. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7084. mutex_unlock(&dev_priv->sb_lock);
  7085. }
  7086. /* Sequence to disable CLKOUT_DP */
  7087. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7088. {
  7089. struct drm_i915_private *dev_priv = dev->dev_private;
  7090. uint32_t reg, tmp;
  7091. mutex_lock(&dev_priv->sb_lock);
  7092. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7093. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7094. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7095. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7096. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7097. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7098. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7099. tmp |= SBI_SSCCTL_PATHALT;
  7100. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7101. udelay(32);
  7102. }
  7103. tmp |= SBI_SSCCTL_DISABLE;
  7104. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7105. }
  7106. mutex_unlock(&dev_priv->sb_lock);
  7107. }
  7108. static void lpt_init_pch_refclk(struct drm_device *dev)
  7109. {
  7110. struct intel_encoder *encoder;
  7111. bool has_vga = false;
  7112. for_each_intel_encoder(dev, encoder) {
  7113. switch (encoder->type) {
  7114. case INTEL_OUTPUT_ANALOG:
  7115. has_vga = true;
  7116. break;
  7117. default:
  7118. break;
  7119. }
  7120. }
  7121. if (has_vga)
  7122. lpt_enable_clkout_dp(dev, true, true);
  7123. else
  7124. lpt_disable_clkout_dp(dev);
  7125. }
  7126. /*
  7127. * Initialize reference clocks when the driver loads
  7128. */
  7129. void intel_init_pch_refclk(struct drm_device *dev)
  7130. {
  7131. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7132. ironlake_init_pch_refclk(dev);
  7133. else if (HAS_PCH_LPT(dev))
  7134. lpt_init_pch_refclk(dev);
  7135. }
  7136. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7137. {
  7138. struct drm_device *dev = crtc_state->base.crtc->dev;
  7139. struct drm_i915_private *dev_priv = dev->dev_private;
  7140. struct drm_atomic_state *state = crtc_state->base.state;
  7141. struct drm_connector *connector;
  7142. struct drm_connector_state *connector_state;
  7143. struct intel_encoder *encoder;
  7144. int num_connectors = 0, i;
  7145. bool is_lvds = false;
  7146. for_each_connector_in_state(state, connector, connector_state, i) {
  7147. if (connector_state->crtc != crtc_state->base.crtc)
  7148. continue;
  7149. encoder = to_intel_encoder(connector_state->best_encoder);
  7150. switch (encoder->type) {
  7151. case INTEL_OUTPUT_LVDS:
  7152. is_lvds = true;
  7153. break;
  7154. default:
  7155. break;
  7156. }
  7157. num_connectors++;
  7158. }
  7159. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7160. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7161. dev_priv->vbt.lvds_ssc_freq);
  7162. return dev_priv->vbt.lvds_ssc_freq;
  7163. }
  7164. return 120000;
  7165. }
  7166. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7167. {
  7168. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7169. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7170. int pipe = intel_crtc->pipe;
  7171. uint32_t val;
  7172. val = 0;
  7173. switch (intel_crtc->config->pipe_bpp) {
  7174. case 18:
  7175. val |= PIPECONF_6BPC;
  7176. break;
  7177. case 24:
  7178. val |= PIPECONF_8BPC;
  7179. break;
  7180. case 30:
  7181. val |= PIPECONF_10BPC;
  7182. break;
  7183. case 36:
  7184. val |= PIPECONF_12BPC;
  7185. break;
  7186. default:
  7187. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7188. BUG();
  7189. }
  7190. if (intel_crtc->config->dither)
  7191. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7192. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7193. val |= PIPECONF_INTERLACED_ILK;
  7194. else
  7195. val |= PIPECONF_PROGRESSIVE;
  7196. if (intel_crtc->config->limited_color_range)
  7197. val |= PIPECONF_COLOR_RANGE_SELECT;
  7198. I915_WRITE(PIPECONF(pipe), val);
  7199. POSTING_READ(PIPECONF(pipe));
  7200. }
  7201. /*
  7202. * Set up the pipe CSC unit.
  7203. *
  7204. * Currently only full range RGB to limited range RGB conversion
  7205. * is supported, but eventually this should handle various
  7206. * RGB<->YCbCr scenarios as well.
  7207. */
  7208. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7209. {
  7210. struct drm_device *dev = crtc->dev;
  7211. struct drm_i915_private *dev_priv = dev->dev_private;
  7212. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7213. int pipe = intel_crtc->pipe;
  7214. uint16_t coeff = 0x7800; /* 1.0 */
  7215. /*
  7216. * TODO: Check what kind of values actually come out of the pipe
  7217. * with these coeff/postoff values and adjust to get the best
  7218. * accuracy. Perhaps we even need to take the bpc value into
  7219. * consideration.
  7220. */
  7221. if (intel_crtc->config->limited_color_range)
  7222. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7223. /*
  7224. * GY/GU and RY/RU should be the other way around according
  7225. * to BSpec, but reality doesn't agree. Just set them up in
  7226. * a way that results in the correct picture.
  7227. */
  7228. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7229. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7230. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7231. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7232. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7233. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7234. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7235. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7236. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7237. if (INTEL_INFO(dev)->gen > 6) {
  7238. uint16_t postoff = 0;
  7239. if (intel_crtc->config->limited_color_range)
  7240. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7241. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7242. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7243. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7244. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7245. } else {
  7246. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7247. if (intel_crtc->config->limited_color_range)
  7248. mode |= CSC_BLACK_SCREEN_OFFSET;
  7249. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7250. }
  7251. }
  7252. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7253. {
  7254. struct drm_device *dev = crtc->dev;
  7255. struct drm_i915_private *dev_priv = dev->dev_private;
  7256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7257. enum pipe pipe = intel_crtc->pipe;
  7258. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7259. uint32_t val;
  7260. val = 0;
  7261. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7262. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7263. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7264. val |= PIPECONF_INTERLACED_ILK;
  7265. else
  7266. val |= PIPECONF_PROGRESSIVE;
  7267. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7268. POSTING_READ(PIPECONF(cpu_transcoder));
  7269. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7270. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7271. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7272. val = 0;
  7273. switch (intel_crtc->config->pipe_bpp) {
  7274. case 18:
  7275. val |= PIPEMISC_DITHER_6_BPC;
  7276. break;
  7277. case 24:
  7278. val |= PIPEMISC_DITHER_8_BPC;
  7279. break;
  7280. case 30:
  7281. val |= PIPEMISC_DITHER_10_BPC;
  7282. break;
  7283. case 36:
  7284. val |= PIPEMISC_DITHER_12_BPC;
  7285. break;
  7286. default:
  7287. /* Case prevented by pipe_config_set_bpp. */
  7288. BUG();
  7289. }
  7290. if (intel_crtc->config->dither)
  7291. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7292. I915_WRITE(PIPEMISC(pipe), val);
  7293. }
  7294. }
  7295. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7296. struct intel_crtc_state *crtc_state,
  7297. intel_clock_t *clock,
  7298. bool *has_reduced_clock,
  7299. intel_clock_t *reduced_clock)
  7300. {
  7301. struct drm_device *dev = crtc->dev;
  7302. struct drm_i915_private *dev_priv = dev->dev_private;
  7303. int refclk;
  7304. const intel_limit_t *limit;
  7305. bool ret;
  7306. refclk = ironlake_get_refclk(crtc_state);
  7307. /*
  7308. * Returns a set of divisors for the desired target clock with the given
  7309. * refclk, or FALSE. The returned values represent the clock equation:
  7310. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7311. */
  7312. limit = intel_limit(crtc_state, refclk);
  7313. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7314. crtc_state->port_clock,
  7315. refclk, NULL, clock);
  7316. if (!ret)
  7317. return false;
  7318. return true;
  7319. }
  7320. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7321. {
  7322. /*
  7323. * Account for spread spectrum to avoid
  7324. * oversubscribing the link. Max center spread
  7325. * is 2.5%; use 5% for safety's sake.
  7326. */
  7327. u32 bps = target_clock * bpp * 21 / 20;
  7328. return DIV_ROUND_UP(bps, link_bw * 8);
  7329. }
  7330. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7331. {
  7332. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7333. }
  7334. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7335. struct intel_crtc_state *crtc_state,
  7336. u32 *fp,
  7337. intel_clock_t *reduced_clock, u32 *fp2)
  7338. {
  7339. struct drm_crtc *crtc = &intel_crtc->base;
  7340. struct drm_device *dev = crtc->dev;
  7341. struct drm_i915_private *dev_priv = dev->dev_private;
  7342. struct drm_atomic_state *state = crtc_state->base.state;
  7343. struct drm_connector *connector;
  7344. struct drm_connector_state *connector_state;
  7345. struct intel_encoder *encoder;
  7346. uint32_t dpll;
  7347. int factor, num_connectors = 0, i;
  7348. bool is_lvds = false, is_sdvo = false;
  7349. for_each_connector_in_state(state, connector, connector_state, i) {
  7350. if (connector_state->crtc != crtc_state->base.crtc)
  7351. continue;
  7352. encoder = to_intel_encoder(connector_state->best_encoder);
  7353. switch (encoder->type) {
  7354. case INTEL_OUTPUT_LVDS:
  7355. is_lvds = true;
  7356. break;
  7357. case INTEL_OUTPUT_SDVO:
  7358. case INTEL_OUTPUT_HDMI:
  7359. is_sdvo = true;
  7360. break;
  7361. default:
  7362. break;
  7363. }
  7364. num_connectors++;
  7365. }
  7366. /* Enable autotuning of the PLL clock (if permissible) */
  7367. factor = 21;
  7368. if (is_lvds) {
  7369. if ((intel_panel_use_ssc(dev_priv) &&
  7370. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7371. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7372. factor = 25;
  7373. } else if (crtc_state->sdvo_tv_clock)
  7374. factor = 20;
  7375. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7376. *fp |= FP_CB_TUNE;
  7377. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7378. *fp2 |= FP_CB_TUNE;
  7379. dpll = 0;
  7380. if (is_lvds)
  7381. dpll |= DPLLB_MODE_LVDS;
  7382. else
  7383. dpll |= DPLLB_MODE_DAC_SERIAL;
  7384. dpll |= (crtc_state->pixel_multiplier - 1)
  7385. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7386. if (is_sdvo)
  7387. dpll |= DPLL_SDVO_HIGH_SPEED;
  7388. if (crtc_state->has_dp_encoder)
  7389. dpll |= DPLL_SDVO_HIGH_SPEED;
  7390. /* compute bitmask from p1 value */
  7391. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7392. /* also FPA1 */
  7393. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7394. switch (crtc_state->dpll.p2) {
  7395. case 5:
  7396. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7397. break;
  7398. case 7:
  7399. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7400. break;
  7401. case 10:
  7402. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7403. break;
  7404. case 14:
  7405. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7406. break;
  7407. }
  7408. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7409. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7410. else
  7411. dpll |= PLL_REF_INPUT_DREFCLK;
  7412. return dpll | DPLL_VCO_ENABLE;
  7413. }
  7414. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7415. struct intel_crtc_state *crtc_state)
  7416. {
  7417. struct drm_device *dev = crtc->base.dev;
  7418. intel_clock_t clock, reduced_clock;
  7419. u32 dpll = 0, fp = 0, fp2 = 0;
  7420. bool ok, has_reduced_clock = false;
  7421. bool is_lvds = false;
  7422. struct intel_shared_dpll *pll;
  7423. memset(&crtc_state->dpll_hw_state, 0,
  7424. sizeof(crtc_state->dpll_hw_state));
  7425. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7426. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7427. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7428. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7429. &has_reduced_clock, &reduced_clock);
  7430. if (!ok && !crtc_state->clock_set) {
  7431. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7432. return -EINVAL;
  7433. }
  7434. /* Compat-code for transition, will disappear. */
  7435. if (!crtc_state->clock_set) {
  7436. crtc_state->dpll.n = clock.n;
  7437. crtc_state->dpll.m1 = clock.m1;
  7438. crtc_state->dpll.m2 = clock.m2;
  7439. crtc_state->dpll.p1 = clock.p1;
  7440. crtc_state->dpll.p2 = clock.p2;
  7441. }
  7442. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7443. if (crtc_state->has_pch_encoder) {
  7444. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7445. if (has_reduced_clock)
  7446. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7447. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7448. &fp, &reduced_clock,
  7449. has_reduced_clock ? &fp2 : NULL);
  7450. crtc_state->dpll_hw_state.dpll = dpll;
  7451. crtc_state->dpll_hw_state.fp0 = fp;
  7452. if (has_reduced_clock)
  7453. crtc_state->dpll_hw_state.fp1 = fp2;
  7454. else
  7455. crtc_state->dpll_hw_state.fp1 = fp;
  7456. pll = intel_get_shared_dpll(crtc, crtc_state);
  7457. if (pll == NULL) {
  7458. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7459. pipe_name(crtc->pipe));
  7460. return -EINVAL;
  7461. }
  7462. }
  7463. if (is_lvds && has_reduced_clock)
  7464. crtc->lowfreq_avail = true;
  7465. else
  7466. crtc->lowfreq_avail = false;
  7467. return 0;
  7468. }
  7469. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7470. struct intel_link_m_n *m_n)
  7471. {
  7472. struct drm_device *dev = crtc->base.dev;
  7473. struct drm_i915_private *dev_priv = dev->dev_private;
  7474. enum pipe pipe = crtc->pipe;
  7475. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7476. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7477. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7478. & ~TU_SIZE_MASK;
  7479. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7480. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7481. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7482. }
  7483. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7484. enum transcoder transcoder,
  7485. struct intel_link_m_n *m_n,
  7486. struct intel_link_m_n *m2_n2)
  7487. {
  7488. struct drm_device *dev = crtc->base.dev;
  7489. struct drm_i915_private *dev_priv = dev->dev_private;
  7490. enum pipe pipe = crtc->pipe;
  7491. if (INTEL_INFO(dev)->gen >= 5) {
  7492. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7493. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7494. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7495. & ~TU_SIZE_MASK;
  7496. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7497. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7498. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7499. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7500. * gen < 8) and if DRRS is supported (to make sure the
  7501. * registers are not unnecessarily read).
  7502. */
  7503. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7504. crtc->config->has_drrs) {
  7505. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7506. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7507. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7508. & ~TU_SIZE_MASK;
  7509. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7510. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7511. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7512. }
  7513. } else {
  7514. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7515. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7516. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7517. & ~TU_SIZE_MASK;
  7518. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7519. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7520. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7521. }
  7522. }
  7523. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7524. struct intel_crtc_state *pipe_config)
  7525. {
  7526. if (pipe_config->has_pch_encoder)
  7527. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7528. else
  7529. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7530. &pipe_config->dp_m_n,
  7531. &pipe_config->dp_m2_n2);
  7532. }
  7533. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7534. struct intel_crtc_state *pipe_config)
  7535. {
  7536. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7537. &pipe_config->fdi_m_n, NULL);
  7538. }
  7539. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7540. struct intel_crtc_state *pipe_config)
  7541. {
  7542. struct drm_device *dev = crtc->base.dev;
  7543. struct drm_i915_private *dev_priv = dev->dev_private;
  7544. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7545. uint32_t ps_ctrl = 0;
  7546. int id = -1;
  7547. int i;
  7548. /* find scaler attached to this pipe */
  7549. for (i = 0; i < crtc->num_scalers; i++) {
  7550. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7551. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7552. id = i;
  7553. pipe_config->pch_pfit.enabled = true;
  7554. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7555. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7556. break;
  7557. }
  7558. }
  7559. scaler_state->scaler_id = id;
  7560. if (id >= 0) {
  7561. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7562. } else {
  7563. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7564. }
  7565. }
  7566. static void
  7567. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7568. struct intel_initial_plane_config *plane_config)
  7569. {
  7570. struct drm_device *dev = crtc->base.dev;
  7571. struct drm_i915_private *dev_priv = dev->dev_private;
  7572. u32 val, base, offset, stride_mult, tiling;
  7573. int pipe = crtc->pipe;
  7574. int fourcc, pixel_format;
  7575. unsigned int aligned_height;
  7576. struct drm_framebuffer *fb;
  7577. struct intel_framebuffer *intel_fb;
  7578. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7579. if (!intel_fb) {
  7580. DRM_DEBUG_KMS("failed to alloc fb\n");
  7581. return;
  7582. }
  7583. fb = &intel_fb->base;
  7584. val = I915_READ(PLANE_CTL(pipe, 0));
  7585. if (!(val & PLANE_CTL_ENABLE))
  7586. goto error;
  7587. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7588. fourcc = skl_format_to_fourcc(pixel_format,
  7589. val & PLANE_CTL_ORDER_RGBX,
  7590. val & PLANE_CTL_ALPHA_MASK);
  7591. fb->pixel_format = fourcc;
  7592. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7593. tiling = val & PLANE_CTL_TILED_MASK;
  7594. switch (tiling) {
  7595. case PLANE_CTL_TILED_LINEAR:
  7596. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7597. break;
  7598. case PLANE_CTL_TILED_X:
  7599. plane_config->tiling = I915_TILING_X;
  7600. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7601. break;
  7602. case PLANE_CTL_TILED_Y:
  7603. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7604. break;
  7605. case PLANE_CTL_TILED_YF:
  7606. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7607. break;
  7608. default:
  7609. MISSING_CASE(tiling);
  7610. goto error;
  7611. }
  7612. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7613. plane_config->base = base;
  7614. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7615. val = I915_READ(PLANE_SIZE(pipe, 0));
  7616. fb->height = ((val >> 16) & 0xfff) + 1;
  7617. fb->width = ((val >> 0) & 0x1fff) + 1;
  7618. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7619. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7620. fb->pixel_format);
  7621. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7622. aligned_height = intel_fb_align_height(dev, fb->height,
  7623. fb->pixel_format,
  7624. fb->modifier[0]);
  7625. plane_config->size = fb->pitches[0] * aligned_height;
  7626. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7627. pipe_name(pipe), fb->width, fb->height,
  7628. fb->bits_per_pixel, base, fb->pitches[0],
  7629. plane_config->size);
  7630. plane_config->fb = intel_fb;
  7631. return;
  7632. error:
  7633. kfree(fb);
  7634. }
  7635. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7636. struct intel_crtc_state *pipe_config)
  7637. {
  7638. struct drm_device *dev = crtc->base.dev;
  7639. struct drm_i915_private *dev_priv = dev->dev_private;
  7640. uint32_t tmp;
  7641. tmp = I915_READ(PF_CTL(crtc->pipe));
  7642. if (tmp & PF_ENABLE) {
  7643. pipe_config->pch_pfit.enabled = true;
  7644. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7645. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7646. /* We currently do not free assignements of panel fitters on
  7647. * ivb/hsw (since we don't use the higher upscaling modes which
  7648. * differentiates them) so just WARN about this case for now. */
  7649. if (IS_GEN7(dev)) {
  7650. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7651. PF_PIPE_SEL_IVB(crtc->pipe));
  7652. }
  7653. }
  7654. }
  7655. static void
  7656. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7657. struct intel_initial_plane_config *plane_config)
  7658. {
  7659. struct drm_device *dev = crtc->base.dev;
  7660. struct drm_i915_private *dev_priv = dev->dev_private;
  7661. u32 val, base, offset;
  7662. int pipe = crtc->pipe;
  7663. int fourcc, pixel_format;
  7664. unsigned int aligned_height;
  7665. struct drm_framebuffer *fb;
  7666. struct intel_framebuffer *intel_fb;
  7667. val = I915_READ(DSPCNTR(pipe));
  7668. if (!(val & DISPLAY_PLANE_ENABLE))
  7669. return;
  7670. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7671. if (!intel_fb) {
  7672. DRM_DEBUG_KMS("failed to alloc fb\n");
  7673. return;
  7674. }
  7675. fb = &intel_fb->base;
  7676. if (INTEL_INFO(dev)->gen >= 4) {
  7677. if (val & DISPPLANE_TILED) {
  7678. plane_config->tiling = I915_TILING_X;
  7679. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7680. }
  7681. }
  7682. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7683. fourcc = i9xx_format_to_fourcc(pixel_format);
  7684. fb->pixel_format = fourcc;
  7685. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7686. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7687. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7688. offset = I915_READ(DSPOFFSET(pipe));
  7689. } else {
  7690. if (plane_config->tiling)
  7691. offset = I915_READ(DSPTILEOFF(pipe));
  7692. else
  7693. offset = I915_READ(DSPLINOFF(pipe));
  7694. }
  7695. plane_config->base = base;
  7696. val = I915_READ(PIPESRC(pipe));
  7697. fb->width = ((val >> 16) & 0xfff) + 1;
  7698. fb->height = ((val >> 0) & 0xfff) + 1;
  7699. val = I915_READ(DSPSTRIDE(pipe));
  7700. fb->pitches[0] = val & 0xffffffc0;
  7701. aligned_height = intel_fb_align_height(dev, fb->height,
  7702. fb->pixel_format,
  7703. fb->modifier[0]);
  7704. plane_config->size = fb->pitches[0] * aligned_height;
  7705. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7706. pipe_name(pipe), fb->width, fb->height,
  7707. fb->bits_per_pixel, base, fb->pitches[0],
  7708. plane_config->size);
  7709. plane_config->fb = intel_fb;
  7710. }
  7711. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7712. struct intel_crtc_state *pipe_config)
  7713. {
  7714. struct drm_device *dev = crtc->base.dev;
  7715. struct drm_i915_private *dev_priv = dev->dev_private;
  7716. uint32_t tmp;
  7717. if (!intel_display_power_is_enabled(dev_priv,
  7718. POWER_DOMAIN_PIPE(crtc->pipe)))
  7719. return false;
  7720. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7721. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7722. tmp = I915_READ(PIPECONF(crtc->pipe));
  7723. if (!(tmp & PIPECONF_ENABLE))
  7724. return false;
  7725. switch (tmp & PIPECONF_BPC_MASK) {
  7726. case PIPECONF_6BPC:
  7727. pipe_config->pipe_bpp = 18;
  7728. break;
  7729. case PIPECONF_8BPC:
  7730. pipe_config->pipe_bpp = 24;
  7731. break;
  7732. case PIPECONF_10BPC:
  7733. pipe_config->pipe_bpp = 30;
  7734. break;
  7735. case PIPECONF_12BPC:
  7736. pipe_config->pipe_bpp = 36;
  7737. break;
  7738. default:
  7739. break;
  7740. }
  7741. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7742. pipe_config->limited_color_range = true;
  7743. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7744. struct intel_shared_dpll *pll;
  7745. pipe_config->has_pch_encoder = true;
  7746. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7747. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7748. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7749. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7750. if (HAS_PCH_IBX(dev_priv->dev)) {
  7751. pipe_config->shared_dpll =
  7752. (enum intel_dpll_id) crtc->pipe;
  7753. } else {
  7754. tmp = I915_READ(PCH_DPLL_SEL);
  7755. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7756. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7757. else
  7758. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7759. }
  7760. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7761. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7762. &pipe_config->dpll_hw_state));
  7763. tmp = pipe_config->dpll_hw_state.dpll;
  7764. pipe_config->pixel_multiplier =
  7765. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7766. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7767. ironlake_pch_clock_get(crtc, pipe_config);
  7768. } else {
  7769. pipe_config->pixel_multiplier = 1;
  7770. }
  7771. intel_get_pipe_timings(crtc, pipe_config);
  7772. ironlake_get_pfit_config(crtc, pipe_config);
  7773. return true;
  7774. }
  7775. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7776. {
  7777. struct drm_device *dev = dev_priv->dev;
  7778. struct intel_crtc *crtc;
  7779. for_each_intel_crtc(dev, crtc)
  7780. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7781. pipe_name(crtc->pipe));
  7782. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7783. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7784. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7785. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7786. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7787. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7788. "CPU PWM1 enabled\n");
  7789. if (IS_HASWELL(dev))
  7790. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7791. "CPU PWM2 enabled\n");
  7792. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7793. "PCH PWM1 enabled\n");
  7794. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7795. "Utility pin enabled\n");
  7796. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7797. /*
  7798. * In theory we can still leave IRQs enabled, as long as only the HPD
  7799. * interrupts remain enabled. We used to check for that, but since it's
  7800. * gen-specific and since we only disable LCPLL after we fully disable
  7801. * the interrupts, the check below should be enough.
  7802. */
  7803. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7804. }
  7805. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7806. {
  7807. struct drm_device *dev = dev_priv->dev;
  7808. if (IS_HASWELL(dev))
  7809. return I915_READ(D_COMP_HSW);
  7810. else
  7811. return I915_READ(D_COMP_BDW);
  7812. }
  7813. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7814. {
  7815. struct drm_device *dev = dev_priv->dev;
  7816. if (IS_HASWELL(dev)) {
  7817. mutex_lock(&dev_priv->rps.hw_lock);
  7818. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7819. val))
  7820. DRM_ERROR("Failed to write to D_COMP\n");
  7821. mutex_unlock(&dev_priv->rps.hw_lock);
  7822. } else {
  7823. I915_WRITE(D_COMP_BDW, val);
  7824. POSTING_READ(D_COMP_BDW);
  7825. }
  7826. }
  7827. /*
  7828. * This function implements pieces of two sequences from BSpec:
  7829. * - Sequence for display software to disable LCPLL
  7830. * - Sequence for display software to allow package C8+
  7831. * The steps implemented here are just the steps that actually touch the LCPLL
  7832. * register. Callers should take care of disabling all the display engine
  7833. * functions, doing the mode unset, fixing interrupts, etc.
  7834. */
  7835. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7836. bool switch_to_fclk, bool allow_power_down)
  7837. {
  7838. uint32_t val;
  7839. assert_can_disable_lcpll(dev_priv);
  7840. val = I915_READ(LCPLL_CTL);
  7841. if (switch_to_fclk) {
  7842. val |= LCPLL_CD_SOURCE_FCLK;
  7843. I915_WRITE(LCPLL_CTL, val);
  7844. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7845. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7846. DRM_ERROR("Switching to FCLK failed\n");
  7847. val = I915_READ(LCPLL_CTL);
  7848. }
  7849. val |= LCPLL_PLL_DISABLE;
  7850. I915_WRITE(LCPLL_CTL, val);
  7851. POSTING_READ(LCPLL_CTL);
  7852. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7853. DRM_ERROR("LCPLL still locked\n");
  7854. val = hsw_read_dcomp(dev_priv);
  7855. val |= D_COMP_COMP_DISABLE;
  7856. hsw_write_dcomp(dev_priv, val);
  7857. ndelay(100);
  7858. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7859. 1))
  7860. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7861. if (allow_power_down) {
  7862. val = I915_READ(LCPLL_CTL);
  7863. val |= LCPLL_POWER_DOWN_ALLOW;
  7864. I915_WRITE(LCPLL_CTL, val);
  7865. POSTING_READ(LCPLL_CTL);
  7866. }
  7867. }
  7868. /*
  7869. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7870. * source.
  7871. */
  7872. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7873. {
  7874. uint32_t val;
  7875. val = I915_READ(LCPLL_CTL);
  7876. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7877. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7878. return;
  7879. /*
  7880. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7881. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7882. */
  7883. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7884. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7885. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7886. I915_WRITE(LCPLL_CTL, val);
  7887. POSTING_READ(LCPLL_CTL);
  7888. }
  7889. val = hsw_read_dcomp(dev_priv);
  7890. val |= D_COMP_COMP_FORCE;
  7891. val &= ~D_COMP_COMP_DISABLE;
  7892. hsw_write_dcomp(dev_priv, val);
  7893. val = I915_READ(LCPLL_CTL);
  7894. val &= ~LCPLL_PLL_DISABLE;
  7895. I915_WRITE(LCPLL_CTL, val);
  7896. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7897. DRM_ERROR("LCPLL not locked yet\n");
  7898. if (val & LCPLL_CD_SOURCE_FCLK) {
  7899. val = I915_READ(LCPLL_CTL);
  7900. val &= ~LCPLL_CD_SOURCE_FCLK;
  7901. I915_WRITE(LCPLL_CTL, val);
  7902. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7903. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7904. DRM_ERROR("Switching back to LCPLL failed\n");
  7905. }
  7906. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7907. intel_update_cdclk(dev_priv->dev);
  7908. }
  7909. /*
  7910. * Package states C8 and deeper are really deep PC states that can only be
  7911. * reached when all the devices on the system allow it, so even if the graphics
  7912. * device allows PC8+, it doesn't mean the system will actually get to these
  7913. * states. Our driver only allows PC8+ when going into runtime PM.
  7914. *
  7915. * The requirements for PC8+ are that all the outputs are disabled, the power
  7916. * well is disabled and most interrupts are disabled, and these are also
  7917. * requirements for runtime PM. When these conditions are met, we manually do
  7918. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7919. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7920. * hang the machine.
  7921. *
  7922. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7923. * the state of some registers, so when we come back from PC8+ we need to
  7924. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7925. * need to take care of the registers kept by RC6. Notice that this happens even
  7926. * if we don't put the device in PCI D3 state (which is what currently happens
  7927. * because of the runtime PM support).
  7928. *
  7929. * For more, read "Display Sequences for Package C8" on the hardware
  7930. * documentation.
  7931. */
  7932. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7933. {
  7934. struct drm_device *dev = dev_priv->dev;
  7935. uint32_t val;
  7936. DRM_DEBUG_KMS("Enabling package C8+\n");
  7937. if (HAS_PCH_LPT_LP(dev)) {
  7938. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7939. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7940. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7941. }
  7942. lpt_disable_clkout_dp(dev);
  7943. hsw_disable_lcpll(dev_priv, true, true);
  7944. }
  7945. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7946. {
  7947. struct drm_device *dev = dev_priv->dev;
  7948. uint32_t val;
  7949. DRM_DEBUG_KMS("Disabling package C8+\n");
  7950. hsw_restore_lcpll(dev_priv);
  7951. lpt_init_pch_refclk(dev);
  7952. if (HAS_PCH_LPT_LP(dev)) {
  7953. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7954. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7955. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7956. }
  7957. intel_prepare_ddi(dev);
  7958. }
  7959. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7960. {
  7961. struct drm_device *dev = old_state->dev;
  7962. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7963. broxton_set_cdclk(dev, req_cdclk);
  7964. }
  7965. /* compute the max rate for new configuration */
  7966. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7967. {
  7968. struct intel_crtc *intel_crtc;
  7969. struct intel_crtc_state *crtc_state;
  7970. int max_pixel_rate = 0;
  7971. for_each_intel_crtc(state->dev, intel_crtc) {
  7972. int pixel_rate;
  7973. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7974. if (IS_ERR(crtc_state))
  7975. return PTR_ERR(crtc_state);
  7976. if (!crtc_state->base.enable)
  7977. continue;
  7978. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7979. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7980. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7981. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7982. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7983. }
  7984. return max_pixel_rate;
  7985. }
  7986. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7987. {
  7988. struct drm_i915_private *dev_priv = dev->dev_private;
  7989. uint32_t val, data;
  7990. int ret;
  7991. if (WARN((I915_READ(LCPLL_CTL) &
  7992. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7993. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  7994. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  7995. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  7996. "trying to change cdclk frequency with cdclk not enabled\n"))
  7997. return;
  7998. mutex_lock(&dev_priv->rps.hw_lock);
  7999. ret = sandybridge_pcode_write(dev_priv,
  8000. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8001. mutex_unlock(&dev_priv->rps.hw_lock);
  8002. if (ret) {
  8003. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8004. return;
  8005. }
  8006. val = I915_READ(LCPLL_CTL);
  8007. val |= LCPLL_CD_SOURCE_FCLK;
  8008. I915_WRITE(LCPLL_CTL, val);
  8009. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8010. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8011. DRM_ERROR("Switching to FCLK failed\n");
  8012. val = I915_READ(LCPLL_CTL);
  8013. val &= ~LCPLL_CLK_FREQ_MASK;
  8014. switch (cdclk) {
  8015. case 450000:
  8016. val |= LCPLL_CLK_FREQ_450;
  8017. data = 0;
  8018. break;
  8019. case 540000:
  8020. val |= LCPLL_CLK_FREQ_54O_BDW;
  8021. data = 1;
  8022. break;
  8023. case 337500:
  8024. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8025. data = 2;
  8026. break;
  8027. case 675000:
  8028. val |= LCPLL_CLK_FREQ_675_BDW;
  8029. data = 3;
  8030. break;
  8031. default:
  8032. WARN(1, "invalid cdclk frequency\n");
  8033. return;
  8034. }
  8035. I915_WRITE(LCPLL_CTL, val);
  8036. val = I915_READ(LCPLL_CTL);
  8037. val &= ~LCPLL_CD_SOURCE_FCLK;
  8038. I915_WRITE(LCPLL_CTL, val);
  8039. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8040. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8041. DRM_ERROR("Switching back to LCPLL failed\n");
  8042. mutex_lock(&dev_priv->rps.hw_lock);
  8043. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8044. mutex_unlock(&dev_priv->rps.hw_lock);
  8045. intel_update_cdclk(dev);
  8046. WARN(cdclk != dev_priv->cdclk_freq,
  8047. "cdclk requested %d kHz but got %d kHz\n",
  8048. cdclk, dev_priv->cdclk_freq);
  8049. }
  8050. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8051. {
  8052. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8053. int max_pixclk = ilk_max_pixel_rate(state);
  8054. int cdclk;
  8055. /*
  8056. * FIXME should also account for plane ratio
  8057. * once 64bpp pixel formats are supported.
  8058. */
  8059. if (max_pixclk > 540000)
  8060. cdclk = 675000;
  8061. else if (max_pixclk > 450000)
  8062. cdclk = 540000;
  8063. else if (max_pixclk > 337500)
  8064. cdclk = 450000;
  8065. else
  8066. cdclk = 337500;
  8067. /*
  8068. * FIXME move the cdclk caclulation to
  8069. * compute_config() so we can fail gracegully.
  8070. */
  8071. if (cdclk > dev_priv->max_cdclk_freq) {
  8072. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8073. cdclk, dev_priv->max_cdclk_freq);
  8074. cdclk = dev_priv->max_cdclk_freq;
  8075. }
  8076. to_intel_atomic_state(state)->cdclk = cdclk;
  8077. return 0;
  8078. }
  8079. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8080. {
  8081. struct drm_device *dev = old_state->dev;
  8082. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8083. broadwell_set_cdclk(dev, req_cdclk);
  8084. }
  8085. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8086. struct intel_crtc_state *crtc_state)
  8087. {
  8088. if (!intel_ddi_pll_select(crtc, crtc_state))
  8089. return -EINVAL;
  8090. crtc->lowfreq_avail = false;
  8091. return 0;
  8092. }
  8093. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8094. enum port port,
  8095. struct intel_crtc_state *pipe_config)
  8096. {
  8097. switch (port) {
  8098. case PORT_A:
  8099. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8100. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8101. break;
  8102. case PORT_B:
  8103. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8104. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8105. break;
  8106. case PORT_C:
  8107. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8108. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8109. break;
  8110. default:
  8111. DRM_ERROR("Incorrect port type\n");
  8112. }
  8113. }
  8114. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8115. enum port port,
  8116. struct intel_crtc_state *pipe_config)
  8117. {
  8118. u32 temp, dpll_ctl1;
  8119. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8120. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8121. switch (pipe_config->ddi_pll_sel) {
  8122. case SKL_DPLL0:
  8123. /*
  8124. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8125. * of the shared DPLL framework and thus needs to be read out
  8126. * separately
  8127. */
  8128. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8129. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8130. break;
  8131. case SKL_DPLL1:
  8132. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8133. break;
  8134. case SKL_DPLL2:
  8135. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8136. break;
  8137. case SKL_DPLL3:
  8138. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8139. break;
  8140. }
  8141. }
  8142. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8143. enum port port,
  8144. struct intel_crtc_state *pipe_config)
  8145. {
  8146. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8147. switch (pipe_config->ddi_pll_sel) {
  8148. case PORT_CLK_SEL_WRPLL1:
  8149. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8150. break;
  8151. case PORT_CLK_SEL_WRPLL2:
  8152. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8153. break;
  8154. }
  8155. }
  8156. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8157. struct intel_crtc_state *pipe_config)
  8158. {
  8159. struct drm_device *dev = crtc->base.dev;
  8160. struct drm_i915_private *dev_priv = dev->dev_private;
  8161. struct intel_shared_dpll *pll;
  8162. enum port port;
  8163. uint32_t tmp;
  8164. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8165. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8166. if (IS_SKYLAKE(dev))
  8167. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8168. else if (IS_BROXTON(dev))
  8169. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8170. else
  8171. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8172. if (pipe_config->shared_dpll >= 0) {
  8173. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8174. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8175. &pipe_config->dpll_hw_state));
  8176. }
  8177. /*
  8178. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8179. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8180. * the PCH transcoder is on.
  8181. */
  8182. if (INTEL_INFO(dev)->gen < 9 &&
  8183. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8184. pipe_config->has_pch_encoder = true;
  8185. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8186. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8187. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8188. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8189. }
  8190. }
  8191. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8192. struct intel_crtc_state *pipe_config)
  8193. {
  8194. struct drm_device *dev = crtc->base.dev;
  8195. struct drm_i915_private *dev_priv = dev->dev_private;
  8196. enum intel_display_power_domain pfit_domain;
  8197. uint32_t tmp;
  8198. if (!intel_display_power_is_enabled(dev_priv,
  8199. POWER_DOMAIN_PIPE(crtc->pipe)))
  8200. return false;
  8201. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8202. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8203. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8204. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8205. enum pipe trans_edp_pipe;
  8206. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8207. default:
  8208. WARN(1, "unknown pipe linked to edp transcoder\n");
  8209. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8210. case TRANS_DDI_EDP_INPUT_A_ON:
  8211. trans_edp_pipe = PIPE_A;
  8212. break;
  8213. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8214. trans_edp_pipe = PIPE_B;
  8215. break;
  8216. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8217. trans_edp_pipe = PIPE_C;
  8218. break;
  8219. }
  8220. if (trans_edp_pipe == crtc->pipe)
  8221. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8222. }
  8223. if (!intel_display_power_is_enabled(dev_priv,
  8224. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8225. return false;
  8226. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8227. if (!(tmp & PIPECONF_ENABLE))
  8228. return false;
  8229. haswell_get_ddi_port_state(crtc, pipe_config);
  8230. intel_get_pipe_timings(crtc, pipe_config);
  8231. if (INTEL_INFO(dev)->gen >= 9) {
  8232. skl_init_scalers(dev, crtc, pipe_config);
  8233. }
  8234. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8235. if (INTEL_INFO(dev)->gen >= 9) {
  8236. pipe_config->scaler_state.scaler_id = -1;
  8237. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8238. }
  8239. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8240. if (INTEL_INFO(dev)->gen >= 9)
  8241. skylake_get_pfit_config(crtc, pipe_config);
  8242. else
  8243. ironlake_get_pfit_config(crtc, pipe_config);
  8244. }
  8245. if (IS_HASWELL(dev))
  8246. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8247. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8248. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8249. pipe_config->pixel_multiplier =
  8250. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8251. } else {
  8252. pipe_config->pixel_multiplier = 1;
  8253. }
  8254. return true;
  8255. }
  8256. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8257. {
  8258. struct drm_device *dev = crtc->dev;
  8259. struct drm_i915_private *dev_priv = dev->dev_private;
  8260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8261. uint32_t cntl = 0, size = 0;
  8262. if (base) {
  8263. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8264. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8265. unsigned int stride = roundup_pow_of_two(width) * 4;
  8266. switch (stride) {
  8267. default:
  8268. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8269. width, stride);
  8270. stride = 256;
  8271. /* fallthrough */
  8272. case 256:
  8273. case 512:
  8274. case 1024:
  8275. case 2048:
  8276. break;
  8277. }
  8278. cntl |= CURSOR_ENABLE |
  8279. CURSOR_GAMMA_ENABLE |
  8280. CURSOR_FORMAT_ARGB |
  8281. CURSOR_STRIDE(stride);
  8282. size = (height << 12) | width;
  8283. }
  8284. if (intel_crtc->cursor_cntl != 0 &&
  8285. (intel_crtc->cursor_base != base ||
  8286. intel_crtc->cursor_size != size ||
  8287. intel_crtc->cursor_cntl != cntl)) {
  8288. /* On these chipsets we can only modify the base/size/stride
  8289. * whilst the cursor is disabled.
  8290. */
  8291. I915_WRITE(CURCNTR(PIPE_A), 0);
  8292. POSTING_READ(CURCNTR(PIPE_A));
  8293. intel_crtc->cursor_cntl = 0;
  8294. }
  8295. if (intel_crtc->cursor_base != base) {
  8296. I915_WRITE(CURBASE(PIPE_A), base);
  8297. intel_crtc->cursor_base = base;
  8298. }
  8299. if (intel_crtc->cursor_size != size) {
  8300. I915_WRITE(CURSIZE, size);
  8301. intel_crtc->cursor_size = size;
  8302. }
  8303. if (intel_crtc->cursor_cntl != cntl) {
  8304. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8305. POSTING_READ(CURCNTR(PIPE_A));
  8306. intel_crtc->cursor_cntl = cntl;
  8307. }
  8308. }
  8309. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8310. {
  8311. struct drm_device *dev = crtc->dev;
  8312. struct drm_i915_private *dev_priv = dev->dev_private;
  8313. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8314. int pipe = intel_crtc->pipe;
  8315. uint32_t cntl;
  8316. cntl = 0;
  8317. if (base) {
  8318. cntl = MCURSOR_GAMMA_ENABLE;
  8319. switch (intel_crtc->base.cursor->state->crtc_w) {
  8320. case 64:
  8321. cntl |= CURSOR_MODE_64_ARGB_AX;
  8322. break;
  8323. case 128:
  8324. cntl |= CURSOR_MODE_128_ARGB_AX;
  8325. break;
  8326. case 256:
  8327. cntl |= CURSOR_MODE_256_ARGB_AX;
  8328. break;
  8329. default:
  8330. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8331. return;
  8332. }
  8333. cntl |= pipe << 28; /* Connect to correct pipe */
  8334. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8335. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8336. }
  8337. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8338. cntl |= CURSOR_ROTATE_180;
  8339. if (intel_crtc->cursor_cntl != cntl) {
  8340. I915_WRITE(CURCNTR(pipe), cntl);
  8341. POSTING_READ(CURCNTR(pipe));
  8342. intel_crtc->cursor_cntl = cntl;
  8343. }
  8344. /* and commit changes on next vblank */
  8345. I915_WRITE(CURBASE(pipe), base);
  8346. POSTING_READ(CURBASE(pipe));
  8347. intel_crtc->cursor_base = base;
  8348. }
  8349. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8350. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8351. bool on)
  8352. {
  8353. struct drm_device *dev = crtc->dev;
  8354. struct drm_i915_private *dev_priv = dev->dev_private;
  8355. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8356. int pipe = intel_crtc->pipe;
  8357. struct drm_plane_state *cursor_state = crtc->cursor->state;
  8358. int x = cursor_state->crtc_x;
  8359. int y = cursor_state->crtc_y;
  8360. u32 base = 0, pos = 0;
  8361. if (on)
  8362. base = intel_crtc->cursor_addr;
  8363. if (x >= intel_crtc->config->pipe_src_w)
  8364. base = 0;
  8365. if (y >= intel_crtc->config->pipe_src_h)
  8366. base = 0;
  8367. if (x < 0) {
  8368. if (x + cursor_state->crtc_w <= 0)
  8369. base = 0;
  8370. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8371. x = -x;
  8372. }
  8373. pos |= x << CURSOR_X_SHIFT;
  8374. if (y < 0) {
  8375. if (y + cursor_state->crtc_h <= 0)
  8376. base = 0;
  8377. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8378. y = -y;
  8379. }
  8380. pos |= y << CURSOR_Y_SHIFT;
  8381. if (base == 0 && intel_crtc->cursor_base == 0)
  8382. return;
  8383. I915_WRITE(CURPOS(pipe), pos);
  8384. /* ILK+ do this automagically */
  8385. if (HAS_GMCH_DISPLAY(dev) &&
  8386. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8387. base += (cursor_state->crtc_h *
  8388. cursor_state->crtc_w - 1) * 4;
  8389. }
  8390. if (IS_845G(dev) || IS_I865G(dev))
  8391. i845_update_cursor(crtc, base);
  8392. else
  8393. i9xx_update_cursor(crtc, base);
  8394. }
  8395. static bool cursor_size_ok(struct drm_device *dev,
  8396. uint32_t width, uint32_t height)
  8397. {
  8398. if (width == 0 || height == 0)
  8399. return false;
  8400. /*
  8401. * 845g/865g are special in that they are only limited by
  8402. * the width of their cursors, the height is arbitrary up to
  8403. * the precision of the register. Everything else requires
  8404. * square cursors, limited to a few power-of-two sizes.
  8405. */
  8406. if (IS_845G(dev) || IS_I865G(dev)) {
  8407. if ((width & 63) != 0)
  8408. return false;
  8409. if (width > (IS_845G(dev) ? 64 : 512))
  8410. return false;
  8411. if (height > 1023)
  8412. return false;
  8413. } else {
  8414. switch (width | height) {
  8415. case 256:
  8416. case 128:
  8417. if (IS_GEN2(dev))
  8418. return false;
  8419. case 64:
  8420. break;
  8421. default:
  8422. return false;
  8423. }
  8424. }
  8425. return true;
  8426. }
  8427. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8428. u16 *blue, uint32_t start, uint32_t size)
  8429. {
  8430. int end = (start + size > 256) ? 256 : start + size, i;
  8431. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8432. for (i = start; i < end; i++) {
  8433. intel_crtc->lut_r[i] = red[i] >> 8;
  8434. intel_crtc->lut_g[i] = green[i] >> 8;
  8435. intel_crtc->lut_b[i] = blue[i] >> 8;
  8436. }
  8437. intel_crtc_load_lut(crtc);
  8438. }
  8439. /* VESA 640x480x72Hz mode to set on the pipe */
  8440. static struct drm_display_mode load_detect_mode = {
  8441. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8442. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8443. };
  8444. struct drm_framebuffer *
  8445. __intel_framebuffer_create(struct drm_device *dev,
  8446. struct drm_mode_fb_cmd2 *mode_cmd,
  8447. struct drm_i915_gem_object *obj)
  8448. {
  8449. struct intel_framebuffer *intel_fb;
  8450. int ret;
  8451. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8452. if (!intel_fb) {
  8453. drm_gem_object_unreference(&obj->base);
  8454. return ERR_PTR(-ENOMEM);
  8455. }
  8456. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8457. if (ret)
  8458. goto err;
  8459. return &intel_fb->base;
  8460. err:
  8461. drm_gem_object_unreference(&obj->base);
  8462. kfree(intel_fb);
  8463. return ERR_PTR(ret);
  8464. }
  8465. static struct drm_framebuffer *
  8466. intel_framebuffer_create(struct drm_device *dev,
  8467. struct drm_mode_fb_cmd2 *mode_cmd,
  8468. struct drm_i915_gem_object *obj)
  8469. {
  8470. struct drm_framebuffer *fb;
  8471. int ret;
  8472. ret = i915_mutex_lock_interruptible(dev);
  8473. if (ret)
  8474. return ERR_PTR(ret);
  8475. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8476. mutex_unlock(&dev->struct_mutex);
  8477. return fb;
  8478. }
  8479. static u32
  8480. intel_framebuffer_pitch_for_width(int width, int bpp)
  8481. {
  8482. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8483. return ALIGN(pitch, 64);
  8484. }
  8485. static u32
  8486. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8487. {
  8488. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8489. return PAGE_ALIGN(pitch * mode->vdisplay);
  8490. }
  8491. static struct drm_framebuffer *
  8492. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8493. struct drm_display_mode *mode,
  8494. int depth, int bpp)
  8495. {
  8496. struct drm_i915_gem_object *obj;
  8497. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8498. obj = i915_gem_alloc_object(dev,
  8499. intel_framebuffer_size_for_mode(mode, bpp));
  8500. if (obj == NULL)
  8501. return ERR_PTR(-ENOMEM);
  8502. mode_cmd.width = mode->hdisplay;
  8503. mode_cmd.height = mode->vdisplay;
  8504. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8505. bpp);
  8506. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8507. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8508. }
  8509. static struct drm_framebuffer *
  8510. mode_fits_in_fbdev(struct drm_device *dev,
  8511. struct drm_display_mode *mode)
  8512. {
  8513. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8514. struct drm_i915_private *dev_priv = dev->dev_private;
  8515. struct drm_i915_gem_object *obj;
  8516. struct drm_framebuffer *fb;
  8517. if (!dev_priv->fbdev)
  8518. return NULL;
  8519. if (!dev_priv->fbdev->fb)
  8520. return NULL;
  8521. obj = dev_priv->fbdev->fb->obj;
  8522. BUG_ON(!obj);
  8523. fb = &dev_priv->fbdev->fb->base;
  8524. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8525. fb->bits_per_pixel))
  8526. return NULL;
  8527. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8528. return NULL;
  8529. return fb;
  8530. #else
  8531. return NULL;
  8532. #endif
  8533. }
  8534. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8535. struct drm_crtc *crtc,
  8536. struct drm_display_mode *mode,
  8537. struct drm_framebuffer *fb,
  8538. int x, int y)
  8539. {
  8540. struct drm_plane_state *plane_state;
  8541. int hdisplay, vdisplay;
  8542. int ret;
  8543. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8544. if (IS_ERR(plane_state))
  8545. return PTR_ERR(plane_state);
  8546. if (mode)
  8547. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8548. else
  8549. hdisplay = vdisplay = 0;
  8550. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8551. if (ret)
  8552. return ret;
  8553. drm_atomic_set_fb_for_plane(plane_state, fb);
  8554. plane_state->crtc_x = 0;
  8555. plane_state->crtc_y = 0;
  8556. plane_state->crtc_w = hdisplay;
  8557. plane_state->crtc_h = vdisplay;
  8558. plane_state->src_x = x << 16;
  8559. plane_state->src_y = y << 16;
  8560. plane_state->src_w = hdisplay << 16;
  8561. plane_state->src_h = vdisplay << 16;
  8562. return 0;
  8563. }
  8564. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8565. struct drm_display_mode *mode,
  8566. struct intel_load_detect_pipe *old,
  8567. struct drm_modeset_acquire_ctx *ctx)
  8568. {
  8569. struct intel_crtc *intel_crtc;
  8570. struct intel_encoder *intel_encoder =
  8571. intel_attached_encoder(connector);
  8572. struct drm_crtc *possible_crtc;
  8573. struct drm_encoder *encoder = &intel_encoder->base;
  8574. struct drm_crtc *crtc = NULL;
  8575. struct drm_device *dev = encoder->dev;
  8576. struct drm_framebuffer *fb;
  8577. struct drm_mode_config *config = &dev->mode_config;
  8578. struct drm_atomic_state *state = NULL;
  8579. struct drm_connector_state *connector_state;
  8580. struct intel_crtc_state *crtc_state;
  8581. int ret, i = -1;
  8582. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8583. connector->base.id, connector->name,
  8584. encoder->base.id, encoder->name);
  8585. retry:
  8586. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8587. if (ret)
  8588. goto fail;
  8589. /*
  8590. * Algorithm gets a little messy:
  8591. *
  8592. * - if the connector already has an assigned crtc, use it (but make
  8593. * sure it's on first)
  8594. *
  8595. * - try to find the first unused crtc that can drive this connector,
  8596. * and use that if we find one
  8597. */
  8598. /* See if we already have a CRTC for this connector */
  8599. if (encoder->crtc) {
  8600. crtc = encoder->crtc;
  8601. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8602. if (ret)
  8603. goto fail;
  8604. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8605. if (ret)
  8606. goto fail;
  8607. old->dpms_mode = connector->dpms;
  8608. old->load_detect_temp = false;
  8609. /* Make sure the crtc and connector are running */
  8610. if (connector->dpms != DRM_MODE_DPMS_ON)
  8611. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8612. return true;
  8613. }
  8614. /* Find an unused one (if possible) */
  8615. for_each_crtc(dev, possible_crtc) {
  8616. i++;
  8617. if (!(encoder->possible_crtcs & (1 << i)))
  8618. continue;
  8619. if (possible_crtc->state->enable)
  8620. continue;
  8621. crtc = possible_crtc;
  8622. break;
  8623. }
  8624. /*
  8625. * If we didn't find an unused CRTC, don't use any.
  8626. */
  8627. if (!crtc) {
  8628. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8629. goto fail;
  8630. }
  8631. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8632. if (ret)
  8633. goto fail;
  8634. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8635. if (ret)
  8636. goto fail;
  8637. intel_crtc = to_intel_crtc(crtc);
  8638. old->dpms_mode = connector->dpms;
  8639. old->load_detect_temp = true;
  8640. old->release_fb = NULL;
  8641. state = drm_atomic_state_alloc(dev);
  8642. if (!state)
  8643. return false;
  8644. state->acquire_ctx = ctx;
  8645. connector_state = drm_atomic_get_connector_state(state, connector);
  8646. if (IS_ERR(connector_state)) {
  8647. ret = PTR_ERR(connector_state);
  8648. goto fail;
  8649. }
  8650. connector_state->crtc = crtc;
  8651. connector_state->best_encoder = &intel_encoder->base;
  8652. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8653. if (IS_ERR(crtc_state)) {
  8654. ret = PTR_ERR(crtc_state);
  8655. goto fail;
  8656. }
  8657. crtc_state->base.active = crtc_state->base.enable = true;
  8658. if (!mode)
  8659. mode = &load_detect_mode;
  8660. /* We need a framebuffer large enough to accommodate all accesses
  8661. * that the plane may generate whilst we perform load detection.
  8662. * We can not rely on the fbcon either being present (we get called
  8663. * during its initialisation to detect all boot displays, or it may
  8664. * not even exist) or that it is large enough to satisfy the
  8665. * requested mode.
  8666. */
  8667. fb = mode_fits_in_fbdev(dev, mode);
  8668. if (fb == NULL) {
  8669. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8670. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8671. old->release_fb = fb;
  8672. } else
  8673. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8674. if (IS_ERR(fb)) {
  8675. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8676. goto fail;
  8677. }
  8678. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8679. if (ret)
  8680. goto fail;
  8681. drm_mode_copy(&crtc_state->base.mode, mode);
  8682. if (drm_atomic_commit(state)) {
  8683. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8684. if (old->release_fb)
  8685. old->release_fb->funcs->destroy(old->release_fb);
  8686. goto fail;
  8687. }
  8688. crtc->primary->crtc = crtc;
  8689. /* let the connector get through one full cycle before testing */
  8690. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8691. return true;
  8692. fail:
  8693. drm_atomic_state_free(state);
  8694. state = NULL;
  8695. if (ret == -EDEADLK) {
  8696. drm_modeset_backoff(ctx);
  8697. goto retry;
  8698. }
  8699. return false;
  8700. }
  8701. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8702. struct intel_load_detect_pipe *old,
  8703. struct drm_modeset_acquire_ctx *ctx)
  8704. {
  8705. struct drm_device *dev = connector->dev;
  8706. struct intel_encoder *intel_encoder =
  8707. intel_attached_encoder(connector);
  8708. struct drm_encoder *encoder = &intel_encoder->base;
  8709. struct drm_crtc *crtc = encoder->crtc;
  8710. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8711. struct drm_atomic_state *state;
  8712. struct drm_connector_state *connector_state;
  8713. struct intel_crtc_state *crtc_state;
  8714. int ret;
  8715. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8716. connector->base.id, connector->name,
  8717. encoder->base.id, encoder->name);
  8718. if (old->load_detect_temp) {
  8719. state = drm_atomic_state_alloc(dev);
  8720. if (!state)
  8721. goto fail;
  8722. state->acquire_ctx = ctx;
  8723. connector_state = drm_atomic_get_connector_state(state, connector);
  8724. if (IS_ERR(connector_state))
  8725. goto fail;
  8726. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8727. if (IS_ERR(crtc_state))
  8728. goto fail;
  8729. connector_state->best_encoder = NULL;
  8730. connector_state->crtc = NULL;
  8731. crtc_state->base.enable = crtc_state->base.active = false;
  8732. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8733. 0, 0);
  8734. if (ret)
  8735. goto fail;
  8736. ret = drm_atomic_commit(state);
  8737. if (ret)
  8738. goto fail;
  8739. if (old->release_fb) {
  8740. drm_framebuffer_unregister_private(old->release_fb);
  8741. drm_framebuffer_unreference(old->release_fb);
  8742. }
  8743. return;
  8744. }
  8745. /* Switch crtc and encoder back off if necessary */
  8746. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8747. connector->funcs->dpms(connector, old->dpms_mode);
  8748. return;
  8749. fail:
  8750. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8751. drm_atomic_state_free(state);
  8752. }
  8753. static int i9xx_pll_refclk(struct drm_device *dev,
  8754. const struct intel_crtc_state *pipe_config)
  8755. {
  8756. struct drm_i915_private *dev_priv = dev->dev_private;
  8757. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8758. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8759. return dev_priv->vbt.lvds_ssc_freq;
  8760. else if (HAS_PCH_SPLIT(dev))
  8761. return 120000;
  8762. else if (!IS_GEN2(dev))
  8763. return 96000;
  8764. else
  8765. return 48000;
  8766. }
  8767. /* Returns the clock of the currently programmed mode of the given pipe. */
  8768. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8769. struct intel_crtc_state *pipe_config)
  8770. {
  8771. struct drm_device *dev = crtc->base.dev;
  8772. struct drm_i915_private *dev_priv = dev->dev_private;
  8773. int pipe = pipe_config->cpu_transcoder;
  8774. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8775. u32 fp;
  8776. intel_clock_t clock;
  8777. int port_clock;
  8778. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8779. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8780. fp = pipe_config->dpll_hw_state.fp0;
  8781. else
  8782. fp = pipe_config->dpll_hw_state.fp1;
  8783. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8784. if (IS_PINEVIEW(dev)) {
  8785. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8786. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8787. } else {
  8788. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8789. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8790. }
  8791. if (!IS_GEN2(dev)) {
  8792. if (IS_PINEVIEW(dev))
  8793. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8794. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8795. else
  8796. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8797. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8798. switch (dpll & DPLL_MODE_MASK) {
  8799. case DPLLB_MODE_DAC_SERIAL:
  8800. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8801. 5 : 10;
  8802. break;
  8803. case DPLLB_MODE_LVDS:
  8804. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8805. 7 : 14;
  8806. break;
  8807. default:
  8808. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8809. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8810. return;
  8811. }
  8812. if (IS_PINEVIEW(dev))
  8813. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8814. else
  8815. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8816. } else {
  8817. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8818. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8819. if (is_lvds) {
  8820. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8821. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8822. if (lvds & LVDS_CLKB_POWER_UP)
  8823. clock.p2 = 7;
  8824. else
  8825. clock.p2 = 14;
  8826. } else {
  8827. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8828. clock.p1 = 2;
  8829. else {
  8830. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8831. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8832. }
  8833. if (dpll & PLL_P2_DIVIDE_BY_4)
  8834. clock.p2 = 4;
  8835. else
  8836. clock.p2 = 2;
  8837. }
  8838. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8839. }
  8840. /*
  8841. * This value includes pixel_multiplier. We will use
  8842. * port_clock to compute adjusted_mode.crtc_clock in the
  8843. * encoder's get_config() function.
  8844. */
  8845. pipe_config->port_clock = port_clock;
  8846. }
  8847. int intel_dotclock_calculate(int link_freq,
  8848. const struct intel_link_m_n *m_n)
  8849. {
  8850. /*
  8851. * The calculation for the data clock is:
  8852. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8853. * But we want to avoid losing precison if possible, so:
  8854. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8855. *
  8856. * and the link clock is simpler:
  8857. * link_clock = (m * link_clock) / n
  8858. */
  8859. if (!m_n->link_n)
  8860. return 0;
  8861. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8862. }
  8863. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8864. struct intel_crtc_state *pipe_config)
  8865. {
  8866. struct drm_device *dev = crtc->base.dev;
  8867. /* read out port_clock from the DPLL */
  8868. i9xx_crtc_clock_get(crtc, pipe_config);
  8869. /*
  8870. * This value does not include pixel_multiplier.
  8871. * We will check that port_clock and adjusted_mode.crtc_clock
  8872. * agree once we know their relationship in the encoder's
  8873. * get_config() function.
  8874. */
  8875. pipe_config->base.adjusted_mode.crtc_clock =
  8876. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8877. &pipe_config->fdi_m_n);
  8878. }
  8879. /** Returns the currently programmed mode of the given pipe. */
  8880. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8881. struct drm_crtc *crtc)
  8882. {
  8883. struct drm_i915_private *dev_priv = dev->dev_private;
  8884. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8885. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8886. struct drm_display_mode *mode;
  8887. struct intel_crtc_state pipe_config;
  8888. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8889. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8890. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8891. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8892. enum pipe pipe = intel_crtc->pipe;
  8893. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8894. if (!mode)
  8895. return NULL;
  8896. /*
  8897. * Construct a pipe_config sufficient for getting the clock info
  8898. * back out of crtc_clock_get.
  8899. *
  8900. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8901. * to use a real value here instead.
  8902. */
  8903. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8904. pipe_config.pixel_multiplier = 1;
  8905. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8906. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8907. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8908. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8909. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8910. mode->hdisplay = (htot & 0xffff) + 1;
  8911. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8912. mode->hsync_start = (hsync & 0xffff) + 1;
  8913. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8914. mode->vdisplay = (vtot & 0xffff) + 1;
  8915. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8916. mode->vsync_start = (vsync & 0xffff) + 1;
  8917. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8918. drm_mode_set_name(mode);
  8919. return mode;
  8920. }
  8921. void intel_mark_busy(struct drm_device *dev)
  8922. {
  8923. struct drm_i915_private *dev_priv = dev->dev_private;
  8924. if (dev_priv->mm.busy)
  8925. return;
  8926. intel_runtime_pm_get(dev_priv);
  8927. i915_update_gfx_val(dev_priv);
  8928. if (INTEL_INFO(dev)->gen >= 6)
  8929. gen6_rps_busy(dev_priv);
  8930. dev_priv->mm.busy = true;
  8931. }
  8932. void intel_mark_idle(struct drm_device *dev)
  8933. {
  8934. struct drm_i915_private *dev_priv = dev->dev_private;
  8935. if (!dev_priv->mm.busy)
  8936. return;
  8937. dev_priv->mm.busy = false;
  8938. if (INTEL_INFO(dev)->gen >= 6)
  8939. gen6_rps_idle(dev->dev_private);
  8940. intel_runtime_pm_put(dev_priv);
  8941. }
  8942. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8943. {
  8944. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8945. struct drm_device *dev = crtc->dev;
  8946. struct intel_unpin_work *work;
  8947. spin_lock_irq(&dev->event_lock);
  8948. work = intel_crtc->unpin_work;
  8949. intel_crtc->unpin_work = NULL;
  8950. spin_unlock_irq(&dev->event_lock);
  8951. if (work) {
  8952. cancel_work_sync(&work->work);
  8953. kfree(work);
  8954. }
  8955. drm_crtc_cleanup(crtc);
  8956. kfree(intel_crtc);
  8957. }
  8958. static void intel_unpin_work_fn(struct work_struct *__work)
  8959. {
  8960. struct intel_unpin_work *work =
  8961. container_of(__work, struct intel_unpin_work, work);
  8962. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8963. struct drm_device *dev = crtc->base.dev;
  8964. struct drm_plane *primary = crtc->base.primary;
  8965. mutex_lock(&dev->struct_mutex);
  8966. intel_unpin_fb_obj(work->old_fb, primary->state);
  8967. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8968. if (work->flip_queued_req)
  8969. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8970. mutex_unlock(&dev->struct_mutex);
  8971. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8972. drm_framebuffer_unreference(work->old_fb);
  8973. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8974. atomic_dec(&crtc->unpin_work_count);
  8975. kfree(work);
  8976. }
  8977. static void do_intel_finish_page_flip(struct drm_device *dev,
  8978. struct drm_crtc *crtc)
  8979. {
  8980. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8981. struct intel_unpin_work *work;
  8982. unsigned long flags;
  8983. /* Ignore early vblank irqs */
  8984. if (intel_crtc == NULL)
  8985. return;
  8986. /*
  8987. * This is called both by irq handlers and the reset code (to complete
  8988. * lost pageflips) so needs the full irqsave spinlocks.
  8989. */
  8990. spin_lock_irqsave(&dev->event_lock, flags);
  8991. work = intel_crtc->unpin_work;
  8992. /* Ensure we don't miss a work->pending update ... */
  8993. smp_rmb();
  8994. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8995. spin_unlock_irqrestore(&dev->event_lock, flags);
  8996. return;
  8997. }
  8998. page_flip_completed(intel_crtc);
  8999. spin_unlock_irqrestore(&dev->event_lock, flags);
  9000. }
  9001. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9002. {
  9003. struct drm_i915_private *dev_priv = dev->dev_private;
  9004. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9005. do_intel_finish_page_flip(dev, crtc);
  9006. }
  9007. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9008. {
  9009. struct drm_i915_private *dev_priv = dev->dev_private;
  9010. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9011. do_intel_finish_page_flip(dev, crtc);
  9012. }
  9013. /* Is 'a' after or equal to 'b'? */
  9014. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9015. {
  9016. return !((a - b) & 0x80000000);
  9017. }
  9018. static bool page_flip_finished(struct intel_crtc *crtc)
  9019. {
  9020. struct drm_device *dev = crtc->base.dev;
  9021. struct drm_i915_private *dev_priv = dev->dev_private;
  9022. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9023. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9024. return true;
  9025. /*
  9026. * The relevant registers doen't exist on pre-ctg.
  9027. * As the flip done interrupt doesn't trigger for mmio
  9028. * flips on gmch platforms, a flip count check isn't
  9029. * really needed there. But since ctg has the registers,
  9030. * include it in the check anyway.
  9031. */
  9032. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9033. return true;
  9034. /*
  9035. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9036. * used the same base address. In that case the mmio flip might
  9037. * have completed, but the CS hasn't even executed the flip yet.
  9038. *
  9039. * A flip count check isn't enough as the CS might have updated
  9040. * the base address just after start of vblank, but before we
  9041. * managed to process the interrupt. This means we'd complete the
  9042. * CS flip too soon.
  9043. *
  9044. * Combining both checks should get us a good enough result. It may
  9045. * still happen that the CS flip has been executed, but has not
  9046. * yet actually completed. But in case the base address is the same
  9047. * anyway, we don't really care.
  9048. */
  9049. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9050. crtc->unpin_work->gtt_offset &&
  9051. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9052. crtc->unpin_work->flip_count);
  9053. }
  9054. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9055. {
  9056. struct drm_i915_private *dev_priv = dev->dev_private;
  9057. struct intel_crtc *intel_crtc =
  9058. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9059. unsigned long flags;
  9060. /*
  9061. * This is called both by irq handlers and the reset code (to complete
  9062. * lost pageflips) so needs the full irqsave spinlocks.
  9063. *
  9064. * NB: An MMIO update of the plane base pointer will also
  9065. * generate a page-flip completion irq, i.e. every modeset
  9066. * is also accompanied by a spurious intel_prepare_page_flip().
  9067. */
  9068. spin_lock_irqsave(&dev->event_lock, flags);
  9069. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9070. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9071. spin_unlock_irqrestore(&dev->event_lock, flags);
  9072. }
  9073. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9074. {
  9075. /* Ensure that the work item is consistent when activating it ... */
  9076. smp_wmb();
  9077. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9078. /* and that it is marked active as soon as the irq could fire. */
  9079. smp_wmb();
  9080. }
  9081. static int intel_gen2_queue_flip(struct drm_device *dev,
  9082. struct drm_crtc *crtc,
  9083. struct drm_framebuffer *fb,
  9084. struct drm_i915_gem_object *obj,
  9085. struct drm_i915_gem_request *req,
  9086. uint32_t flags)
  9087. {
  9088. struct intel_engine_cs *ring = req->ring;
  9089. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9090. u32 flip_mask;
  9091. int ret;
  9092. ret = intel_ring_begin(req, 6);
  9093. if (ret)
  9094. return ret;
  9095. /* Can't queue multiple flips, so wait for the previous
  9096. * one to finish before executing the next.
  9097. */
  9098. if (intel_crtc->plane)
  9099. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9100. else
  9101. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9102. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9103. intel_ring_emit(ring, MI_NOOP);
  9104. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9105. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9106. intel_ring_emit(ring, fb->pitches[0]);
  9107. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9108. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9109. intel_mark_page_flip_active(intel_crtc);
  9110. return 0;
  9111. }
  9112. static int intel_gen3_queue_flip(struct drm_device *dev,
  9113. struct drm_crtc *crtc,
  9114. struct drm_framebuffer *fb,
  9115. struct drm_i915_gem_object *obj,
  9116. struct drm_i915_gem_request *req,
  9117. uint32_t flags)
  9118. {
  9119. struct intel_engine_cs *ring = req->ring;
  9120. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9121. u32 flip_mask;
  9122. int ret;
  9123. ret = intel_ring_begin(req, 6);
  9124. if (ret)
  9125. return ret;
  9126. if (intel_crtc->plane)
  9127. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9128. else
  9129. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9130. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9131. intel_ring_emit(ring, MI_NOOP);
  9132. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9133. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9134. intel_ring_emit(ring, fb->pitches[0]);
  9135. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9136. intel_ring_emit(ring, MI_NOOP);
  9137. intel_mark_page_flip_active(intel_crtc);
  9138. return 0;
  9139. }
  9140. static int intel_gen4_queue_flip(struct drm_device *dev,
  9141. struct drm_crtc *crtc,
  9142. struct drm_framebuffer *fb,
  9143. struct drm_i915_gem_object *obj,
  9144. struct drm_i915_gem_request *req,
  9145. uint32_t flags)
  9146. {
  9147. struct intel_engine_cs *ring = req->ring;
  9148. struct drm_i915_private *dev_priv = dev->dev_private;
  9149. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9150. uint32_t pf, pipesrc;
  9151. int ret;
  9152. ret = intel_ring_begin(req, 4);
  9153. if (ret)
  9154. return ret;
  9155. /* i965+ uses the linear or tiled offsets from the
  9156. * Display Registers (which do not change across a page-flip)
  9157. * so we need only reprogram the base address.
  9158. */
  9159. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9160. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9161. intel_ring_emit(ring, fb->pitches[0]);
  9162. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9163. obj->tiling_mode);
  9164. /* XXX Enabling the panel-fitter across page-flip is so far
  9165. * untested on non-native modes, so ignore it for now.
  9166. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9167. */
  9168. pf = 0;
  9169. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9170. intel_ring_emit(ring, pf | pipesrc);
  9171. intel_mark_page_flip_active(intel_crtc);
  9172. return 0;
  9173. }
  9174. static int intel_gen6_queue_flip(struct drm_device *dev,
  9175. struct drm_crtc *crtc,
  9176. struct drm_framebuffer *fb,
  9177. struct drm_i915_gem_object *obj,
  9178. struct drm_i915_gem_request *req,
  9179. uint32_t flags)
  9180. {
  9181. struct intel_engine_cs *ring = req->ring;
  9182. struct drm_i915_private *dev_priv = dev->dev_private;
  9183. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9184. uint32_t pf, pipesrc;
  9185. int ret;
  9186. ret = intel_ring_begin(req, 4);
  9187. if (ret)
  9188. return ret;
  9189. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9190. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9191. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9192. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9193. /* Contrary to the suggestions in the documentation,
  9194. * "Enable Panel Fitter" does not seem to be required when page
  9195. * flipping with a non-native mode, and worse causes a normal
  9196. * modeset to fail.
  9197. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9198. */
  9199. pf = 0;
  9200. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9201. intel_ring_emit(ring, pf | pipesrc);
  9202. intel_mark_page_flip_active(intel_crtc);
  9203. return 0;
  9204. }
  9205. static int intel_gen7_queue_flip(struct drm_device *dev,
  9206. struct drm_crtc *crtc,
  9207. struct drm_framebuffer *fb,
  9208. struct drm_i915_gem_object *obj,
  9209. struct drm_i915_gem_request *req,
  9210. uint32_t flags)
  9211. {
  9212. struct intel_engine_cs *ring = req->ring;
  9213. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9214. uint32_t plane_bit = 0;
  9215. int len, ret;
  9216. switch (intel_crtc->plane) {
  9217. case PLANE_A:
  9218. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9219. break;
  9220. case PLANE_B:
  9221. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9222. break;
  9223. case PLANE_C:
  9224. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9225. break;
  9226. default:
  9227. WARN_ONCE(1, "unknown plane in flip command\n");
  9228. return -ENODEV;
  9229. }
  9230. len = 4;
  9231. if (ring->id == RCS) {
  9232. len += 6;
  9233. /*
  9234. * On Gen 8, SRM is now taking an extra dword to accommodate
  9235. * 48bits addresses, and we need a NOOP for the batch size to
  9236. * stay even.
  9237. */
  9238. if (IS_GEN8(dev))
  9239. len += 2;
  9240. }
  9241. /*
  9242. * BSpec MI_DISPLAY_FLIP for IVB:
  9243. * "The full packet must be contained within the same cache line."
  9244. *
  9245. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9246. * cacheline, if we ever start emitting more commands before
  9247. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9248. * then do the cacheline alignment, and finally emit the
  9249. * MI_DISPLAY_FLIP.
  9250. */
  9251. ret = intel_ring_cacheline_align(req);
  9252. if (ret)
  9253. return ret;
  9254. ret = intel_ring_begin(req, len);
  9255. if (ret)
  9256. return ret;
  9257. /* Unmask the flip-done completion message. Note that the bspec says that
  9258. * we should do this for both the BCS and RCS, and that we must not unmask
  9259. * more than one flip event at any time (or ensure that one flip message
  9260. * can be sent by waiting for flip-done prior to queueing new flips).
  9261. * Experimentation says that BCS works despite DERRMR masking all
  9262. * flip-done completion events and that unmasking all planes at once
  9263. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9264. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9265. */
  9266. if (ring->id == RCS) {
  9267. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9268. intel_ring_emit(ring, DERRMR);
  9269. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9270. DERRMR_PIPEB_PRI_FLIP_DONE |
  9271. DERRMR_PIPEC_PRI_FLIP_DONE));
  9272. if (IS_GEN8(dev))
  9273. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9274. MI_SRM_LRM_GLOBAL_GTT);
  9275. else
  9276. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9277. MI_SRM_LRM_GLOBAL_GTT);
  9278. intel_ring_emit(ring, DERRMR);
  9279. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9280. if (IS_GEN8(dev)) {
  9281. intel_ring_emit(ring, 0);
  9282. intel_ring_emit(ring, MI_NOOP);
  9283. }
  9284. }
  9285. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9286. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9287. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9288. intel_ring_emit(ring, (MI_NOOP));
  9289. intel_mark_page_flip_active(intel_crtc);
  9290. return 0;
  9291. }
  9292. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9293. struct drm_i915_gem_object *obj)
  9294. {
  9295. /*
  9296. * This is not being used for older platforms, because
  9297. * non-availability of flip done interrupt forces us to use
  9298. * CS flips. Older platforms derive flip done using some clever
  9299. * tricks involving the flip_pending status bits and vblank irqs.
  9300. * So using MMIO flips there would disrupt this mechanism.
  9301. */
  9302. if (ring == NULL)
  9303. return true;
  9304. if (INTEL_INFO(ring->dev)->gen < 5)
  9305. return false;
  9306. if (i915.use_mmio_flip < 0)
  9307. return false;
  9308. else if (i915.use_mmio_flip > 0)
  9309. return true;
  9310. else if (i915.enable_execlists)
  9311. return true;
  9312. else
  9313. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9314. }
  9315. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9316. {
  9317. struct drm_device *dev = intel_crtc->base.dev;
  9318. struct drm_i915_private *dev_priv = dev->dev_private;
  9319. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9320. const enum pipe pipe = intel_crtc->pipe;
  9321. u32 ctl, stride;
  9322. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9323. ctl &= ~PLANE_CTL_TILED_MASK;
  9324. switch (fb->modifier[0]) {
  9325. case DRM_FORMAT_MOD_NONE:
  9326. break;
  9327. case I915_FORMAT_MOD_X_TILED:
  9328. ctl |= PLANE_CTL_TILED_X;
  9329. break;
  9330. case I915_FORMAT_MOD_Y_TILED:
  9331. ctl |= PLANE_CTL_TILED_Y;
  9332. break;
  9333. case I915_FORMAT_MOD_Yf_TILED:
  9334. ctl |= PLANE_CTL_TILED_YF;
  9335. break;
  9336. default:
  9337. MISSING_CASE(fb->modifier[0]);
  9338. }
  9339. /*
  9340. * The stride is either expressed as a multiple of 64 bytes chunks for
  9341. * linear buffers or in number of tiles for tiled buffers.
  9342. */
  9343. stride = fb->pitches[0] /
  9344. intel_fb_stride_alignment(dev, fb->modifier[0],
  9345. fb->pixel_format);
  9346. /*
  9347. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9348. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9349. */
  9350. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9351. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9352. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9353. POSTING_READ(PLANE_SURF(pipe, 0));
  9354. }
  9355. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9356. {
  9357. struct drm_device *dev = intel_crtc->base.dev;
  9358. struct drm_i915_private *dev_priv = dev->dev_private;
  9359. struct intel_framebuffer *intel_fb =
  9360. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9361. struct drm_i915_gem_object *obj = intel_fb->obj;
  9362. u32 dspcntr;
  9363. u32 reg;
  9364. reg = DSPCNTR(intel_crtc->plane);
  9365. dspcntr = I915_READ(reg);
  9366. if (obj->tiling_mode != I915_TILING_NONE)
  9367. dspcntr |= DISPPLANE_TILED;
  9368. else
  9369. dspcntr &= ~DISPPLANE_TILED;
  9370. I915_WRITE(reg, dspcntr);
  9371. I915_WRITE(DSPSURF(intel_crtc->plane),
  9372. intel_crtc->unpin_work->gtt_offset);
  9373. POSTING_READ(DSPSURF(intel_crtc->plane));
  9374. }
  9375. /*
  9376. * XXX: This is the temporary way to update the plane registers until we get
  9377. * around to using the usual plane update functions for MMIO flips
  9378. */
  9379. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9380. {
  9381. struct drm_device *dev = intel_crtc->base.dev;
  9382. intel_mark_page_flip_active(intel_crtc);
  9383. intel_pipe_update_start(intel_crtc);
  9384. if (INTEL_INFO(dev)->gen >= 9)
  9385. skl_do_mmio_flip(intel_crtc);
  9386. else
  9387. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9388. ilk_do_mmio_flip(intel_crtc);
  9389. intel_pipe_update_end(intel_crtc);
  9390. }
  9391. static void intel_mmio_flip_work_func(struct work_struct *work)
  9392. {
  9393. struct intel_mmio_flip *mmio_flip =
  9394. container_of(work, struct intel_mmio_flip, work);
  9395. if (mmio_flip->req)
  9396. WARN_ON(__i915_wait_request(mmio_flip->req,
  9397. mmio_flip->crtc->reset_counter,
  9398. false, NULL,
  9399. &mmio_flip->i915->rps.mmioflips));
  9400. intel_do_mmio_flip(mmio_flip->crtc);
  9401. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9402. kfree(mmio_flip);
  9403. }
  9404. static int intel_queue_mmio_flip(struct drm_device *dev,
  9405. struct drm_crtc *crtc,
  9406. struct drm_framebuffer *fb,
  9407. struct drm_i915_gem_object *obj,
  9408. struct intel_engine_cs *ring,
  9409. uint32_t flags)
  9410. {
  9411. struct intel_mmio_flip *mmio_flip;
  9412. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9413. if (mmio_flip == NULL)
  9414. return -ENOMEM;
  9415. mmio_flip->i915 = to_i915(dev);
  9416. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9417. mmio_flip->crtc = to_intel_crtc(crtc);
  9418. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9419. schedule_work(&mmio_flip->work);
  9420. return 0;
  9421. }
  9422. static int intel_default_queue_flip(struct drm_device *dev,
  9423. struct drm_crtc *crtc,
  9424. struct drm_framebuffer *fb,
  9425. struct drm_i915_gem_object *obj,
  9426. struct drm_i915_gem_request *req,
  9427. uint32_t flags)
  9428. {
  9429. return -ENODEV;
  9430. }
  9431. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9432. struct drm_crtc *crtc)
  9433. {
  9434. struct drm_i915_private *dev_priv = dev->dev_private;
  9435. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9436. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9437. u32 addr;
  9438. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9439. return true;
  9440. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9441. return false;
  9442. if (!work->enable_stall_check)
  9443. return false;
  9444. if (work->flip_ready_vblank == 0) {
  9445. if (work->flip_queued_req &&
  9446. !i915_gem_request_completed(work->flip_queued_req, true))
  9447. return false;
  9448. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9449. }
  9450. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9451. return false;
  9452. /* Potential stall - if we see that the flip has happened,
  9453. * assume a missed interrupt. */
  9454. if (INTEL_INFO(dev)->gen >= 4)
  9455. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9456. else
  9457. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9458. /* There is a potential issue here with a false positive after a flip
  9459. * to the same address. We could address this by checking for a
  9460. * non-incrementing frame counter.
  9461. */
  9462. return addr == work->gtt_offset;
  9463. }
  9464. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9465. {
  9466. struct drm_i915_private *dev_priv = dev->dev_private;
  9467. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9468. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9469. struct intel_unpin_work *work;
  9470. WARN_ON(!in_interrupt());
  9471. if (crtc == NULL)
  9472. return;
  9473. spin_lock(&dev->event_lock);
  9474. work = intel_crtc->unpin_work;
  9475. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9476. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9477. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9478. page_flip_completed(intel_crtc);
  9479. work = NULL;
  9480. }
  9481. if (work != NULL &&
  9482. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9483. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9484. spin_unlock(&dev->event_lock);
  9485. }
  9486. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9487. struct drm_framebuffer *fb,
  9488. struct drm_pending_vblank_event *event,
  9489. uint32_t page_flip_flags)
  9490. {
  9491. struct drm_device *dev = crtc->dev;
  9492. struct drm_i915_private *dev_priv = dev->dev_private;
  9493. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9494. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9495. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9496. struct drm_plane *primary = crtc->primary;
  9497. enum pipe pipe = intel_crtc->pipe;
  9498. struct intel_unpin_work *work;
  9499. struct intel_engine_cs *ring;
  9500. bool mmio_flip;
  9501. struct drm_i915_gem_request *request = NULL;
  9502. int ret;
  9503. /*
  9504. * drm_mode_page_flip_ioctl() should already catch this, but double
  9505. * check to be safe. In the future we may enable pageflipping from
  9506. * a disabled primary plane.
  9507. */
  9508. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9509. return -EBUSY;
  9510. /* Can't change pixel format via MI display flips. */
  9511. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9512. return -EINVAL;
  9513. /*
  9514. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9515. * Note that pitch changes could also affect these register.
  9516. */
  9517. if (INTEL_INFO(dev)->gen > 3 &&
  9518. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9519. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9520. return -EINVAL;
  9521. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9522. goto out_hang;
  9523. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9524. if (work == NULL)
  9525. return -ENOMEM;
  9526. work->event = event;
  9527. work->crtc = crtc;
  9528. work->old_fb = old_fb;
  9529. INIT_WORK(&work->work, intel_unpin_work_fn);
  9530. ret = drm_crtc_vblank_get(crtc);
  9531. if (ret)
  9532. goto free_work;
  9533. /* We borrow the event spin lock for protecting unpin_work */
  9534. spin_lock_irq(&dev->event_lock);
  9535. if (intel_crtc->unpin_work) {
  9536. /* Before declaring the flip queue wedged, check if
  9537. * the hardware completed the operation behind our backs.
  9538. */
  9539. if (__intel_pageflip_stall_check(dev, crtc)) {
  9540. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9541. page_flip_completed(intel_crtc);
  9542. } else {
  9543. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9544. spin_unlock_irq(&dev->event_lock);
  9545. drm_crtc_vblank_put(crtc);
  9546. kfree(work);
  9547. return -EBUSY;
  9548. }
  9549. }
  9550. intel_crtc->unpin_work = work;
  9551. spin_unlock_irq(&dev->event_lock);
  9552. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9553. flush_workqueue(dev_priv->wq);
  9554. /* Reference the objects for the scheduled work. */
  9555. drm_framebuffer_reference(work->old_fb);
  9556. drm_gem_object_reference(&obj->base);
  9557. crtc->primary->fb = fb;
  9558. update_state_fb(crtc->primary);
  9559. work->pending_flip_obj = obj;
  9560. ret = i915_mutex_lock_interruptible(dev);
  9561. if (ret)
  9562. goto cleanup;
  9563. atomic_inc(&intel_crtc->unpin_work_count);
  9564. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9565. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9566. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9567. if (IS_VALLEYVIEW(dev)) {
  9568. ring = &dev_priv->ring[BCS];
  9569. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9570. /* vlv: DISPLAY_FLIP fails to change tiling */
  9571. ring = NULL;
  9572. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9573. ring = &dev_priv->ring[BCS];
  9574. } else if (INTEL_INFO(dev)->gen >= 7) {
  9575. ring = i915_gem_request_get_ring(obj->last_write_req);
  9576. if (ring == NULL || ring->id != RCS)
  9577. ring = &dev_priv->ring[BCS];
  9578. } else {
  9579. ring = &dev_priv->ring[RCS];
  9580. }
  9581. mmio_flip = use_mmio_flip(ring, obj);
  9582. /* When using CS flips, we want to emit semaphores between rings.
  9583. * However, when using mmio flips we will create a task to do the
  9584. * synchronisation, so all we want here is to pin the framebuffer
  9585. * into the display plane and skip any waits.
  9586. */
  9587. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9588. crtc->primary->state,
  9589. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9590. if (ret)
  9591. goto cleanup_pending;
  9592. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9593. obj, 0);
  9594. work->gtt_offset += intel_crtc->dspaddr_offset;
  9595. if (mmio_flip) {
  9596. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9597. page_flip_flags);
  9598. if (ret)
  9599. goto cleanup_unpin;
  9600. i915_gem_request_assign(&work->flip_queued_req,
  9601. obj->last_write_req);
  9602. } else {
  9603. if (!request) {
  9604. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9605. if (ret)
  9606. goto cleanup_unpin;
  9607. }
  9608. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9609. page_flip_flags);
  9610. if (ret)
  9611. goto cleanup_unpin;
  9612. i915_gem_request_assign(&work->flip_queued_req, request);
  9613. }
  9614. if (request)
  9615. i915_add_request_no_flush(request);
  9616. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9617. work->enable_stall_check = true;
  9618. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9619. to_intel_plane(primary)->frontbuffer_bit);
  9620. mutex_unlock(&dev->struct_mutex);
  9621. intel_fbc_disable_crtc(intel_crtc);
  9622. intel_frontbuffer_flip_prepare(dev,
  9623. to_intel_plane(primary)->frontbuffer_bit);
  9624. trace_i915_flip_request(intel_crtc->plane, obj);
  9625. return 0;
  9626. cleanup_unpin:
  9627. intel_unpin_fb_obj(fb, crtc->primary->state);
  9628. cleanup_pending:
  9629. if (request)
  9630. i915_gem_request_cancel(request);
  9631. atomic_dec(&intel_crtc->unpin_work_count);
  9632. mutex_unlock(&dev->struct_mutex);
  9633. cleanup:
  9634. crtc->primary->fb = old_fb;
  9635. update_state_fb(crtc->primary);
  9636. drm_gem_object_unreference_unlocked(&obj->base);
  9637. drm_framebuffer_unreference(work->old_fb);
  9638. spin_lock_irq(&dev->event_lock);
  9639. intel_crtc->unpin_work = NULL;
  9640. spin_unlock_irq(&dev->event_lock);
  9641. drm_crtc_vblank_put(crtc);
  9642. free_work:
  9643. kfree(work);
  9644. if (ret == -EIO) {
  9645. struct drm_atomic_state *state;
  9646. struct drm_plane_state *plane_state;
  9647. out_hang:
  9648. state = drm_atomic_state_alloc(dev);
  9649. if (!state)
  9650. return -ENOMEM;
  9651. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9652. retry:
  9653. plane_state = drm_atomic_get_plane_state(state, primary);
  9654. ret = PTR_ERR_OR_ZERO(plane_state);
  9655. if (!ret) {
  9656. drm_atomic_set_fb_for_plane(plane_state, fb);
  9657. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9658. if (!ret)
  9659. ret = drm_atomic_commit(state);
  9660. }
  9661. if (ret == -EDEADLK) {
  9662. drm_modeset_backoff(state->acquire_ctx);
  9663. drm_atomic_state_clear(state);
  9664. goto retry;
  9665. }
  9666. if (ret)
  9667. drm_atomic_state_free(state);
  9668. if (ret == 0 && event) {
  9669. spin_lock_irq(&dev->event_lock);
  9670. drm_send_vblank_event(dev, pipe, event);
  9671. spin_unlock_irq(&dev->event_lock);
  9672. }
  9673. }
  9674. return ret;
  9675. }
  9676. /**
  9677. * intel_wm_need_update - Check whether watermarks need updating
  9678. * @plane: drm plane
  9679. * @state: new plane state
  9680. *
  9681. * Check current plane state versus the new one to determine whether
  9682. * watermarks need to be recalculated.
  9683. *
  9684. * Returns true or false.
  9685. */
  9686. static bool intel_wm_need_update(struct drm_plane *plane,
  9687. struct drm_plane_state *state)
  9688. {
  9689. /* Update watermarks on tiling changes. */
  9690. if (!plane->state->fb || !state->fb ||
  9691. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9692. plane->state->rotation != state->rotation)
  9693. return true;
  9694. if (plane->state->crtc_w != state->crtc_w)
  9695. return true;
  9696. return false;
  9697. }
  9698. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9699. struct drm_plane_state *plane_state)
  9700. {
  9701. struct drm_crtc *crtc = crtc_state->crtc;
  9702. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9703. struct drm_plane *plane = plane_state->plane;
  9704. struct drm_device *dev = crtc->dev;
  9705. struct drm_i915_private *dev_priv = dev->dev_private;
  9706. struct intel_plane_state *old_plane_state =
  9707. to_intel_plane_state(plane->state);
  9708. int idx = intel_crtc->base.base.id, ret;
  9709. int i = drm_plane_index(plane);
  9710. bool mode_changed = needs_modeset(crtc_state);
  9711. bool was_crtc_enabled = crtc->state->active;
  9712. bool is_crtc_enabled = crtc_state->active;
  9713. bool turn_off, turn_on, visible, was_visible;
  9714. struct drm_framebuffer *fb = plane_state->fb;
  9715. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9716. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9717. ret = skl_update_scaler_plane(
  9718. to_intel_crtc_state(crtc_state),
  9719. to_intel_plane_state(plane_state));
  9720. if (ret)
  9721. return ret;
  9722. }
  9723. /*
  9724. * Disabling a plane is always okay; we just need to update
  9725. * fb tracking in a special way since cleanup_fb() won't
  9726. * get called by the plane helpers.
  9727. */
  9728. if (old_plane_state->base.fb && !fb)
  9729. intel_crtc->atomic.disabled_planes |= 1 << i;
  9730. was_visible = old_plane_state->visible;
  9731. visible = to_intel_plane_state(plane_state)->visible;
  9732. if (!was_crtc_enabled && WARN_ON(was_visible))
  9733. was_visible = false;
  9734. if (!is_crtc_enabled && WARN_ON(visible))
  9735. visible = false;
  9736. if (!was_visible && !visible)
  9737. return 0;
  9738. turn_off = was_visible && (!visible || mode_changed);
  9739. turn_on = visible && (!was_visible || mode_changed);
  9740. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9741. plane->base.id, fb ? fb->base.id : -1);
  9742. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9743. plane->base.id, was_visible, visible,
  9744. turn_off, turn_on, mode_changed);
  9745. if (turn_on) {
  9746. intel_crtc->atomic.update_wm_pre = true;
  9747. /* must disable cxsr around plane enable/disable */
  9748. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9749. intel_crtc->atomic.disable_cxsr = true;
  9750. /* to potentially re-enable cxsr */
  9751. intel_crtc->atomic.wait_vblank = true;
  9752. intel_crtc->atomic.update_wm_post = true;
  9753. }
  9754. } else if (turn_off) {
  9755. intel_crtc->atomic.update_wm_post = true;
  9756. /* must disable cxsr around plane enable/disable */
  9757. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9758. if (is_crtc_enabled)
  9759. intel_crtc->atomic.wait_vblank = true;
  9760. intel_crtc->atomic.disable_cxsr = true;
  9761. }
  9762. } else if (intel_wm_need_update(plane, plane_state)) {
  9763. intel_crtc->atomic.update_wm_pre = true;
  9764. }
  9765. if (visible || was_visible)
  9766. intel_crtc->atomic.fb_bits |=
  9767. to_intel_plane(plane)->frontbuffer_bit;
  9768. switch (plane->type) {
  9769. case DRM_PLANE_TYPE_PRIMARY:
  9770. intel_crtc->atomic.wait_for_flips = true;
  9771. intel_crtc->atomic.pre_disable_primary = turn_off;
  9772. intel_crtc->atomic.post_enable_primary = turn_on;
  9773. if (turn_off) {
  9774. /*
  9775. * FIXME: Actually if we will still have any other
  9776. * plane enabled on the pipe we could let IPS enabled
  9777. * still, but for now lets consider that when we make
  9778. * primary invisible by setting DSPCNTR to 0 on
  9779. * update_primary_plane function IPS needs to be
  9780. * disable.
  9781. */
  9782. intel_crtc->atomic.disable_ips = true;
  9783. intel_crtc->atomic.disable_fbc = true;
  9784. }
  9785. /*
  9786. * FBC does not work on some platforms for rotated
  9787. * planes, so disable it when rotation is not 0 and
  9788. * update it when rotation is set back to 0.
  9789. *
  9790. * FIXME: This is redundant with the fbc update done in
  9791. * the primary plane enable function except that that
  9792. * one is done too late. We eventually need to unify
  9793. * this.
  9794. */
  9795. if (visible &&
  9796. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9797. dev_priv->fbc.crtc == intel_crtc &&
  9798. plane_state->rotation != BIT(DRM_ROTATE_0))
  9799. intel_crtc->atomic.disable_fbc = true;
  9800. /*
  9801. * BDW signals flip done immediately if the plane
  9802. * is disabled, even if the plane enable is already
  9803. * armed to occur at the next vblank :(
  9804. */
  9805. if (turn_on && IS_BROADWELL(dev))
  9806. intel_crtc->atomic.wait_vblank = true;
  9807. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9808. break;
  9809. case DRM_PLANE_TYPE_CURSOR:
  9810. break;
  9811. case DRM_PLANE_TYPE_OVERLAY:
  9812. if (turn_off && !mode_changed) {
  9813. intel_crtc->atomic.wait_vblank = true;
  9814. intel_crtc->atomic.update_sprite_watermarks |=
  9815. 1 << i;
  9816. }
  9817. }
  9818. return 0;
  9819. }
  9820. static bool encoders_cloneable(const struct intel_encoder *a,
  9821. const struct intel_encoder *b)
  9822. {
  9823. /* masks could be asymmetric, so check both ways */
  9824. return a == b || (a->cloneable & (1 << b->type) &&
  9825. b->cloneable & (1 << a->type));
  9826. }
  9827. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9828. struct intel_crtc *crtc,
  9829. struct intel_encoder *encoder)
  9830. {
  9831. struct intel_encoder *source_encoder;
  9832. struct drm_connector *connector;
  9833. struct drm_connector_state *connector_state;
  9834. int i;
  9835. for_each_connector_in_state(state, connector, connector_state, i) {
  9836. if (connector_state->crtc != &crtc->base)
  9837. continue;
  9838. source_encoder =
  9839. to_intel_encoder(connector_state->best_encoder);
  9840. if (!encoders_cloneable(encoder, source_encoder))
  9841. return false;
  9842. }
  9843. return true;
  9844. }
  9845. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9846. struct intel_crtc *crtc)
  9847. {
  9848. struct intel_encoder *encoder;
  9849. struct drm_connector *connector;
  9850. struct drm_connector_state *connector_state;
  9851. int i;
  9852. for_each_connector_in_state(state, connector, connector_state, i) {
  9853. if (connector_state->crtc != &crtc->base)
  9854. continue;
  9855. encoder = to_intel_encoder(connector_state->best_encoder);
  9856. if (!check_single_encoder_cloning(state, crtc, encoder))
  9857. return false;
  9858. }
  9859. return true;
  9860. }
  9861. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9862. struct drm_crtc_state *crtc_state)
  9863. {
  9864. struct drm_device *dev = crtc->dev;
  9865. struct drm_i915_private *dev_priv = dev->dev_private;
  9866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9867. struct intel_crtc_state *pipe_config =
  9868. to_intel_crtc_state(crtc_state);
  9869. struct drm_atomic_state *state = crtc_state->state;
  9870. int ret;
  9871. bool mode_changed = needs_modeset(crtc_state);
  9872. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9873. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9874. return -EINVAL;
  9875. }
  9876. if (mode_changed && !crtc_state->active)
  9877. intel_crtc->atomic.update_wm_post = true;
  9878. if (mode_changed && crtc_state->enable &&
  9879. dev_priv->display.crtc_compute_clock &&
  9880. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9881. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9882. pipe_config);
  9883. if (ret)
  9884. return ret;
  9885. }
  9886. ret = 0;
  9887. if (INTEL_INFO(dev)->gen >= 9) {
  9888. if (mode_changed)
  9889. ret = skl_update_scaler_crtc(pipe_config);
  9890. if (!ret)
  9891. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9892. pipe_config);
  9893. }
  9894. return ret;
  9895. }
  9896. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9897. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9898. .load_lut = intel_crtc_load_lut,
  9899. .atomic_begin = intel_begin_crtc_commit,
  9900. .atomic_flush = intel_finish_crtc_commit,
  9901. .atomic_check = intel_crtc_atomic_check,
  9902. };
  9903. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9904. {
  9905. struct intel_connector *connector;
  9906. for_each_intel_connector(dev, connector) {
  9907. if (connector->base.encoder) {
  9908. connector->base.state->best_encoder =
  9909. connector->base.encoder;
  9910. connector->base.state->crtc =
  9911. connector->base.encoder->crtc;
  9912. } else {
  9913. connector->base.state->best_encoder = NULL;
  9914. connector->base.state->crtc = NULL;
  9915. }
  9916. }
  9917. }
  9918. static void
  9919. connected_sink_compute_bpp(struct intel_connector *connector,
  9920. struct intel_crtc_state *pipe_config)
  9921. {
  9922. int bpp = pipe_config->pipe_bpp;
  9923. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9924. connector->base.base.id,
  9925. connector->base.name);
  9926. /* Don't use an invalid EDID bpc value */
  9927. if (connector->base.display_info.bpc &&
  9928. connector->base.display_info.bpc * 3 < bpp) {
  9929. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9930. bpp, connector->base.display_info.bpc*3);
  9931. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9932. }
  9933. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9934. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9935. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9936. bpp);
  9937. pipe_config->pipe_bpp = 24;
  9938. }
  9939. }
  9940. static int
  9941. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9942. struct intel_crtc_state *pipe_config)
  9943. {
  9944. struct drm_device *dev = crtc->base.dev;
  9945. struct drm_atomic_state *state;
  9946. struct drm_connector *connector;
  9947. struct drm_connector_state *connector_state;
  9948. int bpp, i;
  9949. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9950. bpp = 10*3;
  9951. else if (INTEL_INFO(dev)->gen >= 5)
  9952. bpp = 12*3;
  9953. else
  9954. bpp = 8*3;
  9955. pipe_config->pipe_bpp = bpp;
  9956. state = pipe_config->base.state;
  9957. /* Clamp display bpp to EDID value */
  9958. for_each_connector_in_state(state, connector, connector_state, i) {
  9959. if (connector_state->crtc != &crtc->base)
  9960. continue;
  9961. connected_sink_compute_bpp(to_intel_connector(connector),
  9962. pipe_config);
  9963. }
  9964. return bpp;
  9965. }
  9966. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9967. {
  9968. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9969. "type: 0x%x flags: 0x%x\n",
  9970. mode->crtc_clock,
  9971. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9972. mode->crtc_hsync_end, mode->crtc_htotal,
  9973. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9974. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9975. }
  9976. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9977. struct intel_crtc_state *pipe_config,
  9978. const char *context)
  9979. {
  9980. struct drm_device *dev = crtc->base.dev;
  9981. struct drm_plane *plane;
  9982. struct intel_plane *intel_plane;
  9983. struct intel_plane_state *state;
  9984. struct drm_framebuffer *fb;
  9985. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9986. context, pipe_config, pipe_name(crtc->pipe));
  9987. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9988. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9989. pipe_config->pipe_bpp, pipe_config->dither);
  9990. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9991. pipe_config->has_pch_encoder,
  9992. pipe_config->fdi_lanes,
  9993. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9994. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9995. pipe_config->fdi_m_n.tu);
  9996. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9997. pipe_config->has_dp_encoder,
  9998. pipe_config->lane_count,
  9999. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10000. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10001. pipe_config->dp_m_n.tu);
  10002. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10003. pipe_config->has_dp_encoder,
  10004. pipe_config->lane_count,
  10005. pipe_config->dp_m2_n2.gmch_m,
  10006. pipe_config->dp_m2_n2.gmch_n,
  10007. pipe_config->dp_m2_n2.link_m,
  10008. pipe_config->dp_m2_n2.link_n,
  10009. pipe_config->dp_m2_n2.tu);
  10010. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10011. pipe_config->has_audio,
  10012. pipe_config->has_infoframe);
  10013. DRM_DEBUG_KMS("requested mode:\n");
  10014. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10015. DRM_DEBUG_KMS("adjusted mode:\n");
  10016. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10017. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10018. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10019. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10020. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10021. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10022. crtc->num_scalers,
  10023. pipe_config->scaler_state.scaler_users,
  10024. pipe_config->scaler_state.scaler_id);
  10025. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10026. pipe_config->gmch_pfit.control,
  10027. pipe_config->gmch_pfit.pgm_ratios,
  10028. pipe_config->gmch_pfit.lvds_border_bits);
  10029. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10030. pipe_config->pch_pfit.pos,
  10031. pipe_config->pch_pfit.size,
  10032. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10033. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10034. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10035. if (IS_BROXTON(dev)) {
  10036. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10037. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10038. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10039. pipe_config->ddi_pll_sel,
  10040. pipe_config->dpll_hw_state.ebb0,
  10041. pipe_config->dpll_hw_state.ebb4,
  10042. pipe_config->dpll_hw_state.pll0,
  10043. pipe_config->dpll_hw_state.pll1,
  10044. pipe_config->dpll_hw_state.pll2,
  10045. pipe_config->dpll_hw_state.pll3,
  10046. pipe_config->dpll_hw_state.pll6,
  10047. pipe_config->dpll_hw_state.pll8,
  10048. pipe_config->dpll_hw_state.pll9,
  10049. pipe_config->dpll_hw_state.pll10,
  10050. pipe_config->dpll_hw_state.pcsdw12);
  10051. } else if (IS_SKYLAKE(dev)) {
  10052. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10053. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10054. pipe_config->ddi_pll_sel,
  10055. pipe_config->dpll_hw_state.ctrl1,
  10056. pipe_config->dpll_hw_state.cfgcr1,
  10057. pipe_config->dpll_hw_state.cfgcr2);
  10058. } else if (HAS_DDI(dev)) {
  10059. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10060. pipe_config->ddi_pll_sel,
  10061. pipe_config->dpll_hw_state.wrpll);
  10062. } else {
  10063. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10064. "fp0: 0x%x, fp1: 0x%x\n",
  10065. pipe_config->dpll_hw_state.dpll,
  10066. pipe_config->dpll_hw_state.dpll_md,
  10067. pipe_config->dpll_hw_state.fp0,
  10068. pipe_config->dpll_hw_state.fp1);
  10069. }
  10070. DRM_DEBUG_KMS("planes on this crtc\n");
  10071. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10072. intel_plane = to_intel_plane(plane);
  10073. if (intel_plane->pipe != crtc->pipe)
  10074. continue;
  10075. state = to_intel_plane_state(plane->state);
  10076. fb = state->base.fb;
  10077. if (!fb) {
  10078. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10079. "disabled, scaler_id = %d\n",
  10080. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10081. plane->base.id, intel_plane->pipe,
  10082. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10083. drm_plane_index(plane), state->scaler_id);
  10084. continue;
  10085. }
  10086. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10087. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10088. plane->base.id, intel_plane->pipe,
  10089. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10090. drm_plane_index(plane));
  10091. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10092. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10093. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10094. state->scaler_id,
  10095. state->src.x1 >> 16, state->src.y1 >> 16,
  10096. drm_rect_width(&state->src) >> 16,
  10097. drm_rect_height(&state->src) >> 16,
  10098. state->dst.x1, state->dst.y1,
  10099. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10100. }
  10101. }
  10102. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10103. {
  10104. struct drm_device *dev = state->dev;
  10105. struct intel_encoder *encoder;
  10106. struct drm_connector *connector;
  10107. struct drm_connector_state *connector_state;
  10108. unsigned int used_ports = 0;
  10109. int i;
  10110. /*
  10111. * Walk the connector list instead of the encoder
  10112. * list to detect the problem on ddi platforms
  10113. * where there's just one encoder per digital port.
  10114. */
  10115. for_each_connector_in_state(state, connector, connector_state, i) {
  10116. if (!connector_state->best_encoder)
  10117. continue;
  10118. encoder = to_intel_encoder(connector_state->best_encoder);
  10119. WARN_ON(!connector_state->crtc);
  10120. switch (encoder->type) {
  10121. unsigned int port_mask;
  10122. case INTEL_OUTPUT_UNKNOWN:
  10123. if (WARN_ON(!HAS_DDI(dev)))
  10124. break;
  10125. case INTEL_OUTPUT_DISPLAYPORT:
  10126. case INTEL_OUTPUT_HDMI:
  10127. case INTEL_OUTPUT_EDP:
  10128. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10129. /* the same port mustn't appear more than once */
  10130. if (used_ports & port_mask)
  10131. return false;
  10132. used_ports |= port_mask;
  10133. default:
  10134. break;
  10135. }
  10136. }
  10137. return true;
  10138. }
  10139. static void
  10140. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10141. {
  10142. struct drm_crtc_state tmp_state;
  10143. struct intel_crtc_scaler_state scaler_state;
  10144. struct intel_dpll_hw_state dpll_hw_state;
  10145. enum intel_dpll_id shared_dpll;
  10146. uint32_t ddi_pll_sel;
  10147. bool force_thru;
  10148. /* FIXME: before the switch to atomic started, a new pipe_config was
  10149. * kzalloc'd. Code that depends on any field being zero should be
  10150. * fixed, so that the crtc_state can be safely duplicated. For now,
  10151. * only fields that are know to not cause problems are preserved. */
  10152. tmp_state = crtc_state->base;
  10153. scaler_state = crtc_state->scaler_state;
  10154. shared_dpll = crtc_state->shared_dpll;
  10155. dpll_hw_state = crtc_state->dpll_hw_state;
  10156. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10157. force_thru = crtc_state->pch_pfit.force_thru;
  10158. memset(crtc_state, 0, sizeof *crtc_state);
  10159. crtc_state->base = tmp_state;
  10160. crtc_state->scaler_state = scaler_state;
  10161. crtc_state->shared_dpll = shared_dpll;
  10162. crtc_state->dpll_hw_state = dpll_hw_state;
  10163. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10164. crtc_state->pch_pfit.force_thru = force_thru;
  10165. }
  10166. static int
  10167. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10168. struct intel_crtc_state *pipe_config)
  10169. {
  10170. struct drm_atomic_state *state = pipe_config->base.state;
  10171. struct intel_encoder *encoder;
  10172. struct drm_connector *connector;
  10173. struct drm_connector_state *connector_state;
  10174. int base_bpp, ret = -EINVAL;
  10175. int i;
  10176. bool retry = true;
  10177. clear_intel_crtc_state(pipe_config);
  10178. pipe_config->cpu_transcoder =
  10179. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10180. /*
  10181. * Sanitize sync polarity flags based on requested ones. If neither
  10182. * positive or negative polarity is requested, treat this as meaning
  10183. * negative polarity.
  10184. */
  10185. if (!(pipe_config->base.adjusted_mode.flags &
  10186. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10187. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10188. if (!(pipe_config->base.adjusted_mode.flags &
  10189. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10190. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10191. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10192. pipe_config);
  10193. if (base_bpp < 0)
  10194. goto fail;
  10195. /*
  10196. * Determine the real pipe dimensions. Note that stereo modes can
  10197. * increase the actual pipe size due to the frame doubling and
  10198. * insertion of additional space for blanks between the frame. This
  10199. * is stored in the crtc timings. We use the requested mode to do this
  10200. * computation to clearly distinguish it from the adjusted mode, which
  10201. * can be changed by the connectors in the below retry loop.
  10202. */
  10203. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10204. &pipe_config->pipe_src_w,
  10205. &pipe_config->pipe_src_h);
  10206. encoder_retry:
  10207. /* Ensure the port clock defaults are reset when retrying. */
  10208. pipe_config->port_clock = 0;
  10209. pipe_config->pixel_multiplier = 1;
  10210. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10211. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10212. CRTC_STEREO_DOUBLE);
  10213. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10214. * adjust it according to limitations or connector properties, and also
  10215. * a chance to reject the mode entirely.
  10216. */
  10217. for_each_connector_in_state(state, connector, connector_state, i) {
  10218. if (connector_state->crtc != crtc)
  10219. continue;
  10220. encoder = to_intel_encoder(connector_state->best_encoder);
  10221. if (!(encoder->compute_config(encoder, pipe_config))) {
  10222. DRM_DEBUG_KMS("Encoder config failure\n");
  10223. goto fail;
  10224. }
  10225. }
  10226. /* Set default port clock if not overwritten by the encoder. Needs to be
  10227. * done afterwards in case the encoder adjusts the mode. */
  10228. if (!pipe_config->port_clock)
  10229. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10230. * pipe_config->pixel_multiplier;
  10231. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10232. if (ret < 0) {
  10233. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10234. goto fail;
  10235. }
  10236. if (ret == RETRY) {
  10237. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10238. ret = -EINVAL;
  10239. goto fail;
  10240. }
  10241. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10242. retry = false;
  10243. goto encoder_retry;
  10244. }
  10245. /* Dithering seems to not pass-through bits correctly when it should, so
  10246. * only enable it on 6bpc panels. */
  10247. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10248. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10249. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10250. fail:
  10251. return ret;
  10252. }
  10253. static void
  10254. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10255. {
  10256. struct drm_crtc *crtc;
  10257. struct drm_crtc_state *crtc_state;
  10258. int i;
  10259. /* Double check state. */
  10260. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10261. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10262. /* Update hwmode for vblank functions */
  10263. if (crtc->state->active)
  10264. crtc->hwmode = crtc->state->adjusted_mode;
  10265. else
  10266. crtc->hwmode.crtc_clock = 0;
  10267. }
  10268. }
  10269. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10270. {
  10271. int diff;
  10272. if (clock1 == clock2)
  10273. return true;
  10274. if (!clock1 || !clock2)
  10275. return false;
  10276. diff = abs(clock1 - clock2);
  10277. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10278. return true;
  10279. return false;
  10280. }
  10281. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10282. list_for_each_entry((intel_crtc), \
  10283. &(dev)->mode_config.crtc_list, \
  10284. base.head) \
  10285. if (mask & (1 <<(intel_crtc)->pipe))
  10286. static bool
  10287. intel_compare_m_n(unsigned int m, unsigned int n,
  10288. unsigned int m2, unsigned int n2,
  10289. bool exact)
  10290. {
  10291. if (m == m2 && n == n2)
  10292. return true;
  10293. if (exact || !m || !n || !m2 || !n2)
  10294. return false;
  10295. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10296. if (m > m2) {
  10297. while (m > m2) {
  10298. m2 <<= 1;
  10299. n2 <<= 1;
  10300. }
  10301. } else if (m < m2) {
  10302. while (m < m2) {
  10303. m <<= 1;
  10304. n <<= 1;
  10305. }
  10306. }
  10307. return m == m2 && n == n2;
  10308. }
  10309. static bool
  10310. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10311. struct intel_link_m_n *m2_n2,
  10312. bool adjust)
  10313. {
  10314. if (m_n->tu == m2_n2->tu &&
  10315. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10316. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10317. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10318. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10319. if (adjust)
  10320. *m2_n2 = *m_n;
  10321. return true;
  10322. }
  10323. return false;
  10324. }
  10325. static bool
  10326. intel_pipe_config_compare(struct drm_device *dev,
  10327. struct intel_crtc_state *current_config,
  10328. struct intel_crtc_state *pipe_config,
  10329. bool adjust)
  10330. {
  10331. bool ret = true;
  10332. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10333. do { \
  10334. if (!adjust) \
  10335. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10336. else \
  10337. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10338. } while (0)
  10339. #define PIPE_CONF_CHECK_X(name) \
  10340. if (current_config->name != pipe_config->name) { \
  10341. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10342. "(expected 0x%08x, found 0x%08x)\n", \
  10343. current_config->name, \
  10344. pipe_config->name); \
  10345. ret = false; \
  10346. }
  10347. #define PIPE_CONF_CHECK_I(name) \
  10348. if (current_config->name != pipe_config->name) { \
  10349. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10350. "(expected %i, found %i)\n", \
  10351. current_config->name, \
  10352. pipe_config->name); \
  10353. ret = false; \
  10354. }
  10355. #define PIPE_CONF_CHECK_M_N(name) \
  10356. if (!intel_compare_link_m_n(&current_config->name, \
  10357. &pipe_config->name,\
  10358. adjust)) { \
  10359. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10360. "(expected tu %i gmch %i/%i link %i/%i, " \
  10361. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10362. current_config->name.tu, \
  10363. current_config->name.gmch_m, \
  10364. current_config->name.gmch_n, \
  10365. current_config->name.link_m, \
  10366. current_config->name.link_n, \
  10367. pipe_config->name.tu, \
  10368. pipe_config->name.gmch_m, \
  10369. pipe_config->name.gmch_n, \
  10370. pipe_config->name.link_m, \
  10371. pipe_config->name.link_n); \
  10372. ret = false; \
  10373. }
  10374. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10375. if (!intel_compare_link_m_n(&current_config->name, \
  10376. &pipe_config->name, adjust) && \
  10377. !intel_compare_link_m_n(&current_config->alt_name, \
  10378. &pipe_config->name, adjust)) { \
  10379. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10380. "(expected tu %i gmch %i/%i link %i/%i, " \
  10381. "or tu %i gmch %i/%i link %i/%i, " \
  10382. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10383. current_config->name.tu, \
  10384. current_config->name.gmch_m, \
  10385. current_config->name.gmch_n, \
  10386. current_config->name.link_m, \
  10387. current_config->name.link_n, \
  10388. current_config->alt_name.tu, \
  10389. current_config->alt_name.gmch_m, \
  10390. current_config->alt_name.gmch_n, \
  10391. current_config->alt_name.link_m, \
  10392. current_config->alt_name.link_n, \
  10393. pipe_config->name.tu, \
  10394. pipe_config->name.gmch_m, \
  10395. pipe_config->name.gmch_n, \
  10396. pipe_config->name.link_m, \
  10397. pipe_config->name.link_n); \
  10398. ret = false; \
  10399. }
  10400. /* This is required for BDW+ where there is only one set of registers for
  10401. * switching between high and low RR.
  10402. * This macro can be used whenever a comparison has to be made between one
  10403. * hw state and multiple sw state variables.
  10404. */
  10405. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10406. if ((current_config->name != pipe_config->name) && \
  10407. (current_config->alt_name != pipe_config->name)) { \
  10408. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10409. "(expected %i or %i, found %i)\n", \
  10410. current_config->name, \
  10411. current_config->alt_name, \
  10412. pipe_config->name); \
  10413. ret = false; \
  10414. }
  10415. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10416. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10417. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10418. "(expected %i, found %i)\n", \
  10419. current_config->name & (mask), \
  10420. pipe_config->name & (mask)); \
  10421. ret = false; \
  10422. }
  10423. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10424. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10425. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10426. "(expected %i, found %i)\n", \
  10427. current_config->name, \
  10428. pipe_config->name); \
  10429. ret = false; \
  10430. }
  10431. #define PIPE_CONF_QUIRK(quirk) \
  10432. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10433. PIPE_CONF_CHECK_I(cpu_transcoder);
  10434. PIPE_CONF_CHECK_I(has_pch_encoder);
  10435. PIPE_CONF_CHECK_I(fdi_lanes);
  10436. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10437. PIPE_CONF_CHECK_I(has_dp_encoder);
  10438. PIPE_CONF_CHECK_I(lane_count);
  10439. if (INTEL_INFO(dev)->gen < 8) {
  10440. PIPE_CONF_CHECK_M_N(dp_m_n);
  10441. PIPE_CONF_CHECK_I(has_drrs);
  10442. if (current_config->has_drrs)
  10443. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10444. } else
  10445. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10446. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10447. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10448. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10449. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10450. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10451. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10452. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10453. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10454. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10455. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10456. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10457. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10458. PIPE_CONF_CHECK_I(pixel_multiplier);
  10459. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10460. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10461. IS_VALLEYVIEW(dev))
  10462. PIPE_CONF_CHECK_I(limited_color_range);
  10463. PIPE_CONF_CHECK_I(has_infoframe);
  10464. PIPE_CONF_CHECK_I(has_audio);
  10465. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10466. DRM_MODE_FLAG_INTERLACE);
  10467. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10468. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10469. DRM_MODE_FLAG_PHSYNC);
  10470. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10471. DRM_MODE_FLAG_NHSYNC);
  10472. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10473. DRM_MODE_FLAG_PVSYNC);
  10474. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10475. DRM_MODE_FLAG_NVSYNC);
  10476. }
  10477. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10478. /* pfit ratios are autocomputed by the hw on gen4+ */
  10479. if (INTEL_INFO(dev)->gen < 4)
  10480. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10481. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10482. if (!adjust) {
  10483. PIPE_CONF_CHECK_I(pipe_src_w);
  10484. PIPE_CONF_CHECK_I(pipe_src_h);
  10485. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10486. if (current_config->pch_pfit.enabled) {
  10487. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10488. PIPE_CONF_CHECK_X(pch_pfit.size);
  10489. }
  10490. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10491. }
  10492. /* BDW+ don't expose a synchronous way to read the state */
  10493. if (IS_HASWELL(dev))
  10494. PIPE_CONF_CHECK_I(ips_enabled);
  10495. PIPE_CONF_CHECK_I(double_wide);
  10496. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10497. PIPE_CONF_CHECK_I(shared_dpll);
  10498. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10499. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10500. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10501. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10502. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10503. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10504. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10505. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10506. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10507. PIPE_CONF_CHECK_I(pipe_bpp);
  10508. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10509. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10510. #undef PIPE_CONF_CHECK_X
  10511. #undef PIPE_CONF_CHECK_I
  10512. #undef PIPE_CONF_CHECK_I_ALT
  10513. #undef PIPE_CONF_CHECK_FLAGS
  10514. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10515. #undef PIPE_CONF_QUIRK
  10516. #undef INTEL_ERR_OR_DBG_KMS
  10517. return ret;
  10518. }
  10519. static void check_wm_state(struct drm_device *dev)
  10520. {
  10521. struct drm_i915_private *dev_priv = dev->dev_private;
  10522. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10523. struct intel_crtc *intel_crtc;
  10524. int plane;
  10525. if (INTEL_INFO(dev)->gen < 9)
  10526. return;
  10527. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10528. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10529. for_each_intel_crtc(dev, intel_crtc) {
  10530. struct skl_ddb_entry *hw_entry, *sw_entry;
  10531. const enum pipe pipe = intel_crtc->pipe;
  10532. if (!intel_crtc->active)
  10533. continue;
  10534. /* planes */
  10535. for_each_plane(dev_priv, pipe, plane) {
  10536. hw_entry = &hw_ddb.plane[pipe][plane];
  10537. sw_entry = &sw_ddb->plane[pipe][plane];
  10538. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10539. continue;
  10540. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10541. "(expected (%u,%u), found (%u,%u))\n",
  10542. pipe_name(pipe), plane + 1,
  10543. sw_entry->start, sw_entry->end,
  10544. hw_entry->start, hw_entry->end);
  10545. }
  10546. /* cursor */
  10547. hw_entry = &hw_ddb.cursor[pipe];
  10548. sw_entry = &sw_ddb->cursor[pipe];
  10549. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10550. continue;
  10551. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10552. "(expected (%u,%u), found (%u,%u))\n",
  10553. pipe_name(pipe),
  10554. sw_entry->start, sw_entry->end,
  10555. hw_entry->start, hw_entry->end);
  10556. }
  10557. }
  10558. static void
  10559. check_connector_state(struct drm_device *dev,
  10560. struct drm_atomic_state *old_state)
  10561. {
  10562. struct drm_connector_state *old_conn_state;
  10563. struct drm_connector *connector;
  10564. int i;
  10565. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10566. struct drm_encoder *encoder = connector->encoder;
  10567. struct drm_connector_state *state = connector->state;
  10568. /* This also checks the encoder/connector hw state with the
  10569. * ->get_hw_state callbacks. */
  10570. intel_connector_check_state(to_intel_connector(connector));
  10571. I915_STATE_WARN(state->best_encoder != encoder,
  10572. "connector's atomic encoder doesn't match legacy encoder\n");
  10573. }
  10574. }
  10575. static void
  10576. check_encoder_state(struct drm_device *dev)
  10577. {
  10578. struct intel_encoder *encoder;
  10579. struct intel_connector *connector;
  10580. for_each_intel_encoder(dev, encoder) {
  10581. bool enabled = false;
  10582. enum pipe pipe;
  10583. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10584. encoder->base.base.id,
  10585. encoder->base.name);
  10586. for_each_intel_connector(dev, connector) {
  10587. if (connector->base.state->best_encoder != &encoder->base)
  10588. continue;
  10589. enabled = true;
  10590. I915_STATE_WARN(connector->base.state->crtc !=
  10591. encoder->base.crtc,
  10592. "connector's crtc doesn't match encoder crtc\n");
  10593. }
  10594. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10595. "encoder's enabled state mismatch "
  10596. "(expected %i, found %i)\n",
  10597. !!encoder->base.crtc, enabled);
  10598. if (!encoder->base.crtc) {
  10599. bool active;
  10600. active = encoder->get_hw_state(encoder, &pipe);
  10601. I915_STATE_WARN(active,
  10602. "encoder detached but still enabled on pipe %c.\n",
  10603. pipe_name(pipe));
  10604. }
  10605. }
  10606. }
  10607. static void
  10608. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10609. {
  10610. struct drm_i915_private *dev_priv = dev->dev_private;
  10611. struct intel_encoder *encoder;
  10612. struct drm_crtc_state *old_crtc_state;
  10613. struct drm_crtc *crtc;
  10614. int i;
  10615. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10616. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10617. struct intel_crtc_state *pipe_config, *sw_config;
  10618. bool active;
  10619. if (!needs_modeset(crtc->state) &&
  10620. !to_intel_crtc_state(crtc->state)->update_pipe)
  10621. continue;
  10622. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10623. pipe_config = to_intel_crtc_state(old_crtc_state);
  10624. memset(pipe_config, 0, sizeof(*pipe_config));
  10625. pipe_config->base.crtc = crtc;
  10626. pipe_config->base.state = old_state;
  10627. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10628. crtc->base.id);
  10629. active = dev_priv->display.get_pipe_config(intel_crtc,
  10630. pipe_config);
  10631. /* hw state is inconsistent with the pipe quirk */
  10632. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10633. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10634. active = crtc->state->active;
  10635. I915_STATE_WARN(crtc->state->active != active,
  10636. "crtc active state doesn't match with hw state "
  10637. "(expected %i, found %i)\n", crtc->state->active, active);
  10638. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10639. "transitional active state does not match atomic hw state "
  10640. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10641. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10642. enum pipe pipe;
  10643. active = encoder->get_hw_state(encoder, &pipe);
  10644. I915_STATE_WARN(active != crtc->state->active,
  10645. "[ENCODER:%i] active %i with crtc active %i\n",
  10646. encoder->base.base.id, active, crtc->state->active);
  10647. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10648. "Encoder connected to wrong pipe %c\n",
  10649. pipe_name(pipe));
  10650. if (active)
  10651. encoder->get_config(encoder, pipe_config);
  10652. }
  10653. if (!crtc->state->active)
  10654. continue;
  10655. sw_config = to_intel_crtc_state(crtc->state);
  10656. if (!intel_pipe_config_compare(dev, sw_config,
  10657. pipe_config, false)) {
  10658. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10659. intel_dump_pipe_config(intel_crtc, pipe_config,
  10660. "[hw state]");
  10661. intel_dump_pipe_config(intel_crtc, sw_config,
  10662. "[sw state]");
  10663. }
  10664. }
  10665. }
  10666. static void
  10667. check_shared_dpll_state(struct drm_device *dev)
  10668. {
  10669. struct drm_i915_private *dev_priv = dev->dev_private;
  10670. struct intel_crtc *crtc;
  10671. struct intel_dpll_hw_state dpll_hw_state;
  10672. int i;
  10673. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10674. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10675. int enabled_crtcs = 0, active_crtcs = 0;
  10676. bool active;
  10677. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10678. DRM_DEBUG_KMS("%s\n", pll->name);
  10679. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10680. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10681. "more active pll users than references: %i vs %i\n",
  10682. pll->active, hweight32(pll->config.crtc_mask));
  10683. I915_STATE_WARN(pll->active && !pll->on,
  10684. "pll in active use but not on in sw tracking\n");
  10685. I915_STATE_WARN(pll->on && !pll->active,
  10686. "pll in on but not on in use in sw tracking\n");
  10687. I915_STATE_WARN(pll->on != active,
  10688. "pll on state mismatch (expected %i, found %i)\n",
  10689. pll->on, active);
  10690. for_each_intel_crtc(dev, crtc) {
  10691. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10692. enabled_crtcs++;
  10693. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10694. active_crtcs++;
  10695. }
  10696. I915_STATE_WARN(pll->active != active_crtcs,
  10697. "pll active crtcs mismatch (expected %i, found %i)\n",
  10698. pll->active, active_crtcs);
  10699. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10700. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10701. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10702. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10703. sizeof(dpll_hw_state)),
  10704. "pll hw state mismatch\n");
  10705. }
  10706. }
  10707. static void
  10708. intel_modeset_check_state(struct drm_device *dev,
  10709. struct drm_atomic_state *old_state)
  10710. {
  10711. check_wm_state(dev);
  10712. check_connector_state(dev, old_state);
  10713. check_encoder_state(dev);
  10714. check_crtc_state(dev, old_state);
  10715. check_shared_dpll_state(dev);
  10716. }
  10717. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10718. int dotclock)
  10719. {
  10720. /*
  10721. * FDI already provided one idea for the dotclock.
  10722. * Yell if the encoder disagrees.
  10723. */
  10724. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10725. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10726. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10727. }
  10728. static void update_scanline_offset(struct intel_crtc *crtc)
  10729. {
  10730. struct drm_device *dev = crtc->base.dev;
  10731. /*
  10732. * The scanline counter increments at the leading edge of hsync.
  10733. *
  10734. * On most platforms it starts counting from vtotal-1 on the
  10735. * first active line. That means the scanline counter value is
  10736. * always one less than what we would expect. Ie. just after
  10737. * start of vblank, which also occurs at start of hsync (on the
  10738. * last active line), the scanline counter will read vblank_start-1.
  10739. *
  10740. * On gen2 the scanline counter starts counting from 1 instead
  10741. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10742. * to keep the value positive), instead of adding one.
  10743. *
  10744. * On HSW+ the behaviour of the scanline counter depends on the output
  10745. * type. For DP ports it behaves like most other platforms, but on HDMI
  10746. * there's an extra 1 line difference. So we need to add two instead of
  10747. * one to the value.
  10748. */
  10749. if (IS_GEN2(dev)) {
  10750. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10751. int vtotal;
  10752. vtotal = adjusted_mode->crtc_vtotal;
  10753. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10754. vtotal /= 2;
  10755. crtc->scanline_offset = vtotal - 1;
  10756. } else if (HAS_DDI(dev) &&
  10757. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10758. crtc->scanline_offset = 2;
  10759. } else
  10760. crtc->scanline_offset = 1;
  10761. }
  10762. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10763. {
  10764. struct drm_device *dev = state->dev;
  10765. struct drm_i915_private *dev_priv = to_i915(dev);
  10766. struct intel_shared_dpll_config *shared_dpll = NULL;
  10767. struct intel_crtc *intel_crtc;
  10768. struct intel_crtc_state *intel_crtc_state;
  10769. struct drm_crtc *crtc;
  10770. struct drm_crtc_state *crtc_state;
  10771. int i;
  10772. if (!dev_priv->display.crtc_compute_clock)
  10773. return;
  10774. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10775. int dpll;
  10776. intel_crtc = to_intel_crtc(crtc);
  10777. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10778. dpll = intel_crtc_state->shared_dpll;
  10779. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10780. continue;
  10781. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10782. if (!shared_dpll)
  10783. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10784. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10785. }
  10786. }
  10787. /*
  10788. * This implements the workaround described in the "notes" section of the mode
  10789. * set sequence documentation. When going from no pipes or single pipe to
  10790. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10791. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10792. */
  10793. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10794. {
  10795. struct drm_crtc_state *crtc_state;
  10796. struct intel_crtc *intel_crtc;
  10797. struct drm_crtc *crtc;
  10798. struct intel_crtc_state *first_crtc_state = NULL;
  10799. struct intel_crtc_state *other_crtc_state = NULL;
  10800. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10801. int i;
  10802. /* look at all crtc's that are going to be enabled in during modeset */
  10803. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10804. intel_crtc = to_intel_crtc(crtc);
  10805. if (!crtc_state->active || !needs_modeset(crtc_state))
  10806. continue;
  10807. if (first_crtc_state) {
  10808. other_crtc_state = to_intel_crtc_state(crtc_state);
  10809. break;
  10810. } else {
  10811. first_crtc_state = to_intel_crtc_state(crtc_state);
  10812. first_pipe = intel_crtc->pipe;
  10813. }
  10814. }
  10815. /* No workaround needed? */
  10816. if (!first_crtc_state)
  10817. return 0;
  10818. /* w/a possibly needed, check how many crtc's are already enabled. */
  10819. for_each_intel_crtc(state->dev, intel_crtc) {
  10820. struct intel_crtc_state *pipe_config;
  10821. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10822. if (IS_ERR(pipe_config))
  10823. return PTR_ERR(pipe_config);
  10824. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10825. if (!pipe_config->base.active ||
  10826. needs_modeset(&pipe_config->base))
  10827. continue;
  10828. /* 2 or more enabled crtcs means no need for w/a */
  10829. if (enabled_pipe != INVALID_PIPE)
  10830. return 0;
  10831. enabled_pipe = intel_crtc->pipe;
  10832. }
  10833. if (enabled_pipe != INVALID_PIPE)
  10834. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10835. else if (other_crtc_state)
  10836. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10837. return 0;
  10838. }
  10839. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10840. {
  10841. struct drm_crtc *crtc;
  10842. struct drm_crtc_state *crtc_state;
  10843. int ret = 0;
  10844. /* add all active pipes to the state */
  10845. for_each_crtc(state->dev, crtc) {
  10846. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10847. if (IS_ERR(crtc_state))
  10848. return PTR_ERR(crtc_state);
  10849. if (!crtc_state->active || needs_modeset(crtc_state))
  10850. continue;
  10851. crtc_state->mode_changed = true;
  10852. ret = drm_atomic_add_affected_connectors(state, crtc);
  10853. if (ret)
  10854. break;
  10855. ret = drm_atomic_add_affected_planes(state, crtc);
  10856. if (ret)
  10857. break;
  10858. }
  10859. return ret;
  10860. }
  10861. static int intel_modeset_checks(struct drm_atomic_state *state)
  10862. {
  10863. struct drm_device *dev = state->dev;
  10864. struct drm_i915_private *dev_priv = dev->dev_private;
  10865. int ret;
  10866. if (!check_digital_port_conflicts(state)) {
  10867. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10868. return -EINVAL;
  10869. }
  10870. /*
  10871. * See if the config requires any additional preparation, e.g.
  10872. * to adjust global state with pipes off. We need to do this
  10873. * here so we can get the modeset_pipe updated config for the new
  10874. * mode set on this crtc. For other crtcs we need to use the
  10875. * adjusted_mode bits in the crtc directly.
  10876. */
  10877. if (dev_priv->display.modeset_calc_cdclk) {
  10878. unsigned int cdclk;
  10879. ret = dev_priv->display.modeset_calc_cdclk(state);
  10880. cdclk = to_intel_atomic_state(state)->cdclk;
  10881. if (!ret && cdclk != dev_priv->cdclk_freq)
  10882. ret = intel_modeset_all_pipes(state);
  10883. if (ret < 0)
  10884. return ret;
  10885. } else
  10886. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10887. intel_modeset_clear_plls(state);
  10888. if (IS_HASWELL(dev))
  10889. return haswell_mode_set_planes_workaround(state);
  10890. return 0;
  10891. }
  10892. /**
  10893. * intel_atomic_check - validate state object
  10894. * @dev: drm device
  10895. * @state: state to validate
  10896. */
  10897. static int intel_atomic_check(struct drm_device *dev,
  10898. struct drm_atomic_state *state)
  10899. {
  10900. struct drm_crtc *crtc;
  10901. struct drm_crtc_state *crtc_state;
  10902. int ret, i;
  10903. bool any_ms = false;
  10904. ret = drm_atomic_helper_check_modeset(dev, state);
  10905. if (ret)
  10906. return ret;
  10907. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10908. struct intel_crtc_state *pipe_config =
  10909. to_intel_crtc_state(crtc_state);
  10910. /* Catch I915_MODE_FLAG_INHERITED */
  10911. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10912. crtc_state->mode_changed = true;
  10913. if (!crtc_state->enable) {
  10914. if (needs_modeset(crtc_state))
  10915. any_ms = true;
  10916. continue;
  10917. }
  10918. if (!needs_modeset(crtc_state))
  10919. continue;
  10920. /* FIXME: For only active_changed we shouldn't need to do any
  10921. * state recomputation at all. */
  10922. ret = drm_atomic_add_affected_connectors(state, crtc);
  10923. if (ret)
  10924. return ret;
  10925. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10926. if (ret)
  10927. return ret;
  10928. if (intel_pipe_config_compare(state->dev,
  10929. to_intel_crtc_state(crtc->state),
  10930. pipe_config, true)) {
  10931. crtc_state->mode_changed = false;
  10932. to_intel_crtc_state(crtc_state)->update_pipe = true;
  10933. }
  10934. if (needs_modeset(crtc_state)) {
  10935. any_ms = true;
  10936. ret = drm_atomic_add_affected_planes(state, crtc);
  10937. if (ret)
  10938. return ret;
  10939. }
  10940. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10941. needs_modeset(crtc_state) ?
  10942. "[modeset]" : "[fastset]");
  10943. }
  10944. if (any_ms) {
  10945. ret = intel_modeset_checks(state);
  10946. if (ret)
  10947. return ret;
  10948. } else
  10949. to_intel_atomic_state(state)->cdclk =
  10950. to_i915(state->dev)->cdclk_freq;
  10951. return drm_atomic_helper_check_planes(state->dev, state);
  10952. }
  10953. /**
  10954. * intel_atomic_commit - commit validated state object
  10955. * @dev: DRM device
  10956. * @state: the top-level driver state object
  10957. * @async: asynchronous commit
  10958. *
  10959. * This function commits a top-level state object that has been validated
  10960. * with drm_atomic_helper_check().
  10961. *
  10962. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  10963. * we can only handle plane-related operations and do not yet support
  10964. * asynchronous commit.
  10965. *
  10966. * RETURNS
  10967. * Zero for success or -errno.
  10968. */
  10969. static int intel_atomic_commit(struct drm_device *dev,
  10970. struct drm_atomic_state *state,
  10971. bool async)
  10972. {
  10973. struct drm_i915_private *dev_priv = dev->dev_private;
  10974. struct drm_crtc *crtc;
  10975. struct drm_crtc_state *crtc_state;
  10976. int ret = 0;
  10977. int i;
  10978. bool any_ms = false;
  10979. if (async) {
  10980. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  10981. return -EINVAL;
  10982. }
  10983. ret = drm_atomic_helper_prepare_planes(dev, state);
  10984. if (ret)
  10985. return ret;
  10986. drm_atomic_helper_swap_state(dev, state);
  10987. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10988. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10989. if (!needs_modeset(crtc->state))
  10990. continue;
  10991. any_ms = true;
  10992. intel_pre_plane_update(intel_crtc);
  10993. if (crtc_state->active) {
  10994. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  10995. dev_priv->display.crtc_disable(crtc);
  10996. intel_crtc->active = false;
  10997. intel_disable_shared_dpll(intel_crtc);
  10998. }
  10999. }
  11000. /* Only after disabling all output pipelines that will be changed can we
  11001. * update the the output configuration. */
  11002. intel_modeset_update_crtc_state(state);
  11003. if (any_ms) {
  11004. intel_shared_dpll_commit(state);
  11005. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11006. modeset_update_crtc_power_domains(state);
  11007. }
  11008. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11009. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11010. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11011. bool modeset = needs_modeset(crtc->state);
  11012. bool update_pipe = !modeset &&
  11013. to_intel_crtc_state(crtc->state)->update_pipe;
  11014. unsigned long put_domains = 0;
  11015. if (modeset && crtc->state->active) {
  11016. update_scanline_offset(to_intel_crtc(crtc));
  11017. dev_priv->display.crtc_enable(crtc);
  11018. }
  11019. if (update_pipe) {
  11020. put_domains = modeset_get_crtc_power_domains(crtc);
  11021. /* make sure intel_modeset_check_state runs */
  11022. any_ms = true;
  11023. }
  11024. if (!modeset)
  11025. intel_pre_plane_update(intel_crtc);
  11026. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11027. if (put_domains)
  11028. modeset_put_power_domains(dev_priv, put_domains);
  11029. intel_post_plane_update(intel_crtc);
  11030. }
  11031. /* FIXME: add subpixel order */
  11032. drm_atomic_helper_wait_for_vblanks(dev, state);
  11033. drm_atomic_helper_cleanup_planes(dev, state);
  11034. if (any_ms)
  11035. intel_modeset_check_state(dev, state);
  11036. drm_atomic_state_free(state);
  11037. return 0;
  11038. }
  11039. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11040. {
  11041. struct drm_device *dev = crtc->dev;
  11042. struct drm_atomic_state *state;
  11043. struct drm_crtc_state *crtc_state;
  11044. int ret;
  11045. state = drm_atomic_state_alloc(dev);
  11046. if (!state) {
  11047. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11048. crtc->base.id);
  11049. return;
  11050. }
  11051. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11052. retry:
  11053. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11054. ret = PTR_ERR_OR_ZERO(crtc_state);
  11055. if (!ret) {
  11056. if (!crtc_state->active)
  11057. goto out;
  11058. crtc_state->mode_changed = true;
  11059. ret = drm_atomic_commit(state);
  11060. }
  11061. if (ret == -EDEADLK) {
  11062. drm_atomic_state_clear(state);
  11063. drm_modeset_backoff(state->acquire_ctx);
  11064. goto retry;
  11065. }
  11066. if (ret)
  11067. out:
  11068. drm_atomic_state_free(state);
  11069. }
  11070. #undef for_each_intel_crtc_masked
  11071. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11072. .gamma_set = intel_crtc_gamma_set,
  11073. .set_config = drm_atomic_helper_set_config,
  11074. .destroy = intel_crtc_destroy,
  11075. .page_flip = intel_crtc_page_flip,
  11076. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11077. .atomic_destroy_state = intel_crtc_destroy_state,
  11078. };
  11079. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11080. struct intel_shared_dpll *pll,
  11081. struct intel_dpll_hw_state *hw_state)
  11082. {
  11083. uint32_t val;
  11084. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11085. return false;
  11086. val = I915_READ(PCH_DPLL(pll->id));
  11087. hw_state->dpll = val;
  11088. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11089. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11090. return val & DPLL_VCO_ENABLE;
  11091. }
  11092. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11093. struct intel_shared_dpll *pll)
  11094. {
  11095. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11096. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11097. }
  11098. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11099. struct intel_shared_dpll *pll)
  11100. {
  11101. /* PCH refclock must be enabled first */
  11102. ibx_assert_pch_refclk_enabled(dev_priv);
  11103. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11104. /* Wait for the clocks to stabilize. */
  11105. POSTING_READ(PCH_DPLL(pll->id));
  11106. udelay(150);
  11107. /* The pixel multiplier can only be updated once the
  11108. * DPLL is enabled and the clocks are stable.
  11109. *
  11110. * So write it again.
  11111. */
  11112. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11113. POSTING_READ(PCH_DPLL(pll->id));
  11114. udelay(200);
  11115. }
  11116. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11117. struct intel_shared_dpll *pll)
  11118. {
  11119. struct drm_device *dev = dev_priv->dev;
  11120. struct intel_crtc *crtc;
  11121. /* Make sure no transcoder isn't still depending on us. */
  11122. for_each_intel_crtc(dev, crtc) {
  11123. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11124. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11125. }
  11126. I915_WRITE(PCH_DPLL(pll->id), 0);
  11127. POSTING_READ(PCH_DPLL(pll->id));
  11128. udelay(200);
  11129. }
  11130. static char *ibx_pch_dpll_names[] = {
  11131. "PCH DPLL A",
  11132. "PCH DPLL B",
  11133. };
  11134. static void ibx_pch_dpll_init(struct drm_device *dev)
  11135. {
  11136. struct drm_i915_private *dev_priv = dev->dev_private;
  11137. int i;
  11138. dev_priv->num_shared_dpll = 2;
  11139. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11140. dev_priv->shared_dplls[i].id = i;
  11141. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11142. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11143. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11144. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11145. dev_priv->shared_dplls[i].get_hw_state =
  11146. ibx_pch_dpll_get_hw_state;
  11147. }
  11148. }
  11149. static void intel_shared_dpll_init(struct drm_device *dev)
  11150. {
  11151. struct drm_i915_private *dev_priv = dev->dev_private;
  11152. intel_update_cdclk(dev);
  11153. if (HAS_DDI(dev))
  11154. intel_ddi_pll_init(dev);
  11155. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11156. ibx_pch_dpll_init(dev);
  11157. else
  11158. dev_priv->num_shared_dpll = 0;
  11159. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11160. }
  11161. /**
  11162. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11163. * @plane: drm plane to prepare for
  11164. * @fb: framebuffer to prepare for presentation
  11165. *
  11166. * Prepares a framebuffer for usage on a display plane. Generally this
  11167. * involves pinning the underlying object and updating the frontbuffer tracking
  11168. * bits. Some older platforms need special physical address handling for
  11169. * cursor planes.
  11170. *
  11171. * Returns 0 on success, negative error code on failure.
  11172. */
  11173. int
  11174. intel_prepare_plane_fb(struct drm_plane *plane,
  11175. const struct drm_plane_state *new_state)
  11176. {
  11177. struct drm_device *dev = plane->dev;
  11178. struct drm_framebuffer *fb = new_state->fb;
  11179. struct intel_plane *intel_plane = to_intel_plane(plane);
  11180. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11181. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11182. int ret = 0;
  11183. if (!obj)
  11184. return 0;
  11185. mutex_lock(&dev->struct_mutex);
  11186. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11187. INTEL_INFO(dev)->cursor_needs_physical) {
  11188. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11189. ret = i915_gem_object_attach_phys(obj, align);
  11190. if (ret)
  11191. DRM_DEBUG_KMS("failed to attach phys object\n");
  11192. } else {
  11193. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11194. }
  11195. if (ret == 0)
  11196. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11197. mutex_unlock(&dev->struct_mutex);
  11198. return ret;
  11199. }
  11200. /**
  11201. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11202. * @plane: drm plane to clean up for
  11203. * @fb: old framebuffer that was on plane
  11204. *
  11205. * Cleans up a framebuffer that has just been removed from a plane.
  11206. */
  11207. void
  11208. intel_cleanup_plane_fb(struct drm_plane *plane,
  11209. const struct drm_plane_state *old_state)
  11210. {
  11211. struct drm_device *dev = plane->dev;
  11212. struct drm_i915_gem_object *obj = intel_fb_obj(old_state->fb);
  11213. if (!obj)
  11214. return;
  11215. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11216. !INTEL_INFO(dev)->cursor_needs_physical) {
  11217. mutex_lock(&dev->struct_mutex);
  11218. intel_unpin_fb_obj(old_state->fb, old_state);
  11219. mutex_unlock(&dev->struct_mutex);
  11220. }
  11221. }
  11222. int
  11223. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11224. {
  11225. int max_scale;
  11226. struct drm_device *dev;
  11227. struct drm_i915_private *dev_priv;
  11228. int crtc_clock, cdclk;
  11229. if (!intel_crtc || !crtc_state)
  11230. return DRM_PLANE_HELPER_NO_SCALING;
  11231. dev = intel_crtc->base.dev;
  11232. dev_priv = dev->dev_private;
  11233. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11234. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11235. if (!crtc_clock || !cdclk)
  11236. return DRM_PLANE_HELPER_NO_SCALING;
  11237. /*
  11238. * skl max scale is lower of:
  11239. * close to 3 but not 3, -1 is for that purpose
  11240. * or
  11241. * cdclk/crtc_clock
  11242. */
  11243. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11244. return max_scale;
  11245. }
  11246. static int
  11247. intel_check_primary_plane(struct drm_plane *plane,
  11248. struct intel_crtc_state *crtc_state,
  11249. struct intel_plane_state *state)
  11250. {
  11251. struct drm_crtc *crtc = state->base.crtc;
  11252. struct drm_framebuffer *fb = state->base.fb;
  11253. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11254. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11255. bool can_position = false;
  11256. /* use scaler when colorkey is not required */
  11257. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11258. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11259. min_scale = 1;
  11260. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11261. can_position = true;
  11262. }
  11263. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11264. &state->dst, &state->clip,
  11265. min_scale, max_scale,
  11266. can_position, true,
  11267. &state->visible);
  11268. }
  11269. static void
  11270. intel_commit_primary_plane(struct drm_plane *plane,
  11271. struct intel_plane_state *state)
  11272. {
  11273. struct drm_crtc *crtc = state->base.crtc;
  11274. struct drm_framebuffer *fb = state->base.fb;
  11275. struct drm_device *dev = plane->dev;
  11276. struct drm_i915_private *dev_priv = dev->dev_private;
  11277. struct intel_crtc *intel_crtc;
  11278. struct drm_rect *src = &state->src;
  11279. crtc = crtc ? crtc : plane->crtc;
  11280. intel_crtc = to_intel_crtc(crtc);
  11281. plane->fb = fb;
  11282. crtc->x = src->x1 >> 16;
  11283. crtc->y = src->y1 >> 16;
  11284. if (!crtc->state->active)
  11285. return;
  11286. dev_priv->display.update_primary_plane(crtc, fb,
  11287. state->src.x1 >> 16,
  11288. state->src.y1 >> 16);
  11289. }
  11290. static void
  11291. intel_disable_primary_plane(struct drm_plane *plane,
  11292. struct drm_crtc *crtc)
  11293. {
  11294. struct drm_device *dev = plane->dev;
  11295. struct drm_i915_private *dev_priv = dev->dev_private;
  11296. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11297. }
  11298. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11299. struct drm_crtc_state *old_crtc_state)
  11300. {
  11301. struct drm_device *dev = crtc->dev;
  11302. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11303. struct intel_crtc_state *old_intel_state =
  11304. to_intel_crtc_state(old_crtc_state);
  11305. bool modeset = needs_modeset(crtc->state);
  11306. if (intel_crtc->atomic.update_wm_pre)
  11307. intel_update_watermarks(crtc);
  11308. /* Perform vblank evasion around commit operation */
  11309. if (crtc->state->active)
  11310. intel_pipe_update_start(intel_crtc);
  11311. if (modeset)
  11312. return;
  11313. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11314. intel_update_pipe_config(intel_crtc, old_intel_state);
  11315. else if (INTEL_INFO(dev)->gen >= 9)
  11316. skl_detach_scalers(intel_crtc);
  11317. }
  11318. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11319. struct drm_crtc_state *old_crtc_state)
  11320. {
  11321. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11322. if (crtc->state->active)
  11323. intel_pipe_update_end(intel_crtc);
  11324. }
  11325. /**
  11326. * intel_plane_destroy - destroy a plane
  11327. * @plane: plane to destroy
  11328. *
  11329. * Common destruction function for all types of planes (primary, cursor,
  11330. * sprite).
  11331. */
  11332. void intel_plane_destroy(struct drm_plane *plane)
  11333. {
  11334. struct intel_plane *intel_plane = to_intel_plane(plane);
  11335. drm_plane_cleanup(plane);
  11336. kfree(intel_plane);
  11337. }
  11338. const struct drm_plane_funcs intel_plane_funcs = {
  11339. .update_plane = drm_atomic_helper_update_plane,
  11340. .disable_plane = drm_atomic_helper_disable_plane,
  11341. .destroy = intel_plane_destroy,
  11342. .set_property = drm_atomic_helper_plane_set_property,
  11343. .atomic_get_property = intel_plane_atomic_get_property,
  11344. .atomic_set_property = intel_plane_atomic_set_property,
  11345. .atomic_duplicate_state = intel_plane_duplicate_state,
  11346. .atomic_destroy_state = intel_plane_destroy_state,
  11347. };
  11348. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11349. int pipe)
  11350. {
  11351. struct intel_plane *primary;
  11352. struct intel_plane_state *state;
  11353. const uint32_t *intel_primary_formats;
  11354. unsigned int num_formats;
  11355. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11356. if (primary == NULL)
  11357. return NULL;
  11358. state = intel_create_plane_state(&primary->base);
  11359. if (!state) {
  11360. kfree(primary);
  11361. return NULL;
  11362. }
  11363. primary->base.state = &state->base;
  11364. primary->can_scale = false;
  11365. primary->max_downscale = 1;
  11366. if (INTEL_INFO(dev)->gen >= 9) {
  11367. primary->can_scale = true;
  11368. state->scaler_id = -1;
  11369. }
  11370. primary->pipe = pipe;
  11371. primary->plane = pipe;
  11372. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11373. primary->check_plane = intel_check_primary_plane;
  11374. primary->commit_plane = intel_commit_primary_plane;
  11375. primary->disable_plane = intel_disable_primary_plane;
  11376. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11377. primary->plane = !pipe;
  11378. if (INTEL_INFO(dev)->gen >= 9) {
  11379. intel_primary_formats = skl_primary_formats;
  11380. num_formats = ARRAY_SIZE(skl_primary_formats);
  11381. } else if (INTEL_INFO(dev)->gen >= 4) {
  11382. intel_primary_formats = i965_primary_formats;
  11383. num_formats = ARRAY_SIZE(i965_primary_formats);
  11384. } else {
  11385. intel_primary_formats = i8xx_primary_formats;
  11386. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11387. }
  11388. drm_universal_plane_init(dev, &primary->base, 0,
  11389. &intel_plane_funcs,
  11390. intel_primary_formats, num_formats,
  11391. DRM_PLANE_TYPE_PRIMARY);
  11392. if (INTEL_INFO(dev)->gen >= 4)
  11393. intel_create_rotation_property(dev, primary);
  11394. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11395. return &primary->base;
  11396. }
  11397. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11398. {
  11399. if (!dev->mode_config.rotation_property) {
  11400. unsigned long flags = BIT(DRM_ROTATE_0) |
  11401. BIT(DRM_ROTATE_180);
  11402. if (INTEL_INFO(dev)->gen >= 9)
  11403. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11404. dev->mode_config.rotation_property =
  11405. drm_mode_create_rotation_property(dev, flags);
  11406. }
  11407. if (dev->mode_config.rotation_property)
  11408. drm_object_attach_property(&plane->base.base,
  11409. dev->mode_config.rotation_property,
  11410. plane->base.state->rotation);
  11411. }
  11412. static int
  11413. intel_check_cursor_plane(struct drm_plane *plane,
  11414. struct intel_crtc_state *crtc_state,
  11415. struct intel_plane_state *state)
  11416. {
  11417. struct drm_crtc *crtc = crtc_state->base.crtc;
  11418. struct drm_framebuffer *fb = state->base.fb;
  11419. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11420. unsigned stride;
  11421. int ret;
  11422. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11423. &state->dst, &state->clip,
  11424. DRM_PLANE_HELPER_NO_SCALING,
  11425. DRM_PLANE_HELPER_NO_SCALING,
  11426. true, true, &state->visible);
  11427. if (ret)
  11428. return ret;
  11429. /* if we want to turn off the cursor ignore width and height */
  11430. if (!obj)
  11431. return 0;
  11432. /* Check for which cursor types we support */
  11433. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11434. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11435. state->base.crtc_w, state->base.crtc_h);
  11436. return -EINVAL;
  11437. }
  11438. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11439. if (obj->base.size < stride * state->base.crtc_h) {
  11440. DRM_DEBUG_KMS("buffer is too small\n");
  11441. return -ENOMEM;
  11442. }
  11443. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11444. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11445. return -EINVAL;
  11446. }
  11447. return 0;
  11448. }
  11449. static void
  11450. intel_disable_cursor_plane(struct drm_plane *plane,
  11451. struct drm_crtc *crtc)
  11452. {
  11453. intel_crtc_update_cursor(crtc, false);
  11454. }
  11455. static void
  11456. intel_commit_cursor_plane(struct drm_plane *plane,
  11457. struct intel_plane_state *state)
  11458. {
  11459. struct drm_crtc *crtc = state->base.crtc;
  11460. struct drm_device *dev = plane->dev;
  11461. struct intel_crtc *intel_crtc;
  11462. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11463. uint32_t addr;
  11464. crtc = crtc ? crtc : plane->crtc;
  11465. intel_crtc = to_intel_crtc(crtc);
  11466. if (intel_crtc->cursor_bo == obj)
  11467. goto update;
  11468. if (!obj)
  11469. addr = 0;
  11470. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11471. addr = i915_gem_obj_ggtt_offset(obj);
  11472. else
  11473. addr = obj->phys_handle->busaddr;
  11474. intel_crtc->cursor_addr = addr;
  11475. intel_crtc->cursor_bo = obj;
  11476. update:
  11477. if (crtc->state->active)
  11478. intel_crtc_update_cursor(crtc, state->visible);
  11479. }
  11480. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11481. int pipe)
  11482. {
  11483. struct intel_plane *cursor;
  11484. struct intel_plane_state *state;
  11485. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11486. if (cursor == NULL)
  11487. return NULL;
  11488. state = intel_create_plane_state(&cursor->base);
  11489. if (!state) {
  11490. kfree(cursor);
  11491. return NULL;
  11492. }
  11493. cursor->base.state = &state->base;
  11494. cursor->can_scale = false;
  11495. cursor->max_downscale = 1;
  11496. cursor->pipe = pipe;
  11497. cursor->plane = pipe;
  11498. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11499. cursor->check_plane = intel_check_cursor_plane;
  11500. cursor->commit_plane = intel_commit_cursor_plane;
  11501. cursor->disable_plane = intel_disable_cursor_plane;
  11502. drm_universal_plane_init(dev, &cursor->base, 0,
  11503. &intel_plane_funcs,
  11504. intel_cursor_formats,
  11505. ARRAY_SIZE(intel_cursor_formats),
  11506. DRM_PLANE_TYPE_CURSOR);
  11507. if (INTEL_INFO(dev)->gen >= 4) {
  11508. if (!dev->mode_config.rotation_property)
  11509. dev->mode_config.rotation_property =
  11510. drm_mode_create_rotation_property(dev,
  11511. BIT(DRM_ROTATE_0) |
  11512. BIT(DRM_ROTATE_180));
  11513. if (dev->mode_config.rotation_property)
  11514. drm_object_attach_property(&cursor->base.base,
  11515. dev->mode_config.rotation_property,
  11516. state->base.rotation);
  11517. }
  11518. if (INTEL_INFO(dev)->gen >=9)
  11519. state->scaler_id = -1;
  11520. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11521. return &cursor->base;
  11522. }
  11523. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11524. struct intel_crtc_state *crtc_state)
  11525. {
  11526. int i;
  11527. struct intel_scaler *intel_scaler;
  11528. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11529. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11530. intel_scaler = &scaler_state->scalers[i];
  11531. intel_scaler->in_use = 0;
  11532. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11533. }
  11534. scaler_state->scaler_id = -1;
  11535. }
  11536. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11537. {
  11538. struct drm_i915_private *dev_priv = dev->dev_private;
  11539. struct intel_crtc *intel_crtc;
  11540. struct intel_crtc_state *crtc_state = NULL;
  11541. struct drm_plane *primary = NULL;
  11542. struct drm_plane *cursor = NULL;
  11543. int i, ret;
  11544. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11545. if (intel_crtc == NULL)
  11546. return;
  11547. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11548. if (!crtc_state)
  11549. goto fail;
  11550. intel_crtc->config = crtc_state;
  11551. intel_crtc->base.state = &crtc_state->base;
  11552. crtc_state->base.crtc = &intel_crtc->base;
  11553. /* initialize shared scalers */
  11554. if (INTEL_INFO(dev)->gen >= 9) {
  11555. if (pipe == PIPE_C)
  11556. intel_crtc->num_scalers = 1;
  11557. else
  11558. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11559. skl_init_scalers(dev, intel_crtc, crtc_state);
  11560. }
  11561. primary = intel_primary_plane_create(dev, pipe);
  11562. if (!primary)
  11563. goto fail;
  11564. cursor = intel_cursor_plane_create(dev, pipe);
  11565. if (!cursor)
  11566. goto fail;
  11567. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11568. cursor, &intel_crtc_funcs);
  11569. if (ret)
  11570. goto fail;
  11571. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11572. for (i = 0; i < 256; i++) {
  11573. intel_crtc->lut_r[i] = i;
  11574. intel_crtc->lut_g[i] = i;
  11575. intel_crtc->lut_b[i] = i;
  11576. }
  11577. /*
  11578. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11579. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11580. */
  11581. intel_crtc->pipe = pipe;
  11582. intel_crtc->plane = pipe;
  11583. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11584. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11585. intel_crtc->plane = !pipe;
  11586. }
  11587. intel_crtc->cursor_base = ~0;
  11588. intel_crtc->cursor_cntl = ~0;
  11589. intel_crtc->cursor_size = ~0;
  11590. intel_crtc->wm.cxsr_allowed = true;
  11591. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11592. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11593. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11594. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11595. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11596. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11597. return;
  11598. fail:
  11599. if (primary)
  11600. drm_plane_cleanup(primary);
  11601. if (cursor)
  11602. drm_plane_cleanup(cursor);
  11603. kfree(crtc_state);
  11604. kfree(intel_crtc);
  11605. }
  11606. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11607. {
  11608. struct drm_encoder *encoder = connector->base.encoder;
  11609. struct drm_device *dev = connector->base.dev;
  11610. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11611. if (!encoder || WARN_ON(!encoder->crtc))
  11612. return INVALID_PIPE;
  11613. return to_intel_crtc(encoder->crtc)->pipe;
  11614. }
  11615. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11616. struct drm_file *file)
  11617. {
  11618. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11619. struct drm_crtc *drmmode_crtc;
  11620. struct intel_crtc *crtc;
  11621. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11622. if (!drmmode_crtc) {
  11623. DRM_ERROR("no such CRTC id\n");
  11624. return -ENOENT;
  11625. }
  11626. crtc = to_intel_crtc(drmmode_crtc);
  11627. pipe_from_crtc_id->pipe = crtc->pipe;
  11628. return 0;
  11629. }
  11630. static int intel_encoder_clones(struct intel_encoder *encoder)
  11631. {
  11632. struct drm_device *dev = encoder->base.dev;
  11633. struct intel_encoder *source_encoder;
  11634. int index_mask = 0;
  11635. int entry = 0;
  11636. for_each_intel_encoder(dev, source_encoder) {
  11637. if (encoders_cloneable(encoder, source_encoder))
  11638. index_mask |= (1 << entry);
  11639. entry++;
  11640. }
  11641. return index_mask;
  11642. }
  11643. static bool has_edp_a(struct drm_device *dev)
  11644. {
  11645. struct drm_i915_private *dev_priv = dev->dev_private;
  11646. if (!IS_MOBILE(dev))
  11647. return false;
  11648. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11649. return false;
  11650. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11651. return false;
  11652. return true;
  11653. }
  11654. static bool intel_crt_present(struct drm_device *dev)
  11655. {
  11656. struct drm_i915_private *dev_priv = dev->dev_private;
  11657. if (INTEL_INFO(dev)->gen >= 9)
  11658. return false;
  11659. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11660. return false;
  11661. if (IS_CHERRYVIEW(dev))
  11662. return false;
  11663. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11664. return false;
  11665. return true;
  11666. }
  11667. static void intel_setup_outputs(struct drm_device *dev)
  11668. {
  11669. struct drm_i915_private *dev_priv = dev->dev_private;
  11670. struct intel_encoder *encoder;
  11671. bool dpd_is_edp = false;
  11672. intel_lvds_init(dev);
  11673. if (intel_crt_present(dev))
  11674. intel_crt_init(dev);
  11675. if (IS_BROXTON(dev)) {
  11676. /*
  11677. * FIXME: Broxton doesn't support port detection via the
  11678. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11679. * detect the ports.
  11680. */
  11681. intel_ddi_init(dev, PORT_A);
  11682. intel_ddi_init(dev, PORT_B);
  11683. intel_ddi_init(dev, PORT_C);
  11684. } else if (HAS_DDI(dev)) {
  11685. int found;
  11686. /*
  11687. * Haswell uses DDI functions to detect digital outputs.
  11688. * On SKL pre-D0 the strap isn't connected, so we assume
  11689. * it's there.
  11690. */
  11691. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11692. /* WaIgnoreDDIAStrap: skl */
  11693. if (found || IS_SKYLAKE(dev))
  11694. intel_ddi_init(dev, PORT_A);
  11695. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11696. * register */
  11697. found = I915_READ(SFUSE_STRAP);
  11698. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11699. intel_ddi_init(dev, PORT_B);
  11700. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11701. intel_ddi_init(dev, PORT_C);
  11702. if (found & SFUSE_STRAP_DDID_DETECTED)
  11703. intel_ddi_init(dev, PORT_D);
  11704. /*
  11705. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11706. */
  11707. if (IS_SKYLAKE(dev) &&
  11708. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11709. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11710. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11711. intel_ddi_init(dev, PORT_E);
  11712. } else if (HAS_PCH_SPLIT(dev)) {
  11713. int found;
  11714. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11715. if (has_edp_a(dev))
  11716. intel_dp_init(dev, DP_A, PORT_A);
  11717. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11718. /* PCH SDVOB multiplex with HDMIB */
  11719. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11720. if (!found)
  11721. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11722. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11723. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11724. }
  11725. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11726. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11727. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11728. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11729. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11730. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11731. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11732. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11733. } else if (IS_VALLEYVIEW(dev)) {
  11734. /*
  11735. * The DP_DETECTED bit is the latched state of the DDC
  11736. * SDA pin at boot. However since eDP doesn't require DDC
  11737. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11738. * eDP ports may have been muxed to an alternate function.
  11739. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11740. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11741. * detect eDP ports.
  11742. */
  11743. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  11744. !intel_dp_is_edp(dev, PORT_B))
  11745. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  11746. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  11747. intel_dp_is_edp(dev, PORT_B))
  11748. intel_dp_init(dev, VLV_DP_B, PORT_B);
  11749. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  11750. !intel_dp_is_edp(dev, PORT_C))
  11751. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  11752. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  11753. intel_dp_is_edp(dev, PORT_C))
  11754. intel_dp_init(dev, VLV_DP_C, PORT_C);
  11755. if (IS_CHERRYVIEW(dev)) {
  11756. /* eDP not supported on port D, so don't check VBT */
  11757. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  11758. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  11759. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  11760. intel_dp_init(dev, CHV_DP_D, PORT_D);
  11761. }
  11762. intel_dsi_init(dev);
  11763. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11764. bool found = false;
  11765. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11766. DRM_DEBUG_KMS("probing SDVOB\n");
  11767. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11768. if (!found && IS_G4X(dev)) {
  11769. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11770. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11771. }
  11772. if (!found && IS_G4X(dev))
  11773. intel_dp_init(dev, DP_B, PORT_B);
  11774. }
  11775. /* Before G4X SDVOC doesn't have its own detect register */
  11776. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11777. DRM_DEBUG_KMS("probing SDVOC\n");
  11778. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11779. }
  11780. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11781. if (IS_G4X(dev)) {
  11782. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11783. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11784. }
  11785. if (IS_G4X(dev))
  11786. intel_dp_init(dev, DP_C, PORT_C);
  11787. }
  11788. if (IS_G4X(dev) &&
  11789. (I915_READ(DP_D) & DP_DETECTED))
  11790. intel_dp_init(dev, DP_D, PORT_D);
  11791. } else if (IS_GEN2(dev))
  11792. intel_dvo_init(dev);
  11793. if (SUPPORTS_TV(dev))
  11794. intel_tv_init(dev);
  11795. intel_psr_init(dev);
  11796. for_each_intel_encoder(dev, encoder) {
  11797. encoder->base.possible_crtcs = encoder->crtc_mask;
  11798. encoder->base.possible_clones =
  11799. intel_encoder_clones(encoder);
  11800. }
  11801. intel_init_pch_refclk(dev);
  11802. drm_helper_move_panel_connectors_to_head(dev);
  11803. }
  11804. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11805. {
  11806. struct drm_device *dev = fb->dev;
  11807. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11808. drm_framebuffer_cleanup(fb);
  11809. mutex_lock(&dev->struct_mutex);
  11810. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11811. drm_gem_object_unreference(&intel_fb->obj->base);
  11812. mutex_unlock(&dev->struct_mutex);
  11813. kfree(intel_fb);
  11814. }
  11815. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11816. struct drm_file *file,
  11817. unsigned int *handle)
  11818. {
  11819. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11820. struct drm_i915_gem_object *obj = intel_fb->obj;
  11821. return drm_gem_handle_create(file, &obj->base, handle);
  11822. }
  11823. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11824. struct drm_file *file,
  11825. unsigned flags, unsigned color,
  11826. struct drm_clip_rect *clips,
  11827. unsigned num_clips)
  11828. {
  11829. struct drm_device *dev = fb->dev;
  11830. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11831. struct drm_i915_gem_object *obj = intel_fb->obj;
  11832. mutex_lock(&dev->struct_mutex);
  11833. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11834. mutex_unlock(&dev->struct_mutex);
  11835. return 0;
  11836. }
  11837. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11838. .destroy = intel_user_framebuffer_destroy,
  11839. .create_handle = intel_user_framebuffer_create_handle,
  11840. .dirty = intel_user_framebuffer_dirty,
  11841. };
  11842. static
  11843. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11844. uint32_t pixel_format)
  11845. {
  11846. u32 gen = INTEL_INFO(dev)->gen;
  11847. if (gen >= 9) {
  11848. /* "The stride in bytes must not exceed the of the size of 8K
  11849. * pixels and 32K bytes."
  11850. */
  11851. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11852. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11853. return 32*1024;
  11854. } else if (gen >= 4) {
  11855. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11856. return 16*1024;
  11857. else
  11858. return 32*1024;
  11859. } else if (gen >= 3) {
  11860. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11861. return 8*1024;
  11862. else
  11863. return 16*1024;
  11864. } else {
  11865. /* XXX DSPC is limited to 4k tiled */
  11866. return 8*1024;
  11867. }
  11868. }
  11869. static int intel_framebuffer_init(struct drm_device *dev,
  11870. struct intel_framebuffer *intel_fb,
  11871. struct drm_mode_fb_cmd2 *mode_cmd,
  11872. struct drm_i915_gem_object *obj)
  11873. {
  11874. unsigned int aligned_height;
  11875. int ret;
  11876. u32 pitch_limit, stride_alignment;
  11877. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11878. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11879. /* Enforce that fb modifier and tiling mode match, but only for
  11880. * X-tiled. This is needed for FBC. */
  11881. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11882. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11883. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11884. return -EINVAL;
  11885. }
  11886. } else {
  11887. if (obj->tiling_mode == I915_TILING_X)
  11888. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11889. else if (obj->tiling_mode == I915_TILING_Y) {
  11890. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11891. return -EINVAL;
  11892. }
  11893. }
  11894. /* Passed in modifier sanity checking. */
  11895. switch (mode_cmd->modifier[0]) {
  11896. case I915_FORMAT_MOD_Y_TILED:
  11897. case I915_FORMAT_MOD_Yf_TILED:
  11898. if (INTEL_INFO(dev)->gen < 9) {
  11899. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11900. mode_cmd->modifier[0]);
  11901. return -EINVAL;
  11902. }
  11903. case DRM_FORMAT_MOD_NONE:
  11904. case I915_FORMAT_MOD_X_TILED:
  11905. break;
  11906. default:
  11907. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11908. mode_cmd->modifier[0]);
  11909. return -EINVAL;
  11910. }
  11911. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11912. mode_cmd->pixel_format);
  11913. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11914. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11915. mode_cmd->pitches[0], stride_alignment);
  11916. return -EINVAL;
  11917. }
  11918. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11919. mode_cmd->pixel_format);
  11920. if (mode_cmd->pitches[0] > pitch_limit) {
  11921. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11922. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11923. "tiled" : "linear",
  11924. mode_cmd->pitches[0], pitch_limit);
  11925. return -EINVAL;
  11926. }
  11927. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11928. mode_cmd->pitches[0] != obj->stride) {
  11929. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11930. mode_cmd->pitches[0], obj->stride);
  11931. return -EINVAL;
  11932. }
  11933. /* Reject formats not supported by any plane early. */
  11934. switch (mode_cmd->pixel_format) {
  11935. case DRM_FORMAT_C8:
  11936. case DRM_FORMAT_RGB565:
  11937. case DRM_FORMAT_XRGB8888:
  11938. case DRM_FORMAT_ARGB8888:
  11939. break;
  11940. case DRM_FORMAT_XRGB1555:
  11941. if (INTEL_INFO(dev)->gen > 3) {
  11942. DRM_DEBUG("unsupported pixel format: %s\n",
  11943. drm_get_format_name(mode_cmd->pixel_format));
  11944. return -EINVAL;
  11945. }
  11946. break;
  11947. case DRM_FORMAT_ABGR8888:
  11948. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  11949. DRM_DEBUG("unsupported pixel format: %s\n",
  11950. drm_get_format_name(mode_cmd->pixel_format));
  11951. return -EINVAL;
  11952. }
  11953. break;
  11954. case DRM_FORMAT_XBGR8888:
  11955. case DRM_FORMAT_XRGB2101010:
  11956. case DRM_FORMAT_XBGR2101010:
  11957. if (INTEL_INFO(dev)->gen < 4) {
  11958. DRM_DEBUG("unsupported pixel format: %s\n",
  11959. drm_get_format_name(mode_cmd->pixel_format));
  11960. return -EINVAL;
  11961. }
  11962. break;
  11963. case DRM_FORMAT_ABGR2101010:
  11964. if (!IS_VALLEYVIEW(dev)) {
  11965. DRM_DEBUG("unsupported pixel format: %s\n",
  11966. drm_get_format_name(mode_cmd->pixel_format));
  11967. return -EINVAL;
  11968. }
  11969. break;
  11970. case DRM_FORMAT_YUYV:
  11971. case DRM_FORMAT_UYVY:
  11972. case DRM_FORMAT_YVYU:
  11973. case DRM_FORMAT_VYUY:
  11974. if (INTEL_INFO(dev)->gen < 5) {
  11975. DRM_DEBUG("unsupported pixel format: %s\n",
  11976. drm_get_format_name(mode_cmd->pixel_format));
  11977. return -EINVAL;
  11978. }
  11979. break;
  11980. default:
  11981. DRM_DEBUG("unsupported pixel format: %s\n",
  11982. drm_get_format_name(mode_cmd->pixel_format));
  11983. return -EINVAL;
  11984. }
  11985. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11986. if (mode_cmd->offsets[0] != 0)
  11987. return -EINVAL;
  11988. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11989. mode_cmd->pixel_format,
  11990. mode_cmd->modifier[0]);
  11991. /* FIXME drm helper for size checks (especially planar formats)? */
  11992. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11993. return -EINVAL;
  11994. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11995. intel_fb->obj = obj;
  11996. intel_fb->obj->framebuffer_references++;
  11997. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11998. if (ret) {
  11999. DRM_ERROR("framebuffer init failed %d\n", ret);
  12000. return ret;
  12001. }
  12002. return 0;
  12003. }
  12004. static struct drm_framebuffer *
  12005. intel_user_framebuffer_create(struct drm_device *dev,
  12006. struct drm_file *filp,
  12007. struct drm_mode_fb_cmd2 *mode_cmd)
  12008. {
  12009. struct drm_i915_gem_object *obj;
  12010. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12011. mode_cmd->handles[0]));
  12012. if (&obj->base == NULL)
  12013. return ERR_PTR(-ENOENT);
  12014. return intel_framebuffer_create(dev, mode_cmd, obj);
  12015. }
  12016. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12017. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12018. {
  12019. }
  12020. #endif
  12021. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12022. .fb_create = intel_user_framebuffer_create,
  12023. .output_poll_changed = intel_fbdev_output_poll_changed,
  12024. .atomic_check = intel_atomic_check,
  12025. .atomic_commit = intel_atomic_commit,
  12026. .atomic_state_alloc = intel_atomic_state_alloc,
  12027. .atomic_state_clear = intel_atomic_state_clear,
  12028. };
  12029. /* Set up chip specific display functions */
  12030. static void intel_init_display(struct drm_device *dev)
  12031. {
  12032. struct drm_i915_private *dev_priv = dev->dev_private;
  12033. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12034. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12035. else if (IS_CHERRYVIEW(dev))
  12036. dev_priv->display.find_dpll = chv_find_best_dpll;
  12037. else if (IS_VALLEYVIEW(dev))
  12038. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12039. else if (IS_PINEVIEW(dev))
  12040. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12041. else
  12042. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12043. if (INTEL_INFO(dev)->gen >= 9) {
  12044. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12045. dev_priv->display.get_initial_plane_config =
  12046. skylake_get_initial_plane_config;
  12047. dev_priv->display.crtc_compute_clock =
  12048. haswell_crtc_compute_clock;
  12049. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12050. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12051. dev_priv->display.update_primary_plane =
  12052. skylake_update_primary_plane;
  12053. } else if (HAS_DDI(dev)) {
  12054. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12055. dev_priv->display.get_initial_plane_config =
  12056. ironlake_get_initial_plane_config;
  12057. dev_priv->display.crtc_compute_clock =
  12058. haswell_crtc_compute_clock;
  12059. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12060. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12061. dev_priv->display.update_primary_plane =
  12062. ironlake_update_primary_plane;
  12063. } else if (HAS_PCH_SPLIT(dev)) {
  12064. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12065. dev_priv->display.get_initial_plane_config =
  12066. ironlake_get_initial_plane_config;
  12067. dev_priv->display.crtc_compute_clock =
  12068. ironlake_crtc_compute_clock;
  12069. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12070. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12071. dev_priv->display.update_primary_plane =
  12072. ironlake_update_primary_plane;
  12073. } else if (IS_VALLEYVIEW(dev)) {
  12074. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12075. dev_priv->display.get_initial_plane_config =
  12076. i9xx_get_initial_plane_config;
  12077. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12078. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12079. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12080. dev_priv->display.update_primary_plane =
  12081. i9xx_update_primary_plane;
  12082. } else {
  12083. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12084. dev_priv->display.get_initial_plane_config =
  12085. i9xx_get_initial_plane_config;
  12086. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12087. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12088. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12089. dev_priv->display.update_primary_plane =
  12090. i9xx_update_primary_plane;
  12091. }
  12092. /* Returns the core display clock speed */
  12093. if (IS_SKYLAKE(dev))
  12094. dev_priv->display.get_display_clock_speed =
  12095. skylake_get_display_clock_speed;
  12096. else if (IS_BROXTON(dev))
  12097. dev_priv->display.get_display_clock_speed =
  12098. broxton_get_display_clock_speed;
  12099. else if (IS_BROADWELL(dev))
  12100. dev_priv->display.get_display_clock_speed =
  12101. broadwell_get_display_clock_speed;
  12102. else if (IS_HASWELL(dev))
  12103. dev_priv->display.get_display_clock_speed =
  12104. haswell_get_display_clock_speed;
  12105. else if (IS_VALLEYVIEW(dev))
  12106. dev_priv->display.get_display_clock_speed =
  12107. valleyview_get_display_clock_speed;
  12108. else if (IS_GEN5(dev))
  12109. dev_priv->display.get_display_clock_speed =
  12110. ilk_get_display_clock_speed;
  12111. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12112. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12113. dev_priv->display.get_display_clock_speed =
  12114. i945_get_display_clock_speed;
  12115. else if (IS_GM45(dev))
  12116. dev_priv->display.get_display_clock_speed =
  12117. gm45_get_display_clock_speed;
  12118. else if (IS_CRESTLINE(dev))
  12119. dev_priv->display.get_display_clock_speed =
  12120. i965gm_get_display_clock_speed;
  12121. else if (IS_PINEVIEW(dev))
  12122. dev_priv->display.get_display_clock_speed =
  12123. pnv_get_display_clock_speed;
  12124. else if (IS_G33(dev) || IS_G4X(dev))
  12125. dev_priv->display.get_display_clock_speed =
  12126. g33_get_display_clock_speed;
  12127. else if (IS_I915G(dev))
  12128. dev_priv->display.get_display_clock_speed =
  12129. i915_get_display_clock_speed;
  12130. else if (IS_I945GM(dev) || IS_845G(dev))
  12131. dev_priv->display.get_display_clock_speed =
  12132. i9xx_misc_get_display_clock_speed;
  12133. else if (IS_PINEVIEW(dev))
  12134. dev_priv->display.get_display_clock_speed =
  12135. pnv_get_display_clock_speed;
  12136. else if (IS_I915GM(dev))
  12137. dev_priv->display.get_display_clock_speed =
  12138. i915gm_get_display_clock_speed;
  12139. else if (IS_I865G(dev))
  12140. dev_priv->display.get_display_clock_speed =
  12141. i865_get_display_clock_speed;
  12142. else if (IS_I85X(dev))
  12143. dev_priv->display.get_display_clock_speed =
  12144. i85x_get_display_clock_speed;
  12145. else { /* 830 */
  12146. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12147. dev_priv->display.get_display_clock_speed =
  12148. i830_get_display_clock_speed;
  12149. }
  12150. if (IS_GEN5(dev)) {
  12151. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12152. } else if (IS_GEN6(dev)) {
  12153. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12154. } else if (IS_IVYBRIDGE(dev)) {
  12155. /* FIXME: detect B0+ stepping and use auto training */
  12156. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12157. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12158. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12159. if (IS_BROADWELL(dev)) {
  12160. dev_priv->display.modeset_commit_cdclk =
  12161. broadwell_modeset_commit_cdclk;
  12162. dev_priv->display.modeset_calc_cdclk =
  12163. broadwell_modeset_calc_cdclk;
  12164. }
  12165. } else if (IS_VALLEYVIEW(dev)) {
  12166. dev_priv->display.modeset_commit_cdclk =
  12167. valleyview_modeset_commit_cdclk;
  12168. dev_priv->display.modeset_calc_cdclk =
  12169. valleyview_modeset_calc_cdclk;
  12170. } else if (IS_BROXTON(dev)) {
  12171. dev_priv->display.modeset_commit_cdclk =
  12172. broxton_modeset_commit_cdclk;
  12173. dev_priv->display.modeset_calc_cdclk =
  12174. broxton_modeset_calc_cdclk;
  12175. }
  12176. switch (INTEL_INFO(dev)->gen) {
  12177. case 2:
  12178. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12179. break;
  12180. case 3:
  12181. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12182. break;
  12183. case 4:
  12184. case 5:
  12185. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12186. break;
  12187. case 6:
  12188. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12189. break;
  12190. case 7:
  12191. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12192. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12193. break;
  12194. case 9:
  12195. /* Drop through - unsupported since execlist only. */
  12196. default:
  12197. /* Default just returns -ENODEV to indicate unsupported */
  12198. dev_priv->display.queue_flip = intel_default_queue_flip;
  12199. }
  12200. intel_panel_init_backlight_funcs(dev);
  12201. mutex_init(&dev_priv->pps_mutex);
  12202. }
  12203. /*
  12204. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12205. * resume, or other times. This quirk makes sure that's the case for
  12206. * affected systems.
  12207. */
  12208. static void quirk_pipea_force(struct drm_device *dev)
  12209. {
  12210. struct drm_i915_private *dev_priv = dev->dev_private;
  12211. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12212. DRM_INFO("applying pipe a force quirk\n");
  12213. }
  12214. static void quirk_pipeb_force(struct drm_device *dev)
  12215. {
  12216. struct drm_i915_private *dev_priv = dev->dev_private;
  12217. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12218. DRM_INFO("applying pipe b force quirk\n");
  12219. }
  12220. /*
  12221. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12222. */
  12223. static void quirk_ssc_force_disable(struct drm_device *dev)
  12224. {
  12225. struct drm_i915_private *dev_priv = dev->dev_private;
  12226. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12227. DRM_INFO("applying lvds SSC disable quirk\n");
  12228. }
  12229. /*
  12230. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12231. * brightness value
  12232. */
  12233. static void quirk_invert_brightness(struct drm_device *dev)
  12234. {
  12235. struct drm_i915_private *dev_priv = dev->dev_private;
  12236. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12237. DRM_INFO("applying inverted panel brightness quirk\n");
  12238. }
  12239. /* Some VBT's incorrectly indicate no backlight is present */
  12240. static void quirk_backlight_present(struct drm_device *dev)
  12241. {
  12242. struct drm_i915_private *dev_priv = dev->dev_private;
  12243. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12244. DRM_INFO("applying backlight present quirk\n");
  12245. }
  12246. struct intel_quirk {
  12247. int device;
  12248. int subsystem_vendor;
  12249. int subsystem_device;
  12250. void (*hook)(struct drm_device *dev);
  12251. };
  12252. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12253. struct intel_dmi_quirk {
  12254. void (*hook)(struct drm_device *dev);
  12255. const struct dmi_system_id (*dmi_id_list)[];
  12256. };
  12257. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12258. {
  12259. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12260. return 1;
  12261. }
  12262. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12263. {
  12264. .dmi_id_list = &(const struct dmi_system_id[]) {
  12265. {
  12266. .callback = intel_dmi_reverse_brightness,
  12267. .ident = "NCR Corporation",
  12268. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12269. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12270. },
  12271. },
  12272. { } /* terminating entry */
  12273. },
  12274. .hook = quirk_invert_brightness,
  12275. },
  12276. };
  12277. static struct intel_quirk intel_quirks[] = {
  12278. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12279. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12280. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12281. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12282. /* 830 needs to leave pipe A & dpll A up */
  12283. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12284. /* 830 needs to leave pipe B & dpll B up */
  12285. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12286. /* Lenovo U160 cannot use SSC on LVDS */
  12287. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12288. /* Sony Vaio Y cannot use SSC on LVDS */
  12289. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12290. /* Acer Aspire 5734Z must invert backlight brightness */
  12291. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12292. /* Acer/eMachines G725 */
  12293. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12294. /* Acer/eMachines e725 */
  12295. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12296. /* Acer/Packard Bell NCL20 */
  12297. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12298. /* Acer Aspire 4736Z */
  12299. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12300. /* Acer Aspire 5336 */
  12301. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12302. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12303. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12304. /* Acer C720 Chromebook (Core i3 4005U) */
  12305. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12306. /* Apple Macbook 2,1 (Core 2 T7400) */
  12307. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12308. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12309. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12310. /* HP Chromebook 14 (Celeron 2955U) */
  12311. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12312. /* Dell Chromebook 11 */
  12313. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12314. };
  12315. static void intel_init_quirks(struct drm_device *dev)
  12316. {
  12317. struct pci_dev *d = dev->pdev;
  12318. int i;
  12319. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12320. struct intel_quirk *q = &intel_quirks[i];
  12321. if (d->device == q->device &&
  12322. (d->subsystem_vendor == q->subsystem_vendor ||
  12323. q->subsystem_vendor == PCI_ANY_ID) &&
  12324. (d->subsystem_device == q->subsystem_device ||
  12325. q->subsystem_device == PCI_ANY_ID))
  12326. q->hook(dev);
  12327. }
  12328. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12329. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12330. intel_dmi_quirks[i].hook(dev);
  12331. }
  12332. }
  12333. /* Disable the VGA plane that we never use */
  12334. static void i915_disable_vga(struct drm_device *dev)
  12335. {
  12336. struct drm_i915_private *dev_priv = dev->dev_private;
  12337. u8 sr1;
  12338. u32 vga_reg = i915_vgacntrl_reg(dev);
  12339. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12340. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12341. outb(SR01, VGA_SR_INDEX);
  12342. sr1 = inb(VGA_SR_DATA);
  12343. outb(sr1 | 1<<5, VGA_SR_DATA);
  12344. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12345. udelay(300);
  12346. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12347. POSTING_READ(vga_reg);
  12348. }
  12349. void intel_modeset_init_hw(struct drm_device *dev)
  12350. {
  12351. intel_update_cdclk(dev);
  12352. intel_prepare_ddi(dev);
  12353. intel_init_clock_gating(dev);
  12354. intel_enable_gt_powersave(dev);
  12355. }
  12356. void intel_modeset_init(struct drm_device *dev)
  12357. {
  12358. struct drm_i915_private *dev_priv = dev->dev_private;
  12359. int sprite, ret;
  12360. enum pipe pipe;
  12361. struct intel_crtc *crtc;
  12362. drm_mode_config_init(dev);
  12363. dev->mode_config.min_width = 0;
  12364. dev->mode_config.min_height = 0;
  12365. dev->mode_config.preferred_depth = 24;
  12366. dev->mode_config.prefer_shadow = 1;
  12367. dev->mode_config.allow_fb_modifiers = true;
  12368. dev->mode_config.funcs = &intel_mode_funcs;
  12369. intel_init_quirks(dev);
  12370. intel_init_pm(dev);
  12371. if (INTEL_INFO(dev)->num_pipes == 0)
  12372. return;
  12373. /*
  12374. * There may be no VBT; and if the BIOS enabled SSC we can
  12375. * just keep using it to avoid unnecessary flicker. Whereas if the
  12376. * BIOS isn't using it, don't assume it will work even if the VBT
  12377. * indicates as much.
  12378. */
  12379. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12380. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12381. DREF_SSC1_ENABLE);
  12382. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12383. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12384. bios_lvds_use_ssc ? "en" : "dis",
  12385. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12386. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12387. }
  12388. }
  12389. intel_init_display(dev);
  12390. intel_init_audio(dev);
  12391. if (IS_GEN2(dev)) {
  12392. dev->mode_config.max_width = 2048;
  12393. dev->mode_config.max_height = 2048;
  12394. } else if (IS_GEN3(dev)) {
  12395. dev->mode_config.max_width = 4096;
  12396. dev->mode_config.max_height = 4096;
  12397. } else {
  12398. dev->mode_config.max_width = 8192;
  12399. dev->mode_config.max_height = 8192;
  12400. }
  12401. if (IS_845G(dev) || IS_I865G(dev)) {
  12402. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12403. dev->mode_config.cursor_height = 1023;
  12404. } else if (IS_GEN2(dev)) {
  12405. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12406. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12407. } else {
  12408. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12409. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12410. }
  12411. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12412. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12413. INTEL_INFO(dev)->num_pipes,
  12414. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12415. for_each_pipe(dev_priv, pipe) {
  12416. intel_crtc_init(dev, pipe);
  12417. for_each_sprite(dev_priv, pipe, sprite) {
  12418. ret = intel_plane_init(dev, pipe, sprite);
  12419. if (ret)
  12420. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12421. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12422. }
  12423. }
  12424. intel_shared_dpll_init(dev);
  12425. /* Just disable it once at startup */
  12426. i915_disable_vga(dev);
  12427. intel_setup_outputs(dev);
  12428. /* Just in case the BIOS is doing something questionable. */
  12429. intel_fbc_disable(dev_priv);
  12430. drm_modeset_lock_all(dev);
  12431. intel_modeset_setup_hw_state(dev);
  12432. drm_modeset_unlock_all(dev);
  12433. for_each_intel_crtc(dev, crtc) {
  12434. struct intel_initial_plane_config plane_config = {};
  12435. if (!crtc->active)
  12436. continue;
  12437. /*
  12438. * Note that reserving the BIOS fb up front prevents us
  12439. * from stuffing other stolen allocations like the ring
  12440. * on top. This prevents some ugliness at boot time, and
  12441. * can even allow for smooth boot transitions if the BIOS
  12442. * fb is large enough for the active pipe configuration.
  12443. */
  12444. dev_priv->display.get_initial_plane_config(crtc,
  12445. &plane_config);
  12446. /*
  12447. * If the fb is shared between multiple heads, we'll
  12448. * just get the first one.
  12449. */
  12450. intel_find_initial_plane_obj(crtc, &plane_config);
  12451. }
  12452. }
  12453. static void intel_enable_pipe_a(struct drm_device *dev)
  12454. {
  12455. struct intel_connector *connector;
  12456. struct drm_connector *crt = NULL;
  12457. struct intel_load_detect_pipe load_detect_temp;
  12458. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12459. /* We can't just switch on the pipe A, we need to set things up with a
  12460. * proper mode and output configuration. As a gross hack, enable pipe A
  12461. * by enabling the load detect pipe once. */
  12462. for_each_intel_connector(dev, connector) {
  12463. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12464. crt = &connector->base;
  12465. break;
  12466. }
  12467. }
  12468. if (!crt)
  12469. return;
  12470. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12471. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12472. }
  12473. static bool
  12474. intel_check_plane_mapping(struct intel_crtc *crtc)
  12475. {
  12476. struct drm_device *dev = crtc->base.dev;
  12477. struct drm_i915_private *dev_priv = dev->dev_private;
  12478. u32 reg, val;
  12479. if (INTEL_INFO(dev)->num_pipes == 1)
  12480. return true;
  12481. reg = DSPCNTR(!crtc->plane);
  12482. val = I915_READ(reg);
  12483. if ((val & DISPLAY_PLANE_ENABLE) &&
  12484. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12485. return false;
  12486. return true;
  12487. }
  12488. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12489. {
  12490. struct drm_device *dev = crtc->base.dev;
  12491. struct intel_encoder *encoder;
  12492. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12493. return true;
  12494. return false;
  12495. }
  12496. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12497. {
  12498. struct drm_device *dev = crtc->base.dev;
  12499. struct drm_i915_private *dev_priv = dev->dev_private;
  12500. u32 reg;
  12501. /* Clear any frame start delays used for debugging left by the BIOS */
  12502. reg = PIPECONF(crtc->config->cpu_transcoder);
  12503. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12504. /* restore vblank interrupts to correct state */
  12505. drm_crtc_vblank_reset(&crtc->base);
  12506. if (crtc->active) {
  12507. struct intel_plane *plane;
  12508. drm_crtc_vblank_on(&crtc->base);
  12509. /* Disable everything but the primary plane */
  12510. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12511. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12512. continue;
  12513. plane->disable_plane(&plane->base, &crtc->base);
  12514. }
  12515. }
  12516. /* We need to sanitize the plane -> pipe mapping first because this will
  12517. * disable the crtc (and hence change the state) if it is wrong. Note
  12518. * that gen4+ has a fixed plane -> pipe mapping. */
  12519. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12520. bool plane;
  12521. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12522. crtc->base.base.id);
  12523. /* Pipe has the wrong plane attached and the plane is active.
  12524. * Temporarily change the plane mapping and disable everything
  12525. * ... */
  12526. plane = crtc->plane;
  12527. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12528. crtc->plane = !plane;
  12529. intel_crtc_disable_noatomic(&crtc->base);
  12530. crtc->plane = plane;
  12531. }
  12532. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12533. crtc->pipe == PIPE_A && !crtc->active) {
  12534. /* BIOS forgot to enable pipe A, this mostly happens after
  12535. * resume. Force-enable the pipe to fix this, the update_dpms
  12536. * call below we restore the pipe to the right state, but leave
  12537. * the required bits on. */
  12538. intel_enable_pipe_a(dev);
  12539. }
  12540. /* Adjust the state of the output pipe according to whether we
  12541. * have active connectors/encoders. */
  12542. if (!intel_crtc_has_encoders(crtc))
  12543. intel_crtc_disable_noatomic(&crtc->base);
  12544. if (crtc->active != crtc->base.state->active) {
  12545. struct intel_encoder *encoder;
  12546. /* This can happen either due to bugs in the get_hw_state
  12547. * functions or because of calls to intel_crtc_disable_noatomic,
  12548. * or because the pipe is force-enabled due to the
  12549. * pipe A quirk. */
  12550. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12551. crtc->base.base.id,
  12552. crtc->base.state->enable ? "enabled" : "disabled",
  12553. crtc->active ? "enabled" : "disabled");
  12554. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12555. crtc->base.state->active = crtc->active;
  12556. crtc->base.enabled = crtc->active;
  12557. /* Because we only establish the connector -> encoder ->
  12558. * crtc links if something is active, this means the
  12559. * crtc is now deactivated. Break the links. connector
  12560. * -> encoder links are only establish when things are
  12561. * actually up, hence no need to break them. */
  12562. WARN_ON(crtc->active);
  12563. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12564. encoder->base.crtc = NULL;
  12565. }
  12566. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12567. /*
  12568. * We start out with underrun reporting disabled to avoid races.
  12569. * For correct bookkeeping mark this on active crtcs.
  12570. *
  12571. * Also on gmch platforms we dont have any hardware bits to
  12572. * disable the underrun reporting. Which means we need to start
  12573. * out with underrun reporting disabled also on inactive pipes,
  12574. * since otherwise we'll complain about the garbage we read when
  12575. * e.g. coming up after runtime pm.
  12576. *
  12577. * No protection against concurrent access is required - at
  12578. * worst a fifo underrun happens which also sets this to false.
  12579. */
  12580. crtc->cpu_fifo_underrun_disabled = true;
  12581. crtc->pch_fifo_underrun_disabled = true;
  12582. }
  12583. }
  12584. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12585. {
  12586. struct intel_connector *connector;
  12587. struct drm_device *dev = encoder->base.dev;
  12588. bool active = false;
  12589. /* We need to check both for a crtc link (meaning that the
  12590. * encoder is active and trying to read from a pipe) and the
  12591. * pipe itself being active. */
  12592. bool has_active_crtc = encoder->base.crtc &&
  12593. to_intel_crtc(encoder->base.crtc)->active;
  12594. for_each_intel_connector(dev, connector) {
  12595. if (connector->base.encoder != &encoder->base)
  12596. continue;
  12597. active = true;
  12598. break;
  12599. }
  12600. if (active && !has_active_crtc) {
  12601. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12602. encoder->base.base.id,
  12603. encoder->base.name);
  12604. /* Connector is active, but has no active pipe. This is
  12605. * fallout from our resume register restoring. Disable
  12606. * the encoder manually again. */
  12607. if (encoder->base.crtc) {
  12608. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12609. encoder->base.base.id,
  12610. encoder->base.name);
  12611. encoder->disable(encoder);
  12612. if (encoder->post_disable)
  12613. encoder->post_disable(encoder);
  12614. }
  12615. encoder->base.crtc = NULL;
  12616. /* Inconsistent output/port/pipe state happens presumably due to
  12617. * a bug in one of the get_hw_state functions. Or someplace else
  12618. * in our code, like the register restore mess on resume. Clamp
  12619. * things to off as a safer default. */
  12620. for_each_intel_connector(dev, connector) {
  12621. if (connector->encoder != encoder)
  12622. continue;
  12623. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12624. connector->base.encoder = NULL;
  12625. }
  12626. }
  12627. /* Enabled encoders without active connectors will be fixed in
  12628. * the crtc fixup. */
  12629. }
  12630. void i915_redisable_vga_power_on(struct drm_device *dev)
  12631. {
  12632. struct drm_i915_private *dev_priv = dev->dev_private;
  12633. u32 vga_reg = i915_vgacntrl_reg(dev);
  12634. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12635. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12636. i915_disable_vga(dev);
  12637. }
  12638. }
  12639. void i915_redisable_vga(struct drm_device *dev)
  12640. {
  12641. struct drm_i915_private *dev_priv = dev->dev_private;
  12642. /* This function can be called both from intel_modeset_setup_hw_state or
  12643. * at a very early point in our resume sequence, where the power well
  12644. * structures are not yet restored. Since this function is at a very
  12645. * paranoid "someone might have enabled VGA while we were not looking"
  12646. * level, just check if the power well is enabled instead of trying to
  12647. * follow the "don't touch the power well if we don't need it" policy
  12648. * the rest of the driver uses. */
  12649. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12650. return;
  12651. i915_redisable_vga_power_on(dev);
  12652. }
  12653. static bool primary_get_hw_state(struct intel_plane *plane)
  12654. {
  12655. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12656. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12657. }
  12658. /* FIXME read out full plane state for all planes */
  12659. static void readout_plane_state(struct intel_crtc *crtc)
  12660. {
  12661. struct drm_plane *primary = crtc->base.primary;
  12662. struct intel_plane_state *plane_state =
  12663. to_intel_plane_state(primary->state);
  12664. plane_state->visible =
  12665. primary_get_hw_state(to_intel_plane(primary));
  12666. if (plane_state->visible)
  12667. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  12668. }
  12669. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12670. {
  12671. struct drm_i915_private *dev_priv = dev->dev_private;
  12672. enum pipe pipe;
  12673. struct intel_crtc *crtc;
  12674. struct intel_encoder *encoder;
  12675. struct intel_connector *connector;
  12676. int i;
  12677. for_each_intel_crtc(dev, crtc) {
  12678. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12679. memset(crtc->config, 0, sizeof(*crtc->config));
  12680. crtc->config->base.crtc = &crtc->base;
  12681. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12682. crtc->config);
  12683. crtc->base.state->active = crtc->active;
  12684. crtc->base.enabled = crtc->active;
  12685. readout_plane_state(crtc);
  12686. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12687. crtc->base.base.id,
  12688. crtc->active ? "enabled" : "disabled");
  12689. }
  12690. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12691. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12692. pll->on = pll->get_hw_state(dev_priv, pll,
  12693. &pll->config.hw_state);
  12694. pll->active = 0;
  12695. pll->config.crtc_mask = 0;
  12696. for_each_intel_crtc(dev, crtc) {
  12697. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12698. pll->active++;
  12699. pll->config.crtc_mask |= 1 << crtc->pipe;
  12700. }
  12701. }
  12702. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12703. pll->name, pll->config.crtc_mask, pll->on);
  12704. if (pll->config.crtc_mask)
  12705. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12706. }
  12707. for_each_intel_encoder(dev, encoder) {
  12708. pipe = 0;
  12709. if (encoder->get_hw_state(encoder, &pipe)) {
  12710. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12711. encoder->base.crtc = &crtc->base;
  12712. encoder->get_config(encoder, crtc->config);
  12713. } else {
  12714. encoder->base.crtc = NULL;
  12715. }
  12716. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12717. encoder->base.base.id,
  12718. encoder->base.name,
  12719. encoder->base.crtc ? "enabled" : "disabled",
  12720. pipe_name(pipe));
  12721. }
  12722. for_each_intel_connector(dev, connector) {
  12723. if (connector->get_hw_state(connector)) {
  12724. connector->base.dpms = DRM_MODE_DPMS_ON;
  12725. connector->base.encoder = &connector->encoder->base;
  12726. } else {
  12727. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12728. connector->base.encoder = NULL;
  12729. }
  12730. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12731. connector->base.base.id,
  12732. connector->base.name,
  12733. connector->base.encoder ? "enabled" : "disabled");
  12734. }
  12735. for_each_intel_crtc(dev, crtc) {
  12736. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12737. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12738. if (crtc->base.state->active) {
  12739. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12740. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12741. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12742. /*
  12743. * The initial mode needs to be set in order to keep
  12744. * the atomic core happy. It wants a valid mode if the
  12745. * crtc's enabled, so we do the above call.
  12746. *
  12747. * At this point some state updated by the connectors
  12748. * in their ->detect() callback has not run yet, so
  12749. * no recalculation can be done yet.
  12750. *
  12751. * Even if we could do a recalculation and modeset
  12752. * right now it would cause a double modeset if
  12753. * fbdev or userspace chooses a different initial mode.
  12754. *
  12755. * If that happens, someone indicated they wanted a
  12756. * mode change, which means it's safe to do a full
  12757. * recalculation.
  12758. */
  12759. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12760. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12761. update_scanline_offset(crtc);
  12762. }
  12763. }
  12764. }
  12765. /* Scan out the current hw modeset state,
  12766. * and sanitizes it to the current state
  12767. */
  12768. static void
  12769. intel_modeset_setup_hw_state(struct drm_device *dev)
  12770. {
  12771. struct drm_i915_private *dev_priv = dev->dev_private;
  12772. enum pipe pipe;
  12773. struct intel_crtc *crtc;
  12774. struct intel_encoder *encoder;
  12775. int i;
  12776. intel_modeset_readout_hw_state(dev);
  12777. /* HW state is read out, now we need to sanitize this mess. */
  12778. for_each_intel_encoder(dev, encoder) {
  12779. intel_sanitize_encoder(encoder);
  12780. }
  12781. for_each_pipe(dev_priv, pipe) {
  12782. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12783. intel_sanitize_crtc(crtc);
  12784. intel_dump_pipe_config(crtc, crtc->config,
  12785. "[setup_hw_state]");
  12786. }
  12787. intel_modeset_update_connector_atomic_state(dev);
  12788. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12789. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12790. if (!pll->on || pll->active)
  12791. continue;
  12792. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12793. pll->disable(dev_priv, pll);
  12794. pll->on = false;
  12795. }
  12796. if (IS_VALLEYVIEW(dev))
  12797. vlv_wm_get_hw_state(dev);
  12798. else if (IS_GEN9(dev))
  12799. skl_wm_get_hw_state(dev);
  12800. else if (HAS_PCH_SPLIT(dev))
  12801. ilk_wm_get_hw_state(dev);
  12802. for_each_intel_crtc(dev, crtc) {
  12803. unsigned long put_domains;
  12804. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  12805. if (WARN_ON(put_domains))
  12806. modeset_put_power_domains(dev_priv, put_domains);
  12807. }
  12808. intel_display_set_init_power(dev_priv, false);
  12809. }
  12810. void intel_display_resume(struct drm_device *dev)
  12811. {
  12812. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  12813. struct intel_connector *conn;
  12814. struct intel_plane *plane;
  12815. struct drm_crtc *crtc;
  12816. int ret;
  12817. if (!state)
  12818. return;
  12819. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12820. /* preserve complete old state, including dpll */
  12821. intel_atomic_get_shared_dpll_state(state);
  12822. for_each_crtc(dev, crtc) {
  12823. struct drm_crtc_state *crtc_state =
  12824. drm_atomic_get_crtc_state(state, crtc);
  12825. ret = PTR_ERR_OR_ZERO(crtc_state);
  12826. if (ret)
  12827. goto err;
  12828. /* force a restore */
  12829. crtc_state->mode_changed = true;
  12830. }
  12831. for_each_intel_plane(dev, plane) {
  12832. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  12833. if (ret)
  12834. goto err;
  12835. }
  12836. for_each_intel_connector(dev, conn) {
  12837. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  12838. if (ret)
  12839. goto err;
  12840. }
  12841. intel_modeset_setup_hw_state(dev);
  12842. i915_redisable_vga(dev);
  12843. ret = drm_atomic_commit(state);
  12844. if (!ret)
  12845. return;
  12846. err:
  12847. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12848. drm_atomic_state_free(state);
  12849. }
  12850. void intel_modeset_gem_init(struct drm_device *dev)
  12851. {
  12852. struct drm_crtc *c;
  12853. struct drm_i915_gem_object *obj;
  12854. int ret;
  12855. mutex_lock(&dev->struct_mutex);
  12856. intel_init_gt_powersave(dev);
  12857. mutex_unlock(&dev->struct_mutex);
  12858. intel_modeset_init_hw(dev);
  12859. intel_setup_overlay(dev);
  12860. /*
  12861. * Make sure any fbs we allocated at startup are properly
  12862. * pinned & fenced. When we do the allocation it's too early
  12863. * for this.
  12864. */
  12865. for_each_crtc(dev, c) {
  12866. obj = intel_fb_obj(c->primary->fb);
  12867. if (obj == NULL)
  12868. continue;
  12869. mutex_lock(&dev->struct_mutex);
  12870. ret = intel_pin_and_fence_fb_obj(c->primary,
  12871. c->primary->fb,
  12872. c->primary->state,
  12873. NULL, NULL);
  12874. mutex_unlock(&dev->struct_mutex);
  12875. if (ret) {
  12876. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12877. to_intel_crtc(c)->pipe);
  12878. drm_framebuffer_unreference(c->primary->fb);
  12879. c->primary->fb = NULL;
  12880. c->primary->crtc = c->primary->state->crtc = NULL;
  12881. update_state_fb(c->primary);
  12882. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12883. }
  12884. }
  12885. intel_backlight_register(dev);
  12886. }
  12887. void intel_connector_unregister(struct intel_connector *intel_connector)
  12888. {
  12889. struct drm_connector *connector = &intel_connector->base;
  12890. intel_panel_destroy_backlight(connector);
  12891. drm_connector_unregister(connector);
  12892. }
  12893. void intel_modeset_cleanup(struct drm_device *dev)
  12894. {
  12895. struct drm_i915_private *dev_priv = dev->dev_private;
  12896. struct drm_connector *connector;
  12897. intel_disable_gt_powersave(dev);
  12898. intel_backlight_unregister(dev);
  12899. /*
  12900. * Interrupts and polling as the first thing to avoid creating havoc.
  12901. * Too much stuff here (turning of connectors, ...) would
  12902. * experience fancy races otherwise.
  12903. */
  12904. intel_irq_uninstall(dev_priv);
  12905. /*
  12906. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12907. * poll handlers. Hence disable polling after hpd handling is shut down.
  12908. */
  12909. drm_kms_helper_poll_fini(dev);
  12910. intel_unregister_dsm_handler();
  12911. intel_fbc_disable(dev_priv);
  12912. /* flush any delayed tasks or pending work */
  12913. flush_scheduled_work();
  12914. /* destroy the backlight and sysfs files before encoders/connectors */
  12915. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12916. struct intel_connector *intel_connector;
  12917. intel_connector = to_intel_connector(connector);
  12918. intel_connector->unregister(intel_connector);
  12919. }
  12920. drm_mode_config_cleanup(dev);
  12921. intel_cleanup_overlay(dev);
  12922. mutex_lock(&dev->struct_mutex);
  12923. intel_cleanup_gt_powersave(dev);
  12924. mutex_unlock(&dev->struct_mutex);
  12925. }
  12926. /*
  12927. * Return which encoder is currently attached for connector.
  12928. */
  12929. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12930. {
  12931. return &intel_attached_encoder(connector)->base;
  12932. }
  12933. void intel_connector_attach_encoder(struct intel_connector *connector,
  12934. struct intel_encoder *encoder)
  12935. {
  12936. connector->encoder = encoder;
  12937. drm_mode_connector_attach_encoder(&connector->base,
  12938. &encoder->base);
  12939. }
  12940. /*
  12941. * set vga decode state - true == enable VGA decode
  12942. */
  12943. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12944. {
  12945. struct drm_i915_private *dev_priv = dev->dev_private;
  12946. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12947. u16 gmch_ctrl;
  12948. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12949. DRM_ERROR("failed to read control word\n");
  12950. return -EIO;
  12951. }
  12952. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12953. return 0;
  12954. if (state)
  12955. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12956. else
  12957. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12958. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12959. DRM_ERROR("failed to write control word\n");
  12960. return -EIO;
  12961. }
  12962. return 0;
  12963. }
  12964. struct intel_display_error_state {
  12965. u32 power_well_driver;
  12966. int num_transcoders;
  12967. struct intel_cursor_error_state {
  12968. u32 control;
  12969. u32 position;
  12970. u32 base;
  12971. u32 size;
  12972. } cursor[I915_MAX_PIPES];
  12973. struct intel_pipe_error_state {
  12974. bool power_domain_on;
  12975. u32 source;
  12976. u32 stat;
  12977. } pipe[I915_MAX_PIPES];
  12978. struct intel_plane_error_state {
  12979. u32 control;
  12980. u32 stride;
  12981. u32 size;
  12982. u32 pos;
  12983. u32 addr;
  12984. u32 surface;
  12985. u32 tile_offset;
  12986. } plane[I915_MAX_PIPES];
  12987. struct intel_transcoder_error_state {
  12988. bool power_domain_on;
  12989. enum transcoder cpu_transcoder;
  12990. u32 conf;
  12991. u32 htotal;
  12992. u32 hblank;
  12993. u32 hsync;
  12994. u32 vtotal;
  12995. u32 vblank;
  12996. u32 vsync;
  12997. } transcoder[4];
  12998. };
  12999. struct intel_display_error_state *
  13000. intel_display_capture_error_state(struct drm_device *dev)
  13001. {
  13002. struct drm_i915_private *dev_priv = dev->dev_private;
  13003. struct intel_display_error_state *error;
  13004. int transcoders[] = {
  13005. TRANSCODER_A,
  13006. TRANSCODER_B,
  13007. TRANSCODER_C,
  13008. TRANSCODER_EDP,
  13009. };
  13010. int i;
  13011. if (INTEL_INFO(dev)->num_pipes == 0)
  13012. return NULL;
  13013. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13014. if (error == NULL)
  13015. return NULL;
  13016. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13017. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13018. for_each_pipe(dev_priv, i) {
  13019. error->pipe[i].power_domain_on =
  13020. __intel_display_power_is_enabled(dev_priv,
  13021. POWER_DOMAIN_PIPE(i));
  13022. if (!error->pipe[i].power_domain_on)
  13023. continue;
  13024. error->cursor[i].control = I915_READ(CURCNTR(i));
  13025. error->cursor[i].position = I915_READ(CURPOS(i));
  13026. error->cursor[i].base = I915_READ(CURBASE(i));
  13027. error->plane[i].control = I915_READ(DSPCNTR(i));
  13028. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13029. if (INTEL_INFO(dev)->gen <= 3) {
  13030. error->plane[i].size = I915_READ(DSPSIZE(i));
  13031. error->plane[i].pos = I915_READ(DSPPOS(i));
  13032. }
  13033. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13034. error->plane[i].addr = I915_READ(DSPADDR(i));
  13035. if (INTEL_INFO(dev)->gen >= 4) {
  13036. error->plane[i].surface = I915_READ(DSPSURF(i));
  13037. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13038. }
  13039. error->pipe[i].source = I915_READ(PIPESRC(i));
  13040. if (HAS_GMCH_DISPLAY(dev))
  13041. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13042. }
  13043. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13044. if (HAS_DDI(dev_priv->dev))
  13045. error->num_transcoders++; /* Account for eDP. */
  13046. for (i = 0; i < error->num_transcoders; i++) {
  13047. enum transcoder cpu_transcoder = transcoders[i];
  13048. error->transcoder[i].power_domain_on =
  13049. __intel_display_power_is_enabled(dev_priv,
  13050. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13051. if (!error->transcoder[i].power_domain_on)
  13052. continue;
  13053. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13054. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13055. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13056. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13057. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13058. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13059. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13060. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13061. }
  13062. return error;
  13063. }
  13064. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13065. void
  13066. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13067. struct drm_device *dev,
  13068. struct intel_display_error_state *error)
  13069. {
  13070. struct drm_i915_private *dev_priv = dev->dev_private;
  13071. int i;
  13072. if (!error)
  13073. return;
  13074. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13075. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13076. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13077. error->power_well_driver);
  13078. for_each_pipe(dev_priv, i) {
  13079. err_printf(m, "Pipe [%d]:\n", i);
  13080. err_printf(m, " Power: %s\n",
  13081. error->pipe[i].power_domain_on ? "on" : "off");
  13082. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13083. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13084. err_printf(m, "Plane [%d]:\n", i);
  13085. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13086. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13087. if (INTEL_INFO(dev)->gen <= 3) {
  13088. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13089. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13090. }
  13091. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13092. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13093. if (INTEL_INFO(dev)->gen >= 4) {
  13094. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13095. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13096. }
  13097. err_printf(m, "Cursor [%d]:\n", i);
  13098. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13099. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13100. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13101. }
  13102. for (i = 0; i < error->num_transcoders; i++) {
  13103. err_printf(m, "CPU transcoder: %c\n",
  13104. transcoder_name(error->transcoder[i].cpu_transcoder));
  13105. err_printf(m, " Power: %s\n",
  13106. error->transcoder[i].power_domain_on ? "on" : "off");
  13107. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13108. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13109. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13110. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13111. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13112. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13113. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13114. }
  13115. }
  13116. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13117. {
  13118. struct intel_crtc *crtc;
  13119. for_each_intel_crtc(dev, crtc) {
  13120. struct intel_unpin_work *work;
  13121. spin_lock_irq(&dev->event_lock);
  13122. work = crtc->unpin_work;
  13123. if (work && work->event &&
  13124. work->event->base.file_priv == file) {
  13125. kfree(work->event);
  13126. work->event = NULL;
  13127. }
  13128. spin_unlock_irq(&dev->event_lock);
  13129. }
  13130. }