intel_display.c 431 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. /* Primary plane formats for gen <= 3 */
  47. static const uint32_t i8xx_primary_formats[] = {
  48. DRM_FORMAT_C8,
  49. DRM_FORMAT_RGB565,
  50. DRM_FORMAT_XRGB1555,
  51. DRM_FORMAT_XRGB8888,
  52. };
  53. /* Primary plane formats for gen >= 4 */
  54. static const uint32_t i965_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB8888,
  58. DRM_FORMAT_XBGR8888,
  59. DRM_FORMAT_XRGB2101010,
  60. DRM_FORMAT_XBGR2101010,
  61. };
  62. static const uint32_t skl_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_ARGB8888,
  68. DRM_FORMAT_ABGR8888,
  69. DRM_FORMAT_XRGB2101010,
  70. DRM_FORMAT_XBGR2101010,
  71. };
  72. /* Cursor formats */
  73. static const uint32_t intel_cursor_formats[] = {
  74. DRM_FORMAT_ARGB8888,
  75. };
  76. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  77. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  78. struct intel_crtc_state *pipe_config);
  79. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  80. struct intel_crtc_state *pipe_config);
  81. static int intel_framebuffer_init(struct drm_device *dev,
  82. struct intel_framebuffer *ifb,
  83. struct drm_mode_fb_cmd2 *mode_cmd,
  84. struct drm_i915_gem_object *obj);
  85. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  86. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  87. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  88. struct intel_link_m_n *m_n,
  89. struct intel_link_m_n *m2_n2);
  90. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  91. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  92. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  93. static void vlv_prepare_pll(struct intel_crtc *crtc,
  94. const struct intel_crtc_state *pipe_config);
  95. static void chv_prepare_pll(struct intel_crtc *crtc,
  96. const struct intel_crtc_state *pipe_config);
  97. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  98. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  99. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  100. struct intel_crtc_state *crtc_state);
  101. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  102. int num_connectors);
  103. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  104. typedef struct {
  105. int min, max;
  106. } intel_range_t;
  107. typedef struct {
  108. int dot_limit;
  109. int p2_slow, p2_fast;
  110. } intel_p2_t;
  111. typedef struct intel_limit intel_limit_t;
  112. struct intel_limit {
  113. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  114. intel_p2_t p2;
  115. };
  116. int
  117. intel_pch_rawclk(struct drm_device *dev)
  118. {
  119. struct drm_i915_private *dev_priv = dev->dev_private;
  120. WARN_ON(!HAS_PCH_SPLIT(dev));
  121. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  122. }
  123. static inline u32 /* units of 100MHz */
  124. intel_fdi_link_freq(struct drm_device *dev)
  125. {
  126. if (IS_GEN5(dev)) {
  127. struct drm_i915_private *dev_priv = dev->dev_private;
  128. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  129. } else
  130. return 27;
  131. }
  132. static const intel_limit_t intel_limits_i8xx_dac = {
  133. .dot = { .min = 25000, .max = 350000 },
  134. .vco = { .min = 908000, .max = 1512000 },
  135. .n = { .min = 2, .max = 16 },
  136. .m = { .min = 96, .max = 140 },
  137. .m1 = { .min = 18, .max = 26 },
  138. .m2 = { .min = 6, .max = 16 },
  139. .p = { .min = 4, .max = 128 },
  140. .p1 = { .min = 2, .max = 33 },
  141. .p2 = { .dot_limit = 165000,
  142. .p2_slow = 4, .p2_fast = 2 },
  143. };
  144. static const intel_limit_t intel_limits_i8xx_dvo = {
  145. .dot = { .min = 25000, .max = 350000 },
  146. .vco = { .min = 908000, .max = 1512000 },
  147. .n = { .min = 2, .max = 16 },
  148. .m = { .min = 96, .max = 140 },
  149. .m1 = { .min = 18, .max = 26 },
  150. .m2 = { .min = 6, .max = 16 },
  151. .p = { .min = 4, .max = 128 },
  152. .p1 = { .min = 2, .max = 33 },
  153. .p2 = { .dot_limit = 165000,
  154. .p2_slow = 4, .p2_fast = 4 },
  155. };
  156. static const intel_limit_t intel_limits_i8xx_lvds = {
  157. .dot = { .min = 25000, .max = 350000 },
  158. .vco = { .min = 908000, .max = 1512000 },
  159. .n = { .min = 2, .max = 16 },
  160. .m = { .min = 96, .max = 140 },
  161. .m1 = { .min = 18, .max = 26 },
  162. .m2 = { .min = 6, .max = 16 },
  163. .p = { .min = 4, .max = 128 },
  164. .p1 = { .min = 1, .max = 6 },
  165. .p2 = { .dot_limit = 165000,
  166. .p2_slow = 14, .p2_fast = 7 },
  167. };
  168. static const intel_limit_t intel_limits_i9xx_sdvo = {
  169. .dot = { .min = 20000, .max = 400000 },
  170. .vco = { .min = 1400000, .max = 2800000 },
  171. .n = { .min = 1, .max = 6 },
  172. .m = { .min = 70, .max = 120 },
  173. .m1 = { .min = 8, .max = 18 },
  174. .m2 = { .min = 3, .max = 7 },
  175. .p = { .min = 5, .max = 80 },
  176. .p1 = { .min = 1, .max = 8 },
  177. .p2 = { .dot_limit = 200000,
  178. .p2_slow = 10, .p2_fast = 5 },
  179. };
  180. static const intel_limit_t intel_limits_i9xx_lvds = {
  181. .dot = { .min = 20000, .max = 400000 },
  182. .vco = { .min = 1400000, .max = 2800000 },
  183. .n = { .min = 1, .max = 6 },
  184. .m = { .min = 70, .max = 120 },
  185. .m1 = { .min = 8, .max = 18 },
  186. .m2 = { .min = 3, .max = 7 },
  187. .p = { .min = 7, .max = 98 },
  188. .p1 = { .min = 1, .max = 8 },
  189. .p2 = { .dot_limit = 112000,
  190. .p2_slow = 14, .p2_fast = 7 },
  191. };
  192. static const intel_limit_t intel_limits_g4x_sdvo = {
  193. .dot = { .min = 25000, .max = 270000 },
  194. .vco = { .min = 1750000, .max = 3500000},
  195. .n = { .min = 1, .max = 4 },
  196. .m = { .min = 104, .max = 138 },
  197. .m1 = { .min = 17, .max = 23 },
  198. .m2 = { .min = 5, .max = 11 },
  199. .p = { .min = 10, .max = 30 },
  200. .p1 = { .min = 1, .max = 3},
  201. .p2 = { .dot_limit = 270000,
  202. .p2_slow = 10,
  203. .p2_fast = 10
  204. },
  205. };
  206. static const intel_limit_t intel_limits_g4x_hdmi = {
  207. .dot = { .min = 22000, .max = 400000 },
  208. .vco = { .min = 1750000, .max = 3500000},
  209. .n = { .min = 1, .max = 4 },
  210. .m = { .min = 104, .max = 138 },
  211. .m1 = { .min = 16, .max = 23 },
  212. .m2 = { .min = 5, .max = 11 },
  213. .p = { .min = 5, .max = 80 },
  214. .p1 = { .min = 1, .max = 8},
  215. .p2 = { .dot_limit = 165000,
  216. .p2_slow = 10, .p2_fast = 5 },
  217. };
  218. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  219. .dot = { .min = 20000, .max = 115000 },
  220. .vco = { .min = 1750000, .max = 3500000 },
  221. .n = { .min = 1, .max = 3 },
  222. .m = { .min = 104, .max = 138 },
  223. .m1 = { .min = 17, .max = 23 },
  224. .m2 = { .min = 5, .max = 11 },
  225. .p = { .min = 28, .max = 112 },
  226. .p1 = { .min = 2, .max = 8 },
  227. .p2 = { .dot_limit = 0,
  228. .p2_slow = 14, .p2_fast = 14
  229. },
  230. };
  231. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  232. .dot = { .min = 80000, .max = 224000 },
  233. .vco = { .min = 1750000, .max = 3500000 },
  234. .n = { .min = 1, .max = 3 },
  235. .m = { .min = 104, .max = 138 },
  236. .m1 = { .min = 17, .max = 23 },
  237. .m2 = { .min = 5, .max = 11 },
  238. .p = { .min = 14, .max = 42 },
  239. .p1 = { .min = 2, .max = 6 },
  240. .p2 = { .dot_limit = 0,
  241. .p2_slow = 7, .p2_fast = 7
  242. },
  243. };
  244. static const intel_limit_t intel_limits_pineview_sdvo = {
  245. .dot = { .min = 20000, .max = 400000},
  246. .vco = { .min = 1700000, .max = 3500000 },
  247. /* Pineview's Ncounter is a ring counter */
  248. .n = { .min = 3, .max = 6 },
  249. .m = { .min = 2, .max = 256 },
  250. /* Pineview only has one combined m divider, which we treat as m2. */
  251. .m1 = { .min = 0, .max = 0 },
  252. .m2 = { .min = 0, .max = 254 },
  253. .p = { .min = 5, .max = 80 },
  254. .p1 = { .min = 1, .max = 8 },
  255. .p2 = { .dot_limit = 200000,
  256. .p2_slow = 10, .p2_fast = 5 },
  257. };
  258. static const intel_limit_t intel_limits_pineview_lvds = {
  259. .dot = { .min = 20000, .max = 400000 },
  260. .vco = { .min = 1700000, .max = 3500000 },
  261. .n = { .min = 3, .max = 6 },
  262. .m = { .min = 2, .max = 256 },
  263. .m1 = { .min = 0, .max = 0 },
  264. .m2 = { .min = 0, .max = 254 },
  265. .p = { .min = 7, .max = 112 },
  266. .p1 = { .min = 1, .max = 8 },
  267. .p2 = { .dot_limit = 112000,
  268. .p2_slow = 14, .p2_fast = 14 },
  269. };
  270. /* Ironlake / Sandybridge
  271. *
  272. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  273. * the range value for them is (actual_value - 2).
  274. */
  275. static const intel_limit_t intel_limits_ironlake_dac = {
  276. .dot = { .min = 25000, .max = 350000 },
  277. .vco = { .min = 1760000, .max = 3510000 },
  278. .n = { .min = 1, .max = 5 },
  279. .m = { .min = 79, .max = 127 },
  280. .m1 = { .min = 12, .max = 22 },
  281. .m2 = { .min = 5, .max = 9 },
  282. .p = { .min = 5, .max = 80 },
  283. .p1 = { .min = 1, .max = 8 },
  284. .p2 = { .dot_limit = 225000,
  285. .p2_slow = 10, .p2_fast = 5 },
  286. };
  287. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  288. .dot = { .min = 25000, .max = 350000 },
  289. .vco = { .min = 1760000, .max = 3510000 },
  290. .n = { .min = 1, .max = 3 },
  291. .m = { .min = 79, .max = 118 },
  292. .m1 = { .min = 12, .max = 22 },
  293. .m2 = { .min = 5, .max = 9 },
  294. .p = { .min = 28, .max = 112 },
  295. .p1 = { .min = 2, .max = 8 },
  296. .p2 = { .dot_limit = 225000,
  297. .p2_slow = 14, .p2_fast = 14 },
  298. };
  299. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  300. .dot = { .min = 25000, .max = 350000 },
  301. .vco = { .min = 1760000, .max = 3510000 },
  302. .n = { .min = 1, .max = 3 },
  303. .m = { .min = 79, .max = 127 },
  304. .m1 = { .min = 12, .max = 22 },
  305. .m2 = { .min = 5, .max = 9 },
  306. .p = { .min = 14, .max = 56 },
  307. .p1 = { .min = 2, .max = 8 },
  308. .p2 = { .dot_limit = 225000,
  309. .p2_slow = 7, .p2_fast = 7 },
  310. };
  311. /* LVDS 100mhz refclk limits. */
  312. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  313. .dot = { .min = 25000, .max = 350000 },
  314. .vco = { .min = 1760000, .max = 3510000 },
  315. .n = { .min = 1, .max = 2 },
  316. .m = { .min = 79, .max = 126 },
  317. .m1 = { .min = 12, .max = 22 },
  318. .m2 = { .min = 5, .max = 9 },
  319. .p = { .min = 28, .max = 112 },
  320. .p1 = { .min = 2, .max = 8 },
  321. .p2 = { .dot_limit = 225000,
  322. .p2_slow = 14, .p2_fast = 14 },
  323. };
  324. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  325. .dot = { .min = 25000, .max = 350000 },
  326. .vco = { .min = 1760000, .max = 3510000 },
  327. .n = { .min = 1, .max = 3 },
  328. .m = { .min = 79, .max = 126 },
  329. .m1 = { .min = 12, .max = 22 },
  330. .m2 = { .min = 5, .max = 9 },
  331. .p = { .min = 14, .max = 42 },
  332. .p1 = { .min = 2, .max = 6 },
  333. .p2 = { .dot_limit = 225000,
  334. .p2_slow = 7, .p2_fast = 7 },
  335. };
  336. static const intel_limit_t intel_limits_vlv = {
  337. /*
  338. * These are the data rate limits (measured in fast clocks)
  339. * since those are the strictest limits we have. The fast
  340. * clock and actual rate limits are more relaxed, so checking
  341. * them would make no difference.
  342. */
  343. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  344. .vco = { .min = 4000000, .max = 6000000 },
  345. .n = { .min = 1, .max = 7 },
  346. .m1 = { .min = 2, .max = 3 },
  347. .m2 = { .min = 11, .max = 156 },
  348. .p1 = { .min = 2, .max = 3 },
  349. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  350. };
  351. static const intel_limit_t intel_limits_chv = {
  352. /*
  353. * These are the data rate limits (measured in fast clocks)
  354. * since those are the strictest limits we have. The fast
  355. * clock and actual rate limits are more relaxed, so checking
  356. * them would make no difference.
  357. */
  358. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  359. .vco = { .min = 4800000, .max = 6480000 },
  360. .n = { .min = 1, .max = 1 },
  361. .m1 = { .min = 2, .max = 2 },
  362. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  363. .p1 = { .min = 2, .max = 4 },
  364. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  365. };
  366. static const intel_limit_t intel_limits_bxt = {
  367. /* FIXME: find real dot limits */
  368. .dot = { .min = 0, .max = INT_MAX },
  369. .vco = { .min = 4800000, .max = 6700000 },
  370. .n = { .min = 1, .max = 1 },
  371. .m1 = { .min = 2, .max = 2 },
  372. /* FIXME: find real m2 limits */
  373. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  374. .p1 = { .min = 2, .max = 4 },
  375. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  376. };
  377. static bool
  378. needs_modeset(struct drm_crtc_state *state)
  379. {
  380. return drm_atomic_crtc_needs_modeset(state);
  381. }
  382. /**
  383. * Returns whether any output on the specified pipe is of the specified type
  384. */
  385. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  386. {
  387. struct drm_device *dev = crtc->base.dev;
  388. struct intel_encoder *encoder;
  389. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  390. if (encoder->type == type)
  391. return true;
  392. return false;
  393. }
  394. /**
  395. * Returns whether any output on the specified pipe will have the specified
  396. * type after a staged modeset is complete, i.e., the same as
  397. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  398. * encoder->crtc.
  399. */
  400. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  401. int type)
  402. {
  403. struct drm_atomic_state *state = crtc_state->base.state;
  404. struct drm_connector *connector;
  405. struct drm_connector_state *connector_state;
  406. struct intel_encoder *encoder;
  407. int i, num_connectors = 0;
  408. for_each_connector_in_state(state, connector, connector_state, i) {
  409. if (connector_state->crtc != crtc_state->base.crtc)
  410. continue;
  411. num_connectors++;
  412. encoder = to_intel_encoder(connector_state->best_encoder);
  413. if (encoder->type == type)
  414. return true;
  415. }
  416. WARN_ON(num_connectors == 0);
  417. return false;
  418. }
  419. static const intel_limit_t *
  420. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  421. {
  422. struct drm_device *dev = crtc_state->base.crtc->dev;
  423. const intel_limit_t *limit;
  424. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  425. if (intel_is_dual_link_lvds(dev)) {
  426. if (refclk == 100000)
  427. limit = &intel_limits_ironlake_dual_lvds_100m;
  428. else
  429. limit = &intel_limits_ironlake_dual_lvds;
  430. } else {
  431. if (refclk == 100000)
  432. limit = &intel_limits_ironlake_single_lvds_100m;
  433. else
  434. limit = &intel_limits_ironlake_single_lvds;
  435. }
  436. } else
  437. limit = &intel_limits_ironlake_dac;
  438. return limit;
  439. }
  440. static const intel_limit_t *
  441. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  442. {
  443. struct drm_device *dev = crtc_state->base.crtc->dev;
  444. const intel_limit_t *limit;
  445. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  446. if (intel_is_dual_link_lvds(dev))
  447. limit = &intel_limits_g4x_dual_channel_lvds;
  448. else
  449. limit = &intel_limits_g4x_single_channel_lvds;
  450. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  451. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  452. limit = &intel_limits_g4x_hdmi;
  453. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  454. limit = &intel_limits_g4x_sdvo;
  455. } else /* The option is for other outputs */
  456. limit = &intel_limits_i9xx_sdvo;
  457. return limit;
  458. }
  459. static const intel_limit_t *
  460. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  461. {
  462. struct drm_device *dev = crtc_state->base.crtc->dev;
  463. const intel_limit_t *limit;
  464. if (IS_BROXTON(dev))
  465. limit = &intel_limits_bxt;
  466. else if (HAS_PCH_SPLIT(dev))
  467. limit = intel_ironlake_limit(crtc_state, refclk);
  468. else if (IS_G4X(dev)) {
  469. limit = intel_g4x_limit(crtc_state);
  470. } else if (IS_PINEVIEW(dev)) {
  471. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  472. limit = &intel_limits_pineview_lvds;
  473. else
  474. limit = &intel_limits_pineview_sdvo;
  475. } else if (IS_CHERRYVIEW(dev)) {
  476. limit = &intel_limits_chv;
  477. } else if (IS_VALLEYVIEW(dev)) {
  478. limit = &intel_limits_vlv;
  479. } else if (!IS_GEN2(dev)) {
  480. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  481. limit = &intel_limits_i9xx_lvds;
  482. else
  483. limit = &intel_limits_i9xx_sdvo;
  484. } else {
  485. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  486. limit = &intel_limits_i8xx_lvds;
  487. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  488. limit = &intel_limits_i8xx_dvo;
  489. else
  490. limit = &intel_limits_i8xx_dac;
  491. }
  492. return limit;
  493. }
  494. /*
  495. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  496. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  497. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  498. * The helpers' return value is the rate of the clock that is fed to the
  499. * display engine's pipe which can be the above fast dot clock rate or a
  500. * divided-down version of it.
  501. */
  502. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  503. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  504. {
  505. clock->m = clock->m2 + 2;
  506. clock->p = clock->p1 * clock->p2;
  507. if (WARN_ON(clock->n == 0 || clock->p == 0))
  508. return 0;
  509. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  510. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  511. return clock->dot;
  512. }
  513. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  514. {
  515. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  516. }
  517. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  518. {
  519. clock->m = i9xx_dpll_compute_m(clock);
  520. clock->p = clock->p1 * clock->p2;
  521. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  522. return 0;
  523. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  524. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  525. return clock->dot;
  526. }
  527. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  528. {
  529. clock->m = clock->m1 * clock->m2;
  530. clock->p = clock->p1 * clock->p2;
  531. if (WARN_ON(clock->n == 0 || clock->p == 0))
  532. return 0;
  533. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  534. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  535. return clock->dot / 5;
  536. }
  537. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  538. {
  539. clock->m = clock->m1 * clock->m2;
  540. clock->p = clock->p1 * clock->p2;
  541. if (WARN_ON(clock->n == 0 || clock->p == 0))
  542. return 0;
  543. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  544. clock->n << 22);
  545. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  546. return clock->dot / 5;
  547. }
  548. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  549. /**
  550. * Returns whether the given set of divisors are valid for a given refclk with
  551. * the given connectors.
  552. */
  553. static bool intel_PLL_is_valid(struct drm_device *dev,
  554. const intel_limit_t *limit,
  555. const intel_clock_t *clock)
  556. {
  557. if (clock->n < limit->n.min || limit->n.max < clock->n)
  558. INTELPllInvalid("n out of range\n");
  559. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  560. INTELPllInvalid("p1 out of range\n");
  561. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  562. INTELPllInvalid("m2 out of range\n");
  563. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  564. INTELPllInvalid("m1 out of range\n");
  565. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  566. if (clock->m1 <= clock->m2)
  567. INTELPllInvalid("m1 <= m2\n");
  568. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  569. if (clock->p < limit->p.min || limit->p.max < clock->p)
  570. INTELPllInvalid("p out of range\n");
  571. if (clock->m < limit->m.min || limit->m.max < clock->m)
  572. INTELPllInvalid("m out of range\n");
  573. }
  574. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  575. INTELPllInvalid("vco out of range\n");
  576. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  577. * connector, etc., rather than just a single range.
  578. */
  579. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  580. INTELPllInvalid("dot out of range\n");
  581. return true;
  582. }
  583. static int
  584. i9xx_select_p2_div(const intel_limit_t *limit,
  585. const struct intel_crtc_state *crtc_state,
  586. int target)
  587. {
  588. struct drm_device *dev = crtc_state->base.crtc->dev;
  589. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  590. /*
  591. * For LVDS just rely on its current settings for dual-channel.
  592. * We haven't figured out how to reliably set up different
  593. * single/dual channel state, if we even can.
  594. */
  595. if (intel_is_dual_link_lvds(dev))
  596. return limit->p2.p2_fast;
  597. else
  598. return limit->p2.p2_slow;
  599. } else {
  600. if (target < limit->p2.dot_limit)
  601. return limit->p2.p2_slow;
  602. else
  603. return limit->p2.p2_fast;
  604. }
  605. }
  606. static bool
  607. i9xx_find_best_dpll(const intel_limit_t *limit,
  608. struct intel_crtc_state *crtc_state,
  609. int target, int refclk, intel_clock_t *match_clock,
  610. intel_clock_t *best_clock)
  611. {
  612. struct drm_device *dev = crtc_state->base.crtc->dev;
  613. intel_clock_t clock;
  614. int err = target;
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  618. clock.m1++) {
  619. for (clock.m2 = limit->m2.min;
  620. clock.m2 <= limit->m2.max; clock.m2++) {
  621. if (clock.m2 >= clock.m1)
  622. break;
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. i9xx_calc_dpll_params(refclk, &clock);
  629. if (!intel_PLL_is_valid(dev, limit,
  630. &clock))
  631. continue;
  632. if (match_clock &&
  633. clock.p != match_clock->p)
  634. continue;
  635. this_err = abs(clock.dot - target);
  636. if (this_err < err) {
  637. *best_clock = clock;
  638. err = this_err;
  639. }
  640. }
  641. }
  642. }
  643. }
  644. return (err != target);
  645. }
  646. static bool
  647. pnv_find_best_dpll(const intel_limit_t *limit,
  648. struct intel_crtc_state *crtc_state,
  649. int target, int refclk, intel_clock_t *match_clock,
  650. intel_clock_t *best_clock)
  651. {
  652. struct drm_device *dev = crtc_state->base.crtc->dev;
  653. intel_clock_t clock;
  654. int err = target;
  655. memset(best_clock, 0, sizeof(*best_clock));
  656. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  657. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  658. clock.m1++) {
  659. for (clock.m2 = limit->m2.min;
  660. clock.m2 <= limit->m2.max; clock.m2++) {
  661. for (clock.n = limit->n.min;
  662. clock.n <= limit->n.max; clock.n++) {
  663. for (clock.p1 = limit->p1.min;
  664. clock.p1 <= limit->p1.max; clock.p1++) {
  665. int this_err;
  666. pnv_calc_dpll_params(refclk, &clock);
  667. if (!intel_PLL_is_valid(dev, limit,
  668. &clock))
  669. continue;
  670. if (match_clock &&
  671. clock.p != match_clock->p)
  672. continue;
  673. this_err = abs(clock.dot - target);
  674. if (this_err < err) {
  675. *best_clock = clock;
  676. err = this_err;
  677. }
  678. }
  679. }
  680. }
  681. }
  682. return (err != target);
  683. }
  684. static bool
  685. g4x_find_best_dpll(const intel_limit_t *limit,
  686. struct intel_crtc_state *crtc_state,
  687. int target, int refclk, intel_clock_t *match_clock,
  688. intel_clock_t *best_clock)
  689. {
  690. struct drm_device *dev = crtc_state->base.crtc->dev;
  691. intel_clock_t clock;
  692. int max_n;
  693. bool found = false;
  694. /* approximately equals target * 0.00585 */
  695. int err_most = (target >> 8) + (target >> 9);
  696. memset(best_clock, 0, sizeof(*best_clock));
  697. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  698. max_n = limit->n.max;
  699. /* based on hardware requirement, prefer smaller n to precision */
  700. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  701. /* based on hardware requirement, prefere larger m1,m2 */
  702. for (clock.m1 = limit->m1.max;
  703. clock.m1 >= limit->m1.min; clock.m1--) {
  704. for (clock.m2 = limit->m2.max;
  705. clock.m2 >= limit->m2.min; clock.m2--) {
  706. for (clock.p1 = limit->p1.max;
  707. clock.p1 >= limit->p1.min; clock.p1--) {
  708. int this_err;
  709. i9xx_calc_dpll_params(refclk, &clock);
  710. if (!intel_PLL_is_valid(dev, limit,
  711. &clock))
  712. continue;
  713. this_err = abs(clock.dot - target);
  714. if (this_err < err_most) {
  715. *best_clock = clock;
  716. err_most = this_err;
  717. max_n = clock.n;
  718. found = true;
  719. }
  720. }
  721. }
  722. }
  723. }
  724. return found;
  725. }
  726. /*
  727. * Check if the calculated PLL configuration is more optimal compared to the
  728. * best configuration and error found so far. Return the calculated error.
  729. */
  730. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  731. const intel_clock_t *calculated_clock,
  732. const intel_clock_t *best_clock,
  733. unsigned int best_error_ppm,
  734. unsigned int *error_ppm)
  735. {
  736. /*
  737. * For CHV ignore the error and consider only the P value.
  738. * Prefer a bigger P value based on HW requirements.
  739. */
  740. if (IS_CHERRYVIEW(dev)) {
  741. *error_ppm = 0;
  742. return calculated_clock->p > best_clock->p;
  743. }
  744. if (WARN_ON_ONCE(!target_freq))
  745. return false;
  746. *error_ppm = div_u64(1000000ULL *
  747. abs(target_freq - calculated_clock->dot),
  748. target_freq);
  749. /*
  750. * Prefer a better P value over a better (smaller) error if the error
  751. * is small. Ensure this preference for future configurations too by
  752. * setting the error to 0.
  753. */
  754. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  755. *error_ppm = 0;
  756. return true;
  757. }
  758. return *error_ppm + 10 < best_error_ppm;
  759. }
  760. static bool
  761. vlv_find_best_dpll(const intel_limit_t *limit,
  762. struct intel_crtc_state *crtc_state,
  763. int target, int refclk, intel_clock_t *match_clock,
  764. intel_clock_t *best_clock)
  765. {
  766. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  767. struct drm_device *dev = crtc->base.dev;
  768. intel_clock_t clock;
  769. unsigned int bestppm = 1000000;
  770. /* min update 19.2 MHz */
  771. int max_n = min(limit->n.max, refclk / 19200);
  772. bool found = false;
  773. target *= 5; /* fast clock */
  774. memset(best_clock, 0, sizeof(*best_clock));
  775. /* based on hardware requirement, prefer smaller n to precision */
  776. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  777. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  778. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  779. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  780. clock.p = clock.p1 * clock.p2;
  781. /* based on hardware requirement, prefer bigger m1,m2 values */
  782. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  783. unsigned int ppm;
  784. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  785. refclk * clock.m1);
  786. vlv_calc_dpll_params(refclk, &clock);
  787. if (!intel_PLL_is_valid(dev, limit,
  788. &clock))
  789. continue;
  790. if (!vlv_PLL_is_optimal(dev, target,
  791. &clock,
  792. best_clock,
  793. bestppm, &ppm))
  794. continue;
  795. *best_clock = clock;
  796. bestppm = ppm;
  797. found = true;
  798. }
  799. }
  800. }
  801. }
  802. return found;
  803. }
  804. static bool
  805. chv_find_best_dpll(const intel_limit_t *limit,
  806. struct intel_crtc_state *crtc_state,
  807. int target, int refclk, intel_clock_t *match_clock,
  808. intel_clock_t *best_clock)
  809. {
  810. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  811. struct drm_device *dev = crtc->base.dev;
  812. unsigned int best_error_ppm;
  813. intel_clock_t clock;
  814. uint64_t m2;
  815. int found = false;
  816. memset(best_clock, 0, sizeof(*best_clock));
  817. best_error_ppm = 1000000;
  818. /*
  819. * Based on hardware doc, the n always set to 1, and m1 always
  820. * set to 2. If requires to support 200Mhz refclk, we need to
  821. * revisit this because n may not 1 anymore.
  822. */
  823. clock.n = 1, clock.m1 = 2;
  824. target *= 5; /* fast clock */
  825. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  826. for (clock.p2 = limit->p2.p2_fast;
  827. clock.p2 >= limit->p2.p2_slow;
  828. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  829. unsigned int error_ppm;
  830. clock.p = clock.p1 * clock.p2;
  831. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  832. clock.n) << 22, refclk * clock.m1);
  833. if (m2 > INT_MAX/clock.m1)
  834. continue;
  835. clock.m2 = m2;
  836. chv_calc_dpll_params(refclk, &clock);
  837. if (!intel_PLL_is_valid(dev, limit, &clock))
  838. continue;
  839. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  840. best_error_ppm, &error_ppm))
  841. continue;
  842. *best_clock = clock;
  843. best_error_ppm = error_ppm;
  844. found = true;
  845. }
  846. }
  847. return found;
  848. }
  849. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  850. intel_clock_t *best_clock)
  851. {
  852. int refclk = i9xx_get_refclk(crtc_state, 0);
  853. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  854. target_clock, refclk, NULL, best_clock);
  855. }
  856. bool intel_crtc_active(struct drm_crtc *crtc)
  857. {
  858. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  859. /* Be paranoid as we can arrive here with only partial
  860. * state retrieved from the hardware during setup.
  861. *
  862. * We can ditch the adjusted_mode.crtc_clock check as soon
  863. * as Haswell has gained clock readout/fastboot support.
  864. *
  865. * We can ditch the crtc->primary->fb check as soon as we can
  866. * properly reconstruct framebuffers.
  867. *
  868. * FIXME: The intel_crtc->active here should be switched to
  869. * crtc->state->active once we have proper CRTC states wired up
  870. * for atomic.
  871. */
  872. return intel_crtc->active && crtc->primary->state->fb &&
  873. intel_crtc->config->base.adjusted_mode.crtc_clock;
  874. }
  875. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  876. enum pipe pipe)
  877. {
  878. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  880. return intel_crtc->config->cpu_transcoder;
  881. }
  882. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  883. {
  884. struct drm_i915_private *dev_priv = dev->dev_private;
  885. u32 reg = PIPEDSL(pipe);
  886. u32 line1, line2;
  887. u32 line_mask;
  888. if (IS_GEN2(dev))
  889. line_mask = DSL_LINEMASK_GEN2;
  890. else
  891. line_mask = DSL_LINEMASK_GEN3;
  892. line1 = I915_READ(reg) & line_mask;
  893. msleep(5);
  894. line2 = I915_READ(reg) & line_mask;
  895. return line1 == line2;
  896. }
  897. /*
  898. * intel_wait_for_pipe_off - wait for pipe to turn off
  899. * @crtc: crtc whose pipe to wait for
  900. *
  901. * After disabling a pipe, we can't wait for vblank in the usual way,
  902. * spinning on the vblank interrupt status bit, since we won't actually
  903. * see an interrupt when the pipe is disabled.
  904. *
  905. * On Gen4 and above:
  906. * wait for the pipe register state bit to turn off
  907. *
  908. * Otherwise:
  909. * wait for the display line value to settle (it usually
  910. * ends up stopping at the start of the next frame).
  911. *
  912. */
  913. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  914. {
  915. struct drm_device *dev = crtc->base.dev;
  916. struct drm_i915_private *dev_priv = dev->dev_private;
  917. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  918. enum pipe pipe = crtc->pipe;
  919. if (INTEL_INFO(dev)->gen >= 4) {
  920. int reg = PIPECONF(cpu_transcoder);
  921. /* Wait for the Pipe State to go off */
  922. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  923. 100))
  924. WARN(1, "pipe_off wait timed out\n");
  925. } else {
  926. /* Wait for the display line to settle */
  927. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  928. WARN(1, "pipe_off wait timed out\n");
  929. }
  930. }
  931. /*
  932. * ibx_digital_port_connected - is the specified port connected?
  933. * @dev_priv: i915 private structure
  934. * @port: the port to test
  935. *
  936. * Returns true if @port is connected, false otherwise.
  937. */
  938. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  939. struct intel_digital_port *port)
  940. {
  941. u32 bit;
  942. if (HAS_PCH_IBX(dev_priv->dev)) {
  943. switch (port->port) {
  944. case PORT_B:
  945. bit = SDE_PORTB_HOTPLUG;
  946. break;
  947. case PORT_C:
  948. bit = SDE_PORTC_HOTPLUG;
  949. break;
  950. case PORT_D:
  951. bit = SDE_PORTD_HOTPLUG;
  952. break;
  953. default:
  954. return true;
  955. }
  956. } else {
  957. switch (port->port) {
  958. case PORT_B:
  959. bit = SDE_PORTB_HOTPLUG_CPT;
  960. break;
  961. case PORT_C:
  962. bit = SDE_PORTC_HOTPLUG_CPT;
  963. break;
  964. case PORT_D:
  965. bit = SDE_PORTD_HOTPLUG_CPT;
  966. break;
  967. case PORT_E:
  968. bit = SDE_PORTE_HOTPLUG_SPT;
  969. break;
  970. default:
  971. return true;
  972. }
  973. }
  974. return I915_READ(SDEISR) & bit;
  975. }
  976. static const char *state_string(bool enabled)
  977. {
  978. return enabled ? "on" : "off";
  979. }
  980. /* Only for pre-ILK configs */
  981. void assert_pll(struct drm_i915_private *dev_priv,
  982. enum pipe pipe, bool state)
  983. {
  984. int reg;
  985. u32 val;
  986. bool cur_state;
  987. reg = DPLL(pipe);
  988. val = I915_READ(reg);
  989. cur_state = !!(val & DPLL_VCO_ENABLE);
  990. I915_STATE_WARN(cur_state != state,
  991. "PLL state assertion failure (expected %s, current %s)\n",
  992. state_string(state), state_string(cur_state));
  993. }
  994. /* XXX: the dsi pll is shared between MIPI DSI ports */
  995. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  996. {
  997. u32 val;
  998. bool cur_state;
  999. mutex_lock(&dev_priv->sb_lock);
  1000. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1001. mutex_unlock(&dev_priv->sb_lock);
  1002. cur_state = val & DSI_PLL_VCO_EN;
  1003. I915_STATE_WARN(cur_state != state,
  1004. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1005. state_string(state), state_string(cur_state));
  1006. }
  1007. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1008. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1009. struct intel_shared_dpll *
  1010. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1011. {
  1012. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1013. if (crtc->config->shared_dpll < 0)
  1014. return NULL;
  1015. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1016. }
  1017. /* For ILK+ */
  1018. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1019. struct intel_shared_dpll *pll,
  1020. bool state)
  1021. {
  1022. bool cur_state;
  1023. struct intel_dpll_hw_state hw_state;
  1024. if (WARN (!pll,
  1025. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1026. return;
  1027. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1028. I915_STATE_WARN(cur_state != state,
  1029. "%s assertion failure (expected %s, current %s)\n",
  1030. pll->name, state_string(state), state_string(cur_state));
  1031. }
  1032. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1033. enum pipe pipe, bool state)
  1034. {
  1035. int reg;
  1036. u32 val;
  1037. bool cur_state;
  1038. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1039. pipe);
  1040. if (HAS_DDI(dev_priv->dev)) {
  1041. /* DDI does not have a specific FDI_TX register */
  1042. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1043. val = I915_READ(reg);
  1044. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1045. } else {
  1046. reg = FDI_TX_CTL(pipe);
  1047. val = I915_READ(reg);
  1048. cur_state = !!(val & FDI_TX_ENABLE);
  1049. }
  1050. I915_STATE_WARN(cur_state != state,
  1051. "FDI TX state assertion failure (expected %s, current %s)\n",
  1052. state_string(state), state_string(cur_state));
  1053. }
  1054. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1055. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1056. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1057. enum pipe pipe, bool state)
  1058. {
  1059. int reg;
  1060. u32 val;
  1061. bool cur_state;
  1062. reg = FDI_RX_CTL(pipe);
  1063. val = I915_READ(reg);
  1064. cur_state = !!(val & FDI_RX_ENABLE);
  1065. I915_STATE_WARN(cur_state != state,
  1066. "FDI RX state assertion failure (expected %s, current %s)\n",
  1067. state_string(state), state_string(cur_state));
  1068. }
  1069. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1070. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1071. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1072. enum pipe pipe)
  1073. {
  1074. int reg;
  1075. u32 val;
  1076. /* ILK FDI PLL is always enabled */
  1077. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1078. return;
  1079. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1080. if (HAS_DDI(dev_priv->dev))
  1081. return;
  1082. reg = FDI_TX_CTL(pipe);
  1083. val = I915_READ(reg);
  1084. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1085. }
  1086. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1087. enum pipe pipe, bool state)
  1088. {
  1089. int reg;
  1090. u32 val;
  1091. bool cur_state;
  1092. reg = FDI_RX_CTL(pipe);
  1093. val = I915_READ(reg);
  1094. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1095. I915_STATE_WARN(cur_state != state,
  1096. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1097. state_string(state), state_string(cur_state));
  1098. }
  1099. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1100. enum pipe pipe)
  1101. {
  1102. struct drm_device *dev = dev_priv->dev;
  1103. int pp_reg;
  1104. u32 val;
  1105. enum pipe panel_pipe = PIPE_A;
  1106. bool locked = true;
  1107. if (WARN_ON(HAS_DDI(dev)))
  1108. return;
  1109. if (HAS_PCH_SPLIT(dev)) {
  1110. u32 port_sel;
  1111. pp_reg = PCH_PP_CONTROL;
  1112. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1113. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1114. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1115. panel_pipe = PIPE_B;
  1116. /* XXX: else fix for eDP */
  1117. } else if (IS_VALLEYVIEW(dev)) {
  1118. /* presumably write lock depends on pipe, not port select */
  1119. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1120. panel_pipe = pipe;
  1121. } else {
  1122. pp_reg = PP_CONTROL;
  1123. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1124. panel_pipe = PIPE_B;
  1125. }
  1126. val = I915_READ(pp_reg);
  1127. if (!(val & PANEL_POWER_ON) ||
  1128. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1129. locked = false;
  1130. I915_STATE_WARN(panel_pipe == pipe && locked,
  1131. "panel assertion failure, pipe %c regs locked\n",
  1132. pipe_name(pipe));
  1133. }
  1134. static void assert_cursor(struct drm_i915_private *dev_priv,
  1135. enum pipe pipe, bool state)
  1136. {
  1137. struct drm_device *dev = dev_priv->dev;
  1138. bool cur_state;
  1139. if (IS_845G(dev) || IS_I865G(dev))
  1140. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1141. else
  1142. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1143. I915_STATE_WARN(cur_state != state,
  1144. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1145. pipe_name(pipe), state_string(state), state_string(cur_state));
  1146. }
  1147. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1148. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1149. void assert_pipe(struct drm_i915_private *dev_priv,
  1150. enum pipe pipe, bool state)
  1151. {
  1152. int reg;
  1153. u32 val;
  1154. bool cur_state;
  1155. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1156. pipe);
  1157. /* if we need the pipe quirk it must be always on */
  1158. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1159. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1160. state = true;
  1161. if (!intel_display_power_is_enabled(dev_priv,
  1162. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1163. cur_state = false;
  1164. } else {
  1165. reg = PIPECONF(cpu_transcoder);
  1166. val = I915_READ(reg);
  1167. cur_state = !!(val & PIPECONF_ENABLE);
  1168. }
  1169. I915_STATE_WARN(cur_state != state,
  1170. "pipe %c assertion failure (expected %s, current %s)\n",
  1171. pipe_name(pipe), state_string(state), state_string(cur_state));
  1172. }
  1173. static void assert_plane(struct drm_i915_private *dev_priv,
  1174. enum plane plane, bool state)
  1175. {
  1176. int reg;
  1177. u32 val;
  1178. bool cur_state;
  1179. reg = DSPCNTR(plane);
  1180. val = I915_READ(reg);
  1181. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1182. I915_STATE_WARN(cur_state != state,
  1183. "plane %c assertion failure (expected %s, current %s)\n",
  1184. plane_name(plane), state_string(state), state_string(cur_state));
  1185. }
  1186. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1187. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1188. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1189. enum pipe pipe)
  1190. {
  1191. struct drm_device *dev = dev_priv->dev;
  1192. int reg, i;
  1193. u32 val;
  1194. int cur_pipe;
  1195. /* Primary planes are fixed to pipes on gen4+ */
  1196. if (INTEL_INFO(dev)->gen >= 4) {
  1197. reg = DSPCNTR(pipe);
  1198. val = I915_READ(reg);
  1199. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1200. "plane %c assertion failure, should be disabled but not\n",
  1201. plane_name(pipe));
  1202. return;
  1203. }
  1204. /* Need to check both planes against the pipe */
  1205. for_each_pipe(dev_priv, i) {
  1206. reg = DSPCNTR(i);
  1207. val = I915_READ(reg);
  1208. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1209. DISPPLANE_SEL_PIPE_SHIFT;
  1210. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1211. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1212. plane_name(i), pipe_name(pipe));
  1213. }
  1214. }
  1215. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1216. enum pipe pipe)
  1217. {
  1218. struct drm_device *dev = dev_priv->dev;
  1219. int reg, sprite;
  1220. u32 val;
  1221. if (INTEL_INFO(dev)->gen >= 9) {
  1222. for_each_sprite(dev_priv, pipe, sprite) {
  1223. val = I915_READ(PLANE_CTL(pipe, sprite));
  1224. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1225. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1226. sprite, pipe_name(pipe));
  1227. }
  1228. } else if (IS_VALLEYVIEW(dev)) {
  1229. for_each_sprite(dev_priv, pipe, sprite) {
  1230. reg = SPCNTR(pipe, sprite);
  1231. val = I915_READ(reg);
  1232. I915_STATE_WARN(val & SP_ENABLE,
  1233. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1234. sprite_name(pipe, sprite), pipe_name(pipe));
  1235. }
  1236. } else if (INTEL_INFO(dev)->gen >= 7) {
  1237. reg = SPRCTL(pipe);
  1238. val = I915_READ(reg);
  1239. I915_STATE_WARN(val & SPRITE_ENABLE,
  1240. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1241. plane_name(pipe), pipe_name(pipe));
  1242. } else if (INTEL_INFO(dev)->gen >= 5) {
  1243. reg = DVSCNTR(pipe);
  1244. val = I915_READ(reg);
  1245. I915_STATE_WARN(val & DVS_ENABLE,
  1246. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1247. plane_name(pipe), pipe_name(pipe));
  1248. }
  1249. }
  1250. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1251. {
  1252. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1253. drm_crtc_vblank_put(crtc);
  1254. }
  1255. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1256. {
  1257. u32 val;
  1258. bool enabled;
  1259. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1260. val = I915_READ(PCH_DREF_CONTROL);
  1261. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1262. DREF_SUPERSPREAD_SOURCE_MASK));
  1263. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1264. }
  1265. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1266. enum pipe pipe)
  1267. {
  1268. int reg;
  1269. u32 val;
  1270. bool enabled;
  1271. reg = PCH_TRANSCONF(pipe);
  1272. val = I915_READ(reg);
  1273. enabled = !!(val & TRANS_ENABLE);
  1274. I915_STATE_WARN(enabled,
  1275. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1276. pipe_name(pipe));
  1277. }
  1278. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1279. enum pipe pipe, u32 port_sel, u32 val)
  1280. {
  1281. if ((val & DP_PORT_EN) == 0)
  1282. return false;
  1283. if (HAS_PCH_CPT(dev_priv->dev)) {
  1284. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1285. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1286. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1287. return false;
  1288. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1289. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1290. return false;
  1291. } else {
  1292. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1293. return false;
  1294. }
  1295. return true;
  1296. }
  1297. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1298. enum pipe pipe, u32 val)
  1299. {
  1300. if ((val & SDVO_ENABLE) == 0)
  1301. return false;
  1302. if (HAS_PCH_CPT(dev_priv->dev)) {
  1303. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1304. return false;
  1305. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1306. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1307. return false;
  1308. } else {
  1309. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1310. return false;
  1311. }
  1312. return true;
  1313. }
  1314. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1315. enum pipe pipe, u32 val)
  1316. {
  1317. if ((val & LVDS_PORT_EN) == 0)
  1318. return false;
  1319. if (HAS_PCH_CPT(dev_priv->dev)) {
  1320. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1321. return false;
  1322. } else {
  1323. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1324. return false;
  1325. }
  1326. return true;
  1327. }
  1328. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1329. enum pipe pipe, u32 val)
  1330. {
  1331. if ((val & ADPA_DAC_ENABLE) == 0)
  1332. return false;
  1333. if (HAS_PCH_CPT(dev_priv->dev)) {
  1334. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1335. return false;
  1336. } else {
  1337. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1338. return false;
  1339. }
  1340. return true;
  1341. }
  1342. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1343. enum pipe pipe, int reg, u32 port_sel)
  1344. {
  1345. u32 val = I915_READ(reg);
  1346. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1347. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1348. reg, pipe_name(pipe));
  1349. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1350. && (val & DP_PIPEB_SELECT),
  1351. "IBX PCH dp port still using transcoder B\n");
  1352. }
  1353. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1354. enum pipe pipe, int reg)
  1355. {
  1356. u32 val = I915_READ(reg);
  1357. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1358. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1359. reg, pipe_name(pipe));
  1360. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1361. && (val & SDVO_PIPE_B_SELECT),
  1362. "IBX PCH hdmi port still using transcoder B\n");
  1363. }
  1364. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1365. enum pipe pipe)
  1366. {
  1367. int reg;
  1368. u32 val;
  1369. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1370. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1371. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1372. reg = PCH_ADPA;
  1373. val = I915_READ(reg);
  1374. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1375. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1376. pipe_name(pipe));
  1377. reg = PCH_LVDS;
  1378. val = I915_READ(reg);
  1379. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1380. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1381. pipe_name(pipe));
  1382. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1383. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1384. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1385. }
  1386. static void intel_init_dpio(struct drm_device *dev)
  1387. {
  1388. struct drm_i915_private *dev_priv = dev->dev_private;
  1389. if (!IS_VALLEYVIEW(dev))
  1390. return;
  1391. /*
  1392. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1393. * CHV x1 PHY (DP/HDMI D)
  1394. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1395. */
  1396. if (IS_CHERRYVIEW(dev)) {
  1397. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1398. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1399. } else {
  1400. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1401. }
  1402. }
  1403. static void vlv_enable_pll(struct intel_crtc *crtc,
  1404. const struct intel_crtc_state *pipe_config)
  1405. {
  1406. struct drm_device *dev = crtc->base.dev;
  1407. struct drm_i915_private *dev_priv = dev->dev_private;
  1408. int reg = DPLL(crtc->pipe);
  1409. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1410. assert_pipe_disabled(dev_priv, crtc->pipe);
  1411. /* No really, not for ILK+ */
  1412. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1413. /* PLL is protected by panel, make sure we can write it */
  1414. if (IS_MOBILE(dev_priv->dev))
  1415. assert_panel_unlocked(dev_priv, crtc->pipe);
  1416. I915_WRITE(reg, dpll);
  1417. POSTING_READ(reg);
  1418. udelay(150);
  1419. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1420. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1421. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1422. POSTING_READ(DPLL_MD(crtc->pipe));
  1423. /* We do this three times for luck */
  1424. I915_WRITE(reg, dpll);
  1425. POSTING_READ(reg);
  1426. udelay(150); /* wait for warmup */
  1427. I915_WRITE(reg, dpll);
  1428. POSTING_READ(reg);
  1429. udelay(150); /* wait for warmup */
  1430. I915_WRITE(reg, dpll);
  1431. POSTING_READ(reg);
  1432. udelay(150); /* wait for warmup */
  1433. }
  1434. static void chv_enable_pll(struct intel_crtc *crtc,
  1435. const struct intel_crtc_state *pipe_config)
  1436. {
  1437. struct drm_device *dev = crtc->base.dev;
  1438. struct drm_i915_private *dev_priv = dev->dev_private;
  1439. int pipe = crtc->pipe;
  1440. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1441. u32 tmp;
  1442. assert_pipe_disabled(dev_priv, crtc->pipe);
  1443. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1444. mutex_lock(&dev_priv->sb_lock);
  1445. /* Enable back the 10bit clock to display controller */
  1446. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1447. tmp |= DPIO_DCLKP_EN;
  1448. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1449. mutex_unlock(&dev_priv->sb_lock);
  1450. /*
  1451. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1452. */
  1453. udelay(1);
  1454. /* Enable PLL */
  1455. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1456. /* Check PLL is locked */
  1457. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1458. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1459. /* not sure when this should be written */
  1460. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1461. POSTING_READ(DPLL_MD(pipe));
  1462. }
  1463. static int intel_num_dvo_pipes(struct drm_device *dev)
  1464. {
  1465. struct intel_crtc *crtc;
  1466. int count = 0;
  1467. for_each_intel_crtc(dev, crtc)
  1468. count += crtc->base.state->active &&
  1469. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1470. return count;
  1471. }
  1472. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1473. {
  1474. struct drm_device *dev = crtc->base.dev;
  1475. struct drm_i915_private *dev_priv = dev->dev_private;
  1476. int reg = DPLL(crtc->pipe);
  1477. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1478. assert_pipe_disabled(dev_priv, crtc->pipe);
  1479. /* No really, not for ILK+ */
  1480. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1481. /* PLL is protected by panel, make sure we can write it */
  1482. if (IS_MOBILE(dev) && !IS_I830(dev))
  1483. assert_panel_unlocked(dev_priv, crtc->pipe);
  1484. /* Enable DVO 2x clock on both PLLs if necessary */
  1485. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1486. /*
  1487. * It appears to be important that we don't enable this
  1488. * for the current pipe before otherwise configuring the
  1489. * PLL. No idea how this should be handled if multiple
  1490. * DVO outputs are enabled simultaneosly.
  1491. */
  1492. dpll |= DPLL_DVO_2X_MODE;
  1493. I915_WRITE(DPLL(!crtc->pipe),
  1494. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1495. }
  1496. /* Wait for the clocks to stabilize. */
  1497. POSTING_READ(reg);
  1498. udelay(150);
  1499. if (INTEL_INFO(dev)->gen >= 4) {
  1500. I915_WRITE(DPLL_MD(crtc->pipe),
  1501. crtc->config->dpll_hw_state.dpll_md);
  1502. } else {
  1503. /* The pixel multiplier can only be updated once the
  1504. * DPLL is enabled and the clocks are stable.
  1505. *
  1506. * So write it again.
  1507. */
  1508. I915_WRITE(reg, dpll);
  1509. }
  1510. /* We do this three times for luck */
  1511. I915_WRITE(reg, dpll);
  1512. POSTING_READ(reg);
  1513. udelay(150); /* wait for warmup */
  1514. I915_WRITE(reg, dpll);
  1515. POSTING_READ(reg);
  1516. udelay(150); /* wait for warmup */
  1517. I915_WRITE(reg, dpll);
  1518. POSTING_READ(reg);
  1519. udelay(150); /* wait for warmup */
  1520. }
  1521. /**
  1522. * i9xx_disable_pll - disable a PLL
  1523. * @dev_priv: i915 private structure
  1524. * @pipe: pipe PLL to disable
  1525. *
  1526. * Disable the PLL for @pipe, making sure the pipe is off first.
  1527. *
  1528. * Note! This is for pre-ILK only.
  1529. */
  1530. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1531. {
  1532. struct drm_device *dev = crtc->base.dev;
  1533. struct drm_i915_private *dev_priv = dev->dev_private;
  1534. enum pipe pipe = crtc->pipe;
  1535. /* Disable DVO 2x clock on both PLLs if necessary */
  1536. if (IS_I830(dev) &&
  1537. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1538. !intel_num_dvo_pipes(dev)) {
  1539. I915_WRITE(DPLL(PIPE_B),
  1540. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1541. I915_WRITE(DPLL(PIPE_A),
  1542. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1543. }
  1544. /* Don't disable pipe or pipe PLLs if needed */
  1545. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1546. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1547. return;
  1548. /* Make sure the pipe isn't still relying on us */
  1549. assert_pipe_disabled(dev_priv, pipe);
  1550. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1551. POSTING_READ(DPLL(pipe));
  1552. }
  1553. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1554. {
  1555. u32 val;
  1556. /* Make sure the pipe isn't still relying on us */
  1557. assert_pipe_disabled(dev_priv, pipe);
  1558. /*
  1559. * Leave integrated clock source and reference clock enabled for pipe B.
  1560. * The latter is needed for VGA hotplug / manual detection.
  1561. */
  1562. val = DPLL_VGA_MODE_DIS;
  1563. if (pipe == PIPE_B)
  1564. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1565. I915_WRITE(DPLL(pipe), val);
  1566. POSTING_READ(DPLL(pipe));
  1567. }
  1568. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1569. {
  1570. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1571. u32 val;
  1572. /* Make sure the pipe isn't still relying on us */
  1573. assert_pipe_disabled(dev_priv, pipe);
  1574. /* Set PLL en = 0 */
  1575. val = DPLL_SSC_REF_CLK_CHV |
  1576. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1577. if (pipe != PIPE_A)
  1578. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1579. I915_WRITE(DPLL(pipe), val);
  1580. POSTING_READ(DPLL(pipe));
  1581. mutex_lock(&dev_priv->sb_lock);
  1582. /* Disable 10bit clock to display controller */
  1583. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1584. val &= ~DPIO_DCLKP_EN;
  1585. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1586. /* disable left/right clock distribution */
  1587. if (pipe != PIPE_B) {
  1588. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1589. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1590. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1591. } else {
  1592. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1593. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1594. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1595. }
  1596. mutex_unlock(&dev_priv->sb_lock);
  1597. }
  1598. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1599. struct intel_digital_port *dport,
  1600. unsigned int expected_mask)
  1601. {
  1602. u32 port_mask;
  1603. int dpll_reg;
  1604. switch (dport->port) {
  1605. case PORT_B:
  1606. port_mask = DPLL_PORTB_READY_MASK;
  1607. dpll_reg = DPLL(0);
  1608. break;
  1609. case PORT_C:
  1610. port_mask = DPLL_PORTC_READY_MASK;
  1611. dpll_reg = DPLL(0);
  1612. expected_mask <<= 4;
  1613. break;
  1614. case PORT_D:
  1615. port_mask = DPLL_PORTD_READY_MASK;
  1616. dpll_reg = DPIO_PHY_STATUS;
  1617. break;
  1618. default:
  1619. BUG();
  1620. }
  1621. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1622. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1623. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1624. }
  1625. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1626. {
  1627. struct drm_device *dev = crtc->base.dev;
  1628. struct drm_i915_private *dev_priv = dev->dev_private;
  1629. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1630. if (WARN_ON(pll == NULL))
  1631. return;
  1632. WARN_ON(!pll->config.crtc_mask);
  1633. if (pll->active == 0) {
  1634. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1635. WARN_ON(pll->on);
  1636. assert_shared_dpll_disabled(dev_priv, pll);
  1637. pll->mode_set(dev_priv, pll);
  1638. }
  1639. }
  1640. /**
  1641. * intel_enable_shared_dpll - enable PCH PLL
  1642. * @dev_priv: i915 private structure
  1643. * @pipe: pipe PLL to enable
  1644. *
  1645. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1646. * drives the transcoder clock.
  1647. */
  1648. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1649. {
  1650. struct drm_device *dev = crtc->base.dev;
  1651. struct drm_i915_private *dev_priv = dev->dev_private;
  1652. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1653. if (WARN_ON(pll == NULL))
  1654. return;
  1655. if (WARN_ON(pll->config.crtc_mask == 0))
  1656. return;
  1657. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1658. pll->name, pll->active, pll->on,
  1659. crtc->base.base.id);
  1660. if (pll->active++) {
  1661. WARN_ON(!pll->on);
  1662. assert_shared_dpll_enabled(dev_priv, pll);
  1663. return;
  1664. }
  1665. WARN_ON(pll->on);
  1666. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1667. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1668. pll->enable(dev_priv, pll);
  1669. pll->on = true;
  1670. }
  1671. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1672. {
  1673. struct drm_device *dev = crtc->base.dev;
  1674. struct drm_i915_private *dev_priv = dev->dev_private;
  1675. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1676. /* PCH only available on ILK+ */
  1677. if (INTEL_INFO(dev)->gen < 5)
  1678. return;
  1679. if (pll == NULL)
  1680. return;
  1681. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1682. return;
  1683. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1684. pll->name, pll->active, pll->on,
  1685. crtc->base.base.id);
  1686. if (WARN_ON(pll->active == 0)) {
  1687. assert_shared_dpll_disabled(dev_priv, pll);
  1688. return;
  1689. }
  1690. assert_shared_dpll_enabled(dev_priv, pll);
  1691. WARN_ON(!pll->on);
  1692. if (--pll->active)
  1693. return;
  1694. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1695. pll->disable(dev_priv, pll);
  1696. pll->on = false;
  1697. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1698. }
  1699. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1700. enum pipe pipe)
  1701. {
  1702. struct drm_device *dev = dev_priv->dev;
  1703. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1704. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1705. uint32_t reg, val, pipeconf_val;
  1706. /* PCH only available on ILK+ */
  1707. BUG_ON(!HAS_PCH_SPLIT(dev));
  1708. /* Make sure PCH DPLL is enabled */
  1709. assert_shared_dpll_enabled(dev_priv,
  1710. intel_crtc_to_shared_dpll(intel_crtc));
  1711. /* FDI must be feeding us bits for PCH ports */
  1712. assert_fdi_tx_enabled(dev_priv, pipe);
  1713. assert_fdi_rx_enabled(dev_priv, pipe);
  1714. if (HAS_PCH_CPT(dev)) {
  1715. /* Workaround: Set the timing override bit before enabling the
  1716. * pch transcoder. */
  1717. reg = TRANS_CHICKEN2(pipe);
  1718. val = I915_READ(reg);
  1719. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1720. I915_WRITE(reg, val);
  1721. }
  1722. reg = PCH_TRANSCONF(pipe);
  1723. val = I915_READ(reg);
  1724. pipeconf_val = I915_READ(PIPECONF(pipe));
  1725. if (HAS_PCH_IBX(dev_priv->dev)) {
  1726. /*
  1727. * Make the BPC in transcoder be consistent with
  1728. * that in pipeconf reg. For HDMI we must use 8bpc
  1729. * here for both 8bpc and 12bpc.
  1730. */
  1731. val &= ~PIPECONF_BPC_MASK;
  1732. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1733. val |= PIPECONF_8BPC;
  1734. else
  1735. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1736. }
  1737. val &= ~TRANS_INTERLACE_MASK;
  1738. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1739. if (HAS_PCH_IBX(dev_priv->dev) &&
  1740. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1741. val |= TRANS_LEGACY_INTERLACED_ILK;
  1742. else
  1743. val |= TRANS_INTERLACED;
  1744. else
  1745. val |= TRANS_PROGRESSIVE;
  1746. I915_WRITE(reg, val | TRANS_ENABLE);
  1747. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1748. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1749. }
  1750. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1751. enum transcoder cpu_transcoder)
  1752. {
  1753. u32 val, pipeconf_val;
  1754. /* PCH only available on ILK+ */
  1755. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1756. /* FDI must be feeding us bits for PCH ports */
  1757. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1758. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1759. /* Workaround: set timing override bit. */
  1760. val = I915_READ(_TRANSA_CHICKEN2);
  1761. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1762. I915_WRITE(_TRANSA_CHICKEN2, val);
  1763. val = TRANS_ENABLE;
  1764. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1765. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1766. PIPECONF_INTERLACED_ILK)
  1767. val |= TRANS_INTERLACED;
  1768. else
  1769. val |= TRANS_PROGRESSIVE;
  1770. I915_WRITE(LPT_TRANSCONF, val);
  1771. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1772. DRM_ERROR("Failed to enable PCH transcoder\n");
  1773. }
  1774. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1775. enum pipe pipe)
  1776. {
  1777. struct drm_device *dev = dev_priv->dev;
  1778. uint32_t reg, val;
  1779. /* FDI relies on the transcoder */
  1780. assert_fdi_tx_disabled(dev_priv, pipe);
  1781. assert_fdi_rx_disabled(dev_priv, pipe);
  1782. /* Ports must be off as well */
  1783. assert_pch_ports_disabled(dev_priv, pipe);
  1784. reg = PCH_TRANSCONF(pipe);
  1785. val = I915_READ(reg);
  1786. val &= ~TRANS_ENABLE;
  1787. I915_WRITE(reg, val);
  1788. /* wait for PCH transcoder off, transcoder state */
  1789. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1790. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1791. if (!HAS_PCH_IBX(dev)) {
  1792. /* Workaround: Clear the timing override chicken bit again. */
  1793. reg = TRANS_CHICKEN2(pipe);
  1794. val = I915_READ(reg);
  1795. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1796. I915_WRITE(reg, val);
  1797. }
  1798. }
  1799. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1800. {
  1801. u32 val;
  1802. val = I915_READ(LPT_TRANSCONF);
  1803. val &= ~TRANS_ENABLE;
  1804. I915_WRITE(LPT_TRANSCONF, val);
  1805. /* wait for PCH transcoder off, transcoder state */
  1806. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1807. DRM_ERROR("Failed to disable PCH transcoder\n");
  1808. /* Workaround: clear timing override bit. */
  1809. val = I915_READ(_TRANSA_CHICKEN2);
  1810. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1811. I915_WRITE(_TRANSA_CHICKEN2, val);
  1812. }
  1813. /**
  1814. * intel_enable_pipe - enable a pipe, asserting requirements
  1815. * @crtc: crtc responsible for the pipe
  1816. *
  1817. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1818. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1819. */
  1820. static void intel_enable_pipe(struct intel_crtc *crtc)
  1821. {
  1822. struct drm_device *dev = crtc->base.dev;
  1823. struct drm_i915_private *dev_priv = dev->dev_private;
  1824. enum pipe pipe = crtc->pipe;
  1825. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1826. pipe);
  1827. enum pipe pch_transcoder;
  1828. int reg;
  1829. u32 val;
  1830. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1831. assert_planes_disabled(dev_priv, pipe);
  1832. assert_cursor_disabled(dev_priv, pipe);
  1833. assert_sprites_disabled(dev_priv, pipe);
  1834. if (HAS_PCH_LPT(dev_priv->dev))
  1835. pch_transcoder = TRANSCODER_A;
  1836. else
  1837. pch_transcoder = pipe;
  1838. /*
  1839. * A pipe without a PLL won't actually be able to drive bits from
  1840. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1841. * need the check.
  1842. */
  1843. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1844. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1845. assert_dsi_pll_enabled(dev_priv);
  1846. else
  1847. assert_pll_enabled(dev_priv, pipe);
  1848. else {
  1849. if (crtc->config->has_pch_encoder) {
  1850. /* if driving the PCH, we need FDI enabled */
  1851. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1852. assert_fdi_tx_pll_enabled(dev_priv,
  1853. (enum pipe) cpu_transcoder);
  1854. }
  1855. /* FIXME: assert CPU port conditions for SNB+ */
  1856. }
  1857. reg = PIPECONF(cpu_transcoder);
  1858. val = I915_READ(reg);
  1859. if (val & PIPECONF_ENABLE) {
  1860. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1861. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1862. return;
  1863. }
  1864. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1865. POSTING_READ(reg);
  1866. }
  1867. /**
  1868. * intel_disable_pipe - disable a pipe, asserting requirements
  1869. * @crtc: crtc whose pipes is to be disabled
  1870. *
  1871. * Disable the pipe of @crtc, making sure that various hardware
  1872. * specific requirements are met, if applicable, e.g. plane
  1873. * disabled, panel fitter off, etc.
  1874. *
  1875. * Will wait until the pipe has shut down before returning.
  1876. */
  1877. static void intel_disable_pipe(struct intel_crtc *crtc)
  1878. {
  1879. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1880. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1881. enum pipe pipe = crtc->pipe;
  1882. int reg;
  1883. u32 val;
  1884. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1885. /*
  1886. * Make sure planes won't keep trying to pump pixels to us,
  1887. * or we might hang the display.
  1888. */
  1889. assert_planes_disabled(dev_priv, pipe);
  1890. assert_cursor_disabled(dev_priv, pipe);
  1891. assert_sprites_disabled(dev_priv, pipe);
  1892. reg = PIPECONF(cpu_transcoder);
  1893. val = I915_READ(reg);
  1894. if ((val & PIPECONF_ENABLE) == 0)
  1895. return;
  1896. /*
  1897. * Double wide has implications for planes
  1898. * so best keep it disabled when not needed.
  1899. */
  1900. if (crtc->config->double_wide)
  1901. val &= ~PIPECONF_DOUBLE_WIDE;
  1902. /* Don't disable pipe or pipe PLLs if needed */
  1903. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1904. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1905. val &= ~PIPECONF_ENABLE;
  1906. I915_WRITE(reg, val);
  1907. if ((val & PIPECONF_ENABLE) == 0)
  1908. intel_wait_for_pipe_off(crtc);
  1909. }
  1910. static bool need_vtd_wa(struct drm_device *dev)
  1911. {
  1912. #ifdef CONFIG_INTEL_IOMMU
  1913. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1914. return true;
  1915. #endif
  1916. return false;
  1917. }
  1918. unsigned int
  1919. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1920. uint64_t fb_format_modifier)
  1921. {
  1922. unsigned int tile_height;
  1923. uint32_t pixel_bytes;
  1924. switch (fb_format_modifier) {
  1925. case DRM_FORMAT_MOD_NONE:
  1926. tile_height = 1;
  1927. break;
  1928. case I915_FORMAT_MOD_X_TILED:
  1929. tile_height = IS_GEN2(dev) ? 16 : 8;
  1930. break;
  1931. case I915_FORMAT_MOD_Y_TILED:
  1932. tile_height = 32;
  1933. break;
  1934. case I915_FORMAT_MOD_Yf_TILED:
  1935. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1936. switch (pixel_bytes) {
  1937. default:
  1938. case 1:
  1939. tile_height = 64;
  1940. break;
  1941. case 2:
  1942. case 4:
  1943. tile_height = 32;
  1944. break;
  1945. case 8:
  1946. tile_height = 16;
  1947. break;
  1948. case 16:
  1949. WARN_ONCE(1,
  1950. "128-bit pixels are not supported for display!");
  1951. tile_height = 16;
  1952. break;
  1953. }
  1954. break;
  1955. default:
  1956. MISSING_CASE(fb_format_modifier);
  1957. tile_height = 1;
  1958. break;
  1959. }
  1960. return tile_height;
  1961. }
  1962. unsigned int
  1963. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1964. uint32_t pixel_format, uint64_t fb_format_modifier)
  1965. {
  1966. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1967. fb_format_modifier));
  1968. }
  1969. static int
  1970. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1971. const struct drm_plane_state *plane_state)
  1972. {
  1973. struct intel_rotation_info *info = &view->rotation_info;
  1974. unsigned int tile_height, tile_pitch;
  1975. *view = i915_ggtt_view_normal;
  1976. if (!plane_state)
  1977. return 0;
  1978. if (!intel_rotation_90_or_270(plane_state->rotation))
  1979. return 0;
  1980. *view = i915_ggtt_view_rotated;
  1981. info->height = fb->height;
  1982. info->pixel_format = fb->pixel_format;
  1983. info->pitch = fb->pitches[0];
  1984. info->fb_modifier = fb->modifier[0];
  1985. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1986. fb->modifier[0]);
  1987. tile_pitch = PAGE_SIZE / tile_height;
  1988. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1989. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1990. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1991. return 0;
  1992. }
  1993. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1994. {
  1995. if (INTEL_INFO(dev_priv)->gen >= 9)
  1996. return 256 * 1024;
  1997. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1998. IS_VALLEYVIEW(dev_priv))
  1999. return 128 * 1024;
  2000. else if (INTEL_INFO(dev_priv)->gen >= 4)
  2001. return 4 * 1024;
  2002. else
  2003. return 0;
  2004. }
  2005. int
  2006. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2007. struct drm_framebuffer *fb,
  2008. const struct drm_plane_state *plane_state,
  2009. struct intel_engine_cs *pipelined,
  2010. struct drm_i915_gem_request **pipelined_request)
  2011. {
  2012. struct drm_device *dev = fb->dev;
  2013. struct drm_i915_private *dev_priv = dev->dev_private;
  2014. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2015. struct i915_ggtt_view view;
  2016. u32 alignment;
  2017. int ret;
  2018. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2019. switch (fb->modifier[0]) {
  2020. case DRM_FORMAT_MOD_NONE:
  2021. alignment = intel_linear_alignment(dev_priv);
  2022. break;
  2023. case I915_FORMAT_MOD_X_TILED:
  2024. if (INTEL_INFO(dev)->gen >= 9)
  2025. alignment = 256 * 1024;
  2026. else {
  2027. /* pin() will align the object as required by fence */
  2028. alignment = 0;
  2029. }
  2030. break;
  2031. case I915_FORMAT_MOD_Y_TILED:
  2032. case I915_FORMAT_MOD_Yf_TILED:
  2033. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2034. "Y tiling bo slipped through, driver bug!\n"))
  2035. return -EINVAL;
  2036. alignment = 1 * 1024 * 1024;
  2037. break;
  2038. default:
  2039. MISSING_CASE(fb->modifier[0]);
  2040. return -EINVAL;
  2041. }
  2042. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2043. if (ret)
  2044. return ret;
  2045. /* Note that the w/a also requires 64 PTE of padding following the
  2046. * bo. We currently fill all unused PTE with the shadow page and so
  2047. * we should always have valid PTE following the scanout preventing
  2048. * the VT-d warning.
  2049. */
  2050. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2051. alignment = 256 * 1024;
  2052. /*
  2053. * Global gtt pte registers are special registers which actually forward
  2054. * writes to a chunk of system memory. Which means that there is no risk
  2055. * that the register values disappear as soon as we call
  2056. * intel_runtime_pm_put(), so it is correct to wrap only the
  2057. * pin/unpin/fence and not more.
  2058. */
  2059. intel_runtime_pm_get(dev_priv);
  2060. dev_priv->mm.interruptible = false;
  2061. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2062. pipelined_request, &view);
  2063. if (ret)
  2064. goto err_interruptible;
  2065. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2066. * fence, whereas 965+ only requires a fence if using
  2067. * framebuffer compression. For simplicity, we always install
  2068. * a fence as the cost is not that onerous.
  2069. */
  2070. ret = i915_gem_object_get_fence(obj);
  2071. if (ret == -EDEADLK) {
  2072. /*
  2073. * -EDEADLK means there are no free fences
  2074. * no pending flips.
  2075. *
  2076. * This is propagated to atomic, but it uses
  2077. * -EDEADLK to force a locking recovery, so
  2078. * change the returned error to -EBUSY.
  2079. */
  2080. ret = -EBUSY;
  2081. goto err_unpin;
  2082. } else if (ret)
  2083. goto err_unpin;
  2084. i915_gem_object_pin_fence(obj);
  2085. dev_priv->mm.interruptible = true;
  2086. intel_runtime_pm_put(dev_priv);
  2087. return 0;
  2088. err_unpin:
  2089. i915_gem_object_unpin_from_display_plane(obj, &view);
  2090. err_interruptible:
  2091. dev_priv->mm.interruptible = true;
  2092. intel_runtime_pm_put(dev_priv);
  2093. return ret;
  2094. }
  2095. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2096. const struct drm_plane_state *plane_state)
  2097. {
  2098. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2099. struct i915_ggtt_view view;
  2100. int ret;
  2101. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2102. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2103. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2104. i915_gem_object_unpin_fence(obj);
  2105. i915_gem_object_unpin_from_display_plane(obj, &view);
  2106. }
  2107. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2108. * is assumed to be a power-of-two. */
  2109. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2110. int *x, int *y,
  2111. unsigned int tiling_mode,
  2112. unsigned int cpp,
  2113. unsigned int pitch)
  2114. {
  2115. if (tiling_mode != I915_TILING_NONE) {
  2116. unsigned int tile_rows, tiles;
  2117. tile_rows = *y / 8;
  2118. *y %= 8;
  2119. tiles = *x / (512/cpp);
  2120. *x %= 512/cpp;
  2121. return tile_rows * pitch * 8 + tiles * 4096;
  2122. } else {
  2123. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2124. unsigned int offset;
  2125. offset = *y * pitch + *x * cpp;
  2126. *y = (offset & alignment) / pitch;
  2127. *x = ((offset & alignment) - *y * pitch) / cpp;
  2128. return offset & ~alignment;
  2129. }
  2130. }
  2131. static int i9xx_format_to_fourcc(int format)
  2132. {
  2133. switch (format) {
  2134. case DISPPLANE_8BPP:
  2135. return DRM_FORMAT_C8;
  2136. case DISPPLANE_BGRX555:
  2137. return DRM_FORMAT_XRGB1555;
  2138. case DISPPLANE_BGRX565:
  2139. return DRM_FORMAT_RGB565;
  2140. default:
  2141. case DISPPLANE_BGRX888:
  2142. return DRM_FORMAT_XRGB8888;
  2143. case DISPPLANE_RGBX888:
  2144. return DRM_FORMAT_XBGR8888;
  2145. case DISPPLANE_BGRX101010:
  2146. return DRM_FORMAT_XRGB2101010;
  2147. case DISPPLANE_RGBX101010:
  2148. return DRM_FORMAT_XBGR2101010;
  2149. }
  2150. }
  2151. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2152. {
  2153. switch (format) {
  2154. case PLANE_CTL_FORMAT_RGB_565:
  2155. return DRM_FORMAT_RGB565;
  2156. default:
  2157. case PLANE_CTL_FORMAT_XRGB_8888:
  2158. if (rgb_order) {
  2159. if (alpha)
  2160. return DRM_FORMAT_ABGR8888;
  2161. else
  2162. return DRM_FORMAT_XBGR8888;
  2163. } else {
  2164. if (alpha)
  2165. return DRM_FORMAT_ARGB8888;
  2166. else
  2167. return DRM_FORMAT_XRGB8888;
  2168. }
  2169. case PLANE_CTL_FORMAT_XRGB_2101010:
  2170. if (rgb_order)
  2171. return DRM_FORMAT_XBGR2101010;
  2172. else
  2173. return DRM_FORMAT_XRGB2101010;
  2174. }
  2175. }
  2176. static bool
  2177. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2178. struct intel_initial_plane_config *plane_config)
  2179. {
  2180. struct drm_device *dev = crtc->base.dev;
  2181. struct drm_i915_gem_object *obj = NULL;
  2182. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2183. struct drm_framebuffer *fb = &plane_config->fb->base;
  2184. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2185. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2186. PAGE_SIZE);
  2187. size_aligned -= base_aligned;
  2188. if (plane_config->size == 0)
  2189. return false;
  2190. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2191. base_aligned,
  2192. base_aligned,
  2193. size_aligned);
  2194. if (!obj)
  2195. return false;
  2196. obj->tiling_mode = plane_config->tiling;
  2197. if (obj->tiling_mode == I915_TILING_X)
  2198. obj->stride = fb->pitches[0];
  2199. mode_cmd.pixel_format = fb->pixel_format;
  2200. mode_cmd.width = fb->width;
  2201. mode_cmd.height = fb->height;
  2202. mode_cmd.pitches[0] = fb->pitches[0];
  2203. mode_cmd.modifier[0] = fb->modifier[0];
  2204. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2205. mutex_lock(&dev->struct_mutex);
  2206. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2207. &mode_cmd, obj)) {
  2208. DRM_DEBUG_KMS("intel fb init failed\n");
  2209. goto out_unref_obj;
  2210. }
  2211. mutex_unlock(&dev->struct_mutex);
  2212. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2213. return true;
  2214. out_unref_obj:
  2215. drm_gem_object_unreference(&obj->base);
  2216. mutex_unlock(&dev->struct_mutex);
  2217. return false;
  2218. }
  2219. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2220. static void
  2221. update_state_fb(struct drm_plane *plane)
  2222. {
  2223. if (plane->fb == plane->state->fb)
  2224. return;
  2225. if (plane->state->fb)
  2226. drm_framebuffer_unreference(plane->state->fb);
  2227. plane->state->fb = plane->fb;
  2228. if (plane->state->fb)
  2229. drm_framebuffer_reference(plane->state->fb);
  2230. }
  2231. static void
  2232. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2233. struct intel_initial_plane_config *plane_config)
  2234. {
  2235. struct drm_device *dev = intel_crtc->base.dev;
  2236. struct drm_i915_private *dev_priv = dev->dev_private;
  2237. struct drm_crtc *c;
  2238. struct intel_crtc *i;
  2239. struct drm_i915_gem_object *obj;
  2240. struct drm_plane *primary = intel_crtc->base.primary;
  2241. struct drm_plane_state *plane_state = primary->state;
  2242. struct drm_framebuffer *fb;
  2243. if (!plane_config->fb)
  2244. return;
  2245. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2246. fb = &plane_config->fb->base;
  2247. goto valid_fb;
  2248. }
  2249. kfree(plane_config->fb);
  2250. /*
  2251. * Failed to alloc the obj, check to see if we should share
  2252. * an fb with another CRTC instead
  2253. */
  2254. for_each_crtc(dev, c) {
  2255. i = to_intel_crtc(c);
  2256. if (c == &intel_crtc->base)
  2257. continue;
  2258. if (!i->active)
  2259. continue;
  2260. fb = c->primary->fb;
  2261. if (!fb)
  2262. continue;
  2263. obj = intel_fb_obj(fb);
  2264. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2265. drm_framebuffer_reference(fb);
  2266. goto valid_fb;
  2267. }
  2268. }
  2269. return;
  2270. valid_fb:
  2271. plane_state->src_x = plane_state->src_y = 0;
  2272. plane_state->src_w = fb->width << 16;
  2273. plane_state->src_h = fb->height << 16;
  2274. plane_state->crtc_x = plane_state->src_y = 0;
  2275. plane_state->crtc_w = fb->width;
  2276. plane_state->crtc_h = fb->height;
  2277. obj = intel_fb_obj(fb);
  2278. if (obj->tiling_mode != I915_TILING_NONE)
  2279. dev_priv->preserve_bios_swizzle = true;
  2280. drm_framebuffer_reference(fb);
  2281. primary->fb = primary->state->fb = fb;
  2282. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2283. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2284. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2285. }
  2286. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2287. struct drm_framebuffer *fb,
  2288. int x, int y)
  2289. {
  2290. struct drm_device *dev = crtc->dev;
  2291. struct drm_i915_private *dev_priv = dev->dev_private;
  2292. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2293. struct drm_plane *primary = crtc->primary;
  2294. bool visible = to_intel_plane_state(primary->state)->visible;
  2295. struct drm_i915_gem_object *obj;
  2296. int plane = intel_crtc->plane;
  2297. unsigned long linear_offset;
  2298. u32 dspcntr;
  2299. u32 reg = DSPCNTR(plane);
  2300. int pixel_size;
  2301. if (!visible || !fb) {
  2302. I915_WRITE(reg, 0);
  2303. if (INTEL_INFO(dev)->gen >= 4)
  2304. I915_WRITE(DSPSURF(plane), 0);
  2305. else
  2306. I915_WRITE(DSPADDR(plane), 0);
  2307. POSTING_READ(reg);
  2308. return;
  2309. }
  2310. obj = intel_fb_obj(fb);
  2311. if (WARN_ON(obj == NULL))
  2312. return;
  2313. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2314. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2315. dspcntr |= DISPLAY_PLANE_ENABLE;
  2316. if (INTEL_INFO(dev)->gen < 4) {
  2317. if (intel_crtc->pipe == PIPE_B)
  2318. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2319. /* pipesrc and dspsize control the size that is scaled from,
  2320. * which should always be the user's requested size.
  2321. */
  2322. I915_WRITE(DSPSIZE(plane),
  2323. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2324. (intel_crtc->config->pipe_src_w - 1));
  2325. I915_WRITE(DSPPOS(plane), 0);
  2326. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2327. I915_WRITE(PRIMSIZE(plane),
  2328. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2329. (intel_crtc->config->pipe_src_w - 1));
  2330. I915_WRITE(PRIMPOS(plane), 0);
  2331. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2332. }
  2333. switch (fb->pixel_format) {
  2334. case DRM_FORMAT_C8:
  2335. dspcntr |= DISPPLANE_8BPP;
  2336. break;
  2337. case DRM_FORMAT_XRGB1555:
  2338. dspcntr |= DISPPLANE_BGRX555;
  2339. break;
  2340. case DRM_FORMAT_RGB565:
  2341. dspcntr |= DISPPLANE_BGRX565;
  2342. break;
  2343. case DRM_FORMAT_XRGB8888:
  2344. dspcntr |= DISPPLANE_BGRX888;
  2345. break;
  2346. case DRM_FORMAT_XBGR8888:
  2347. dspcntr |= DISPPLANE_RGBX888;
  2348. break;
  2349. case DRM_FORMAT_XRGB2101010:
  2350. dspcntr |= DISPPLANE_BGRX101010;
  2351. break;
  2352. case DRM_FORMAT_XBGR2101010:
  2353. dspcntr |= DISPPLANE_RGBX101010;
  2354. break;
  2355. default:
  2356. BUG();
  2357. }
  2358. if (INTEL_INFO(dev)->gen >= 4 &&
  2359. obj->tiling_mode != I915_TILING_NONE)
  2360. dspcntr |= DISPPLANE_TILED;
  2361. if (IS_G4X(dev))
  2362. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2363. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2364. if (INTEL_INFO(dev)->gen >= 4) {
  2365. intel_crtc->dspaddr_offset =
  2366. intel_gen4_compute_page_offset(dev_priv,
  2367. &x, &y, obj->tiling_mode,
  2368. pixel_size,
  2369. fb->pitches[0]);
  2370. linear_offset -= intel_crtc->dspaddr_offset;
  2371. } else {
  2372. intel_crtc->dspaddr_offset = linear_offset;
  2373. }
  2374. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2375. dspcntr |= DISPPLANE_ROTATE_180;
  2376. x += (intel_crtc->config->pipe_src_w - 1);
  2377. y += (intel_crtc->config->pipe_src_h - 1);
  2378. /* Finding the last pixel of the last line of the display
  2379. data and adding to linear_offset*/
  2380. linear_offset +=
  2381. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2382. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2383. }
  2384. I915_WRITE(reg, dspcntr);
  2385. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2386. if (INTEL_INFO(dev)->gen >= 4) {
  2387. I915_WRITE(DSPSURF(plane),
  2388. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2389. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2390. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2391. } else
  2392. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2393. POSTING_READ(reg);
  2394. }
  2395. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2396. struct drm_framebuffer *fb,
  2397. int x, int y)
  2398. {
  2399. struct drm_device *dev = crtc->dev;
  2400. struct drm_i915_private *dev_priv = dev->dev_private;
  2401. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2402. struct drm_plane *primary = crtc->primary;
  2403. bool visible = to_intel_plane_state(primary->state)->visible;
  2404. struct drm_i915_gem_object *obj;
  2405. int plane = intel_crtc->plane;
  2406. unsigned long linear_offset;
  2407. u32 dspcntr;
  2408. u32 reg = DSPCNTR(plane);
  2409. int pixel_size;
  2410. if (!visible || !fb) {
  2411. I915_WRITE(reg, 0);
  2412. I915_WRITE(DSPSURF(plane), 0);
  2413. POSTING_READ(reg);
  2414. return;
  2415. }
  2416. obj = intel_fb_obj(fb);
  2417. if (WARN_ON(obj == NULL))
  2418. return;
  2419. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2420. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2421. dspcntr |= DISPLAY_PLANE_ENABLE;
  2422. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2423. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2424. switch (fb->pixel_format) {
  2425. case DRM_FORMAT_C8:
  2426. dspcntr |= DISPPLANE_8BPP;
  2427. break;
  2428. case DRM_FORMAT_RGB565:
  2429. dspcntr |= DISPPLANE_BGRX565;
  2430. break;
  2431. case DRM_FORMAT_XRGB8888:
  2432. dspcntr |= DISPPLANE_BGRX888;
  2433. break;
  2434. case DRM_FORMAT_XBGR8888:
  2435. dspcntr |= DISPPLANE_RGBX888;
  2436. break;
  2437. case DRM_FORMAT_XRGB2101010:
  2438. dspcntr |= DISPPLANE_BGRX101010;
  2439. break;
  2440. case DRM_FORMAT_XBGR2101010:
  2441. dspcntr |= DISPPLANE_RGBX101010;
  2442. break;
  2443. default:
  2444. BUG();
  2445. }
  2446. if (obj->tiling_mode != I915_TILING_NONE)
  2447. dspcntr |= DISPPLANE_TILED;
  2448. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2449. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2450. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2451. intel_crtc->dspaddr_offset =
  2452. intel_gen4_compute_page_offset(dev_priv,
  2453. &x, &y, obj->tiling_mode,
  2454. pixel_size,
  2455. fb->pitches[0]);
  2456. linear_offset -= intel_crtc->dspaddr_offset;
  2457. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2458. dspcntr |= DISPPLANE_ROTATE_180;
  2459. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2460. x += (intel_crtc->config->pipe_src_w - 1);
  2461. y += (intel_crtc->config->pipe_src_h - 1);
  2462. /* Finding the last pixel of the last line of the display
  2463. data and adding to linear_offset*/
  2464. linear_offset +=
  2465. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2466. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2467. }
  2468. }
  2469. I915_WRITE(reg, dspcntr);
  2470. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2471. I915_WRITE(DSPSURF(plane),
  2472. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2473. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2474. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2475. } else {
  2476. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2477. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2478. }
  2479. POSTING_READ(reg);
  2480. }
  2481. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2482. uint32_t pixel_format)
  2483. {
  2484. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2485. /*
  2486. * The stride is either expressed as a multiple of 64 bytes
  2487. * chunks for linear buffers or in number of tiles for tiled
  2488. * buffers.
  2489. */
  2490. switch (fb_modifier) {
  2491. case DRM_FORMAT_MOD_NONE:
  2492. return 64;
  2493. case I915_FORMAT_MOD_X_TILED:
  2494. if (INTEL_INFO(dev)->gen == 2)
  2495. return 128;
  2496. return 512;
  2497. case I915_FORMAT_MOD_Y_TILED:
  2498. /* No need to check for old gens and Y tiling since this is
  2499. * about the display engine and those will be blocked before
  2500. * we get here.
  2501. */
  2502. return 128;
  2503. case I915_FORMAT_MOD_Yf_TILED:
  2504. if (bits_per_pixel == 8)
  2505. return 64;
  2506. else
  2507. return 128;
  2508. default:
  2509. MISSING_CASE(fb_modifier);
  2510. return 64;
  2511. }
  2512. }
  2513. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2514. struct drm_i915_gem_object *obj)
  2515. {
  2516. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2517. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2518. view = &i915_ggtt_view_rotated;
  2519. return i915_gem_obj_ggtt_offset_view(obj, view);
  2520. }
  2521. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2522. {
  2523. struct drm_device *dev = intel_crtc->base.dev;
  2524. struct drm_i915_private *dev_priv = dev->dev_private;
  2525. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2526. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2527. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2528. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2529. intel_crtc->base.base.id, intel_crtc->pipe, id);
  2530. }
  2531. /*
  2532. * This function detaches (aka. unbinds) unused scalers in hardware
  2533. */
  2534. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2535. {
  2536. struct intel_crtc_scaler_state *scaler_state;
  2537. int i;
  2538. scaler_state = &intel_crtc->config->scaler_state;
  2539. /* loop through and disable scalers that aren't in use */
  2540. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2541. if (!scaler_state->scalers[i].in_use)
  2542. skl_detach_scaler(intel_crtc, i);
  2543. }
  2544. }
  2545. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2546. {
  2547. switch (pixel_format) {
  2548. case DRM_FORMAT_C8:
  2549. return PLANE_CTL_FORMAT_INDEXED;
  2550. case DRM_FORMAT_RGB565:
  2551. return PLANE_CTL_FORMAT_RGB_565;
  2552. case DRM_FORMAT_XBGR8888:
  2553. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2554. case DRM_FORMAT_XRGB8888:
  2555. return PLANE_CTL_FORMAT_XRGB_8888;
  2556. /*
  2557. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2558. * to be already pre-multiplied. We need to add a knob (or a different
  2559. * DRM_FORMAT) for user-space to configure that.
  2560. */
  2561. case DRM_FORMAT_ABGR8888:
  2562. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2563. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2564. case DRM_FORMAT_ARGB8888:
  2565. return PLANE_CTL_FORMAT_XRGB_8888 |
  2566. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2567. case DRM_FORMAT_XRGB2101010:
  2568. return PLANE_CTL_FORMAT_XRGB_2101010;
  2569. case DRM_FORMAT_XBGR2101010:
  2570. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2571. case DRM_FORMAT_YUYV:
  2572. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2573. case DRM_FORMAT_YVYU:
  2574. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2575. case DRM_FORMAT_UYVY:
  2576. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2577. case DRM_FORMAT_VYUY:
  2578. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2579. default:
  2580. MISSING_CASE(pixel_format);
  2581. }
  2582. return 0;
  2583. }
  2584. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2585. {
  2586. switch (fb_modifier) {
  2587. case DRM_FORMAT_MOD_NONE:
  2588. break;
  2589. case I915_FORMAT_MOD_X_TILED:
  2590. return PLANE_CTL_TILED_X;
  2591. case I915_FORMAT_MOD_Y_TILED:
  2592. return PLANE_CTL_TILED_Y;
  2593. case I915_FORMAT_MOD_Yf_TILED:
  2594. return PLANE_CTL_TILED_YF;
  2595. default:
  2596. MISSING_CASE(fb_modifier);
  2597. }
  2598. return 0;
  2599. }
  2600. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2601. {
  2602. switch (rotation) {
  2603. case BIT(DRM_ROTATE_0):
  2604. break;
  2605. /*
  2606. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2607. * while i915 HW rotation is clockwise, thats why this swapping.
  2608. */
  2609. case BIT(DRM_ROTATE_90):
  2610. return PLANE_CTL_ROTATE_270;
  2611. case BIT(DRM_ROTATE_180):
  2612. return PLANE_CTL_ROTATE_180;
  2613. case BIT(DRM_ROTATE_270):
  2614. return PLANE_CTL_ROTATE_90;
  2615. default:
  2616. MISSING_CASE(rotation);
  2617. }
  2618. return 0;
  2619. }
  2620. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2621. struct drm_framebuffer *fb,
  2622. int x, int y)
  2623. {
  2624. struct drm_device *dev = crtc->dev;
  2625. struct drm_i915_private *dev_priv = dev->dev_private;
  2626. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2627. struct drm_plane *plane = crtc->primary;
  2628. bool visible = to_intel_plane_state(plane->state)->visible;
  2629. struct drm_i915_gem_object *obj;
  2630. int pipe = intel_crtc->pipe;
  2631. u32 plane_ctl, stride_div, stride;
  2632. u32 tile_height, plane_offset, plane_size;
  2633. unsigned int rotation;
  2634. int x_offset, y_offset;
  2635. unsigned long surf_addr;
  2636. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2637. struct intel_plane_state *plane_state;
  2638. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2639. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2640. int scaler_id = -1;
  2641. plane_state = to_intel_plane_state(plane->state);
  2642. if (!visible || !fb) {
  2643. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2644. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2645. POSTING_READ(PLANE_CTL(pipe, 0));
  2646. return;
  2647. }
  2648. plane_ctl = PLANE_CTL_ENABLE |
  2649. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2650. PLANE_CTL_PIPE_CSC_ENABLE;
  2651. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2652. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2653. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2654. rotation = plane->state->rotation;
  2655. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2656. obj = intel_fb_obj(fb);
  2657. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2658. fb->pixel_format);
  2659. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2660. /*
  2661. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2662. * update_plane helpers are called from legacy paths.
  2663. * Once full atomic crtc is available, below check can be avoided.
  2664. */
  2665. if (drm_rect_width(&plane_state->src)) {
  2666. scaler_id = plane_state->scaler_id;
  2667. src_x = plane_state->src.x1 >> 16;
  2668. src_y = plane_state->src.y1 >> 16;
  2669. src_w = drm_rect_width(&plane_state->src) >> 16;
  2670. src_h = drm_rect_height(&plane_state->src) >> 16;
  2671. dst_x = plane_state->dst.x1;
  2672. dst_y = plane_state->dst.y1;
  2673. dst_w = drm_rect_width(&plane_state->dst);
  2674. dst_h = drm_rect_height(&plane_state->dst);
  2675. WARN_ON(x != src_x || y != src_y);
  2676. } else {
  2677. src_w = intel_crtc->config->pipe_src_w;
  2678. src_h = intel_crtc->config->pipe_src_h;
  2679. }
  2680. if (intel_rotation_90_or_270(rotation)) {
  2681. /* stride = Surface height in tiles */
  2682. tile_height = intel_tile_height(dev, fb->pixel_format,
  2683. fb->modifier[0]);
  2684. stride = DIV_ROUND_UP(fb->height, tile_height);
  2685. x_offset = stride * tile_height - y - src_h;
  2686. y_offset = x;
  2687. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2688. } else {
  2689. stride = fb->pitches[0] / stride_div;
  2690. x_offset = x;
  2691. y_offset = y;
  2692. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2693. }
  2694. plane_offset = y_offset << 16 | x_offset;
  2695. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2696. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2697. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2698. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2699. if (scaler_id >= 0) {
  2700. uint32_t ps_ctrl = 0;
  2701. WARN_ON(!dst_w || !dst_h);
  2702. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2703. crtc_state->scaler_state.scalers[scaler_id].mode;
  2704. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2705. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2706. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2707. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2708. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2709. } else {
  2710. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2711. }
  2712. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2713. POSTING_READ(PLANE_SURF(pipe, 0));
  2714. }
  2715. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2716. static int
  2717. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2718. int x, int y, enum mode_set_atomic state)
  2719. {
  2720. struct drm_device *dev = crtc->dev;
  2721. struct drm_i915_private *dev_priv = dev->dev_private;
  2722. if (dev_priv->fbc.disable_fbc)
  2723. dev_priv->fbc.disable_fbc(dev_priv);
  2724. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2725. return 0;
  2726. }
  2727. static void intel_complete_page_flips(struct drm_device *dev)
  2728. {
  2729. struct drm_crtc *crtc;
  2730. for_each_crtc(dev, crtc) {
  2731. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2732. enum plane plane = intel_crtc->plane;
  2733. intel_prepare_page_flip(dev, plane);
  2734. intel_finish_page_flip_plane(dev, plane);
  2735. }
  2736. }
  2737. static void intel_update_primary_planes(struct drm_device *dev)
  2738. {
  2739. struct drm_i915_private *dev_priv = dev->dev_private;
  2740. struct drm_crtc *crtc;
  2741. for_each_crtc(dev, crtc) {
  2742. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2743. drm_modeset_lock(&crtc->mutex, NULL);
  2744. /*
  2745. * FIXME: Once we have proper support for primary planes (and
  2746. * disabling them without disabling the entire crtc) allow again
  2747. * a NULL crtc->primary->fb.
  2748. */
  2749. if (intel_crtc->active && crtc->primary->fb)
  2750. dev_priv->display.update_primary_plane(crtc,
  2751. crtc->primary->fb,
  2752. crtc->x,
  2753. crtc->y);
  2754. drm_modeset_unlock(&crtc->mutex);
  2755. }
  2756. }
  2757. void intel_prepare_reset(struct drm_device *dev)
  2758. {
  2759. /* no reset support for gen2 */
  2760. if (IS_GEN2(dev))
  2761. return;
  2762. /* reset doesn't touch the display */
  2763. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2764. return;
  2765. drm_modeset_lock_all(dev);
  2766. /*
  2767. * Disabling the crtcs gracefully seems nicer. Also the
  2768. * g33 docs say we should at least disable all the planes.
  2769. */
  2770. intel_display_suspend(dev);
  2771. }
  2772. void intel_finish_reset(struct drm_device *dev)
  2773. {
  2774. struct drm_i915_private *dev_priv = to_i915(dev);
  2775. /*
  2776. * Flips in the rings will be nuked by the reset,
  2777. * so complete all pending flips so that user space
  2778. * will get its events and not get stuck.
  2779. */
  2780. intel_complete_page_flips(dev);
  2781. /* no reset support for gen2 */
  2782. if (IS_GEN2(dev))
  2783. return;
  2784. /* reset doesn't touch the display */
  2785. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2786. /*
  2787. * Flips in the rings have been nuked by the reset,
  2788. * so update the base address of all primary
  2789. * planes to the the last fb to make sure we're
  2790. * showing the correct fb after a reset.
  2791. */
  2792. intel_update_primary_planes(dev);
  2793. return;
  2794. }
  2795. /*
  2796. * The display has been reset as well,
  2797. * so need a full re-initialization.
  2798. */
  2799. intel_runtime_pm_disable_interrupts(dev_priv);
  2800. intel_runtime_pm_enable_interrupts(dev_priv);
  2801. intel_modeset_init_hw(dev);
  2802. spin_lock_irq(&dev_priv->irq_lock);
  2803. if (dev_priv->display.hpd_irq_setup)
  2804. dev_priv->display.hpd_irq_setup(dev);
  2805. spin_unlock_irq(&dev_priv->irq_lock);
  2806. intel_display_resume(dev);
  2807. intel_hpd_init(dev_priv);
  2808. drm_modeset_unlock_all(dev);
  2809. }
  2810. static void
  2811. intel_finish_fb(struct drm_framebuffer *old_fb)
  2812. {
  2813. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2814. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2815. bool was_interruptible = dev_priv->mm.interruptible;
  2816. int ret;
  2817. /* Big Hammer, we also need to ensure that any pending
  2818. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2819. * current scanout is retired before unpinning the old
  2820. * framebuffer. Note that we rely on userspace rendering
  2821. * into the buffer attached to the pipe they are waiting
  2822. * on. If not, userspace generates a GPU hang with IPEHR
  2823. * point to the MI_WAIT_FOR_EVENT.
  2824. *
  2825. * This should only fail upon a hung GPU, in which case we
  2826. * can safely continue.
  2827. */
  2828. dev_priv->mm.interruptible = false;
  2829. ret = i915_gem_object_wait_rendering(obj, true);
  2830. dev_priv->mm.interruptible = was_interruptible;
  2831. WARN_ON(ret);
  2832. }
  2833. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2834. {
  2835. struct drm_device *dev = crtc->dev;
  2836. struct drm_i915_private *dev_priv = dev->dev_private;
  2837. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2838. bool pending;
  2839. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2840. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2841. return false;
  2842. spin_lock_irq(&dev->event_lock);
  2843. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2844. spin_unlock_irq(&dev->event_lock);
  2845. return pending;
  2846. }
  2847. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2848. {
  2849. struct drm_device *dev = crtc->base.dev;
  2850. struct drm_i915_private *dev_priv = dev->dev_private;
  2851. const struct drm_display_mode *adjusted_mode;
  2852. if (!i915.fastboot)
  2853. return;
  2854. /*
  2855. * Update pipe size and adjust fitter if needed: the reason for this is
  2856. * that in compute_mode_changes we check the native mode (not the pfit
  2857. * mode) to see if we can flip rather than do a full mode set. In the
  2858. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2859. * pfit state, we'll end up with a big fb scanned out into the wrong
  2860. * sized surface.
  2861. *
  2862. * To fix this properly, we need to hoist the checks up into
  2863. * compute_mode_changes (or above), check the actual pfit state and
  2864. * whether the platform allows pfit disable with pipe active, and only
  2865. * then update the pipesrc and pfit state, even on the flip path.
  2866. */
  2867. adjusted_mode = &crtc->config->base.adjusted_mode;
  2868. I915_WRITE(PIPESRC(crtc->pipe),
  2869. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2870. (adjusted_mode->crtc_vdisplay - 1));
  2871. if (!crtc->config->pch_pfit.enabled &&
  2872. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2873. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2874. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2875. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2876. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2877. }
  2878. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2879. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2880. }
  2881. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2882. {
  2883. struct drm_device *dev = crtc->dev;
  2884. struct drm_i915_private *dev_priv = dev->dev_private;
  2885. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2886. int pipe = intel_crtc->pipe;
  2887. u32 reg, temp;
  2888. /* enable normal train */
  2889. reg = FDI_TX_CTL(pipe);
  2890. temp = I915_READ(reg);
  2891. if (IS_IVYBRIDGE(dev)) {
  2892. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2893. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2894. } else {
  2895. temp &= ~FDI_LINK_TRAIN_NONE;
  2896. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2897. }
  2898. I915_WRITE(reg, temp);
  2899. reg = FDI_RX_CTL(pipe);
  2900. temp = I915_READ(reg);
  2901. if (HAS_PCH_CPT(dev)) {
  2902. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2903. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2904. } else {
  2905. temp &= ~FDI_LINK_TRAIN_NONE;
  2906. temp |= FDI_LINK_TRAIN_NONE;
  2907. }
  2908. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2909. /* wait one idle pattern time */
  2910. POSTING_READ(reg);
  2911. udelay(1000);
  2912. /* IVB wants error correction enabled */
  2913. if (IS_IVYBRIDGE(dev))
  2914. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2915. FDI_FE_ERRC_ENABLE);
  2916. }
  2917. /* The FDI link training functions for ILK/Ibexpeak. */
  2918. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2919. {
  2920. struct drm_device *dev = crtc->dev;
  2921. struct drm_i915_private *dev_priv = dev->dev_private;
  2922. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2923. int pipe = intel_crtc->pipe;
  2924. u32 reg, temp, tries;
  2925. /* FDI needs bits from pipe first */
  2926. assert_pipe_enabled(dev_priv, pipe);
  2927. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2928. for train result */
  2929. reg = FDI_RX_IMR(pipe);
  2930. temp = I915_READ(reg);
  2931. temp &= ~FDI_RX_SYMBOL_LOCK;
  2932. temp &= ~FDI_RX_BIT_LOCK;
  2933. I915_WRITE(reg, temp);
  2934. I915_READ(reg);
  2935. udelay(150);
  2936. /* enable CPU FDI TX and PCH FDI RX */
  2937. reg = FDI_TX_CTL(pipe);
  2938. temp = I915_READ(reg);
  2939. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2940. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2941. temp &= ~FDI_LINK_TRAIN_NONE;
  2942. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2943. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2944. reg = FDI_RX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. temp &= ~FDI_LINK_TRAIN_NONE;
  2947. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2948. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2949. POSTING_READ(reg);
  2950. udelay(150);
  2951. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2952. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2953. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2954. FDI_RX_PHASE_SYNC_POINTER_EN);
  2955. reg = FDI_RX_IIR(pipe);
  2956. for (tries = 0; tries < 5; tries++) {
  2957. temp = I915_READ(reg);
  2958. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2959. if ((temp & FDI_RX_BIT_LOCK)) {
  2960. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2961. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2962. break;
  2963. }
  2964. }
  2965. if (tries == 5)
  2966. DRM_ERROR("FDI train 1 fail!\n");
  2967. /* Train 2 */
  2968. reg = FDI_TX_CTL(pipe);
  2969. temp = I915_READ(reg);
  2970. temp &= ~FDI_LINK_TRAIN_NONE;
  2971. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2972. I915_WRITE(reg, temp);
  2973. reg = FDI_RX_CTL(pipe);
  2974. temp = I915_READ(reg);
  2975. temp &= ~FDI_LINK_TRAIN_NONE;
  2976. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2977. I915_WRITE(reg, temp);
  2978. POSTING_READ(reg);
  2979. udelay(150);
  2980. reg = FDI_RX_IIR(pipe);
  2981. for (tries = 0; tries < 5; tries++) {
  2982. temp = I915_READ(reg);
  2983. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2984. if (temp & FDI_RX_SYMBOL_LOCK) {
  2985. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2986. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2987. break;
  2988. }
  2989. }
  2990. if (tries == 5)
  2991. DRM_ERROR("FDI train 2 fail!\n");
  2992. DRM_DEBUG_KMS("FDI train done\n");
  2993. }
  2994. static const int snb_b_fdi_train_param[] = {
  2995. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2996. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2997. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2998. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2999. };
  3000. /* The FDI link training functions for SNB/Cougarpoint. */
  3001. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3002. {
  3003. struct drm_device *dev = crtc->dev;
  3004. struct drm_i915_private *dev_priv = dev->dev_private;
  3005. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3006. int pipe = intel_crtc->pipe;
  3007. u32 reg, temp, i, retry;
  3008. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3009. for train result */
  3010. reg = FDI_RX_IMR(pipe);
  3011. temp = I915_READ(reg);
  3012. temp &= ~FDI_RX_SYMBOL_LOCK;
  3013. temp &= ~FDI_RX_BIT_LOCK;
  3014. I915_WRITE(reg, temp);
  3015. POSTING_READ(reg);
  3016. udelay(150);
  3017. /* enable CPU FDI TX and PCH FDI RX */
  3018. reg = FDI_TX_CTL(pipe);
  3019. temp = I915_READ(reg);
  3020. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3021. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3022. temp &= ~FDI_LINK_TRAIN_NONE;
  3023. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3024. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3025. /* SNB-B */
  3026. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3027. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3028. I915_WRITE(FDI_RX_MISC(pipe),
  3029. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3030. reg = FDI_RX_CTL(pipe);
  3031. temp = I915_READ(reg);
  3032. if (HAS_PCH_CPT(dev)) {
  3033. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3034. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3035. } else {
  3036. temp &= ~FDI_LINK_TRAIN_NONE;
  3037. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3038. }
  3039. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3040. POSTING_READ(reg);
  3041. udelay(150);
  3042. for (i = 0; i < 4; i++) {
  3043. reg = FDI_TX_CTL(pipe);
  3044. temp = I915_READ(reg);
  3045. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3046. temp |= snb_b_fdi_train_param[i];
  3047. I915_WRITE(reg, temp);
  3048. POSTING_READ(reg);
  3049. udelay(500);
  3050. for (retry = 0; retry < 5; retry++) {
  3051. reg = FDI_RX_IIR(pipe);
  3052. temp = I915_READ(reg);
  3053. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3054. if (temp & FDI_RX_BIT_LOCK) {
  3055. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3056. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3057. break;
  3058. }
  3059. udelay(50);
  3060. }
  3061. if (retry < 5)
  3062. break;
  3063. }
  3064. if (i == 4)
  3065. DRM_ERROR("FDI train 1 fail!\n");
  3066. /* Train 2 */
  3067. reg = FDI_TX_CTL(pipe);
  3068. temp = I915_READ(reg);
  3069. temp &= ~FDI_LINK_TRAIN_NONE;
  3070. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3071. if (IS_GEN6(dev)) {
  3072. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3073. /* SNB-B */
  3074. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3075. }
  3076. I915_WRITE(reg, temp);
  3077. reg = FDI_RX_CTL(pipe);
  3078. temp = I915_READ(reg);
  3079. if (HAS_PCH_CPT(dev)) {
  3080. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3081. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3082. } else {
  3083. temp &= ~FDI_LINK_TRAIN_NONE;
  3084. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3085. }
  3086. I915_WRITE(reg, temp);
  3087. POSTING_READ(reg);
  3088. udelay(150);
  3089. for (i = 0; i < 4; i++) {
  3090. reg = FDI_TX_CTL(pipe);
  3091. temp = I915_READ(reg);
  3092. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3093. temp |= snb_b_fdi_train_param[i];
  3094. I915_WRITE(reg, temp);
  3095. POSTING_READ(reg);
  3096. udelay(500);
  3097. for (retry = 0; retry < 5; retry++) {
  3098. reg = FDI_RX_IIR(pipe);
  3099. temp = I915_READ(reg);
  3100. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3101. if (temp & FDI_RX_SYMBOL_LOCK) {
  3102. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3103. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3104. break;
  3105. }
  3106. udelay(50);
  3107. }
  3108. if (retry < 5)
  3109. break;
  3110. }
  3111. if (i == 4)
  3112. DRM_ERROR("FDI train 2 fail!\n");
  3113. DRM_DEBUG_KMS("FDI train done.\n");
  3114. }
  3115. /* Manual link training for Ivy Bridge A0 parts */
  3116. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3117. {
  3118. struct drm_device *dev = crtc->dev;
  3119. struct drm_i915_private *dev_priv = dev->dev_private;
  3120. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3121. int pipe = intel_crtc->pipe;
  3122. u32 reg, temp, i, j;
  3123. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3124. for train result */
  3125. reg = FDI_RX_IMR(pipe);
  3126. temp = I915_READ(reg);
  3127. temp &= ~FDI_RX_SYMBOL_LOCK;
  3128. temp &= ~FDI_RX_BIT_LOCK;
  3129. I915_WRITE(reg, temp);
  3130. POSTING_READ(reg);
  3131. udelay(150);
  3132. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3133. I915_READ(FDI_RX_IIR(pipe)));
  3134. /* Try each vswing and preemphasis setting twice before moving on */
  3135. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3136. /* disable first in case we need to retry */
  3137. reg = FDI_TX_CTL(pipe);
  3138. temp = I915_READ(reg);
  3139. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3140. temp &= ~FDI_TX_ENABLE;
  3141. I915_WRITE(reg, temp);
  3142. reg = FDI_RX_CTL(pipe);
  3143. temp = I915_READ(reg);
  3144. temp &= ~FDI_LINK_TRAIN_AUTO;
  3145. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3146. temp &= ~FDI_RX_ENABLE;
  3147. I915_WRITE(reg, temp);
  3148. /* enable CPU FDI TX and PCH FDI RX */
  3149. reg = FDI_TX_CTL(pipe);
  3150. temp = I915_READ(reg);
  3151. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3152. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3153. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3154. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3155. temp |= snb_b_fdi_train_param[j/2];
  3156. temp |= FDI_COMPOSITE_SYNC;
  3157. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3158. I915_WRITE(FDI_RX_MISC(pipe),
  3159. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3160. reg = FDI_RX_CTL(pipe);
  3161. temp = I915_READ(reg);
  3162. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3163. temp |= FDI_COMPOSITE_SYNC;
  3164. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3165. POSTING_READ(reg);
  3166. udelay(1); /* should be 0.5us */
  3167. for (i = 0; i < 4; i++) {
  3168. reg = FDI_RX_IIR(pipe);
  3169. temp = I915_READ(reg);
  3170. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3171. if (temp & FDI_RX_BIT_LOCK ||
  3172. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3173. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3174. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3175. i);
  3176. break;
  3177. }
  3178. udelay(1); /* should be 0.5us */
  3179. }
  3180. if (i == 4) {
  3181. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3182. continue;
  3183. }
  3184. /* Train 2 */
  3185. reg = FDI_TX_CTL(pipe);
  3186. temp = I915_READ(reg);
  3187. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3188. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3189. I915_WRITE(reg, temp);
  3190. reg = FDI_RX_CTL(pipe);
  3191. temp = I915_READ(reg);
  3192. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3193. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3194. I915_WRITE(reg, temp);
  3195. POSTING_READ(reg);
  3196. udelay(2); /* should be 1.5us */
  3197. for (i = 0; i < 4; i++) {
  3198. reg = FDI_RX_IIR(pipe);
  3199. temp = I915_READ(reg);
  3200. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3201. if (temp & FDI_RX_SYMBOL_LOCK ||
  3202. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3203. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3204. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3205. i);
  3206. goto train_done;
  3207. }
  3208. udelay(2); /* should be 1.5us */
  3209. }
  3210. if (i == 4)
  3211. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3212. }
  3213. train_done:
  3214. DRM_DEBUG_KMS("FDI train done.\n");
  3215. }
  3216. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3217. {
  3218. struct drm_device *dev = intel_crtc->base.dev;
  3219. struct drm_i915_private *dev_priv = dev->dev_private;
  3220. int pipe = intel_crtc->pipe;
  3221. u32 reg, temp;
  3222. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3223. reg = FDI_RX_CTL(pipe);
  3224. temp = I915_READ(reg);
  3225. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3226. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3227. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3228. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3229. POSTING_READ(reg);
  3230. udelay(200);
  3231. /* Switch from Rawclk to PCDclk */
  3232. temp = I915_READ(reg);
  3233. I915_WRITE(reg, temp | FDI_PCDCLK);
  3234. POSTING_READ(reg);
  3235. udelay(200);
  3236. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3237. reg = FDI_TX_CTL(pipe);
  3238. temp = I915_READ(reg);
  3239. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3240. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3241. POSTING_READ(reg);
  3242. udelay(100);
  3243. }
  3244. }
  3245. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3246. {
  3247. struct drm_device *dev = intel_crtc->base.dev;
  3248. struct drm_i915_private *dev_priv = dev->dev_private;
  3249. int pipe = intel_crtc->pipe;
  3250. u32 reg, temp;
  3251. /* Switch from PCDclk to Rawclk */
  3252. reg = FDI_RX_CTL(pipe);
  3253. temp = I915_READ(reg);
  3254. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3255. /* Disable CPU FDI TX PLL */
  3256. reg = FDI_TX_CTL(pipe);
  3257. temp = I915_READ(reg);
  3258. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3259. POSTING_READ(reg);
  3260. udelay(100);
  3261. reg = FDI_RX_CTL(pipe);
  3262. temp = I915_READ(reg);
  3263. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3264. /* Wait for the clocks to turn off. */
  3265. POSTING_READ(reg);
  3266. udelay(100);
  3267. }
  3268. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3269. {
  3270. struct drm_device *dev = crtc->dev;
  3271. struct drm_i915_private *dev_priv = dev->dev_private;
  3272. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3273. int pipe = intel_crtc->pipe;
  3274. u32 reg, temp;
  3275. /* disable CPU FDI tx and PCH FDI rx */
  3276. reg = FDI_TX_CTL(pipe);
  3277. temp = I915_READ(reg);
  3278. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3279. POSTING_READ(reg);
  3280. reg = FDI_RX_CTL(pipe);
  3281. temp = I915_READ(reg);
  3282. temp &= ~(0x7 << 16);
  3283. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3284. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3285. POSTING_READ(reg);
  3286. udelay(100);
  3287. /* Ironlake workaround, disable clock pointer after downing FDI */
  3288. if (HAS_PCH_IBX(dev))
  3289. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3290. /* still set train pattern 1 */
  3291. reg = FDI_TX_CTL(pipe);
  3292. temp = I915_READ(reg);
  3293. temp &= ~FDI_LINK_TRAIN_NONE;
  3294. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3295. I915_WRITE(reg, temp);
  3296. reg = FDI_RX_CTL(pipe);
  3297. temp = I915_READ(reg);
  3298. if (HAS_PCH_CPT(dev)) {
  3299. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3300. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3301. } else {
  3302. temp &= ~FDI_LINK_TRAIN_NONE;
  3303. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3304. }
  3305. /* BPC in FDI rx is consistent with that in PIPECONF */
  3306. temp &= ~(0x07 << 16);
  3307. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3308. I915_WRITE(reg, temp);
  3309. POSTING_READ(reg);
  3310. udelay(100);
  3311. }
  3312. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3313. {
  3314. struct intel_crtc *crtc;
  3315. /* Note that we don't need to be called with mode_config.lock here
  3316. * as our list of CRTC objects is static for the lifetime of the
  3317. * device and so cannot disappear as we iterate. Similarly, we can
  3318. * happily treat the predicates as racy, atomic checks as userspace
  3319. * cannot claim and pin a new fb without at least acquring the
  3320. * struct_mutex and so serialising with us.
  3321. */
  3322. for_each_intel_crtc(dev, crtc) {
  3323. if (atomic_read(&crtc->unpin_work_count) == 0)
  3324. continue;
  3325. if (crtc->unpin_work)
  3326. intel_wait_for_vblank(dev, crtc->pipe);
  3327. return true;
  3328. }
  3329. return false;
  3330. }
  3331. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3332. {
  3333. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3334. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3335. /* ensure that the unpin work is consistent wrt ->pending. */
  3336. smp_rmb();
  3337. intel_crtc->unpin_work = NULL;
  3338. if (work->event)
  3339. drm_send_vblank_event(intel_crtc->base.dev,
  3340. intel_crtc->pipe,
  3341. work->event);
  3342. drm_crtc_vblank_put(&intel_crtc->base);
  3343. wake_up_all(&dev_priv->pending_flip_queue);
  3344. queue_work(dev_priv->wq, &work->work);
  3345. trace_i915_flip_complete(intel_crtc->plane,
  3346. work->pending_flip_obj);
  3347. }
  3348. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3349. {
  3350. struct drm_device *dev = crtc->dev;
  3351. struct drm_i915_private *dev_priv = dev->dev_private;
  3352. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3353. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3354. !intel_crtc_has_pending_flip(crtc),
  3355. 60*HZ) == 0)) {
  3356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3357. spin_lock_irq(&dev->event_lock);
  3358. if (intel_crtc->unpin_work) {
  3359. WARN_ONCE(1, "Removing stuck page flip\n");
  3360. page_flip_completed(intel_crtc);
  3361. }
  3362. spin_unlock_irq(&dev->event_lock);
  3363. }
  3364. if (crtc->primary->fb) {
  3365. mutex_lock(&dev->struct_mutex);
  3366. intel_finish_fb(crtc->primary->fb);
  3367. mutex_unlock(&dev->struct_mutex);
  3368. }
  3369. }
  3370. /* Program iCLKIP clock to the desired frequency */
  3371. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3372. {
  3373. struct drm_device *dev = crtc->dev;
  3374. struct drm_i915_private *dev_priv = dev->dev_private;
  3375. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3376. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3377. u32 temp;
  3378. mutex_lock(&dev_priv->sb_lock);
  3379. /* It is necessary to ungate the pixclk gate prior to programming
  3380. * the divisors, and gate it back when it is done.
  3381. */
  3382. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3383. /* Disable SSCCTL */
  3384. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3385. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3386. SBI_SSCCTL_DISABLE,
  3387. SBI_ICLK);
  3388. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3389. if (clock == 20000) {
  3390. auxdiv = 1;
  3391. divsel = 0x41;
  3392. phaseinc = 0x20;
  3393. } else {
  3394. /* The iCLK virtual clock root frequency is in MHz,
  3395. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3396. * divisors, it is necessary to divide one by another, so we
  3397. * convert the virtual clock precision to KHz here for higher
  3398. * precision.
  3399. */
  3400. u32 iclk_virtual_root_freq = 172800 * 1000;
  3401. u32 iclk_pi_range = 64;
  3402. u32 desired_divisor, msb_divisor_value, pi_value;
  3403. desired_divisor = (iclk_virtual_root_freq / clock);
  3404. msb_divisor_value = desired_divisor / iclk_pi_range;
  3405. pi_value = desired_divisor % iclk_pi_range;
  3406. auxdiv = 0;
  3407. divsel = msb_divisor_value - 2;
  3408. phaseinc = pi_value;
  3409. }
  3410. /* This should not happen with any sane values */
  3411. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3412. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3413. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3414. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3415. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3416. clock,
  3417. auxdiv,
  3418. divsel,
  3419. phasedir,
  3420. phaseinc);
  3421. /* Program SSCDIVINTPHASE6 */
  3422. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3423. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3424. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3425. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3426. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3427. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3428. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3429. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3430. /* Program SSCAUXDIV */
  3431. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3432. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3433. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3434. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3435. /* Enable modulator and associated divider */
  3436. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3437. temp &= ~SBI_SSCCTL_DISABLE;
  3438. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3439. /* Wait for initialization time */
  3440. udelay(24);
  3441. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3442. mutex_unlock(&dev_priv->sb_lock);
  3443. }
  3444. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3445. enum pipe pch_transcoder)
  3446. {
  3447. struct drm_device *dev = crtc->base.dev;
  3448. struct drm_i915_private *dev_priv = dev->dev_private;
  3449. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3450. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3451. I915_READ(HTOTAL(cpu_transcoder)));
  3452. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3453. I915_READ(HBLANK(cpu_transcoder)));
  3454. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3455. I915_READ(HSYNC(cpu_transcoder)));
  3456. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3457. I915_READ(VTOTAL(cpu_transcoder)));
  3458. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3459. I915_READ(VBLANK(cpu_transcoder)));
  3460. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3461. I915_READ(VSYNC(cpu_transcoder)));
  3462. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3463. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3464. }
  3465. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3466. {
  3467. struct drm_i915_private *dev_priv = dev->dev_private;
  3468. uint32_t temp;
  3469. temp = I915_READ(SOUTH_CHICKEN1);
  3470. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3471. return;
  3472. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3473. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3474. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3475. if (enable)
  3476. temp |= FDI_BC_BIFURCATION_SELECT;
  3477. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3478. I915_WRITE(SOUTH_CHICKEN1, temp);
  3479. POSTING_READ(SOUTH_CHICKEN1);
  3480. }
  3481. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3482. {
  3483. struct drm_device *dev = intel_crtc->base.dev;
  3484. switch (intel_crtc->pipe) {
  3485. case PIPE_A:
  3486. break;
  3487. case PIPE_B:
  3488. if (intel_crtc->config->fdi_lanes > 2)
  3489. cpt_set_fdi_bc_bifurcation(dev, false);
  3490. else
  3491. cpt_set_fdi_bc_bifurcation(dev, true);
  3492. break;
  3493. case PIPE_C:
  3494. cpt_set_fdi_bc_bifurcation(dev, true);
  3495. break;
  3496. default:
  3497. BUG();
  3498. }
  3499. }
  3500. /*
  3501. * Enable PCH resources required for PCH ports:
  3502. * - PCH PLLs
  3503. * - FDI training & RX/TX
  3504. * - update transcoder timings
  3505. * - DP transcoding bits
  3506. * - transcoder
  3507. */
  3508. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3509. {
  3510. struct drm_device *dev = crtc->dev;
  3511. struct drm_i915_private *dev_priv = dev->dev_private;
  3512. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3513. int pipe = intel_crtc->pipe;
  3514. u32 reg, temp;
  3515. assert_pch_transcoder_disabled(dev_priv, pipe);
  3516. if (IS_IVYBRIDGE(dev))
  3517. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3518. /* Write the TU size bits before fdi link training, so that error
  3519. * detection works. */
  3520. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3521. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3522. /* For PCH output, training FDI link */
  3523. dev_priv->display.fdi_link_train(crtc);
  3524. /* We need to program the right clock selection before writing the pixel
  3525. * mutliplier into the DPLL. */
  3526. if (HAS_PCH_CPT(dev)) {
  3527. u32 sel;
  3528. temp = I915_READ(PCH_DPLL_SEL);
  3529. temp |= TRANS_DPLL_ENABLE(pipe);
  3530. sel = TRANS_DPLLB_SEL(pipe);
  3531. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3532. temp |= sel;
  3533. else
  3534. temp &= ~sel;
  3535. I915_WRITE(PCH_DPLL_SEL, temp);
  3536. }
  3537. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3538. * transcoder, and we actually should do this to not upset any PCH
  3539. * transcoder that already use the clock when we share it.
  3540. *
  3541. * Note that enable_shared_dpll tries to do the right thing, but
  3542. * get_shared_dpll unconditionally resets the pll - we need that to have
  3543. * the right LVDS enable sequence. */
  3544. intel_enable_shared_dpll(intel_crtc);
  3545. /* set transcoder timing, panel must allow it */
  3546. assert_panel_unlocked(dev_priv, pipe);
  3547. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3548. intel_fdi_normal_train(crtc);
  3549. /* For PCH DP, enable TRANS_DP_CTL */
  3550. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3551. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3552. reg = TRANS_DP_CTL(pipe);
  3553. temp = I915_READ(reg);
  3554. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3555. TRANS_DP_SYNC_MASK |
  3556. TRANS_DP_BPC_MASK);
  3557. temp |= TRANS_DP_OUTPUT_ENABLE;
  3558. temp |= bpc << 9; /* same format but at 11:9 */
  3559. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3560. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3561. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3562. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3563. switch (intel_trans_dp_port_sel(crtc)) {
  3564. case PCH_DP_B:
  3565. temp |= TRANS_DP_PORT_SEL_B;
  3566. break;
  3567. case PCH_DP_C:
  3568. temp |= TRANS_DP_PORT_SEL_C;
  3569. break;
  3570. case PCH_DP_D:
  3571. temp |= TRANS_DP_PORT_SEL_D;
  3572. break;
  3573. default:
  3574. BUG();
  3575. }
  3576. I915_WRITE(reg, temp);
  3577. }
  3578. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3579. }
  3580. static void lpt_pch_enable(struct drm_crtc *crtc)
  3581. {
  3582. struct drm_device *dev = crtc->dev;
  3583. struct drm_i915_private *dev_priv = dev->dev_private;
  3584. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3585. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3586. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3587. lpt_program_iclkip(crtc);
  3588. /* Set transcoder timing. */
  3589. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3590. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3591. }
  3592. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3593. struct intel_crtc_state *crtc_state)
  3594. {
  3595. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3596. struct intel_shared_dpll *pll;
  3597. struct intel_shared_dpll_config *shared_dpll;
  3598. enum intel_dpll_id i;
  3599. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3600. if (HAS_PCH_IBX(dev_priv->dev)) {
  3601. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3602. i = (enum intel_dpll_id) crtc->pipe;
  3603. pll = &dev_priv->shared_dplls[i];
  3604. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3605. crtc->base.base.id, pll->name);
  3606. WARN_ON(shared_dpll[i].crtc_mask);
  3607. goto found;
  3608. }
  3609. if (IS_BROXTON(dev_priv->dev)) {
  3610. /* PLL is attached to port in bxt */
  3611. struct intel_encoder *encoder;
  3612. struct intel_digital_port *intel_dig_port;
  3613. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3614. if (WARN_ON(!encoder))
  3615. return NULL;
  3616. intel_dig_port = enc_to_dig_port(&encoder->base);
  3617. /* 1:1 mapping between ports and PLLs */
  3618. i = (enum intel_dpll_id)intel_dig_port->port;
  3619. pll = &dev_priv->shared_dplls[i];
  3620. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3621. crtc->base.base.id, pll->name);
  3622. WARN_ON(shared_dpll[i].crtc_mask);
  3623. goto found;
  3624. }
  3625. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3626. pll = &dev_priv->shared_dplls[i];
  3627. /* Only want to check enabled timings first */
  3628. if (shared_dpll[i].crtc_mask == 0)
  3629. continue;
  3630. if (memcmp(&crtc_state->dpll_hw_state,
  3631. &shared_dpll[i].hw_state,
  3632. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3633. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3634. crtc->base.base.id, pll->name,
  3635. shared_dpll[i].crtc_mask,
  3636. pll->active);
  3637. goto found;
  3638. }
  3639. }
  3640. /* Ok no matching timings, maybe there's a free one? */
  3641. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3642. pll = &dev_priv->shared_dplls[i];
  3643. if (shared_dpll[i].crtc_mask == 0) {
  3644. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3645. crtc->base.base.id, pll->name);
  3646. goto found;
  3647. }
  3648. }
  3649. return NULL;
  3650. found:
  3651. if (shared_dpll[i].crtc_mask == 0)
  3652. shared_dpll[i].hw_state =
  3653. crtc_state->dpll_hw_state;
  3654. crtc_state->shared_dpll = i;
  3655. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3656. pipe_name(crtc->pipe));
  3657. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3658. return pll;
  3659. }
  3660. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3661. {
  3662. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3663. struct intel_shared_dpll_config *shared_dpll;
  3664. struct intel_shared_dpll *pll;
  3665. enum intel_dpll_id i;
  3666. if (!to_intel_atomic_state(state)->dpll_set)
  3667. return;
  3668. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3669. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3670. pll = &dev_priv->shared_dplls[i];
  3671. pll->config = shared_dpll[i];
  3672. }
  3673. }
  3674. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3675. {
  3676. struct drm_i915_private *dev_priv = dev->dev_private;
  3677. int dslreg = PIPEDSL(pipe);
  3678. u32 temp;
  3679. temp = I915_READ(dslreg);
  3680. udelay(500);
  3681. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3682. if (wait_for(I915_READ(dslreg) != temp, 5))
  3683. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3684. }
  3685. }
  3686. static int
  3687. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3688. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3689. int src_w, int src_h, int dst_w, int dst_h)
  3690. {
  3691. struct intel_crtc_scaler_state *scaler_state =
  3692. &crtc_state->scaler_state;
  3693. struct intel_crtc *intel_crtc =
  3694. to_intel_crtc(crtc_state->base.crtc);
  3695. int need_scaling;
  3696. need_scaling = intel_rotation_90_or_270(rotation) ?
  3697. (src_h != dst_w || src_w != dst_h):
  3698. (src_w != dst_w || src_h != dst_h);
  3699. /*
  3700. * if plane is being disabled or scaler is no more required or force detach
  3701. * - free scaler binded to this plane/crtc
  3702. * - in order to do this, update crtc->scaler_usage
  3703. *
  3704. * Here scaler state in crtc_state is set free so that
  3705. * scaler can be assigned to other user. Actual register
  3706. * update to free the scaler is done in plane/panel-fit programming.
  3707. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3708. */
  3709. if (force_detach || !need_scaling) {
  3710. if (*scaler_id >= 0) {
  3711. scaler_state->scaler_users &= ~(1 << scaler_user);
  3712. scaler_state->scalers[*scaler_id].in_use = 0;
  3713. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3714. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3715. intel_crtc->pipe, scaler_user, *scaler_id,
  3716. scaler_state->scaler_users);
  3717. *scaler_id = -1;
  3718. }
  3719. return 0;
  3720. }
  3721. /* range checks */
  3722. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3723. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3724. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3725. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3726. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3727. "size is out of scaler range\n",
  3728. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3729. return -EINVAL;
  3730. }
  3731. /* mark this plane as a scaler user in crtc_state */
  3732. scaler_state->scaler_users |= (1 << scaler_user);
  3733. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3734. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3735. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3736. scaler_state->scaler_users);
  3737. return 0;
  3738. }
  3739. /**
  3740. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3741. *
  3742. * @state: crtc's scaler state
  3743. *
  3744. * Return
  3745. * 0 - scaler_usage updated successfully
  3746. * error - requested scaling cannot be supported or other error condition
  3747. */
  3748. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3749. {
  3750. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3751. struct drm_display_mode *adjusted_mode =
  3752. &state->base.adjusted_mode;
  3753. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3754. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3755. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3756. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3757. state->pipe_src_w, state->pipe_src_h,
  3758. adjusted_mode->hdisplay, adjusted_mode->vdisplay);
  3759. }
  3760. /**
  3761. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3762. *
  3763. * @state: crtc's scaler state
  3764. * @plane_state: atomic plane state to update
  3765. *
  3766. * Return
  3767. * 0 - scaler_usage updated successfully
  3768. * error - requested scaling cannot be supported or other error condition
  3769. */
  3770. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3771. struct intel_plane_state *plane_state)
  3772. {
  3773. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3774. struct intel_plane *intel_plane =
  3775. to_intel_plane(plane_state->base.plane);
  3776. struct drm_framebuffer *fb = plane_state->base.fb;
  3777. int ret;
  3778. bool force_detach = !fb || !plane_state->visible;
  3779. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3780. intel_plane->base.base.id, intel_crtc->pipe,
  3781. drm_plane_index(&intel_plane->base));
  3782. ret = skl_update_scaler(crtc_state, force_detach,
  3783. drm_plane_index(&intel_plane->base),
  3784. &plane_state->scaler_id,
  3785. plane_state->base.rotation,
  3786. drm_rect_width(&plane_state->src) >> 16,
  3787. drm_rect_height(&plane_state->src) >> 16,
  3788. drm_rect_width(&plane_state->dst),
  3789. drm_rect_height(&plane_state->dst));
  3790. if (ret || plane_state->scaler_id < 0)
  3791. return ret;
  3792. /* check colorkey */
  3793. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3794. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3795. intel_plane->base.base.id);
  3796. return -EINVAL;
  3797. }
  3798. /* Check src format */
  3799. switch (fb->pixel_format) {
  3800. case DRM_FORMAT_RGB565:
  3801. case DRM_FORMAT_XBGR8888:
  3802. case DRM_FORMAT_XRGB8888:
  3803. case DRM_FORMAT_ABGR8888:
  3804. case DRM_FORMAT_ARGB8888:
  3805. case DRM_FORMAT_XRGB2101010:
  3806. case DRM_FORMAT_XBGR2101010:
  3807. case DRM_FORMAT_YUYV:
  3808. case DRM_FORMAT_YVYU:
  3809. case DRM_FORMAT_UYVY:
  3810. case DRM_FORMAT_VYUY:
  3811. break;
  3812. default:
  3813. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3814. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3815. return -EINVAL;
  3816. }
  3817. return 0;
  3818. }
  3819. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3820. {
  3821. int i;
  3822. for (i = 0; i < crtc->num_scalers; i++)
  3823. skl_detach_scaler(crtc, i);
  3824. }
  3825. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3826. {
  3827. struct drm_device *dev = crtc->base.dev;
  3828. struct drm_i915_private *dev_priv = dev->dev_private;
  3829. int pipe = crtc->pipe;
  3830. struct intel_crtc_scaler_state *scaler_state =
  3831. &crtc->config->scaler_state;
  3832. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3833. if (crtc->config->pch_pfit.enabled) {
  3834. int id;
  3835. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3836. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3837. return;
  3838. }
  3839. id = scaler_state->scaler_id;
  3840. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3841. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3842. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3843. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3844. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3845. }
  3846. }
  3847. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3848. {
  3849. struct drm_device *dev = crtc->base.dev;
  3850. struct drm_i915_private *dev_priv = dev->dev_private;
  3851. int pipe = crtc->pipe;
  3852. if (crtc->config->pch_pfit.enabled) {
  3853. /* Force use of hard-coded filter coefficients
  3854. * as some pre-programmed values are broken,
  3855. * e.g. x201.
  3856. */
  3857. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3858. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3859. PF_PIPE_SEL_IVB(pipe));
  3860. else
  3861. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3862. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3863. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3864. }
  3865. }
  3866. void hsw_enable_ips(struct intel_crtc *crtc)
  3867. {
  3868. struct drm_device *dev = crtc->base.dev;
  3869. struct drm_i915_private *dev_priv = dev->dev_private;
  3870. if (!crtc->config->ips_enabled)
  3871. return;
  3872. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3873. intel_wait_for_vblank(dev, crtc->pipe);
  3874. assert_plane_enabled(dev_priv, crtc->plane);
  3875. if (IS_BROADWELL(dev)) {
  3876. mutex_lock(&dev_priv->rps.hw_lock);
  3877. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3878. mutex_unlock(&dev_priv->rps.hw_lock);
  3879. /* Quoting Art Runyan: "its not safe to expect any particular
  3880. * value in IPS_CTL bit 31 after enabling IPS through the
  3881. * mailbox." Moreover, the mailbox may return a bogus state,
  3882. * so we need to just enable it and continue on.
  3883. */
  3884. } else {
  3885. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3886. /* The bit only becomes 1 in the next vblank, so this wait here
  3887. * is essentially intel_wait_for_vblank. If we don't have this
  3888. * and don't wait for vblanks until the end of crtc_enable, then
  3889. * the HW state readout code will complain that the expected
  3890. * IPS_CTL value is not the one we read. */
  3891. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3892. DRM_ERROR("Timed out waiting for IPS enable\n");
  3893. }
  3894. }
  3895. void hsw_disable_ips(struct intel_crtc *crtc)
  3896. {
  3897. struct drm_device *dev = crtc->base.dev;
  3898. struct drm_i915_private *dev_priv = dev->dev_private;
  3899. if (!crtc->config->ips_enabled)
  3900. return;
  3901. assert_plane_enabled(dev_priv, crtc->plane);
  3902. if (IS_BROADWELL(dev)) {
  3903. mutex_lock(&dev_priv->rps.hw_lock);
  3904. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3905. mutex_unlock(&dev_priv->rps.hw_lock);
  3906. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3907. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3908. DRM_ERROR("Timed out waiting for IPS disable\n");
  3909. } else {
  3910. I915_WRITE(IPS_CTL, 0);
  3911. POSTING_READ(IPS_CTL);
  3912. }
  3913. /* We need to wait for a vblank before we can disable the plane. */
  3914. intel_wait_for_vblank(dev, crtc->pipe);
  3915. }
  3916. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3917. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3918. {
  3919. struct drm_device *dev = crtc->dev;
  3920. struct drm_i915_private *dev_priv = dev->dev_private;
  3921. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3922. enum pipe pipe = intel_crtc->pipe;
  3923. int palreg = PALETTE(pipe);
  3924. int i;
  3925. bool reenable_ips = false;
  3926. /* The clocks have to be on to load the palette. */
  3927. if (!crtc->state->active)
  3928. return;
  3929. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3930. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3931. assert_dsi_pll_enabled(dev_priv);
  3932. else
  3933. assert_pll_enabled(dev_priv, pipe);
  3934. }
  3935. /* use legacy palette for Ironlake */
  3936. if (!HAS_GMCH_DISPLAY(dev))
  3937. palreg = LGC_PALETTE(pipe);
  3938. /* Workaround : Do not read or write the pipe palette/gamma data while
  3939. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3940. */
  3941. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3942. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3943. GAMMA_MODE_MODE_SPLIT)) {
  3944. hsw_disable_ips(intel_crtc);
  3945. reenable_ips = true;
  3946. }
  3947. for (i = 0; i < 256; i++) {
  3948. I915_WRITE(palreg + 4 * i,
  3949. (intel_crtc->lut_r[i] << 16) |
  3950. (intel_crtc->lut_g[i] << 8) |
  3951. intel_crtc->lut_b[i]);
  3952. }
  3953. if (reenable_ips)
  3954. hsw_enable_ips(intel_crtc);
  3955. }
  3956. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3957. {
  3958. if (intel_crtc->overlay) {
  3959. struct drm_device *dev = intel_crtc->base.dev;
  3960. struct drm_i915_private *dev_priv = dev->dev_private;
  3961. mutex_lock(&dev->struct_mutex);
  3962. dev_priv->mm.interruptible = false;
  3963. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3964. dev_priv->mm.interruptible = true;
  3965. mutex_unlock(&dev->struct_mutex);
  3966. }
  3967. /* Let userspace switch the overlay on again. In most cases userspace
  3968. * has to recompute where to put it anyway.
  3969. */
  3970. }
  3971. /**
  3972. * intel_post_enable_primary - Perform operations after enabling primary plane
  3973. * @crtc: the CRTC whose primary plane was just enabled
  3974. *
  3975. * Performs potentially sleeping operations that must be done after the primary
  3976. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3977. * called due to an explicit primary plane update, or due to an implicit
  3978. * re-enable that is caused when a sprite plane is updated to no longer
  3979. * completely hide the primary plane.
  3980. */
  3981. static void
  3982. intel_post_enable_primary(struct drm_crtc *crtc)
  3983. {
  3984. struct drm_device *dev = crtc->dev;
  3985. struct drm_i915_private *dev_priv = dev->dev_private;
  3986. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3987. int pipe = intel_crtc->pipe;
  3988. /*
  3989. * BDW signals flip done immediately if the plane
  3990. * is disabled, even if the plane enable is already
  3991. * armed to occur at the next vblank :(
  3992. */
  3993. if (IS_BROADWELL(dev))
  3994. intel_wait_for_vblank(dev, pipe);
  3995. /*
  3996. * FIXME IPS should be fine as long as one plane is
  3997. * enabled, but in practice it seems to have problems
  3998. * when going from primary only to sprite only and vice
  3999. * versa.
  4000. */
  4001. hsw_enable_ips(intel_crtc);
  4002. /*
  4003. * Gen2 reports pipe underruns whenever all planes are disabled.
  4004. * So don't enable underrun reporting before at least some planes
  4005. * are enabled.
  4006. * FIXME: Need to fix the logic to work when we turn off all planes
  4007. * but leave the pipe running.
  4008. */
  4009. if (IS_GEN2(dev))
  4010. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4011. /* Underruns don't raise interrupts, so check manually. */
  4012. if (HAS_GMCH_DISPLAY(dev))
  4013. i9xx_check_fifo_underruns(dev_priv);
  4014. }
  4015. /**
  4016. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4017. * @crtc: the CRTC whose primary plane is to be disabled
  4018. *
  4019. * Performs potentially sleeping operations that must be done before the
  4020. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4021. * be called due to an explicit primary plane update, or due to an implicit
  4022. * disable that is caused when a sprite plane completely hides the primary
  4023. * plane.
  4024. */
  4025. static void
  4026. intel_pre_disable_primary(struct drm_crtc *crtc)
  4027. {
  4028. struct drm_device *dev = crtc->dev;
  4029. struct drm_i915_private *dev_priv = dev->dev_private;
  4030. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4031. int pipe = intel_crtc->pipe;
  4032. /*
  4033. * Gen2 reports pipe underruns whenever all planes are disabled.
  4034. * So diasble underrun reporting before all the planes get disabled.
  4035. * FIXME: Need to fix the logic to work when we turn off all planes
  4036. * but leave the pipe running.
  4037. */
  4038. if (IS_GEN2(dev))
  4039. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4040. /*
  4041. * Vblank time updates from the shadow to live plane control register
  4042. * are blocked if the memory self-refresh mode is active at that
  4043. * moment. So to make sure the plane gets truly disabled, disable
  4044. * first the self-refresh mode. The self-refresh enable bit in turn
  4045. * will be checked/applied by the HW only at the next frame start
  4046. * event which is after the vblank start event, so we need to have a
  4047. * wait-for-vblank between disabling the plane and the pipe.
  4048. */
  4049. if (HAS_GMCH_DISPLAY(dev)) {
  4050. intel_set_memory_cxsr(dev_priv, false);
  4051. dev_priv->wm.vlv.cxsr = false;
  4052. intel_wait_for_vblank(dev, pipe);
  4053. }
  4054. /*
  4055. * FIXME IPS should be fine as long as one plane is
  4056. * enabled, but in practice it seems to have problems
  4057. * when going from primary only to sprite only and vice
  4058. * versa.
  4059. */
  4060. hsw_disable_ips(intel_crtc);
  4061. }
  4062. static void intel_post_plane_update(struct intel_crtc *crtc)
  4063. {
  4064. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4065. struct drm_device *dev = crtc->base.dev;
  4066. struct drm_i915_private *dev_priv = dev->dev_private;
  4067. struct drm_plane *plane;
  4068. if (atomic->wait_vblank)
  4069. intel_wait_for_vblank(dev, crtc->pipe);
  4070. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4071. if (atomic->disable_cxsr)
  4072. crtc->wm.cxsr_allowed = true;
  4073. if (crtc->atomic.update_wm_post)
  4074. intel_update_watermarks(&crtc->base);
  4075. if (atomic->update_fbc)
  4076. intel_fbc_update(dev_priv);
  4077. if (atomic->post_enable_primary)
  4078. intel_post_enable_primary(&crtc->base);
  4079. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4080. intel_update_sprite_watermarks(plane, &crtc->base,
  4081. 0, 0, 0, false, false);
  4082. memset(atomic, 0, sizeof(*atomic));
  4083. }
  4084. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4085. {
  4086. struct drm_device *dev = crtc->base.dev;
  4087. struct drm_i915_private *dev_priv = dev->dev_private;
  4088. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4089. struct drm_plane *p;
  4090. /* Track fb's for any planes being disabled */
  4091. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4092. struct intel_plane *plane = to_intel_plane(p);
  4093. mutex_lock(&dev->struct_mutex);
  4094. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
  4095. plane->frontbuffer_bit);
  4096. mutex_unlock(&dev->struct_mutex);
  4097. }
  4098. if (atomic->wait_for_flips)
  4099. intel_crtc_wait_for_pending_flips(&crtc->base);
  4100. if (atomic->disable_fbc)
  4101. intel_fbc_disable_crtc(crtc);
  4102. if (crtc->atomic.disable_ips)
  4103. hsw_disable_ips(crtc);
  4104. if (atomic->pre_disable_primary)
  4105. intel_pre_disable_primary(&crtc->base);
  4106. if (atomic->disable_cxsr) {
  4107. crtc->wm.cxsr_allowed = false;
  4108. intel_set_memory_cxsr(dev_priv, false);
  4109. }
  4110. }
  4111. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4112. {
  4113. struct drm_device *dev = crtc->dev;
  4114. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4115. struct drm_plane *p;
  4116. int pipe = intel_crtc->pipe;
  4117. intel_crtc_dpms_overlay_disable(intel_crtc);
  4118. drm_for_each_plane_mask(p, dev, plane_mask)
  4119. to_intel_plane(p)->disable_plane(p, crtc);
  4120. /*
  4121. * FIXME: Once we grow proper nuclear flip support out of this we need
  4122. * to compute the mask of flip planes precisely. For the time being
  4123. * consider this a flip to a NULL plane.
  4124. */
  4125. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4126. }
  4127. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4128. {
  4129. struct drm_device *dev = crtc->dev;
  4130. struct drm_i915_private *dev_priv = dev->dev_private;
  4131. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4132. struct intel_encoder *encoder;
  4133. int pipe = intel_crtc->pipe;
  4134. if (WARN_ON(intel_crtc->active))
  4135. return;
  4136. if (intel_crtc->config->has_pch_encoder)
  4137. intel_prepare_shared_dpll(intel_crtc);
  4138. if (intel_crtc->config->has_dp_encoder)
  4139. intel_dp_set_m_n(intel_crtc, M1_N1);
  4140. intel_set_pipe_timings(intel_crtc);
  4141. if (intel_crtc->config->has_pch_encoder) {
  4142. intel_cpu_transcoder_set_m_n(intel_crtc,
  4143. &intel_crtc->config->fdi_m_n, NULL);
  4144. }
  4145. ironlake_set_pipeconf(crtc);
  4146. intel_crtc->active = true;
  4147. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4148. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4149. for_each_encoder_on_crtc(dev, crtc, encoder)
  4150. if (encoder->pre_enable)
  4151. encoder->pre_enable(encoder);
  4152. if (intel_crtc->config->has_pch_encoder) {
  4153. /* Note: FDI PLL enabling _must_ be done before we enable the
  4154. * cpu pipes, hence this is separate from all the other fdi/pch
  4155. * enabling. */
  4156. ironlake_fdi_pll_enable(intel_crtc);
  4157. } else {
  4158. assert_fdi_tx_disabled(dev_priv, pipe);
  4159. assert_fdi_rx_disabled(dev_priv, pipe);
  4160. }
  4161. ironlake_pfit_enable(intel_crtc);
  4162. /*
  4163. * On ILK+ LUT must be loaded before the pipe is running but with
  4164. * clocks enabled
  4165. */
  4166. intel_crtc_load_lut(crtc);
  4167. intel_update_watermarks(crtc);
  4168. intel_enable_pipe(intel_crtc);
  4169. if (intel_crtc->config->has_pch_encoder)
  4170. ironlake_pch_enable(crtc);
  4171. assert_vblank_disabled(crtc);
  4172. drm_crtc_vblank_on(crtc);
  4173. for_each_encoder_on_crtc(dev, crtc, encoder)
  4174. encoder->enable(encoder);
  4175. if (HAS_PCH_CPT(dev))
  4176. cpt_verify_modeset(dev, intel_crtc->pipe);
  4177. }
  4178. /* IPS only exists on ULT machines and is tied to pipe A. */
  4179. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4180. {
  4181. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4182. }
  4183. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4184. {
  4185. struct drm_device *dev = crtc->dev;
  4186. struct drm_i915_private *dev_priv = dev->dev_private;
  4187. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4188. struct intel_encoder *encoder;
  4189. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4190. struct intel_crtc_state *pipe_config =
  4191. to_intel_crtc_state(crtc->state);
  4192. if (WARN_ON(intel_crtc->active))
  4193. return;
  4194. if (intel_crtc_to_shared_dpll(intel_crtc))
  4195. intel_enable_shared_dpll(intel_crtc);
  4196. if (intel_crtc->config->has_dp_encoder)
  4197. intel_dp_set_m_n(intel_crtc, M1_N1);
  4198. intel_set_pipe_timings(intel_crtc);
  4199. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4200. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4201. intel_crtc->config->pixel_multiplier - 1);
  4202. }
  4203. if (intel_crtc->config->has_pch_encoder) {
  4204. intel_cpu_transcoder_set_m_n(intel_crtc,
  4205. &intel_crtc->config->fdi_m_n, NULL);
  4206. }
  4207. haswell_set_pipeconf(crtc);
  4208. intel_set_pipe_csc(crtc);
  4209. intel_crtc->active = true;
  4210. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4211. for_each_encoder_on_crtc(dev, crtc, encoder)
  4212. if (encoder->pre_enable)
  4213. encoder->pre_enable(encoder);
  4214. if (intel_crtc->config->has_pch_encoder) {
  4215. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4216. true);
  4217. dev_priv->display.fdi_link_train(crtc);
  4218. }
  4219. intel_ddi_enable_pipe_clock(intel_crtc);
  4220. if (INTEL_INFO(dev)->gen == 9)
  4221. skylake_pfit_enable(intel_crtc);
  4222. else if (INTEL_INFO(dev)->gen < 9)
  4223. ironlake_pfit_enable(intel_crtc);
  4224. else
  4225. MISSING_CASE(INTEL_INFO(dev)->gen);
  4226. /*
  4227. * On ILK+ LUT must be loaded before the pipe is running but with
  4228. * clocks enabled
  4229. */
  4230. intel_crtc_load_lut(crtc);
  4231. intel_ddi_set_pipe_settings(crtc);
  4232. intel_ddi_enable_transcoder_func(crtc);
  4233. intel_update_watermarks(crtc);
  4234. intel_enable_pipe(intel_crtc);
  4235. if (intel_crtc->config->has_pch_encoder)
  4236. lpt_pch_enable(crtc);
  4237. if (intel_crtc->config->dp_encoder_is_mst)
  4238. intel_ddi_set_vc_payload_alloc(crtc, true);
  4239. assert_vblank_disabled(crtc);
  4240. drm_crtc_vblank_on(crtc);
  4241. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4242. encoder->enable(encoder);
  4243. intel_opregion_notify_encoder(encoder, true);
  4244. }
  4245. /* If we change the relative order between pipe/planes enabling, we need
  4246. * to change the workaround. */
  4247. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4248. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4249. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4250. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4251. }
  4252. }
  4253. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4254. {
  4255. struct drm_device *dev = crtc->base.dev;
  4256. struct drm_i915_private *dev_priv = dev->dev_private;
  4257. int pipe = crtc->pipe;
  4258. /* To avoid upsetting the power well on haswell only disable the pfit if
  4259. * it's in use. The hw state code will make sure we get this right. */
  4260. if (crtc->config->pch_pfit.enabled) {
  4261. I915_WRITE(PF_CTL(pipe), 0);
  4262. I915_WRITE(PF_WIN_POS(pipe), 0);
  4263. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4264. }
  4265. }
  4266. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4267. {
  4268. struct drm_device *dev = crtc->dev;
  4269. struct drm_i915_private *dev_priv = dev->dev_private;
  4270. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4271. struct intel_encoder *encoder;
  4272. int pipe = intel_crtc->pipe;
  4273. u32 reg, temp;
  4274. for_each_encoder_on_crtc(dev, crtc, encoder)
  4275. encoder->disable(encoder);
  4276. drm_crtc_vblank_off(crtc);
  4277. assert_vblank_disabled(crtc);
  4278. if (intel_crtc->config->has_pch_encoder)
  4279. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4280. intel_disable_pipe(intel_crtc);
  4281. ironlake_pfit_disable(intel_crtc);
  4282. if (intel_crtc->config->has_pch_encoder)
  4283. ironlake_fdi_disable(crtc);
  4284. for_each_encoder_on_crtc(dev, crtc, encoder)
  4285. if (encoder->post_disable)
  4286. encoder->post_disable(encoder);
  4287. if (intel_crtc->config->has_pch_encoder) {
  4288. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4289. if (HAS_PCH_CPT(dev)) {
  4290. /* disable TRANS_DP_CTL */
  4291. reg = TRANS_DP_CTL(pipe);
  4292. temp = I915_READ(reg);
  4293. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4294. TRANS_DP_PORT_SEL_MASK);
  4295. temp |= TRANS_DP_PORT_SEL_NONE;
  4296. I915_WRITE(reg, temp);
  4297. /* disable DPLL_SEL */
  4298. temp = I915_READ(PCH_DPLL_SEL);
  4299. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4300. I915_WRITE(PCH_DPLL_SEL, temp);
  4301. }
  4302. ironlake_fdi_pll_disable(intel_crtc);
  4303. }
  4304. intel_crtc->active = false;
  4305. intel_update_watermarks(crtc);
  4306. }
  4307. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4308. {
  4309. struct drm_device *dev = crtc->dev;
  4310. struct drm_i915_private *dev_priv = dev->dev_private;
  4311. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4312. struct intel_encoder *encoder;
  4313. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4314. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4315. intel_opregion_notify_encoder(encoder, false);
  4316. encoder->disable(encoder);
  4317. }
  4318. drm_crtc_vblank_off(crtc);
  4319. assert_vblank_disabled(crtc);
  4320. if (intel_crtc->config->has_pch_encoder)
  4321. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4322. false);
  4323. intel_disable_pipe(intel_crtc);
  4324. if (intel_crtc->config->dp_encoder_is_mst)
  4325. intel_ddi_set_vc_payload_alloc(crtc, false);
  4326. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4327. if (INTEL_INFO(dev)->gen == 9)
  4328. skylake_scaler_disable(intel_crtc);
  4329. else if (INTEL_INFO(dev)->gen < 9)
  4330. ironlake_pfit_disable(intel_crtc);
  4331. else
  4332. MISSING_CASE(INTEL_INFO(dev)->gen);
  4333. intel_ddi_disable_pipe_clock(intel_crtc);
  4334. if (intel_crtc->config->has_pch_encoder) {
  4335. lpt_disable_pch_transcoder(dev_priv);
  4336. intel_ddi_fdi_disable(crtc);
  4337. }
  4338. for_each_encoder_on_crtc(dev, crtc, encoder)
  4339. if (encoder->post_disable)
  4340. encoder->post_disable(encoder);
  4341. intel_crtc->active = false;
  4342. intel_update_watermarks(crtc);
  4343. }
  4344. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4345. {
  4346. struct drm_device *dev = crtc->base.dev;
  4347. struct drm_i915_private *dev_priv = dev->dev_private;
  4348. struct intel_crtc_state *pipe_config = crtc->config;
  4349. if (!pipe_config->gmch_pfit.control)
  4350. return;
  4351. /*
  4352. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4353. * according to register description and PRM.
  4354. */
  4355. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4356. assert_pipe_disabled(dev_priv, crtc->pipe);
  4357. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4358. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4359. /* Border color in case we don't scale up to the full screen. Black by
  4360. * default, change to something else for debugging. */
  4361. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4362. }
  4363. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4364. {
  4365. switch (port) {
  4366. case PORT_A:
  4367. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4368. case PORT_B:
  4369. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4370. case PORT_C:
  4371. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4372. case PORT_D:
  4373. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4374. case PORT_E:
  4375. return POWER_DOMAIN_PORT_DDI_E_2_LANES;
  4376. default:
  4377. WARN_ON_ONCE(1);
  4378. return POWER_DOMAIN_PORT_OTHER;
  4379. }
  4380. }
  4381. #define for_each_power_domain(domain, mask) \
  4382. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4383. if ((1 << (domain)) & (mask))
  4384. enum intel_display_power_domain
  4385. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4386. {
  4387. struct drm_device *dev = intel_encoder->base.dev;
  4388. struct intel_digital_port *intel_dig_port;
  4389. switch (intel_encoder->type) {
  4390. case INTEL_OUTPUT_UNKNOWN:
  4391. /* Only DDI platforms should ever use this output type */
  4392. WARN_ON_ONCE(!HAS_DDI(dev));
  4393. case INTEL_OUTPUT_DISPLAYPORT:
  4394. case INTEL_OUTPUT_HDMI:
  4395. case INTEL_OUTPUT_EDP:
  4396. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4397. return port_to_power_domain(intel_dig_port->port);
  4398. case INTEL_OUTPUT_DP_MST:
  4399. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4400. return port_to_power_domain(intel_dig_port->port);
  4401. case INTEL_OUTPUT_ANALOG:
  4402. return POWER_DOMAIN_PORT_CRT;
  4403. case INTEL_OUTPUT_DSI:
  4404. return POWER_DOMAIN_PORT_DSI;
  4405. default:
  4406. return POWER_DOMAIN_PORT_OTHER;
  4407. }
  4408. }
  4409. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4410. {
  4411. struct drm_device *dev = crtc->dev;
  4412. struct intel_encoder *intel_encoder;
  4413. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4414. enum pipe pipe = intel_crtc->pipe;
  4415. unsigned long mask;
  4416. enum transcoder transcoder;
  4417. if (!crtc->state->active)
  4418. return 0;
  4419. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4420. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4421. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4422. if (intel_crtc->config->pch_pfit.enabled ||
  4423. intel_crtc->config->pch_pfit.force_thru)
  4424. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4425. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4426. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4427. return mask;
  4428. }
  4429. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4430. {
  4431. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4432. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4433. enum intel_display_power_domain domain;
  4434. unsigned long domains, new_domains, old_domains;
  4435. old_domains = intel_crtc->enabled_power_domains;
  4436. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4437. domains = new_domains & ~old_domains;
  4438. for_each_power_domain(domain, domains)
  4439. intel_display_power_get(dev_priv, domain);
  4440. return old_domains & ~new_domains;
  4441. }
  4442. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4443. unsigned long domains)
  4444. {
  4445. enum intel_display_power_domain domain;
  4446. for_each_power_domain(domain, domains)
  4447. intel_display_power_put(dev_priv, domain);
  4448. }
  4449. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4450. {
  4451. struct drm_device *dev = state->dev;
  4452. struct drm_i915_private *dev_priv = dev->dev_private;
  4453. unsigned long put_domains[I915_MAX_PIPES] = {};
  4454. struct drm_crtc_state *crtc_state;
  4455. struct drm_crtc *crtc;
  4456. int i;
  4457. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4458. if (needs_modeset(crtc->state))
  4459. put_domains[to_intel_crtc(crtc)->pipe] =
  4460. modeset_get_crtc_power_domains(crtc);
  4461. }
  4462. if (dev_priv->display.modeset_commit_cdclk) {
  4463. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4464. if (cdclk != dev_priv->cdclk_freq &&
  4465. !WARN_ON(!state->allow_modeset))
  4466. dev_priv->display.modeset_commit_cdclk(state);
  4467. }
  4468. for (i = 0; i < I915_MAX_PIPES; i++)
  4469. if (put_domains[i])
  4470. modeset_put_power_domains(dev_priv, put_domains[i]);
  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. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4509. dev_priv->max_cdclk_freq);
  4510. }
  4511. static void intel_update_cdclk(struct drm_device *dev)
  4512. {
  4513. struct drm_i915_private *dev_priv = dev->dev_private;
  4514. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4515. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4516. dev_priv->cdclk_freq);
  4517. /*
  4518. * Program the gmbus_freq based on the cdclk frequency.
  4519. * BSpec erroneously claims we should aim for 4MHz, but
  4520. * in fact 1MHz is the correct frequency.
  4521. */
  4522. if (IS_VALLEYVIEW(dev)) {
  4523. /*
  4524. * Program the gmbus_freq based on the cdclk frequency.
  4525. * BSpec erroneously claims we should aim for 4MHz, but
  4526. * in fact 1MHz is the correct frequency.
  4527. */
  4528. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4529. }
  4530. if (dev_priv->max_cdclk_freq == 0)
  4531. intel_update_max_cdclk(dev);
  4532. }
  4533. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4534. {
  4535. struct drm_i915_private *dev_priv = dev->dev_private;
  4536. uint32_t divider;
  4537. uint32_t ratio;
  4538. uint32_t current_freq;
  4539. int ret;
  4540. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4541. switch (frequency) {
  4542. case 144000:
  4543. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4544. ratio = BXT_DE_PLL_RATIO(60);
  4545. break;
  4546. case 288000:
  4547. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4548. ratio = BXT_DE_PLL_RATIO(60);
  4549. break;
  4550. case 384000:
  4551. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4552. ratio = BXT_DE_PLL_RATIO(60);
  4553. break;
  4554. case 576000:
  4555. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4556. ratio = BXT_DE_PLL_RATIO(60);
  4557. break;
  4558. case 624000:
  4559. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4560. ratio = BXT_DE_PLL_RATIO(65);
  4561. break;
  4562. case 19200:
  4563. /*
  4564. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4565. * to suppress GCC warning.
  4566. */
  4567. ratio = 0;
  4568. divider = 0;
  4569. break;
  4570. default:
  4571. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4572. return;
  4573. }
  4574. mutex_lock(&dev_priv->rps.hw_lock);
  4575. /* Inform power controller of upcoming frequency change */
  4576. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4577. 0x80000000);
  4578. mutex_unlock(&dev_priv->rps.hw_lock);
  4579. if (ret) {
  4580. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4581. ret, frequency);
  4582. return;
  4583. }
  4584. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4585. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4586. current_freq = current_freq * 500 + 1000;
  4587. /*
  4588. * DE PLL has to be disabled when
  4589. * - setting to 19.2MHz (bypass, PLL isn't used)
  4590. * - before setting to 624MHz (PLL needs toggling)
  4591. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4592. */
  4593. if (frequency == 19200 || frequency == 624000 ||
  4594. current_freq == 624000) {
  4595. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4596. /* Timeout 200us */
  4597. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4598. 1))
  4599. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4600. }
  4601. if (frequency != 19200) {
  4602. uint32_t val;
  4603. val = I915_READ(BXT_DE_PLL_CTL);
  4604. val &= ~BXT_DE_PLL_RATIO_MASK;
  4605. val |= ratio;
  4606. I915_WRITE(BXT_DE_PLL_CTL, val);
  4607. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4608. /* Timeout 200us */
  4609. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4610. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4611. val = I915_READ(CDCLK_CTL);
  4612. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4613. val |= divider;
  4614. /*
  4615. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4616. * enable otherwise.
  4617. */
  4618. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4619. if (frequency >= 500000)
  4620. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4621. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4622. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4623. val |= (frequency - 1000) / 500;
  4624. I915_WRITE(CDCLK_CTL, val);
  4625. }
  4626. mutex_lock(&dev_priv->rps.hw_lock);
  4627. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4628. DIV_ROUND_UP(frequency, 25000));
  4629. mutex_unlock(&dev_priv->rps.hw_lock);
  4630. if (ret) {
  4631. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4632. ret, frequency);
  4633. return;
  4634. }
  4635. intel_update_cdclk(dev);
  4636. }
  4637. void broxton_init_cdclk(struct drm_device *dev)
  4638. {
  4639. struct drm_i915_private *dev_priv = dev->dev_private;
  4640. uint32_t val;
  4641. /*
  4642. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4643. * or else the reset will hang because there is no PCH to respond.
  4644. * Move the handshake programming to initialization sequence.
  4645. * Previously was left up to BIOS.
  4646. */
  4647. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4648. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4649. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4650. /* Enable PG1 for cdclk */
  4651. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4652. /* check if cd clock is enabled */
  4653. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4654. DRM_DEBUG_KMS("Display already initialized\n");
  4655. return;
  4656. }
  4657. /*
  4658. * FIXME:
  4659. * - The initial CDCLK needs to be read from VBT.
  4660. * Need to make this change after VBT has changes for BXT.
  4661. * - check if setting the max (or any) cdclk freq is really necessary
  4662. * here, it belongs to modeset time
  4663. */
  4664. broxton_set_cdclk(dev, 624000);
  4665. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4666. POSTING_READ(DBUF_CTL);
  4667. udelay(10);
  4668. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4669. DRM_ERROR("DBuf power enable timeout!\n");
  4670. }
  4671. void broxton_uninit_cdclk(struct drm_device *dev)
  4672. {
  4673. struct drm_i915_private *dev_priv = dev->dev_private;
  4674. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4675. POSTING_READ(DBUF_CTL);
  4676. udelay(10);
  4677. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4678. DRM_ERROR("DBuf power disable timeout!\n");
  4679. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4680. broxton_set_cdclk(dev, 19200);
  4681. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4682. }
  4683. static const struct skl_cdclk_entry {
  4684. unsigned int freq;
  4685. unsigned int vco;
  4686. } skl_cdclk_frequencies[] = {
  4687. { .freq = 308570, .vco = 8640 },
  4688. { .freq = 337500, .vco = 8100 },
  4689. { .freq = 432000, .vco = 8640 },
  4690. { .freq = 450000, .vco = 8100 },
  4691. { .freq = 540000, .vco = 8100 },
  4692. { .freq = 617140, .vco = 8640 },
  4693. { .freq = 675000, .vco = 8100 },
  4694. };
  4695. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4696. {
  4697. return (freq - 1000) / 500;
  4698. }
  4699. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4700. {
  4701. unsigned int i;
  4702. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4703. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4704. if (e->freq == freq)
  4705. return e->vco;
  4706. }
  4707. return 8100;
  4708. }
  4709. static void
  4710. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4711. {
  4712. unsigned int min_freq;
  4713. u32 val;
  4714. /* select the minimum CDCLK before enabling DPLL 0 */
  4715. val = I915_READ(CDCLK_CTL);
  4716. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4717. val |= CDCLK_FREQ_337_308;
  4718. if (required_vco == 8640)
  4719. min_freq = 308570;
  4720. else
  4721. min_freq = 337500;
  4722. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4723. I915_WRITE(CDCLK_CTL, val);
  4724. POSTING_READ(CDCLK_CTL);
  4725. /*
  4726. * We always enable DPLL0 with the lowest link rate possible, but still
  4727. * taking into account the VCO required to operate the eDP panel at the
  4728. * desired frequency. The usual DP link rates operate with a VCO of
  4729. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4730. * The modeset code is responsible for the selection of the exact link
  4731. * rate later on, with the constraint of choosing a frequency that
  4732. * works with required_vco.
  4733. */
  4734. val = I915_READ(DPLL_CTRL1);
  4735. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4736. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4737. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4738. if (required_vco == 8640)
  4739. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4740. SKL_DPLL0);
  4741. else
  4742. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4743. SKL_DPLL0);
  4744. I915_WRITE(DPLL_CTRL1, val);
  4745. POSTING_READ(DPLL_CTRL1);
  4746. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4747. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4748. DRM_ERROR("DPLL0 not locked\n");
  4749. }
  4750. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4751. {
  4752. int ret;
  4753. u32 val;
  4754. /* inform PCU we want to change CDCLK */
  4755. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4756. mutex_lock(&dev_priv->rps.hw_lock);
  4757. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4758. mutex_unlock(&dev_priv->rps.hw_lock);
  4759. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4760. }
  4761. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4762. {
  4763. unsigned int i;
  4764. for (i = 0; i < 15; i++) {
  4765. if (skl_cdclk_pcu_ready(dev_priv))
  4766. return true;
  4767. udelay(10);
  4768. }
  4769. return false;
  4770. }
  4771. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4772. {
  4773. struct drm_device *dev = dev_priv->dev;
  4774. u32 freq_select, pcu_ack;
  4775. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4776. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4777. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4778. return;
  4779. }
  4780. /* set CDCLK_CTL */
  4781. switch(freq) {
  4782. case 450000:
  4783. case 432000:
  4784. freq_select = CDCLK_FREQ_450_432;
  4785. pcu_ack = 1;
  4786. break;
  4787. case 540000:
  4788. freq_select = CDCLK_FREQ_540;
  4789. pcu_ack = 2;
  4790. break;
  4791. case 308570:
  4792. case 337500:
  4793. default:
  4794. freq_select = CDCLK_FREQ_337_308;
  4795. pcu_ack = 0;
  4796. break;
  4797. case 617140:
  4798. case 675000:
  4799. freq_select = CDCLK_FREQ_675_617;
  4800. pcu_ack = 3;
  4801. break;
  4802. }
  4803. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4804. POSTING_READ(CDCLK_CTL);
  4805. /* inform PCU of the change */
  4806. mutex_lock(&dev_priv->rps.hw_lock);
  4807. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4808. mutex_unlock(&dev_priv->rps.hw_lock);
  4809. intel_update_cdclk(dev);
  4810. }
  4811. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4812. {
  4813. /* disable DBUF power */
  4814. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4815. POSTING_READ(DBUF_CTL);
  4816. udelay(10);
  4817. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4818. DRM_ERROR("DBuf power disable timeout\n");
  4819. /* disable DPLL0 */
  4820. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4821. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4822. DRM_ERROR("Couldn't disable DPLL0\n");
  4823. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4824. }
  4825. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4826. {
  4827. u32 val;
  4828. unsigned int required_vco;
  4829. /* enable PCH reset handshake */
  4830. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4831. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4832. /* enable PG1 and Misc I/O */
  4833. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4834. /* DPLL0 not enabled (happens on early BIOS versions) */
  4835. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4836. /* enable DPLL0 */
  4837. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4838. skl_dpll0_enable(dev_priv, required_vco);
  4839. }
  4840. /* set CDCLK to the frequency the BIOS chose */
  4841. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4842. /* enable DBUF power */
  4843. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4844. POSTING_READ(DBUF_CTL);
  4845. udelay(10);
  4846. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4847. DRM_ERROR("DBuf power enable timeout\n");
  4848. }
  4849. /* returns HPLL frequency in kHz */
  4850. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4851. {
  4852. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4853. /* Obtain SKU information */
  4854. mutex_lock(&dev_priv->sb_lock);
  4855. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4856. CCK_FUSE_HPLL_FREQ_MASK;
  4857. mutex_unlock(&dev_priv->sb_lock);
  4858. return vco_freq[hpll_freq] * 1000;
  4859. }
  4860. /* Adjust CDclk dividers to allow high res or save power if possible */
  4861. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4862. {
  4863. struct drm_i915_private *dev_priv = dev->dev_private;
  4864. u32 val, cmd;
  4865. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4866. != dev_priv->cdclk_freq);
  4867. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4868. cmd = 2;
  4869. else if (cdclk == 266667)
  4870. cmd = 1;
  4871. else
  4872. cmd = 0;
  4873. mutex_lock(&dev_priv->rps.hw_lock);
  4874. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4875. val &= ~DSPFREQGUAR_MASK;
  4876. val |= (cmd << DSPFREQGUAR_SHIFT);
  4877. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4878. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4879. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4880. 50)) {
  4881. DRM_ERROR("timed out waiting for CDclk change\n");
  4882. }
  4883. mutex_unlock(&dev_priv->rps.hw_lock);
  4884. mutex_lock(&dev_priv->sb_lock);
  4885. if (cdclk == 400000) {
  4886. u32 divider;
  4887. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4888. /* adjust cdclk divider */
  4889. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4890. val &= ~DISPLAY_FREQUENCY_VALUES;
  4891. val |= divider;
  4892. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4893. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4894. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4895. 50))
  4896. DRM_ERROR("timed out waiting for CDclk change\n");
  4897. }
  4898. /* adjust self-refresh exit latency value */
  4899. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4900. val &= ~0x7f;
  4901. /*
  4902. * For high bandwidth configs, we set a higher latency in the bunit
  4903. * so that the core display fetch happens in time to avoid underruns.
  4904. */
  4905. if (cdclk == 400000)
  4906. val |= 4500 / 250; /* 4.5 usec */
  4907. else
  4908. val |= 3000 / 250; /* 3.0 usec */
  4909. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4910. mutex_unlock(&dev_priv->sb_lock);
  4911. intel_update_cdclk(dev);
  4912. }
  4913. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4914. {
  4915. struct drm_i915_private *dev_priv = dev->dev_private;
  4916. u32 val, cmd;
  4917. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4918. != dev_priv->cdclk_freq);
  4919. switch (cdclk) {
  4920. case 333333:
  4921. case 320000:
  4922. case 266667:
  4923. case 200000:
  4924. break;
  4925. default:
  4926. MISSING_CASE(cdclk);
  4927. return;
  4928. }
  4929. /*
  4930. * Specs are full of misinformation, but testing on actual
  4931. * hardware has shown that we just need to write the desired
  4932. * CCK divider into the Punit register.
  4933. */
  4934. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4935. mutex_lock(&dev_priv->rps.hw_lock);
  4936. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4937. val &= ~DSPFREQGUAR_MASK_CHV;
  4938. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4939. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4940. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4941. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4942. 50)) {
  4943. DRM_ERROR("timed out waiting for CDclk change\n");
  4944. }
  4945. mutex_unlock(&dev_priv->rps.hw_lock);
  4946. intel_update_cdclk(dev);
  4947. }
  4948. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4949. int max_pixclk)
  4950. {
  4951. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4952. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4953. /*
  4954. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4955. * 200MHz
  4956. * 267MHz
  4957. * 320/333MHz (depends on HPLL freq)
  4958. * 400MHz (VLV only)
  4959. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4960. * of the lower bin and adjust if needed.
  4961. *
  4962. * We seem to get an unstable or solid color picture at 200MHz.
  4963. * Not sure what's wrong. For now use 200MHz only when all pipes
  4964. * are off.
  4965. */
  4966. if (!IS_CHERRYVIEW(dev_priv) &&
  4967. max_pixclk > freq_320*limit/100)
  4968. return 400000;
  4969. else if (max_pixclk > 266667*limit/100)
  4970. return freq_320;
  4971. else if (max_pixclk > 0)
  4972. return 266667;
  4973. else
  4974. return 200000;
  4975. }
  4976. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4977. int max_pixclk)
  4978. {
  4979. /*
  4980. * FIXME:
  4981. * - remove the guardband, it's not needed on BXT
  4982. * - set 19.2MHz bypass frequency if there are no active pipes
  4983. */
  4984. if (max_pixclk > 576000*9/10)
  4985. return 624000;
  4986. else if (max_pixclk > 384000*9/10)
  4987. return 576000;
  4988. else if (max_pixclk > 288000*9/10)
  4989. return 384000;
  4990. else if (max_pixclk > 144000*9/10)
  4991. return 288000;
  4992. else
  4993. return 144000;
  4994. }
  4995. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4996. * that's non-NULL, look at current state otherwise. */
  4997. static int intel_mode_max_pixclk(struct drm_device *dev,
  4998. struct drm_atomic_state *state)
  4999. {
  5000. struct intel_crtc *intel_crtc;
  5001. struct intel_crtc_state *crtc_state;
  5002. int max_pixclk = 0;
  5003. for_each_intel_crtc(dev, intel_crtc) {
  5004. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5005. if (IS_ERR(crtc_state))
  5006. return PTR_ERR(crtc_state);
  5007. if (!crtc_state->base.enable)
  5008. continue;
  5009. max_pixclk = max(max_pixclk,
  5010. crtc_state->base.adjusted_mode.crtc_clock);
  5011. }
  5012. return max_pixclk;
  5013. }
  5014. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5015. {
  5016. struct drm_device *dev = state->dev;
  5017. struct drm_i915_private *dev_priv = dev->dev_private;
  5018. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5019. if (max_pixclk < 0)
  5020. return max_pixclk;
  5021. to_intel_atomic_state(state)->cdclk =
  5022. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5023. return 0;
  5024. }
  5025. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5026. {
  5027. struct drm_device *dev = state->dev;
  5028. struct drm_i915_private *dev_priv = dev->dev_private;
  5029. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5030. if (max_pixclk < 0)
  5031. return max_pixclk;
  5032. to_intel_atomic_state(state)->cdclk =
  5033. broxton_calc_cdclk(dev_priv, max_pixclk);
  5034. return 0;
  5035. }
  5036. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5037. {
  5038. unsigned int credits, default_credits;
  5039. if (IS_CHERRYVIEW(dev_priv))
  5040. default_credits = PFI_CREDIT(12);
  5041. else
  5042. default_credits = PFI_CREDIT(8);
  5043. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5044. /* CHV suggested value is 31 or 63 */
  5045. if (IS_CHERRYVIEW(dev_priv))
  5046. credits = PFI_CREDIT_63;
  5047. else
  5048. credits = PFI_CREDIT(15);
  5049. } else {
  5050. credits = default_credits;
  5051. }
  5052. /*
  5053. * WA - write default credits before re-programming
  5054. * FIXME: should we also set the resend bit here?
  5055. */
  5056. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5057. default_credits);
  5058. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5059. credits | PFI_CREDIT_RESEND);
  5060. /*
  5061. * FIXME is this guaranteed to clear
  5062. * immediately or should we poll for it?
  5063. */
  5064. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5065. }
  5066. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5067. {
  5068. struct drm_device *dev = old_state->dev;
  5069. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5070. struct drm_i915_private *dev_priv = dev->dev_private;
  5071. /*
  5072. * FIXME: We can end up here with all power domains off, yet
  5073. * with a CDCLK frequency other than the minimum. To account
  5074. * for this take the PIPE-A power domain, which covers the HW
  5075. * blocks needed for the following programming. This can be
  5076. * removed once it's guaranteed that we get here either with
  5077. * the minimum CDCLK set, or the required power domains
  5078. * enabled.
  5079. */
  5080. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5081. if (IS_CHERRYVIEW(dev))
  5082. cherryview_set_cdclk(dev, req_cdclk);
  5083. else
  5084. valleyview_set_cdclk(dev, req_cdclk);
  5085. vlv_program_pfi_credits(dev_priv);
  5086. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5087. }
  5088. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5089. {
  5090. struct drm_device *dev = crtc->dev;
  5091. struct drm_i915_private *dev_priv = to_i915(dev);
  5092. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5093. struct intel_encoder *encoder;
  5094. int pipe = intel_crtc->pipe;
  5095. bool is_dsi;
  5096. if (WARN_ON(intel_crtc->active))
  5097. return;
  5098. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5099. if (!is_dsi) {
  5100. if (IS_CHERRYVIEW(dev))
  5101. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5102. else
  5103. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5104. }
  5105. if (intel_crtc->config->has_dp_encoder)
  5106. intel_dp_set_m_n(intel_crtc, M1_N1);
  5107. intel_set_pipe_timings(intel_crtc);
  5108. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5109. struct drm_i915_private *dev_priv = dev->dev_private;
  5110. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5111. I915_WRITE(CHV_CANVAS(pipe), 0);
  5112. }
  5113. i9xx_set_pipeconf(intel_crtc);
  5114. intel_crtc->active = true;
  5115. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5116. for_each_encoder_on_crtc(dev, crtc, encoder)
  5117. if (encoder->pre_pll_enable)
  5118. encoder->pre_pll_enable(encoder);
  5119. if (!is_dsi) {
  5120. if (IS_CHERRYVIEW(dev))
  5121. chv_enable_pll(intel_crtc, intel_crtc->config);
  5122. else
  5123. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5124. }
  5125. for_each_encoder_on_crtc(dev, crtc, encoder)
  5126. if (encoder->pre_enable)
  5127. encoder->pre_enable(encoder);
  5128. i9xx_pfit_enable(intel_crtc);
  5129. intel_crtc_load_lut(crtc);
  5130. intel_enable_pipe(intel_crtc);
  5131. assert_vblank_disabled(crtc);
  5132. drm_crtc_vblank_on(crtc);
  5133. for_each_encoder_on_crtc(dev, crtc, encoder)
  5134. encoder->enable(encoder);
  5135. }
  5136. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5137. {
  5138. struct drm_device *dev = crtc->base.dev;
  5139. struct drm_i915_private *dev_priv = dev->dev_private;
  5140. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5141. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5142. }
  5143. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5144. {
  5145. struct drm_device *dev = crtc->dev;
  5146. struct drm_i915_private *dev_priv = to_i915(dev);
  5147. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5148. struct intel_encoder *encoder;
  5149. int pipe = intel_crtc->pipe;
  5150. if (WARN_ON(intel_crtc->active))
  5151. return;
  5152. i9xx_set_pll_dividers(intel_crtc);
  5153. if (intel_crtc->config->has_dp_encoder)
  5154. intel_dp_set_m_n(intel_crtc, M1_N1);
  5155. intel_set_pipe_timings(intel_crtc);
  5156. i9xx_set_pipeconf(intel_crtc);
  5157. intel_crtc->active = true;
  5158. if (!IS_GEN2(dev))
  5159. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5160. for_each_encoder_on_crtc(dev, crtc, encoder)
  5161. if (encoder->pre_enable)
  5162. encoder->pre_enable(encoder);
  5163. i9xx_enable_pll(intel_crtc);
  5164. i9xx_pfit_enable(intel_crtc);
  5165. intel_crtc_load_lut(crtc);
  5166. intel_update_watermarks(crtc);
  5167. intel_enable_pipe(intel_crtc);
  5168. assert_vblank_disabled(crtc);
  5169. drm_crtc_vblank_on(crtc);
  5170. for_each_encoder_on_crtc(dev, crtc, encoder)
  5171. encoder->enable(encoder);
  5172. }
  5173. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5174. {
  5175. struct drm_device *dev = crtc->base.dev;
  5176. struct drm_i915_private *dev_priv = dev->dev_private;
  5177. if (!crtc->config->gmch_pfit.control)
  5178. return;
  5179. assert_pipe_disabled(dev_priv, crtc->pipe);
  5180. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5181. I915_READ(PFIT_CONTROL));
  5182. I915_WRITE(PFIT_CONTROL, 0);
  5183. }
  5184. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5185. {
  5186. struct drm_device *dev = crtc->dev;
  5187. struct drm_i915_private *dev_priv = dev->dev_private;
  5188. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5189. struct intel_encoder *encoder;
  5190. int pipe = intel_crtc->pipe;
  5191. /*
  5192. * On gen2 planes are double buffered but the pipe isn't, so we must
  5193. * wait for planes to fully turn off before disabling the pipe.
  5194. * We also need to wait on all gmch platforms because of the
  5195. * self-refresh mode constraint explained above.
  5196. */
  5197. intel_wait_for_vblank(dev, pipe);
  5198. for_each_encoder_on_crtc(dev, crtc, encoder)
  5199. encoder->disable(encoder);
  5200. drm_crtc_vblank_off(crtc);
  5201. assert_vblank_disabled(crtc);
  5202. intel_disable_pipe(intel_crtc);
  5203. i9xx_pfit_disable(intel_crtc);
  5204. for_each_encoder_on_crtc(dev, crtc, encoder)
  5205. if (encoder->post_disable)
  5206. encoder->post_disable(encoder);
  5207. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5208. if (IS_CHERRYVIEW(dev))
  5209. chv_disable_pll(dev_priv, pipe);
  5210. else if (IS_VALLEYVIEW(dev))
  5211. vlv_disable_pll(dev_priv, pipe);
  5212. else
  5213. i9xx_disable_pll(intel_crtc);
  5214. }
  5215. if (!IS_GEN2(dev))
  5216. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5217. intel_crtc->active = false;
  5218. intel_update_watermarks(crtc);
  5219. }
  5220. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5221. {
  5222. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5223. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5224. enum intel_display_power_domain domain;
  5225. unsigned long domains;
  5226. if (!intel_crtc->active)
  5227. return;
  5228. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5229. intel_crtc_wait_for_pending_flips(crtc);
  5230. intel_pre_disable_primary(crtc);
  5231. }
  5232. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5233. dev_priv->display.crtc_disable(crtc);
  5234. intel_disable_shared_dpll(intel_crtc);
  5235. domains = intel_crtc->enabled_power_domains;
  5236. for_each_power_domain(domain, domains)
  5237. intel_display_power_put(dev_priv, domain);
  5238. intel_crtc->enabled_power_domains = 0;
  5239. }
  5240. /*
  5241. * turn all crtc's off, but do not adjust state
  5242. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5243. */
  5244. int intel_display_suspend(struct drm_device *dev)
  5245. {
  5246. struct drm_mode_config *config = &dev->mode_config;
  5247. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5248. struct drm_atomic_state *state;
  5249. struct drm_crtc *crtc;
  5250. unsigned crtc_mask = 0;
  5251. int ret = 0;
  5252. if (WARN_ON(!ctx))
  5253. return 0;
  5254. lockdep_assert_held(&ctx->ww_ctx);
  5255. state = drm_atomic_state_alloc(dev);
  5256. if (WARN_ON(!state))
  5257. return -ENOMEM;
  5258. state->acquire_ctx = ctx;
  5259. state->allow_modeset = true;
  5260. for_each_crtc(dev, crtc) {
  5261. struct drm_crtc_state *crtc_state =
  5262. drm_atomic_get_crtc_state(state, crtc);
  5263. ret = PTR_ERR_OR_ZERO(crtc_state);
  5264. if (ret)
  5265. goto free;
  5266. if (!crtc_state->active)
  5267. continue;
  5268. crtc_state->active = false;
  5269. crtc_mask |= 1 << drm_crtc_index(crtc);
  5270. }
  5271. if (crtc_mask) {
  5272. ret = drm_atomic_commit(state);
  5273. if (!ret) {
  5274. for_each_crtc(dev, crtc)
  5275. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5276. crtc->state->active = true;
  5277. return ret;
  5278. }
  5279. }
  5280. free:
  5281. if (ret)
  5282. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5283. drm_atomic_state_free(state);
  5284. return ret;
  5285. }
  5286. void intel_encoder_destroy(struct drm_encoder *encoder)
  5287. {
  5288. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5289. drm_encoder_cleanup(encoder);
  5290. kfree(intel_encoder);
  5291. }
  5292. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5293. * internal consistency). */
  5294. static void intel_connector_check_state(struct intel_connector *connector)
  5295. {
  5296. struct drm_crtc *crtc = connector->base.state->crtc;
  5297. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5298. connector->base.base.id,
  5299. connector->base.name);
  5300. if (connector->get_hw_state(connector)) {
  5301. struct intel_encoder *encoder = connector->encoder;
  5302. struct drm_connector_state *conn_state = connector->base.state;
  5303. I915_STATE_WARN(!crtc,
  5304. "connector enabled without attached crtc\n");
  5305. if (!crtc)
  5306. return;
  5307. I915_STATE_WARN(!crtc->state->active,
  5308. "connector is active, but attached crtc isn't\n");
  5309. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5310. return;
  5311. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5312. "atomic encoder doesn't match attached encoder\n");
  5313. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5314. "attached encoder crtc differs from connector crtc\n");
  5315. } else {
  5316. I915_STATE_WARN(crtc && crtc->state->active,
  5317. "attached crtc is active, but connector isn't\n");
  5318. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5319. "best encoder set without crtc!\n");
  5320. }
  5321. }
  5322. int intel_connector_init(struct intel_connector *connector)
  5323. {
  5324. struct drm_connector_state *connector_state;
  5325. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5326. if (!connector_state)
  5327. return -ENOMEM;
  5328. connector->base.state = connector_state;
  5329. return 0;
  5330. }
  5331. struct intel_connector *intel_connector_alloc(void)
  5332. {
  5333. struct intel_connector *connector;
  5334. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5335. if (!connector)
  5336. return NULL;
  5337. if (intel_connector_init(connector) < 0) {
  5338. kfree(connector);
  5339. return NULL;
  5340. }
  5341. return connector;
  5342. }
  5343. /* Simple connector->get_hw_state implementation for encoders that support only
  5344. * one connector and no cloning and hence the encoder state determines the state
  5345. * of the connector. */
  5346. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5347. {
  5348. enum pipe pipe = 0;
  5349. struct intel_encoder *encoder = connector->encoder;
  5350. return encoder->get_hw_state(encoder, &pipe);
  5351. }
  5352. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5353. {
  5354. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5355. return crtc_state->fdi_lanes;
  5356. return 0;
  5357. }
  5358. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5359. struct intel_crtc_state *pipe_config)
  5360. {
  5361. struct drm_atomic_state *state = pipe_config->base.state;
  5362. struct intel_crtc *other_crtc;
  5363. struct intel_crtc_state *other_crtc_state;
  5364. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5365. pipe_name(pipe), pipe_config->fdi_lanes);
  5366. if (pipe_config->fdi_lanes > 4) {
  5367. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5368. pipe_name(pipe), pipe_config->fdi_lanes);
  5369. return -EINVAL;
  5370. }
  5371. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5372. if (pipe_config->fdi_lanes > 2) {
  5373. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5374. pipe_config->fdi_lanes);
  5375. return -EINVAL;
  5376. } else {
  5377. return 0;
  5378. }
  5379. }
  5380. if (INTEL_INFO(dev)->num_pipes == 2)
  5381. return 0;
  5382. /* Ivybridge 3 pipe is really complicated */
  5383. switch (pipe) {
  5384. case PIPE_A:
  5385. return 0;
  5386. case PIPE_B:
  5387. if (pipe_config->fdi_lanes <= 2)
  5388. return 0;
  5389. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5390. other_crtc_state =
  5391. intel_atomic_get_crtc_state(state, other_crtc);
  5392. if (IS_ERR(other_crtc_state))
  5393. return PTR_ERR(other_crtc_state);
  5394. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5395. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5396. pipe_name(pipe), pipe_config->fdi_lanes);
  5397. return -EINVAL;
  5398. }
  5399. return 0;
  5400. case PIPE_C:
  5401. if (pipe_config->fdi_lanes > 2) {
  5402. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5403. pipe_name(pipe), pipe_config->fdi_lanes);
  5404. return -EINVAL;
  5405. }
  5406. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5407. other_crtc_state =
  5408. intel_atomic_get_crtc_state(state, other_crtc);
  5409. if (IS_ERR(other_crtc_state))
  5410. return PTR_ERR(other_crtc_state);
  5411. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5412. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5413. return -EINVAL;
  5414. }
  5415. return 0;
  5416. default:
  5417. BUG();
  5418. }
  5419. }
  5420. #define RETRY 1
  5421. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5422. struct intel_crtc_state *pipe_config)
  5423. {
  5424. struct drm_device *dev = intel_crtc->base.dev;
  5425. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5426. int lane, link_bw, fdi_dotclock, ret;
  5427. bool needs_recompute = false;
  5428. retry:
  5429. /* FDI is a binary signal running at ~2.7GHz, encoding
  5430. * each output octet as 10 bits. The actual frequency
  5431. * is stored as a divider into a 100MHz clock, and the
  5432. * mode pixel clock is stored in units of 1KHz.
  5433. * Hence the bw of each lane in terms of the mode signal
  5434. * is:
  5435. */
  5436. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5437. fdi_dotclock = adjusted_mode->crtc_clock;
  5438. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5439. pipe_config->pipe_bpp);
  5440. pipe_config->fdi_lanes = lane;
  5441. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5442. link_bw, &pipe_config->fdi_m_n);
  5443. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5444. intel_crtc->pipe, pipe_config);
  5445. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5446. pipe_config->pipe_bpp -= 2*3;
  5447. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5448. pipe_config->pipe_bpp);
  5449. needs_recompute = true;
  5450. pipe_config->bw_constrained = true;
  5451. goto retry;
  5452. }
  5453. if (needs_recompute)
  5454. return RETRY;
  5455. return ret;
  5456. }
  5457. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5458. struct intel_crtc_state *pipe_config)
  5459. {
  5460. if (pipe_config->pipe_bpp > 24)
  5461. return false;
  5462. /* HSW can handle pixel rate up to cdclk? */
  5463. if (IS_HASWELL(dev_priv->dev))
  5464. return true;
  5465. /*
  5466. * We compare against max which means we must take
  5467. * the increased cdclk requirement into account when
  5468. * calculating the new cdclk.
  5469. *
  5470. * Should measure whether using a lower cdclk w/o IPS
  5471. */
  5472. return ilk_pipe_pixel_rate(pipe_config) <=
  5473. dev_priv->max_cdclk_freq * 95 / 100;
  5474. }
  5475. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5476. struct intel_crtc_state *pipe_config)
  5477. {
  5478. struct drm_device *dev = crtc->base.dev;
  5479. struct drm_i915_private *dev_priv = dev->dev_private;
  5480. pipe_config->ips_enabled = i915.enable_ips &&
  5481. hsw_crtc_supports_ips(crtc) &&
  5482. pipe_config_supports_ips(dev_priv, pipe_config);
  5483. }
  5484. static int intel_crtc_compute_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. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5490. /* FIXME should check pixel clock limits on all platforms */
  5491. if (INTEL_INFO(dev)->gen < 4) {
  5492. int clock_limit = dev_priv->max_cdclk_freq;
  5493. /*
  5494. * Enable pixel doubling when the dot clock
  5495. * is > 90% of the (display) core speed.
  5496. *
  5497. * GDG double wide on either pipe,
  5498. * otherwise pipe A only.
  5499. */
  5500. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5501. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5502. clock_limit *= 2;
  5503. pipe_config->double_wide = true;
  5504. }
  5505. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5506. return -EINVAL;
  5507. }
  5508. /*
  5509. * Pipe horizontal size must be even in:
  5510. * - DVO ganged mode
  5511. * - LVDS dual channel mode
  5512. * - Double wide pipe
  5513. */
  5514. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5515. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5516. pipe_config->pipe_src_w &= ~1;
  5517. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5518. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5519. */
  5520. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5521. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5522. return -EINVAL;
  5523. if (HAS_IPS(dev))
  5524. hsw_compute_ips_config(crtc, pipe_config);
  5525. if (pipe_config->has_pch_encoder)
  5526. return ironlake_fdi_compute_config(crtc, pipe_config);
  5527. return 0;
  5528. }
  5529. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5530. {
  5531. struct drm_i915_private *dev_priv = to_i915(dev);
  5532. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5533. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5534. uint32_t linkrate;
  5535. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5536. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5537. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5538. return 540000;
  5539. linkrate = (I915_READ(DPLL_CTRL1) &
  5540. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5541. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5542. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5543. /* vco 8640 */
  5544. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5545. case CDCLK_FREQ_450_432:
  5546. return 432000;
  5547. case CDCLK_FREQ_337_308:
  5548. return 308570;
  5549. case CDCLK_FREQ_675_617:
  5550. return 617140;
  5551. default:
  5552. WARN(1, "Unknown cd freq selection\n");
  5553. }
  5554. } else {
  5555. /* vco 8100 */
  5556. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5557. case CDCLK_FREQ_450_432:
  5558. return 450000;
  5559. case CDCLK_FREQ_337_308:
  5560. return 337500;
  5561. case CDCLK_FREQ_675_617:
  5562. return 675000;
  5563. default:
  5564. WARN(1, "Unknown cd freq selection\n");
  5565. }
  5566. }
  5567. /* error case, do as if DPLL0 isn't enabled */
  5568. return 24000;
  5569. }
  5570. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5571. {
  5572. struct drm_i915_private *dev_priv = to_i915(dev);
  5573. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5574. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5575. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5576. int cdclk;
  5577. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5578. return 19200;
  5579. cdclk = 19200 * pll_ratio / 2;
  5580. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5581. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5582. return cdclk; /* 576MHz or 624MHz */
  5583. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5584. return cdclk * 2 / 3; /* 384MHz */
  5585. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5586. return cdclk / 2; /* 288MHz */
  5587. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5588. return cdclk / 4; /* 144MHz */
  5589. }
  5590. /* error case, do as if DE PLL isn't enabled */
  5591. return 19200;
  5592. }
  5593. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5594. {
  5595. struct drm_i915_private *dev_priv = dev->dev_private;
  5596. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5597. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5598. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5599. return 800000;
  5600. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5601. return 450000;
  5602. else if (freq == LCPLL_CLK_FREQ_450)
  5603. return 450000;
  5604. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5605. return 540000;
  5606. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5607. return 337500;
  5608. else
  5609. return 675000;
  5610. }
  5611. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5612. {
  5613. struct drm_i915_private *dev_priv = dev->dev_private;
  5614. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5615. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5616. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5617. return 800000;
  5618. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5619. return 450000;
  5620. else if (freq == LCPLL_CLK_FREQ_450)
  5621. return 450000;
  5622. else if (IS_HSW_ULT(dev))
  5623. return 337500;
  5624. else
  5625. return 540000;
  5626. }
  5627. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5628. {
  5629. struct drm_i915_private *dev_priv = dev->dev_private;
  5630. u32 val;
  5631. int divider;
  5632. if (dev_priv->hpll_freq == 0)
  5633. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5634. mutex_lock(&dev_priv->sb_lock);
  5635. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5636. mutex_unlock(&dev_priv->sb_lock);
  5637. divider = val & DISPLAY_FREQUENCY_VALUES;
  5638. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5639. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5640. "cdclk change in progress\n");
  5641. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5642. }
  5643. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5644. {
  5645. return 450000;
  5646. }
  5647. static int i945_get_display_clock_speed(struct drm_device *dev)
  5648. {
  5649. return 400000;
  5650. }
  5651. static int i915_get_display_clock_speed(struct drm_device *dev)
  5652. {
  5653. return 333333;
  5654. }
  5655. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5656. {
  5657. return 200000;
  5658. }
  5659. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5660. {
  5661. u16 gcfgc = 0;
  5662. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5663. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5664. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5665. return 266667;
  5666. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5667. return 333333;
  5668. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5669. return 444444;
  5670. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5671. return 200000;
  5672. default:
  5673. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5674. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5675. return 133333;
  5676. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5677. return 166667;
  5678. }
  5679. }
  5680. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5681. {
  5682. u16 gcfgc = 0;
  5683. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5684. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5685. return 133333;
  5686. else {
  5687. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5688. case GC_DISPLAY_CLOCK_333_MHZ:
  5689. return 333333;
  5690. default:
  5691. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5692. return 190000;
  5693. }
  5694. }
  5695. }
  5696. static int i865_get_display_clock_speed(struct drm_device *dev)
  5697. {
  5698. return 266667;
  5699. }
  5700. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5701. {
  5702. u16 hpllcc = 0;
  5703. /*
  5704. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5705. * encoding is different :(
  5706. * FIXME is this the right way to detect 852GM/852GMV?
  5707. */
  5708. if (dev->pdev->revision == 0x1)
  5709. return 133333;
  5710. pci_bus_read_config_word(dev->pdev->bus,
  5711. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5712. /* Assume that the hardware is in the high speed state. This
  5713. * should be the default.
  5714. */
  5715. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5716. case GC_CLOCK_133_200:
  5717. case GC_CLOCK_133_200_2:
  5718. case GC_CLOCK_100_200:
  5719. return 200000;
  5720. case GC_CLOCK_166_250:
  5721. return 250000;
  5722. case GC_CLOCK_100_133:
  5723. return 133333;
  5724. case GC_CLOCK_133_266:
  5725. case GC_CLOCK_133_266_2:
  5726. case GC_CLOCK_166_266:
  5727. return 266667;
  5728. }
  5729. /* Shouldn't happen */
  5730. return 0;
  5731. }
  5732. static int i830_get_display_clock_speed(struct drm_device *dev)
  5733. {
  5734. return 133333;
  5735. }
  5736. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5737. {
  5738. struct drm_i915_private *dev_priv = dev->dev_private;
  5739. static const unsigned int blb_vco[8] = {
  5740. [0] = 3200000,
  5741. [1] = 4000000,
  5742. [2] = 5333333,
  5743. [3] = 4800000,
  5744. [4] = 6400000,
  5745. };
  5746. static const unsigned int pnv_vco[8] = {
  5747. [0] = 3200000,
  5748. [1] = 4000000,
  5749. [2] = 5333333,
  5750. [3] = 4800000,
  5751. [4] = 2666667,
  5752. };
  5753. static const unsigned int cl_vco[8] = {
  5754. [0] = 3200000,
  5755. [1] = 4000000,
  5756. [2] = 5333333,
  5757. [3] = 6400000,
  5758. [4] = 3333333,
  5759. [5] = 3566667,
  5760. [6] = 4266667,
  5761. };
  5762. static const unsigned int elk_vco[8] = {
  5763. [0] = 3200000,
  5764. [1] = 4000000,
  5765. [2] = 5333333,
  5766. [3] = 4800000,
  5767. };
  5768. static const unsigned int ctg_vco[8] = {
  5769. [0] = 3200000,
  5770. [1] = 4000000,
  5771. [2] = 5333333,
  5772. [3] = 6400000,
  5773. [4] = 2666667,
  5774. [5] = 4266667,
  5775. };
  5776. const unsigned int *vco_table;
  5777. unsigned int vco;
  5778. uint8_t tmp = 0;
  5779. /* FIXME other chipsets? */
  5780. if (IS_GM45(dev))
  5781. vco_table = ctg_vco;
  5782. else if (IS_G4X(dev))
  5783. vco_table = elk_vco;
  5784. else if (IS_CRESTLINE(dev))
  5785. vco_table = cl_vco;
  5786. else if (IS_PINEVIEW(dev))
  5787. vco_table = pnv_vco;
  5788. else if (IS_G33(dev))
  5789. vco_table = blb_vco;
  5790. else
  5791. return 0;
  5792. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5793. vco = vco_table[tmp & 0x7];
  5794. if (vco == 0)
  5795. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5796. else
  5797. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5798. return vco;
  5799. }
  5800. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5801. {
  5802. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5803. uint16_t tmp = 0;
  5804. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5805. cdclk_sel = (tmp >> 12) & 0x1;
  5806. switch (vco) {
  5807. case 2666667:
  5808. case 4000000:
  5809. case 5333333:
  5810. return cdclk_sel ? 333333 : 222222;
  5811. case 3200000:
  5812. return cdclk_sel ? 320000 : 228571;
  5813. default:
  5814. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5815. return 222222;
  5816. }
  5817. }
  5818. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5819. {
  5820. static const uint8_t div_3200[] = { 16, 10, 8 };
  5821. static const uint8_t div_4000[] = { 20, 12, 10 };
  5822. static const uint8_t div_5333[] = { 24, 16, 14 };
  5823. const uint8_t *div_table;
  5824. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5825. uint16_t tmp = 0;
  5826. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5827. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5828. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5829. goto fail;
  5830. switch (vco) {
  5831. case 3200000:
  5832. div_table = div_3200;
  5833. break;
  5834. case 4000000:
  5835. div_table = div_4000;
  5836. break;
  5837. case 5333333:
  5838. div_table = div_5333;
  5839. break;
  5840. default:
  5841. goto fail;
  5842. }
  5843. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5844. fail:
  5845. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5846. return 200000;
  5847. }
  5848. static int g33_get_display_clock_speed(struct drm_device *dev)
  5849. {
  5850. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5851. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5852. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5853. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5854. const uint8_t *div_table;
  5855. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5856. uint16_t tmp = 0;
  5857. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5858. cdclk_sel = (tmp >> 4) & 0x7;
  5859. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5860. goto fail;
  5861. switch (vco) {
  5862. case 3200000:
  5863. div_table = div_3200;
  5864. break;
  5865. case 4000000:
  5866. div_table = div_4000;
  5867. break;
  5868. case 4800000:
  5869. div_table = div_4800;
  5870. break;
  5871. case 5333333:
  5872. div_table = div_5333;
  5873. break;
  5874. default:
  5875. goto fail;
  5876. }
  5877. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5878. fail:
  5879. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5880. return 190476;
  5881. }
  5882. static void
  5883. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5884. {
  5885. while (*num > DATA_LINK_M_N_MASK ||
  5886. *den > DATA_LINK_M_N_MASK) {
  5887. *num >>= 1;
  5888. *den >>= 1;
  5889. }
  5890. }
  5891. static void compute_m_n(unsigned int m, unsigned int n,
  5892. uint32_t *ret_m, uint32_t *ret_n)
  5893. {
  5894. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5895. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5896. intel_reduce_m_n_ratio(ret_m, ret_n);
  5897. }
  5898. void
  5899. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5900. int pixel_clock, int link_clock,
  5901. struct intel_link_m_n *m_n)
  5902. {
  5903. m_n->tu = 64;
  5904. compute_m_n(bits_per_pixel * pixel_clock,
  5905. link_clock * nlanes * 8,
  5906. &m_n->gmch_m, &m_n->gmch_n);
  5907. compute_m_n(pixel_clock, link_clock,
  5908. &m_n->link_m, &m_n->link_n);
  5909. }
  5910. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5911. {
  5912. if (i915.panel_use_ssc >= 0)
  5913. return i915.panel_use_ssc != 0;
  5914. return dev_priv->vbt.lvds_use_ssc
  5915. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5916. }
  5917. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5918. int num_connectors)
  5919. {
  5920. struct drm_device *dev = crtc_state->base.crtc->dev;
  5921. struct drm_i915_private *dev_priv = dev->dev_private;
  5922. int refclk;
  5923. WARN_ON(!crtc_state->base.state);
  5924. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5925. refclk = 100000;
  5926. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5927. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5928. refclk = dev_priv->vbt.lvds_ssc_freq;
  5929. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5930. } else if (!IS_GEN2(dev)) {
  5931. refclk = 96000;
  5932. } else {
  5933. refclk = 48000;
  5934. }
  5935. return refclk;
  5936. }
  5937. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5938. {
  5939. return (1 << dpll->n) << 16 | dpll->m2;
  5940. }
  5941. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5942. {
  5943. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5944. }
  5945. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5946. struct intel_crtc_state *crtc_state,
  5947. intel_clock_t *reduced_clock)
  5948. {
  5949. struct drm_device *dev = crtc->base.dev;
  5950. u32 fp, fp2 = 0;
  5951. if (IS_PINEVIEW(dev)) {
  5952. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5953. if (reduced_clock)
  5954. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5955. } else {
  5956. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5957. if (reduced_clock)
  5958. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5959. }
  5960. crtc_state->dpll_hw_state.fp0 = fp;
  5961. crtc->lowfreq_avail = false;
  5962. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5963. reduced_clock) {
  5964. crtc_state->dpll_hw_state.fp1 = fp2;
  5965. crtc->lowfreq_avail = true;
  5966. } else {
  5967. crtc_state->dpll_hw_state.fp1 = fp;
  5968. }
  5969. }
  5970. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5971. pipe)
  5972. {
  5973. u32 reg_val;
  5974. /*
  5975. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5976. * and set it to a reasonable value instead.
  5977. */
  5978. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5979. reg_val &= 0xffffff00;
  5980. reg_val |= 0x00000030;
  5981. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5982. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5983. reg_val &= 0x8cffffff;
  5984. reg_val = 0x8c000000;
  5985. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5986. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5987. reg_val &= 0xffffff00;
  5988. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5989. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5990. reg_val &= 0x00ffffff;
  5991. reg_val |= 0xb0000000;
  5992. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5993. }
  5994. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5995. struct intel_link_m_n *m_n)
  5996. {
  5997. struct drm_device *dev = crtc->base.dev;
  5998. struct drm_i915_private *dev_priv = dev->dev_private;
  5999. int pipe = crtc->pipe;
  6000. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6001. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6002. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6003. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6004. }
  6005. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6006. struct intel_link_m_n *m_n,
  6007. struct intel_link_m_n *m2_n2)
  6008. {
  6009. struct drm_device *dev = crtc->base.dev;
  6010. struct drm_i915_private *dev_priv = dev->dev_private;
  6011. int pipe = crtc->pipe;
  6012. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6013. if (INTEL_INFO(dev)->gen >= 5) {
  6014. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6015. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6016. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6017. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6018. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6019. * for gen < 8) and if DRRS is supported (to make sure the
  6020. * registers are not unnecessarily accessed).
  6021. */
  6022. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6023. crtc->config->has_drrs) {
  6024. I915_WRITE(PIPE_DATA_M2(transcoder),
  6025. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6026. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6027. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6028. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6029. }
  6030. } else {
  6031. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6032. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6033. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6034. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6035. }
  6036. }
  6037. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6038. {
  6039. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6040. if (m_n == M1_N1) {
  6041. dp_m_n = &crtc->config->dp_m_n;
  6042. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6043. } else if (m_n == M2_N2) {
  6044. /*
  6045. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6046. * needs to be programmed into M1_N1.
  6047. */
  6048. dp_m_n = &crtc->config->dp_m2_n2;
  6049. } else {
  6050. DRM_ERROR("Unsupported divider value\n");
  6051. return;
  6052. }
  6053. if (crtc->config->has_pch_encoder)
  6054. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6055. else
  6056. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6057. }
  6058. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6059. struct intel_crtc_state *pipe_config)
  6060. {
  6061. u32 dpll, dpll_md;
  6062. /*
  6063. * Enable DPIO clock input. We should never disable the reference
  6064. * clock for pipe B, since VGA hotplug / manual detection depends
  6065. * on it.
  6066. */
  6067. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6068. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6069. /* We should never disable this, set it here for state tracking */
  6070. if (crtc->pipe == PIPE_B)
  6071. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6072. dpll |= DPLL_VCO_ENABLE;
  6073. pipe_config->dpll_hw_state.dpll = dpll;
  6074. dpll_md = (pipe_config->pixel_multiplier - 1)
  6075. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6076. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6077. }
  6078. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6079. const struct intel_crtc_state *pipe_config)
  6080. {
  6081. struct drm_device *dev = crtc->base.dev;
  6082. struct drm_i915_private *dev_priv = dev->dev_private;
  6083. int pipe = crtc->pipe;
  6084. u32 mdiv;
  6085. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6086. u32 coreclk, reg_val;
  6087. mutex_lock(&dev_priv->sb_lock);
  6088. bestn = pipe_config->dpll.n;
  6089. bestm1 = pipe_config->dpll.m1;
  6090. bestm2 = pipe_config->dpll.m2;
  6091. bestp1 = pipe_config->dpll.p1;
  6092. bestp2 = pipe_config->dpll.p2;
  6093. /* See eDP HDMI DPIO driver vbios notes doc */
  6094. /* PLL B needs special handling */
  6095. if (pipe == PIPE_B)
  6096. vlv_pllb_recal_opamp(dev_priv, pipe);
  6097. /* Set up Tx target for periodic Rcomp update */
  6098. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6099. /* Disable target IRef on PLL */
  6100. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6101. reg_val &= 0x00ffffff;
  6102. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6103. /* Disable fast lock */
  6104. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6105. /* Set idtafcrecal before PLL is enabled */
  6106. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6107. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6108. mdiv |= ((bestn << DPIO_N_SHIFT));
  6109. mdiv |= (1 << DPIO_K_SHIFT);
  6110. /*
  6111. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6112. * but we don't support that).
  6113. * Note: don't use the DAC post divider as it seems unstable.
  6114. */
  6115. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6116. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6117. mdiv |= DPIO_ENABLE_CALIBRATION;
  6118. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6119. /* Set HBR and RBR LPF coefficients */
  6120. if (pipe_config->port_clock == 162000 ||
  6121. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6122. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6123. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6124. 0x009f0003);
  6125. else
  6126. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6127. 0x00d0000f);
  6128. if (pipe_config->has_dp_encoder) {
  6129. /* Use SSC source */
  6130. if (pipe == PIPE_A)
  6131. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6132. 0x0df40000);
  6133. else
  6134. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6135. 0x0df70000);
  6136. } else { /* HDMI or VGA */
  6137. /* Use bend source */
  6138. if (pipe == PIPE_A)
  6139. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6140. 0x0df70000);
  6141. else
  6142. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6143. 0x0df40000);
  6144. }
  6145. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6146. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6147. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6148. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6149. coreclk |= 0x01000000;
  6150. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6151. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6152. mutex_unlock(&dev_priv->sb_lock);
  6153. }
  6154. static void chv_compute_dpll(struct intel_crtc *crtc,
  6155. struct intel_crtc_state *pipe_config)
  6156. {
  6157. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6158. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6159. DPLL_VCO_ENABLE;
  6160. if (crtc->pipe != PIPE_A)
  6161. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6162. pipe_config->dpll_hw_state.dpll_md =
  6163. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6164. }
  6165. static void chv_prepare_pll(struct intel_crtc *crtc,
  6166. const struct intel_crtc_state *pipe_config)
  6167. {
  6168. struct drm_device *dev = crtc->base.dev;
  6169. struct drm_i915_private *dev_priv = dev->dev_private;
  6170. int pipe = crtc->pipe;
  6171. int dpll_reg = DPLL(crtc->pipe);
  6172. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6173. u32 loopfilter, tribuf_calcntr;
  6174. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6175. u32 dpio_val;
  6176. int vco;
  6177. bestn = pipe_config->dpll.n;
  6178. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6179. bestm1 = pipe_config->dpll.m1;
  6180. bestm2 = pipe_config->dpll.m2 >> 22;
  6181. bestp1 = pipe_config->dpll.p1;
  6182. bestp2 = pipe_config->dpll.p2;
  6183. vco = pipe_config->dpll.vco;
  6184. dpio_val = 0;
  6185. loopfilter = 0;
  6186. /*
  6187. * Enable Refclk and SSC
  6188. */
  6189. I915_WRITE(dpll_reg,
  6190. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6191. mutex_lock(&dev_priv->sb_lock);
  6192. /* p1 and p2 divider */
  6193. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6194. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6195. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6196. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6197. 1 << DPIO_CHV_K_DIV_SHIFT);
  6198. /* Feedback post-divider - m2 */
  6199. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6200. /* Feedback refclk divider - n and m1 */
  6201. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6202. DPIO_CHV_M1_DIV_BY_2 |
  6203. 1 << DPIO_CHV_N_DIV_SHIFT);
  6204. /* M2 fraction division */
  6205. if (bestm2_frac)
  6206. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6207. /* M2 fraction division enable */
  6208. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6209. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6210. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6211. if (bestm2_frac)
  6212. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6213. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6214. /* Program digital lock detect threshold */
  6215. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6216. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6217. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6218. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6219. if (!bestm2_frac)
  6220. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6221. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6222. /* Loop filter */
  6223. if (vco == 5400000) {
  6224. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6225. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6226. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6227. tribuf_calcntr = 0x9;
  6228. } else if (vco <= 6200000) {
  6229. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6230. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6231. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6232. tribuf_calcntr = 0x9;
  6233. } else if (vco <= 6480000) {
  6234. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6235. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6236. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6237. tribuf_calcntr = 0x8;
  6238. } else {
  6239. /* Not supported. Apply the same limits as in the max case */
  6240. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6241. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6242. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6243. tribuf_calcntr = 0;
  6244. }
  6245. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6246. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6247. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6248. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6249. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6250. /* AFC Recal */
  6251. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6252. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6253. DPIO_AFC_RECAL);
  6254. mutex_unlock(&dev_priv->sb_lock);
  6255. }
  6256. /**
  6257. * vlv_force_pll_on - forcibly enable just the PLL
  6258. * @dev_priv: i915 private structure
  6259. * @pipe: pipe PLL to enable
  6260. * @dpll: PLL configuration
  6261. *
  6262. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6263. * in cases where we need the PLL enabled even when @pipe is not going to
  6264. * be enabled.
  6265. */
  6266. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6267. const struct dpll *dpll)
  6268. {
  6269. struct intel_crtc *crtc =
  6270. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6271. struct intel_crtc_state pipe_config = {
  6272. .base.crtc = &crtc->base,
  6273. .pixel_multiplier = 1,
  6274. .dpll = *dpll,
  6275. };
  6276. if (IS_CHERRYVIEW(dev)) {
  6277. chv_compute_dpll(crtc, &pipe_config);
  6278. chv_prepare_pll(crtc, &pipe_config);
  6279. chv_enable_pll(crtc, &pipe_config);
  6280. } else {
  6281. vlv_compute_dpll(crtc, &pipe_config);
  6282. vlv_prepare_pll(crtc, &pipe_config);
  6283. vlv_enable_pll(crtc, &pipe_config);
  6284. }
  6285. }
  6286. /**
  6287. * vlv_force_pll_off - forcibly disable just the PLL
  6288. * @dev_priv: i915 private structure
  6289. * @pipe: pipe PLL to disable
  6290. *
  6291. * Disable the PLL for @pipe. To be used in cases where we need
  6292. * the PLL enabled even when @pipe is not going to be enabled.
  6293. */
  6294. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6295. {
  6296. if (IS_CHERRYVIEW(dev))
  6297. chv_disable_pll(to_i915(dev), pipe);
  6298. else
  6299. vlv_disable_pll(to_i915(dev), pipe);
  6300. }
  6301. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6302. struct intel_crtc_state *crtc_state,
  6303. intel_clock_t *reduced_clock,
  6304. int num_connectors)
  6305. {
  6306. struct drm_device *dev = crtc->base.dev;
  6307. struct drm_i915_private *dev_priv = dev->dev_private;
  6308. u32 dpll;
  6309. bool is_sdvo;
  6310. struct dpll *clock = &crtc_state->dpll;
  6311. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6312. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6313. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6314. dpll = DPLL_VGA_MODE_DIS;
  6315. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6316. dpll |= DPLLB_MODE_LVDS;
  6317. else
  6318. dpll |= DPLLB_MODE_DAC_SERIAL;
  6319. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6320. dpll |= (crtc_state->pixel_multiplier - 1)
  6321. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6322. }
  6323. if (is_sdvo)
  6324. dpll |= DPLL_SDVO_HIGH_SPEED;
  6325. if (crtc_state->has_dp_encoder)
  6326. dpll |= DPLL_SDVO_HIGH_SPEED;
  6327. /* compute bitmask from p1 value */
  6328. if (IS_PINEVIEW(dev))
  6329. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6330. else {
  6331. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6332. if (IS_G4X(dev) && reduced_clock)
  6333. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6334. }
  6335. switch (clock->p2) {
  6336. case 5:
  6337. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6338. break;
  6339. case 7:
  6340. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6341. break;
  6342. case 10:
  6343. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6344. break;
  6345. case 14:
  6346. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6347. break;
  6348. }
  6349. if (INTEL_INFO(dev)->gen >= 4)
  6350. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6351. if (crtc_state->sdvo_tv_clock)
  6352. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6353. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6354. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6355. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6356. else
  6357. dpll |= PLL_REF_INPUT_DREFCLK;
  6358. dpll |= DPLL_VCO_ENABLE;
  6359. crtc_state->dpll_hw_state.dpll = dpll;
  6360. if (INTEL_INFO(dev)->gen >= 4) {
  6361. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6362. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6363. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6364. }
  6365. }
  6366. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6367. struct intel_crtc_state *crtc_state,
  6368. intel_clock_t *reduced_clock,
  6369. int num_connectors)
  6370. {
  6371. struct drm_device *dev = crtc->base.dev;
  6372. struct drm_i915_private *dev_priv = dev->dev_private;
  6373. u32 dpll;
  6374. struct dpll *clock = &crtc_state->dpll;
  6375. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6376. dpll = DPLL_VGA_MODE_DIS;
  6377. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6378. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6379. } else {
  6380. if (clock->p1 == 2)
  6381. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6382. else
  6383. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6384. if (clock->p2 == 4)
  6385. dpll |= PLL_P2_DIVIDE_BY_4;
  6386. }
  6387. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6388. dpll |= DPLL_DVO_2X_MODE;
  6389. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6390. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6391. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6392. else
  6393. dpll |= PLL_REF_INPUT_DREFCLK;
  6394. dpll |= DPLL_VCO_ENABLE;
  6395. crtc_state->dpll_hw_state.dpll = dpll;
  6396. }
  6397. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6398. {
  6399. struct drm_device *dev = intel_crtc->base.dev;
  6400. struct drm_i915_private *dev_priv = dev->dev_private;
  6401. enum pipe pipe = intel_crtc->pipe;
  6402. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6403. struct drm_display_mode *adjusted_mode =
  6404. &intel_crtc->config->base.adjusted_mode;
  6405. uint32_t crtc_vtotal, crtc_vblank_end;
  6406. int vsyncshift = 0;
  6407. /* We need to be careful not to changed the adjusted mode, for otherwise
  6408. * the hw state checker will get angry at the mismatch. */
  6409. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6410. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6411. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6412. /* the chip adds 2 halflines automatically */
  6413. crtc_vtotal -= 1;
  6414. crtc_vblank_end -= 1;
  6415. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6416. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6417. else
  6418. vsyncshift = adjusted_mode->crtc_hsync_start -
  6419. adjusted_mode->crtc_htotal / 2;
  6420. if (vsyncshift < 0)
  6421. vsyncshift += adjusted_mode->crtc_htotal;
  6422. }
  6423. if (INTEL_INFO(dev)->gen > 3)
  6424. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6425. I915_WRITE(HTOTAL(cpu_transcoder),
  6426. (adjusted_mode->crtc_hdisplay - 1) |
  6427. ((adjusted_mode->crtc_htotal - 1) << 16));
  6428. I915_WRITE(HBLANK(cpu_transcoder),
  6429. (adjusted_mode->crtc_hblank_start - 1) |
  6430. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6431. I915_WRITE(HSYNC(cpu_transcoder),
  6432. (adjusted_mode->crtc_hsync_start - 1) |
  6433. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6434. I915_WRITE(VTOTAL(cpu_transcoder),
  6435. (adjusted_mode->crtc_vdisplay - 1) |
  6436. ((crtc_vtotal - 1) << 16));
  6437. I915_WRITE(VBLANK(cpu_transcoder),
  6438. (adjusted_mode->crtc_vblank_start - 1) |
  6439. ((crtc_vblank_end - 1) << 16));
  6440. I915_WRITE(VSYNC(cpu_transcoder),
  6441. (adjusted_mode->crtc_vsync_start - 1) |
  6442. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6443. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6444. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6445. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6446. * bits. */
  6447. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6448. (pipe == PIPE_B || pipe == PIPE_C))
  6449. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6450. /* pipesrc controls the size that is scaled from, which should
  6451. * always be the user's requested size.
  6452. */
  6453. I915_WRITE(PIPESRC(pipe),
  6454. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6455. (intel_crtc->config->pipe_src_h - 1));
  6456. }
  6457. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6458. struct intel_crtc_state *pipe_config)
  6459. {
  6460. struct drm_device *dev = crtc->base.dev;
  6461. struct drm_i915_private *dev_priv = dev->dev_private;
  6462. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6463. uint32_t tmp;
  6464. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6465. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6466. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6467. tmp = I915_READ(HBLANK(cpu_transcoder));
  6468. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6469. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6470. tmp = I915_READ(HSYNC(cpu_transcoder));
  6471. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6472. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6473. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6474. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6475. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6476. tmp = I915_READ(VBLANK(cpu_transcoder));
  6477. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6478. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6479. tmp = I915_READ(VSYNC(cpu_transcoder));
  6480. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6481. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6482. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6483. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6484. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6485. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6486. }
  6487. tmp = I915_READ(PIPESRC(crtc->pipe));
  6488. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6489. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6490. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6491. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6492. }
  6493. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6494. struct intel_crtc_state *pipe_config)
  6495. {
  6496. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6497. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6498. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6499. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6500. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6501. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6502. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6503. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6504. mode->flags = pipe_config->base.adjusted_mode.flags;
  6505. mode->type = DRM_MODE_TYPE_DRIVER;
  6506. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6507. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6508. mode->hsync = drm_mode_hsync(mode);
  6509. mode->vrefresh = drm_mode_vrefresh(mode);
  6510. drm_mode_set_name(mode);
  6511. }
  6512. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6513. {
  6514. struct drm_device *dev = intel_crtc->base.dev;
  6515. struct drm_i915_private *dev_priv = dev->dev_private;
  6516. uint32_t pipeconf;
  6517. pipeconf = 0;
  6518. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6519. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6520. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6521. if (intel_crtc->config->double_wide)
  6522. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6523. /* only g4x and later have fancy bpc/dither controls */
  6524. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6525. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6526. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6527. pipeconf |= PIPECONF_DITHER_EN |
  6528. PIPECONF_DITHER_TYPE_SP;
  6529. switch (intel_crtc->config->pipe_bpp) {
  6530. case 18:
  6531. pipeconf |= PIPECONF_6BPC;
  6532. break;
  6533. case 24:
  6534. pipeconf |= PIPECONF_8BPC;
  6535. break;
  6536. case 30:
  6537. pipeconf |= PIPECONF_10BPC;
  6538. break;
  6539. default:
  6540. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6541. BUG();
  6542. }
  6543. }
  6544. if (HAS_PIPE_CXSR(dev)) {
  6545. if (intel_crtc->lowfreq_avail) {
  6546. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6547. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6548. } else {
  6549. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6550. }
  6551. }
  6552. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6553. if (INTEL_INFO(dev)->gen < 4 ||
  6554. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6555. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6556. else
  6557. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6558. } else
  6559. pipeconf |= PIPECONF_PROGRESSIVE;
  6560. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6561. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6562. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6563. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6564. }
  6565. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6566. struct intel_crtc_state *crtc_state)
  6567. {
  6568. struct drm_device *dev = crtc->base.dev;
  6569. struct drm_i915_private *dev_priv = dev->dev_private;
  6570. int refclk, num_connectors = 0;
  6571. intel_clock_t clock;
  6572. bool ok;
  6573. bool is_dsi = false;
  6574. struct intel_encoder *encoder;
  6575. const intel_limit_t *limit;
  6576. struct drm_atomic_state *state = crtc_state->base.state;
  6577. struct drm_connector *connector;
  6578. struct drm_connector_state *connector_state;
  6579. int i;
  6580. memset(&crtc_state->dpll_hw_state, 0,
  6581. sizeof(crtc_state->dpll_hw_state));
  6582. for_each_connector_in_state(state, connector, connector_state, i) {
  6583. if (connector_state->crtc != &crtc->base)
  6584. continue;
  6585. encoder = to_intel_encoder(connector_state->best_encoder);
  6586. switch (encoder->type) {
  6587. case INTEL_OUTPUT_DSI:
  6588. is_dsi = true;
  6589. break;
  6590. default:
  6591. break;
  6592. }
  6593. num_connectors++;
  6594. }
  6595. if (is_dsi)
  6596. return 0;
  6597. if (!crtc_state->clock_set) {
  6598. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6599. /*
  6600. * Returns a set of divisors for the desired target clock with
  6601. * the given refclk, or FALSE. The returned values represent
  6602. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6603. * 2) / p1 / p2.
  6604. */
  6605. limit = intel_limit(crtc_state, refclk);
  6606. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6607. crtc_state->port_clock,
  6608. refclk, NULL, &clock);
  6609. if (!ok) {
  6610. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6611. return -EINVAL;
  6612. }
  6613. /* Compat-code for transition, will disappear. */
  6614. crtc_state->dpll.n = clock.n;
  6615. crtc_state->dpll.m1 = clock.m1;
  6616. crtc_state->dpll.m2 = clock.m2;
  6617. crtc_state->dpll.p1 = clock.p1;
  6618. crtc_state->dpll.p2 = clock.p2;
  6619. }
  6620. if (IS_GEN2(dev)) {
  6621. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6622. num_connectors);
  6623. } else if (IS_CHERRYVIEW(dev)) {
  6624. chv_compute_dpll(crtc, crtc_state);
  6625. } else if (IS_VALLEYVIEW(dev)) {
  6626. vlv_compute_dpll(crtc, crtc_state);
  6627. } else {
  6628. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6629. num_connectors);
  6630. }
  6631. return 0;
  6632. }
  6633. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6634. struct intel_crtc_state *pipe_config)
  6635. {
  6636. struct drm_device *dev = crtc->base.dev;
  6637. struct drm_i915_private *dev_priv = dev->dev_private;
  6638. uint32_t tmp;
  6639. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6640. return;
  6641. tmp = I915_READ(PFIT_CONTROL);
  6642. if (!(tmp & PFIT_ENABLE))
  6643. return;
  6644. /* Check whether the pfit is attached to our pipe. */
  6645. if (INTEL_INFO(dev)->gen < 4) {
  6646. if (crtc->pipe != PIPE_B)
  6647. return;
  6648. } else {
  6649. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6650. return;
  6651. }
  6652. pipe_config->gmch_pfit.control = tmp;
  6653. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6654. if (INTEL_INFO(dev)->gen < 5)
  6655. pipe_config->gmch_pfit.lvds_border_bits =
  6656. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6657. }
  6658. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6659. struct intel_crtc_state *pipe_config)
  6660. {
  6661. struct drm_device *dev = crtc->base.dev;
  6662. struct drm_i915_private *dev_priv = dev->dev_private;
  6663. int pipe = pipe_config->cpu_transcoder;
  6664. intel_clock_t clock;
  6665. u32 mdiv;
  6666. int refclk = 100000;
  6667. /* In case of MIPI DPLL will not even be used */
  6668. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6669. return;
  6670. mutex_lock(&dev_priv->sb_lock);
  6671. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6672. mutex_unlock(&dev_priv->sb_lock);
  6673. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6674. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6675. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6676. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6677. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6678. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6679. }
  6680. static void
  6681. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6682. struct intel_initial_plane_config *plane_config)
  6683. {
  6684. struct drm_device *dev = crtc->base.dev;
  6685. struct drm_i915_private *dev_priv = dev->dev_private;
  6686. u32 val, base, offset;
  6687. int pipe = crtc->pipe, plane = crtc->plane;
  6688. int fourcc, pixel_format;
  6689. unsigned int aligned_height;
  6690. struct drm_framebuffer *fb;
  6691. struct intel_framebuffer *intel_fb;
  6692. val = I915_READ(DSPCNTR(plane));
  6693. if (!(val & DISPLAY_PLANE_ENABLE))
  6694. return;
  6695. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6696. if (!intel_fb) {
  6697. DRM_DEBUG_KMS("failed to alloc fb\n");
  6698. return;
  6699. }
  6700. fb = &intel_fb->base;
  6701. if (INTEL_INFO(dev)->gen >= 4) {
  6702. if (val & DISPPLANE_TILED) {
  6703. plane_config->tiling = I915_TILING_X;
  6704. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6705. }
  6706. }
  6707. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6708. fourcc = i9xx_format_to_fourcc(pixel_format);
  6709. fb->pixel_format = fourcc;
  6710. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6711. if (INTEL_INFO(dev)->gen >= 4) {
  6712. if (plane_config->tiling)
  6713. offset = I915_READ(DSPTILEOFF(plane));
  6714. else
  6715. offset = I915_READ(DSPLINOFF(plane));
  6716. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6717. } else {
  6718. base = I915_READ(DSPADDR(plane));
  6719. }
  6720. plane_config->base = base;
  6721. val = I915_READ(PIPESRC(pipe));
  6722. fb->width = ((val >> 16) & 0xfff) + 1;
  6723. fb->height = ((val >> 0) & 0xfff) + 1;
  6724. val = I915_READ(DSPSTRIDE(pipe));
  6725. fb->pitches[0] = val & 0xffffffc0;
  6726. aligned_height = intel_fb_align_height(dev, fb->height,
  6727. fb->pixel_format,
  6728. fb->modifier[0]);
  6729. plane_config->size = fb->pitches[0] * aligned_height;
  6730. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6731. pipe_name(pipe), plane, fb->width, fb->height,
  6732. fb->bits_per_pixel, base, fb->pitches[0],
  6733. plane_config->size);
  6734. plane_config->fb = intel_fb;
  6735. }
  6736. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6737. struct intel_crtc_state *pipe_config)
  6738. {
  6739. struct drm_device *dev = crtc->base.dev;
  6740. struct drm_i915_private *dev_priv = dev->dev_private;
  6741. int pipe = pipe_config->cpu_transcoder;
  6742. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6743. intel_clock_t clock;
  6744. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6745. int refclk = 100000;
  6746. mutex_lock(&dev_priv->sb_lock);
  6747. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6748. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6749. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6750. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6751. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6752. mutex_unlock(&dev_priv->sb_lock);
  6753. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6754. clock.m2 = (pll_dw0 & 0xff) << 22;
  6755. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6756. clock.m2 |= pll_dw2 & 0x3fffff;
  6757. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6758. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6759. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6760. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6761. }
  6762. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6763. struct intel_crtc_state *pipe_config)
  6764. {
  6765. struct drm_device *dev = crtc->base.dev;
  6766. struct drm_i915_private *dev_priv = dev->dev_private;
  6767. uint32_t tmp;
  6768. if (!intel_display_power_is_enabled(dev_priv,
  6769. POWER_DOMAIN_PIPE(crtc->pipe)))
  6770. return false;
  6771. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6772. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6773. tmp = I915_READ(PIPECONF(crtc->pipe));
  6774. if (!(tmp & PIPECONF_ENABLE))
  6775. return false;
  6776. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6777. switch (tmp & PIPECONF_BPC_MASK) {
  6778. case PIPECONF_6BPC:
  6779. pipe_config->pipe_bpp = 18;
  6780. break;
  6781. case PIPECONF_8BPC:
  6782. pipe_config->pipe_bpp = 24;
  6783. break;
  6784. case PIPECONF_10BPC:
  6785. pipe_config->pipe_bpp = 30;
  6786. break;
  6787. default:
  6788. break;
  6789. }
  6790. }
  6791. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6792. pipe_config->limited_color_range = true;
  6793. if (INTEL_INFO(dev)->gen < 4)
  6794. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6795. intel_get_pipe_timings(crtc, pipe_config);
  6796. i9xx_get_pfit_config(crtc, pipe_config);
  6797. if (INTEL_INFO(dev)->gen >= 4) {
  6798. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6799. pipe_config->pixel_multiplier =
  6800. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6801. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6802. pipe_config->dpll_hw_state.dpll_md = tmp;
  6803. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6804. tmp = I915_READ(DPLL(crtc->pipe));
  6805. pipe_config->pixel_multiplier =
  6806. ((tmp & SDVO_MULTIPLIER_MASK)
  6807. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6808. } else {
  6809. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6810. * port and will be fixed up in the encoder->get_config
  6811. * function. */
  6812. pipe_config->pixel_multiplier = 1;
  6813. }
  6814. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6815. if (!IS_VALLEYVIEW(dev)) {
  6816. /*
  6817. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6818. * on 830. Filter it out here so that we don't
  6819. * report errors due to that.
  6820. */
  6821. if (IS_I830(dev))
  6822. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6823. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6824. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6825. } else {
  6826. /* Mask out read-only status bits. */
  6827. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6828. DPLL_PORTC_READY_MASK |
  6829. DPLL_PORTB_READY_MASK);
  6830. }
  6831. if (IS_CHERRYVIEW(dev))
  6832. chv_crtc_clock_get(crtc, pipe_config);
  6833. else if (IS_VALLEYVIEW(dev))
  6834. vlv_crtc_clock_get(crtc, pipe_config);
  6835. else
  6836. i9xx_crtc_clock_get(crtc, pipe_config);
  6837. return true;
  6838. }
  6839. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6840. {
  6841. struct drm_i915_private *dev_priv = dev->dev_private;
  6842. struct intel_encoder *encoder;
  6843. u32 val, final;
  6844. bool has_lvds = false;
  6845. bool has_cpu_edp = false;
  6846. bool has_panel = false;
  6847. bool has_ck505 = false;
  6848. bool can_ssc = false;
  6849. /* We need to take the global config into account */
  6850. for_each_intel_encoder(dev, encoder) {
  6851. switch (encoder->type) {
  6852. case INTEL_OUTPUT_LVDS:
  6853. has_panel = true;
  6854. has_lvds = true;
  6855. break;
  6856. case INTEL_OUTPUT_EDP:
  6857. has_panel = true;
  6858. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6859. has_cpu_edp = true;
  6860. break;
  6861. default:
  6862. break;
  6863. }
  6864. }
  6865. if (HAS_PCH_IBX(dev)) {
  6866. has_ck505 = dev_priv->vbt.display_clock_mode;
  6867. can_ssc = has_ck505;
  6868. } else {
  6869. has_ck505 = false;
  6870. can_ssc = true;
  6871. }
  6872. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6873. has_panel, has_lvds, has_ck505);
  6874. /* Ironlake: try to setup display ref clock before DPLL
  6875. * enabling. This is only under driver's control after
  6876. * PCH B stepping, previous chipset stepping should be
  6877. * ignoring this setting.
  6878. */
  6879. val = I915_READ(PCH_DREF_CONTROL);
  6880. /* As we must carefully and slowly disable/enable each source in turn,
  6881. * compute the final state we want first and check if we need to
  6882. * make any changes at all.
  6883. */
  6884. final = val;
  6885. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6886. if (has_ck505)
  6887. final |= DREF_NONSPREAD_CK505_ENABLE;
  6888. else
  6889. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6890. final &= ~DREF_SSC_SOURCE_MASK;
  6891. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6892. final &= ~DREF_SSC1_ENABLE;
  6893. if (has_panel) {
  6894. final |= DREF_SSC_SOURCE_ENABLE;
  6895. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6896. final |= DREF_SSC1_ENABLE;
  6897. if (has_cpu_edp) {
  6898. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6899. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6900. else
  6901. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6902. } else
  6903. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6904. } else {
  6905. final |= DREF_SSC_SOURCE_DISABLE;
  6906. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6907. }
  6908. if (final == val)
  6909. return;
  6910. /* Always enable nonspread source */
  6911. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6912. if (has_ck505)
  6913. val |= DREF_NONSPREAD_CK505_ENABLE;
  6914. else
  6915. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6916. if (has_panel) {
  6917. val &= ~DREF_SSC_SOURCE_MASK;
  6918. val |= DREF_SSC_SOURCE_ENABLE;
  6919. /* SSC must be turned on before enabling the CPU output */
  6920. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6921. DRM_DEBUG_KMS("Using SSC on panel\n");
  6922. val |= DREF_SSC1_ENABLE;
  6923. } else
  6924. val &= ~DREF_SSC1_ENABLE;
  6925. /* Get SSC going before enabling the outputs */
  6926. I915_WRITE(PCH_DREF_CONTROL, val);
  6927. POSTING_READ(PCH_DREF_CONTROL);
  6928. udelay(200);
  6929. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6930. /* Enable CPU source on CPU attached eDP */
  6931. if (has_cpu_edp) {
  6932. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6933. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6934. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6935. } else
  6936. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6937. } else
  6938. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6939. I915_WRITE(PCH_DREF_CONTROL, val);
  6940. POSTING_READ(PCH_DREF_CONTROL);
  6941. udelay(200);
  6942. } else {
  6943. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6944. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6945. /* Turn off CPU output */
  6946. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6947. I915_WRITE(PCH_DREF_CONTROL, val);
  6948. POSTING_READ(PCH_DREF_CONTROL);
  6949. udelay(200);
  6950. /* Turn off the SSC source */
  6951. val &= ~DREF_SSC_SOURCE_MASK;
  6952. val |= DREF_SSC_SOURCE_DISABLE;
  6953. /* Turn off SSC1 */
  6954. val &= ~DREF_SSC1_ENABLE;
  6955. I915_WRITE(PCH_DREF_CONTROL, val);
  6956. POSTING_READ(PCH_DREF_CONTROL);
  6957. udelay(200);
  6958. }
  6959. BUG_ON(val != final);
  6960. }
  6961. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6962. {
  6963. uint32_t tmp;
  6964. tmp = I915_READ(SOUTH_CHICKEN2);
  6965. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6966. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6967. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6968. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6969. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6970. tmp = I915_READ(SOUTH_CHICKEN2);
  6971. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6972. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6973. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6974. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6975. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6976. }
  6977. /* WaMPhyProgramming:hsw */
  6978. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6979. {
  6980. uint32_t tmp;
  6981. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6982. tmp &= ~(0xFF << 24);
  6983. tmp |= (0x12 << 24);
  6984. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6985. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6986. tmp |= (1 << 11);
  6987. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6988. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6989. tmp |= (1 << 11);
  6990. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6991. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6992. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6993. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6994. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6995. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6996. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6997. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6998. tmp &= ~(7 << 13);
  6999. tmp |= (5 << 13);
  7000. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7001. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7002. tmp &= ~(7 << 13);
  7003. tmp |= (5 << 13);
  7004. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7005. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7006. tmp &= ~0xFF;
  7007. tmp |= 0x1C;
  7008. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7009. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7010. tmp &= ~0xFF;
  7011. tmp |= 0x1C;
  7012. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7013. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7014. tmp &= ~(0xFF << 16);
  7015. tmp |= (0x1C << 16);
  7016. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7017. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7018. tmp &= ~(0xFF << 16);
  7019. tmp |= (0x1C << 16);
  7020. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7021. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7022. tmp |= (1 << 27);
  7023. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7024. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7025. tmp |= (1 << 27);
  7026. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7027. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7028. tmp &= ~(0xF << 28);
  7029. tmp |= (4 << 28);
  7030. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7031. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7032. tmp &= ~(0xF << 28);
  7033. tmp |= (4 << 28);
  7034. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7035. }
  7036. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7037. * Programming" based on the parameters passed:
  7038. * - Sequence to enable CLKOUT_DP
  7039. * - Sequence to enable CLKOUT_DP without spread
  7040. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7041. */
  7042. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7043. bool with_fdi)
  7044. {
  7045. struct drm_i915_private *dev_priv = dev->dev_private;
  7046. uint32_t reg, tmp;
  7047. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7048. with_spread = true;
  7049. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7050. with_fdi, "LP PCH doesn't have FDI\n"))
  7051. with_fdi = false;
  7052. mutex_lock(&dev_priv->sb_lock);
  7053. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7054. tmp &= ~SBI_SSCCTL_DISABLE;
  7055. tmp |= SBI_SSCCTL_PATHALT;
  7056. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7057. udelay(24);
  7058. if (with_spread) {
  7059. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7060. tmp &= ~SBI_SSCCTL_PATHALT;
  7061. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7062. if (with_fdi) {
  7063. lpt_reset_fdi_mphy(dev_priv);
  7064. lpt_program_fdi_mphy(dev_priv);
  7065. }
  7066. }
  7067. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7068. SBI_GEN0 : SBI_DBUFF0;
  7069. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7070. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7071. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7072. mutex_unlock(&dev_priv->sb_lock);
  7073. }
  7074. /* Sequence to disable CLKOUT_DP */
  7075. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7076. {
  7077. struct drm_i915_private *dev_priv = dev->dev_private;
  7078. uint32_t reg, tmp;
  7079. mutex_lock(&dev_priv->sb_lock);
  7080. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7081. SBI_GEN0 : SBI_DBUFF0;
  7082. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7083. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7084. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7085. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7086. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7087. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7088. tmp |= SBI_SSCCTL_PATHALT;
  7089. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7090. udelay(32);
  7091. }
  7092. tmp |= SBI_SSCCTL_DISABLE;
  7093. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7094. }
  7095. mutex_unlock(&dev_priv->sb_lock);
  7096. }
  7097. static void lpt_init_pch_refclk(struct drm_device *dev)
  7098. {
  7099. struct intel_encoder *encoder;
  7100. bool has_vga = false;
  7101. for_each_intel_encoder(dev, encoder) {
  7102. switch (encoder->type) {
  7103. case INTEL_OUTPUT_ANALOG:
  7104. has_vga = true;
  7105. break;
  7106. default:
  7107. break;
  7108. }
  7109. }
  7110. if (has_vga)
  7111. lpt_enable_clkout_dp(dev, true, true);
  7112. else
  7113. lpt_disable_clkout_dp(dev);
  7114. }
  7115. /*
  7116. * Initialize reference clocks when the driver loads
  7117. */
  7118. void intel_init_pch_refclk(struct drm_device *dev)
  7119. {
  7120. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7121. ironlake_init_pch_refclk(dev);
  7122. else if (HAS_PCH_LPT(dev))
  7123. lpt_init_pch_refclk(dev);
  7124. }
  7125. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7126. {
  7127. struct drm_device *dev = crtc_state->base.crtc->dev;
  7128. struct drm_i915_private *dev_priv = dev->dev_private;
  7129. struct drm_atomic_state *state = crtc_state->base.state;
  7130. struct drm_connector *connector;
  7131. struct drm_connector_state *connector_state;
  7132. struct intel_encoder *encoder;
  7133. int num_connectors = 0, i;
  7134. bool is_lvds = false;
  7135. for_each_connector_in_state(state, connector, connector_state, i) {
  7136. if (connector_state->crtc != crtc_state->base.crtc)
  7137. continue;
  7138. encoder = to_intel_encoder(connector_state->best_encoder);
  7139. switch (encoder->type) {
  7140. case INTEL_OUTPUT_LVDS:
  7141. is_lvds = true;
  7142. break;
  7143. default:
  7144. break;
  7145. }
  7146. num_connectors++;
  7147. }
  7148. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7149. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7150. dev_priv->vbt.lvds_ssc_freq);
  7151. return dev_priv->vbt.lvds_ssc_freq;
  7152. }
  7153. return 120000;
  7154. }
  7155. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7156. {
  7157. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7158. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7159. int pipe = intel_crtc->pipe;
  7160. uint32_t val;
  7161. val = 0;
  7162. switch (intel_crtc->config->pipe_bpp) {
  7163. case 18:
  7164. val |= PIPECONF_6BPC;
  7165. break;
  7166. case 24:
  7167. val |= PIPECONF_8BPC;
  7168. break;
  7169. case 30:
  7170. val |= PIPECONF_10BPC;
  7171. break;
  7172. case 36:
  7173. val |= PIPECONF_12BPC;
  7174. break;
  7175. default:
  7176. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7177. BUG();
  7178. }
  7179. if (intel_crtc->config->dither)
  7180. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7181. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7182. val |= PIPECONF_INTERLACED_ILK;
  7183. else
  7184. val |= PIPECONF_PROGRESSIVE;
  7185. if (intel_crtc->config->limited_color_range)
  7186. val |= PIPECONF_COLOR_RANGE_SELECT;
  7187. I915_WRITE(PIPECONF(pipe), val);
  7188. POSTING_READ(PIPECONF(pipe));
  7189. }
  7190. /*
  7191. * Set up the pipe CSC unit.
  7192. *
  7193. * Currently only full range RGB to limited range RGB conversion
  7194. * is supported, but eventually this should handle various
  7195. * RGB<->YCbCr scenarios as well.
  7196. */
  7197. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7198. {
  7199. struct drm_device *dev = crtc->dev;
  7200. struct drm_i915_private *dev_priv = dev->dev_private;
  7201. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7202. int pipe = intel_crtc->pipe;
  7203. uint16_t coeff = 0x7800; /* 1.0 */
  7204. /*
  7205. * TODO: Check what kind of values actually come out of the pipe
  7206. * with these coeff/postoff values and adjust to get the best
  7207. * accuracy. Perhaps we even need to take the bpc value into
  7208. * consideration.
  7209. */
  7210. if (intel_crtc->config->limited_color_range)
  7211. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7212. /*
  7213. * GY/GU and RY/RU should be the other way around according
  7214. * to BSpec, but reality doesn't agree. Just set them up in
  7215. * a way that results in the correct picture.
  7216. */
  7217. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7218. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7219. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7220. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7221. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7222. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7223. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7224. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7225. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7226. if (INTEL_INFO(dev)->gen > 6) {
  7227. uint16_t postoff = 0;
  7228. if (intel_crtc->config->limited_color_range)
  7229. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7230. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7231. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7232. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7233. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7234. } else {
  7235. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7236. if (intel_crtc->config->limited_color_range)
  7237. mode |= CSC_BLACK_SCREEN_OFFSET;
  7238. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7239. }
  7240. }
  7241. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7242. {
  7243. struct drm_device *dev = crtc->dev;
  7244. struct drm_i915_private *dev_priv = dev->dev_private;
  7245. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7246. enum pipe pipe = intel_crtc->pipe;
  7247. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7248. uint32_t val;
  7249. val = 0;
  7250. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7251. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7252. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7253. val |= PIPECONF_INTERLACED_ILK;
  7254. else
  7255. val |= PIPECONF_PROGRESSIVE;
  7256. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7257. POSTING_READ(PIPECONF(cpu_transcoder));
  7258. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7259. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7260. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7261. val = 0;
  7262. switch (intel_crtc->config->pipe_bpp) {
  7263. case 18:
  7264. val |= PIPEMISC_DITHER_6_BPC;
  7265. break;
  7266. case 24:
  7267. val |= PIPEMISC_DITHER_8_BPC;
  7268. break;
  7269. case 30:
  7270. val |= PIPEMISC_DITHER_10_BPC;
  7271. break;
  7272. case 36:
  7273. val |= PIPEMISC_DITHER_12_BPC;
  7274. break;
  7275. default:
  7276. /* Case prevented by pipe_config_set_bpp. */
  7277. BUG();
  7278. }
  7279. if (intel_crtc->config->dither)
  7280. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7281. I915_WRITE(PIPEMISC(pipe), val);
  7282. }
  7283. }
  7284. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7285. struct intel_crtc_state *crtc_state,
  7286. intel_clock_t *clock,
  7287. bool *has_reduced_clock,
  7288. intel_clock_t *reduced_clock)
  7289. {
  7290. struct drm_device *dev = crtc->dev;
  7291. struct drm_i915_private *dev_priv = dev->dev_private;
  7292. int refclk;
  7293. const intel_limit_t *limit;
  7294. bool ret;
  7295. refclk = ironlake_get_refclk(crtc_state);
  7296. /*
  7297. * Returns a set of divisors for the desired target clock with the given
  7298. * refclk, or FALSE. The returned values represent the clock equation:
  7299. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7300. */
  7301. limit = intel_limit(crtc_state, refclk);
  7302. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7303. crtc_state->port_clock,
  7304. refclk, NULL, clock);
  7305. if (!ret)
  7306. return false;
  7307. return true;
  7308. }
  7309. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7310. {
  7311. /*
  7312. * Account for spread spectrum to avoid
  7313. * oversubscribing the link. Max center spread
  7314. * is 2.5%; use 5% for safety's sake.
  7315. */
  7316. u32 bps = target_clock * bpp * 21 / 20;
  7317. return DIV_ROUND_UP(bps, link_bw * 8);
  7318. }
  7319. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7320. {
  7321. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7322. }
  7323. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7324. struct intel_crtc_state *crtc_state,
  7325. u32 *fp,
  7326. intel_clock_t *reduced_clock, u32 *fp2)
  7327. {
  7328. struct drm_crtc *crtc = &intel_crtc->base;
  7329. struct drm_device *dev = crtc->dev;
  7330. struct drm_i915_private *dev_priv = dev->dev_private;
  7331. struct drm_atomic_state *state = crtc_state->base.state;
  7332. struct drm_connector *connector;
  7333. struct drm_connector_state *connector_state;
  7334. struct intel_encoder *encoder;
  7335. uint32_t dpll;
  7336. int factor, num_connectors = 0, i;
  7337. bool is_lvds = false, is_sdvo = false;
  7338. for_each_connector_in_state(state, connector, connector_state, i) {
  7339. if (connector_state->crtc != crtc_state->base.crtc)
  7340. continue;
  7341. encoder = to_intel_encoder(connector_state->best_encoder);
  7342. switch (encoder->type) {
  7343. case INTEL_OUTPUT_LVDS:
  7344. is_lvds = true;
  7345. break;
  7346. case INTEL_OUTPUT_SDVO:
  7347. case INTEL_OUTPUT_HDMI:
  7348. is_sdvo = true;
  7349. break;
  7350. default:
  7351. break;
  7352. }
  7353. num_connectors++;
  7354. }
  7355. /* Enable autotuning of the PLL clock (if permissible) */
  7356. factor = 21;
  7357. if (is_lvds) {
  7358. if ((intel_panel_use_ssc(dev_priv) &&
  7359. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7360. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7361. factor = 25;
  7362. } else if (crtc_state->sdvo_tv_clock)
  7363. factor = 20;
  7364. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7365. *fp |= FP_CB_TUNE;
  7366. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7367. *fp2 |= FP_CB_TUNE;
  7368. dpll = 0;
  7369. if (is_lvds)
  7370. dpll |= DPLLB_MODE_LVDS;
  7371. else
  7372. dpll |= DPLLB_MODE_DAC_SERIAL;
  7373. dpll |= (crtc_state->pixel_multiplier - 1)
  7374. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7375. if (is_sdvo)
  7376. dpll |= DPLL_SDVO_HIGH_SPEED;
  7377. if (crtc_state->has_dp_encoder)
  7378. dpll |= DPLL_SDVO_HIGH_SPEED;
  7379. /* compute bitmask from p1 value */
  7380. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7381. /* also FPA1 */
  7382. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7383. switch (crtc_state->dpll.p2) {
  7384. case 5:
  7385. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7386. break;
  7387. case 7:
  7388. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7389. break;
  7390. case 10:
  7391. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7392. break;
  7393. case 14:
  7394. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7395. break;
  7396. }
  7397. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7398. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7399. else
  7400. dpll |= PLL_REF_INPUT_DREFCLK;
  7401. return dpll | DPLL_VCO_ENABLE;
  7402. }
  7403. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7404. struct intel_crtc_state *crtc_state)
  7405. {
  7406. struct drm_device *dev = crtc->base.dev;
  7407. intel_clock_t clock, reduced_clock;
  7408. u32 dpll = 0, fp = 0, fp2 = 0;
  7409. bool ok, has_reduced_clock = false;
  7410. bool is_lvds = false;
  7411. struct intel_shared_dpll *pll;
  7412. memset(&crtc_state->dpll_hw_state, 0,
  7413. sizeof(crtc_state->dpll_hw_state));
  7414. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7415. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7416. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7417. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7418. &has_reduced_clock, &reduced_clock);
  7419. if (!ok && !crtc_state->clock_set) {
  7420. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7421. return -EINVAL;
  7422. }
  7423. /* Compat-code for transition, will disappear. */
  7424. if (!crtc_state->clock_set) {
  7425. crtc_state->dpll.n = clock.n;
  7426. crtc_state->dpll.m1 = clock.m1;
  7427. crtc_state->dpll.m2 = clock.m2;
  7428. crtc_state->dpll.p1 = clock.p1;
  7429. crtc_state->dpll.p2 = clock.p2;
  7430. }
  7431. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7432. if (crtc_state->has_pch_encoder) {
  7433. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7434. if (has_reduced_clock)
  7435. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7436. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7437. &fp, &reduced_clock,
  7438. has_reduced_clock ? &fp2 : NULL);
  7439. crtc_state->dpll_hw_state.dpll = dpll;
  7440. crtc_state->dpll_hw_state.fp0 = fp;
  7441. if (has_reduced_clock)
  7442. crtc_state->dpll_hw_state.fp1 = fp2;
  7443. else
  7444. crtc_state->dpll_hw_state.fp1 = fp;
  7445. pll = intel_get_shared_dpll(crtc, crtc_state);
  7446. if (pll == NULL) {
  7447. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7448. pipe_name(crtc->pipe));
  7449. return -EINVAL;
  7450. }
  7451. }
  7452. if (is_lvds && has_reduced_clock)
  7453. crtc->lowfreq_avail = true;
  7454. else
  7455. crtc->lowfreq_avail = false;
  7456. return 0;
  7457. }
  7458. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7459. struct intel_link_m_n *m_n)
  7460. {
  7461. struct drm_device *dev = crtc->base.dev;
  7462. struct drm_i915_private *dev_priv = dev->dev_private;
  7463. enum pipe pipe = crtc->pipe;
  7464. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7465. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7466. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7467. & ~TU_SIZE_MASK;
  7468. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7469. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7470. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7471. }
  7472. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7473. enum transcoder transcoder,
  7474. struct intel_link_m_n *m_n,
  7475. struct intel_link_m_n *m2_n2)
  7476. {
  7477. struct drm_device *dev = crtc->base.dev;
  7478. struct drm_i915_private *dev_priv = dev->dev_private;
  7479. enum pipe pipe = crtc->pipe;
  7480. if (INTEL_INFO(dev)->gen >= 5) {
  7481. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7482. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7483. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7484. & ~TU_SIZE_MASK;
  7485. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7486. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7487. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7488. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7489. * gen < 8) and if DRRS is supported (to make sure the
  7490. * registers are not unnecessarily read).
  7491. */
  7492. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7493. crtc->config->has_drrs) {
  7494. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7495. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7496. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7497. & ~TU_SIZE_MASK;
  7498. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7499. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7500. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7501. }
  7502. } else {
  7503. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7504. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7505. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7506. & ~TU_SIZE_MASK;
  7507. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7508. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7509. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7510. }
  7511. }
  7512. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7513. struct intel_crtc_state *pipe_config)
  7514. {
  7515. if (pipe_config->has_pch_encoder)
  7516. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7517. else
  7518. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7519. &pipe_config->dp_m_n,
  7520. &pipe_config->dp_m2_n2);
  7521. }
  7522. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7523. struct intel_crtc_state *pipe_config)
  7524. {
  7525. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7526. &pipe_config->fdi_m_n, NULL);
  7527. }
  7528. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7529. struct intel_crtc_state *pipe_config)
  7530. {
  7531. struct drm_device *dev = crtc->base.dev;
  7532. struct drm_i915_private *dev_priv = dev->dev_private;
  7533. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7534. uint32_t ps_ctrl = 0;
  7535. int id = -1;
  7536. int i;
  7537. /* find scaler attached to this pipe */
  7538. for (i = 0; i < crtc->num_scalers; i++) {
  7539. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7540. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7541. id = i;
  7542. pipe_config->pch_pfit.enabled = true;
  7543. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7544. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7545. break;
  7546. }
  7547. }
  7548. scaler_state->scaler_id = id;
  7549. if (id >= 0) {
  7550. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7551. } else {
  7552. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7553. }
  7554. }
  7555. static void
  7556. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7557. struct intel_initial_plane_config *plane_config)
  7558. {
  7559. struct drm_device *dev = crtc->base.dev;
  7560. struct drm_i915_private *dev_priv = dev->dev_private;
  7561. u32 val, base, offset, stride_mult, tiling;
  7562. int pipe = crtc->pipe;
  7563. int fourcc, pixel_format;
  7564. unsigned int aligned_height;
  7565. struct drm_framebuffer *fb;
  7566. struct intel_framebuffer *intel_fb;
  7567. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7568. if (!intel_fb) {
  7569. DRM_DEBUG_KMS("failed to alloc fb\n");
  7570. return;
  7571. }
  7572. fb = &intel_fb->base;
  7573. val = I915_READ(PLANE_CTL(pipe, 0));
  7574. if (!(val & PLANE_CTL_ENABLE))
  7575. goto error;
  7576. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7577. fourcc = skl_format_to_fourcc(pixel_format,
  7578. val & PLANE_CTL_ORDER_RGBX,
  7579. val & PLANE_CTL_ALPHA_MASK);
  7580. fb->pixel_format = fourcc;
  7581. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7582. tiling = val & PLANE_CTL_TILED_MASK;
  7583. switch (tiling) {
  7584. case PLANE_CTL_TILED_LINEAR:
  7585. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7586. break;
  7587. case PLANE_CTL_TILED_X:
  7588. plane_config->tiling = I915_TILING_X;
  7589. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7590. break;
  7591. case PLANE_CTL_TILED_Y:
  7592. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7593. break;
  7594. case PLANE_CTL_TILED_YF:
  7595. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7596. break;
  7597. default:
  7598. MISSING_CASE(tiling);
  7599. goto error;
  7600. }
  7601. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7602. plane_config->base = base;
  7603. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7604. val = I915_READ(PLANE_SIZE(pipe, 0));
  7605. fb->height = ((val >> 16) & 0xfff) + 1;
  7606. fb->width = ((val >> 0) & 0x1fff) + 1;
  7607. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7608. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7609. fb->pixel_format);
  7610. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7611. aligned_height = intel_fb_align_height(dev, fb->height,
  7612. fb->pixel_format,
  7613. fb->modifier[0]);
  7614. plane_config->size = fb->pitches[0] * aligned_height;
  7615. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7616. pipe_name(pipe), fb->width, fb->height,
  7617. fb->bits_per_pixel, base, fb->pitches[0],
  7618. plane_config->size);
  7619. plane_config->fb = intel_fb;
  7620. return;
  7621. error:
  7622. kfree(fb);
  7623. }
  7624. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7625. struct intel_crtc_state *pipe_config)
  7626. {
  7627. struct drm_device *dev = crtc->base.dev;
  7628. struct drm_i915_private *dev_priv = dev->dev_private;
  7629. uint32_t tmp;
  7630. tmp = I915_READ(PF_CTL(crtc->pipe));
  7631. if (tmp & PF_ENABLE) {
  7632. pipe_config->pch_pfit.enabled = true;
  7633. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7634. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7635. /* We currently do not free assignements of panel fitters on
  7636. * ivb/hsw (since we don't use the higher upscaling modes which
  7637. * differentiates them) so just WARN about this case for now. */
  7638. if (IS_GEN7(dev)) {
  7639. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7640. PF_PIPE_SEL_IVB(crtc->pipe));
  7641. }
  7642. }
  7643. }
  7644. static void
  7645. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7646. struct intel_initial_plane_config *plane_config)
  7647. {
  7648. struct drm_device *dev = crtc->base.dev;
  7649. struct drm_i915_private *dev_priv = dev->dev_private;
  7650. u32 val, base, offset;
  7651. int pipe = crtc->pipe;
  7652. int fourcc, pixel_format;
  7653. unsigned int aligned_height;
  7654. struct drm_framebuffer *fb;
  7655. struct intel_framebuffer *intel_fb;
  7656. val = I915_READ(DSPCNTR(pipe));
  7657. if (!(val & DISPLAY_PLANE_ENABLE))
  7658. return;
  7659. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7660. if (!intel_fb) {
  7661. DRM_DEBUG_KMS("failed to alloc fb\n");
  7662. return;
  7663. }
  7664. fb = &intel_fb->base;
  7665. if (INTEL_INFO(dev)->gen >= 4) {
  7666. if (val & DISPPLANE_TILED) {
  7667. plane_config->tiling = I915_TILING_X;
  7668. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7669. }
  7670. }
  7671. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7672. fourcc = i9xx_format_to_fourcc(pixel_format);
  7673. fb->pixel_format = fourcc;
  7674. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7675. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7676. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7677. offset = I915_READ(DSPOFFSET(pipe));
  7678. } else {
  7679. if (plane_config->tiling)
  7680. offset = I915_READ(DSPTILEOFF(pipe));
  7681. else
  7682. offset = I915_READ(DSPLINOFF(pipe));
  7683. }
  7684. plane_config->base = base;
  7685. val = I915_READ(PIPESRC(pipe));
  7686. fb->width = ((val >> 16) & 0xfff) + 1;
  7687. fb->height = ((val >> 0) & 0xfff) + 1;
  7688. val = I915_READ(DSPSTRIDE(pipe));
  7689. fb->pitches[0] = val & 0xffffffc0;
  7690. aligned_height = intel_fb_align_height(dev, fb->height,
  7691. fb->pixel_format,
  7692. fb->modifier[0]);
  7693. plane_config->size = fb->pitches[0] * aligned_height;
  7694. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7695. pipe_name(pipe), fb->width, fb->height,
  7696. fb->bits_per_pixel, base, fb->pitches[0],
  7697. plane_config->size);
  7698. plane_config->fb = intel_fb;
  7699. }
  7700. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7701. struct intel_crtc_state *pipe_config)
  7702. {
  7703. struct drm_device *dev = crtc->base.dev;
  7704. struct drm_i915_private *dev_priv = dev->dev_private;
  7705. uint32_t tmp;
  7706. if (!intel_display_power_is_enabled(dev_priv,
  7707. POWER_DOMAIN_PIPE(crtc->pipe)))
  7708. return false;
  7709. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7710. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7711. tmp = I915_READ(PIPECONF(crtc->pipe));
  7712. if (!(tmp & PIPECONF_ENABLE))
  7713. return false;
  7714. switch (tmp & PIPECONF_BPC_MASK) {
  7715. case PIPECONF_6BPC:
  7716. pipe_config->pipe_bpp = 18;
  7717. break;
  7718. case PIPECONF_8BPC:
  7719. pipe_config->pipe_bpp = 24;
  7720. break;
  7721. case PIPECONF_10BPC:
  7722. pipe_config->pipe_bpp = 30;
  7723. break;
  7724. case PIPECONF_12BPC:
  7725. pipe_config->pipe_bpp = 36;
  7726. break;
  7727. default:
  7728. break;
  7729. }
  7730. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7731. pipe_config->limited_color_range = true;
  7732. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7733. struct intel_shared_dpll *pll;
  7734. pipe_config->has_pch_encoder = true;
  7735. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7736. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7737. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7738. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7739. if (HAS_PCH_IBX(dev_priv->dev)) {
  7740. pipe_config->shared_dpll =
  7741. (enum intel_dpll_id) crtc->pipe;
  7742. } else {
  7743. tmp = I915_READ(PCH_DPLL_SEL);
  7744. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7745. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7746. else
  7747. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7748. }
  7749. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7750. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7751. &pipe_config->dpll_hw_state));
  7752. tmp = pipe_config->dpll_hw_state.dpll;
  7753. pipe_config->pixel_multiplier =
  7754. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7755. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7756. ironlake_pch_clock_get(crtc, pipe_config);
  7757. } else {
  7758. pipe_config->pixel_multiplier = 1;
  7759. }
  7760. intel_get_pipe_timings(crtc, pipe_config);
  7761. ironlake_get_pfit_config(crtc, pipe_config);
  7762. return true;
  7763. }
  7764. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7765. {
  7766. struct drm_device *dev = dev_priv->dev;
  7767. struct intel_crtc *crtc;
  7768. for_each_intel_crtc(dev, crtc)
  7769. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7770. pipe_name(crtc->pipe));
  7771. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7772. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7773. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7774. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7775. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7776. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7777. "CPU PWM1 enabled\n");
  7778. if (IS_HASWELL(dev))
  7779. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7780. "CPU PWM2 enabled\n");
  7781. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7782. "PCH PWM1 enabled\n");
  7783. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7784. "Utility pin enabled\n");
  7785. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7786. /*
  7787. * In theory we can still leave IRQs enabled, as long as only the HPD
  7788. * interrupts remain enabled. We used to check for that, but since it's
  7789. * gen-specific and since we only disable LCPLL after we fully disable
  7790. * the interrupts, the check below should be enough.
  7791. */
  7792. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7793. }
  7794. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7795. {
  7796. struct drm_device *dev = dev_priv->dev;
  7797. if (IS_HASWELL(dev))
  7798. return I915_READ(D_COMP_HSW);
  7799. else
  7800. return I915_READ(D_COMP_BDW);
  7801. }
  7802. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7803. {
  7804. struct drm_device *dev = dev_priv->dev;
  7805. if (IS_HASWELL(dev)) {
  7806. mutex_lock(&dev_priv->rps.hw_lock);
  7807. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7808. val))
  7809. DRM_ERROR("Failed to write to D_COMP\n");
  7810. mutex_unlock(&dev_priv->rps.hw_lock);
  7811. } else {
  7812. I915_WRITE(D_COMP_BDW, val);
  7813. POSTING_READ(D_COMP_BDW);
  7814. }
  7815. }
  7816. /*
  7817. * This function implements pieces of two sequences from BSpec:
  7818. * - Sequence for display software to disable LCPLL
  7819. * - Sequence for display software to allow package C8+
  7820. * The steps implemented here are just the steps that actually touch the LCPLL
  7821. * register. Callers should take care of disabling all the display engine
  7822. * functions, doing the mode unset, fixing interrupts, etc.
  7823. */
  7824. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7825. bool switch_to_fclk, bool allow_power_down)
  7826. {
  7827. uint32_t val;
  7828. assert_can_disable_lcpll(dev_priv);
  7829. val = I915_READ(LCPLL_CTL);
  7830. if (switch_to_fclk) {
  7831. val |= LCPLL_CD_SOURCE_FCLK;
  7832. I915_WRITE(LCPLL_CTL, val);
  7833. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7834. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7835. DRM_ERROR("Switching to FCLK failed\n");
  7836. val = I915_READ(LCPLL_CTL);
  7837. }
  7838. val |= LCPLL_PLL_DISABLE;
  7839. I915_WRITE(LCPLL_CTL, val);
  7840. POSTING_READ(LCPLL_CTL);
  7841. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7842. DRM_ERROR("LCPLL still locked\n");
  7843. val = hsw_read_dcomp(dev_priv);
  7844. val |= D_COMP_COMP_DISABLE;
  7845. hsw_write_dcomp(dev_priv, val);
  7846. ndelay(100);
  7847. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7848. 1))
  7849. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7850. if (allow_power_down) {
  7851. val = I915_READ(LCPLL_CTL);
  7852. val |= LCPLL_POWER_DOWN_ALLOW;
  7853. I915_WRITE(LCPLL_CTL, val);
  7854. POSTING_READ(LCPLL_CTL);
  7855. }
  7856. }
  7857. /*
  7858. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7859. * source.
  7860. */
  7861. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7862. {
  7863. uint32_t val;
  7864. val = I915_READ(LCPLL_CTL);
  7865. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7866. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7867. return;
  7868. /*
  7869. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7870. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7871. */
  7872. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7873. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7874. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7875. I915_WRITE(LCPLL_CTL, val);
  7876. POSTING_READ(LCPLL_CTL);
  7877. }
  7878. val = hsw_read_dcomp(dev_priv);
  7879. val |= D_COMP_COMP_FORCE;
  7880. val &= ~D_COMP_COMP_DISABLE;
  7881. hsw_write_dcomp(dev_priv, val);
  7882. val = I915_READ(LCPLL_CTL);
  7883. val &= ~LCPLL_PLL_DISABLE;
  7884. I915_WRITE(LCPLL_CTL, val);
  7885. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7886. DRM_ERROR("LCPLL not locked yet\n");
  7887. if (val & LCPLL_CD_SOURCE_FCLK) {
  7888. val = I915_READ(LCPLL_CTL);
  7889. val &= ~LCPLL_CD_SOURCE_FCLK;
  7890. I915_WRITE(LCPLL_CTL, val);
  7891. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7892. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7893. DRM_ERROR("Switching back to LCPLL failed\n");
  7894. }
  7895. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7896. intel_update_cdclk(dev_priv->dev);
  7897. }
  7898. /*
  7899. * Package states C8 and deeper are really deep PC states that can only be
  7900. * reached when all the devices on the system allow it, so even if the graphics
  7901. * device allows PC8+, it doesn't mean the system will actually get to these
  7902. * states. Our driver only allows PC8+ when going into runtime PM.
  7903. *
  7904. * The requirements for PC8+ are that all the outputs are disabled, the power
  7905. * well is disabled and most interrupts are disabled, and these are also
  7906. * requirements for runtime PM. When these conditions are met, we manually do
  7907. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7908. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7909. * hang the machine.
  7910. *
  7911. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7912. * the state of some registers, so when we come back from PC8+ we need to
  7913. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7914. * need to take care of the registers kept by RC6. Notice that this happens even
  7915. * if we don't put the device in PCI D3 state (which is what currently happens
  7916. * because of the runtime PM support).
  7917. *
  7918. * For more, read "Display Sequences for Package C8" on the hardware
  7919. * documentation.
  7920. */
  7921. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7922. {
  7923. struct drm_device *dev = dev_priv->dev;
  7924. uint32_t val;
  7925. DRM_DEBUG_KMS("Enabling package C8+\n");
  7926. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7927. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7928. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7929. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7930. }
  7931. lpt_disable_clkout_dp(dev);
  7932. hsw_disable_lcpll(dev_priv, true, true);
  7933. }
  7934. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7935. {
  7936. struct drm_device *dev = dev_priv->dev;
  7937. uint32_t val;
  7938. DRM_DEBUG_KMS("Disabling package C8+\n");
  7939. hsw_restore_lcpll(dev_priv);
  7940. lpt_init_pch_refclk(dev);
  7941. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7942. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7943. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7944. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7945. }
  7946. intel_prepare_ddi(dev);
  7947. }
  7948. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7949. {
  7950. struct drm_device *dev = old_state->dev;
  7951. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7952. broxton_set_cdclk(dev, req_cdclk);
  7953. }
  7954. /* compute the max rate for new configuration */
  7955. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7956. {
  7957. struct intel_crtc *intel_crtc;
  7958. struct intel_crtc_state *crtc_state;
  7959. int max_pixel_rate = 0;
  7960. for_each_intel_crtc(state->dev, intel_crtc) {
  7961. int pixel_rate;
  7962. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7963. if (IS_ERR(crtc_state))
  7964. return PTR_ERR(crtc_state);
  7965. if (!crtc_state->base.enable)
  7966. continue;
  7967. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7968. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7969. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7970. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7971. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7972. }
  7973. return max_pixel_rate;
  7974. }
  7975. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7976. {
  7977. struct drm_i915_private *dev_priv = dev->dev_private;
  7978. uint32_t val, data;
  7979. int ret;
  7980. if (WARN((I915_READ(LCPLL_CTL) &
  7981. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7982. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  7983. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  7984. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  7985. "trying to change cdclk frequency with cdclk not enabled\n"))
  7986. return;
  7987. mutex_lock(&dev_priv->rps.hw_lock);
  7988. ret = sandybridge_pcode_write(dev_priv,
  7989. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  7990. mutex_unlock(&dev_priv->rps.hw_lock);
  7991. if (ret) {
  7992. DRM_ERROR("failed to inform pcode about cdclk change\n");
  7993. return;
  7994. }
  7995. val = I915_READ(LCPLL_CTL);
  7996. val |= LCPLL_CD_SOURCE_FCLK;
  7997. I915_WRITE(LCPLL_CTL, val);
  7998. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7999. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8000. DRM_ERROR("Switching to FCLK failed\n");
  8001. val = I915_READ(LCPLL_CTL);
  8002. val &= ~LCPLL_CLK_FREQ_MASK;
  8003. switch (cdclk) {
  8004. case 450000:
  8005. val |= LCPLL_CLK_FREQ_450;
  8006. data = 0;
  8007. break;
  8008. case 540000:
  8009. val |= LCPLL_CLK_FREQ_54O_BDW;
  8010. data = 1;
  8011. break;
  8012. case 337500:
  8013. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8014. data = 2;
  8015. break;
  8016. case 675000:
  8017. val |= LCPLL_CLK_FREQ_675_BDW;
  8018. data = 3;
  8019. break;
  8020. default:
  8021. WARN(1, "invalid cdclk frequency\n");
  8022. return;
  8023. }
  8024. I915_WRITE(LCPLL_CTL, val);
  8025. val = I915_READ(LCPLL_CTL);
  8026. val &= ~LCPLL_CD_SOURCE_FCLK;
  8027. I915_WRITE(LCPLL_CTL, val);
  8028. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8029. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8030. DRM_ERROR("Switching back to LCPLL failed\n");
  8031. mutex_lock(&dev_priv->rps.hw_lock);
  8032. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8033. mutex_unlock(&dev_priv->rps.hw_lock);
  8034. intel_update_cdclk(dev);
  8035. WARN(cdclk != dev_priv->cdclk_freq,
  8036. "cdclk requested %d kHz but got %d kHz\n",
  8037. cdclk, dev_priv->cdclk_freq);
  8038. }
  8039. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8040. {
  8041. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8042. int max_pixclk = ilk_max_pixel_rate(state);
  8043. int cdclk;
  8044. /*
  8045. * FIXME should also account for plane ratio
  8046. * once 64bpp pixel formats are supported.
  8047. */
  8048. if (max_pixclk > 540000)
  8049. cdclk = 675000;
  8050. else if (max_pixclk > 450000)
  8051. cdclk = 540000;
  8052. else if (max_pixclk > 337500)
  8053. cdclk = 450000;
  8054. else
  8055. cdclk = 337500;
  8056. /*
  8057. * FIXME move the cdclk caclulation to
  8058. * compute_config() so we can fail gracegully.
  8059. */
  8060. if (cdclk > dev_priv->max_cdclk_freq) {
  8061. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8062. cdclk, dev_priv->max_cdclk_freq);
  8063. cdclk = dev_priv->max_cdclk_freq;
  8064. }
  8065. to_intel_atomic_state(state)->cdclk = cdclk;
  8066. return 0;
  8067. }
  8068. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8069. {
  8070. struct drm_device *dev = old_state->dev;
  8071. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8072. broadwell_set_cdclk(dev, req_cdclk);
  8073. }
  8074. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8075. struct intel_crtc_state *crtc_state)
  8076. {
  8077. if (!intel_ddi_pll_select(crtc, crtc_state))
  8078. return -EINVAL;
  8079. crtc->lowfreq_avail = false;
  8080. return 0;
  8081. }
  8082. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8083. enum port port,
  8084. struct intel_crtc_state *pipe_config)
  8085. {
  8086. switch (port) {
  8087. case PORT_A:
  8088. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8089. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8090. break;
  8091. case PORT_B:
  8092. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8093. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8094. break;
  8095. case PORT_C:
  8096. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8097. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8098. break;
  8099. default:
  8100. DRM_ERROR("Incorrect port type\n");
  8101. }
  8102. }
  8103. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8104. enum port port,
  8105. struct intel_crtc_state *pipe_config)
  8106. {
  8107. u32 temp, dpll_ctl1;
  8108. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8109. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8110. switch (pipe_config->ddi_pll_sel) {
  8111. case SKL_DPLL0:
  8112. /*
  8113. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8114. * of the shared DPLL framework and thus needs to be read out
  8115. * separately
  8116. */
  8117. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8118. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8119. break;
  8120. case SKL_DPLL1:
  8121. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8122. break;
  8123. case SKL_DPLL2:
  8124. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8125. break;
  8126. case SKL_DPLL3:
  8127. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8128. break;
  8129. }
  8130. }
  8131. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8132. enum port port,
  8133. struct intel_crtc_state *pipe_config)
  8134. {
  8135. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8136. switch (pipe_config->ddi_pll_sel) {
  8137. case PORT_CLK_SEL_WRPLL1:
  8138. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8139. break;
  8140. case PORT_CLK_SEL_WRPLL2:
  8141. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8142. break;
  8143. }
  8144. }
  8145. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8146. struct intel_crtc_state *pipe_config)
  8147. {
  8148. struct drm_device *dev = crtc->base.dev;
  8149. struct drm_i915_private *dev_priv = dev->dev_private;
  8150. struct intel_shared_dpll *pll;
  8151. enum port port;
  8152. uint32_t tmp;
  8153. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8154. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8155. if (IS_SKYLAKE(dev))
  8156. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8157. else if (IS_BROXTON(dev))
  8158. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8159. else
  8160. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8161. if (pipe_config->shared_dpll >= 0) {
  8162. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8163. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8164. &pipe_config->dpll_hw_state));
  8165. }
  8166. /*
  8167. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8168. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8169. * the PCH transcoder is on.
  8170. */
  8171. if (INTEL_INFO(dev)->gen < 9 &&
  8172. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8173. pipe_config->has_pch_encoder = true;
  8174. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8175. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8176. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8177. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8178. }
  8179. }
  8180. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8181. struct intel_crtc_state *pipe_config)
  8182. {
  8183. struct drm_device *dev = crtc->base.dev;
  8184. struct drm_i915_private *dev_priv = dev->dev_private;
  8185. enum intel_display_power_domain pfit_domain;
  8186. uint32_t tmp;
  8187. if (!intel_display_power_is_enabled(dev_priv,
  8188. POWER_DOMAIN_PIPE(crtc->pipe)))
  8189. return false;
  8190. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8191. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8192. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8193. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8194. enum pipe trans_edp_pipe;
  8195. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8196. default:
  8197. WARN(1, "unknown pipe linked to edp transcoder\n");
  8198. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8199. case TRANS_DDI_EDP_INPUT_A_ON:
  8200. trans_edp_pipe = PIPE_A;
  8201. break;
  8202. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8203. trans_edp_pipe = PIPE_B;
  8204. break;
  8205. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8206. trans_edp_pipe = PIPE_C;
  8207. break;
  8208. }
  8209. if (trans_edp_pipe == crtc->pipe)
  8210. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8211. }
  8212. if (!intel_display_power_is_enabled(dev_priv,
  8213. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8214. return false;
  8215. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8216. if (!(tmp & PIPECONF_ENABLE))
  8217. return false;
  8218. haswell_get_ddi_port_state(crtc, pipe_config);
  8219. intel_get_pipe_timings(crtc, pipe_config);
  8220. if (INTEL_INFO(dev)->gen >= 9) {
  8221. skl_init_scalers(dev, crtc, pipe_config);
  8222. }
  8223. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8224. if (INTEL_INFO(dev)->gen >= 9) {
  8225. pipe_config->scaler_state.scaler_id = -1;
  8226. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8227. }
  8228. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8229. if (INTEL_INFO(dev)->gen == 9)
  8230. skylake_get_pfit_config(crtc, pipe_config);
  8231. else if (INTEL_INFO(dev)->gen < 9)
  8232. ironlake_get_pfit_config(crtc, pipe_config);
  8233. else
  8234. MISSING_CASE(INTEL_INFO(dev)->gen);
  8235. }
  8236. if (IS_HASWELL(dev))
  8237. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8238. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8239. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8240. pipe_config->pixel_multiplier =
  8241. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8242. } else {
  8243. pipe_config->pixel_multiplier = 1;
  8244. }
  8245. return true;
  8246. }
  8247. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8248. {
  8249. struct drm_device *dev = crtc->dev;
  8250. struct drm_i915_private *dev_priv = dev->dev_private;
  8251. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8252. uint32_t cntl = 0, size = 0;
  8253. if (base) {
  8254. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8255. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8256. unsigned int stride = roundup_pow_of_two(width) * 4;
  8257. switch (stride) {
  8258. default:
  8259. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8260. width, stride);
  8261. stride = 256;
  8262. /* fallthrough */
  8263. case 256:
  8264. case 512:
  8265. case 1024:
  8266. case 2048:
  8267. break;
  8268. }
  8269. cntl |= CURSOR_ENABLE |
  8270. CURSOR_GAMMA_ENABLE |
  8271. CURSOR_FORMAT_ARGB |
  8272. CURSOR_STRIDE(stride);
  8273. size = (height << 12) | width;
  8274. }
  8275. if (intel_crtc->cursor_cntl != 0 &&
  8276. (intel_crtc->cursor_base != base ||
  8277. intel_crtc->cursor_size != size ||
  8278. intel_crtc->cursor_cntl != cntl)) {
  8279. /* On these chipsets we can only modify the base/size/stride
  8280. * whilst the cursor is disabled.
  8281. */
  8282. I915_WRITE(_CURACNTR, 0);
  8283. POSTING_READ(_CURACNTR);
  8284. intel_crtc->cursor_cntl = 0;
  8285. }
  8286. if (intel_crtc->cursor_base != base) {
  8287. I915_WRITE(_CURABASE, base);
  8288. intel_crtc->cursor_base = base;
  8289. }
  8290. if (intel_crtc->cursor_size != size) {
  8291. I915_WRITE(CURSIZE, size);
  8292. intel_crtc->cursor_size = size;
  8293. }
  8294. if (intel_crtc->cursor_cntl != cntl) {
  8295. I915_WRITE(_CURACNTR, cntl);
  8296. POSTING_READ(_CURACNTR);
  8297. intel_crtc->cursor_cntl = cntl;
  8298. }
  8299. }
  8300. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8301. {
  8302. struct drm_device *dev = crtc->dev;
  8303. struct drm_i915_private *dev_priv = dev->dev_private;
  8304. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8305. int pipe = intel_crtc->pipe;
  8306. uint32_t cntl;
  8307. cntl = 0;
  8308. if (base) {
  8309. cntl = MCURSOR_GAMMA_ENABLE;
  8310. switch (intel_crtc->base.cursor->state->crtc_w) {
  8311. case 64:
  8312. cntl |= CURSOR_MODE_64_ARGB_AX;
  8313. break;
  8314. case 128:
  8315. cntl |= CURSOR_MODE_128_ARGB_AX;
  8316. break;
  8317. case 256:
  8318. cntl |= CURSOR_MODE_256_ARGB_AX;
  8319. break;
  8320. default:
  8321. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8322. return;
  8323. }
  8324. cntl |= pipe << 28; /* Connect to correct pipe */
  8325. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8326. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8327. }
  8328. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8329. cntl |= CURSOR_ROTATE_180;
  8330. if (intel_crtc->cursor_cntl != cntl) {
  8331. I915_WRITE(CURCNTR(pipe), cntl);
  8332. POSTING_READ(CURCNTR(pipe));
  8333. intel_crtc->cursor_cntl = cntl;
  8334. }
  8335. /* and commit changes on next vblank */
  8336. I915_WRITE(CURBASE(pipe), base);
  8337. POSTING_READ(CURBASE(pipe));
  8338. intel_crtc->cursor_base = base;
  8339. }
  8340. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8341. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8342. bool on)
  8343. {
  8344. struct drm_device *dev = crtc->dev;
  8345. struct drm_i915_private *dev_priv = dev->dev_private;
  8346. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8347. int pipe = intel_crtc->pipe;
  8348. int x = crtc->cursor_x;
  8349. int y = crtc->cursor_y;
  8350. u32 base = 0, pos = 0;
  8351. if (on)
  8352. base = intel_crtc->cursor_addr;
  8353. if (x >= intel_crtc->config->pipe_src_w)
  8354. base = 0;
  8355. if (y >= intel_crtc->config->pipe_src_h)
  8356. base = 0;
  8357. if (x < 0) {
  8358. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8359. base = 0;
  8360. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8361. x = -x;
  8362. }
  8363. pos |= x << CURSOR_X_SHIFT;
  8364. if (y < 0) {
  8365. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8366. base = 0;
  8367. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8368. y = -y;
  8369. }
  8370. pos |= y << CURSOR_Y_SHIFT;
  8371. if (base == 0 && intel_crtc->cursor_base == 0)
  8372. return;
  8373. I915_WRITE(CURPOS(pipe), pos);
  8374. /* ILK+ do this automagically */
  8375. if (HAS_GMCH_DISPLAY(dev) &&
  8376. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8377. base += (intel_crtc->base.cursor->state->crtc_h *
  8378. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8379. }
  8380. if (IS_845G(dev) || IS_I865G(dev))
  8381. i845_update_cursor(crtc, base);
  8382. else
  8383. i9xx_update_cursor(crtc, base);
  8384. }
  8385. static bool cursor_size_ok(struct drm_device *dev,
  8386. uint32_t width, uint32_t height)
  8387. {
  8388. if (width == 0 || height == 0)
  8389. return false;
  8390. /*
  8391. * 845g/865g are special in that they are only limited by
  8392. * the width of their cursors, the height is arbitrary up to
  8393. * the precision of the register. Everything else requires
  8394. * square cursors, limited to a few power-of-two sizes.
  8395. */
  8396. if (IS_845G(dev) || IS_I865G(dev)) {
  8397. if ((width & 63) != 0)
  8398. return false;
  8399. if (width > (IS_845G(dev) ? 64 : 512))
  8400. return false;
  8401. if (height > 1023)
  8402. return false;
  8403. } else {
  8404. switch (width | height) {
  8405. case 256:
  8406. case 128:
  8407. if (IS_GEN2(dev))
  8408. return false;
  8409. case 64:
  8410. break;
  8411. default:
  8412. return false;
  8413. }
  8414. }
  8415. return true;
  8416. }
  8417. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8418. u16 *blue, uint32_t start, uint32_t size)
  8419. {
  8420. int end = (start + size > 256) ? 256 : start + size, i;
  8421. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8422. for (i = start; i < end; i++) {
  8423. intel_crtc->lut_r[i] = red[i] >> 8;
  8424. intel_crtc->lut_g[i] = green[i] >> 8;
  8425. intel_crtc->lut_b[i] = blue[i] >> 8;
  8426. }
  8427. intel_crtc_load_lut(crtc);
  8428. }
  8429. /* VESA 640x480x72Hz mode to set on the pipe */
  8430. static struct drm_display_mode load_detect_mode = {
  8431. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8432. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8433. };
  8434. struct drm_framebuffer *
  8435. __intel_framebuffer_create(struct drm_device *dev,
  8436. struct drm_mode_fb_cmd2 *mode_cmd,
  8437. struct drm_i915_gem_object *obj)
  8438. {
  8439. struct intel_framebuffer *intel_fb;
  8440. int ret;
  8441. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8442. if (!intel_fb) {
  8443. drm_gem_object_unreference(&obj->base);
  8444. return ERR_PTR(-ENOMEM);
  8445. }
  8446. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8447. if (ret)
  8448. goto err;
  8449. return &intel_fb->base;
  8450. err:
  8451. drm_gem_object_unreference(&obj->base);
  8452. kfree(intel_fb);
  8453. return ERR_PTR(ret);
  8454. }
  8455. static struct drm_framebuffer *
  8456. intel_framebuffer_create(struct drm_device *dev,
  8457. struct drm_mode_fb_cmd2 *mode_cmd,
  8458. struct drm_i915_gem_object *obj)
  8459. {
  8460. struct drm_framebuffer *fb;
  8461. int ret;
  8462. ret = i915_mutex_lock_interruptible(dev);
  8463. if (ret)
  8464. return ERR_PTR(ret);
  8465. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8466. mutex_unlock(&dev->struct_mutex);
  8467. return fb;
  8468. }
  8469. static u32
  8470. intel_framebuffer_pitch_for_width(int width, int bpp)
  8471. {
  8472. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8473. return ALIGN(pitch, 64);
  8474. }
  8475. static u32
  8476. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8477. {
  8478. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8479. return PAGE_ALIGN(pitch * mode->vdisplay);
  8480. }
  8481. static struct drm_framebuffer *
  8482. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8483. struct drm_display_mode *mode,
  8484. int depth, int bpp)
  8485. {
  8486. struct drm_i915_gem_object *obj;
  8487. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8488. obj = i915_gem_alloc_object(dev,
  8489. intel_framebuffer_size_for_mode(mode, bpp));
  8490. if (obj == NULL)
  8491. return ERR_PTR(-ENOMEM);
  8492. mode_cmd.width = mode->hdisplay;
  8493. mode_cmd.height = mode->vdisplay;
  8494. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8495. bpp);
  8496. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8497. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8498. }
  8499. static struct drm_framebuffer *
  8500. mode_fits_in_fbdev(struct drm_device *dev,
  8501. struct drm_display_mode *mode)
  8502. {
  8503. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8504. struct drm_i915_private *dev_priv = dev->dev_private;
  8505. struct drm_i915_gem_object *obj;
  8506. struct drm_framebuffer *fb;
  8507. if (!dev_priv->fbdev)
  8508. return NULL;
  8509. if (!dev_priv->fbdev->fb)
  8510. return NULL;
  8511. obj = dev_priv->fbdev->fb->obj;
  8512. BUG_ON(!obj);
  8513. fb = &dev_priv->fbdev->fb->base;
  8514. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8515. fb->bits_per_pixel))
  8516. return NULL;
  8517. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8518. return NULL;
  8519. return fb;
  8520. #else
  8521. return NULL;
  8522. #endif
  8523. }
  8524. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8525. struct drm_crtc *crtc,
  8526. struct drm_display_mode *mode,
  8527. struct drm_framebuffer *fb,
  8528. int x, int y)
  8529. {
  8530. struct drm_plane_state *plane_state;
  8531. int hdisplay, vdisplay;
  8532. int ret;
  8533. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8534. if (IS_ERR(plane_state))
  8535. return PTR_ERR(plane_state);
  8536. if (mode)
  8537. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8538. else
  8539. hdisplay = vdisplay = 0;
  8540. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8541. if (ret)
  8542. return ret;
  8543. drm_atomic_set_fb_for_plane(plane_state, fb);
  8544. plane_state->crtc_x = 0;
  8545. plane_state->crtc_y = 0;
  8546. plane_state->crtc_w = hdisplay;
  8547. plane_state->crtc_h = vdisplay;
  8548. plane_state->src_x = x << 16;
  8549. plane_state->src_y = y << 16;
  8550. plane_state->src_w = hdisplay << 16;
  8551. plane_state->src_h = vdisplay << 16;
  8552. return 0;
  8553. }
  8554. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8555. struct drm_display_mode *mode,
  8556. struct intel_load_detect_pipe *old,
  8557. struct drm_modeset_acquire_ctx *ctx)
  8558. {
  8559. struct intel_crtc *intel_crtc;
  8560. struct intel_encoder *intel_encoder =
  8561. intel_attached_encoder(connector);
  8562. struct drm_crtc *possible_crtc;
  8563. struct drm_encoder *encoder = &intel_encoder->base;
  8564. struct drm_crtc *crtc = NULL;
  8565. struct drm_device *dev = encoder->dev;
  8566. struct drm_framebuffer *fb;
  8567. struct drm_mode_config *config = &dev->mode_config;
  8568. struct drm_atomic_state *state = NULL;
  8569. struct drm_connector_state *connector_state;
  8570. struct intel_crtc_state *crtc_state;
  8571. int ret, i = -1;
  8572. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8573. connector->base.id, connector->name,
  8574. encoder->base.id, encoder->name);
  8575. retry:
  8576. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8577. if (ret)
  8578. goto fail;
  8579. /*
  8580. * Algorithm gets a little messy:
  8581. *
  8582. * - if the connector already has an assigned crtc, use it (but make
  8583. * sure it's on first)
  8584. *
  8585. * - try to find the first unused crtc that can drive this connector,
  8586. * and use that if we find one
  8587. */
  8588. /* See if we already have a CRTC for this connector */
  8589. if (encoder->crtc) {
  8590. crtc = encoder->crtc;
  8591. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8592. if (ret)
  8593. goto fail;
  8594. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8595. if (ret)
  8596. goto fail;
  8597. old->dpms_mode = connector->dpms;
  8598. old->load_detect_temp = false;
  8599. /* Make sure the crtc and connector are running */
  8600. if (connector->dpms != DRM_MODE_DPMS_ON)
  8601. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8602. return true;
  8603. }
  8604. /* Find an unused one (if possible) */
  8605. for_each_crtc(dev, possible_crtc) {
  8606. i++;
  8607. if (!(encoder->possible_crtcs & (1 << i)))
  8608. continue;
  8609. if (possible_crtc->state->enable)
  8610. continue;
  8611. crtc = possible_crtc;
  8612. break;
  8613. }
  8614. /*
  8615. * If we didn't find an unused CRTC, don't use any.
  8616. */
  8617. if (!crtc) {
  8618. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8619. goto fail;
  8620. }
  8621. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8622. if (ret)
  8623. goto fail;
  8624. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8625. if (ret)
  8626. goto fail;
  8627. intel_crtc = to_intel_crtc(crtc);
  8628. old->dpms_mode = connector->dpms;
  8629. old->load_detect_temp = true;
  8630. old->release_fb = NULL;
  8631. state = drm_atomic_state_alloc(dev);
  8632. if (!state)
  8633. return false;
  8634. state->acquire_ctx = ctx;
  8635. connector_state = drm_atomic_get_connector_state(state, connector);
  8636. if (IS_ERR(connector_state)) {
  8637. ret = PTR_ERR(connector_state);
  8638. goto fail;
  8639. }
  8640. connector_state->crtc = crtc;
  8641. connector_state->best_encoder = &intel_encoder->base;
  8642. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8643. if (IS_ERR(crtc_state)) {
  8644. ret = PTR_ERR(crtc_state);
  8645. goto fail;
  8646. }
  8647. crtc_state->base.active = crtc_state->base.enable = true;
  8648. if (!mode)
  8649. mode = &load_detect_mode;
  8650. /* We need a framebuffer large enough to accommodate all accesses
  8651. * that the plane may generate whilst we perform load detection.
  8652. * We can not rely on the fbcon either being present (we get called
  8653. * during its initialisation to detect all boot displays, or it may
  8654. * not even exist) or that it is large enough to satisfy the
  8655. * requested mode.
  8656. */
  8657. fb = mode_fits_in_fbdev(dev, mode);
  8658. if (fb == NULL) {
  8659. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8660. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8661. old->release_fb = fb;
  8662. } else
  8663. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8664. if (IS_ERR(fb)) {
  8665. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8666. goto fail;
  8667. }
  8668. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8669. if (ret)
  8670. goto fail;
  8671. drm_mode_copy(&crtc_state->base.mode, mode);
  8672. if (drm_atomic_commit(state)) {
  8673. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8674. if (old->release_fb)
  8675. old->release_fb->funcs->destroy(old->release_fb);
  8676. goto fail;
  8677. }
  8678. crtc->primary->crtc = crtc;
  8679. /* let the connector get through one full cycle before testing */
  8680. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8681. return true;
  8682. fail:
  8683. drm_atomic_state_free(state);
  8684. state = NULL;
  8685. if (ret == -EDEADLK) {
  8686. drm_modeset_backoff(ctx);
  8687. goto retry;
  8688. }
  8689. return false;
  8690. }
  8691. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8692. struct intel_load_detect_pipe *old,
  8693. struct drm_modeset_acquire_ctx *ctx)
  8694. {
  8695. struct drm_device *dev = connector->dev;
  8696. struct intel_encoder *intel_encoder =
  8697. intel_attached_encoder(connector);
  8698. struct drm_encoder *encoder = &intel_encoder->base;
  8699. struct drm_crtc *crtc = encoder->crtc;
  8700. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8701. struct drm_atomic_state *state;
  8702. struct drm_connector_state *connector_state;
  8703. struct intel_crtc_state *crtc_state;
  8704. int ret;
  8705. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8706. connector->base.id, connector->name,
  8707. encoder->base.id, encoder->name);
  8708. if (old->load_detect_temp) {
  8709. state = drm_atomic_state_alloc(dev);
  8710. if (!state)
  8711. goto fail;
  8712. state->acquire_ctx = ctx;
  8713. connector_state = drm_atomic_get_connector_state(state, connector);
  8714. if (IS_ERR(connector_state))
  8715. goto fail;
  8716. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8717. if (IS_ERR(crtc_state))
  8718. goto fail;
  8719. connector_state->best_encoder = NULL;
  8720. connector_state->crtc = NULL;
  8721. crtc_state->base.enable = crtc_state->base.active = false;
  8722. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8723. 0, 0);
  8724. if (ret)
  8725. goto fail;
  8726. ret = drm_atomic_commit(state);
  8727. if (ret)
  8728. goto fail;
  8729. if (old->release_fb) {
  8730. drm_framebuffer_unregister_private(old->release_fb);
  8731. drm_framebuffer_unreference(old->release_fb);
  8732. }
  8733. return;
  8734. }
  8735. /* Switch crtc and encoder back off if necessary */
  8736. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8737. connector->funcs->dpms(connector, old->dpms_mode);
  8738. return;
  8739. fail:
  8740. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8741. drm_atomic_state_free(state);
  8742. }
  8743. static int i9xx_pll_refclk(struct drm_device *dev,
  8744. const struct intel_crtc_state *pipe_config)
  8745. {
  8746. struct drm_i915_private *dev_priv = dev->dev_private;
  8747. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8748. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8749. return dev_priv->vbt.lvds_ssc_freq;
  8750. else if (HAS_PCH_SPLIT(dev))
  8751. return 120000;
  8752. else if (!IS_GEN2(dev))
  8753. return 96000;
  8754. else
  8755. return 48000;
  8756. }
  8757. /* Returns the clock of the currently programmed mode of the given pipe. */
  8758. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8759. struct intel_crtc_state *pipe_config)
  8760. {
  8761. struct drm_device *dev = crtc->base.dev;
  8762. struct drm_i915_private *dev_priv = dev->dev_private;
  8763. int pipe = pipe_config->cpu_transcoder;
  8764. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8765. u32 fp;
  8766. intel_clock_t clock;
  8767. int port_clock;
  8768. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8769. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8770. fp = pipe_config->dpll_hw_state.fp0;
  8771. else
  8772. fp = pipe_config->dpll_hw_state.fp1;
  8773. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8774. if (IS_PINEVIEW(dev)) {
  8775. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8776. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8777. } else {
  8778. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8779. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8780. }
  8781. if (!IS_GEN2(dev)) {
  8782. if (IS_PINEVIEW(dev))
  8783. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8784. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8785. else
  8786. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8787. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8788. switch (dpll & DPLL_MODE_MASK) {
  8789. case DPLLB_MODE_DAC_SERIAL:
  8790. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8791. 5 : 10;
  8792. break;
  8793. case DPLLB_MODE_LVDS:
  8794. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8795. 7 : 14;
  8796. break;
  8797. default:
  8798. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8799. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8800. return;
  8801. }
  8802. if (IS_PINEVIEW(dev))
  8803. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8804. else
  8805. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8806. } else {
  8807. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8808. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8809. if (is_lvds) {
  8810. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8811. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8812. if (lvds & LVDS_CLKB_POWER_UP)
  8813. clock.p2 = 7;
  8814. else
  8815. clock.p2 = 14;
  8816. } else {
  8817. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8818. clock.p1 = 2;
  8819. else {
  8820. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8821. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8822. }
  8823. if (dpll & PLL_P2_DIVIDE_BY_4)
  8824. clock.p2 = 4;
  8825. else
  8826. clock.p2 = 2;
  8827. }
  8828. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8829. }
  8830. /*
  8831. * This value includes pixel_multiplier. We will use
  8832. * port_clock to compute adjusted_mode.crtc_clock in the
  8833. * encoder's get_config() function.
  8834. */
  8835. pipe_config->port_clock = port_clock;
  8836. }
  8837. int intel_dotclock_calculate(int link_freq,
  8838. const struct intel_link_m_n *m_n)
  8839. {
  8840. /*
  8841. * The calculation for the data clock is:
  8842. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8843. * But we want to avoid losing precison if possible, so:
  8844. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8845. *
  8846. * and the link clock is simpler:
  8847. * link_clock = (m * link_clock) / n
  8848. */
  8849. if (!m_n->link_n)
  8850. return 0;
  8851. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8852. }
  8853. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8854. struct intel_crtc_state *pipe_config)
  8855. {
  8856. struct drm_device *dev = crtc->base.dev;
  8857. /* read out port_clock from the DPLL */
  8858. i9xx_crtc_clock_get(crtc, pipe_config);
  8859. /*
  8860. * This value does not include pixel_multiplier.
  8861. * We will check that port_clock and adjusted_mode.crtc_clock
  8862. * agree once we know their relationship in the encoder's
  8863. * get_config() function.
  8864. */
  8865. pipe_config->base.adjusted_mode.crtc_clock =
  8866. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8867. &pipe_config->fdi_m_n);
  8868. }
  8869. /** Returns the currently programmed mode of the given pipe. */
  8870. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8871. struct drm_crtc *crtc)
  8872. {
  8873. struct drm_i915_private *dev_priv = dev->dev_private;
  8874. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8875. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8876. struct drm_display_mode *mode;
  8877. struct intel_crtc_state pipe_config;
  8878. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8879. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8880. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8881. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8882. enum pipe pipe = intel_crtc->pipe;
  8883. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8884. if (!mode)
  8885. return NULL;
  8886. /*
  8887. * Construct a pipe_config sufficient for getting the clock info
  8888. * back out of crtc_clock_get.
  8889. *
  8890. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8891. * to use a real value here instead.
  8892. */
  8893. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8894. pipe_config.pixel_multiplier = 1;
  8895. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8896. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8897. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8898. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8899. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8900. mode->hdisplay = (htot & 0xffff) + 1;
  8901. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8902. mode->hsync_start = (hsync & 0xffff) + 1;
  8903. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8904. mode->vdisplay = (vtot & 0xffff) + 1;
  8905. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8906. mode->vsync_start = (vsync & 0xffff) + 1;
  8907. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8908. drm_mode_set_name(mode);
  8909. return mode;
  8910. }
  8911. void intel_mark_busy(struct drm_device *dev)
  8912. {
  8913. struct drm_i915_private *dev_priv = dev->dev_private;
  8914. if (dev_priv->mm.busy)
  8915. return;
  8916. intel_runtime_pm_get(dev_priv);
  8917. i915_update_gfx_val(dev_priv);
  8918. if (INTEL_INFO(dev)->gen >= 6)
  8919. gen6_rps_busy(dev_priv);
  8920. dev_priv->mm.busy = true;
  8921. }
  8922. void intel_mark_idle(struct drm_device *dev)
  8923. {
  8924. struct drm_i915_private *dev_priv = dev->dev_private;
  8925. if (!dev_priv->mm.busy)
  8926. return;
  8927. dev_priv->mm.busy = false;
  8928. if (INTEL_INFO(dev)->gen >= 6)
  8929. gen6_rps_idle(dev->dev_private);
  8930. intel_runtime_pm_put(dev_priv);
  8931. }
  8932. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8933. {
  8934. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8935. struct drm_device *dev = crtc->dev;
  8936. struct intel_unpin_work *work;
  8937. spin_lock_irq(&dev->event_lock);
  8938. work = intel_crtc->unpin_work;
  8939. intel_crtc->unpin_work = NULL;
  8940. spin_unlock_irq(&dev->event_lock);
  8941. if (work) {
  8942. cancel_work_sync(&work->work);
  8943. kfree(work);
  8944. }
  8945. drm_crtc_cleanup(crtc);
  8946. kfree(intel_crtc);
  8947. }
  8948. static void intel_unpin_work_fn(struct work_struct *__work)
  8949. {
  8950. struct intel_unpin_work *work =
  8951. container_of(__work, struct intel_unpin_work, work);
  8952. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8953. struct drm_device *dev = crtc->base.dev;
  8954. struct drm_plane *primary = crtc->base.primary;
  8955. mutex_lock(&dev->struct_mutex);
  8956. intel_unpin_fb_obj(work->old_fb, primary->state);
  8957. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8958. if (work->flip_queued_req)
  8959. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8960. mutex_unlock(&dev->struct_mutex);
  8961. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8962. drm_framebuffer_unreference(work->old_fb);
  8963. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8964. atomic_dec(&crtc->unpin_work_count);
  8965. kfree(work);
  8966. }
  8967. static void do_intel_finish_page_flip(struct drm_device *dev,
  8968. struct drm_crtc *crtc)
  8969. {
  8970. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8971. struct intel_unpin_work *work;
  8972. unsigned long flags;
  8973. /* Ignore early vblank irqs */
  8974. if (intel_crtc == NULL)
  8975. return;
  8976. /*
  8977. * This is called both by irq handlers and the reset code (to complete
  8978. * lost pageflips) so needs the full irqsave spinlocks.
  8979. */
  8980. spin_lock_irqsave(&dev->event_lock, flags);
  8981. work = intel_crtc->unpin_work;
  8982. /* Ensure we don't miss a work->pending update ... */
  8983. smp_rmb();
  8984. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8985. spin_unlock_irqrestore(&dev->event_lock, flags);
  8986. return;
  8987. }
  8988. page_flip_completed(intel_crtc);
  8989. spin_unlock_irqrestore(&dev->event_lock, flags);
  8990. }
  8991. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8992. {
  8993. struct drm_i915_private *dev_priv = dev->dev_private;
  8994. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8995. do_intel_finish_page_flip(dev, crtc);
  8996. }
  8997. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  8998. {
  8999. struct drm_i915_private *dev_priv = dev->dev_private;
  9000. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9001. do_intel_finish_page_flip(dev, crtc);
  9002. }
  9003. /* Is 'a' after or equal to 'b'? */
  9004. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9005. {
  9006. return !((a - b) & 0x80000000);
  9007. }
  9008. static bool page_flip_finished(struct intel_crtc *crtc)
  9009. {
  9010. struct drm_device *dev = crtc->base.dev;
  9011. struct drm_i915_private *dev_priv = dev->dev_private;
  9012. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9013. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9014. return true;
  9015. /*
  9016. * The relevant registers doen't exist on pre-ctg.
  9017. * As the flip done interrupt doesn't trigger for mmio
  9018. * flips on gmch platforms, a flip count check isn't
  9019. * really needed there. But since ctg has the registers,
  9020. * include it in the check anyway.
  9021. */
  9022. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9023. return true;
  9024. /*
  9025. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9026. * used the same base address. In that case the mmio flip might
  9027. * have completed, but the CS hasn't even executed the flip yet.
  9028. *
  9029. * A flip count check isn't enough as the CS might have updated
  9030. * the base address just after start of vblank, but before we
  9031. * managed to process the interrupt. This means we'd complete the
  9032. * CS flip too soon.
  9033. *
  9034. * Combining both checks should get us a good enough result. It may
  9035. * still happen that the CS flip has been executed, but has not
  9036. * yet actually completed. But in case the base address is the same
  9037. * anyway, we don't really care.
  9038. */
  9039. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9040. crtc->unpin_work->gtt_offset &&
  9041. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9042. crtc->unpin_work->flip_count);
  9043. }
  9044. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9045. {
  9046. struct drm_i915_private *dev_priv = dev->dev_private;
  9047. struct intel_crtc *intel_crtc =
  9048. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9049. unsigned long flags;
  9050. /*
  9051. * This is called both by irq handlers and the reset code (to complete
  9052. * lost pageflips) so needs the full irqsave spinlocks.
  9053. *
  9054. * NB: An MMIO update of the plane base pointer will also
  9055. * generate a page-flip completion irq, i.e. every modeset
  9056. * is also accompanied by a spurious intel_prepare_page_flip().
  9057. */
  9058. spin_lock_irqsave(&dev->event_lock, flags);
  9059. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9060. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9061. spin_unlock_irqrestore(&dev->event_lock, flags);
  9062. }
  9063. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9064. {
  9065. /* Ensure that the work item is consistent when activating it ... */
  9066. smp_wmb();
  9067. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9068. /* and that it is marked active as soon as the irq could fire. */
  9069. smp_wmb();
  9070. }
  9071. static int intel_gen2_queue_flip(struct drm_device *dev,
  9072. struct drm_crtc *crtc,
  9073. struct drm_framebuffer *fb,
  9074. struct drm_i915_gem_object *obj,
  9075. struct drm_i915_gem_request *req,
  9076. uint32_t flags)
  9077. {
  9078. struct intel_engine_cs *ring = req->ring;
  9079. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9080. u32 flip_mask;
  9081. int ret;
  9082. ret = intel_ring_begin(req, 6);
  9083. if (ret)
  9084. return ret;
  9085. /* Can't queue multiple flips, so wait for the previous
  9086. * one to finish before executing the next.
  9087. */
  9088. if (intel_crtc->plane)
  9089. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9090. else
  9091. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9092. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9093. intel_ring_emit(ring, MI_NOOP);
  9094. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9095. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9096. intel_ring_emit(ring, fb->pitches[0]);
  9097. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9098. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9099. intel_mark_page_flip_active(intel_crtc);
  9100. return 0;
  9101. }
  9102. static int intel_gen3_queue_flip(struct drm_device *dev,
  9103. struct drm_crtc *crtc,
  9104. struct drm_framebuffer *fb,
  9105. struct drm_i915_gem_object *obj,
  9106. struct drm_i915_gem_request *req,
  9107. uint32_t flags)
  9108. {
  9109. struct intel_engine_cs *ring = req->ring;
  9110. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9111. u32 flip_mask;
  9112. int ret;
  9113. ret = intel_ring_begin(req, 6);
  9114. if (ret)
  9115. return ret;
  9116. if (intel_crtc->plane)
  9117. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9118. else
  9119. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9120. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9121. intel_ring_emit(ring, MI_NOOP);
  9122. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9123. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9124. intel_ring_emit(ring, fb->pitches[0]);
  9125. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9126. intel_ring_emit(ring, MI_NOOP);
  9127. intel_mark_page_flip_active(intel_crtc);
  9128. return 0;
  9129. }
  9130. static int intel_gen4_queue_flip(struct drm_device *dev,
  9131. struct drm_crtc *crtc,
  9132. struct drm_framebuffer *fb,
  9133. struct drm_i915_gem_object *obj,
  9134. struct drm_i915_gem_request *req,
  9135. uint32_t flags)
  9136. {
  9137. struct intel_engine_cs *ring = req->ring;
  9138. struct drm_i915_private *dev_priv = dev->dev_private;
  9139. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9140. uint32_t pf, pipesrc;
  9141. int ret;
  9142. ret = intel_ring_begin(req, 4);
  9143. if (ret)
  9144. return ret;
  9145. /* i965+ uses the linear or tiled offsets from the
  9146. * Display Registers (which do not change across a page-flip)
  9147. * so we need only reprogram the base address.
  9148. */
  9149. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9150. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9151. intel_ring_emit(ring, fb->pitches[0]);
  9152. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9153. obj->tiling_mode);
  9154. /* XXX Enabling the panel-fitter across page-flip is so far
  9155. * untested on non-native modes, so ignore it for now.
  9156. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9157. */
  9158. pf = 0;
  9159. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9160. intel_ring_emit(ring, pf | pipesrc);
  9161. intel_mark_page_flip_active(intel_crtc);
  9162. return 0;
  9163. }
  9164. static int intel_gen6_queue_flip(struct drm_device *dev,
  9165. struct drm_crtc *crtc,
  9166. struct drm_framebuffer *fb,
  9167. struct drm_i915_gem_object *obj,
  9168. struct drm_i915_gem_request *req,
  9169. uint32_t flags)
  9170. {
  9171. struct intel_engine_cs *ring = req->ring;
  9172. struct drm_i915_private *dev_priv = dev->dev_private;
  9173. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9174. uint32_t pf, pipesrc;
  9175. int ret;
  9176. ret = intel_ring_begin(req, 4);
  9177. if (ret)
  9178. return ret;
  9179. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9180. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9181. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9182. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9183. /* Contrary to the suggestions in the documentation,
  9184. * "Enable Panel Fitter" does not seem to be required when page
  9185. * flipping with a non-native mode, and worse causes a normal
  9186. * modeset to fail.
  9187. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9188. */
  9189. pf = 0;
  9190. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9191. intel_ring_emit(ring, pf | pipesrc);
  9192. intel_mark_page_flip_active(intel_crtc);
  9193. return 0;
  9194. }
  9195. static int intel_gen7_queue_flip(struct drm_device *dev,
  9196. struct drm_crtc *crtc,
  9197. struct drm_framebuffer *fb,
  9198. struct drm_i915_gem_object *obj,
  9199. struct drm_i915_gem_request *req,
  9200. uint32_t flags)
  9201. {
  9202. struct intel_engine_cs *ring = req->ring;
  9203. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9204. uint32_t plane_bit = 0;
  9205. int len, ret;
  9206. switch (intel_crtc->plane) {
  9207. case PLANE_A:
  9208. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9209. break;
  9210. case PLANE_B:
  9211. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9212. break;
  9213. case PLANE_C:
  9214. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9215. break;
  9216. default:
  9217. WARN_ONCE(1, "unknown plane in flip command\n");
  9218. return -ENODEV;
  9219. }
  9220. len = 4;
  9221. if (ring->id == RCS) {
  9222. len += 6;
  9223. /*
  9224. * On Gen 8, SRM is now taking an extra dword to accommodate
  9225. * 48bits addresses, and we need a NOOP for the batch size to
  9226. * stay even.
  9227. */
  9228. if (IS_GEN8(dev))
  9229. len += 2;
  9230. }
  9231. /*
  9232. * BSpec MI_DISPLAY_FLIP for IVB:
  9233. * "The full packet must be contained within the same cache line."
  9234. *
  9235. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9236. * cacheline, if we ever start emitting more commands before
  9237. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9238. * then do the cacheline alignment, and finally emit the
  9239. * MI_DISPLAY_FLIP.
  9240. */
  9241. ret = intel_ring_cacheline_align(req);
  9242. if (ret)
  9243. return ret;
  9244. ret = intel_ring_begin(req, len);
  9245. if (ret)
  9246. return ret;
  9247. /* Unmask the flip-done completion message. Note that the bspec says that
  9248. * we should do this for both the BCS and RCS, and that we must not unmask
  9249. * more than one flip event at any time (or ensure that one flip message
  9250. * can be sent by waiting for flip-done prior to queueing new flips).
  9251. * Experimentation says that BCS works despite DERRMR masking all
  9252. * flip-done completion events and that unmasking all planes at once
  9253. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9254. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9255. */
  9256. if (ring->id == RCS) {
  9257. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9258. intel_ring_emit(ring, DERRMR);
  9259. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9260. DERRMR_PIPEB_PRI_FLIP_DONE |
  9261. DERRMR_PIPEC_PRI_FLIP_DONE));
  9262. if (IS_GEN8(dev))
  9263. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9264. MI_SRM_LRM_GLOBAL_GTT);
  9265. else
  9266. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9267. MI_SRM_LRM_GLOBAL_GTT);
  9268. intel_ring_emit(ring, DERRMR);
  9269. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9270. if (IS_GEN8(dev)) {
  9271. intel_ring_emit(ring, 0);
  9272. intel_ring_emit(ring, MI_NOOP);
  9273. }
  9274. }
  9275. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9276. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9277. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9278. intel_ring_emit(ring, (MI_NOOP));
  9279. intel_mark_page_flip_active(intel_crtc);
  9280. return 0;
  9281. }
  9282. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9283. struct drm_i915_gem_object *obj)
  9284. {
  9285. /*
  9286. * This is not being used for older platforms, because
  9287. * non-availability of flip done interrupt forces us to use
  9288. * CS flips. Older platforms derive flip done using some clever
  9289. * tricks involving the flip_pending status bits and vblank irqs.
  9290. * So using MMIO flips there would disrupt this mechanism.
  9291. */
  9292. if (ring == NULL)
  9293. return true;
  9294. if (INTEL_INFO(ring->dev)->gen < 5)
  9295. return false;
  9296. if (i915.use_mmio_flip < 0)
  9297. return false;
  9298. else if (i915.use_mmio_flip > 0)
  9299. return true;
  9300. else if (i915.enable_execlists)
  9301. return true;
  9302. else
  9303. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9304. }
  9305. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9306. {
  9307. struct drm_device *dev = intel_crtc->base.dev;
  9308. struct drm_i915_private *dev_priv = dev->dev_private;
  9309. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9310. const enum pipe pipe = intel_crtc->pipe;
  9311. u32 ctl, stride;
  9312. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9313. ctl &= ~PLANE_CTL_TILED_MASK;
  9314. switch (fb->modifier[0]) {
  9315. case DRM_FORMAT_MOD_NONE:
  9316. break;
  9317. case I915_FORMAT_MOD_X_TILED:
  9318. ctl |= PLANE_CTL_TILED_X;
  9319. break;
  9320. case I915_FORMAT_MOD_Y_TILED:
  9321. ctl |= PLANE_CTL_TILED_Y;
  9322. break;
  9323. case I915_FORMAT_MOD_Yf_TILED:
  9324. ctl |= PLANE_CTL_TILED_YF;
  9325. break;
  9326. default:
  9327. MISSING_CASE(fb->modifier[0]);
  9328. }
  9329. /*
  9330. * The stride is either expressed as a multiple of 64 bytes chunks for
  9331. * linear buffers or in number of tiles for tiled buffers.
  9332. */
  9333. stride = fb->pitches[0] /
  9334. intel_fb_stride_alignment(dev, fb->modifier[0],
  9335. fb->pixel_format);
  9336. /*
  9337. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9338. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9339. */
  9340. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9341. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9342. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9343. POSTING_READ(PLANE_SURF(pipe, 0));
  9344. }
  9345. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9346. {
  9347. struct drm_device *dev = intel_crtc->base.dev;
  9348. struct drm_i915_private *dev_priv = dev->dev_private;
  9349. struct intel_framebuffer *intel_fb =
  9350. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9351. struct drm_i915_gem_object *obj = intel_fb->obj;
  9352. u32 dspcntr;
  9353. u32 reg;
  9354. reg = DSPCNTR(intel_crtc->plane);
  9355. dspcntr = I915_READ(reg);
  9356. if (obj->tiling_mode != I915_TILING_NONE)
  9357. dspcntr |= DISPPLANE_TILED;
  9358. else
  9359. dspcntr &= ~DISPPLANE_TILED;
  9360. I915_WRITE(reg, dspcntr);
  9361. I915_WRITE(DSPSURF(intel_crtc->plane),
  9362. intel_crtc->unpin_work->gtt_offset);
  9363. POSTING_READ(DSPSURF(intel_crtc->plane));
  9364. }
  9365. /*
  9366. * XXX: This is the temporary way to update the plane registers until we get
  9367. * around to using the usual plane update functions for MMIO flips
  9368. */
  9369. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9370. {
  9371. struct drm_device *dev = intel_crtc->base.dev;
  9372. u32 start_vbl_count;
  9373. intel_mark_page_flip_active(intel_crtc);
  9374. intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9375. if (INTEL_INFO(dev)->gen >= 9)
  9376. skl_do_mmio_flip(intel_crtc);
  9377. else
  9378. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9379. ilk_do_mmio_flip(intel_crtc);
  9380. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9381. }
  9382. static void intel_mmio_flip_work_func(struct work_struct *work)
  9383. {
  9384. struct intel_mmio_flip *mmio_flip =
  9385. container_of(work, struct intel_mmio_flip, work);
  9386. if (mmio_flip->req)
  9387. WARN_ON(__i915_wait_request(mmio_flip->req,
  9388. mmio_flip->crtc->reset_counter,
  9389. false, NULL,
  9390. &mmio_flip->i915->rps.mmioflips));
  9391. intel_do_mmio_flip(mmio_flip->crtc);
  9392. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9393. kfree(mmio_flip);
  9394. }
  9395. static int intel_queue_mmio_flip(struct drm_device *dev,
  9396. struct drm_crtc *crtc,
  9397. struct drm_framebuffer *fb,
  9398. struct drm_i915_gem_object *obj,
  9399. struct intel_engine_cs *ring,
  9400. uint32_t flags)
  9401. {
  9402. struct intel_mmio_flip *mmio_flip;
  9403. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9404. if (mmio_flip == NULL)
  9405. return -ENOMEM;
  9406. mmio_flip->i915 = to_i915(dev);
  9407. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9408. mmio_flip->crtc = to_intel_crtc(crtc);
  9409. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9410. schedule_work(&mmio_flip->work);
  9411. return 0;
  9412. }
  9413. static int intel_default_queue_flip(struct drm_device *dev,
  9414. struct drm_crtc *crtc,
  9415. struct drm_framebuffer *fb,
  9416. struct drm_i915_gem_object *obj,
  9417. struct drm_i915_gem_request *req,
  9418. uint32_t flags)
  9419. {
  9420. return -ENODEV;
  9421. }
  9422. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9423. struct drm_crtc *crtc)
  9424. {
  9425. struct drm_i915_private *dev_priv = dev->dev_private;
  9426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9427. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9428. u32 addr;
  9429. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9430. return true;
  9431. if (!work->enable_stall_check)
  9432. return false;
  9433. if (work->flip_ready_vblank == 0) {
  9434. if (work->flip_queued_req &&
  9435. !i915_gem_request_completed(work->flip_queued_req, true))
  9436. return false;
  9437. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9438. }
  9439. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9440. return false;
  9441. /* Potential stall - if we see that the flip has happened,
  9442. * assume a missed interrupt. */
  9443. if (INTEL_INFO(dev)->gen >= 4)
  9444. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9445. else
  9446. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9447. /* There is a potential issue here with a false positive after a flip
  9448. * to the same address. We could address this by checking for a
  9449. * non-incrementing frame counter.
  9450. */
  9451. return addr == work->gtt_offset;
  9452. }
  9453. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9454. {
  9455. struct drm_i915_private *dev_priv = dev->dev_private;
  9456. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9457. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9458. struct intel_unpin_work *work;
  9459. WARN_ON(!in_interrupt());
  9460. if (crtc == NULL)
  9461. return;
  9462. spin_lock(&dev->event_lock);
  9463. work = intel_crtc->unpin_work;
  9464. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9465. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9466. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9467. page_flip_completed(intel_crtc);
  9468. work = NULL;
  9469. }
  9470. if (work != NULL &&
  9471. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9472. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9473. spin_unlock(&dev->event_lock);
  9474. }
  9475. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9476. struct drm_framebuffer *fb,
  9477. struct drm_pending_vblank_event *event,
  9478. uint32_t page_flip_flags)
  9479. {
  9480. struct drm_device *dev = crtc->dev;
  9481. struct drm_i915_private *dev_priv = dev->dev_private;
  9482. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9483. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9484. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9485. struct drm_plane *primary = crtc->primary;
  9486. enum pipe pipe = intel_crtc->pipe;
  9487. struct intel_unpin_work *work;
  9488. struct intel_engine_cs *ring;
  9489. bool mmio_flip;
  9490. struct drm_i915_gem_request *request = NULL;
  9491. int ret;
  9492. /*
  9493. * drm_mode_page_flip_ioctl() should already catch this, but double
  9494. * check to be safe. In the future we may enable pageflipping from
  9495. * a disabled primary plane.
  9496. */
  9497. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9498. return -EBUSY;
  9499. /* Can't change pixel format via MI display flips. */
  9500. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9501. return -EINVAL;
  9502. /*
  9503. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9504. * Note that pitch changes could also affect these register.
  9505. */
  9506. if (INTEL_INFO(dev)->gen > 3 &&
  9507. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9508. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9509. return -EINVAL;
  9510. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9511. goto out_hang;
  9512. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9513. if (work == NULL)
  9514. return -ENOMEM;
  9515. work->event = event;
  9516. work->crtc = crtc;
  9517. work->old_fb = old_fb;
  9518. INIT_WORK(&work->work, intel_unpin_work_fn);
  9519. ret = drm_crtc_vblank_get(crtc);
  9520. if (ret)
  9521. goto free_work;
  9522. /* We borrow the event spin lock for protecting unpin_work */
  9523. spin_lock_irq(&dev->event_lock);
  9524. if (intel_crtc->unpin_work) {
  9525. /* Before declaring the flip queue wedged, check if
  9526. * the hardware completed the operation behind our backs.
  9527. */
  9528. if (__intel_pageflip_stall_check(dev, crtc)) {
  9529. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9530. page_flip_completed(intel_crtc);
  9531. } else {
  9532. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9533. spin_unlock_irq(&dev->event_lock);
  9534. drm_crtc_vblank_put(crtc);
  9535. kfree(work);
  9536. return -EBUSY;
  9537. }
  9538. }
  9539. intel_crtc->unpin_work = work;
  9540. spin_unlock_irq(&dev->event_lock);
  9541. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9542. flush_workqueue(dev_priv->wq);
  9543. /* Reference the objects for the scheduled work. */
  9544. drm_framebuffer_reference(work->old_fb);
  9545. drm_gem_object_reference(&obj->base);
  9546. crtc->primary->fb = fb;
  9547. update_state_fb(crtc->primary);
  9548. work->pending_flip_obj = obj;
  9549. ret = i915_mutex_lock_interruptible(dev);
  9550. if (ret)
  9551. goto cleanup;
  9552. atomic_inc(&intel_crtc->unpin_work_count);
  9553. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9554. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9555. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9556. if (IS_VALLEYVIEW(dev)) {
  9557. ring = &dev_priv->ring[BCS];
  9558. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9559. /* vlv: DISPLAY_FLIP fails to change tiling */
  9560. ring = NULL;
  9561. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9562. ring = &dev_priv->ring[BCS];
  9563. } else if (INTEL_INFO(dev)->gen >= 7) {
  9564. ring = i915_gem_request_get_ring(obj->last_write_req);
  9565. if (ring == NULL || ring->id != RCS)
  9566. ring = &dev_priv->ring[BCS];
  9567. } else {
  9568. ring = &dev_priv->ring[RCS];
  9569. }
  9570. mmio_flip = use_mmio_flip(ring, obj);
  9571. /* When using CS flips, we want to emit semaphores between rings.
  9572. * However, when using mmio flips we will create a task to do the
  9573. * synchronisation, so all we want here is to pin the framebuffer
  9574. * into the display plane and skip any waits.
  9575. */
  9576. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9577. crtc->primary->state,
  9578. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9579. if (ret)
  9580. goto cleanup_pending;
  9581. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9582. + intel_crtc->dspaddr_offset;
  9583. if (mmio_flip) {
  9584. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9585. page_flip_flags);
  9586. if (ret)
  9587. goto cleanup_unpin;
  9588. i915_gem_request_assign(&work->flip_queued_req,
  9589. obj->last_write_req);
  9590. } else {
  9591. if (!request) {
  9592. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9593. if (ret)
  9594. goto cleanup_unpin;
  9595. }
  9596. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9597. page_flip_flags);
  9598. if (ret)
  9599. goto cleanup_unpin;
  9600. i915_gem_request_assign(&work->flip_queued_req, request);
  9601. }
  9602. if (request)
  9603. i915_add_request_no_flush(request);
  9604. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9605. work->enable_stall_check = true;
  9606. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9607. to_intel_plane(primary)->frontbuffer_bit);
  9608. mutex_unlock(&dev->struct_mutex);
  9609. intel_fbc_disable_crtc(intel_crtc);
  9610. intel_frontbuffer_flip_prepare(dev,
  9611. to_intel_plane(primary)->frontbuffer_bit);
  9612. trace_i915_flip_request(intel_crtc->plane, obj);
  9613. return 0;
  9614. cleanup_unpin:
  9615. intel_unpin_fb_obj(fb, crtc->primary->state);
  9616. cleanup_pending:
  9617. if (request)
  9618. i915_gem_request_cancel(request);
  9619. atomic_dec(&intel_crtc->unpin_work_count);
  9620. mutex_unlock(&dev->struct_mutex);
  9621. cleanup:
  9622. crtc->primary->fb = old_fb;
  9623. update_state_fb(crtc->primary);
  9624. drm_gem_object_unreference_unlocked(&obj->base);
  9625. drm_framebuffer_unreference(work->old_fb);
  9626. spin_lock_irq(&dev->event_lock);
  9627. intel_crtc->unpin_work = NULL;
  9628. spin_unlock_irq(&dev->event_lock);
  9629. drm_crtc_vblank_put(crtc);
  9630. free_work:
  9631. kfree(work);
  9632. if (ret == -EIO) {
  9633. struct drm_atomic_state *state;
  9634. struct drm_plane_state *plane_state;
  9635. out_hang:
  9636. state = drm_atomic_state_alloc(dev);
  9637. if (!state)
  9638. return -ENOMEM;
  9639. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9640. retry:
  9641. plane_state = drm_atomic_get_plane_state(state, primary);
  9642. ret = PTR_ERR_OR_ZERO(plane_state);
  9643. if (!ret) {
  9644. drm_atomic_set_fb_for_plane(plane_state, fb);
  9645. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9646. if (!ret)
  9647. ret = drm_atomic_commit(state);
  9648. }
  9649. if (ret == -EDEADLK) {
  9650. drm_modeset_backoff(state->acquire_ctx);
  9651. drm_atomic_state_clear(state);
  9652. goto retry;
  9653. }
  9654. if (ret)
  9655. drm_atomic_state_free(state);
  9656. if (ret == 0 && event) {
  9657. spin_lock_irq(&dev->event_lock);
  9658. drm_send_vblank_event(dev, pipe, event);
  9659. spin_unlock_irq(&dev->event_lock);
  9660. }
  9661. }
  9662. return ret;
  9663. }
  9664. /**
  9665. * intel_wm_need_update - Check whether watermarks need updating
  9666. * @plane: drm plane
  9667. * @state: new plane state
  9668. *
  9669. * Check current plane state versus the new one to determine whether
  9670. * watermarks need to be recalculated.
  9671. *
  9672. * Returns true or false.
  9673. */
  9674. static bool intel_wm_need_update(struct drm_plane *plane,
  9675. struct drm_plane_state *state)
  9676. {
  9677. /* Update watermarks on tiling changes. */
  9678. if (!plane->state->fb || !state->fb ||
  9679. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9680. plane->state->rotation != state->rotation)
  9681. return true;
  9682. if (plane->state->crtc_w != state->crtc_w)
  9683. return true;
  9684. return false;
  9685. }
  9686. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9687. struct drm_plane_state *plane_state)
  9688. {
  9689. struct drm_crtc *crtc = crtc_state->crtc;
  9690. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9691. struct drm_plane *plane = plane_state->plane;
  9692. struct drm_device *dev = crtc->dev;
  9693. struct drm_i915_private *dev_priv = dev->dev_private;
  9694. struct intel_plane_state *old_plane_state =
  9695. to_intel_plane_state(plane->state);
  9696. int idx = intel_crtc->base.base.id, ret;
  9697. int i = drm_plane_index(plane);
  9698. bool mode_changed = needs_modeset(crtc_state);
  9699. bool was_crtc_enabled = crtc->state->active;
  9700. bool is_crtc_enabled = crtc_state->active;
  9701. bool turn_off, turn_on, visible, was_visible;
  9702. struct drm_framebuffer *fb = plane_state->fb;
  9703. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9704. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9705. ret = skl_update_scaler_plane(
  9706. to_intel_crtc_state(crtc_state),
  9707. to_intel_plane_state(plane_state));
  9708. if (ret)
  9709. return ret;
  9710. }
  9711. /*
  9712. * Disabling a plane is always okay; we just need to update
  9713. * fb tracking in a special way since cleanup_fb() won't
  9714. * get called by the plane helpers.
  9715. */
  9716. if (old_plane_state->base.fb && !fb)
  9717. intel_crtc->atomic.disabled_planes |= 1 << i;
  9718. was_visible = old_plane_state->visible;
  9719. visible = to_intel_plane_state(plane_state)->visible;
  9720. if (!was_crtc_enabled && WARN_ON(was_visible))
  9721. was_visible = false;
  9722. if (!is_crtc_enabled && WARN_ON(visible))
  9723. visible = false;
  9724. if (!was_visible && !visible)
  9725. return 0;
  9726. turn_off = was_visible && (!visible || mode_changed);
  9727. turn_on = visible && (!was_visible || mode_changed);
  9728. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9729. plane->base.id, fb ? fb->base.id : -1);
  9730. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9731. plane->base.id, was_visible, visible,
  9732. turn_off, turn_on, mode_changed);
  9733. if (turn_on) {
  9734. intel_crtc->atomic.update_wm_pre = true;
  9735. /* must disable cxsr around plane enable/disable */
  9736. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9737. intel_crtc->atomic.disable_cxsr = true;
  9738. /* to potentially re-enable cxsr */
  9739. intel_crtc->atomic.wait_vblank = true;
  9740. intel_crtc->atomic.update_wm_post = true;
  9741. }
  9742. } else if (turn_off) {
  9743. intel_crtc->atomic.update_wm_post = true;
  9744. /* must disable cxsr around plane enable/disable */
  9745. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9746. if (is_crtc_enabled)
  9747. intel_crtc->atomic.wait_vblank = true;
  9748. intel_crtc->atomic.disable_cxsr = true;
  9749. }
  9750. } else if (intel_wm_need_update(plane, plane_state)) {
  9751. intel_crtc->atomic.update_wm_pre = true;
  9752. }
  9753. if (visible)
  9754. intel_crtc->atomic.fb_bits |=
  9755. to_intel_plane(plane)->frontbuffer_bit;
  9756. switch (plane->type) {
  9757. case DRM_PLANE_TYPE_PRIMARY:
  9758. intel_crtc->atomic.wait_for_flips = true;
  9759. intel_crtc->atomic.pre_disable_primary = turn_off;
  9760. intel_crtc->atomic.post_enable_primary = turn_on;
  9761. if (turn_off) {
  9762. /*
  9763. * FIXME: Actually if we will still have any other
  9764. * plane enabled on the pipe we could let IPS enabled
  9765. * still, but for now lets consider that when we make
  9766. * primary invisible by setting DSPCNTR to 0 on
  9767. * update_primary_plane function IPS needs to be
  9768. * disable.
  9769. */
  9770. intel_crtc->atomic.disable_ips = true;
  9771. intel_crtc->atomic.disable_fbc = true;
  9772. }
  9773. /*
  9774. * FBC does not work on some platforms for rotated
  9775. * planes, so disable it when rotation is not 0 and
  9776. * update it when rotation is set back to 0.
  9777. *
  9778. * FIXME: This is redundant with the fbc update done in
  9779. * the primary plane enable function except that that
  9780. * one is done too late. We eventually need to unify
  9781. * this.
  9782. */
  9783. if (visible &&
  9784. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9785. dev_priv->fbc.crtc == intel_crtc &&
  9786. plane_state->rotation != BIT(DRM_ROTATE_0))
  9787. intel_crtc->atomic.disable_fbc = true;
  9788. /*
  9789. * BDW signals flip done immediately if the plane
  9790. * is disabled, even if the plane enable is already
  9791. * armed to occur at the next vblank :(
  9792. */
  9793. if (turn_on && IS_BROADWELL(dev))
  9794. intel_crtc->atomic.wait_vblank = true;
  9795. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9796. break;
  9797. case DRM_PLANE_TYPE_CURSOR:
  9798. break;
  9799. case DRM_PLANE_TYPE_OVERLAY:
  9800. if (turn_off && !mode_changed) {
  9801. intel_crtc->atomic.wait_vblank = true;
  9802. intel_crtc->atomic.update_sprite_watermarks |=
  9803. 1 << i;
  9804. }
  9805. }
  9806. return 0;
  9807. }
  9808. static bool encoders_cloneable(const struct intel_encoder *a,
  9809. const struct intel_encoder *b)
  9810. {
  9811. /* masks could be asymmetric, so check both ways */
  9812. return a == b || (a->cloneable & (1 << b->type) &&
  9813. b->cloneable & (1 << a->type));
  9814. }
  9815. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9816. struct intel_crtc *crtc,
  9817. struct intel_encoder *encoder)
  9818. {
  9819. struct intel_encoder *source_encoder;
  9820. struct drm_connector *connector;
  9821. struct drm_connector_state *connector_state;
  9822. int i;
  9823. for_each_connector_in_state(state, connector, connector_state, i) {
  9824. if (connector_state->crtc != &crtc->base)
  9825. continue;
  9826. source_encoder =
  9827. to_intel_encoder(connector_state->best_encoder);
  9828. if (!encoders_cloneable(encoder, source_encoder))
  9829. return false;
  9830. }
  9831. return true;
  9832. }
  9833. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9834. struct intel_crtc *crtc)
  9835. {
  9836. struct intel_encoder *encoder;
  9837. struct drm_connector *connector;
  9838. struct drm_connector_state *connector_state;
  9839. int i;
  9840. for_each_connector_in_state(state, connector, connector_state, i) {
  9841. if (connector_state->crtc != &crtc->base)
  9842. continue;
  9843. encoder = to_intel_encoder(connector_state->best_encoder);
  9844. if (!check_single_encoder_cloning(state, crtc, encoder))
  9845. return false;
  9846. }
  9847. return true;
  9848. }
  9849. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9850. struct drm_crtc_state *crtc_state)
  9851. {
  9852. struct drm_device *dev = crtc->dev;
  9853. struct drm_i915_private *dev_priv = dev->dev_private;
  9854. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9855. struct intel_crtc_state *pipe_config =
  9856. to_intel_crtc_state(crtc_state);
  9857. struct drm_atomic_state *state = crtc_state->state;
  9858. int ret;
  9859. bool mode_changed = needs_modeset(crtc_state);
  9860. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9861. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9862. return -EINVAL;
  9863. }
  9864. if (mode_changed && !crtc_state->active)
  9865. intel_crtc->atomic.update_wm_post = true;
  9866. if (mode_changed && crtc_state->enable &&
  9867. dev_priv->display.crtc_compute_clock &&
  9868. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9869. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9870. pipe_config);
  9871. if (ret)
  9872. return ret;
  9873. }
  9874. ret = 0;
  9875. if (INTEL_INFO(dev)->gen >= 9) {
  9876. if (mode_changed)
  9877. ret = skl_update_scaler_crtc(pipe_config);
  9878. if (!ret)
  9879. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9880. pipe_config);
  9881. }
  9882. return ret;
  9883. }
  9884. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9885. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9886. .load_lut = intel_crtc_load_lut,
  9887. .atomic_begin = intel_begin_crtc_commit,
  9888. .atomic_flush = intel_finish_crtc_commit,
  9889. .atomic_check = intel_crtc_atomic_check,
  9890. };
  9891. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9892. {
  9893. struct intel_connector *connector;
  9894. for_each_intel_connector(dev, connector) {
  9895. if (connector->base.encoder) {
  9896. connector->base.state->best_encoder =
  9897. connector->base.encoder;
  9898. connector->base.state->crtc =
  9899. connector->base.encoder->crtc;
  9900. } else {
  9901. connector->base.state->best_encoder = NULL;
  9902. connector->base.state->crtc = NULL;
  9903. }
  9904. }
  9905. }
  9906. static void
  9907. connected_sink_compute_bpp(struct intel_connector *connector,
  9908. struct intel_crtc_state *pipe_config)
  9909. {
  9910. int bpp = pipe_config->pipe_bpp;
  9911. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9912. connector->base.base.id,
  9913. connector->base.name);
  9914. /* Don't use an invalid EDID bpc value */
  9915. if (connector->base.display_info.bpc &&
  9916. connector->base.display_info.bpc * 3 < bpp) {
  9917. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9918. bpp, connector->base.display_info.bpc*3);
  9919. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9920. }
  9921. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9922. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9923. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9924. bpp);
  9925. pipe_config->pipe_bpp = 24;
  9926. }
  9927. }
  9928. static int
  9929. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9930. struct intel_crtc_state *pipe_config)
  9931. {
  9932. struct drm_device *dev = crtc->base.dev;
  9933. struct drm_atomic_state *state;
  9934. struct drm_connector *connector;
  9935. struct drm_connector_state *connector_state;
  9936. int bpp, i;
  9937. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9938. bpp = 10*3;
  9939. else if (INTEL_INFO(dev)->gen >= 5)
  9940. bpp = 12*3;
  9941. else
  9942. bpp = 8*3;
  9943. pipe_config->pipe_bpp = bpp;
  9944. state = pipe_config->base.state;
  9945. /* Clamp display bpp to EDID value */
  9946. for_each_connector_in_state(state, connector, connector_state, i) {
  9947. if (connector_state->crtc != &crtc->base)
  9948. continue;
  9949. connected_sink_compute_bpp(to_intel_connector(connector),
  9950. pipe_config);
  9951. }
  9952. return bpp;
  9953. }
  9954. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9955. {
  9956. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9957. "type: 0x%x flags: 0x%x\n",
  9958. mode->crtc_clock,
  9959. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9960. mode->crtc_hsync_end, mode->crtc_htotal,
  9961. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9962. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9963. }
  9964. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9965. struct intel_crtc_state *pipe_config,
  9966. const char *context)
  9967. {
  9968. struct drm_device *dev = crtc->base.dev;
  9969. struct drm_plane *plane;
  9970. struct intel_plane *intel_plane;
  9971. struct intel_plane_state *state;
  9972. struct drm_framebuffer *fb;
  9973. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9974. context, pipe_config, pipe_name(crtc->pipe));
  9975. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9976. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9977. pipe_config->pipe_bpp, pipe_config->dither);
  9978. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9979. pipe_config->has_pch_encoder,
  9980. pipe_config->fdi_lanes,
  9981. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9982. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9983. pipe_config->fdi_m_n.tu);
  9984. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9985. pipe_config->has_dp_encoder,
  9986. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9987. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9988. pipe_config->dp_m_n.tu);
  9989. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9990. pipe_config->has_dp_encoder,
  9991. pipe_config->dp_m2_n2.gmch_m,
  9992. pipe_config->dp_m2_n2.gmch_n,
  9993. pipe_config->dp_m2_n2.link_m,
  9994. pipe_config->dp_m2_n2.link_n,
  9995. pipe_config->dp_m2_n2.tu);
  9996. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9997. pipe_config->has_audio,
  9998. pipe_config->has_infoframe);
  9999. DRM_DEBUG_KMS("requested mode:\n");
  10000. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10001. DRM_DEBUG_KMS("adjusted mode:\n");
  10002. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10003. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10004. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10005. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10006. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10007. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10008. crtc->num_scalers,
  10009. pipe_config->scaler_state.scaler_users,
  10010. pipe_config->scaler_state.scaler_id);
  10011. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10012. pipe_config->gmch_pfit.control,
  10013. pipe_config->gmch_pfit.pgm_ratios,
  10014. pipe_config->gmch_pfit.lvds_border_bits);
  10015. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10016. pipe_config->pch_pfit.pos,
  10017. pipe_config->pch_pfit.size,
  10018. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10019. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10020. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10021. if (IS_BROXTON(dev)) {
  10022. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10023. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10024. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10025. pipe_config->ddi_pll_sel,
  10026. pipe_config->dpll_hw_state.ebb0,
  10027. pipe_config->dpll_hw_state.ebb4,
  10028. pipe_config->dpll_hw_state.pll0,
  10029. pipe_config->dpll_hw_state.pll1,
  10030. pipe_config->dpll_hw_state.pll2,
  10031. pipe_config->dpll_hw_state.pll3,
  10032. pipe_config->dpll_hw_state.pll6,
  10033. pipe_config->dpll_hw_state.pll8,
  10034. pipe_config->dpll_hw_state.pll9,
  10035. pipe_config->dpll_hw_state.pll10,
  10036. pipe_config->dpll_hw_state.pcsdw12);
  10037. } else if (IS_SKYLAKE(dev)) {
  10038. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10039. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10040. pipe_config->ddi_pll_sel,
  10041. pipe_config->dpll_hw_state.ctrl1,
  10042. pipe_config->dpll_hw_state.cfgcr1,
  10043. pipe_config->dpll_hw_state.cfgcr2);
  10044. } else if (HAS_DDI(dev)) {
  10045. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10046. pipe_config->ddi_pll_sel,
  10047. pipe_config->dpll_hw_state.wrpll);
  10048. } else {
  10049. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10050. "fp0: 0x%x, fp1: 0x%x\n",
  10051. pipe_config->dpll_hw_state.dpll,
  10052. pipe_config->dpll_hw_state.dpll_md,
  10053. pipe_config->dpll_hw_state.fp0,
  10054. pipe_config->dpll_hw_state.fp1);
  10055. }
  10056. DRM_DEBUG_KMS("planes on this crtc\n");
  10057. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10058. intel_plane = to_intel_plane(plane);
  10059. if (intel_plane->pipe != crtc->pipe)
  10060. continue;
  10061. state = to_intel_plane_state(plane->state);
  10062. fb = state->base.fb;
  10063. if (!fb) {
  10064. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10065. "disabled, scaler_id = %d\n",
  10066. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10067. plane->base.id, intel_plane->pipe,
  10068. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10069. drm_plane_index(plane), state->scaler_id);
  10070. continue;
  10071. }
  10072. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10073. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10074. plane->base.id, intel_plane->pipe,
  10075. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10076. drm_plane_index(plane));
  10077. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10078. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10079. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10080. state->scaler_id,
  10081. state->src.x1 >> 16, state->src.y1 >> 16,
  10082. drm_rect_width(&state->src) >> 16,
  10083. drm_rect_height(&state->src) >> 16,
  10084. state->dst.x1, state->dst.y1,
  10085. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10086. }
  10087. }
  10088. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10089. {
  10090. struct drm_device *dev = state->dev;
  10091. struct intel_encoder *encoder;
  10092. struct drm_connector *connector;
  10093. struct drm_connector_state *connector_state;
  10094. unsigned int used_ports = 0;
  10095. int i;
  10096. /*
  10097. * Walk the connector list instead of the encoder
  10098. * list to detect the problem on ddi platforms
  10099. * where there's just one encoder per digital port.
  10100. */
  10101. for_each_connector_in_state(state, connector, connector_state, i) {
  10102. if (!connector_state->best_encoder)
  10103. continue;
  10104. encoder = to_intel_encoder(connector_state->best_encoder);
  10105. WARN_ON(!connector_state->crtc);
  10106. switch (encoder->type) {
  10107. unsigned int port_mask;
  10108. case INTEL_OUTPUT_UNKNOWN:
  10109. if (WARN_ON(!HAS_DDI(dev)))
  10110. break;
  10111. case INTEL_OUTPUT_DISPLAYPORT:
  10112. case INTEL_OUTPUT_HDMI:
  10113. case INTEL_OUTPUT_EDP:
  10114. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10115. /* the same port mustn't appear more than once */
  10116. if (used_ports & port_mask)
  10117. return false;
  10118. used_ports |= port_mask;
  10119. default:
  10120. break;
  10121. }
  10122. }
  10123. return true;
  10124. }
  10125. static void
  10126. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10127. {
  10128. struct drm_crtc_state tmp_state;
  10129. struct intel_crtc_scaler_state scaler_state;
  10130. struct intel_dpll_hw_state dpll_hw_state;
  10131. enum intel_dpll_id shared_dpll;
  10132. uint32_t ddi_pll_sel;
  10133. bool force_thru;
  10134. /* FIXME: before the switch to atomic started, a new pipe_config was
  10135. * kzalloc'd. Code that depends on any field being zero should be
  10136. * fixed, so that the crtc_state can be safely duplicated. For now,
  10137. * only fields that are know to not cause problems are preserved. */
  10138. tmp_state = crtc_state->base;
  10139. scaler_state = crtc_state->scaler_state;
  10140. shared_dpll = crtc_state->shared_dpll;
  10141. dpll_hw_state = crtc_state->dpll_hw_state;
  10142. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10143. force_thru = crtc_state->pch_pfit.force_thru;
  10144. memset(crtc_state, 0, sizeof *crtc_state);
  10145. crtc_state->base = tmp_state;
  10146. crtc_state->scaler_state = scaler_state;
  10147. crtc_state->shared_dpll = shared_dpll;
  10148. crtc_state->dpll_hw_state = dpll_hw_state;
  10149. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10150. crtc_state->pch_pfit.force_thru = force_thru;
  10151. }
  10152. static int
  10153. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10154. struct intel_crtc_state *pipe_config)
  10155. {
  10156. struct drm_atomic_state *state = pipe_config->base.state;
  10157. struct intel_encoder *encoder;
  10158. struct drm_connector *connector;
  10159. struct drm_connector_state *connector_state;
  10160. int base_bpp, ret = -EINVAL;
  10161. int i;
  10162. bool retry = true;
  10163. clear_intel_crtc_state(pipe_config);
  10164. pipe_config->cpu_transcoder =
  10165. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10166. /*
  10167. * Sanitize sync polarity flags based on requested ones. If neither
  10168. * positive or negative polarity is requested, treat this as meaning
  10169. * negative polarity.
  10170. */
  10171. if (!(pipe_config->base.adjusted_mode.flags &
  10172. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10173. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10174. if (!(pipe_config->base.adjusted_mode.flags &
  10175. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10176. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10177. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10178. * plane pixel format and any sink constraints into account. Returns the
  10179. * source plane bpp so that dithering can be selected on mismatches
  10180. * after encoders and crtc also have had their say. */
  10181. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10182. pipe_config);
  10183. if (base_bpp < 0)
  10184. goto fail;
  10185. /*
  10186. * Determine the real pipe dimensions. Note that stereo modes can
  10187. * increase the actual pipe size due to the frame doubling and
  10188. * insertion of additional space for blanks between the frame. This
  10189. * is stored in the crtc timings. We use the requested mode to do this
  10190. * computation to clearly distinguish it from the adjusted mode, which
  10191. * can be changed by the connectors in the below retry loop.
  10192. */
  10193. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10194. &pipe_config->pipe_src_w,
  10195. &pipe_config->pipe_src_h);
  10196. encoder_retry:
  10197. /* Ensure the port clock defaults are reset when retrying. */
  10198. pipe_config->port_clock = 0;
  10199. pipe_config->pixel_multiplier = 1;
  10200. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10201. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10202. CRTC_STEREO_DOUBLE);
  10203. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10204. * adjust it according to limitations or connector properties, and also
  10205. * a chance to reject the mode entirely.
  10206. */
  10207. for_each_connector_in_state(state, connector, connector_state, i) {
  10208. if (connector_state->crtc != crtc)
  10209. continue;
  10210. encoder = to_intel_encoder(connector_state->best_encoder);
  10211. if (!(encoder->compute_config(encoder, pipe_config))) {
  10212. DRM_DEBUG_KMS("Encoder config failure\n");
  10213. goto fail;
  10214. }
  10215. }
  10216. /* Set default port clock if not overwritten by the encoder. Needs to be
  10217. * done afterwards in case the encoder adjusts the mode. */
  10218. if (!pipe_config->port_clock)
  10219. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10220. * pipe_config->pixel_multiplier;
  10221. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10222. if (ret < 0) {
  10223. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10224. goto fail;
  10225. }
  10226. if (ret == RETRY) {
  10227. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10228. ret = -EINVAL;
  10229. goto fail;
  10230. }
  10231. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10232. retry = false;
  10233. goto encoder_retry;
  10234. }
  10235. /* Dithering seems to not pass-through bits correctly when it should, so
  10236. * only enable it on 6bpc panels. */
  10237. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10238. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10239. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10240. fail:
  10241. return ret;
  10242. }
  10243. static void
  10244. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10245. {
  10246. struct drm_crtc *crtc;
  10247. struct drm_crtc_state *crtc_state;
  10248. int i;
  10249. /* Double check state. */
  10250. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10251. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10252. /* Update hwmode for vblank functions */
  10253. if (crtc->state->active)
  10254. crtc->hwmode = crtc->state->adjusted_mode;
  10255. else
  10256. crtc->hwmode.crtc_clock = 0;
  10257. }
  10258. }
  10259. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10260. {
  10261. int diff;
  10262. if (clock1 == clock2)
  10263. return true;
  10264. if (!clock1 || !clock2)
  10265. return false;
  10266. diff = abs(clock1 - clock2);
  10267. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10268. return true;
  10269. return false;
  10270. }
  10271. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10272. list_for_each_entry((intel_crtc), \
  10273. &(dev)->mode_config.crtc_list, \
  10274. base.head) \
  10275. if (mask & (1 <<(intel_crtc)->pipe))
  10276. static bool
  10277. intel_compare_m_n(unsigned int m, unsigned int n,
  10278. unsigned int m2, unsigned int n2,
  10279. bool exact)
  10280. {
  10281. if (m == m2 && n == n2)
  10282. return true;
  10283. if (exact || !m || !n || !m2 || !n2)
  10284. return false;
  10285. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10286. if (m > m2) {
  10287. while (m > m2) {
  10288. m2 <<= 1;
  10289. n2 <<= 1;
  10290. }
  10291. } else if (m < m2) {
  10292. while (m < m2) {
  10293. m <<= 1;
  10294. n <<= 1;
  10295. }
  10296. }
  10297. return m == m2 && n == n2;
  10298. }
  10299. static bool
  10300. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10301. struct intel_link_m_n *m2_n2,
  10302. bool adjust)
  10303. {
  10304. if (m_n->tu == m2_n2->tu &&
  10305. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10306. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10307. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10308. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10309. if (adjust)
  10310. *m2_n2 = *m_n;
  10311. return true;
  10312. }
  10313. return false;
  10314. }
  10315. static bool
  10316. intel_pipe_config_compare(struct drm_device *dev,
  10317. struct intel_crtc_state *current_config,
  10318. struct intel_crtc_state *pipe_config,
  10319. bool adjust)
  10320. {
  10321. bool ret = true;
  10322. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10323. do { \
  10324. if (!adjust) \
  10325. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10326. else \
  10327. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10328. } while (0)
  10329. #define PIPE_CONF_CHECK_X(name) \
  10330. if (current_config->name != pipe_config->name) { \
  10331. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10332. "(expected 0x%08x, found 0x%08x)\n", \
  10333. current_config->name, \
  10334. pipe_config->name); \
  10335. ret = false; \
  10336. }
  10337. #define PIPE_CONF_CHECK_I(name) \
  10338. if (current_config->name != pipe_config->name) { \
  10339. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10340. "(expected %i, found %i)\n", \
  10341. current_config->name, \
  10342. pipe_config->name); \
  10343. ret = false; \
  10344. }
  10345. #define PIPE_CONF_CHECK_M_N(name) \
  10346. if (!intel_compare_link_m_n(&current_config->name, \
  10347. &pipe_config->name,\
  10348. adjust)) { \
  10349. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10350. "(expected tu %i gmch %i/%i link %i/%i, " \
  10351. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10352. current_config->name.tu, \
  10353. current_config->name.gmch_m, \
  10354. current_config->name.gmch_n, \
  10355. current_config->name.link_m, \
  10356. current_config->name.link_n, \
  10357. pipe_config->name.tu, \
  10358. pipe_config->name.gmch_m, \
  10359. pipe_config->name.gmch_n, \
  10360. pipe_config->name.link_m, \
  10361. pipe_config->name.link_n); \
  10362. ret = false; \
  10363. }
  10364. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10365. if (!intel_compare_link_m_n(&current_config->name, \
  10366. &pipe_config->name, adjust) && \
  10367. !intel_compare_link_m_n(&current_config->alt_name, \
  10368. &pipe_config->name, adjust)) { \
  10369. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10370. "(expected tu %i gmch %i/%i link %i/%i, " \
  10371. "or tu %i gmch %i/%i link %i/%i, " \
  10372. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10373. current_config->name.tu, \
  10374. current_config->name.gmch_m, \
  10375. current_config->name.gmch_n, \
  10376. current_config->name.link_m, \
  10377. current_config->name.link_n, \
  10378. current_config->alt_name.tu, \
  10379. current_config->alt_name.gmch_m, \
  10380. current_config->alt_name.gmch_n, \
  10381. current_config->alt_name.link_m, \
  10382. current_config->alt_name.link_n, \
  10383. pipe_config->name.tu, \
  10384. pipe_config->name.gmch_m, \
  10385. pipe_config->name.gmch_n, \
  10386. pipe_config->name.link_m, \
  10387. pipe_config->name.link_n); \
  10388. ret = false; \
  10389. }
  10390. /* This is required for BDW+ where there is only one set of registers for
  10391. * switching between high and low RR.
  10392. * This macro can be used whenever a comparison has to be made between one
  10393. * hw state and multiple sw state variables.
  10394. */
  10395. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10396. if ((current_config->name != pipe_config->name) && \
  10397. (current_config->alt_name != pipe_config->name)) { \
  10398. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10399. "(expected %i or %i, found %i)\n", \
  10400. current_config->name, \
  10401. current_config->alt_name, \
  10402. pipe_config->name); \
  10403. ret = false; \
  10404. }
  10405. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10406. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10407. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10408. "(expected %i, found %i)\n", \
  10409. current_config->name & (mask), \
  10410. pipe_config->name & (mask)); \
  10411. ret = false; \
  10412. }
  10413. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10414. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10415. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10416. "(expected %i, found %i)\n", \
  10417. current_config->name, \
  10418. pipe_config->name); \
  10419. ret = false; \
  10420. }
  10421. #define PIPE_CONF_QUIRK(quirk) \
  10422. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10423. PIPE_CONF_CHECK_I(cpu_transcoder);
  10424. PIPE_CONF_CHECK_I(has_pch_encoder);
  10425. PIPE_CONF_CHECK_I(fdi_lanes);
  10426. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10427. PIPE_CONF_CHECK_I(has_dp_encoder);
  10428. if (INTEL_INFO(dev)->gen < 8) {
  10429. PIPE_CONF_CHECK_M_N(dp_m_n);
  10430. PIPE_CONF_CHECK_I(has_drrs);
  10431. if (current_config->has_drrs)
  10432. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10433. } else
  10434. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10435. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10436. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10437. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10438. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10439. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10440. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10441. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10442. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10443. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10444. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10445. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10446. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10447. PIPE_CONF_CHECK_I(pixel_multiplier);
  10448. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10449. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10450. IS_VALLEYVIEW(dev))
  10451. PIPE_CONF_CHECK_I(limited_color_range);
  10452. PIPE_CONF_CHECK_I(has_infoframe);
  10453. PIPE_CONF_CHECK_I(has_audio);
  10454. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10455. DRM_MODE_FLAG_INTERLACE);
  10456. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10457. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10458. DRM_MODE_FLAG_PHSYNC);
  10459. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10460. DRM_MODE_FLAG_NHSYNC);
  10461. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10462. DRM_MODE_FLAG_PVSYNC);
  10463. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10464. DRM_MODE_FLAG_NVSYNC);
  10465. }
  10466. PIPE_CONF_CHECK_I(pipe_src_w);
  10467. PIPE_CONF_CHECK_I(pipe_src_h);
  10468. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10469. /* pfit ratios are autocomputed by the hw on gen4+ */
  10470. if (INTEL_INFO(dev)->gen < 4)
  10471. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10472. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10473. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10474. if (current_config->pch_pfit.enabled) {
  10475. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10476. PIPE_CONF_CHECK_I(pch_pfit.size);
  10477. }
  10478. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10479. /* BDW+ don't expose a synchronous way to read the state */
  10480. if (IS_HASWELL(dev))
  10481. PIPE_CONF_CHECK_I(ips_enabled);
  10482. PIPE_CONF_CHECK_I(double_wide);
  10483. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10484. PIPE_CONF_CHECK_I(shared_dpll);
  10485. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10486. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10487. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10488. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10489. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10490. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10491. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10492. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10493. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10494. PIPE_CONF_CHECK_I(pipe_bpp);
  10495. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10496. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10497. #undef PIPE_CONF_CHECK_X
  10498. #undef PIPE_CONF_CHECK_I
  10499. #undef PIPE_CONF_CHECK_I_ALT
  10500. #undef PIPE_CONF_CHECK_FLAGS
  10501. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10502. #undef PIPE_CONF_QUIRK
  10503. #undef INTEL_ERR_OR_DBG_KMS
  10504. return ret;
  10505. }
  10506. static void check_wm_state(struct drm_device *dev)
  10507. {
  10508. struct drm_i915_private *dev_priv = dev->dev_private;
  10509. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10510. struct intel_crtc *intel_crtc;
  10511. int plane;
  10512. if (INTEL_INFO(dev)->gen < 9)
  10513. return;
  10514. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10515. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10516. for_each_intel_crtc(dev, intel_crtc) {
  10517. struct skl_ddb_entry *hw_entry, *sw_entry;
  10518. const enum pipe pipe = intel_crtc->pipe;
  10519. if (!intel_crtc->active)
  10520. continue;
  10521. /* planes */
  10522. for_each_plane(dev_priv, pipe, plane) {
  10523. hw_entry = &hw_ddb.plane[pipe][plane];
  10524. sw_entry = &sw_ddb->plane[pipe][plane];
  10525. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10526. continue;
  10527. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10528. "(expected (%u,%u), found (%u,%u))\n",
  10529. pipe_name(pipe), plane + 1,
  10530. sw_entry->start, sw_entry->end,
  10531. hw_entry->start, hw_entry->end);
  10532. }
  10533. /* cursor */
  10534. hw_entry = &hw_ddb.cursor[pipe];
  10535. sw_entry = &sw_ddb->cursor[pipe];
  10536. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10537. continue;
  10538. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10539. "(expected (%u,%u), found (%u,%u))\n",
  10540. pipe_name(pipe),
  10541. sw_entry->start, sw_entry->end,
  10542. hw_entry->start, hw_entry->end);
  10543. }
  10544. }
  10545. static void
  10546. check_connector_state(struct drm_device *dev,
  10547. struct drm_atomic_state *old_state)
  10548. {
  10549. struct drm_connector_state *old_conn_state;
  10550. struct drm_connector *connector;
  10551. int i;
  10552. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10553. struct drm_encoder *encoder = connector->encoder;
  10554. struct drm_connector_state *state = connector->state;
  10555. /* This also checks the encoder/connector hw state with the
  10556. * ->get_hw_state callbacks. */
  10557. intel_connector_check_state(to_intel_connector(connector));
  10558. I915_STATE_WARN(state->best_encoder != encoder,
  10559. "connector's atomic encoder doesn't match legacy encoder\n");
  10560. }
  10561. }
  10562. static void
  10563. check_encoder_state(struct drm_device *dev)
  10564. {
  10565. struct intel_encoder *encoder;
  10566. struct intel_connector *connector;
  10567. for_each_intel_encoder(dev, encoder) {
  10568. bool enabled = false;
  10569. enum pipe pipe;
  10570. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10571. encoder->base.base.id,
  10572. encoder->base.name);
  10573. for_each_intel_connector(dev, connector) {
  10574. if (connector->base.state->best_encoder != &encoder->base)
  10575. continue;
  10576. enabled = true;
  10577. I915_STATE_WARN(connector->base.state->crtc !=
  10578. encoder->base.crtc,
  10579. "connector's crtc doesn't match encoder crtc\n");
  10580. }
  10581. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10582. "encoder's enabled state mismatch "
  10583. "(expected %i, found %i)\n",
  10584. !!encoder->base.crtc, enabled);
  10585. if (!encoder->base.crtc) {
  10586. bool active;
  10587. active = encoder->get_hw_state(encoder, &pipe);
  10588. I915_STATE_WARN(active,
  10589. "encoder detached but still enabled on pipe %c.\n",
  10590. pipe_name(pipe));
  10591. }
  10592. }
  10593. }
  10594. static void
  10595. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10596. {
  10597. struct drm_i915_private *dev_priv = dev->dev_private;
  10598. struct intel_encoder *encoder;
  10599. struct drm_crtc_state *old_crtc_state;
  10600. struct drm_crtc *crtc;
  10601. int i;
  10602. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10604. struct intel_crtc_state *pipe_config, *sw_config;
  10605. bool active;
  10606. if (!needs_modeset(crtc->state))
  10607. continue;
  10608. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10609. pipe_config = to_intel_crtc_state(old_crtc_state);
  10610. memset(pipe_config, 0, sizeof(*pipe_config));
  10611. pipe_config->base.crtc = crtc;
  10612. pipe_config->base.state = old_state;
  10613. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10614. crtc->base.id);
  10615. active = dev_priv->display.get_pipe_config(intel_crtc,
  10616. pipe_config);
  10617. /* hw state is inconsistent with the pipe quirk */
  10618. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10619. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10620. active = crtc->state->active;
  10621. I915_STATE_WARN(crtc->state->active != active,
  10622. "crtc active state doesn't match with hw state "
  10623. "(expected %i, found %i)\n", crtc->state->active, active);
  10624. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10625. "transitional active state does not match atomic hw state "
  10626. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10627. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10628. enum pipe pipe;
  10629. active = encoder->get_hw_state(encoder, &pipe);
  10630. I915_STATE_WARN(active != crtc->state->active,
  10631. "[ENCODER:%i] active %i with crtc active %i\n",
  10632. encoder->base.base.id, active, crtc->state->active);
  10633. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10634. "Encoder connected to wrong pipe %c\n",
  10635. pipe_name(pipe));
  10636. if (active)
  10637. encoder->get_config(encoder, pipe_config);
  10638. }
  10639. if (!crtc->state->active)
  10640. continue;
  10641. sw_config = to_intel_crtc_state(crtc->state);
  10642. if (!intel_pipe_config_compare(dev, sw_config,
  10643. pipe_config, false)) {
  10644. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10645. intel_dump_pipe_config(intel_crtc, pipe_config,
  10646. "[hw state]");
  10647. intel_dump_pipe_config(intel_crtc, sw_config,
  10648. "[sw state]");
  10649. }
  10650. }
  10651. }
  10652. static void
  10653. check_shared_dpll_state(struct drm_device *dev)
  10654. {
  10655. struct drm_i915_private *dev_priv = dev->dev_private;
  10656. struct intel_crtc *crtc;
  10657. struct intel_dpll_hw_state dpll_hw_state;
  10658. int i;
  10659. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10660. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10661. int enabled_crtcs = 0, active_crtcs = 0;
  10662. bool active;
  10663. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10664. DRM_DEBUG_KMS("%s\n", pll->name);
  10665. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10666. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10667. "more active pll users than references: %i vs %i\n",
  10668. pll->active, hweight32(pll->config.crtc_mask));
  10669. I915_STATE_WARN(pll->active && !pll->on,
  10670. "pll in active use but not on in sw tracking\n");
  10671. I915_STATE_WARN(pll->on && !pll->active,
  10672. "pll in on but not on in use in sw tracking\n");
  10673. I915_STATE_WARN(pll->on != active,
  10674. "pll on state mismatch (expected %i, found %i)\n",
  10675. pll->on, active);
  10676. for_each_intel_crtc(dev, crtc) {
  10677. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10678. enabled_crtcs++;
  10679. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10680. active_crtcs++;
  10681. }
  10682. I915_STATE_WARN(pll->active != active_crtcs,
  10683. "pll active crtcs mismatch (expected %i, found %i)\n",
  10684. pll->active, active_crtcs);
  10685. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10686. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10687. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10688. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10689. sizeof(dpll_hw_state)),
  10690. "pll hw state mismatch\n");
  10691. }
  10692. }
  10693. static void
  10694. intel_modeset_check_state(struct drm_device *dev,
  10695. struct drm_atomic_state *old_state)
  10696. {
  10697. check_wm_state(dev);
  10698. check_connector_state(dev, old_state);
  10699. check_encoder_state(dev);
  10700. check_crtc_state(dev, old_state);
  10701. check_shared_dpll_state(dev);
  10702. }
  10703. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10704. int dotclock)
  10705. {
  10706. /*
  10707. * FDI already provided one idea for the dotclock.
  10708. * Yell if the encoder disagrees.
  10709. */
  10710. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10711. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10712. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10713. }
  10714. static void update_scanline_offset(struct intel_crtc *crtc)
  10715. {
  10716. struct drm_device *dev = crtc->base.dev;
  10717. /*
  10718. * The scanline counter increments at the leading edge of hsync.
  10719. *
  10720. * On most platforms it starts counting from vtotal-1 on the
  10721. * first active line. That means the scanline counter value is
  10722. * always one less than what we would expect. Ie. just after
  10723. * start of vblank, which also occurs at start of hsync (on the
  10724. * last active line), the scanline counter will read vblank_start-1.
  10725. *
  10726. * On gen2 the scanline counter starts counting from 1 instead
  10727. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10728. * to keep the value positive), instead of adding one.
  10729. *
  10730. * On HSW+ the behaviour of the scanline counter depends on the output
  10731. * type. For DP ports it behaves like most other platforms, but on HDMI
  10732. * there's an extra 1 line difference. So we need to add two instead of
  10733. * one to the value.
  10734. */
  10735. if (IS_GEN2(dev)) {
  10736. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10737. int vtotal;
  10738. vtotal = mode->crtc_vtotal;
  10739. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10740. vtotal /= 2;
  10741. crtc->scanline_offset = vtotal - 1;
  10742. } else if (HAS_DDI(dev) &&
  10743. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10744. crtc->scanline_offset = 2;
  10745. } else
  10746. crtc->scanline_offset = 1;
  10747. }
  10748. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10749. {
  10750. struct drm_device *dev = state->dev;
  10751. struct drm_i915_private *dev_priv = to_i915(dev);
  10752. struct intel_shared_dpll_config *shared_dpll = NULL;
  10753. struct intel_crtc *intel_crtc;
  10754. struct intel_crtc_state *intel_crtc_state;
  10755. struct drm_crtc *crtc;
  10756. struct drm_crtc_state *crtc_state;
  10757. int i;
  10758. if (!dev_priv->display.crtc_compute_clock)
  10759. return;
  10760. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10761. int dpll;
  10762. intel_crtc = to_intel_crtc(crtc);
  10763. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10764. dpll = intel_crtc_state->shared_dpll;
  10765. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10766. continue;
  10767. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10768. if (!shared_dpll)
  10769. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10770. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10771. }
  10772. }
  10773. /*
  10774. * This implements the workaround described in the "notes" section of the mode
  10775. * set sequence documentation. When going from no pipes or single pipe to
  10776. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10777. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10778. */
  10779. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10780. {
  10781. struct drm_crtc_state *crtc_state;
  10782. struct intel_crtc *intel_crtc;
  10783. struct drm_crtc *crtc;
  10784. struct intel_crtc_state *first_crtc_state = NULL;
  10785. struct intel_crtc_state *other_crtc_state = NULL;
  10786. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10787. int i;
  10788. /* look at all crtc's that are going to be enabled in during modeset */
  10789. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10790. intel_crtc = to_intel_crtc(crtc);
  10791. if (!crtc_state->active || !needs_modeset(crtc_state))
  10792. continue;
  10793. if (first_crtc_state) {
  10794. other_crtc_state = to_intel_crtc_state(crtc_state);
  10795. break;
  10796. } else {
  10797. first_crtc_state = to_intel_crtc_state(crtc_state);
  10798. first_pipe = intel_crtc->pipe;
  10799. }
  10800. }
  10801. /* No workaround needed? */
  10802. if (!first_crtc_state)
  10803. return 0;
  10804. /* w/a possibly needed, check how many crtc's are already enabled. */
  10805. for_each_intel_crtc(state->dev, intel_crtc) {
  10806. struct intel_crtc_state *pipe_config;
  10807. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10808. if (IS_ERR(pipe_config))
  10809. return PTR_ERR(pipe_config);
  10810. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10811. if (!pipe_config->base.active ||
  10812. needs_modeset(&pipe_config->base))
  10813. continue;
  10814. /* 2 or more enabled crtcs means no need for w/a */
  10815. if (enabled_pipe != INVALID_PIPE)
  10816. return 0;
  10817. enabled_pipe = intel_crtc->pipe;
  10818. }
  10819. if (enabled_pipe != INVALID_PIPE)
  10820. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10821. else if (other_crtc_state)
  10822. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10823. return 0;
  10824. }
  10825. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10826. {
  10827. struct drm_crtc *crtc;
  10828. struct drm_crtc_state *crtc_state;
  10829. int ret = 0;
  10830. /* add all active pipes to the state */
  10831. for_each_crtc(state->dev, crtc) {
  10832. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10833. if (IS_ERR(crtc_state))
  10834. return PTR_ERR(crtc_state);
  10835. if (!crtc_state->active || needs_modeset(crtc_state))
  10836. continue;
  10837. crtc_state->mode_changed = true;
  10838. ret = drm_atomic_add_affected_connectors(state, crtc);
  10839. if (ret)
  10840. break;
  10841. ret = drm_atomic_add_affected_planes(state, crtc);
  10842. if (ret)
  10843. break;
  10844. }
  10845. return ret;
  10846. }
  10847. static int intel_modeset_checks(struct drm_atomic_state *state)
  10848. {
  10849. struct drm_device *dev = state->dev;
  10850. struct drm_i915_private *dev_priv = dev->dev_private;
  10851. int ret;
  10852. if (!check_digital_port_conflicts(state)) {
  10853. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10854. return -EINVAL;
  10855. }
  10856. /*
  10857. * See if the config requires any additional preparation, e.g.
  10858. * to adjust global state with pipes off. We need to do this
  10859. * here so we can get the modeset_pipe updated config for the new
  10860. * mode set on this crtc. For other crtcs we need to use the
  10861. * adjusted_mode bits in the crtc directly.
  10862. */
  10863. if (dev_priv->display.modeset_calc_cdclk) {
  10864. unsigned int cdclk;
  10865. ret = dev_priv->display.modeset_calc_cdclk(state);
  10866. cdclk = to_intel_atomic_state(state)->cdclk;
  10867. if (!ret && cdclk != dev_priv->cdclk_freq)
  10868. ret = intel_modeset_all_pipes(state);
  10869. if (ret < 0)
  10870. return ret;
  10871. } else
  10872. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10873. intel_modeset_clear_plls(state);
  10874. if (IS_HASWELL(dev))
  10875. return haswell_mode_set_planes_workaround(state);
  10876. return 0;
  10877. }
  10878. /**
  10879. * intel_atomic_check - validate state object
  10880. * @dev: drm device
  10881. * @state: state to validate
  10882. */
  10883. static int intel_atomic_check(struct drm_device *dev,
  10884. struct drm_atomic_state *state)
  10885. {
  10886. struct drm_crtc *crtc;
  10887. struct drm_crtc_state *crtc_state;
  10888. int ret, i;
  10889. bool any_ms = false;
  10890. ret = drm_atomic_helper_check_modeset(dev, state);
  10891. if (ret)
  10892. return ret;
  10893. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10894. struct intel_crtc_state *pipe_config =
  10895. to_intel_crtc_state(crtc_state);
  10896. /* Catch I915_MODE_FLAG_INHERITED */
  10897. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10898. crtc_state->mode_changed = true;
  10899. if (!crtc_state->enable) {
  10900. if (needs_modeset(crtc_state))
  10901. any_ms = true;
  10902. continue;
  10903. }
  10904. if (!needs_modeset(crtc_state))
  10905. continue;
  10906. /* FIXME: For only active_changed we shouldn't need to do any
  10907. * state recomputation at all. */
  10908. ret = drm_atomic_add_affected_connectors(state, crtc);
  10909. if (ret)
  10910. return ret;
  10911. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10912. if (ret)
  10913. return ret;
  10914. if (i915.fastboot &&
  10915. intel_pipe_config_compare(state->dev,
  10916. to_intel_crtc_state(crtc->state),
  10917. pipe_config, true)) {
  10918. crtc_state->mode_changed = false;
  10919. }
  10920. if (needs_modeset(crtc_state)) {
  10921. any_ms = true;
  10922. ret = drm_atomic_add_affected_planes(state, crtc);
  10923. if (ret)
  10924. return ret;
  10925. }
  10926. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10927. needs_modeset(crtc_state) ?
  10928. "[modeset]" : "[fastset]");
  10929. }
  10930. if (any_ms) {
  10931. ret = intel_modeset_checks(state);
  10932. if (ret)
  10933. return ret;
  10934. } else
  10935. to_intel_atomic_state(state)->cdclk =
  10936. to_i915(state->dev)->cdclk_freq;
  10937. return drm_atomic_helper_check_planes(state->dev, state);
  10938. }
  10939. /**
  10940. * intel_atomic_commit - commit validated state object
  10941. * @dev: DRM device
  10942. * @state: the top-level driver state object
  10943. * @async: asynchronous commit
  10944. *
  10945. * This function commits a top-level state object that has been validated
  10946. * with drm_atomic_helper_check().
  10947. *
  10948. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  10949. * we can only handle plane-related operations and do not yet support
  10950. * asynchronous commit.
  10951. *
  10952. * RETURNS
  10953. * Zero for success or -errno.
  10954. */
  10955. static int intel_atomic_commit(struct drm_device *dev,
  10956. struct drm_atomic_state *state,
  10957. bool async)
  10958. {
  10959. struct drm_i915_private *dev_priv = dev->dev_private;
  10960. struct drm_crtc *crtc;
  10961. struct drm_crtc_state *crtc_state;
  10962. int ret = 0;
  10963. int i;
  10964. bool any_ms = false;
  10965. if (async) {
  10966. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  10967. return -EINVAL;
  10968. }
  10969. ret = drm_atomic_helper_prepare_planes(dev, state);
  10970. if (ret)
  10971. return ret;
  10972. drm_atomic_helper_swap_state(dev, state);
  10973. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10974. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10975. if (!needs_modeset(crtc->state))
  10976. continue;
  10977. any_ms = true;
  10978. intel_pre_plane_update(intel_crtc);
  10979. if (crtc_state->active) {
  10980. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  10981. dev_priv->display.crtc_disable(crtc);
  10982. intel_crtc->active = false;
  10983. intel_disable_shared_dpll(intel_crtc);
  10984. }
  10985. }
  10986. /* Only after disabling all output pipelines that will be changed can we
  10987. * update the the output configuration. */
  10988. intel_modeset_update_crtc_state(state);
  10989. if (any_ms) {
  10990. intel_shared_dpll_commit(state);
  10991. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10992. modeset_update_crtc_power_domains(state);
  10993. }
  10994. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10995. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10996. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10997. bool modeset = needs_modeset(crtc->state);
  10998. if (modeset && crtc->state->active) {
  10999. update_scanline_offset(to_intel_crtc(crtc));
  11000. dev_priv->display.crtc_enable(crtc);
  11001. }
  11002. if (!modeset)
  11003. intel_pre_plane_update(intel_crtc);
  11004. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11005. intel_post_plane_update(intel_crtc);
  11006. }
  11007. /* FIXME: add subpixel order */
  11008. drm_atomic_helper_wait_for_vblanks(dev, state);
  11009. drm_atomic_helper_cleanup_planes(dev, state);
  11010. if (any_ms)
  11011. intel_modeset_check_state(dev, state);
  11012. drm_atomic_state_free(state);
  11013. return 0;
  11014. }
  11015. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11016. {
  11017. struct drm_device *dev = crtc->dev;
  11018. struct drm_atomic_state *state;
  11019. struct drm_crtc_state *crtc_state;
  11020. int ret;
  11021. state = drm_atomic_state_alloc(dev);
  11022. if (!state) {
  11023. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11024. crtc->base.id);
  11025. return;
  11026. }
  11027. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11028. retry:
  11029. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11030. ret = PTR_ERR_OR_ZERO(crtc_state);
  11031. if (!ret) {
  11032. if (!crtc_state->active)
  11033. goto out;
  11034. crtc_state->mode_changed = true;
  11035. ret = drm_atomic_commit(state);
  11036. }
  11037. if (ret == -EDEADLK) {
  11038. drm_atomic_state_clear(state);
  11039. drm_modeset_backoff(state->acquire_ctx);
  11040. goto retry;
  11041. }
  11042. if (ret)
  11043. out:
  11044. drm_atomic_state_free(state);
  11045. }
  11046. #undef for_each_intel_crtc_masked
  11047. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11048. .gamma_set = intel_crtc_gamma_set,
  11049. .set_config = drm_atomic_helper_set_config,
  11050. .destroy = intel_crtc_destroy,
  11051. .page_flip = intel_crtc_page_flip,
  11052. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11053. .atomic_destroy_state = intel_crtc_destroy_state,
  11054. };
  11055. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11056. struct intel_shared_dpll *pll,
  11057. struct intel_dpll_hw_state *hw_state)
  11058. {
  11059. uint32_t val;
  11060. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11061. return false;
  11062. val = I915_READ(PCH_DPLL(pll->id));
  11063. hw_state->dpll = val;
  11064. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11065. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11066. return val & DPLL_VCO_ENABLE;
  11067. }
  11068. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11069. struct intel_shared_dpll *pll)
  11070. {
  11071. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11072. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11073. }
  11074. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11075. struct intel_shared_dpll *pll)
  11076. {
  11077. /* PCH refclock must be enabled first */
  11078. ibx_assert_pch_refclk_enabled(dev_priv);
  11079. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11080. /* Wait for the clocks to stabilize. */
  11081. POSTING_READ(PCH_DPLL(pll->id));
  11082. udelay(150);
  11083. /* The pixel multiplier can only be updated once the
  11084. * DPLL is enabled and the clocks are stable.
  11085. *
  11086. * So write it again.
  11087. */
  11088. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11089. POSTING_READ(PCH_DPLL(pll->id));
  11090. udelay(200);
  11091. }
  11092. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11093. struct intel_shared_dpll *pll)
  11094. {
  11095. struct drm_device *dev = dev_priv->dev;
  11096. struct intel_crtc *crtc;
  11097. /* Make sure no transcoder isn't still depending on us. */
  11098. for_each_intel_crtc(dev, crtc) {
  11099. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11100. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11101. }
  11102. I915_WRITE(PCH_DPLL(pll->id), 0);
  11103. POSTING_READ(PCH_DPLL(pll->id));
  11104. udelay(200);
  11105. }
  11106. static char *ibx_pch_dpll_names[] = {
  11107. "PCH DPLL A",
  11108. "PCH DPLL B",
  11109. };
  11110. static void ibx_pch_dpll_init(struct drm_device *dev)
  11111. {
  11112. struct drm_i915_private *dev_priv = dev->dev_private;
  11113. int i;
  11114. dev_priv->num_shared_dpll = 2;
  11115. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11116. dev_priv->shared_dplls[i].id = i;
  11117. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11118. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11119. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11120. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11121. dev_priv->shared_dplls[i].get_hw_state =
  11122. ibx_pch_dpll_get_hw_state;
  11123. }
  11124. }
  11125. static void intel_shared_dpll_init(struct drm_device *dev)
  11126. {
  11127. struct drm_i915_private *dev_priv = dev->dev_private;
  11128. intel_update_cdclk(dev);
  11129. if (HAS_DDI(dev))
  11130. intel_ddi_pll_init(dev);
  11131. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11132. ibx_pch_dpll_init(dev);
  11133. else
  11134. dev_priv->num_shared_dpll = 0;
  11135. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11136. }
  11137. /**
  11138. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11139. * @plane: drm plane to prepare for
  11140. * @fb: framebuffer to prepare for presentation
  11141. *
  11142. * Prepares a framebuffer for usage on a display plane. Generally this
  11143. * involves pinning the underlying object and updating the frontbuffer tracking
  11144. * bits. Some older platforms need special physical address handling for
  11145. * cursor planes.
  11146. *
  11147. * Returns 0 on success, negative error code on failure.
  11148. */
  11149. int
  11150. intel_prepare_plane_fb(struct drm_plane *plane,
  11151. struct drm_framebuffer *fb,
  11152. const struct drm_plane_state *new_state)
  11153. {
  11154. struct drm_device *dev = plane->dev;
  11155. struct intel_plane *intel_plane = to_intel_plane(plane);
  11156. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11157. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11158. int ret = 0;
  11159. if (!obj)
  11160. return 0;
  11161. mutex_lock(&dev->struct_mutex);
  11162. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11163. INTEL_INFO(dev)->cursor_needs_physical) {
  11164. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11165. ret = i915_gem_object_attach_phys(obj, align);
  11166. if (ret)
  11167. DRM_DEBUG_KMS("failed to attach phys object\n");
  11168. } else {
  11169. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11170. }
  11171. if (ret == 0)
  11172. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11173. mutex_unlock(&dev->struct_mutex);
  11174. return ret;
  11175. }
  11176. /**
  11177. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11178. * @plane: drm plane to clean up for
  11179. * @fb: old framebuffer that was on plane
  11180. *
  11181. * Cleans up a framebuffer that has just been removed from a plane.
  11182. */
  11183. void
  11184. intel_cleanup_plane_fb(struct drm_plane *plane,
  11185. struct drm_framebuffer *fb,
  11186. const struct drm_plane_state *old_state)
  11187. {
  11188. struct drm_device *dev = plane->dev;
  11189. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11190. if (WARN_ON(!obj))
  11191. return;
  11192. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11193. !INTEL_INFO(dev)->cursor_needs_physical) {
  11194. mutex_lock(&dev->struct_mutex);
  11195. intel_unpin_fb_obj(fb, old_state);
  11196. mutex_unlock(&dev->struct_mutex);
  11197. }
  11198. }
  11199. int
  11200. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11201. {
  11202. int max_scale;
  11203. struct drm_device *dev;
  11204. struct drm_i915_private *dev_priv;
  11205. int crtc_clock, cdclk;
  11206. if (!intel_crtc || !crtc_state)
  11207. return DRM_PLANE_HELPER_NO_SCALING;
  11208. dev = intel_crtc->base.dev;
  11209. dev_priv = dev->dev_private;
  11210. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11211. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11212. if (!crtc_clock || !cdclk)
  11213. return DRM_PLANE_HELPER_NO_SCALING;
  11214. /*
  11215. * skl max scale is lower of:
  11216. * close to 3 but not 3, -1 is for that purpose
  11217. * or
  11218. * cdclk/crtc_clock
  11219. */
  11220. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11221. return max_scale;
  11222. }
  11223. static int
  11224. intel_check_primary_plane(struct drm_plane *plane,
  11225. struct intel_crtc_state *crtc_state,
  11226. struct intel_plane_state *state)
  11227. {
  11228. struct drm_crtc *crtc = state->base.crtc;
  11229. struct drm_framebuffer *fb = state->base.fb;
  11230. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11231. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11232. bool can_position = false;
  11233. /* use scaler when colorkey is not required */
  11234. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11235. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11236. min_scale = 1;
  11237. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11238. can_position = true;
  11239. }
  11240. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11241. &state->dst, &state->clip,
  11242. min_scale, max_scale,
  11243. can_position, true,
  11244. &state->visible);
  11245. }
  11246. static void
  11247. intel_commit_primary_plane(struct drm_plane *plane,
  11248. struct intel_plane_state *state)
  11249. {
  11250. struct drm_crtc *crtc = state->base.crtc;
  11251. struct drm_framebuffer *fb = state->base.fb;
  11252. struct drm_device *dev = plane->dev;
  11253. struct drm_i915_private *dev_priv = dev->dev_private;
  11254. struct intel_crtc *intel_crtc;
  11255. struct drm_rect *src = &state->src;
  11256. crtc = crtc ? crtc : plane->crtc;
  11257. intel_crtc = to_intel_crtc(crtc);
  11258. plane->fb = fb;
  11259. crtc->x = src->x1 >> 16;
  11260. crtc->y = src->y1 >> 16;
  11261. if (!crtc->state->active)
  11262. return;
  11263. if (state->visible)
  11264. /* FIXME: kill this fastboot hack */
  11265. intel_update_pipe_size(intel_crtc);
  11266. dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
  11267. }
  11268. static void
  11269. intel_disable_primary_plane(struct drm_plane *plane,
  11270. struct drm_crtc *crtc)
  11271. {
  11272. struct drm_device *dev = plane->dev;
  11273. struct drm_i915_private *dev_priv = dev->dev_private;
  11274. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11275. }
  11276. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11277. struct drm_crtc_state *old_crtc_state)
  11278. {
  11279. struct drm_device *dev = crtc->dev;
  11280. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11281. if (intel_crtc->atomic.update_wm_pre)
  11282. intel_update_watermarks(crtc);
  11283. /* Perform vblank evasion around commit operation */
  11284. if (crtc->state->active)
  11285. intel_pipe_update_start(intel_crtc, &intel_crtc->start_vbl_count);
  11286. if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
  11287. skl_detach_scalers(intel_crtc);
  11288. }
  11289. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11290. struct drm_crtc_state *old_crtc_state)
  11291. {
  11292. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11293. if (crtc->state->active)
  11294. intel_pipe_update_end(intel_crtc, intel_crtc->start_vbl_count);
  11295. }
  11296. /**
  11297. * intel_plane_destroy - destroy a plane
  11298. * @plane: plane to destroy
  11299. *
  11300. * Common destruction function for all types of planes (primary, cursor,
  11301. * sprite).
  11302. */
  11303. void intel_plane_destroy(struct drm_plane *plane)
  11304. {
  11305. struct intel_plane *intel_plane = to_intel_plane(plane);
  11306. drm_plane_cleanup(plane);
  11307. kfree(intel_plane);
  11308. }
  11309. const struct drm_plane_funcs intel_plane_funcs = {
  11310. .update_plane = drm_atomic_helper_update_plane,
  11311. .disable_plane = drm_atomic_helper_disable_plane,
  11312. .destroy = intel_plane_destroy,
  11313. .set_property = drm_atomic_helper_plane_set_property,
  11314. .atomic_get_property = intel_plane_atomic_get_property,
  11315. .atomic_set_property = intel_plane_atomic_set_property,
  11316. .atomic_duplicate_state = intel_plane_duplicate_state,
  11317. .atomic_destroy_state = intel_plane_destroy_state,
  11318. };
  11319. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11320. int pipe)
  11321. {
  11322. struct intel_plane *primary;
  11323. struct intel_plane_state *state;
  11324. const uint32_t *intel_primary_formats;
  11325. unsigned int num_formats;
  11326. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11327. if (primary == NULL)
  11328. return NULL;
  11329. state = intel_create_plane_state(&primary->base);
  11330. if (!state) {
  11331. kfree(primary);
  11332. return NULL;
  11333. }
  11334. primary->base.state = &state->base;
  11335. primary->can_scale = false;
  11336. primary->max_downscale = 1;
  11337. if (INTEL_INFO(dev)->gen >= 9) {
  11338. primary->can_scale = true;
  11339. state->scaler_id = -1;
  11340. }
  11341. primary->pipe = pipe;
  11342. primary->plane = pipe;
  11343. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11344. primary->check_plane = intel_check_primary_plane;
  11345. primary->commit_plane = intel_commit_primary_plane;
  11346. primary->disable_plane = intel_disable_primary_plane;
  11347. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11348. primary->plane = !pipe;
  11349. if (INTEL_INFO(dev)->gen >= 9) {
  11350. intel_primary_formats = skl_primary_formats;
  11351. num_formats = ARRAY_SIZE(skl_primary_formats);
  11352. } else if (INTEL_INFO(dev)->gen >= 4) {
  11353. intel_primary_formats = i965_primary_formats;
  11354. num_formats = ARRAY_SIZE(i965_primary_formats);
  11355. } else {
  11356. intel_primary_formats = i8xx_primary_formats;
  11357. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11358. }
  11359. drm_universal_plane_init(dev, &primary->base, 0,
  11360. &intel_plane_funcs,
  11361. intel_primary_formats, num_formats,
  11362. DRM_PLANE_TYPE_PRIMARY);
  11363. if (INTEL_INFO(dev)->gen >= 4)
  11364. intel_create_rotation_property(dev, primary);
  11365. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11366. return &primary->base;
  11367. }
  11368. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11369. {
  11370. if (!dev->mode_config.rotation_property) {
  11371. unsigned long flags = BIT(DRM_ROTATE_0) |
  11372. BIT(DRM_ROTATE_180);
  11373. if (INTEL_INFO(dev)->gen >= 9)
  11374. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11375. dev->mode_config.rotation_property =
  11376. drm_mode_create_rotation_property(dev, flags);
  11377. }
  11378. if (dev->mode_config.rotation_property)
  11379. drm_object_attach_property(&plane->base.base,
  11380. dev->mode_config.rotation_property,
  11381. plane->base.state->rotation);
  11382. }
  11383. static int
  11384. intel_check_cursor_plane(struct drm_plane *plane,
  11385. struct intel_crtc_state *crtc_state,
  11386. struct intel_plane_state *state)
  11387. {
  11388. struct drm_crtc *crtc = crtc_state->base.crtc;
  11389. struct drm_framebuffer *fb = state->base.fb;
  11390. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11391. unsigned stride;
  11392. int ret;
  11393. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11394. &state->dst, &state->clip,
  11395. DRM_PLANE_HELPER_NO_SCALING,
  11396. DRM_PLANE_HELPER_NO_SCALING,
  11397. true, true, &state->visible);
  11398. if (ret)
  11399. return ret;
  11400. /* if we want to turn off the cursor ignore width and height */
  11401. if (!obj)
  11402. return 0;
  11403. /* Check for which cursor types we support */
  11404. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11405. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11406. state->base.crtc_w, state->base.crtc_h);
  11407. return -EINVAL;
  11408. }
  11409. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11410. if (obj->base.size < stride * state->base.crtc_h) {
  11411. DRM_DEBUG_KMS("buffer is too small\n");
  11412. return -ENOMEM;
  11413. }
  11414. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11415. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11416. return -EINVAL;
  11417. }
  11418. return 0;
  11419. }
  11420. static void
  11421. intel_disable_cursor_plane(struct drm_plane *plane,
  11422. struct drm_crtc *crtc)
  11423. {
  11424. intel_crtc_update_cursor(crtc, false);
  11425. }
  11426. static void
  11427. intel_commit_cursor_plane(struct drm_plane *plane,
  11428. struct intel_plane_state *state)
  11429. {
  11430. struct drm_crtc *crtc = state->base.crtc;
  11431. struct drm_device *dev = plane->dev;
  11432. struct intel_crtc *intel_crtc;
  11433. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11434. uint32_t addr;
  11435. crtc = crtc ? crtc : plane->crtc;
  11436. intel_crtc = to_intel_crtc(crtc);
  11437. plane->fb = state->base.fb;
  11438. crtc->cursor_x = state->base.crtc_x;
  11439. crtc->cursor_y = state->base.crtc_y;
  11440. if (intel_crtc->cursor_bo == obj)
  11441. goto update;
  11442. if (!obj)
  11443. addr = 0;
  11444. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11445. addr = i915_gem_obj_ggtt_offset(obj);
  11446. else
  11447. addr = obj->phys_handle->busaddr;
  11448. intel_crtc->cursor_addr = addr;
  11449. intel_crtc->cursor_bo = obj;
  11450. update:
  11451. if (crtc->state->active)
  11452. intel_crtc_update_cursor(crtc, state->visible);
  11453. }
  11454. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11455. int pipe)
  11456. {
  11457. struct intel_plane *cursor;
  11458. struct intel_plane_state *state;
  11459. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11460. if (cursor == NULL)
  11461. return NULL;
  11462. state = intel_create_plane_state(&cursor->base);
  11463. if (!state) {
  11464. kfree(cursor);
  11465. return NULL;
  11466. }
  11467. cursor->base.state = &state->base;
  11468. cursor->can_scale = false;
  11469. cursor->max_downscale = 1;
  11470. cursor->pipe = pipe;
  11471. cursor->plane = pipe;
  11472. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11473. cursor->check_plane = intel_check_cursor_plane;
  11474. cursor->commit_plane = intel_commit_cursor_plane;
  11475. cursor->disable_plane = intel_disable_cursor_plane;
  11476. drm_universal_plane_init(dev, &cursor->base, 0,
  11477. &intel_plane_funcs,
  11478. intel_cursor_formats,
  11479. ARRAY_SIZE(intel_cursor_formats),
  11480. DRM_PLANE_TYPE_CURSOR);
  11481. if (INTEL_INFO(dev)->gen >= 4) {
  11482. if (!dev->mode_config.rotation_property)
  11483. dev->mode_config.rotation_property =
  11484. drm_mode_create_rotation_property(dev,
  11485. BIT(DRM_ROTATE_0) |
  11486. BIT(DRM_ROTATE_180));
  11487. if (dev->mode_config.rotation_property)
  11488. drm_object_attach_property(&cursor->base.base,
  11489. dev->mode_config.rotation_property,
  11490. state->base.rotation);
  11491. }
  11492. if (INTEL_INFO(dev)->gen >=9)
  11493. state->scaler_id = -1;
  11494. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11495. return &cursor->base;
  11496. }
  11497. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11498. struct intel_crtc_state *crtc_state)
  11499. {
  11500. int i;
  11501. struct intel_scaler *intel_scaler;
  11502. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11503. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11504. intel_scaler = &scaler_state->scalers[i];
  11505. intel_scaler->in_use = 0;
  11506. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11507. }
  11508. scaler_state->scaler_id = -1;
  11509. }
  11510. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11511. {
  11512. struct drm_i915_private *dev_priv = dev->dev_private;
  11513. struct intel_crtc *intel_crtc;
  11514. struct intel_crtc_state *crtc_state = NULL;
  11515. struct drm_plane *primary = NULL;
  11516. struct drm_plane *cursor = NULL;
  11517. int i, ret;
  11518. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11519. if (intel_crtc == NULL)
  11520. return;
  11521. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11522. if (!crtc_state)
  11523. goto fail;
  11524. intel_crtc->config = crtc_state;
  11525. intel_crtc->base.state = &crtc_state->base;
  11526. crtc_state->base.crtc = &intel_crtc->base;
  11527. /* initialize shared scalers */
  11528. if (INTEL_INFO(dev)->gen >= 9) {
  11529. if (pipe == PIPE_C)
  11530. intel_crtc->num_scalers = 1;
  11531. else
  11532. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11533. skl_init_scalers(dev, intel_crtc, crtc_state);
  11534. }
  11535. primary = intel_primary_plane_create(dev, pipe);
  11536. if (!primary)
  11537. goto fail;
  11538. cursor = intel_cursor_plane_create(dev, pipe);
  11539. if (!cursor)
  11540. goto fail;
  11541. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11542. cursor, &intel_crtc_funcs);
  11543. if (ret)
  11544. goto fail;
  11545. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11546. for (i = 0; i < 256; i++) {
  11547. intel_crtc->lut_r[i] = i;
  11548. intel_crtc->lut_g[i] = i;
  11549. intel_crtc->lut_b[i] = i;
  11550. }
  11551. /*
  11552. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11553. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11554. */
  11555. intel_crtc->pipe = pipe;
  11556. intel_crtc->plane = pipe;
  11557. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11558. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11559. intel_crtc->plane = !pipe;
  11560. }
  11561. intel_crtc->cursor_base = ~0;
  11562. intel_crtc->cursor_cntl = ~0;
  11563. intel_crtc->cursor_size = ~0;
  11564. intel_crtc->wm.cxsr_allowed = true;
  11565. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11566. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11567. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11568. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11569. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11570. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11571. return;
  11572. fail:
  11573. if (primary)
  11574. drm_plane_cleanup(primary);
  11575. if (cursor)
  11576. drm_plane_cleanup(cursor);
  11577. kfree(crtc_state);
  11578. kfree(intel_crtc);
  11579. }
  11580. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11581. {
  11582. struct drm_encoder *encoder = connector->base.encoder;
  11583. struct drm_device *dev = connector->base.dev;
  11584. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11585. if (!encoder || WARN_ON(!encoder->crtc))
  11586. return INVALID_PIPE;
  11587. return to_intel_crtc(encoder->crtc)->pipe;
  11588. }
  11589. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11590. struct drm_file *file)
  11591. {
  11592. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11593. struct drm_crtc *drmmode_crtc;
  11594. struct intel_crtc *crtc;
  11595. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11596. if (!drmmode_crtc) {
  11597. DRM_ERROR("no such CRTC id\n");
  11598. return -ENOENT;
  11599. }
  11600. crtc = to_intel_crtc(drmmode_crtc);
  11601. pipe_from_crtc_id->pipe = crtc->pipe;
  11602. return 0;
  11603. }
  11604. static int intel_encoder_clones(struct intel_encoder *encoder)
  11605. {
  11606. struct drm_device *dev = encoder->base.dev;
  11607. struct intel_encoder *source_encoder;
  11608. int index_mask = 0;
  11609. int entry = 0;
  11610. for_each_intel_encoder(dev, source_encoder) {
  11611. if (encoders_cloneable(encoder, source_encoder))
  11612. index_mask |= (1 << entry);
  11613. entry++;
  11614. }
  11615. return index_mask;
  11616. }
  11617. static bool has_edp_a(struct drm_device *dev)
  11618. {
  11619. struct drm_i915_private *dev_priv = dev->dev_private;
  11620. if (!IS_MOBILE(dev))
  11621. return false;
  11622. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11623. return false;
  11624. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11625. return false;
  11626. return true;
  11627. }
  11628. static bool intel_crt_present(struct drm_device *dev)
  11629. {
  11630. struct drm_i915_private *dev_priv = dev->dev_private;
  11631. if (INTEL_INFO(dev)->gen >= 9)
  11632. return false;
  11633. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11634. return false;
  11635. if (IS_CHERRYVIEW(dev))
  11636. return false;
  11637. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11638. return false;
  11639. return true;
  11640. }
  11641. static void intel_setup_outputs(struct drm_device *dev)
  11642. {
  11643. struct drm_i915_private *dev_priv = dev->dev_private;
  11644. struct intel_encoder *encoder;
  11645. bool dpd_is_edp = false;
  11646. intel_lvds_init(dev);
  11647. if (intel_crt_present(dev))
  11648. intel_crt_init(dev);
  11649. if (IS_BROXTON(dev)) {
  11650. /*
  11651. * FIXME: Broxton doesn't support port detection via the
  11652. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11653. * detect the ports.
  11654. */
  11655. intel_ddi_init(dev, PORT_A);
  11656. intel_ddi_init(dev, PORT_B);
  11657. intel_ddi_init(dev, PORT_C);
  11658. } else if (HAS_DDI(dev)) {
  11659. int found;
  11660. /*
  11661. * Haswell uses DDI functions to detect digital outputs.
  11662. * On SKL pre-D0 the strap isn't connected, so we assume
  11663. * it's there.
  11664. */
  11665. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11666. /* WaIgnoreDDIAStrap: skl */
  11667. if (found || IS_SKYLAKE(dev))
  11668. intel_ddi_init(dev, PORT_A);
  11669. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11670. * register */
  11671. found = I915_READ(SFUSE_STRAP);
  11672. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11673. intel_ddi_init(dev, PORT_B);
  11674. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11675. intel_ddi_init(dev, PORT_C);
  11676. if (found & SFUSE_STRAP_DDID_DETECTED)
  11677. intel_ddi_init(dev, PORT_D);
  11678. /*
  11679. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11680. */
  11681. if (IS_SKYLAKE(dev) &&
  11682. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11683. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11684. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11685. intel_ddi_init(dev, PORT_E);
  11686. } else if (HAS_PCH_SPLIT(dev)) {
  11687. int found;
  11688. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11689. if (has_edp_a(dev))
  11690. intel_dp_init(dev, DP_A, PORT_A);
  11691. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11692. /* PCH SDVOB multiplex with HDMIB */
  11693. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11694. if (!found)
  11695. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11696. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11697. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11698. }
  11699. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11700. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11701. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11702. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11703. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11704. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11705. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11706. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11707. } else if (IS_VALLEYVIEW(dev)) {
  11708. /*
  11709. * The DP_DETECTED bit is the latched state of the DDC
  11710. * SDA pin at boot. However since eDP doesn't require DDC
  11711. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11712. * eDP ports may have been muxed to an alternate function.
  11713. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11714. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11715. * detect eDP ports.
  11716. */
  11717. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11718. !intel_dp_is_edp(dev, PORT_B))
  11719. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11720. PORT_B);
  11721. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11722. intel_dp_is_edp(dev, PORT_B))
  11723. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11724. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11725. !intel_dp_is_edp(dev, PORT_C))
  11726. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11727. PORT_C);
  11728. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11729. intel_dp_is_edp(dev, PORT_C))
  11730. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11731. if (IS_CHERRYVIEW(dev)) {
  11732. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11733. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11734. PORT_D);
  11735. /* eDP not supported on port D, so don't check VBT */
  11736. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11737. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11738. }
  11739. intel_dsi_init(dev);
  11740. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11741. bool found = false;
  11742. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11743. DRM_DEBUG_KMS("probing SDVOB\n");
  11744. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11745. if (!found && IS_G4X(dev)) {
  11746. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11747. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11748. }
  11749. if (!found && IS_G4X(dev))
  11750. intel_dp_init(dev, DP_B, PORT_B);
  11751. }
  11752. /* Before G4X SDVOC doesn't have its own detect register */
  11753. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11754. DRM_DEBUG_KMS("probing SDVOC\n");
  11755. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11756. }
  11757. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11758. if (IS_G4X(dev)) {
  11759. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11760. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11761. }
  11762. if (IS_G4X(dev))
  11763. intel_dp_init(dev, DP_C, PORT_C);
  11764. }
  11765. if (IS_G4X(dev) &&
  11766. (I915_READ(DP_D) & DP_DETECTED))
  11767. intel_dp_init(dev, DP_D, PORT_D);
  11768. } else if (IS_GEN2(dev))
  11769. intel_dvo_init(dev);
  11770. if (SUPPORTS_TV(dev))
  11771. intel_tv_init(dev);
  11772. intel_psr_init(dev);
  11773. for_each_intel_encoder(dev, encoder) {
  11774. encoder->base.possible_crtcs = encoder->crtc_mask;
  11775. encoder->base.possible_clones =
  11776. intel_encoder_clones(encoder);
  11777. }
  11778. intel_init_pch_refclk(dev);
  11779. drm_helper_move_panel_connectors_to_head(dev);
  11780. }
  11781. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11782. {
  11783. struct drm_device *dev = fb->dev;
  11784. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11785. drm_framebuffer_cleanup(fb);
  11786. mutex_lock(&dev->struct_mutex);
  11787. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11788. drm_gem_object_unreference(&intel_fb->obj->base);
  11789. mutex_unlock(&dev->struct_mutex);
  11790. kfree(intel_fb);
  11791. }
  11792. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11793. struct drm_file *file,
  11794. unsigned int *handle)
  11795. {
  11796. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11797. struct drm_i915_gem_object *obj = intel_fb->obj;
  11798. return drm_gem_handle_create(file, &obj->base, handle);
  11799. }
  11800. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11801. struct drm_file *file,
  11802. unsigned flags, unsigned color,
  11803. struct drm_clip_rect *clips,
  11804. unsigned num_clips)
  11805. {
  11806. struct drm_device *dev = fb->dev;
  11807. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11808. struct drm_i915_gem_object *obj = intel_fb->obj;
  11809. mutex_lock(&dev->struct_mutex);
  11810. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11811. mutex_unlock(&dev->struct_mutex);
  11812. return 0;
  11813. }
  11814. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11815. .destroy = intel_user_framebuffer_destroy,
  11816. .create_handle = intel_user_framebuffer_create_handle,
  11817. .dirty = intel_user_framebuffer_dirty,
  11818. };
  11819. static
  11820. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11821. uint32_t pixel_format)
  11822. {
  11823. u32 gen = INTEL_INFO(dev)->gen;
  11824. if (gen >= 9) {
  11825. /* "The stride in bytes must not exceed the of the size of 8K
  11826. * pixels and 32K bytes."
  11827. */
  11828. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11829. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11830. return 32*1024;
  11831. } else if (gen >= 4) {
  11832. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11833. return 16*1024;
  11834. else
  11835. return 32*1024;
  11836. } else if (gen >= 3) {
  11837. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11838. return 8*1024;
  11839. else
  11840. return 16*1024;
  11841. } else {
  11842. /* XXX DSPC is limited to 4k tiled */
  11843. return 8*1024;
  11844. }
  11845. }
  11846. static int intel_framebuffer_init(struct drm_device *dev,
  11847. struct intel_framebuffer *intel_fb,
  11848. struct drm_mode_fb_cmd2 *mode_cmd,
  11849. struct drm_i915_gem_object *obj)
  11850. {
  11851. unsigned int aligned_height;
  11852. int ret;
  11853. u32 pitch_limit, stride_alignment;
  11854. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11855. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11856. /* Enforce that fb modifier and tiling mode match, but only for
  11857. * X-tiled. This is needed for FBC. */
  11858. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11859. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11860. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11861. return -EINVAL;
  11862. }
  11863. } else {
  11864. if (obj->tiling_mode == I915_TILING_X)
  11865. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11866. else if (obj->tiling_mode == I915_TILING_Y) {
  11867. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11868. return -EINVAL;
  11869. }
  11870. }
  11871. /* Passed in modifier sanity checking. */
  11872. switch (mode_cmd->modifier[0]) {
  11873. case I915_FORMAT_MOD_Y_TILED:
  11874. case I915_FORMAT_MOD_Yf_TILED:
  11875. if (INTEL_INFO(dev)->gen < 9) {
  11876. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11877. mode_cmd->modifier[0]);
  11878. return -EINVAL;
  11879. }
  11880. case DRM_FORMAT_MOD_NONE:
  11881. case I915_FORMAT_MOD_X_TILED:
  11882. break;
  11883. default:
  11884. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11885. mode_cmd->modifier[0]);
  11886. return -EINVAL;
  11887. }
  11888. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11889. mode_cmd->pixel_format);
  11890. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11891. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11892. mode_cmd->pitches[0], stride_alignment);
  11893. return -EINVAL;
  11894. }
  11895. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11896. mode_cmd->pixel_format);
  11897. if (mode_cmd->pitches[0] > pitch_limit) {
  11898. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11899. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11900. "tiled" : "linear",
  11901. mode_cmd->pitches[0], pitch_limit);
  11902. return -EINVAL;
  11903. }
  11904. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11905. mode_cmd->pitches[0] != obj->stride) {
  11906. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11907. mode_cmd->pitches[0], obj->stride);
  11908. return -EINVAL;
  11909. }
  11910. /* Reject formats not supported by any plane early. */
  11911. switch (mode_cmd->pixel_format) {
  11912. case DRM_FORMAT_C8:
  11913. case DRM_FORMAT_RGB565:
  11914. case DRM_FORMAT_XRGB8888:
  11915. case DRM_FORMAT_ARGB8888:
  11916. break;
  11917. case DRM_FORMAT_XRGB1555:
  11918. if (INTEL_INFO(dev)->gen > 3) {
  11919. DRM_DEBUG("unsupported pixel format: %s\n",
  11920. drm_get_format_name(mode_cmd->pixel_format));
  11921. return -EINVAL;
  11922. }
  11923. break;
  11924. case DRM_FORMAT_ABGR8888:
  11925. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  11926. DRM_DEBUG("unsupported pixel format: %s\n",
  11927. drm_get_format_name(mode_cmd->pixel_format));
  11928. return -EINVAL;
  11929. }
  11930. break;
  11931. case DRM_FORMAT_XBGR8888:
  11932. case DRM_FORMAT_XRGB2101010:
  11933. case DRM_FORMAT_XBGR2101010:
  11934. if (INTEL_INFO(dev)->gen < 4) {
  11935. DRM_DEBUG("unsupported pixel format: %s\n",
  11936. drm_get_format_name(mode_cmd->pixel_format));
  11937. return -EINVAL;
  11938. }
  11939. break;
  11940. case DRM_FORMAT_ABGR2101010:
  11941. if (!IS_VALLEYVIEW(dev)) {
  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_YUYV:
  11948. case DRM_FORMAT_UYVY:
  11949. case DRM_FORMAT_YVYU:
  11950. case DRM_FORMAT_VYUY:
  11951. if (INTEL_INFO(dev)->gen < 5) {
  11952. DRM_DEBUG("unsupported pixel format: %s\n",
  11953. drm_get_format_name(mode_cmd->pixel_format));
  11954. return -EINVAL;
  11955. }
  11956. break;
  11957. default:
  11958. DRM_DEBUG("unsupported pixel format: %s\n",
  11959. drm_get_format_name(mode_cmd->pixel_format));
  11960. return -EINVAL;
  11961. }
  11962. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11963. if (mode_cmd->offsets[0] != 0)
  11964. return -EINVAL;
  11965. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11966. mode_cmd->pixel_format,
  11967. mode_cmd->modifier[0]);
  11968. /* FIXME drm helper for size checks (especially planar formats)? */
  11969. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11970. return -EINVAL;
  11971. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11972. intel_fb->obj = obj;
  11973. intel_fb->obj->framebuffer_references++;
  11974. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11975. if (ret) {
  11976. DRM_ERROR("framebuffer init failed %d\n", ret);
  11977. return ret;
  11978. }
  11979. return 0;
  11980. }
  11981. static struct drm_framebuffer *
  11982. intel_user_framebuffer_create(struct drm_device *dev,
  11983. struct drm_file *filp,
  11984. struct drm_mode_fb_cmd2 *mode_cmd)
  11985. {
  11986. struct drm_i915_gem_object *obj;
  11987. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11988. mode_cmd->handles[0]));
  11989. if (&obj->base == NULL)
  11990. return ERR_PTR(-ENOENT);
  11991. return intel_framebuffer_create(dev, mode_cmd, obj);
  11992. }
  11993. #ifndef CONFIG_DRM_FBDEV_EMULATION
  11994. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  11995. {
  11996. }
  11997. #endif
  11998. static const struct drm_mode_config_funcs intel_mode_funcs = {
  11999. .fb_create = intel_user_framebuffer_create,
  12000. .output_poll_changed = intel_fbdev_output_poll_changed,
  12001. .atomic_check = intel_atomic_check,
  12002. .atomic_commit = intel_atomic_commit,
  12003. .atomic_state_alloc = intel_atomic_state_alloc,
  12004. .atomic_state_clear = intel_atomic_state_clear,
  12005. };
  12006. /* Set up chip specific display functions */
  12007. static void intel_init_display(struct drm_device *dev)
  12008. {
  12009. struct drm_i915_private *dev_priv = dev->dev_private;
  12010. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12011. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12012. else if (IS_CHERRYVIEW(dev))
  12013. dev_priv->display.find_dpll = chv_find_best_dpll;
  12014. else if (IS_VALLEYVIEW(dev))
  12015. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12016. else if (IS_PINEVIEW(dev))
  12017. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12018. else
  12019. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12020. if (INTEL_INFO(dev)->gen >= 9) {
  12021. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12022. dev_priv->display.get_initial_plane_config =
  12023. skylake_get_initial_plane_config;
  12024. dev_priv->display.crtc_compute_clock =
  12025. haswell_crtc_compute_clock;
  12026. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12027. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12028. dev_priv->display.update_primary_plane =
  12029. skylake_update_primary_plane;
  12030. } else if (HAS_DDI(dev)) {
  12031. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12032. dev_priv->display.get_initial_plane_config =
  12033. ironlake_get_initial_plane_config;
  12034. dev_priv->display.crtc_compute_clock =
  12035. haswell_crtc_compute_clock;
  12036. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12037. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12038. dev_priv->display.update_primary_plane =
  12039. ironlake_update_primary_plane;
  12040. } else if (HAS_PCH_SPLIT(dev)) {
  12041. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12042. dev_priv->display.get_initial_plane_config =
  12043. ironlake_get_initial_plane_config;
  12044. dev_priv->display.crtc_compute_clock =
  12045. ironlake_crtc_compute_clock;
  12046. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12047. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12048. dev_priv->display.update_primary_plane =
  12049. ironlake_update_primary_plane;
  12050. } else if (IS_VALLEYVIEW(dev)) {
  12051. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12052. dev_priv->display.get_initial_plane_config =
  12053. i9xx_get_initial_plane_config;
  12054. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12055. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12056. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12057. dev_priv->display.update_primary_plane =
  12058. i9xx_update_primary_plane;
  12059. } else {
  12060. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12061. dev_priv->display.get_initial_plane_config =
  12062. i9xx_get_initial_plane_config;
  12063. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12064. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12065. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12066. dev_priv->display.update_primary_plane =
  12067. i9xx_update_primary_plane;
  12068. }
  12069. /* Returns the core display clock speed */
  12070. if (IS_SKYLAKE(dev))
  12071. dev_priv->display.get_display_clock_speed =
  12072. skylake_get_display_clock_speed;
  12073. else if (IS_BROXTON(dev))
  12074. dev_priv->display.get_display_clock_speed =
  12075. broxton_get_display_clock_speed;
  12076. else if (IS_BROADWELL(dev))
  12077. dev_priv->display.get_display_clock_speed =
  12078. broadwell_get_display_clock_speed;
  12079. else if (IS_HASWELL(dev))
  12080. dev_priv->display.get_display_clock_speed =
  12081. haswell_get_display_clock_speed;
  12082. else if (IS_VALLEYVIEW(dev))
  12083. dev_priv->display.get_display_clock_speed =
  12084. valleyview_get_display_clock_speed;
  12085. else if (IS_GEN5(dev))
  12086. dev_priv->display.get_display_clock_speed =
  12087. ilk_get_display_clock_speed;
  12088. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12089. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12090. dev_priv->display.get_display_clock_speed =
  12091. i945_get_display_clock_speed;
  12092. else if (IS_GM45(dev))
  12093. dev_priv->display.get_display_clock_speed =
  12094. gm45_get_display_clock_speed;
  12095. else if (IS_CRESTLINE(dev))
  12096. dev_priv->display.get_display_clock_speed =
  12097. i965gm_get_display_clock_speed;
  12098. else if (IS_PINEVIEW(dev))
  12099. dev_priv->display.get_display_clock_speed =
  12100. pnv_get_display_clock_speed;
  12101. else if (IS_G33(dev) || IS_G4X(dev))
  12102. dev_priv->display.get_display_clock_speed =
  12103. g33_get_display_clock_speed;
  12104. else if (IS_I915G(dev))
  12105. dev_priv->display.get_display_clock_speed =
  12106. i915_get_display_clock_speed;
  12107. else if (IS_I945GM(dev) || IS_845G(dev))
  12108. dev_priv->display.get_display_clock_speed =
  12109. i9xx_misc_get_display_clock_speed;
  12110. else if (IS_PINEVIEW(dev))
  12111. dev_priv->display.get_display_clock_speed =
  12112. pnv_get_display_clock_speed;
  12113. else if (IS_I915GM(dev))
  12114. dev_priv->display.get_display_clock_speed =
  12115. i915gm_get_display_clock_speed;
  12116. else if (IS_I865G(dev))
  12117. dev_priv->display.get_display_clock_speed =
  12118. i865_get_display_clock_speed;
  12119. else if (IS_I85X(dev))
  12120. dev_priv->display.get_display_clock_speed =
  12121. i85x_get_display_clock_speed;
  12122. else { /* 830 */
  12123. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12124. dev_priv->display.get_display_clock_speed =
  12125. i830_get_display_clock_speed;
  12126. }
  12127. if (IS_GEN5(dev)) {
  12128. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12129. } else if (IS_GEN6(dev)) {
  12130. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12131. } else if (IS_IVYBRIDGE(dev)) {
  12132. /* FIXME: detect B0+ stepping and use auto training */
  12133. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12134. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12135. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12136. if (IS_BROADWELL(dev)) {
  12137. dev_priv->display.modeset_commit_cdclk =
  12138. broadwell_modeset_commit_cdclk;
  12139. dev_priv->display.modeset_calc_cdclk =
  12140. broadwell_modeset_calc_cdclk;
  12141. }
  12142. } else if (IS_VALLEYVIEW(dev)) {
  12143. dev_priv->display.modeset_commit_cdclk =
  12144. valleyview_modeset_commit_cdclk;
  12145. dev_priv->display.modeset_calc_cdclk =
  12146. valleyview_modeset_calc_cdclk;
  12147. } else if (IS_BROXTON(dev)) {
  12148. dev_priv->display.modeset_commit_cdclk =
  12149. broxton_modeset_commit_cdclk;
  12150. dev_priv->display.modeset_calc_cdclk =
  12151. broxton_modeset_calc_cdclk;
  12152. }
  12153. switch (INTEL_INFO(dev)->gen) {
  12154. case 2:
  12155. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12156. break;
  12157. case 3:
  12158. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12159. break;
  12160. case 4:
  12161. case 5:
  12162. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12163. break;
  12164. case 6:
  12165. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12166. break;
  12167. case 7:
  12168. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12169. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12170. break;
  12171. case 9:
  12172. /* Drop through - unsupported since execlist only. */
  12173. default:
  12174. /* Default just returns -ENODEV to indicate unsupported */
  12175. dev_priv->display.queue_flip = intel_default_queue_flip;
  12176. }
  12177. intel_panel_init_backlight_funcs(dev);
  12178. mutex_init(&dev_priv->pps_mutex);
  12179. }
  12180. /*
  12181. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12182. * resume, or other times. This quirk makes sure that's the case for
  12183. * affected systems.
  12184. */
  12185. static void quirk_pipea_force(struct drm_device *dev)
  12186. {
  12187. struct drm_i915_private *dev_priv = dev->dev_private;
  12188. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12189. DRM_INFO("applying pipe a force quirk\n");
  12190. }
  12191. static void quirk_pipeb_force(struct drm_device *dev)
  12192. {
  12193. struct drm_i915_private *dev_priv = dev->dev_private;
  12194. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12195. DRM_INFO("applying pipe b force quirk\n");
  12196. }
  12197. /*
  12198. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12199. */
  12200. static void quirk_ssc_force_disable(struct drm_device *dev)
  12201. {
  12202. struct drm_i915_private *dev_priv = dev->dev_private;
  12203. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12204. DRM_INFO("applying lvds SSC disable quirk\n");
  12205. }
  12206. /*
  12207. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12208. * brightness value
  12209. */
  12210. static void quirk_invert_brightness(struct drm_device *dev)
  12211. {
  12212. struct drm_i915_private *dev_priv = dev->dev_private;
  12213. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12214. DRM_INFO("applying inverted panel brightness quirk\n");
  12215. }
  12216. /* Some VBT's incorrectly indicate no backlight is present */
  12217. static void quirk_backlight_present(struct drm_device *dev)
  12218. {
  12219. struct drm_i915_private *dev_priv = dev->dev_private;
  12220. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12221. DRM_INFO("applying backlight present quirk\n");
  12222. }
  12223. struct intel_quirk {
  12224. int device;
  12225. int subsystem_vendor;
  12226. int subsystem_device;
  12227. void (*hook)(struct drm_device *dev);
  12228. };
  12229. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12230. struct intel_dmi_quirk {
  12231. void (*hook)(struct drm_device *dev);
  12232. const struct dmi_system_id (*dmi_id_list)[];
  12233. };
  12234. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12235. {
  12236. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12237. return 1;
  12238. }
  12239. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12240. {
  12241. .dmi_id_list = &(const struct dmi_system_id[]) {
  12242. {
  12243. .callback = intel_dmi_reverse_brightness,
  12244. .ident = "NCR Corporation",
  12245. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12246. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12247. },
  12248. },
  12249. { } /* terminating entry */
  12250. },
  12251. .hook = quirk_invert_brightness,
  12252. },
  12253. };
  12254. static struct intel_quirk intel_quirks[] = {
  12255. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12256. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12257. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12258. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12259. /* 830 needs to leave pipe A & dpll A up */
  12260. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12261. /* 830 needs to leave pipe B & dpll B up */
  12262. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12263. /* Lenovo U160 cannot use SSC on LVDS */
  12264. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12265. /* Sony Vaio Y cannot use SSC on LVDS */
  12266. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12267. /* Acer Aspire 5734Z must invert backlight brightness */
  12268. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12269. /* Acer/eMachines G725 */
  12270. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12271. /* Acer/eMachines e725 */
  12272. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12273. /* Acer/Packard Bell NCL20 */
  12274. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12275. /* Acer Aspire 4736Z */
  12276. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12277. /* Acer Aspire 5336 */
  12278. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12279. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12280. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12281. /* Acer C720 Chromebook (Core i3 4005U) */
  12282. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12283. /* Apple Macbook 2,1 (Core 2 T7400) */
  12284. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12285. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12286. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12287. /* HP Chromebook 14 (Celeron 2955U) */
  12288. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12289. /* Dell Chromebook 11 */
  12290. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12291. };
  12292. static void intel_init_quirks(struct drm_device *dev)
  12293. {
  12294. struct pci_dev *d = dev->pdev;
  12295. int i;
  12296. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12297. struct intel_quirk *q = &intel_quirks[i];
  12298. if (d->device == q->device &&
  12299. (d->subsystem_vendor == q->subsystem_vendor ||
  12300. q->subsystem_vendor == PCI_ANY_ID) &&
  12301. (d->subsystem_device == q->subsystem_device ||
  12302. q->subsystem_device == PCI_ANY_ID))
  12303. q->hook(dev);
  12304. }
  12305. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12306. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12307. intel_dmi_quirks[i].hook(dev);
  12308. }
  12309. }
  12310. /* Disable the VGA plane that we never use */
  12311. static void i915_disable_vga(struct drm_device *dev)
  12312. {
  12313. struct drm_i915_private *dev_priv = dev->dev_private;
  12314. u8 sr1;
  12315. u32 vga_reg = i915_vgacntrl_reg(dev);
  12316. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12317. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12318. outb(SR01, VGA_SR_INDEX);
  12319. sr1 = inb(VGA_SR_DATA);
  12320. outb(sr1 | 1<<5, VGA_SR_DATA);
  12321. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12322. udelay(300);
  12323. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12324. POSTING_READ(vga_reg);
  12325. }
  12326. void intel_modeset_init_hw(struct drm_device *dev)
  12327. {
  12328. intel_update_cdclk(dev);
  12329. intel_prepare_ddi(dev);
  12330. intel_init_clock_gating(dev);
  12331. intel_enable_gt_powersave(dev);
  12332. }
  12333. void intel_modeset_init(struct drm_device *dev)
  12334. {
  12335. struct drm_i915_private *dev_priv = dev->dev_private;
  12336. int sprite, ret;
  12337. enum pipe pipe;
  12338. struct intel_crtc *crtc;
  12339. drm_mode_config_init(dev);
  12340. dev->mode_config.min_width = 0;
  12341. dev->mode_config.min_height = 0;
  12342. dev->mode_config.preferred_depth = 24;
  12343. dev->mode_config.prefer_shadow = 1;
  12344. dev->mode_config.allow_fb_modifiers = true;
  12345. dev->mode_config.funcs = &intel_mode_funcs;
  12346. intel_init_quirks(dev);
  12347. intel_init_pm(dev);
  12348. if (INTEL_INFO(dev)->num_pipes == 0)
  12349. return;
  12350. /*
  12351. * There may be no VBT; and if the BIOS enabled SSC we can
  12352. * just keep using it to avoid unnecessary flicker. Whereas if the
  12353. * BIOS isn't using it, don't assume it will work even if the VBT
  12354. * indicates as much.
  12355. */
  12356. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12357. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12358. DREF_SSC1_ENABLE);
  12359. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12360. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12361. bios_lvds_use_ssc ? "en" : "dis",
  12362. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12363. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12364. }
  12365. }
  12366. intel_init_display(dev);
  12367. intel_init_audio(dev);
  12368. if (IS_GEN2(dev)) {
  12369. dev->mode_config.max_width = 2048;
  12370. dev->mode_config.max_height = 2048;
  12371. } else if (IS_GEN3(dev)) {
  12372. dev->mode_config.max_width = 4096;
  12373. dev->mode_config.max_height = 4096;
  12374. } else {
  12375. dev->mode_config.max_width = 8192;
  12376. dev->mode_config.max_height = 8192;
  12377. }
  12378. if (IS_845G(dev) || IS_I865G(dev)) {
  12379. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12380. dev->mode_config.cursor_height = 1023;
  12381. } else if (IS_GEN2(dev)) {
  12382. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12383. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12384. } else {
  12385. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12386. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12387. }
  12388. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12389. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12390. INTEL_INFO(dev)->num_pipes,
  12391. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12392. for_each_pipe(dev_priv, pipe) {
  12393. intel_crtc_init(dev, pipe);
  12394. for_each_sprite(dev_priv, pipe, sprite) {
  12395. ret = intel_plane_init(dev, pipe, sprite);
  12396. if (ret)
  12397. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12398. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12399. }
  12400. }
  12401. intel_init_dpio(dev);
  12402. intel_shared_dpll_init(dev);
  12403. /* Just disable it once at startup */
  12404. i915_disable_vga(dev);
  12405. intel_setup_outputs(dev);
  12406. /* Just in case the BIOS is doing something questionable. */
  12407. intel_fbc_disable(dev_priv);
  12408. drm_modeset_lock_all(dev);
  12409. intel_modeset_setup_hw_state(dev);
  12410. drm_modeset_unlock_all(dev);
  12411. for_each_intel_crtc(dev, crtc) {
  12412. struct intel_initial_plane_config plane_config = {};
  12413. if (!crtc->active)
  12414. continue;
  12415. /*
  12416. * Note that reserving the BIOS fb up front prevents us
  12417. * from stuffing other stolen allocations like the ring
  12418. * on top. This prevents some ugliness at boot time, and
  12419. * can even allow for smooth boot transitions if the BIOS
  12420. * fb is large enough for the active pipe configuration.
  12421. */
  12422. dev_priv->display.get_initial_plane_config(crtc,
  12423. &plane_config);
  12424. /*
  12425. * If the fb is shared between multiple heads, we'll
  12426. * just get the first one.
  12427. */
  12428. intel_find_initial_plane_obj(crtc, &plane_config);
  12429. }
  12430. }
  12431. static void intel_enable_pipe_a(struct drm_device *dev)
  12432. {
  12433. struct intel_connector *connector;
  12434. struct drm_connector *crt = NULL;
  12435. struct intel_load_detect_pipe load_detect_temp;
  12436. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12437. /* We can't just switch on the pipe A, we need to set things up with a
  12438. * proper mode and output configuration. As a gross hack, enable pipe A
  12439. * by enabling the load detect pipe once. */
  12440. for_each_intel_connector(dev, connector) {
  12441. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12442. crt = &connector->base;
  12443. break;
  12444. }
  12445. }
  12446. if (!crt)
  12447. return;
  12448. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12449. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12450. }
  12451. static bool
  12452. intel_check_plane_mapping(struct intel_crtc *crtc)
  12453. {
  12454. struct drm_device *dev = crtc->base.dev;
  12455. struct drm_i915_private *dev_priv = dev->dev_private;
  12456. u32 reg, val;
  12457. if (INTEL_INFO(dev)->num_pipes == 1)
  12458. return true;
  12459. reg = DSPCNTR(!crtc->plane);
  12460. val = I915_READ(reg);
  12461. if ((val & DISPLAY_PLANE_ENABLE) &&
  12462. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12463. return false;
  12464. return true;
  12465. }
  12466. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12467. {
  12468. struct drm_device *dev = crtc->base.dev;
  12469. struct drm_i915_private *dev_priv = dev->dev_private;
  12470. struct intel_encoder *encoder;
  12471. u32 reg;
  12472. bool enable;
  12473. /* Clear any frame start delays used for debugging left by the BIOS */
  12474. reg = PIPECONF(crtc->config->cpu_transcoder);
  12475. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12476. /* restore vblank interrupts to correct state */
  12477. drm_crtc_vblank_reset(&crtc->base);
  12478. if (crtc->active) {
  12479. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12480. update_scanline_offset(crtc);
  12481. drm_crtc_vblank_on(&crtc->base);
  12482. }
  12483. /* We need to sanitize the plane -> pipe mapping first because this will
  12484. * disable the crtc (and hence change the state) if it is wrong. Note
  12485. * that gen4+ has a fixed plane -> pipe mapping. */
  12486. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12487. bool plane;
  12488. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12489. crtc->base.base.id);
  12490. /* Pipe has the wrong plane attached and the plane is active.
  12491. * Temporarily change the plane mapping and disable everything
  12492. * ... */
  12493. plane = crtc->plane;
  12494. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12495. crtc->plane = !plane;
  12496. intel_crtc_disable_noatomic(&crtc->base);
  12497. crtc->plane = plane;
  12498. }
  12499. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12500. crtc->pipe == PIPE_A && !crtc->active) {
  12501. /* BIOS forgot to enable pipe A, this mostly happens after
  12502. * resume. Force-enable the pipe to fix this, the update_dpms
  12503. * call below we restore the pipe to the right state, but leave
  12504. * the required bits on. */
  12505. intel_enable_pipe_a(dev);
  12506. }
  12507. /* Adjust the state of the output pipe according to whether we
  12508. * have active connectors/encoders. */
  12509. enable = false;
  12510. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12511. enable = true;
  12512. break;
  12513. }
  12514. if (!enable)
  12515. intel_crtc_disable_noatomic(&crtc->base);
  12516. if (crtc->active != crtc->base.state->active) {
  12517. /* This can happen either due to bugs in the get_hw_state
  12518. * functions or because of calls to intel_crtc_disable_noatomic,
  12519. * or because the pipe is force-enabled due to the
  12520. * pipe A quirk. */
  12521. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12522. crtc->base.base.id,
  12523. crtc->base.state->enable ? "enabled" : "disabled",
  12524. crtc->active ? "enabled" : "disabled");
  12525. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12526. crtc->base.state->active = crtc->active;
  12527. crtc->base.enabled = crtc->active;
  12528. /* Because we only establish the connector -> encoder ->
  12529. * crtc links if something is active, this means the
  12530. * crtc is now deactivated. Break the links. connector
  12531. * -> encoder links are only establish when things are
  12532. * actually up, hence no need to break them. */
  12533. WARN_ON(crtc->active);
  12534. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12535. encoder->base.crtc = NULL;
  12536. }
  12537. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12538. /*
  12539. * We start out with underrun reporting disabled to avoid races.
  12540. * For correct bookkeeping mark this on active crtcs.
  12541. *
  12542. * Also on gmch platforms we dont have any hardware bits to
  12543. * disable the underrun reporting. Which means we need to start
  12544. * out with underrun reporting disabled also on inactive pipes,
  12545. * since otherwise we'll complain about the garbage we read when
  12546. * e.g. coming up after runtime pm.
  12547. *
  12548. * No protection against concurrent access is required - at
  12549. * worst a fifo underrun happens which also sets this to false.
  12550. */
  12551. crtc->cpu_fifo_underrun_disabled = true;
  12552. crtc->pch_fifo_underrun_disabled = true;
  12553. }
  12554. }
  12555. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12556. {
  12557. struct intel_connector *connector;
  12558. struct drm_device *dev = encoder->base.dev;
  12559. bool active = false;
  12560. /* We need to check both for a crtc link (meaning that the
  12561. * encoder is active and trying to read from a pipe) and the
  12562. * pipe itself being active. */
  12563. bool has_active_crtc = encoder->base.crtc &&
  12564. to_intel_crtc(encoder->base.crtc)->active;
  12565. for_each_intel_connector(dev, connector) {
  12566. if (connector->base.encoder != &encoder->base)
  12567. continue;
  12568. active = true;
  12569. break;
  12570. }
  12571. if (active && !has_active_crtc) {
  12572. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12573. encoder->base.base.id,
  12574. encoder->base.name);
  12575. /* Connector is active, but has no active pipe. This is
  12576. * fallout from our resume register restoring. Disable
  12577. * the encoder manually again. */
  12578. if (encoder->base.crtc) {
  12579. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12580. encoder->base.base.id,
  12581. encoder->base.name);
  12582. encoder->disable(encoder);
  12583. if (encoder->post_disable)
  12584. encoder->post_disable(encoder);
  12585. }
  12586. encoder->base.crtc = NULL;
  12587. /* Inconsistent output/port/pipe state happens presumably due to
  12588. * a bug in one of the get_hw_state functions. Or someplace else
  12589. * in our code, like the register restore mess on resume. Clamp
  12590. * things to off as a safer default. */
  12591. for_each_intel_connector(dev, connector) {
  12592. if (connector->encoder != encoder)
  12593. continue;
  12594. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12595. connector->base.encoder = NULL;
  12596. }
  12597. }
  12598. /* Enabled encoders without active connectors will be fixed in
  12599. * the crtc fixup. */
  12600. }
  12601. void i915_redisable_vga_power_on(struct drm_device *dev)
  12602. {
  12603. struct drm_i915_private *dev_priv = dev->dev_private;
  12604. u32 vga_reg = i915_vgacntrl_reg(dev);
  12605. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12606. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12607. i915_disable_vga(dev);
  12608. }
  12609. }
  12610. void i915_redisable_vga(struct drm_device *dev)
  12611. {
  12612. struct drm_i915_private *dev_priv = dev->dev_private;
  12613. /* This function can be called both from intel_modeset_setup_hw_state or
  12614. * at a very early point in our resume sequence, where the power well
  12615. * structures are not yet restored. Since this function is at a very
  12616. * paranoid "someone might have enabled VGA while we were not looking"
  12617. * level, just check if the power well is enabled instead of trying to
  12618. * follow the "don't touch the power well if we don't need it" policy
  12619. * the rest of the driver uses. */
  12620. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12621. return;
  12622. i915_redisable_vga_power_on(dev);
  12623. }
  12624. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12625. {
  12626. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12627. return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
  12628. }
  12629. static void readout_plane_state(struct intel_crtc *crtc,
  12630. struct intel_crtc_state *crtc_state)
  12631. {
  12632. struct intel_plane *p;
  12633. struct intel_plane_state *plane_state;
  12634. bool active = crtc_state->base.active;
  12635. for_each_intel_plane(crtc->base.dev, p) {
  12636. if (crtc->pipe != p->pipe)
  12637. continue;
  12638. plane_state = to_intel_plane_state(p->base.state);
  12639. if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
  12640. plane_state->visible = primary_get_hw_state(crtc);
  12641. else {
  12642. if (active)
  12643. p->disable_plane(&p->base, &crtc->base);
  12644. plane_state->visible = false;
  12645. }
  12646. }
  12647. }
  12648. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12649. {
  12650. struct drm_i915_private *dev_priv = dev->dev_private;
  12651. enum pipe pipe;
  12652. struct intel_crtc *crtc;
  12653. struct intel_encoder *encoder;
  12654. struct intel_connector *connector;
  12655. int i;
  12656. for_each_intel_crtc(dev, crtc) {
  12657. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12658. memset(crtc->config, 0, sizeof(*crtc->config));
  12659. crtc->config->base.crtc = &crtc->base;
  12660. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12661. crtc->config);
  12662. crtc->base.state->active = crtc->active;
  12663. crtc->base.enabled = crtc->active;
  12664. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12665. if (crtc->base.state->active) {
  12666. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12667. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12668. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12669. /*
  12670. * The initial mode needs to be set in order to keep
  12671. * the atomic core happy. It wants a valid mode if the
  12672. * crtc's enabled, so we do the above call.
  12673. *
  12674. * At this point some state updated by the connectors
  12675. * in their ->detect() callback has not run yet, so
  12676. * no recalculation can be done yet.
  12677. *
  12678. * Even if we could do a recalculation and modeset
  12679. * right now it would cause a double modeset if
  12680. * fbdev or userspace chooses a different initial mode.
  12681. *
  12682. * If that happens, someone indicated they wanted a
  12683. * mode change, which means it's safe to do a full
  12684. * recalculation.
  12685. */
  12686. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12687. }
  12688. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12689. readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
  12690. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12691. crtc->base.base.id,
  12692. crtc->active ? "enabled" : "disabled");
  12693. }
  12694. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12695. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12696. pll->on = pll->get_hw_state(dev_priv, pll,
  12697. &pll->config.hw_state);
  12698. pll->active = 0;
  12699. pll->config.crtc_mask = 0;
  12700. for_each_intel_crtc(dev, crtc) {
  12701. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12702. pll->active++;
  12703. pll->config.crtc_mask |= 1 << crtc->pipe;
  12704. }
  12705. }
  12706. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12707. pll->name, pll->config.crtc_mask, pll->on);
  12708. if (pll->config.crtc_mask)
  12709. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12710. }
  12711. for_each_intel_encoder(dev, encoder) {
  12712. pipe = 0;
  12713. if (encoder->get_hw_state(encoder, &pipe)) {
  12714. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12715. encoder->base.crtc = &crtc->base;
  12716. encoder->get_config(encoder, crtc->config);
  12717. } else {
  12718. encoder->base.crtc = NULL;
  12719. }
  12720. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12721. encoder->base.base.id,
  12722. encoder->base.name,
  12723. encoder->base.crtc ? "enabled" : "disabled",
  12724. pipe_name(pipe));
  12725. }
  12726. for_each_intel_connector(dev, connector) {
  12727. if (connector->get_hw_state(connector)) {
  12728. connector->base.dpms = DRM_MODE_DPMS_ON;
  12729. connector->base.encoder = &connector->encoder->base;
  12730. } else {
  12731. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12732. connector->base.encoder = NULL;
  12733. }
  12734. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12735. connector->base.base.id,
  12736. connector->base.name,
  12737. connector->base.encoder ? "enabled" : "disabled");
  12738. }
  12739. }
  12740. /* Scan out the current hw modeset state,
  12741. * and sanitizes it to the current state
  12742. */
  12743. static void
  12744. intel_modeset_setup_hw_state(struct drm_device *dev)
  12745. {
  12746. struct drm_i915_private *dev_priv = dev->dev_private;
  12747. enum pipe pipe;
  12748. struct intel_crtc *crtc;
  12749. struct intel_encoder *encoder;
  12750. int i;
  12751. intel_modeset_readout_hw_state(dev);
  12752. /* HW state is read out, now we need to sanitize this mess. */
  12753. for_each_intel_encoder(dev, encoder) {
  12754. intel_sanitize_encoder(encoder);
  12755. }
  12756. for_each_pipe(dev_priv, pipe) {
  12757. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12758. intel_sanitize_crtc(crtc);
  12759. intel_dump_pipe_config(crtc, crtc->config,
  12760. "[setup_hw_state]");
  12761. }
  12762. intel_modeset_update_connector_atomic_state(dev);
  12763. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12764. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12765. if (!pll->on || pll->active)
  12766. continue;
  12767. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12768. pll->disable(dev_priv, pll);
  12769. pll->on = false;
  12770. }
  12771. if (IS_VALLEYVIEW(dev))
  12772. vlv_wm_get_hw_state(dev);
  12773. else if (IS_GEN9(dev))
  12774. skl_wm_get_hw_state(dev);
  12775. else if (HAS_PCH_SPLIT(dev))
  12776. ilk_wm_get_hw_state(dev);
  12777. for_each_intel_crtc(dev, crtc) {
  12778. unsigned long put_domains;
  12779. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  12780. if (WARN_ON(put_domains))
  12781. modeset_put_power_domains(dev_priv, put_domains);
  12782. }
  12783. intel_display_set_init_power(dev_priv, false);
  12784. }
  12785. void intel_display_resume(struct drm_device *dev)
  12786. {
  12787. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  12788. struct intel_connector *conn;
  12789. struct intel_plane *plane;
  12790. struct drm_crtc *crtc;
  12791. int ret;
  12792. if (!state)
  12793. return;
  12794. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12795. /* preserve complete old state, including dpll */
  12796. intel_atomic_get_shared_dpll_state(state);
  12797. for_each_crtc(dev, crtc) {
  12798. struct drm_crtc_state *crtc_state =
  12799. drm_atomic_get_crtc_state(state, crtc);
  12800. ret = PTR_ERR_OR_ZERO(crtc_state);
  12801. if (ret)
  12802. goto err;
  12803. /* force a restore */
  12804. crtc_state->mode_changed = true;
  12805. }
  12806. for_each_intel_plane(dev, plane) {
  12807. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  12808. if (ret)
  12809. goto err;
  12810. }
  12811. for_each_intel_connector(dev, conn) {
  12812. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  12813. if (ret)
  12814. goto err;
  12815. }
  12816. intel_modeset_setup_hw_state(dev);
  12817. i915_redisable_vga(dev);
  12818. ret = drm_atomic_commit(state);
  12819. if (!ret)
  12820. return;
  12821. err:
  12822. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12823. drm_atomic_state_free(state);
  12824. }
  12825. void intel_modeset_gem_init(struct drm_device *dev)
  12826. {
  12827. struct drm_crtc *c;
  12828. struct drm_i915_gem_object *obj;
  12829. int ret;
  12830. mutex_lock(&dev->struct_mutex);
  12831. intel_init_gt_powersave(dev);
  12832. mutex_unlock(&dev->struct_mutex);
  12833. intel_modeset_init_hw(dev);
  12834. intel_setup_overlay(dev);
  12835. /*
  12836. * Make sure any fbs we allocated at startup are properly
  12837. * pinned & fenced. When we do the allocation it's too early
  12838. * for this.
  12839. */
  12840. for_each_crtc(dev, c) {
  12841. obj = intel_fb_obj(c->primary->fb);
  12842. if (obj == NULL)
  12843. continue;
  12844. mutex_lock(&dev->struct_mutex);
  12845. ret = intel_pin_and_fence_fb_obj(c->primary,
  12846. c->primary->fb,
  12847. c->primary->state,
  12848. NULL, NULL);
  12849. mutex_unlock(&dev->struct_mutex);
  12850. if (ret) {
  12851. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12852. to_intel_crtc(c)->pipe);
  12853. drm_framebuffer_unreference(c->primary->fb);
  12854. c->primary->fb = NULL;
  12855. c->primary->crtc = c->primary->state->crtc = NULL;
  12856. update_state_fb(c->primary);
  12857. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12858. }
  12859. }
  12860. intel_backlight_register(dev);
  12861. }
  12862. void intel_connector_unregister(struct intel_connector *intel_connector)
  12863. {
  12864. struct drm_connector *connector = &intel_connector->base;
  12865. intel_panel_destroy_backlight(connector);
  12866. drm_connector_unregister(connector);
  12867. }
  12868. void intel_modeset_cleanup(struct drm_device *dev)
  12869. {
  12870. struct drm_i915_private *dev_priv = dev->dev_private;
  12871. struct drm_connector *connector;
  12872. intel_disable_gt_powersave(dev);
  12873. intel_backlight_unregister(dev);
  12874. /*
  12875. * Interrupts and polling as the first thing to avoid creating havoc.
  12876. * Too much stuff here (turning of connectors, ...) would
  12877. * experience fancy races otherwise.
  12878. */
  12879. intel_irq_uninstall(dev_priv);
  12880. /*
  12881. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12882. * poll handlers. Hence disable polling after hpd handling is shut down.
  12883. */
  12884. drm_kms_helper_poll_fini(dev);
  12885. intel_unregister_dsm_handler();
  12886. intel_fbc_disable(dev_priv);
  12887. /* flush any delayed tasks or pending work */
  12888. flush_scheduled_work();
  12889. /* destroy the backlight and sysfs files before encoders/connectors */
  12890. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12891. struct intel_connector *intel_connector;
  12892. intel_connector = to_intel_connector(connector);
  12893. intel_connector->unregister(intel_connector);
  12894. }
  12895. drm_mode_config_cleanup(dev);
  12896. intel_cleanup_overlay(dev);
  12897. mutex_lock(&dev->struct_mutex);
  12898. intel_cleanup_gt_powersave(dev);
  12899. mutex_unlock(&dev->struct_mutex);
  12900. }
  12901. /*
  12902. * Return which encoder is currently attached for connector.
  12903. */
  12904. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12905. {
  12906. return &intel_attached_encoder(connector)->base;
  12907. }
  12908. void intel_connector_attach_encoder(struct intel_connector *connector,
  12909. struct intel_encoder *encoder)
  12910. {
  12911. connector->encoder = encoder;
  12912. drm_mode_connector_attach_encoder(&connector->base,
  12913. &encoder->base);
  12914. }
  12915. /*
  12916. * set vga decode state - true == enable VGA decode
  12917. */
  12918. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12919. {
  12920. struct drm_i915_private *dev_priv = dev->dev_private;
  12921. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12922. u16 gmch_ctrl;
  12923. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12924. DRM_ERROR("failed to read control word\n");
  12925. return -EIO;
  12926. }
  12927. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12928. return 0;
  12929. if (state)
  12930. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12931. else
  12932. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12933. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12934. DRM_ERROR("failed to write control word\n");
  12935. return -EIO;
  12936. }
  12937. return 0;
  12938. }
  12939. struct intel_display_error_state {
  12940. u32 power_well_driver;
  12941. int num_transcoders;
  12942. struct intel_cursor_error_state {
  12943. u32 control;
  12944. u32 position;
  12945. u32 base;
  12946. u32 size;
  12947. } cursor[I915_MAX_PIPES];
  12948. struct intel_pipe_error_state {
  12949. bool power_domain_on;
  12950. u32 source;
  12951. u32 stat;
  12952. } pipe[I915_MAX_PIPES];
  12953. struct intel_plane_error_state {
  12954. u32 control;
  12955. u32 stride;
  12956. u32 size;
  12957. u32 pos;
  12958. u32 addr;
  12959. u32 surface;
  12960. u32 tile_offset;
  12961. } plane[I915_MAX_PIPES];
  12962. struct intel_transcoder_error_state {
  12963. bool power_domain_on;
  12964. enum transcoder cpu_transcoder;
  12965. u32 conf;
  12966. u32 htotal;
  12967. u32 hblank;
  12968. u32 hsync;
  12969. u32 vtotal;
  12970. u32 vblank;
  12971. u32 vsync;
  12972. } transcoder[4];
  12973. };
  12974. struct intel_display_error_state *
  12975. intel_display_capture_error_state(struct drm_device *dev)
  12976. {
  12977. struct drm_i915_private *dev_priv = dev->dev_private;
  12978. struct intel_display_error_state *error;
  12979. int transcoders[] = {
  12980. TRANSCODER_A,
  12981. TRANSCODER_B,
  12982. TRANSCODER_C,
  12983. TRANSCODER_EDP,
  12984. };
  12985. int i;
  12986. if (INTEL_INFO(dev)->num_pipes == 0)
  12987. return NULL;
  12988. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  12989. if (error == NULL)
  12990. return NULL;
  12991. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  12992. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  12993. for_each_pipe(dev_priv, i) {
  12994. error->pipe[i].power_domain_on =
  12995. __intel_display_power_is_enabled(dev_priv,
  12996. POWER_DOMAIN_PIPE(i));
  12997. if (!error->pipe[i].power_domain_on)
  12998. continue;
  12999. error->cursor[i].control = I915_READ(CURCNTR(i));
  13000. error->cursor[i].position = I915_READ(CURPOS(i));
  13001. error->cursor[i].base = I915_READ(CURBASE(i));
  13002. error->plane[i].control = I915_READ(DSPCNTR(i));
  13003. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13004. if (INTEL_INFO(dev)->gen <= 3) {
  13005. error->plane[i].size = I915_READ(DSPSIZE(i));
  13006. error->plane[i].pos = I915_READ(DSPPOS(i));
  13007. }
  13008. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13009. error->plane[i].addr = I915_READ(DSPADDR(i));
  13010. if (INTEL_INFO(dev)->gen >= 4) {
  13011. error->plane[i].surface = I915_READ(DSPSURF(i));
  13012. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13013. }
  13014. error->pipe[i].source = I915_READ(PIPESRC(i));
  13015. if (HAS_GMCH_DISPLAY(dev))
  13016. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13017. }
  13018. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13019. if (HAS_DDI(dev_priv->dev))
  13020. error->num_transcoders++; /* Account for eDP. */
  13021. for (i = 0; i < error->num_transcoders; i++) {
  13022. enum transcoder cpu_transcoder = transcoders[i];
  13023. error->transcoder[i].power_domain_on =
  13024. __intel_display_power_is_enabled(dev_priv,
  13025. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13026. if (!error->transcoder[i].power_domain_on)
  13027. continue;
  13028. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13029. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13030. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13031. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13032. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13033. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13034. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13035. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13036. }
  13037. return error;
  13038. }
  13039. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13040. void
  13041. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13042. struct drm_device *dev,
  13043. struct intel_display_error_state *error)
  13044. {
  13045. struct drm_i915_private *dev_priv = dev->dev_private;
  13046. int i;
  13047. if (!error)
  13048. return;
  13049. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13050. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13051. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13052. error->power_well_driver);
  13053. for_each_pipe(dev_priv, i) {
  13054. err_printf(m, "Pipe [%d]:\n", i);
  13055. err_printf(m, " Power: %s\n",
  13056. error->pipe[i].power_domain_on ? "on" : "off");
  13057. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13058. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13059. err_printf(m, "Plane [%d]:\n", i);
  13060. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13061. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13062. if (INTEL_INFO(dev)->gen <= 3) {
  13063. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13064. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13065. }
  13066. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13067. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13068. if (INTEL_INFO(dev)->gen >= 4) {
  13069. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13070. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13071. }
  13072. err_printf(m, "Cursor [%d]:\n", i);
  13073. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13074. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13075. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13076. }
  13077. for (i = 0; i < error->num_transcoders; i++) {
  13078. err_printf(m, "CPU transcoder: %c\n",
  13079. transcoder_name(error->transcoder[i].cpu_transcoder));
  13080. err_printf(m, " Power: %s\n",
  13081. error->transcoder[i].power_domain_on ? "on" : "off");
  13082. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13083. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13084. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13085. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13086. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13087. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13088. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13089. }
  13090. }
  13091. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13092. {
  13093. struct intel_crtc *crtc;
  13094. for_each_intel_crtc(dev, crtc) {
  13095. struct intel_unpin_work *work;
  13096. spin_lock_irq(&dev->event_lock);
  13097. work = crtc->unpin_work;
  13098. if (work && work->event &&
  13099. work->event->base.file_priv == file) {
  13100. kfree(work->event);
  13101. work->event = NULL;
  13102. }
  13103. spin_unlock_irq(&dev->event_lock);
  13104. }
  13105. }