intel_display.c 431 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681
  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. /*
  1497. * Apparently we need to have VGA mode enabled prior to changing
  1498. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1499. * dividers, even though the register value does change.
  1500. */
  1501. I915_WRITE(reg, 0);
  1502. I915_WRITE(reg, dpll);
  1503. /* Wait for the clocks to stabilize. */
  1504. POSTING_READ(reg);
  1505. udelay(150);
  1506. if (INTEL_INFO(dev)->gen >= 4) {
  1507. I915_WRITE(DPLL_MD(crtc->pipe),
  1508. crtc->config->dpll_hw_state.dpll_md);
  1509. } else {
  1510. /* The pixel multiplier can only be updated once the
  1511. * DPLL is enabled and the clocks are stable.
  1512. *
  1513. * So write it again.
  1514. */
  1515. I915_WRITE(reg, dpll);
  1516. }
  1517. /* We do this three times for luck */
  1518. I915_WRITE(reg, dpll);
  1519. POSTING_READ(reg);
  1520. udelay(150); /* wait for warmup */
  1521. I915_WRITE(reg, dpll);
  1522. POSTING_READ(reg);
  1523. udelay(150); /* wait for warmup */
  1524. I915_WRITE(reg, dpll);
  1525. POSTING_READ(reg);
  1526. udelay(150); /* wait for warmup */
  1527. }
  1528. /**
  1529. * i9xx_disable_pll - disable a PLL
  1530. * @dev_priv: i915 private structure
  1531. * @pipe: pipe PLL to disable
  1532. *
  1533. * Disable the PLL for @pipe, making sure the pipe is off first.
  1534. *
  1535. * Note! This is for pre-ILK only.
  1536. */
  1537. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1538. {
  1539. struct drm_device *dev = crtc->base.dev;
  1540. struct drm_i915_private *dev_priv = dev->dev_private;
  1541. enum pipe pipe = crtc->pipe;
  1542. /* Disable DVO 2x clock on both PLLs if necessary */
  1543. if (IS_I830(dev) &&
  1544. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1545. !intel_num_dvo_pipes(dev)) {
  1546. I915_WRITE(DPLL(PIPE_B),
  1547. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1548. I915_WRITE(DPLL(PIPE_A),
  1549. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1550. }
  1551. /* Don't disable pipe or pipe PLLs if needed */
  1552. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1553. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1554. return;
  1555. /* Make sure the pipe isn't still relying on us */
  1556. assert_pipe_disabled(dev_priv, pipe);
  1557. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1558. POSTING_READ(DPLL(pipe));
  1559. }
  1560. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1561. {
  1562. u32 val;
  1563. /* Make sure the pipe isn't still relying on us */
  1564. assert_pipe_disabled(dev_priv, pipe);
  1565. /*
  1566. * Leave integrated clock source and reference clock enabled for pipe B.
  1567. * The latter is needed for VGA hotplug / manual detection.
  1568. */
  1569. val = DPLL_VGA_MODE_DIS;
  1570. if (pipe == PIPE_B)
  1571. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1572. I915_WRITE(DPLL(pipe), val);
  1573. POSTING_READ(DPLL(pipe));
  1574. }
  1575. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1576. {
  1577. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1578. u32 val;
  1579. /* Make sure the pipe isn't still relying on us */
  1580. assert_pipe_disabled(dev_priv, pipe);
  1581. /* Set PLL en = 0 */
  1582. val = DPLL_SSC_REF_CLK_CHV |
  1583. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1584. if (pipe != PIPE_A)
  1585. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1586. I915_WRITE(DPLL(pipe), val);
  1587. POSTING_READ(DPLL(pipe));
  1588. mutex_lock(&dev_priv->sb_lock);
  1589. /* Disable 10bit clock to display controller */
  1590. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1591. val &= ~DPIO_DCLKP_EN;
  1592. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1593. /* disable left/right clock distribution */
  1594. if (pipe != PIPE_B) {
  1595. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1596. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1597. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1598. } else {
  1599. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1600. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1601. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1602. }
  1603. mutex_unlock(&dev_priv->sb_lock);
  1604. }
  1605. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1606. struct intel_digital_port *dport,
  1607. unsigned int expected_mask)
  1608. {
  1609. u32 port_mask;
  1610. int dpll_reg;
  1611. switch (dport->port) {
  1612. case PORT_B:
  1613. port_mask = DPLL_PORTB_READY_MASK;
  1614. dpll_reg = DPLL(0);
  1615. break;
  1616. case PORT_C:
  1617. port_mask = DPLL_PORTC_READY_MASK;
  1618. dpll_reg = DPLL(0);
  1619. expected_mask <<= 4;
  1620. break;
  1621. case PORT_D:
  1622. port_mask = DPLL_PORTD_READY_MASK;
  1623. dpll_reg = DPIO_PHY_STATUS;
  1624. break;
  1625. default:
  1626. BUG();
  1627. }
  1628. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1629. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1630. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1631. }
  1632. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1633. {
  1634. struct drm_device *dev = crtc->base.dev;
  1635. struct drm_i915_private *dev_priv = dev->dev_private;
  1636. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1637. if (WARN_ON(pll == NULL))
  1638. return;
  1639. WARN_ON(!pll->config.crtc_mask);
  1640. if (pll->active == 0) {
  1641. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1642. WARN_ON(pll->on);
  1643. assert_shared_dpll_disabled(dev_priv, pll);
  1644. pll->mode_set(dev_priv, pll);
  1645. }
  1646. }
  1647. /**
  1648. * intel_enable_shared_dpll - enable PCH PLL
  1649. * @dev_priv: i915 private structure
  1650. * @pipe: pipe PLL to enable
  1651. *
  1652. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1653. * drives the transcoder clock.
  1654. */
  1655. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1656. {
  1657. struct drm_device *dev = crtc->base.dev;
  1658. struct drm_i915_private *dev_priv = dev->dev_private;
  1659. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1660. if (WARN_ON(pll == NULL))
  1661. return;
  1662. if (WARN_ON(pll->config.crtc_mask == 0))
  1663. return;
  1664. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1665. pll->name, pll->active, pll->on,
  1666. crtc->base.base.id);
  1667. if (pll->active++) {
  1668. WARN_ON(!pll->on);
  1669. assert_shared_dpll_enabled(dev_priv, pll);
  1670. return;
  1671. }
  1672. WARN_ON(pll->on);
  1673. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1674. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1675. pll->enable(dev_priv, pll);
  1676. pll->on = true;
  1677. }
  1678. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1679. {
  1680. struct drm_device *dev = crtc->base.dev;
  1681. struct drm_i915_private *dev_priv = dev->dev_private;
  1682. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1683. /* PCH only available on ILK+ */
  1684. if (INTEL_INFO(dev)->gen < 5)
  1685. return;
  1686. if (pll == NULL)
  1687. return;
  1688. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1689. return;
  1690. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1691. pll->name, pll->active, pll->on,
  1692. crtc->base.base.id);
  1693. if (WARN_ON(pll->active == 0)) {
  1694. assert_shared_dpll_disabled(dev_priv, pll);
  1695. return;
  1696. }
  1697. assert_shared_dpll_enabled(dev_priv, pll);
  1698. WARN_ON(!pll->on);
  1699. if (--pll->active)
  1700. return;
  1701. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1702. pll->disable(dev_priv, pll);
  1703. pll->on = false;
  1704. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1705. }
  1706. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1707. enum pipe pipe)
  1708. {
  1709. struct drm_device *dev = dev_priv->dev;
  1710. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1711. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1712. uint32_t reg, val, pipeconf_val;
  1713. /* PCH only available on ILK+ */
  1714. BUG_ON(!HAS_PCH_SPLIT(dev));
  1715. /* Make sure PCH DPLL is enabled */
  1716. assert_shared_dpll_enabled(dev_priv,
  1717. intel_crtc_to_shared_dpll(intel_crtc));
  1718. /* FDI must be feeding us bits for PCH ports */
  1719. assert_fdi_tx_enabled(dev_priv, pipe);
  1720. assert_fdi_rx_enabled(dev_priv, pipe);
  1721. if (HAS_PCH_CPT(dev)) {
  1722. /* Workaround: Set the timing override bit before enabling the
  1723. * pch transcoder. */
  1724. reg = TRANS_CHICKEN2(pipe);
  1725. val = I915_READ(reg);
  1726. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1727. I915_WRITE(reg, val);
  1728. }
  1729. reg = PCH_TRANSCONF(pipe);
  1730. val = I915_READ(reg);
  1731. pipeconf_val = I915_READ(PIPECONF(pipe));
  1732. if (HAS_PCH_IBX(dev_priv->dev)) {
  1733. /*
  1734. * Make the BPC in transcoder be consistent with
  1735. * that in pipeconf reg. For HDMI we must use 8bpc
  1736. * here for both 8bpc and 12bpc.
  1737. */
  1738. val &= ~PIPECONF_BPC_MASK;
  1739. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1740. val |= PIPECONF_8BPC;
  1741. else
  1742. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1743. }
  1744. val &= ~TRANS_INTERLACE_MASK;
  1745. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1746. if (HAS_PCH_IBX(dev_priv->dev) &&
  1747. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1748. val |= TRANS_LEGACY_INTERLACED_ILK;
  1749. else
  1750. val |= TRANS_INTERLACED;
  1751. else
  1752. val |= TRANS_PROGRESSIVE;
  1753. I915_WRITE(reg, val | TRANS_ENABLE);
  1754. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1755. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1756. }
  1757. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1758. enum transcoder cpu_transcoder)
  1759. {
  1760. u32 val, pipeconf_val;
  1761. /* PCH only available on ILK+ */
  1762. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1763. /* FDI must be feeding us bits for PCH ports */
  1764. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1765. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1766. /* Workaround: set timing override bit. */
  1767. val = I915_READ(_TRANSA_CHICKEN2);
  1768. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1769. I915_WRITE(_TRANSA_CHICKEN2, val);
  1770. val = TRANS_ENABLE;
  1771. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1772. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1773. PIPECONF_INTERLACED_ILK)
  1774. val |= TRANS_INTERLACED;
  1775. else
  1776. val |= TRANS_PROGRESSIVE;
  1777. I915_WRITE(LPT_TRANSCONF, val);
  1778. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1779. DRM_ERROR("Failed to enable PCH transcoder\n");
  1780. }
  1781. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1782. enum pipe pipe)
  1783. {
  1784. struct drm_device *dev = dev_priv->dev;
  1785. uint32_t reg, val;
  1786. /* FDI relies on the transcoder */
  1787. assert_fdi_tx_disabled(dev_priv, pipe);
  1788. assert_fdi_rx_disabled(dev_priv, pipe);
  1789. /* Ports must be off as well */
  1790. assert_pch_ports_disabled(dev_priv, pipe);
  1791. reg = PCH_TRANSCONF(pipe);
  1792. val = I915_READ(reg);
  1793. val &= ~TRANS_ENABLE;
  1794. I915_WRITE(reg, val);
  1795. /* wait for PCH transcoder off, transcoder state */
  1796. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1797. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1798. if (!HAS_PCH_IBX(dev)) {
  1799. /* Workaround: Clear the timing override chicken bit again. */
  1800. reg = TRANS_CHICKEN2(pipe);
  1801. val = I915_READ(reg);
  1802. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1803. I915_WRITE(reg, val);
  1804. }
  1805. }
  1806. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1807. {
  1808. u32 val;
  1809. val = I915_READ(LPT_TRANSCONF);
  1810. val &= ~TRANS_ENABLE;
  1811. I915_WRITE(LPT_TRANSCONF, val);
  1812. /* wait for PCH transcoder off, transcoder state */
  1813. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1814. DRM_ERROR("Failed to disable PCH transcoder\n");
  1815. /* Workaround: clear timing override bit. */
  1816. val = I915_READ(_TRANSA_CHICKEN2);
  1817. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1818. I915_WRITE(_TRANSA_CHICKEN2, val);
  1819. }
  1820. /**
  1821. * intel_enable_pipe - enable a pipe, asserting requirements
  1822. * @crtc: crtc responsible for the pipe
  1823. *
  1824. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1825. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1826. */
  1827. static void intel_enable_pipe(struct intel_crtc *crtc)
  1828. {
  1829. struct drm_device *dev = crtc->base.dev;
  1830. struct drm_i915_private *dev_priv = dev->dev_private;
  1831. enum pipe pipe = crtc->pipe;
  1832. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1833. pipe);
  1834. enum pipe pch_transcoder;
  1835. int reg;
  1836. u32 val;
  1837. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1838. assert_planes_disabled(dev_priv, pipe);
  1839. assert_cursor_disabled(dev_priv, pipe);
  1840. assert_sprites_disabled(dev_priv, pipe);
  1841. if (HAS_PCH_LPT(dev_priv->dev))
  1842. pch_transcoder = TRANSCODER_A;
  1843. else
  1844. pch_transcoder = pipe;
  1845. /*
  1846. * A pipe without a PLL won't actually be able to drive bits from
  1847. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1848. * need the check.
  1849. */
  1850. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1851. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1852. assert_dsi_pll_enabled(dev_priv);
  1853. else
  1854. assert_pll_enabled(dev_priv, pipe);
  1855. else {
  1856. if (crtc->config->has_pch_encoder) {
  1857. /* if driving the PCH, we need FDI enabled */
  1858. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1859. assert_fdi_tx_pll_enabled(dev_priv,
  1860. (enum pipe) cpu_transcoder);
  1861. }
  1862. /* FIXME: assert CPU port conditions for SNB+ */
  1863. }
  1864. reg = PIPECONF(cpu_transcoder);
  1865. val = I915_READ(reg);
  1866. if (val & PIPECONF_ENABLE) {
  1867. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1868. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1869. return;
  1870. }
  1871. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1872. POSTING_READ(reg);
  1873. }
  1874. /**
  1875. * intel_disable_pipe - disable a pipe, asserting requirements
  1876. * @crtc: crtc whose pipes is to be disabled
  1877. *
  1878. * Disable the pipe of @crtc, making sure that various hardware
  1879. * specific requirements are met, if applicable, e.g. plane
  1880. * disabled, panel fitter off, etc.
  1881. *
  1882. * Will wait until the pipe has shut down before returning.
  1883. */
  1884. static void intel_disable_pipe(struct intel_crtc *crtc)
  1885. {
  1886. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1887. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1888. enum pipe pipe = crtc->pipe;
  1889. int reg;
  1890. u32 val;
  1891. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1892. /*
  1893. * Make sure planes won't keep trying to pump pixels to us,
  1894. * or we might hang the display.
  1895. */
  1896. assert_planes_disabled(dev_priv, pipe);
  1897. assert_cursor_disabled(dev_priv, pipe);
  1898. assert_sprites_disabled(dev_priv, pipe);
  1899. reg = PIPECONF(cpu_transcoder);
  1900. val = I915_READ(reg);
  1901. if ((val & PIPECONF_ENABLE) == 0)
  1902. return;
  1903. /*
  1904. * Double wide has implications for planes
  1905. * so best keep it disabled when not needed.
  1906. */
  1907. if (crtc->config->double_wide)
  1908. val &= ~PIPECONF_DOUBLE_WIDE;
  1909. /* Don't disable pipe or pipe PLLs if needed */
  1910. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1911. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1912. val &= ~PIPECONF_ENABLE;
  1913. I915_WRITE(reg, val);
  1914. if ((val & PIPECONF_ENABLE) == 0)
  1915. intel_wait_for_pipe_off(crtc);
  1916. }
  1917. static bool need_vtd_wa(struct drm_device *dev)
  1918. {
  1919. #ifdef CONFIG_INTEL_IOMMU
  1920. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1921. return true;
  1922. #endif
  1923. return false;
  1924. }
  1925. unsigned int
  1926. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1927. uint64_t fb_format_modifier)
  1928. {
  1929. unsigned int tile_height;
  1930. uint32_t pixel_bytes;
  1931. switch (fb_format_modifier) {
  1932. case DRM_FORMAT_MOD_NONE:
  1933. tile_height = 1;
  1934. break;
  1935. case I915_FORMAT_MOD_X_TILED:
  1936. tile_height = IS_GEN2(dev) ? 16 : 8;
  1937. break;
  1938. case I915_FORMAT_MOD_Y_TILED:
  1939. tile_height = 32;
  1940. break;
  1941. case I915_FORMAT_MOD_Yf_TILED:
  1942. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1943. switch (pixel_bytes) {
  1944. default:
  1945. case 1:
  1946. tile_height = 64;
  1947. break;
  1948. case 2:
  1949. case 4:
  1950. tile_height = 32;
  1951. break;
  1952. case 8:
  1953. tile_height = 16;
  1954. break;
  1955. case 16:
  1956. WARN_ONCE(1,
  1957. "128-bit pixels are not supported for display!");
  1958. tile_height = 16;
  1959. break;
  1960. }
  1961. break;
  1962. default:
  1963. MISSING_CASE(fb_format_modifier);
  1964. tile_height = 1;
  1965. break;
  1966. }
  1967. return tile_height;
  1968. }
  1969. unsigned int
  1970. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1971. uint32_t pixel_format, uint64_t fb_format_modifier)
  1972. {
  1973. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1974. fb_format_modifier));
  1975. }
  1976. static int
  1977. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1978. const struct drm_plane_state *plane_state)
  1979. {
  1980. struct intel_rotation_info *info = &view->rotation_info;
  1981. unsigned int tile_height, tile_pitch;
  1982. *view = i915_ggtt_view_normal;
  1983. if (!plane_state)
  1984. return 0;
  1985. if (!intel_rotation_90_or_270(plane_state->rotation))
  1986. return 0;
  1987. *view = i915_ggtt_view_rotated;
  1988. info->height = fb->height;
  1989. info->pixel_format = fb->pixel_format;
  1990. info->pitch = fb->pitches[0];
  1991. info->fb_modifier = fb->modifier[0];
  1992. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1993. fb->modifier[0]);
  1994. tile_pitch = PAGE_SIZE / tile_height;
  1995. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1996. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1997. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1998. return 0;
  1999. }
  2000. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  2001. {
  2002. if (INTEL_INFO(dev_priv)->gen >= 9)
  2003. return 256 * 1024;
  2004. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  2005. IS_VALLEYVIEW(dev_priv))
  2006. return 128 * 1024;
  2007. else if (INTEL_INFO(dev_priv)->gen >= 4)
  2008. return 4 * 1024;
  2009. else
  2010. return 0;
  2011. }
  2012. int
  2013. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2014. struct drm_framebuffer *fb,
  2015. const struct drm_plane_state *plane_state,
  2016. struct intel_engine_cs *pipelined,
  2017. struct drm_i915_gem_request **pipelined_request)
  2018. {
  2019. struct drm_device *dev = fb->dev;
  2020. struct drm_i915_private *dev_priv = dev->dev_private;
  2021. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2022. struct i915_ggtt_view view;
  2023. u32 alignment;
  2024. int ret;
  2025. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2026. switch (fb->modifier[0]) {
  2027. case DRM_FORMAT_MOD_NONE:
  2028. alignment = intel_linear_alignment(dev_priv);
  2029. break;
  2030. case I915_FORMAT_MOD_X_TILED:
  2031. if (INTEL_INFO(dev)->gen >= 9)
  2032. alignment = 256 * 1024;
  2033. else {
  2034. /* pin() will align the object as required by fence */
  2035. alignment = 0;
  2036. }
  2037. break;
  2038. case I915_FORMAT_MOD_Y_TILED:
  2039. case I915_FORMAT_MOD_Yf_TILED:
  2040. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2041. "Y tiling bo slipped through, driver bug!\n"))
  2042. return -EINVAL;
  2043. alignment = 1 * 1024 * 1024;
  2044. break;
  2045. default:
  2046. MISSING_CASE(fb->modifier[0]);
  2047. return -EINVAL;
  2048. }
  2049. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2050. if (ret)
  2051. return ret;
  2052. /* Note that the w/a also requires 64 PTE of padding following the
  2053. * bo. We currently fill all unused PTE with the shadow page and so
  2054. * we should always have valid PTE following the scanout preventing
  2055. * the VT-d warning.
  2056. */
  2057. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2058. alignment = 256 * 1024;
  2059. /*
  2060. * Global gtt pte registers are special registers which actually forward
  2061. * writes to a chunk of system memory. Which means that there is no risk
  2062. * that the register values disappear as soon as we call
  2063. * intel_runtime_pm_put(), so it is correct to wrap only the
  2064. * pin/unpin/fence and not more.
  2065. */
  2066. intel_runtime_pm_get(dev_priv);
  2067. dev_priv->mm.interruptible = false;
  2068. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2069. pipelined_request, &view);
  2070. if (ret)
  2071. goto err_interruptible;
  2072. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2073. * fence, whereas 965+ only requires a fence if using
  2074. * framebuffer compression. For simplicity, we always install
  2075. * a fence as the cost is not that onerous.
  2076. */
  2077. ret = i915_gem_object_get_fence(obj);
  2078. if (ret == -EDEADLK) {
  2079. /*
  2080. * -EDEADLK means there are no free fences
  2081. * no pending flips.
  2082. *
  2083. * This is propagated to atomic, but it uses
  2084. * -EDEADLK to force a locking recovery, so
  2085. * change the returned error to -EBUSY.
  2086. */
  2087. ret = -EBUSY;
  2088. goto err_unpin;
  2089. } else if (ret)
  2090. goto err_unpin;
  2091. i915_gem_object_pin_fence(obj);
  2092. dev_priv->mm.interruptible = true;
  2093. intel_runtime_pm_put(dev_priv);
  2094. return 0;
  2095. err_unpin:
  2096. i915_gem_object_unpin_from_display_plane(obj, &view);
  2097. err_interruptible:
  2098. dev_priv->mm.interruptible = true;
  2099. intel_runtime_pm_put(dev_priv);
  2100. return ret;
  2101. }
  2102. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2103. const struct drm_plane_state *plane_state)
  2104. {
  2105. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2106. struct i915_ggtt_view view;
  2107. int ret;
  2108. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2109. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2110. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2111. i915_gem_object_unpin_fence(obj);
  2112. i915_gem_object_unpin_from_display_plane(obj, &view);
  2113. }
  2114. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2115. * is assumed to be a power-of-two. */
  2116. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2117. int *x, int *y,
  2118. unsigned int tiling_mode,
  2119. unsigned int cpp,
  2120. unsigned int pitch)
  2121. {
  2122. if (tiling_mode != I915_TILING_NONE) {
  2123. unsigned int tile_rows, tiles;
  2124. tile_rows = *y / 8;
  2125. *y %= 8;
  2126. tiles = *x / (512/cpp);
  2127. *x %= 512/cpp;
  2128. return tile_rows * pitch * 8 + tiles * 4096;
  2129. } else {
  2130. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2131. unsigned int offset;
  2132. offset = *y * pitch + *x * cpp;
  2133. *y = (offset & alignment) / pitch;
  2134. *x = ((offset & alignment) - *y * pitch) / cpp;
  2135. return offset & ~alignment;
  2136. }
  2137. }
  2138. static int i9xx_format_to_fourcc(int format)
  2139. {
  2140. switch (format) {
  2141. case DISPPLANE_8BPP:
  2142. return DRM_FORMAT_C8;
  2143. case DISPPLANE_BGRX555:
  2144. return DRM_FORMAT_XRGB1555;
  2145. case DISPPLANE_BGRX565:
  2146. return DRM_FORMAT_RGB565;
  2147. default:
  2148. case DISPPLANE_BGRX888:
  2149. return DRM_FORMAT_XRGB8888;
  2150. case DISPPLANE_RGBX888:
  2151. return DRM_FORMAT_XBGR8888;
  2152. case DISPPLANE_BGRX101010:
  2153. return DRM_FORMAT_XRGB2101010;
  2154. case DISPPLANE_RGBX101010:
  2155. return DRM_FORMAT_XBGR2101010;
  2156. }
  2157. }
  2158. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2159. {
  2160. switch (format) {
  2161. case PLANE_CTL_FORMAT_RGB_565:
  2162. return DRM_FORMAT_RGB565;
  2163. default:
  2164. case PLANE_CTL_FORMAT_XRGB_8888:
  2165. if (rgb_order) {
  2166. if (alpha)
  2167. return DRM_FORMAT_ABGR8888;
  2168. else
  2169. return DRM_FORMAT_XBGR8888;
  2170. } else {
  2171. if (alpha)
  2172. return DRM_FORMAT_ARGB8888;
  2173. else
  2174. return DRM_FORMAT_XRGB8888;
  2175. }
  2176. case PLANE_CTL_FORMAT_XRGB_2101010:
  2177. if (rgb_order)
  2178. return DRM_FORMAT_XBGR2101010;
  2179. else
  2180. return DRM_FORMAT_XRGB2101010;
  2181. }
  2182. }
  2183. static bool
  2184. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2185. struct intel_initial_plane_config *plane_config)
  2186. {
  2187. struct drm_device *dev = crtc->base.dev;
  2188. struct drm_i915_gem_object *obj = NULL;
  2189. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2190. struct drm_framebuffer *fb = &plane_config->fb->base;
  2191. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2192. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2193. PAGE_SIZE);
  2194. size_aligned -= base_aligned;
  2195. if (plane_config->size == 0)
  2196. return false;
  2197. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2198. base_aligned,
  2199. base_aligned,
  2200. size_aligned);
  2201. if (!obj)
  2202. return false;
  2203. obj->tiling_mode = plane_config->tiling;
  2204. if (obj->tiling_mode == I915_TILING_X)
  2205. obj->stride = fb->pitches[0];
  2206. mode_cmd.pixel_format = fb->pixel_format;
  2207. mode_cmd.width = fb->width;
  2208. mode_cmd.height = fb->height;
  2209. mode_cmd.pitches[0] = fb->pitches[0];
  2210. mode_cmd.modifier[0] = fb->modifier[0];
  2211. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2212. mutex_lock(&dev->struct_mutex);
  2213. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2214. &mode_cmd, obj)) {
  2215. DRM_DEBUG_KMS("intel fb init failed\n");
  2216. goto out_unref_obj;
  2217. }
  2218. mutex_unlock(&dev->struct_mutex);
  2219. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2220. return true;
  2221. out_unref_obj:
  2222. drm_gem_object_unreference(&obj->base);
  2223. mutex_unlock(&dev->struct_mutex);
  2224. return false;
  2225. }
  2226. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2227. static void
  2228. update_state_fb(struct drm_plane *plane)
  2229. {
  2230. if (plane->fb == plane->state->fb)
  2231. return;
  2232. if (plane->state->fb)
  2233. drm_framebuffer_unreference(plane->state->fb);
  2234. plane->state->fb = plane->fb;
  2235. if (plane->state->fb)
  2236. drm_framebuffer_reference(plane->state->fb);
  2237. }
  2238. static void
  2239. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2240. struct intel_initial_plane_config *plane_config)
  2241. {
  2242. struct drm_device *dev = intel_crtc->base.dev;
  2243. struct drm_i915_private *dev_priv = dev->dev_private;
  2244. struct drm_crtc *c;
  2245. struct intel_crtc *i;
  2246. struct drm_i915_gem_object *obj;
  2247. struct drm_plane *primary = intel_crtc->base.primary;
  2248. struct drm_plane_state *plane_state = primary->state;
  2249. struct drm_framebuffer *fb;
  2250. if (!plane_config->fb)
  2251. return;
  2252. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2253. fb = &plane_config->fb->base;
  2254. goto valid_fb;
  2255. }
  2256. kfree(plane_config->fb);
  2257. /*
  2258. * Failed to alloc the obj, check to see if we should share
  2259. * an fb with another CRTC instead
  2260. */
  2261. for_each_crtc(dev, c) {
  2262. i = to_intel_crtc(c);
  2263. if (c == &intel_crtc->base)
  2264. continue;
  2265. if (!i->active)
  2266. continue;
  2267. fb = c->primary->fb;
  2268. if (!fb)
  2269. continue;
  2270. obj = intel_fb_obj(fb);
  2271. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2272. drm_framebuffer_reference(fb);
  2273. goto valid_fb;
  2274. }
  2275. }
  2276. return;
  2277. valid_fb:
  2278. plane_state->src_x = plane_state->src_y = 0;
  2279. plane_state->src_w = fb->width << 16;
  2280. plane_state->src_h = fb->height << 16;
  2281. plane_state->crtc_x = plane_state->src_y = 0;
  2282. plane_state->crtc_w = fb->width;
  2283. plane_state->crtc_h = fb->height;
  2284. obj = intel_fb_obj(fb);
  2285. if (obj->tiling_mode != I915_TILING_NONE)
  2286. dev_priv->preserve_bios_swizzle = true;
  2287. drm_framebuffer_reference(fb);
  2288. primary->fb = primary->state->fb = fb;
  2289. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2290. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2291. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2292. }
  2293. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2294. struct drm_framebuffer *fb,
  2295. int x, int y)
  2296. {
  2297. struct drm_device *dev = crtc->dev;
  2298. struct drm_i915_private *dev_priv = dev->dev_private;
  2299. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2300. struct drm_plane *primary = crtc->primary;
  2301. bool visible = to_intel_plane_state(primary->state)->visible;
  2302. struct drm_i915_gem_object *obj;
  2303. int plane = intel_crtc->plane;
  2304. unsigned long linear_offset;
  2305. u32 dspcntr;
  2306. u32 reg = DSPCNTR(plane);
  2307. int pixel_size;
  2308. if (!visible || !fb) {
  2309. I915_WRITE(reg, 0);
  2310. if (INTEL_INFO(dev)->gen >= 4)
  2311. I915_WRITE(DSPSURF(plane), 0);
  2312. else
  2313. I915_WRITE(DSPADDR(plane), 0);
  2314. POSTING_READ(reg);
  2315. return;
  2316. }
  2317. obj = intel_fb_obj(fb);
  2318. if (WARN_ON(obj == NULL))
  2319. return;
  2320. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2321. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2322. dspcntr |= DISPLAY_PLANE_ENABLE;
  2323. if (INTEL_INFO(dev)->gen < 4) {
  2324. if (intel_crtc->pipe == PIPE_B)
  2325. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2326. /* pipesrc and dspsize control the size that is scaled from,
  2327. * which should always be the user's requested size.
  2328. */
  2329. I915_WRITE(DSPSIZE(plane),
  2330. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2331. (intel_crtc->config->pipe_src_w - 1));
  2332. I915_WRITE(DSPPOS(plane), 0);
  2333. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2334. I915_WRITE(PRIMSIZE(plane),
  2335. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2336. (intel_crtc->config->pipe_src_w - 1));
  2337. I915_WRITE(PRIMPOS(plane), 0);
  2338. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2339. }
  2340. switch (fb->pixel_format) {
  2341. case DRM_FORMAT_C8:
  2342. dspcntr |= DISPPLANE_8BPP;
  2343. break;
  2344. case DRM_FORMAT_XRGB1555:
  2345. dspcntr |= DISPPLANE_BGRX555;
  2346. break;
  2347. case DRM_FORMAT_RGB565:
  2348. dspcntr |= DISPPLANE_BGRX565;
  2349. break;
  2350. case DRM_FORMAT_XRGB8888:
  2351. dspcntr |= DISPPLANE_BGRX888;
  2352. break;
  2353. case DRM_FORMAT_XBGR8888:
  2354. dspcntr |= DISPPLANE_RGBX888;
  2355. break;
  2356. case DRM_FORMAT_XRGB2101010:
  2357. dspcntr |= DISPPLANE_BGRX101010;
  2358. break;
  2359. case DRM_FORMAT_XBGR2101010:
  2360. dspcntr |= DISPPLANE_RGBX101010;
  2361. break;
  2362. default:
  2363. BUG();
  2364. }
  2365. if (INTEL_INFO(dev)->gen >= 4 &&
  2366. obj->tiling_mode != I915_TILING_NONE)
  2367. dspcntr |= DISPPLANE_TILED;
  2368. if (IS_G4X(dev))
  2369. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2370. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2371. if (INTEL_INFO(dev)->gen >= 4) {
  2372. intel_crtc->dspaddr_offset =
  2373. intel_gen4_compute_page_offset(dev_priv,
  2374. &x, &y, obj->tiling_mode,
  2375. pixel_size,
  2376. fb->pitches[0]);
  2377. linear_offset -= intel_crtc->dspaddr_offset;
  2378. } else {
  2379. intel_crtc->dspaddr_offset = linear_offset;
  2380. }
  2381. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2382. dspcntr |= DISPPLANE_ROTATE_180;
  2383. x += (intel_crtc->config->pipe_src_w - 1);
  2384. y += (intel_crtc->config->pipe_src_h - 1);
  2385. /* Finding the last pixel of the last line of the display
  2386. data and adding to linear_offset*/
  2387. linear_offset +=
  2388. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2389. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2390. }
  2391. I915_WRITE(reg, dspcntr);
  2392. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2393. if (INTEL_INFO(dev)->gen >= 4) {
  2394. I915_WRITE(DSPSURF(plane),
  2395. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2396. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2397. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2398. } else
  2399. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2400. POSTING_READ(reg);
  2401. }
  2402. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2403. struct drm_framebuffer *fb,
  2404. int x, int y)
  2405. {
  2406. struct drm_device *dev = crtc->dev;
  2407. struct drm_i915_private *dev_priv = dev->dev_private;
  2408. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2409. struct drm_plane *primary = crtc->primary;
  2410. bool visible = to_intel_plane_state(primary->state)->visible;
  2411. struct drm_i915_gem_object *obj;
  2412. int plane = intel_crtc->plane;
  2413. unsigned long linear_offset;
  2414. u32 dspcntr;
  2415. u32 reg = DSPCNTR(plane);
  2416. int pixel_size;
  2417. if (!visible || !fb) {
  2418. I915_WRITE(reg, 0);
  2419. I915_WRITE(DSPSURF(plane), 0);
  2420. POSTING_READ(reg);
  2421. return;
  2422. }
  2423. obj = intel_fb_obj(fb);
  2424. if (WARN_ON(obj == NULL))
  2425. return;
  2426. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2427. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2428. dspcntr |= DISPLAY_PLANE_ENABLE;
  2429. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2430. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2431. switch (fb->pixel_format) {
  2432. case DRM_FORMAT_C8:
  2433. dspcntr |= DISPPLANE_8BPP;
  2434. break;
  2435. case DRM_FORMAT_RGB565:
  2436. dspcntr |= DISPPLANE_BGRX565;
  2437. break;
  2438. case DRM_FORMAT_XRGB8888:
  2439. dspcntr |= DISPPLANE_BGRX888;
  2440. break;
  2441. case DRM_FORMAT_XBGR8888:
  2442. dspcntr |= DISPPLANE_RGBX888;
  2443. break;
  2444. case DRM_FORMAT_XRGB2101010:
  2445. dspcntr |= DISPPLANE_BGRX101010;
  2446. break;
  2447. case DRM_FORMAT_XBGR2101010:
  2448. dspcntr |= DISPPLANE_RGBX101010;
  2449. break;
  2450. default:
  2451. BUG();
  2452. }
  2453. if (obj->tiling_mode != I915_TILING_NONE)
  2454. dspcntr |= DISPPLANE_TILED;
  2455. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2456. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2457. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2458. intel_crtc->dspaddr_offset =
  2459. intel_gen4_compute_page_offset(dev_priv,
  2460. &x, &y, obj->tiling_mode,
  2461. pixel_size,
  2462. fb->pitches[0]);
  2463. linear_offset -= intel_crtc->dspaddr_offset;
  2464. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2465. dspcntr |= DISPPLANE_ROTATE_180;
  2466. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2467. x += (intel_crtc->config->pipe_src_w - 1);
  2468. y += (intel_crtc->config->pipe_src_h - 1);
  2469. /* Finding the last pixel of the last line of the display
  2470. data and adding to linear_offset*/
  2471. linear_offset +=
  2472. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2473. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2474. }
  2475. }
  2476. I915_WRITE(reg, dspcntr);
  2477. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2478. I915_WRITE(DSPSURF(plane),
  2479. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2480. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2481. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2482. } else {
  2483. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2484. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2485. }
  2486. POSTING_READ(reg);
  2487. }
  2488. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2489. uint32_t pixel_format)
  2490. {
  2491. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2492. /*
  2493. * The stride is either expressed as a multiple of 64 bytes
  2494. * chunks for linear buffers or in number of tiles for tiled
  2495. * buffers.
  2496. */
  2497. switch (fb_modifier) {
  2498. case DRM_FORMAT_MOD_NONE:
  2499. return 64;
  2500. case I915_FORMAT_MOD_X_TILED:
  2501. if (INTEL_INFO(dev)->gen == 2)
  2502. return 128;
  2503. return 512;
  2504. case I915_FORMAT_MOD_Y_TILED:
  2505. /* No need to check for old gens and Y tiling since this is
  2506. * about the display engine and those will be blocked before
  2507. * we get here.
  2508. */
  2509. return 128;
  2510. case I915_FORMAT_MOD_Yf_TILED:
  2511. if (bits_per_pixel == 8)
  2512. return 64;
  2513. else
  2514. return 128;
  2515. default:
  2516. MISSING_CASE(fb_modifier);
  2517. return 64;
  2518. }
  2519. }
  2520. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2521. struct drm_i915_gem_object *obj)
  2522. {
  2523. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2524. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2525. view = &i915_ggtt_view_rotated;
  2526. return i915_gem_obj_ggtt_offset_view(obj, view);
  2527. }
  2528. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2529. {
  2530. struct drm_device *dev = intel_crtc->base.dev;
  2531. struct drm_i915_private *dev_priv = dev->dev_private;
  2532. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2533. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2534. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2535. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2536. intel_crtc->base.base.id, intel_crtc->pipe, id);
  2537. }
  2538. /*
  2539. * This function detaches (aka. unbinds) unused scalers in hardware
  2540. */
  2541. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2542. {
  2543. struct intel_crtc_scaler_state *scaler_state;
  2544. int i;
  2545. scaler_state = &intel_crtc->config->scaler_state;
  2546. /* loop through and disable scalers that aren't in use */
  2547. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2548. if (!scaler_state->scalers[i].in_use)
  2549. skl_detach_scaler(intel_crtc, i);
  2550. }
  2551. }
  2552. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2553. {
  2554. switch (pixel_format) {
  2555. case DRM_FORMAT_C8:
  2556. return PLANE_CTL_FORMAT_INDEXED;
  2557. case DRM_FORMAT_RGB565:
  2558. return PLANE_CTL_FORMAT_RGB_565;
  2559. case DRM_FORMAT_XBGR8888:
  2560. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2561. case DRM_FORMAT_XRGB8888:
  2562. return PLANE_CTL_FORMAT_XRGB_8888;
  2563. /*
  2564. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2565. * to be already pre-multiplied. We need to add a knob (or a different
  2566. * DRM_FORMAT) for user-space to configure that.
  2567. */
  2568. case DRM_FORMAT_ABGR8888:
  2569. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2570. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2571. case DRM_FORMAT_ARGB8888:
  2572. return PLANE_CTL_FORMAT_XRGB_8888 |
  2573. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2574. case DRM_FORMAT_XRGB2101010:
  2575. return PLANE_CTL_FORMAT_XRGB_2101010;
  2576. case DRM_FORMAT_XBGR2101010:
  2577. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2578. case DRM_FORMAT_YUYV:
  2579. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2580. case DRM_FORMAT_YVYU:
  2581. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2582. case DRM_FORMAT_UYVY:
  2583. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2584. case DRM_FORMAT_VYUY:
  2585. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2586. default:
  2587. MISSING_CASE(pixel_format);
  2588. }
  2589. return 0;
  2590. }
  2591. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2592. {
  2593. switch (fb_modifier) {
  2594. case DRM_FORMAT_MOD_NONE:
  2595. break;
  2596. case I915_FORMAT_MOD_X_TILED:
  2597. return PLANE_CTL_TILED_X;
  2598. case I915_FORMAT_MOD_Y_TILED:
  2599. return PLANE_CTL_TILED_Y;
  2600. case I915_FORMAT_MOD_Yf_TILED:
  2601. return PLANE_CTL_TILED_YF;
  2602. default:
  2603. MISSING_CASE(fb_modifier);
  2604. }
  2605. return 0;
  2606. }
  2607. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2608. {
  2609. switch (rotation) {
  2610. case BIT(DRM_ROTATE_0):
  2611. break;
  2612. /*
  2613. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2614. * while i915 HW rotation is clockwise, thats why this swapping.
  2615. */
  2616. case BIT(DRM_ROTATE_90):
  2617. return PLANE_CTL_ROTATE_270;
  2618. case BIT(DRM_ROTATE_180):
  2619. return PLANE_CTL_ROTATE_180;
  2620. case BIT(DRM_ROTATE_270):
  2621. return PLANE_CTL_ROTATE_90;
  2622. default:
  2623. MISSING_CASE(rotation);
  2624. }
  2625. return 0;
  2626. }
  2627. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2628. struct drm_framebuffer *fb,
  2629. int x, int y)
  2630. {
  2631. struct drm_device *dev = crtc->dev;
  2632. struct drm_i915_private *dev_priv = dev->dev_private;
  2633. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2634. struct drm_plane *plane = crtc->primary;
  2635. bool visible = to_intel_plane_state(plane->state)->visible;
  2636. struct drm_i915_gem_object *obj;
  2637. int pipe = intel_crtc->pipe;
  2638. u32 plane_ctl, stride_div, stride;
  2639. u32 tile_height, plane_offset, plane_size;
  2640. unsigned int rotation;
  2641. int x_offset, y_offset;
  2642. unsigned long surf_addr;
  2643. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2644. struct intel_plane_state *plane_state;
  2645. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2646. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2647. int scaler_id = -1;
  2648. plane_state = to_intel_plane_state(plane->state);
  2649. if (!visible || !fb) {
  2650. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2651. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2652. POSTING_READ(PLANE_CTL(pipe, 0));
  2653. return;
  2654. }
  2655. plane_ctl = PLANE_CTL_ENABLE |
  2656. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2657. PLANE_CTL_PIPE_CSC_ENABLE;
  2658. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2659. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2660. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2661. rotation = plane->state->rotation;
  2662. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2663. obj = intel_fb_obj(fb);
  2664. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2665. fb->pixel_format);
  2666. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2667. /*
  2668. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2669. * update_plane helpers are called from legacy paths.
  2670. * Once full atomic crtc is available, below check can be avoided.
  2671. */
  2672. if (drm_rect_width(&plane_state->src)) {
  2673. scaler_id = plane_state->scaler_id;
  2674. src_x = plane_state->src.x1 >> 16;
  2675. src_y = plane_state->src.y1 >> 16;
  2676. src_w = drm_rect_width(&plane_state->src) >> 16;
  2677. src_h = drm_rect_height(&plane_state->src) >> 16;
  2678. dst_x = plane_state->dst.x1;
  2679. dst_y = plane_state->dst.y1;
  2680. dst_w = drm_rect_width(&plane_state->dst);
  2681. dst_h = drm_rect_height(&plane_state->dst);
  2682. WARN_ON(x != src_x || y != src_y);
  2683. } else {
  2684. src_w = intel_crtc->config->pipe_src_w;
  2685. src_h = intel_crtc->config->pipe_src_h;
  2686. }
  2687. if (intel_rotation_90_or_270(rotation)) {
  2688. /* stride = Surface height in tiles */
  2689. tile_height = intel_tile_height(dev, fb->pixel_format,
  2690. fb->modifier[0]);
  2691. stride = DIV_ROUND_UP(fb->height, tile_height);
  2692. x_offset = stride * tile_height - y - src_h;
  2693. y_offset = x;
  2694. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2695. } else {
  2696. stride = fb->pitches[0] / stride_div;
  2697. x_offset = x;
  2698. y_offset = y;
  2699. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2700. }
  2701. plane_offset = y_offset << 16 | x_offset;
  2702. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2703. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2704. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2705. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2706. if (scaler_id >= 0) {
  2707. uint32_t ps_ctrl = 0;
  2708. WARN_ON(!dst_w || !dst_h);
  2709. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2710. crtc_state->scaler_state.scalers[scaler_id].mode;
  2711. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2712. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2713. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2714. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2715. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2716. } else {
  2717. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2718. }
  2719. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2720. POSTING_READ(PLANE_SURF(pipe, 0));
  2721. }
  2722. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2723. static int
  2724. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2725. int x, int y, enum mode_set_atomic state)
  2726. {
  2727. struct drm_device *dev = crtc->dev;
  2728. struct drm_i915_private *dev_priv = dev->dev_private;
  2729. if (dev_priv->fbc.disable_fbc)
  2730. dev_priv->fbc.disable_fbc(dev_priv);
  2731. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2732. return 0;
  2733. }
  2734. static void intel_complete_page_flips(struct drm_device *dev)
  2735. {
  2736. struct drm_crtc *crtc;
  2737. for_each_crtc(dev, crtc) {
  2738. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2739. enum plane plane = intel_crtc->plane;
  2740. intel_prepare_page_flip(dev, plane);
  2741. intel_finish_page_flip_plane(dev, plane);
  2742. }
  2743. }
  2744. static void intel_update_primary_planes(struct drm_device *dev)
  2745. {
  2746. struct drm_i915_private *dev_priv = dev->dev_private;
  2747. struct drm_crtc *crtc;
  2748. for_each_crtc(dev, crtc) {
  2749. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2750. drm_modeset_lock(&crtc->mutex, NULL);
  2751. /*
  2752. * FIXME: Once we have proper support for primary planes (and
  2753. * disabling them without disabling the entire crtc) allow again
  2754. * a NULL crtc->primary->fb.
  2755. */
  2756. if (intel_crtc->active && crtc->primary->fb)
  2757. dev_priv->display.update_primary_plane(crtc,
  2758. crtc->primary->fb,
  2759. crtc->x,
  2760. crtc->y);
  2761. drm_modeset_unlock(&crtc->mutex);
  2762. }
  2763. }
  2764. void intel_prepare_reset(struct drm_device *dev)
  2765. {
  2766. /* no reset support for gen2 */
  2767. if (IS_GEN2(dev))
  2768. return;
  2769. /* reset doesn't touch the display */
  2770. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2771. return;
  2772. drm_modeset_lock_all(dev);
  2773. /*
  2774. * Disabling the crtcs gracefully seems nicer. Also the
  2775. * g33 docs say we should at least disable all the planes.
  2776. */
  2777. intel_display_suspend(dev);
  2778. }
  2779. void intel_finish_reset(struct drm_device *dev)
  2780. {
  2781. struct drm_i915_private *dev_priv = to_i915(dev);
  2782. /*
  2783. * Flips in the rings will be nuked by the reset,
  2784. * so complete all pending flips so that user space
  2785. * will get its events and not get stuck.
  2786. */
  2787. intel_complete_page_flips(dev);
  2788. /* no reset support for gen2 */
  2789. if (IS_GEN2(dev))
  2790. return;
  2791. /* reset doesn't touch the display */
  2792. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2793. /*
  2794. * Flips in the rings have been nuked by the reset,
  2795. * so update the base address of all primary
  2796. * planes to the the last fb to make sure we're
  2797. * showing the correct fb after a reset.
  2798. */
  2799. intel_update_primary_planes(dev);
  2800. return;
  2801. }
  2802. /*
  2803. * The display has been reset as well,
  2804. * so need a full re-initialization.
  2805. */
  2806. intel_runtime_pm_disable_interrupts(dev_priv);
  2807. intel_runtime_pm_enable_interrupts(dev_priv);
  2808. intel_modeset_init_hw(dev);
  2809. spin_lock_irq(&dev_priv->irq_lock);
  2810. if (dev_priv->display.hpd_irq_setup)
  2811. dev_priv->display.hpd_irq_setup(dev);
  2812. spin_unlock_irq(&dev_priv->irq_lock);
  2813. intel_display_resume(dev);
  2814. intel_hpd_init(dev_priv);
  2815. drm_modeset_unlock_all(dev);
  2816. }
  2817. static void
  2818. intel_finish_fb(struct drm_framebuffer *old_fb)
  2819. {
  2820. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2821. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2822. bool was_interruptible = dev_priv->mm.interruptible;
  2823. int ret;
  2824. /* Big Hammer, we also need to ensure that any pending
  2825. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2826. * current scanout is retired before unpinning the old
  2827. * framebuffer. Note that we rely on userspace rendering
  2828. * into the buffer attached to the pipe they are waiting
  2829. * on. If not, userspace generates a GPU hang with IPEHR
  2830. * point to the MI_WAIT_FOR_EVENT.
  2831. *
  2832. * This should only fail upon a hung GPU, in which case we
  2833. * can safely continue.
  2834. */
  2835. dev_priv->mm.interruptible = false;
  2836. ret = i915_gem_object_wait_rendering(obj, true);
  2837. dev_priv->mm.interruptible = was_interruptible;
  2838. WARN_ON(ret);
  2839. }
  2840. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2841. {
  2842. struct drm_device *dev = crtc->dev;
  2843. struct drm_i915_private *dev_priv = dev->dev_private;
  2844. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2845. bool pending;
  2846. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2847. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2848. return false;
  2849. spin_lock_irq(&dev->event_lock);
  2850. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2851. spin_unlock_irq(&dev->event_lock);
  2852. return pending;
  2853. }
  2854. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2855. {
  2856. struct drm_device *dev = crtc->base.dev;
  2857. struct drm_i915_private *dev_priv = dev->dev_private;
  2858. const struct drm_display_mode *adjusted_mode;
  2859. if (!i915.fastboot)
  2860. return;
  2861. /*
  2862. * Update pipe size and adjust fitter if needed: the reason for this is
  2863. * that in compute_mode_changes we check the native mode (not the pfit
  2864. * mode) to see if we can flip rather than do a full mode set. In the
  2865. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2866. * pfit state, we'll end up with a big fb scanned out into the wrong
  2867. * sized surface.
  2868. *
  2869. * To fix this properly, we need to hoist the checks up into
  2870. * compute_mode_changes (or above), check the actual pfit state and
  2871. * whether the platform allows pfit disable with pipe active, and only
  2872. * then update the pipesrc and pfit state, even on the flip path.
  2873. */
  2874. adjusted_mode = &crtc->config->base.adjusted_mode;
  2875. I915_WRITE(PIPESRC(crtc->pipe),
  2876. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2877. (adjusted_mode->crtc_vdisplay - 1));
  2878. if (!crtc->config->pch_pfit.enabled &&
  2879. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2880. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2881. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2882. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2883. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2884. }
  2885. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2886. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2887. }
  2888. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2889. {
  2890. struct drm_device *dev = crtc->dev;
  2891. struct drm_i915_private *dev_priv = dev->dev_private;
  2892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2893. int pipe = intel_crtc->pipe;
  2894. u32 reg, temp;
  2895. /* enable normal train */
  2896. reg = FDI_TX_CTL(pipe);
  2897. temp = I915_READ(reg);
  2898. if (IS_IVYBRIDGE(dev)) {
  2899. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2900. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2901. } else {
  2902. temp &= ~FDI_LINK_TRAIN_NONE;
  2903. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2904. }
  2905. I915_WRITE(reg, temp);
  2906. reg = FDI_RX_CTL(pipe);
  2907. temp = I915_READ(reg);
  2908. if (HAS_PCH_CPT(dev)) {
  2909. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2910. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2911. } else {
  2912. temp &= ~FDI_LINK_TRAIN_NONE;
  2913. temp |= FDI_LINK_TRAIN_NONE;
  2914. }
  2915. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2916. /* wait one idle pattern time */
  2917. POSTING_READ(reg);
  2918. udelay(1000);
  2919. /* IVB wants error correction enabled */
  2920. if (IS_IVYBRIDGE(dev))
  2921. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2922. FDI_FE_ERRC_ENABLE);
  2923. }
  2924. /* The FDI link training functions for ILK/Ibexpeak. */
  2925. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2926. {
  2927. struct drm_device *dev = crtc->dev;
  2928. struct drm_i915_private *dev_priv = dev->dev_private;
  2929. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2930. int pipe = intel_crtc->pipe;
  2931. u32 reg, temp, tries;
  2932. /* FDI needs bits from pipe first */
  2933. assert_pipe_enabled(dev_priv, pipe);
  2934. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2935. for train result */
  2936. reg = FDI_RX_IMR(pipe);
  2937. temp = I915_READ(reg);
  2938. temp &= ~FDI_RX_SYMBOL_LOCK;
  2939. temp &= ~FDI_RX_BIT_LOCK;
  2940. I915_WRITE(reg, temp);
  2941. I915_READ(reg);
  2942. udelay(150);
  2943. /* enable CPU FDI TX and PCH FDI RX */
  2944. reg = FDI_TX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2947. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2948. temp &= ~FDI_LINK_TRAIN_NONE;
  2949. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2950. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2951. reg = FDI_RX_CTL(pipe);
  2952. temp = I915_READ(reg);
  2953. temp &= ~FDI_LINK_TRAIN_NONE;
  2954. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2955. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2956. POSTING_READ(reg);
  2957. udelay(150);
  2958. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2959. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2960. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2961. FDI_RX_PHASE_SYNC_POINTER_EN);
  2962. reg = FDI_RX_IIR(pipe);
  2963. for (tries = 0; tries < 5; tries++) {
  2964. temp = I915_READ(reg);
  2965. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2966. if ((temp & FDI_RX_BIT_LOCK)) {
  2967. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2968. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2969. break;
  2970. }
  2971. }
  2972. if (tries == 5)
  2973. DRM_ERROR("FDI train 1 fail!\n");
  2974. /* Train 2 */
  2975. reg = FDI_TX_CTL(pipe);
  2976. temp = I915_READ(reg);
  2977. temp &= ~FDI_LINK_TRAIN_NONE;
  2978. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2979. I915_WRITE(reg, temp);
  2980. reg = FDI_RX_CTL(pipe);
  2981. temp = I915_READ(reg);
  2982. temp &= ~FDI_LINK_TRAIN_NONE;
  2983. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2984. I915_WRITE(reg, temp);
  2985. POSTING_READ(reg);
  2986. udelay(150);
  2987. reg = FDI_RX_IIR(pipe);
  2988. for (tries = 0; tries < 5; tries++) {
  2989. temp = I915_READ(reg);
  2990. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2991. if (temp & FDI_RX_SYMBOL_LOCK) {
  2992. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2993. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2994. break;
  2995. }
  2996. }
  2997. if (tries == 5)
  2998. DRM_ERROR("FDI train 2 fail!\n");
  2999. DRM_DEBUG_KMS("FDI train done\n");
  3000. }
  3001. static const int snb_b_fdi_train_param[] = {
  3002. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3003. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3004. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3005. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3006. };
  3007. /* The FDI link training functions for SNB/Cougarpoint. */
  3008. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3009. {
  3010. struct drm_device *dev = crtc->dev;
  3011. struct drm_i915_private *dev_priv = dev->dev_private;
  3012. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3013. int pipe = intel_crtc->pipe;
  3014. u32 reg, temp, i, retry;
  3015. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3016. for train result */
  3017. reg = FDI_RX_IMR(pipe);
  3018. temp = I915_READ(reg);
  3019. temp &= ~FDI_RX_SYMBOL_LOCK;
  3020. temp &= ~FDI_RX_BIT_LOCK;
  3021. I915_WRITE(reg, temp);
  3022. POSTING_READ(reg);
  3023. udelay(150);
  3024. /* enable CPU FDI TX and PCH FDI RX */
  3025. reg = FDI_TX_CTL(pipe);
  3026. temp = I915_READ(reg);
  3027. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3028. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3029. temp &= ~FDI_LINK_TRAIN_NONE;
  3030. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3031. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3032. /* SNB-B */
  3033. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3034. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3035. I915_WRITE(FDI_RX_MISC(pipe),
  3036. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3037. reg = FDI_RX_CTL(pipe);
  3038. temp = I915_READ(reg);
  3039. if (HAS_PCH_CPT(dev)) {
  3040. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3041. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3042. } else {
  3043. temp &= ~FDI_LINK_TRAIN_NONE;
  3044. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3045. }
  3046. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3047. POSTING_READ(reg);
  3048. udelay(150);
  3049. for (i = 0; i < 4; i++) {
  3050. reg = FDI_TX_CTL(pipe);
  3051. temp = I915_READ(reg);
  3052. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3053. temp |= snb_b_fdi_train_param[i];
  3054. I915_WRITE(reg, temp);
  3055. POSTING_READ(reg);
  3056. udelay(500);
  3057. for (retry = 0; retry < 5; retry++) {
  3058. reg = FDI_RX_IIR(pipe);
  3059. temp = I915_READ(reg);
  3060. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3061. if (temp & FDI_RX_BIT_LOCK) {
  3062. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3063. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3064. break;
  3065. }
  3066. udelay(50);
  3067. }
  3068. if (retry < 5)
  3069. break;
  3070. }
  3071. if (i == 4)
  3072. DRM_ERROR("FDI train 1 fail!\n");
  3073. /* Train 2 */
  3074. reg = FDI_TX_CTL(pipe);
  3075. temp = I915_READ(reg);
  3076. temp &= ~FDI_LINK_TRAIN_NONE;
  3077. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3078. if (IS_GEN6(dev)) {
  3079. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3080. /* SNB-B */
  3081. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3082. }
  3083. I915_WRITE(reg, temp);
  3084. reg = FDI_RX_CTL(pipe);
  3085. temp = I915_READ(reg);
  3086. if (HAS_PCH_CPT(dev)) {
  3087. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3088. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3089. } else {
  3090. temp &= ~FDI_LINK_TRAIN_NONE;
  3091. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3092. }
  3093. I915_WRITE(reg, temp);
  3094. POSTING_READ(reg);
  3095. udelay(150);
  3096. for (i = 0; i < 4; i++) {
  3097. reg = FDI_TX_CTL(pipe);
  3098. temp = I915_READ(reg);
  3099. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3100. temp |= snb_b_fdi_train_param[i];
  3101. I915_WRITE(reg, temp);
  3102. POSTING_READ(reg);
  3103. udelay(500);
  3104. for (retry = 0; retry < 5; retry++) {
  3105. reg = FDI_RX_IIR(pipe);
  3106. temp = I915_READ(reg);
  3107. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3108. if (temp & FDI_RX_SYMBOL_LOCK) {
  3109. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3110. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3111. break;
  3112. }
  3113. udelay(50);
  3114. }
  3115. if (retry < 5)
  3116. break;
  3117. }
  3118. if (i == 4)
  3119. DRM_ERROR("FDI train 2 fail!\n");
  3120. DRM_DEBUG_KMS("FDI train done.\n");
  3121. }
  3122. /* Manual link training for Ivy Bridge A0 parts */
  3123. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3124. {
  3125. struct drm_device *dev = crtc->dev;
  3126. struct drm_i915_private *dev_priv = dev->dev_private;
  3127. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3128. int pipe = intel_crtc->pipe;
  3129. u32 reg, temp, i, j;
  3130. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3131. for train result */
  3132. reg = FDI_RX_IMR(pipe);
  3133. temp = I915_READ(reg);
  3134. temp &= ~FDI_RX_SYMBOL_LOCK;
  3135. temp &= ~FDI_RX_BIT_LOCK;
  3136. I915_WRITE(reg, temp);
  3137. POSTING_READ(reg);
  3138. udelay(150);
  3139. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3140. I915_READ(FDI_RX_IIR(pipe)));
  3141. /* Try each vswing and preemphasis setting twice before moving on */
  3142. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3143. /* disable first in case we need to retry */
  3144. reg = FDI_TX_CTL(pipe);
  3145. temp = I915_READ(reg);
  3146. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3147. temp &= ~FDI_TX_ENABLE;
  3148. I915_WRITE(reg, temp);
  3149. reg = FDI_RX_CTL(pipe);
  3150. temp = I915_READ(reg);
  3151. temp &= ~FDI_LINK_TRAIN_AUTO;
  3152. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3153. temp &= ~FDI_RX_ENABLE;
  3154. I915_WRITE(reg, temp);
  3155. /* enable CPU FDI TX and PCH FDI RX */
  3156. reg = FDI_TX_CTL(pipe);
  3157. temp = I915_READ(reg);
  3158. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3159. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3160. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3161. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3162. temp |= snb_b_fdi_train_param[j/2];
  3163. temp |= FDI_COMPOSITE_SYNC;
  3164. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3165. I915_WRITE(FDI_RX_MISC(pipe),
  3166. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3167. reg = FDI_RX_CTL(pipe);
  3168. temp = I915_READ(reg);
  3169. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3170. temp |= FDI_COMPOSITE_SYNC;
  3171. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3172. POSTING_READ(reg);
  3173. udelay(1); /* should be 0.5us */
  3174. for (i = 0; i < 4; i++) {
  3175. reg = FDI_RX_IIR(pipe);
  3176. temp = I915_READ(reg);
  3177. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3178. if (temp & FDI_RX_BIT_LOCK ||
  3179. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3180. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3181. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3182. i);
  3183. break;
  3184. }
  3185. udelay(1); /* should be 0.5us */
  3186. }
  3187. if (i == 4) {
  3188. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3189. continue;
  3190. }
  3191. /* Train 2 */
  3192. reg = FDI_TX_CTL(pipe);
  3193. temp = I915_READ(reg);
  3194. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3195. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3196. I915_WRITE(reg, temp);
  3197. reg = FDI_RX_CTL(pipe);
  3198. temp = I915_READ(reg);
  3199. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3200. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3201. I915_WRITE(reg, temp);
  3202. POSTING_READ(reg);
  3203. udelay(2); /* should be 1.5us */
  3204. for (i = 0; i < 4; i++) {
  3205. reg = FDI_RX_IIR(pipe);
  3206. temp = I915_READ(reg);
  3207. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3208. if (temp & FDI_RX_SYMBOL_LOCK ||
  3209. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3210. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3211. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3212. i);
  3213. goto train_done;
  3214. }
  3215. udelay(2); /* should be 1.5us */
  3216. }
  3217. if (i == 4)
  3218. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3219. }
  3220. train_done:
  3221. DRM_DEBUG_KMS("FDI train done.\n");
  3222. }
  3223. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3224. {
  3225. struct drm_device *dev = intel_crtc->base.dev;
  3226. struct drm_i915_private *dev_priv = dev->dev_private;
  3227. int pipe = intel_crtc->pipe;
  3228. u32 reg, temp;
  3229. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3230. reg = FDI_RX_CTL(pipe);
  3231. temp = I915_READ(reg);
  3232. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3233. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3234. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3235. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3236. POSTING_READ(reg);
  3237. udelay(200);
  3238. /* Switch from Rawclk to PCDclk */
  3239. temp = I915_READ(reg);
  3240. I915_WRITE(reg, temp | FDI_PCDCLK);
  3241. POSTING_READ(reg);
  3242. udelay(200);
  3243. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3244. reg = FDI_TX_CTL(pipe);
  3245. temp = I915_READ(reg);
  3246. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3247. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3248. POSTING_READ(reg);
  3249. udelay(100);
  3250. }
  3251. }
  3252. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3253. {
  3254. struct drm_device *dev = intel_crtc->base.dev;
  3255. struct drm_i915_private *dev_priv = dev->dev_private;
  3256. int pipe = intel_crtc->pipe;
  3257. u32 reg, temp;
  3258. /* Switch from PCDclk to Rawclk */
  3259. reg = FDI_RX_CTL(pipe);
  3260. temp = I915_READ(reg);
  3261. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3262. /* Disable CPU FDI TX PLL */
  3263. reg = FDI_TX_CTL(pipe);
  3264. temp = I915_READ(reg);
  3265. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3266. POSTING_READ(reg);
  3267. udelay(100);
  3268. reg = FDI_RX_CTL(pipe);
  3269. temp = I915_READ(reg);
  3270. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3271. /* Wait for the clocks to turn off. */
  3272. POSTING_READ(reg);
  3273. udelay(100);
  3274. }
  3275. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3276. {
  3277. struct drm_device *dev = crtc->dev;
  3278. struct drm_i915_private *dev_priv = dev->dev_private;
  3279. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3280. int pipe = intel_crtc->pipe;
  3281. u32 reg, temp;
  3282. /* disable CPU FDI tx and PCH FDI rx */
  3283. reg = FDI_TX_CTL(pipe);
  3284. temp = I915_READ(reg);
  3285. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3286. POSTING_READ(reg);
  3287. reg = FDI_RX_CTL(pipe);
  3288. temp = I915_READ(reg);
  3289. temp &= ~(0x7 << 16);
  3290. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3291. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3292. POSTING_READ(reg);
  3293. udelay(100);
  3294. /* Ironlake workaround, disable clock pointer after downing FDI */
  3295. if (HAS_PCH_IBX(dev))
  3296. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3297. /* still set train pattern 1 */
  3298. reg = FDI_TX_CTL(pipe);
  3299. temp = I915_READ(reg);
  3300. temp &= ~FDI_LINK_TRAIN_NONE;
  3301. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3302. I915_WRITE(reg, temp);
  3303. reg = FDI_RX_CTL(pipe);
  3304. temp = I915_READ(reg);
  3305. if (HAS_PCH_CPT(dev)) {
  3306. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3307. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3308. } else {
  3309. temp &= ~FDI_LINK_TRAIN_NONE;
  3310. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3311. }
  3312. /* BPC in FDI rx is consistent with that in PIPECONF */
  3313. temp &= ~(0x07 << 16);
  3314. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3315. I915_WRITE(reg, temp);
  3316. POSTING_READ(reg);
  3317. udelay(100);
  3318. }
  3319. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3320. {
  3321. struct intel_crtc *crtc;
  3322. /* Note that we don't need to be called with mode_config.lock here
  3323. * as our list of CRTC objects is static for the lifetime of the
  3324. * device and so cannot disappear as we iterate. Similarly, we can
  3325. * happily treat the predicates as racy, atomic checks as userspace
  3326. * cannot claim and pin a new fb without at least acquring the
  3327. * struct_mutex and so serialising with us.
  3328. */
  3329. for_each_intel_crtc(dev, crtc) {
  3330. if (atomic_read(&crtc->unpin_work_count) == 0)
  3331. continue;
  3332. if (crtc->unpin_work)
  3333. intel_wait_for_vblank(dev, crtc->pipe);
  3334. return true;
  3335. }
  3336. return false;
  3337. }
  3338. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3339. {
  3340. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3341. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3342. /* ensure that the unpin work is consistent wrt ->pending. */
  3343. smp_rmb();
  3344. intel_crtc->unpin_work = NULL;
  3345. if (work->event)
  3346. drm_send_vblank_event(intel_crtc->base.dev,
  3347. intel_crtc->pipe,
  3348. work->event);
  3349. drm_crtc_vblank_put(&intel_crtc->base);
  3350. wake_up_all(&dev_priv->pending_flip_queue);
  3351. queue_work(dev_priv->wq, &work->work);
  3352. trace_i915_flip_complete(intel_crtc->plane,
  3353. work->pending_flip_obj);
  3354. }
  3355. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3356. {
  3357. struct drm_device *dev = crtc->dev;
  3358. struct drm_i915_private *dev_priv = dev->dev_private;
  3359. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3360. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3361. !intel_crtc_has_pending_flip(crtc),
  3362. 60*HZ) == 0)) {
  3363. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3364. spin_lock_irq(&dev->event_lock);
  3365. if (intel_crtc->unpin_work) {
  3366. WARN_ONCE(1, "Removing stuck page flip\n");
  3367. page_flip_completed(intel_crtc);
  3368. }
  3369. spin_unlock_irq(&dev->event_lock);
  3370. }
  3371. if (crtc->primary->fb) {
  3372. mutex_lock(&dev->struct_mutex);
  3373. intel_finish_fb(crtc->primary->fb);
  3374. mutex_unlock(&dev->struct_mutex);
  3375. }
  3376. }
  3377. /* Program iCLKIP clock to the desired frequency */
  3378. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3379. {
  3380. struct drm_device *dev = crtc->dev;
  3381. struct drm_i915_private *dev_priv = dev->dev_private;
  3382. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3383. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3384. u32 temp;
  3385. mutex_lock(&dev_priv->sb_lock);
  3386. /* It is necessary to ungate the pixclk gate prior to programming
  3387. * the divisors, and gate it back when it is done.
  3388. */
  3389. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3390. /* Disable SSCCTL */
  3391. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3392. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3393. SBI_SSCCTL_DISABLE,
  3394. SBI_ICLK);
  3395. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3396. if (clock == 20000) {
  3397. auxdiv = 1;
  3398. divsel = 0x41;
  3399. phaseinc = 0x20;
  3400. } else {
  3401. /* The iCLK virtual clock root frequency is in MHz,
  3402. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3403. * divisors, it is necessary to divide one by another, so we
  3404. * convert the virtual clock precision to KHz here for higher
  3405. * precision.
  3406. */
  3407. u32 iclk_virtual_root_freq = 172800 * 1000;
  3408. u32 iclk_pi_range = 64;
  3409. u32 desired_divisor, msb_divisor_value, pi_value;
  3410. desired_divisor = (iclk_virtual_root_freq / clock);
  3411. msb_divisor_value = desired_divisor / iclk_pi_range;
  3412. pi_value = desired_divisor % iclk_pi_range;
  3413. auxdiv = 0;
  3414. divsel = msb_divisor_value - 2;
  3415. phaseinc = pi_value;
  3416. }
  3417. /* This should not happen with any sane values */
  3418. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3419. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3420. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3421. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3422. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3423. clock,
  3424. auxdiv,
  3425. divsel,
  3426. phasedir,
  3427. phaseinc);
  3428. /* Program SSCDIVINTPHASE6 */
  3429. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3430. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3431. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3432. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3433. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3434. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3435. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3436. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3437. /* Program SSCAUXDIV */
  3438. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3439. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3440. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3441. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3442. /* Enable modulator and associated divider */
  3443. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3444. temp &= ~SBI_SSCCTL_DISABLE;
  3445. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3446. /* Wait for initialization time */
  3447. udelay(24);
  3448. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3449. mutex_unlock(&dev_priv->sb_lock);
  3450. }
  3451. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3452. enum pipe pch_transcoder)
  3453. {
  3454. struct drm_device *dev = crtc->base.dev;
  3455. struct drm_i915_private *dev_priv = dev->dev_private;
  3456. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3457. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3458. I915_READ(HTOTAL(cpu_transcoder)));
  3459. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3460. I915_READ(HBLANK(cpu_transcoder)));
  3461. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3462. I915_READ(HSYNC(cpu_transcoder)));
  3463. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3464. I915_READ(VTOTAL(cpu_transcoder)));
  3465. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3466. I915_READ(VBLANK(cpu_transcoder)));
  3467. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3468. I915_READ(VSYNC(cpu_transcoder)));
  3469. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3470. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3471. }
  3472. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3473. {
  3474. struct drm_i915_private *dev_priv = dev->dev_private;
  3475. uint32_t temp;
  3476. temp = I915_READ(SOUTH_CHICKEN1);
  3477. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3478. return;
  3479. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3480. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3481. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3482. if (enable)
  3483. temp |= FDI_BC_BIFURCATION_SELECT;
  3484. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3485. I915_WRITE(SOUTH_CHICKEN1, temp);
  3486. POSTING_READ(SOUTH_CHICKEN1);
  3487. }
  3488. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3489. {
  3490. struct drm_device *dev = intel_crtc->base.dev;
  3491. switch (intel_crtc->pipe) {
  3492. case PIPE_A:
  3493. break;
  3494. case PIPE_B:
  3495. if (intel_crtc->config->fdi_lanes > 2)
  3496. cpt_set_fdi_bc_bifurcation(dev, false);
  3497. else
  3498. cpt_set_fdi_bc_bifurcation(dev, true);
  3499. break;
  3500. case PIPE_C:
  3501. cpt_set_fdi_bc_bifurcation(dev, true);
  3502. break;
  3503. default:
  3504. BUG();
  3505. }
  3506. }
  3507. /*
  3508. * Enable PCH resources required for PCH ports:
  3509. * - PCH PLLs
  3510. * - FDI training & RX/TX
  3511. * - update transcoder timings
  3512. * - DP transcoding bits
  3513. * - transcoder
  3514. */
  3515. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3516. {
  3517. struct drm_device *dev = crtc->dev;
  3518. struct drm_i915_private *dev_priv = dev->dev_private;
  3519. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3520. int pipe = intel_crtc->pipe;
  3521. u32 reg, temp;
  3522. assert_pch_transcoder_disabled(dev_priv, pipe);
  3523. if (IS_IVYBRIDGE(dev))
  3524. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3525. /* Write the TU size bits before fdi link training, so that error
  3526. * detection works. */
  3527. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3528. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3529. /* For PCH output, training FDI link */
  3530. dev_priv->display.fdi_link_train(crtc);
  3531. /* We need to program the right clock selection before writing the pixel
  3532. * mutliplier into the DPLL. */
  3533. if (HAS_PCH_CPT(dev)) {
  3534. u32 sel;
  3535. temp = I915_READ(PCH_DPLL_SEL);
  3536. temp |= TRANS_DPLL_ENABLE(pipe);
  3537. sel = TRANS_DPLLB_SEL(pipe);
  3538. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3539. temp |= sel;
  3540. else
  3541. temp &= ~sel;
  3542. I915_WRITE(PCH_DPLL_SEL, temp);
  3543. }
  3544. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3545. * transcoder, and we actually should do this to not upset any PCH
  3546. * transcoder that already use the clock when we share it.
  3547. *
  3548. * Note that enable_shared_dpll tries to do the right thing, but
  3549. * get_shared_dpll unconditionally resets the pll - we need that to have
  3550. * the right LVDS enable sequence. */
  3551. intel_enable_shared_dpll(intel_crtc);
  3552. /* set transcoder timing, panel must allow it */
  3553. assert_panel_unlocked(dev_priv, pipe);
  3554. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3555. intel_fdi_normal_train(crtc);
  3556. /* For PCH DP, enable TRANS_DP_CTL */
  3557. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3558. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3559. reg = TRANS_DP_CTL(pipe);
  3560. temp = I915_READ(reg);
  3561. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3562. TRANS_DP_SYNC_MASK |
  3563. TRANS_DP_BPC_MASK);
  3564. temp |= TRANS_DP_OUTPUT_ENABLE;
  3565. temp |= bpc << 9; /* same format but at 11:9 */
  3566. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3567. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3568. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3569. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3570. switch (intel_trans_dp_port_sel(crtc)) {
  3571. case PCH_DP_B:
  3572. temp |= TRANS_DP_PORT_SEL_B;
  3573. break;
  3574. case PCH_DP_C:
  3575. temp |= TRANS_DP_PORT_SEL_C;
  3576. break;
  3577. case PCH_DP_D:
  3578. temp |= TRANS_DP_PORT_SEL_D;
  3579. break;
  3580. default:
  3581. BUG();
  3582. }
  3583. I915_WRITE(reg, temp);
  3584. }
  3585. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3586. }
  3587. static void lpt_pch_enable(struct drm_crtc *crtc)
  3588. {
  3589. struct drm_device *dev = crtc->dev;
  3590. struct drm_i915_private *dev_priv = dev->dev_private;
  3591. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3592. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3593. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3594. lpt_program_iclkip(crtc);
  3595. /* Set transcoder timing. */
  3596. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3597. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3598. }
  3599. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3600. struct intel_crtc_state *crtc_state)
  3601. {
  3602. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3603. struct intel_shared_dpll *pll;
  3604. struct intel_shared_dpll_config *shared_dpll;
  3605. enum intel_dpll_id i;
  3606. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3607. if (HAS_PCH_IBX(dev_priv->dev)) {
  3608. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3609. i = (enum intel_dpll_id) crtc->pipe;
  3610. pll = &dev_priv->shared_dplls[i];
  3611. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3612. crtc->base.base.id, pll->name);
  3613. WARN_ON(shared_dpll[i].crtc_mask);
  3614. goto found;
  3615. }
  3616. if (IS_BROXTON(dev_priv->dev)) {
  3617. /* PLL is attached to port in bxt */
  3618. struct intel_encoder *encoder;
  3619. struct intel_digital_port *intel_dig_port;
  3620. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3621. if (WARN_ON(!encoder))
  3622. return NULL;
  3623. intel_dig_port = enc_to_dig_port(&encoder->base);
  3624. /* 1:1 mapping between ports and PLLs */
  3625. i = (enum intel_dpll_id)intel_dig_port->port;
  3626. pll = &dev_priv->shared_dplls[i];
  3627. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3628. crtc->base.base.id, pll->name);
  3629. WARN_ON(shared_dpll[i].crtc_mask);
  3630. goto found;
  3631. }
  3632. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3633. pll = &dev_priv->shared_dplls[i];
  3634. /* Only want to check enabled timings first */
  3635. if (shared_dpll[i].crtc_mask == 0)
  3636. continue;
  3637. if (memcmp(&crtc_state->dpll_hw_state,
  3638. &shared_dpll[i].hw_state,
  3639. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3640. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3641. crtc->base.base.id, pll->name,
  3642. shared_dpll[i].crtc_mask,
  3643. pll->active);
  3644. goto found;
  3645. }
  3646. }
  3647. /* Ok no matching timings, maybe there's a free one? */
  3648. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3649. pll = &dev_priv->shared_dplls[i];
  3650. if (shared_dpll[i].crtc_mask == 0) {
  3651. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3652. crtc->base.base.id, pll->name);
  3653. goto found;
  3654. }
  3655. }
  3656. return NULL;
  3657. found:
  3658. if (shared_dpll[i].crtc_mask == 0)
  3659. shared_dpll[i].hw_state =
  3660. crtc_state->dpll_hw_state;
  3661. crtc_state->shared_dpll = i;
  3662. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3663. pipe_name(crtc->pipe));
  3664. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3665. return pll;
  3666. }
  3667. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3668. {
  3669. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3670. struct intel_shared_dpll_config *shared_dpll;
  3671. struct intel_shared_dpll *pll;
  3672. enum intel_dpll_id i;
  3673. if (!to_intel_atomic_state(state)->dpll_set)
  3674. return;
  3675. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3676. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3677. pll = &dev_priv->shared_dplls[i];
  3678. pll->config = shared_dpll[i];
  3679. }
  3680. }
  3681. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3682. {
  3683. struct drm_i915_private *dev_priv = dev->dev_private;
  3684. int dslreg = PIPEDSL(pipe);
  3685. u32 temp;
  3686. temp = I915_READ(dslreg);
  3687. udelay(500);
  3688. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3689. if (wait_for(I915_READ(dslreg) != temp, 5))
  3690. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3691. }
  3692. }
  3693. static int
  3694. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3695. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3696. int src_w, int src_h, int dst_w, int dst_h)
  3697. {
  3698. struct intel_crtc_scaler_state *scaler_state =
  3699. &crtc_state->scaler_state;
  3700. struct intel_crtc *intel_crtc =
  3701. to_intel_crtc(crtc_state->base.crtc);
  3702. int need_scaling;
  3703. need_scaling = intel_rotation_90_or_270(rotation) ?
  3704. (src_h != dst_w || src_w != dst_h):
  3705. (src_w != dst_w || src_h != dst_h);
  3706. /*
  3707. * if plane is being disabled or scaler is no more required or force detach
  3708. * - free scaler binded to this plane/crtc
  3709. * - in order to do this, update crtc->scaler_usage
  3710. *
  3711. * Here scaler state in crtc_state is set free so that
  3712. * scaler can be assigned to other user. Actual register
  3713. * update to free the scaler is done in plane/panel-fit programming.
  3714. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3715. */
  3716. if (force_detach || !need_scaling) {
  3717. if (*scaler_id >= 0) {
  3718. scaler_state->scaler_users &= ~(1 << scaler_user);
  3719. scaler_state->scalers[*scaler_id].in_use = 0;
  3720. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3721. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3722. intel_crtc->pipe, scaler_user, *scaler_id,
  3723. scaler_state->scaler_users);
  3724. *scaler_id = -1;
  3725. }
  3726. return 0;
  3727. }
  3728. /* range checks */
  3729. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3730. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3731. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3732. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3733. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3734. "size is out of scaler range\n",
  3735. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3736. return -EINVAL;
  3737. }
  3738. /* mark this plane as a scaler user in crtc_state */
  3739. scaler_state->scaler_users |= (1 << scaler_user);
  3740. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3741. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3742. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3743. scaler_state->scaler_users);
  3744. return 0;
  3745. }
  3746. /**
  3747. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3748. *
  3749. * @state: crtc's scaler state
  3750. *
  3751. * Return
  3752. * 0 - scaler_usage updated successfully
  3753. * error - requested scaling cannot be supported or other error condition
  3754. */
  3755. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3756. {
  3757. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3758. struct drm_display_mode *adjusted_mode =
  3759. &state->base.adjusted_mode;
  3760. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3761. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3762. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3763. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3764. state->pipe_src_w, state->pipe_src_h,
  3765. adjusted_mode->hdisplay, adjusted_mode->vdisplay);
  3766. }
  3767. /**
  3768. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3769. *
  3770. * @state: crtc's scaler state
  3771. * @plane_state: atomic plane state to update
  3772. *
  3773. * Return
  3774. * 0 - scaler_usage updated successfully
  3775. * error - requested scaling cannot be supported or other error condition
  3776. */
  3777. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3778. struct intel_plane_state *plane_state)
  3779. {
  3780. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3781. struct intel_plane *intel_plane =
  3782. to_intel_plane(plane_state->base.plane);
  3783. struct drm_framebuffer *fb = plane_state->base.fb;
  3784. int ret;
  3785. bool force_detach = !fb || !plane_state->visible;
  3786. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3787. intel_plane->base.base.id, intel_crtc->pipe,
  3788. drm_plane_index(&intel_plane->base));
  3789. ret = skl_update_scaler(crtc_state, force_detach,
  3790. drm_plane_index(&intel_plane->base),
  3791. &plane_state->scaler_id,
  3792. plane_state->base.rotation,
  3793. drm_rect_width(&plane_state->src) >> 16,
  3794. drm_rect_height(&plane_state->src) >> 16,
  3795. drm_rect_width(&plane_state->dst),
  3796. drm_rect_height(&plane_state->dst));
  3797. if (ret || plane_state->scaler_id < 0)
  3798. return ret;
  3799. /* check colorkey */
  3800. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3801. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3802. intel_plane->base.base.id);
  3803. return -EINVAL;
  3804. }
  3805. /* Check src format */
  3806. switch (fb->pixel_format) {
  3807. case DRM_FORMAT_RGB565:
  3808. case DRM_FORMAT_XBGR8888:
  3809. case DRM_FORMAT_XRGB8888:
  3810. case DRM_FORMAT_ABGR8888:
  3811. case DRM_FORMAT_ARGB8888:
  3812. case DRM_FORMAT_XRGB2101010:
  3813. case DRM_FORMAT_XBGR2101010:
  3814. case DRM_FORMAT_YUYV:
  3815. case DRM_FORMAT_YVYU:
  3816. case DRM_FORMAT_UYVY:
  3817. case DRM_FORMAT_VYUY:
  3818. break;
  3819. default:
  3820. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3821. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3822. return -EINVAL;
  3823. }
  3824. return 0;
  3825. }
  3826. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3827. {
  3828. int i;
  3829. for (i = 0; i < crtc->num_scalers; i++)
  3830. skl_detach_scaler(crtc, i);
  3831. }
  3832. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3833. {
  3834. struct drm_device *dev = crtc->base.dev;
  3835. struct drm_i915_private *dev_priv = dev->dev_private;
  3836. int pipe = crtc->pipe;
  3837. struct intel_crtc_scaler_state *scaler_state =
  3838. &crtc->config->scaler_state;
  3839. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3840. if (crtc->config->pch_pfit.enabled) {
  3841. int id;
  3842. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3843. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3844. return;
  3845. }
  3846. id = scaler_state->scaler_id;
  3847. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3848. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3849. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3850. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3851. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3852. }
  3853. }
  3854. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3855. {
  3856. struct drm_device *dev = crtc->base.dev;
  3857. struct drm_i915_private *dev_priv = dev->dev_private;
  3858. int pipe = crtc->pipe;
  3859. if (crtc->config->pch_pfit.enabled) {
  3860. /* Force use of hard-coded filter coefficients
  3861. * as some pre-programmed values are broken,
  3862. * e.g. x201.
  3863. */
  3864. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3865. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3866. PF_PIPE_SEL_IVB(pipe));
  3867. else
  3868. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3869. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3870. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3871. }
  3872. }
  3873. void hsw_enable_ips(struct intel_crtc *crtc)
  3874. {
  3875. struct drm_device *dev = crtc->base.dev;
  3876. struct drm_i915_private *dev_priv = dev->dev_private;
  3877. if (!crtc->config->ips_enabled)
  3878. return;
  3879. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3880. intel_wait_for_vblank(dev, crtc->pipe);
  3881. assert_plane_enabled(dev_priv, crtc->plane);
  3882. if (IS_BROADWELL(dev)) {
  3883. mutex_lock(&dev_priv->rps.hw_lock);
  3884. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3885. mutex_unlock(&dev_priv->rps.hw_lock);
  3886. /* Quoting Art Runyan: "its not safe to expect any particular
  3887. * value in IPS_CTL bit 31 after enabling IPS through the
  3888. * mailbox." Moreover, the mailbox may return a bogus state,
  3889. * so we need to just enable it and continue on.
  3890. */
  3891. } else {
  3892. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3893. /* The bit only becomes 1 in the next vblank, so this wait here
  3894. * is essentially intel_wait_for_vblank. If we don't have this
  3895. * and don't wait for vblanks until the end of crtc_enable, then
  3896. * the HW state readout code will complain that the expected
  3897. * IPS_CTL value is not the one we read. */
  3898. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3899. DRM_ERROR("Timed out waiting for IPS enable\n");
  3900. }
  3901. }
  3902. void hsw_disable_ips(struct intel_crtc *crtc)
  3903. {
  3904. struct drm_device *dev = crtc->base.dev;
  3905. struct drm_i915_private *dev_priv = dev->dev_private;
  3906. if (!crtc->config->ips_enabled)
  3907. return;
  3908. assert_plane_enabled(dev_priv, crtc->plane);
  3909. if (IS_BROADWELL(dev)) {
  3910. mutex_lock(&dev_priv->rps.hw_lock);
  3911. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3912. mutex_unlock(&dev_priv->rps.hw_lock);
  3913. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3914. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3915. DRM_ERROR("Timed out waiting for IPS disable\n");
  3916. } else {
  3917. I915_WRITE(IPS_CTL, 0);
  3918. POSTING_READ(IPS_CTL);
  3919. }
  3920. /* We need to wait for a vblank before we can disable the plane. */
  3921. intel_wait_for_vblank(dev, crtc->pipe);
  3922. }
  3923. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3924. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3925. {
  3926. struct drm_device *dev = crtc->dev;
  3927. struct drm_i915_private *dev_priv = dev->dev_private;
  3928. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3929. enum pipe pipe = intel_crtc->pipe;
  3930. int palreg = PALETTE(pipe);
  3931. int i;
  3932. bool reenable_ips = false;
  3933. /* The clocks have to be on to load the palette. */
  3934. if (!crtc->state->active)
  3935. return;
  3936. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3937. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3938. assert_dsi_pll_enabled(dev_priv);
  3939. else
  3940. assert_pll_enabled(dev_priv, pipe);
  3941. }
  3942. /* use legacy palette for Ironlake */
  3943. if (!HAS_GMCH_DISPLAY(dev))
  3944. palreg = LGC_PALETTE(pipe);
  3945. /* Workaround : Do not read or write the pipe palette/gamma data while
  3946. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3947. */
  3948. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3949. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3950. GAMMA_MODE_MODE_SPLIT)) {
  3951. hsw_disable_ips(intel_crtc);
  3952. reenable_ips = true;
  3953. }
  3954. for (i = 0; i < 256; i++) {
  3955. I915_WRITE(palreg + 4 * i,
  3956. (intel_crtc->lut_r[i] << 16) |
  3957. (intel_crtc->lut_g[i] << 8) |
  3958. intel_crtc->lut_b[i]);
  3959. }
  3960. if (reenable_ips)
  3961. hsw_enable_ips(intel_crtc);
  3962. }
  3963. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3964. {
  3965. if (intel_crtc->overlay) {
  3966. struct drm_device *dev = intel_crtc->base.dev;
  3967. struct drm_i915_private *dev_priv = dev->dev_private;
  3968. mutex_lock(&dev->struct_mutex);
  3969. dev_priv->mm.interruptible = false;
  3970. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3971. dev_priv->mm.interruptible = true;
  3972. mutex_unlock(&dev->struct_mutex);
  3973. }
  3974. /* Let userspace switch the overlay on again. In most cases userspace
  3975. * has to recompute where to put it anyway.
  3976. */
  3977. }
  3978. /**
  3979. * intel_post_enable_primary - Perform operations after enabling primary plane
  3980. * @crtc: the CRTC whose primary plane was just enabled
  3981. *
  3982. * Performs potentially sleeping operations that must be done after the primary
  3983. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3984. * called due to an explicit primary plane update, or due to an implicit
  3985. * re-enable that is caused when a sprite plane is updated to no longer
  3986. * completely hide the primary plane.
  3987. */
  3988. static void
  3989. intel_post_enable_primary(struct drm_crtc *crtc)
  3990. {
  3991. struct drm_device *dev = crtc->dev;
  3992. struct drm_i915_private *dev_priv = dev->dev_private;
  3993. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3994. int pipe = intel_crtc->pipe;
  3995. /*
  3996. * BDW signals flip done immediately if the plane
  3997. * is disabled, even if the plane enable is already
  3998. * armed to occur at the next vblank :(
  3999. */
  4000. if (IS_BROADWELL(dev))
  4001. intel_wait_for_vblank(dev, pipe);
  4002. /*
  4003. * FIXME IPS should be fine as long as one plane is
  4004. * enabled, but in practice it seems to have problems
  4005. * when going from primary only to sprite only and vice
  4006. * versa.
  4007. */
  4008. hsw_enable_ips(intel_crtc);
  4009. /*
  4010. * Gen2 reports pipe underruns whenever all planes are disabled.
  4011. * So don't enable underrun reporting before at least some planes
  4012. * are enabled.
  4013. * FIXME: Need to fix the logic to work when we turn off all planes
  4014. * but leave the pipe running.
  4015. */
  4016. if (IS_GEN2(dev))
  4017. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4018. /* Underruns don't raise interrupts, so check manually. */
  4019. if (HAS_GMCH_DISPLAY(dev))
  4020. i9xx_check_fifo_underruns(dev_priv);
  4021. }
  4022. /**
  4023. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4024. * @crtc: the CRTC whose primary plane is to be disabled
  4025. *
  4026. * Performs potentially sleeping operations that must be done before the
  4027. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4028. * be called due to an explicit primary plane update, or due to an implicit
  4029. * disable that is caused when a sprite plane completely hides the primary
  4030. * plane.
  4031. */
  4032. static void
  4033. intel_pre_disable_primary(struct drm_crtc *crtc)
  4034. {
  4035. struct drm_device *dev = crtc->dev;
  4036. struct drm_i915_private *dev_priv = dev->dev_private;
  4037. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4038. int pipe = intel_crtc->pipe;
  4039. /*
  4040. * Gen2 reports pipe underruns whenever all planes are disabled.
  4041. * So diasble underrun reporting before all the planes get disabled.
  4042. * FIXME: Need to fix the logic to work when we turn off all planes
  4043. * but leave the pipe running.
  4044. */
  4045. if (IS_GEN2(dev))
  4046. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4047. /*
  4048. * Vblank time updates from the shadow to live plane control register
  4049. * are blocked if the memory self-refresh mode is active at that
  4050. * moment. So to make sure the plane gets truly disabled, disable
  4051. * first the self-refresh mode. The self-refresh enable bit in turn
  4052. * will be checked/applied by the HW only at the next frame start
  4053. * event which is after the vblank start event, so we need to have a
  4054. * wait-for-vblank between disabling the plane and the pipe.
  4055. */
  4056. if (HAS_GMCH_DISPLAY(dev)) {
  4057. intel_set_memory_cxsr(dev_priv, false);
  4058. dev_priv->wm.vlv.cxsr = false;
  4059. intel_wait_for_vblank(dev, pipe);
  4060. }
  4061. /*
  4062. * FIXME IPS should be fine as long as one plane is
  4063. * enabled, but in practice it seems to have problems
  4064. * when going from primary only to sprite only and vice
  4065. * versa.
  4066. */
  4067. hsw_disable_ips(intel_crtc);
  4068. }
  4069. static void intel_post_plane_update(struct intel_crtc *crtc)
  4070. {
  4071. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4072. struct drm_device *dev = crtc->base.dev;
  4073. struct drm_i915_private *dev_priv = dev->dev_private;
  4074. struct drm_plane *plane;
  4075. if (atomic->wait_vblank)
  4076. intel_wait_for_vblank(dev, crtc->pipe);
  4077. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4078. if (atomic->disable_cxsr)
  4079. crtc->wm.cxsr_allowed = true;
  4080. if (crtc->atomic.update_wm_post)
  4081. intel_update_watermarks(&crtc->base);
  4082. if (atomic->update_fbc)
  4083. intel_fbc_update(dev_priv);
  4084. if (atomic->post_enable_primary)
  4085. intel_post_enable_primary(&crtc->base);
  4086. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4087. intel_update_sprite_watermarks(plane, &crtc->base,
  4088. 0, 0, 0, false, false);
  4089. memset(atomic, 0, sizeof(*atomic));
  4090. }
  4091. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4092. {
  4093. struct drm_device *dev = crtc->base.dev;
  4094. struct drm_i915_private *dev_priv = dev->dev_private;
  4095. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4096. struct drm_plane *p;
  4097. /* Track fb's for any planes being disabled */
  4098. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4099. struct intel_plane *plane = to_intel_plane(p);
  4100. mutex_lock(&dev->struct_mutex);
  4101. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
  4102. plane->frontbuffer_bit);
  4103. mutex_unlock(&dev->struct_mutex);
  4104. }
  4105. if (atomic->wait_for_flips)
  4106. intel_crtc_wait_for_pending_flips(&crtc->base);
  4107. if (atomic->disable_fbc)
  4108. intel_fbc_disable_crtc(crtc);
  4109. if (crtc->atomic.disable_ips)
  4110. hsw_disable_ips(crtc);
  4111. if (atomic->pre_disable_primary)
  4112. intel_pre_disable_primary(&crtc->base);
  4113. if (atomic->disable_cxsr) {
  4114. crtc->wm.cxsr_allowed = false;
  4115. intel_set_memory_cxsr(dev_priv, false);
  4116. }
  4117. }
  4118. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4119. {
  4120. struct drm_device *dev = crtc->dev;
  4121. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4122. struct drm_plane *p;
  4123. int pipe = intel_crtc->pipe;
  4124. intel_crtc_dpms_overlay_disable(intel_crtc);
  4125. drm_for_each_plane_mask(p, dev, plane_mask)
  4126. to_intel_plane(p)->disable_plane(p, crtc);
  4127. /*
  4128. * FIXME: Once we grow proper nuclear flip support out of this we need
  4129. * to compute the mask of flip planes precisely. For the time being
  4130. * consider this a flip to a NULL plane.
  4131. */
  4132. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4133. }
  4134. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4135. {
  4136. struct drm_device *dev = crtc->dev;
  4137. struct drm_i915_private *dev_priv = dev->dev_private;
  4138. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4139. struct intel_encoder *encoder;
  4140. int pipe = intel_crtc->pipe;
  4141. if (WARN_ON(intel_crtc->active))
  4142. return;
  4143. if (intel_crtc->config->has_pch_encoder)
  4144. intel_prepare_shared_dpll(intel_crtc);
  4145. if (intel_crtc->config->has_dp_encoder)
  4146. intel_dp_set_m_n(intel_crtc, M1_N1);
  4147. intel_set_pipe_timings(intel_crtc);
  4148. if (intel_crtc->config->has_pch_encoder) {
  4149. intel_cpu_transcoder_set_m_n(intel_crtc,
  4150. &intel_crtc->config->fdi_m_n, NULL);
  4151. }
  4152. ironlake_set_pipeconf(crtc);
  4153. intel_crtc->active = true;
  4154. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4155. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4156. for_each_encoder_on_crtc(dev, crtc, encoder)
  4157. if (encoder->pre_enable)
  4158. encoder->pre_enable(encoder);
  4159. if (intel_crtc->config->has_pch_encoder) {
  4160. /* Note: FDI PLL enabling _must_ be done before we enable the
  4161. * cpu pipes, hence this is separate from all the other fdi/pch
  4162. * enabling. */
  4163. ironlake_fdi_pll_enable(intel_crtc);
  4164. } else {
  4165. assert_fdi_tx_disabled(dev_priv, pipe);
  4166. assert_fdi_rx_disabled(dev_priv, pipe);
  4167. }
  4168. ironlake_pfit_enable(intel_crtc);
  4169. /*
  4170. * On ILK+ LUT must be loaded before the pipe is running but with
  4171. * clocks enabled
  4172. */
  4173. intel_crtc_load_lut(crtc);
  4174. intel_update_watermarks(crtc);
  4175. intel_enable_pipe(intel_crtc);
  4176. if (intel_crtc->config->has_pch_encoder)
  4177. ironlake_pch_enable(crtc);
  4178. assert_vblank_disabled(crtc);
  4179. drm_crtc_vblank_on(crtc);
  4180. for_each_encoder_on_crtc(dev, crtc, encoder)
  4181. encoder->enable(encoder);
  4182. if (HAS_PCH_CPT(dev))
  4183. cpt_verify_modeset(dev, intel_crtc->pipe);
  4184. }
  4185. /* IPS only exists on ULT machines and is tied to pipe A. */
  4186. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4187. {
  4188. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4189. }
  4190. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4191. {
  4192. struct drm_device *dev = crtc->dev;
  4193. struct drm_i915_private *dev_priv = dev->dev_private;
  4194. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4195. struct intel_encoder *encoder;
  4196. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4197. struct intel_crtc_state *pipe_config =
  4198. to_intel_crtc_state(crtc->state);
  4199. if (WARN_ON(intel_crtc->active))
  4200. return;
  4201. if (intel_crtc_to_shared_dpll(intel_crtc))
  4202. intel_enable_shared_dpll(intel_crtc);
  4203. if (intel_crtc->config->has_dp_encoder)
  4204. intel_dp_set_m_n(intel_crtc, M1_N1);
  4205. intel_set_pipe_timings(intel_crtc);
  4206. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4207. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4208. intel_crtc->config->pixel_multiplier - 1);
  4209. }
  4210. if (intel_crtc->config->has_pch_encoder) {
  4211. intel_cpu_transcoder_set_m_n(intel_crtc,
  4212. &intel_crtc->config->fdi_m_n, NULL);
  4213. }
  4214. haswell_set_pipeconf(crtc);
  4215. intel_set_pipe_csc(crtc);
  4216. intel_crtc->active = true;
  4217. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4218. for_each_encoder_on_crtc(dev, crtc, encoder)
  4219. if (encoder->pre_enable)
  4220. encoder->pre_enable(encoder);
  4221. if (intel_crtc->config->has_pch_encoder) {
  4222. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4223. true);
  4224. dev_priv->display.fdi_link_train(crtc);
  4225. }
  4226. intel_ddi_enable_pipe_clock(intel_crtc);
  4227. if (INTEL_INFO(dev)->gen == 9)
  4228. skylake_pfit_enable(intel_crtc);
  4229. else if (INTEL_INFO(dev)->gen < 9)
  4230. ironlake_pfit_enable(intel_crtc);
  4231. else
  4232. MISSING_CASE(INTEL_INFO(dev)->gen);
  4233. /*
  4234. * On ILK+ LUT must be loaded before the pipe is running but with
  4235. * clocks enabled
  4236. */
  4237. intel_crtc_load_lut(crtc);
  4238. intel_ddi_set_pipe_settings(crtc);
  4239. intel_ddi_enable_transcoder_func(crtc);
  4240. intel_update_watermarks(crtc);
  4241. intel_enable_pipe(intel_crtc);
  4242. if (intel_crtc->config->has_pch_encoder)
  4243. lpt_pch_enable(crtc);
  4244. if (intel_crtc->config->dp_encoder_is_mst)
  4245. intel_ddi_set_vc_payload_alloc(crtc, true);
  4246. assert_vblank_disabled(crtc);
  4247. drm_crtc_vblank_on(crtc);
  4248. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4249. encoder->enable(encoder);
  4250. intel_opregion_notify_encoder(encoder, true);
  4251. }
  4252. /* If we change the relative order between pipe/planes enabling, we need
  4253. * to change the workaround. */
  4254. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4255. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4256. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4257. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4258. }
  4259. }
  4260. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4261. {
  4262. struct drm_device *dev = crtc->base.dev;
  4263. struct drm_i915_private *dev_priv = dev->dev_private;
  4264. int pipe = crtc->pipe;
  4265. /* To avoid upsetting the power well on haswell only disable the pfit if
  4266. * it's in use. The hw state code will make sure we get this right. */
  4267. if (crtc->config->pch_pfit.enabled) {
  4268. I915_WRITE(PF_CTL(pipe), 0);
  4269. I915_WRITE(PF_WIN_POS(pipe), 0);
  4270. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4271. }
  4272. }
  4273. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4274. {
  4275. struct drm_device *dev = crtc->dev;
  4276. struct drm_i915_private *dev_priv = dev->dev_private;
  4277. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4278. struct intel_encoder *encoder;
  4279. int pipe = intel_crtc->pipe;
  4280. u32 reg, temp;
  4281. for_each_encoder_on_crtc(dev, crtc, encoder)
  4282. encoder->disable(encoder);
  4283. drm_crtc_vblank_off(crtc);
  4284. assert_vblank_disabled(crtc);
  4285. if (intel_crtc->config->has_pch_encoder)
  4286. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4287. intel_disable_pipe(intel_crtc);
  4288. ironlake_pfit_disable(intel_crtc);
  4289. if (intel_crtc->config->has_pch_encoder)
  4290. ironlake_fdi_disable(crtc);
  4291. for_each_encoder_on_crtc(dev, crtc, encoder)
  4292. if (encoder->post_disable)
  4293. encoder->post_disable(encoder);
  4294. if (intel_crtc->config->has_pch_encoder) {
  4295. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4296. if (HAS_PCH_CPT(dev)) {
  4297. /* disable TRANS_DP_CTL */
  4298. reg = TRANS_DP_CTL(pipe);
  4299. temp = I915_READ(reg);
  4300. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4301. TRANS_DP_PORT_SEL_MASK);
  4302. temp |= TRANS_DP_PORT_SEL_NONE;
  4303. I915_WRITE(reg, temp);
  4304. /* disable DPLL_SEL */
  4305. temp = I915_READ(PCH_DPLL_SEL);
  4306. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4307. I915_WRITE(PCH_DPLL_SEL, temp);
  4308. }
  4309. ironlake_fdi_pll_disable(intel_crtc);
  4310. }
  4311. intel_crtc->active = false;
  4312. intel_update_watermarks(crtc);
  4313. }
  4314. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4315. {
  4316. struct drm_device *dev = crtc->dev;
  4317. struct drm_i915_private *dev_priv = dev->dev_private;
  4318. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4319. struct intel_encoder *encoder;
  4320. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4321. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4322. intel_opregion_notify_encoder(encoder, false);
  4323. encoder->disable(encoder);
  4324. }
  4325. drm_crtc_vblank_off(crtc);
  4326. assert_vblank_disabled(crtc);
  4327. if (intel_crtc->config->has_pch_encoder)
  4328. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4329. false);
  4330. intel_disable_pipe(intel_crtc);
  4331. if (intel_crtc->config->dp_encoder_is_mst)
  4332. intel_ddi_set_vc_payload_alloc(crtc, false);
  4333. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4334. if (INTEL_INFO(dev)->gen == 9)
  4335. skylake_scaler_disable(intel_crtc);
  4336. else if (INTEL_INFO(dev)->gen < 9)
  4337. ironlake_pfit_disable(intel_crtc);
  4338. else
  4339. MISSING_CASE(INTEL_INFO(dev)->gen);
  4340. intel_ddi_disable_pipe_clock(intel_crtc);
  4341. if (intel_crtc->config->has_pch_encoder) {
  4342. lpt_disable_pch_transcoder(dev_priv);
  4343. intel_ddi_fdi_disable(crtc);
  4344. }
  4345. for_each_encoder_on_crtc(dev, crtc, encoder)
  4346. if (encoder->post_disable)
  4347. encoder->post_disable(encoder);
  4348. intel_crtc->active = false;
  4349. intel_update_watermarks(crtc);
  4350. }
  4351. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4352. {
  4353. struct drm_device *dev = crtc->base.dev;
  4354. struct drm_i915_private *dev_priv = dev->dev_private;
  4355. struct intel_crtc_state *pipe_config = crtc->config;
  4356. if (!pipe_config->gmch_pfit.control)
  4357. return;
  4358. /*
  4359. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4360. * according to register description and PRM.
  4361. */
  4362. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4363. assert_pipe_disabled(dev_priv, crtc->pipe);
  4364. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4365. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4366. /* Border color in case we don't scale up to the full screen. Black by
  4367. * default, change to something else for debugging. */
  4368. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4369. }
  4370. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4371. {
  4372. switch (port) {
  4373. case PORT_A:
  4374. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4375. case PORT_B:
  4376. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4377. case PORT_C:
  4378. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4379. case PORT_D:
  4380. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4381. case PORT_E:
  4382. return POWER_DOMAIN_PORT_DDI_E_2_LANES;
  4383. default:
  4384. WARN_ON_ONCE(1);
  4385. return POWER_DOMAIN_PORT_OTHER;
  4386. }
  4387. }
  4388. #define for_each_power_domain(domain, mask) \
  4389. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4390. if ((1 << (domain)) & (mask))
  4391. enum intel_display_power_domain
  4392. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4393. {
  4394. struct drm_device *dev = intel_encoder->base.dev;
  4395. struct intel_digital_port *intel_dig_port;
  4396. switch (intel_encoder->type) {
  4397. case INTEL_OUTPUT_UNKNOWN:
  4398. /* Only DDI platforms should ever use this output type */
  4399. WARN_ON_ONCE(!HAS_DDI(dev));
  4400. case INTEL_OUTPUT_DISPLAYPORT:
  4401. case INTEL_OUTPUT_HDMI:
  4402. case INTEL_OUTPUT_EDP:
  4403. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4404. return port_to_power_domain(intel_dig_port->port);
  4405. case INTEL_OUTPUT_DP_MST:
  4406. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4407. return port_to_power_domain(intel_dig_port->port);
  4408. case INTEL_OUTPUT_ANALOG:
  4409. return POWER_DOMAIN_PORT_CRT;
  4410. case INTEL_OUTPUT_DSI:
  4411. return POWER_DOMAIN_PORT_DSI;
  4412. default:
  4413. return POWER_DOMAIN_PORT_OTHER;
  4414. }
  4415. }
  4416. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4417. {
  4418. struct drm_device *dev = crtc->dev;
  4419. struct intel_encoder *intel_encoder;
  4420. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4421. enum pipe pipe = intel_crtc->pipe;
  4422. unsigned long mask;
  4423. enum transcoder transcoder;
  4424. if (!crtc->state->active)
  4425. return 0;
  4426. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4427. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4428. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4429. if (intel_crtc->config->pch_pfit.enabled ||
  4430. intel_crtc->config->pch_pfit.force_thru)
  4431. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4432. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4433. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4434. return mask;
  4435. }
  4436. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4437. {
  4438. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4439. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4440. enum intel_display_power_domain domain;
  4441. unsigned long domains, new_domains, old_domains;
  4442. old_domains = intel_crtc->enabled_power_domains;
  4443. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4444. domains = new_domains & ~old_domains;
  4445. for_each_power_domain(domain, domains)
  4446. intel_display_power_get(dev_priv, domain);
  4447. return old_domains & ~new_domains;
  4448. }
  4449. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4450. unsigned long domains)
  4451. {
  4452. enum intel_display_power_domain domain;
  4453. for_each_power_domain(domain, domains)
  4454. intel_display_power_put(dev_priv, domain);
  4455. }
  4456. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4457. {
  4458. struct drm_device *dev = state->dev;
  4459. struct drm_i915_private *dev_priv = dev->dev_private;
  4460. unsigned long put_domains[I915_MAX_PIPES] = {};
  4461. struct drm_crtc_state *crtc_state;
  4462. struct drm_crtc *crtc;
  4463. int i;
  4464. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4465. if (needs_modeset(crtc->state))
  4466. put_domains[to_intel_crtc(crtc)->pipe] =
  4467. modeset_get_crtc_power_domains(crtc);
  4468. }
  4469. if (dev_priv->display.modeset_commit_cdclk) {
  4470. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4471. if (cdclk != dev_priv->cdclk_freq &&
  4472. !WARN_ON(!state->allow_modeset))
  4473. dev_priv->display.modeset_commit_cdclk(state);
  4474. }
  4475. for (i = 0; i < I915_MAX_PIPES; i++)
  4476. if (put_domains[i])
  4477. modeset_put_power_domains(dev_priv, put_domains[i]);
  4478. }
  4479. static void intel_update_max_cdclk(struct drm_device *dev)
  4480. {
  4481. struct drm_i915_private *dev_priv = dev->dev_private;
  4482. if (IS_SKYLAKE(dev)) {
  4483. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4484. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4485. dev_priv->max_cdclk_freq = 675000;
  4486. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4487. dev_priv->max_cdclk_freq = 540000;
  4488. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4489. dev_priv->max_cdclk_freq = 450000;
  4490. else
  4491. dev_priv->max_cdclk_freq = 337500;
  4492. } else if (IS_BROADWELL(dev)) {
  4493. /*
  4494. * FIXME with extra cooling we can allow
  4495. * 540 MHz for ULX and 675 Mhz for ULT.
  4496. * How can we know if extra cooling is
  4497. * available? PCI ID, VTB, something else?
  4498. */
  4499. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4500. dev_priv->max_cdclk_freq = 450000;
  4501. else if (IS_BDW_ULX(dev))
  4502. dev_priv->max_cdclk_freq = 450000;
  4503. else if (IS_BDW_ULT(dev))
  4504. dev_priv->max_cdclk_freq = 540000;
  4505. else
  4506. dev_priv->max_cdclk_freq = 675000;
  4507. } else if (IS_CHERRYVIEW(dev)) {
  4508. dev_priv->max_cdclk_freq = 320000;
  4509. } else if (IS_VALLEYVIEW(dev)) {
  4510. dev_priv->max_cdclk_freq = 400000;
  4511. } else {
  4512. /* otherwise assume cdclk is fixed */
  4513. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4514. }
  4515. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4516. dev_priv->max_cdclk_freq);
  4517. }
  4518. static void intel_update_cdclk(struct drm_device *dev)
  4519. {
  4520. struct drm_i915_private *dev_priv = dev->dev_private;
  4521. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4522. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4523. dev_priv->cdclk_freq);
  4524. /*
  4525. * Program the gmbus_freq based on the cdclk frequency.
  4526. * BSpec erroneously claims we should aim for 4MHz, but
  4527. * in fact 1MHz is the correct frequency.
  4528. */
  4529. if (IS_VALLEYVIEW(dev)) {
  4530. /*
  4531. * Program the gmbus_freq based on the cdclk frequency.
  4532. * BSpec erroneously claims we should aim for 4MHz, but
  4533. * in fact 1MHz is the correct frequency.
  4534. */
  4535. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4536. }
  4537. if (dev_priv->max_cdclk_freq == 0)
  4538. intel_update_max_cdclk(dev);
  4539. }
  4540. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4541. {
  4542. struct drm_i915_private *dev_priv = dev->dev_private;
  4543. uint32_t divider;
  4544. uint32_t ratio;
  4545. uint32_t current_freq;
  4546. int ret;
  4547. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4548. switch (frequency) {
  4549. case 144000:
  4550. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4551. ratio = BXT_DE_PLL_RATIO(60);
  4552. break;
  4553. case 288000:
  4554. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4555. ratio = BXT_DE_PLL_RATIO(60);
  4556. break;
  4557. case 384000:
  4558. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4559. ratio = BXT_DE_PLL_RATIO(60);
  4560. break;
  4561. case 576000:
  4562. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4563. ratio = BXT_DE_PLL_RATIO(60);
  4564. break;
  4565. case 624000:
  4566. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4567. ratio = BXT_DE_PLL_RATIO(65);
  4568. break;
  4569. case 19200:
  4570. /*
  4571. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4572. * to suppress GCC warning.
  4573. */
  4574. ratio = 0;
  4575. divider = 0;
  4576. break;
  4577. default:
  4578. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4579. return;
  4580. }
  4581. mutex_lock(&dev_priv->rps.hw_lock);
  4582. /* Inform power controller of upcoming frequency change */
  4583. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4584. 0x80000000);
  4585. mutex_unlock(&dev_priv->rps.hw_lock);
  4586. if (ret) {
  4587. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4588. ret, frequency);
  4589. return;
  4590. }
  4591. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4592. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4593. current_freq = current_freq * 500 + 1000;
  4594. /*
  4595. * DE PLL has to be disabled when
  4596. * - setting to 19.2MHz (bypass, PLL isn't used)
  4597. * - before setting to 624MHz (PLL needs toggling)
  4598. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4599. */
  4600. if (frequency == 19200 || frequency == 624000 ||
  4601. current_freq == 624000) {
  4602. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4603. /* Timeout 200us */
  4604. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4605. 1))
  4606. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4607. }
  4608. if (frequency != 19200) {
  4609. uint32_t val;
  4610. val = I915_READ(BXT_DE_PLL_CTL);
  4611. val &= ~BXT_DE_PLL_RATIO_MASK;
  4612. val |= ratio;
  4613. I915_WRITE(BXT_DE_PLL_CTL, val);
  4614. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4615. /* Timeout 200us */
  4616. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4617. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4618. val = I915_READ(CDCLK_CTL);
  4619. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4620. val |= divider;
  4621. /*
  4622. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4623. * enable otherwise.
  4624. */
  4625. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4626. if (frequency >= 500000)
  4627. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4628. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4629. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4630. val |= (frequency - 1000) / 500;
  4631. I915_WRITE(CDCLK_CTL, val);
  4632. }
  4633. mutex_lock(&dev_priv->rps.hw_lock);
  4634. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4635. DIV_ROUND_UP(frequency, 25000));
  4636. mutex_unlock(&dev_priv->rps.hw_lock);
  4637. if (ret) {
  4638. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4639. ret, frequency);
  4640. return;
  4641. }
  4642. intel_update_cdclk(dev);
  4643. }
  4644. void broxton_init_cdclk(struct drm_device *dev)
  4645. {
  4646. struct drm_i915_private *dev_priv = dev->dev_private;
  4647. uint32_t val;
  4648. /*
  4649. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4650. * or else the reset will hang because there is no PCH to respond.
  4651. * Move the handshake programming to initialization sequence.
  4652. * Previously was left up to BIOS.
  4653. */
  4654. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4655. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4656. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4657. /* Enable PG1 for cdclk */
  4658. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4659. /* check if cd clock is enabled */
  4660. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4661. DRM_DEBUG_KMS("Display already initialized\n");
  4662. return;
  4663. }
  4664. /*
  4665. * FIXME:
  4666. * - The initial CDCLK needs to be read from VBT.
  4667. * Need to make this change after VBT has changes for BXT.
  4668. * - check if setting the max (or any) cdclk freq is really necessary
  4669. * here, it belongs to modeset time
  4670. */
  4671. broxton_set_cdclk(dev, 624000);
  4672. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4673. POSTING_READ(DBUF_CTL);
  4674. udelay(10);
  4675. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4676. DRM_ERROR("DBuf power enable timeout!\n");
  4677. }
  4678. void broxton_uninit_cdclk(struct drm_device *dev)
  4679. {
  4680. struct drm_i915_private *dev_priv = dev->dev_private;
  4681. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4682. POSTING_READ(DBUF_CTL);
  4683. udelay(10);
  4684. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4685. DRM_ERROR("DBuf power disable timeout!\n");
  4686. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4687. broxton_set_cdclk(dev, 19200);
  4688. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4689. }
  4690. static const struct skl_cdclk_entry {
  4691. unsigned int freq;
  4692. unsigned int vco;
  4693. } skl_cdclk_frequencies[] = {
  4694. { .freq = 308570, .vco = 8640 },
  4695. { .freq = 337500, .vco = 8100 },
  4696. { .freq = 432000, .vco = 8640 },
  4697. { .freq = 450000, .vco = 8100 },
  4698. { .freq = 540000, .vco = 8100 },
  4699. { .freq = 617140, .vco = 8640 },
  4700. { .freq = 675000, .vco = 8100 },
  4701. };
  4702. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4703. {
  4704. return (freq - 1000) / 500;
  4705. }
  4706. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4707. {
  4708. unsigned int i;
  4709. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4710. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4711. if (e->freq == freq)
  4712. return e->vco;
  4713. }
  4714. return 8100;
  4715. }
  4716. static void
  4717. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4718. {
  4719. unsigned int min_freq;
  4720. u32 val;
  4721. /* select the minimum CDCLK before enabling DPLL 0 */
  4722. val = I915_READ(CDCLK_CTL);
  4723. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4724. val |= CDCLK_FREQ_337_308;
  4725. if (required_vco == 8640)
  4726. min_freq = 308570;
  4727. else
  4728. min_freq = 337500;
  4729. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4730. I915_WRITE(CDCLK_CTL, val);
  4731. POSTING_READ(CDCLK_CTL);
  4732. /*
  4733. * We always enable DPLL0 with the lowest link rate possible, but still
  4734. * taking into account the VCO required to operate the eDP panel at the
  4735. * desired frequency. The usual DP link rates operate with a VCO of
  4736. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4737. * The modeset code is responsible for the selection of the exact link
  4738. * rate later on, with the constraint of choosing a frequency that
  4739. * works with required_vco.
  4740. */
  4741. val = I915_READ(DPLL_CTRL1);
  4742. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4743. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4744. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4745. if (required_vco == 8640)
  4746. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4747. SKL_DPLL0);
  4748. else
  4749. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4750. SKL_DPLL0);
  4751. I915_WRITE(DPLL_CTRL1, val);
  4752. POSTING_READ(DPLL_CTRL1);
  4753. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4754. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4755. DRM_ERROR("DPLL0 not locked\n");
  4756. }
  4757. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4758. {
  4759. int ret;
  4760. u32 val;
  4761. /* inform PCU we want to change CDCLK */
  4762. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4763. mutex_lock(&dev_priv->rps.hw_lock);
  4764. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4765. mutex_unlock(&dev_priv->rps.hw_lock);
  4766. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4767. }
  4768. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4769. {
  4770. unsigned int i;
  4771. for (i = 0; i < 15; i++) {
  4772. if (skl_cdclk_pcu_ready(dev_priv))
  4773. return true;
  4774. udelay(10);
  4775. }
  4776. return false;
  4777. }
  4778. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4779. {
  4780. struct drm_device *dev = dev_priv->dev;
  4781. u32 freq_select, pcu_ack;
  4782. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4783. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4784. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4785. return;
  4786. }
  4787. /* set CDCLK_CTL */
  4788. switch(freq) {
  4789. case 450000:
  4790. case 432000:
  4791. freq_select = CDCLK_FREQ_450_432;
  4792. pcu_ack = 1;
  4793. break;
  4794. case 540000:
  4795. freq_select = CDCLK_FREQ_540;
  4796. pcu_ack = 2;
  4797. break;
  4798. case 308570:
  4799. case 337500:
  4800. default:
  4801. freq_select = CDCLK_FREQ_337_308;
  4802. pcu_ack = 0;
  4803. break;
  4804. case 617140:
  4805. case 675000:
  4806. freq_select = CDCLK_FREQ_675_617;
  4807. pcu_ack = 3;
  4808. break;
  4809. }
  4810. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4811. POSTING_READ(CDCLK_CTL);
  4812. /* inform PCU of the change */
  4813. mutex_lock(&dev_priv->rps.hw_lock);
  4814. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4815. mutex_unlock(&dev_priv->rps.hw_lock);
  4816. intel_update_cdclk(dev);
  4817. }
  4818. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4819. {
  4820. /* disable DBUF power */
  4821. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4822. POSTING_READ(DBUF_CTL);
  4823. udelay(10);
  4824. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4825. DRM_ERROR("DBuf power disable timeout\n");
  4826. /* disable DPLL0 */
  4827. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4828. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4829. DRM_ERROR("Couldn't disable DPLL0\n");
  4830. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4831. }
  4832. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4833. {
  4834. u32 val;
  4835. unsigned int required_vco;
  4836. /* enable PCH reset handshake */
  4837. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4838. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4839. /* enable PG1 and Misc I/O */
  4840. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4841. /* DPLL0 not enabled (happens on early BIOS versions) */
  4842. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4843. /* enable DPLL0 */
  4844. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4845. skl_dpll0_enable(dev_priv, required_vco);
  4846. }
  4847. /* set CDCLK to the frequency the BIOS chose */
  4848. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4849. /* enable DBUF power */
  4850. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4851. POSTING_READ(DBUF_CTL);
  4852. udelay(10);
  4853. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4854. DRM_ERROR("DBuf power enable timeout\n");
  4855. }
  4856. /* returns HPLL frequency in kHz */
  4857. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4858. {
  4859. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4860. /* Obtain SKU information */
  4861. mutex_lock(&dev_priv->sb_lock);
  4862. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4863. CCK_FUSE_HPLL_FREQ_MASK;
  4864. mutex_unlock(&dev_priv->sb_lock);
  4865. return vco_freq[hpll_freq] * 1000;
  4866. }
  4867. /* Adjust CDclk dividers to allow high res or save power if possible */
  4868. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4869. {
  4870. struct drm_i915_private *dev_priv = dev->dev_private;
  4871. u32 val, cmd;
  4872. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4873. != dev_priv->cdclk_freq);
  4874. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4875. cmd = 2;
  4876. else if (cdclk == 266667)
  4877. cmd = 1;
  4878. else
  4879. cmd = 0;
  4880. mutex_lock(&dev_priv->rps.hw_lock);
  4881. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4882. val &= ~DSPFREQGUAR_MASK;
  4883. val |= (cmd << DSPFREQGUAR_SHIFT);
  4884. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4885. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4886. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4887. 50)) {
  4888. DRM_ERROR("timed out waiting for CDclk change\n");
  4889. }
  4890. mutex_unlock(&dev_priv->rps.hw_lock);
  4891. mutex_lock(&dev_priv->sb_lock);
  4892. if (cdclk == 400000) {
  4893. u32 divider;
  4894. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4895. /* adjust cdclk divider */
  4896. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4897. val &= ~DISPLAY_FREQUENCY_VALUES;
  4898. val |= divider;
  4899. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4900. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4901. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4902. 50))
  4903. DRM_ERROR("timed out waiting for CDclk change\n");
  4904. }
  4905. /* adjust self-refresh exit latency value */
  4906. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4907. val &= ~0x7f;
  4908. /*
  4909. * For high bandwidth configs, we set a higher latency in the bunit
  4910. * so that the core display fetch happens in time to avoid underruns.
  4911. */
  4912. if (cdclk == 400000)
  4913. val |= 4500 / 250; /* 4.5 usec */
  4914. else
  4915. val |= 3000 / 250; /* 3.0 usec */
  4916. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4917. mutex_unlock(&dev_priv->sb_lock);
  4918. intel_update_cdclk(dev);
  4919. }
  4920. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4921. {
  4922. struct drm_i915_private *dev_priv = dev->dev_private;
  4923. u32 val, cmd;
  4924. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4925. != dev_priv->cdclk_freq);
  4926. switch (cdclk) {
  4927. case 333333:
  4928. case 320000:
  4929. case 266667:
  4930. case 200000:
  4931. break;
  4932. default:
  4933. MISSING_CASE(cdclk);
  4934. return;
  4935. }
  4936. /*
  4937. * Specs are full of misinformation, but testing on actual
  4938. * hardware has shown that we just need to write the desired
  4939. * CCK divider into the Punit register.
  4940. */
  4941. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4942. mutex_lock(&dev_priv->rps.hw_lock);
  4943. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4944. val &= ~DSPFREQGUAR_MASK_CHV;
  4945. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4946. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4947. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4948. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4949. 50)) {
  4950. DRM_ERROR("timed out waiting for CDclk change\n");
  4951. }
  4952. mutex_unlock(&dev_priv->rps.hw_lock);
  4953. intel_update_cdclk(dev);
  4954. }
  4955. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4956. int max_pixclk)
  4957. {
  4958. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4959. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4960. /*
  4961. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4962. * 200MHz
  4963. * 267MHz
  4964. * 320/333MHz (depends on HPLL freq)
  4965. * 400MHz (VLV only)
  4966. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4967. * of the lower bin and adjust if needed.
  4968. *
  4969. * We seem to get an unstable or solid color picture at 200MHz.
  4970. * Not sure what's wrong. For now use 200MHz only when all pipes
  4971. * are off.
  4972. */
  4973. if (!IS_CHERRYVIEW(dev_priv) &&
  4974. max_pixclk > freq_320*limit/100)
  4975. return 400000;
  4976. else if (max_pixclk > 266667*limit/100)
  4977. return freq_320;
  4978. else if (max_pixclk > 0)
  4979. return 266667;
  4980. else
  4981. return 200000;
  4982. }
  4983. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4984. int max_pixclk)
  4985. {
  4986. /*
  4987. * FIXME:
  4988. * - remove the guardband, it's not needed on BXT
  4989. * - set 19.2MHz bypass frequency if there are no active pipes
  4990. */
  4991. if (max_pixclk > 576000*9/10)
  4992. return 624000;
  4993. else if (max_pixclk > 384000*9/10)
  4994. return 576000;
  4995. else if (max_pixclk > 288000*9/10)
  4996. return 384000;
  4997. else if (max_pixclk > 144000*9/10)
  4998. return 288000;
  4999. else
  5000. return 144000;
  5001. }
  5002. /* Compute the max pixel clock for new configuration. Uses atomic state if
  5003. * that's non-NULL, look at current state otherwise. */
  5004. static int intel_mode_max_pixclk(struct drm_device *dev,
  5005. struct drm_atomic_state *state)
  5006. {
  5007. struct intel_crtc *intel_crtc;
  5008. struct intel_crtc_state *crtc_state;
  5009. int max_pixclk = 0;
  5010. for_each_intel_crtc(dev, intel_crtc) {
  5011. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5012. if (IS_ERR(crtc_state))
  5013. return PTR_ERR(crtc_state);
  5014. if (!crtc_state->base.enable)
  5015. continue;
  5016. max_pixclk = max(max_pixclk,
  5017. crtc_state->base.adjusted_mode.crtc_clock);
  5018. }
  5019. return max_pixclk;
  5020. }
  5021. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5022. {
  5023. struct drm_device *dev = state->dev;
  5024. struct drm_i915_private *dev_priv = dev->dev_private;
  5025. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5026. if (max_pixclk < 0)
  5027. return max_pixclk;
  5028. to_intel_atomic_state(state)->cdclk =
  5029. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5030. return 0;
  5031. }
  5032. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5033. {
  5034. struct drm_device *dev = state->dev;
  5035. struct drm_i915_private *dev_priv = dev->dev_private;
  5036. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5037. if (max_pixclk < 0)
  5038. return max_pixclk;
  5039. to_intel_atomic_state(state)->cdclk =
  5040. broxton_calc_cdclk(dev_priv, max_pixclk);
  5041. return 0;
  5042. }
  5043. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5044. {
  5045. unsigned int credits, default_credits;
  5046. if (IS_CHERRYVIEW(dev_priv))
  5047. default_credits = PFI_CREDIT(12);
  5048. else
  5049. default_credits = PFI_CREDIT(8);
  5050. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5051. /* CHV suggested value is 31 or 63 */
  5052. if (IS_CHERRYVIEW(dev_priv))
  5053. credits = PFI_CREDIT_63;
  5054. else
  5055. credits = PFI_CREDIT(15);
  5056. } else {
  5057. credits = default_credits;
  5058. }
  5059. /*
  5060. * WA - write default credits before re-programming
  5061. * FIXME: should we also set the resend bit here?
  5062. */
  5063. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5064. default_credits);
  5065. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5066. credits | PFI_CREDIT_RESEND);
  5067. /*
  5068. * FIXME is this guaranteed to clear
  5069. * immediately or should we poll for it?
  5070. */
  5071. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5072. }
  5073. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5074. {
  5075. struct drm_device *dev = old_state->dev;
  5076. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5077. struct drm_i915_private *dev_priv = dev->dev_private;
  5078. /*
  5079. * FIXME: We can end up here with all power domains off, yet
  5080. * with a CDCLK frequency other than the minimum. To account
  5081. * for this take the PIPE-A power domain, which covers the HW
  5082. * blocks needed for the following programming. This can be
  5083. * removed once it's guaranteed that we get here either with
  5084. * the minimum CDCLK set, or the required power domains
  5085. * enabled.
  5086. */
  5087. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5088. if (IS_CHERRYVIEW(dev))
  5089. cherryview_set_cdclk(dev, req_cdclk);
  5090. else
  5091. valleyview_set_cdclk(dev, req_cdclk);
  5092. vlv_program_pfi_credits(dev_priv);
  5093. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5094. }
  5095. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5096. {
  5097. struct drm_device *dev = crtc->dev;
  5098. struct drm_i915_private *dev_priv = to_i915(dev);
  5099. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5100. struct intel_encoder *encoder;
  5101. int pipe = intel_crtc->pipe;
  5102. bool is_dsi;
  5103. if (WARN_ON(intel_crtc->active))
  5104. return;
  5105. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5106. if (!is_dsi) {
  5107. if (IS_CHERRYVIEW(dev))
  5108. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5109. else
  5110. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5111. }
  5112. if (intel_crtc->config->has_dp_encoder)
  5113. intel_dp_set_m_n(intel_crtc, M1_N1);
  5114. intel_set_pipe_timings(intel_crtc);
  5115. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5116. struct drm_i915_private *dev_priv = dev->dev_private;
  5117. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5118. I915_WRITE(CHV_CANVAS(pipe), 0);
  5119. }
  5120. i9xx_set_pipeconf(intel_crtc);
  5121. intel_crtc->active = true;
  5122. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5123. for_each_encoder_on_crtc(dev, crtc, encoder)
  5124. if (encoder->pre_pll_enable)
  5125. encoder->pre_pll_enable(encoder);
  5126. if (!is_dsi) {
  5127. if (IS_CHERRYVIEW(dev))
  5128. chv_enable_pll(intel_crtc, intel_crtc->config);
  5129. else
  5130. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5131. }
  5132. for_each_encoder_on_crtc(dev, crtc, encoder)
  5133. if (encoder->pre_enable)
  5134. encoder->pre_enable(encoder);
  5135. i9xx_pfit_enable(intel_crtc);
  5136. intel_crtc_load_lut(crtc);
  5137. intel_enable_pipe(intel_crtc);
  5138. assert_vblank_disabled(crtc);
  5139. drm_crtc_vblank_on(crtc);
  5140. for_each_encoder_on_crtc(dev, crtc, encoder)
  5141. encoder->enable(encoder);
  5142. }
  5143. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5144. {
  5145. struct drm_device *dev = crtc->base.dev;
  5146. struct drm_i915_private *dev_priv = dev->dev_private;
  5147. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5148. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5149. }
  5150. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5151. {
  5152. struct drm_device *dev = crtc->dev;
  5153. struct drm_i915_private *dev_priv = to_i915(dev);
  5154. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5155. struct intel_encoder *encoder;
  5156. int pipe = intel_crtc->pipe;
  5157. if (WARN_ON(intel_crtc->active))
  5158. return;
  5159. i9xx_set_pll_dividers(intel_crtc);
  5160. if (intel_crtc->config->has_dp_encoder)
  5161. intel_dp_set_m_n(intel_crtc, M1_N1);
  5162. intel_set_pipe_timings(intel_crtc);
  5163. i9xx_set_pipeconf(intel_crtc);
  5164. intel_crtc->active = true;
  5165. if (!IS_GEN2(dev))
  5166. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5167. for_each_encoder_on_crtc(dev, crtc, encoder)
  5168. if (encoder->pre_enable)
  5169. encoder->pre_enable(encoder);
  5170. i9xx_enable_pll(intel_crtc);
  5171. i9xx_pfit_enable(intel_crtc);
  5172. intel_crtc_load_lut(crtc);
  5173. intel_update_watermarks(crtc);
  5174. intel_enable_pipe(intel_crtc);
  5175. assert_vblank_disabled(crtc);
  5176. drm_crtc_vblank_on(crtc);
  5177. for_each_encoder_on_crtc(dev, crtc, encoder)
  5178. encoder->enable(encoder);
  5179. }
  5180. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5181. {
  5182. struct drm_device *dev = crtc->base.dev;
  5183. struct drm_i915_private *dev_priv = dev->dev_private;
  5184. if (!crtc->config->gmch_pfit.control)
  5185. return;
  5186. assert_pipe_disabled(dev_priv, crtc->pipe);
  5187. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5188. I915_READ(PFIT_CONTROL));
  5189. I915_WRITE(PFIT_CONTROL, 0);
  5190. }
  5191. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5192. {
  5193. struct drm_device *dev = crtc->dev;
  5194. struct drm_i915_private *dev_priv = dev->dev_private;
  5195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5196. struct intel_encoder *encoder;
  5197. int pipe = intel_crtc->pipe;
  5198. /*
  5199. * On gen2 planes are double buffered but the pipe isn't, so we must
  5200. * wait for planes to fully turn off before disabling the pipe.
  5201. * We also need to wait on all gmch platforms because of the
  5202. * self-refresh mode constraint explained above.
  5203. */
  5204. intel_wait_for_vblank(dev, pipe);
  5205. for_each_encoder_on_crtc(dev, crtc, encoder)
  5206. encoder->disable(encoder);
  5207. drm_crtc_vblank_off(crtc);
  5208. assert_vblank_disabled(crtc);
  5209. intel_disable_pipe(intel_crtc);
  5210. i9xx_pfit_disable(intel_crtc);
  5211. for_each_encoder_on_crtc(dev, crtc, encoder)
  5212. if (encoder->post_disable)
  5213. encoder->post_disable(encoder);
  5214. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5215. if (IS_CHERRYVIEW(dev))
  5216. chv_disable_pll(dev_priv, pipe);
  5217. else if (IS_VALLEYVIEW(dev))
  5218. vlv_disable_pll(dev_priv, pipe);
  5219. else
  5220. i9xx_disable_pll(intel_crtc);
  5221. }
  5222. if (!IS_GEN2(dev))
  5223. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5224. intel_crtc->active = false;
  5225. intel_update_watermarks(crtc);
  5226. }
  5227. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5228. {
  5229. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5230. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5231. enum intel_display_power_domain domain;
  5232. unsigned long domains;
  5233. if (!intel_crtc->active)
  5234. return;
  5235. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5236. intel_crtc_wait_for_pending_flips(crtc);
  5237. intel_pre_disable_primary(crtc);
  5238. }
  5239. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5240. dev_priv->display.crtc_disable(crtc);
  5241. intel_disable_shared_dpll(intel_crtc);
  5242. domains = intel_crtc->enabled_power_domains;
  5243. for_each_power_domain(domain, domains)
  5244. intel_display_power_put(dev_priv, domain);
  5245. intel_crtc->enabled_power_domains = 0;
  5246. }
  5247. /*
  5248. * turn all crtc's off, but do not adjust state
  5249. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5250. */
  5251. int intel_display_suspend(struct drm_device *dev)
  5252. {
  5253. struct drm_mode_config *config = &dev->mode_config;
  5254. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5255. struct drm_atomic_state *state;
  5256. struct drm_crtc *crtc;
  5257. unsigned crtc_mask = 0;
  5258. int ret = 0;
  5259. if (WARN_ON(!ctx))
  5260. return 0;
  5261. lockdep_assert_held(&ctx->ww_ctx);
  5262. state = drm_atomic_state_alloc(dev);
  5263. if (WARN_ON(!state))
  5264. return -ENOMEM;
  5265. state->acquire_ctx = ctx;
  5266. state->allow_modeset = true;
  5267. for_each_crtc(dev, crtc) {
  5268. struct drm_crtc_state *crtc_state =
  5269. drm_atomic_get_crtc_state(state, crtc);
  5270. ret = PTR_ERR_OR_ZERO(crtc_state);
  5271. if (ret)
  5272. goto free;
  5273. if (!crtc_state->active)
  5274. continue;
  5275. crtc_state->active = false;
  5276. crtc_mask |= 1 << drm_crtc_index(crtc);
  5277. }
  5278. if (crtc_mask) {
  5279. ret = drm_atomic_commit(state);
  5280. if (!ret) {
  5281. for_each_crtc(dev, crtc)
  5282. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5283. crtc->state->active = true;
  5284. return ret;
  5285. }
  5286. }
  5287. free:
  5288. if (ret)
  5289. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5290. drm_atomic_state_free(state);
  5291. return ret;
  5292. }
  5293. void intel_encoder_destroy(struct drm_encoder *encoder)
  5294. {
  5295. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5296. drm_encoder_cleanup(encoder);
  5297. kfree(intel_encoder);
  5298. }
  5299. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5300. * internal consistency). */
  5301. static void intel_connector_check_state(struct intel_connector *connector)
  5302. {
  5303. struct drm_crtc *crtc = connector->base.state->crtc;
  5304. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5305. connector->base.base.id,
  5306. connector->base.name);
  5307. if (connector->get_hw_state(connector)) {
  5308. struct intel_encoder *encoder = connector->encoder;
  5309. struct drm_connector_state *conn_state = connector->base.state;
  5310. I915_STATE_WARN(!crtc,
  5311. "connector enabled without attached crtc\n");
  5312. if (!crtc)
  5313. return;
  5314. I915_STATE_WARN(!crtc->state->active,
  5315. "connector is active, but attached crtc isn't\n");
  5316. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5317. return;
  5318. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5319. "atomic encoder doesn't match attached encoder\n");
  5320. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5321. "attached encoder crtc differs from connector crtc\n");
  5322. } else {
  5323. I915_STATE_WARN(crtc && crtc->state->active,
  5324. "attached crtc is active, but connector isn't\n");
  5325. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5326. "best encoder set without crtc!\n");
  5327. }
  5328. }
  5329. int intel_connector_init(struct intel_connector *connector)
  5330. {
  5331. struct drm_connector_state *connector_state;
  5332. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5333. if (!connector_state)
  5334. return -ENOMEM;
  5335. connector->base.state = connector_state;
  5336. return 0;
  5337. }
  5338. struct intel_connector *intel_connector_alloc(void)
  5339. {
  5340. struct intel_connector *connector;
  5341. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5342. if (!connector)
  5343. return NULL;
  5344. if (intel_connector_init(connector) < 0) {
  5345. kfree(connector);
  5346. return NULL;
  5347. }
  5348. return connector;
  5349. }
  5350. /* Simple connector->get_hw_state implementation for encoders that support only
  5351. * one connector and no cloning and hence the encoder state determines the state
  5352. * of the connector. */
  5353. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5354. {
  5355. enum pipe pipe = 0;
  5356. struct intel_encoder *encoder = connector->encoder;
  5357. return encoder->get_hw_state(encoder, &pipe);
  5358. }
  5359. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5360. {
  5361. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5362. return crtc_state->fdi_lanes;
  5363. return 0;
  5364. }
  5365. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5366. struct intel_crtc_state *pipe_config)
  5367. {
  5368. struct drm_atomic_state *state = pipe_config->base.state;
  5369. struct intel_crtc *other_crtc;
  5370. struct intel_crtc_state *other_crtc_state;
  5371. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5372. pipe_name(pipe), pipe_config->fdi_lanes);
  5373. if (pipe_config->fdi_lanes > 4) {
  5374. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5375. pipe_name(pipe), pipe_config->fdi_lanes);
  5376. return -EINVAL;
  5377. }
  5378. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5379. if (pipe_config->fdi_lanes > 2) {
  5380. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5381. pipe_config->fdi_lanes);
  5382. return -EINVAL;
  5383. } else {
  5384. return 0;
  5385. }
  5386. }
  5387. if (INTEL_INFO(dev)->num_pipes == 2)
  5388. return 0;
  5389. /* Ivybridge 3 pipe is really complicated */
  5390. switch (pipe) {
  5391. case PIPE_A:
  5392. return 0;
  5393. case PIPE_B:
  5394. if (pipe_config->fdi_lanes <= 2)
  5395. return 0;
  5396. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5397. other_crtc_state =
  5398. intel_atomic_get_crtc_state(state, other_crtc);
  5399. if (IS_ERR(other_crtc_state))
  5400. return PTR_ERR(other_crtc_state);
  5401. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5402. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5403. pipe_name(pipe), pipe_config->fdi_lanes);
  5404. return -EINVAL;
  5405. }
  5406. return 0;
  5407. case PIPE_C:
  5408. if (pipe_config->fdi_lanes > 2) {
  5409. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5410. pipe_name(pipe), pipe_config->fdi_lanes);
  5411. return -EINVAL;
  5412. }
  5413. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5414. other_crtc_state =
  5415. intel_atomic_get_crtc_state(state, other_crtc);
  5416. if (IS_ERR(other_crtc_state))
  5417. return PTR_ERR(other_crtc_state);
  5418. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5419. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5420. return -EINVAL;
  5421. }
  5422. return 0;
  5423. default:
  5424. BUG();
  5425. }
  5426. }
  5427. #define RETRY 1
  5428. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5429. struct intel_crtc_state *pipe_config)
  5430. {
  5431. struct drm_device *dev = intel_crtc->base.dev;
  5432. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5433. int lane, link_bw, fdi_dotclock, ret;
  5434. bool needs_recompute = false;
  5435. retry:
  5436. /* FDI is a binary signal running at ~2.7GHz, encoding
  5437. * each output octet as 10 bits. The actual frequency
  5438. * is stored as a divider into a 100MHz clock, and the
  5439. * mode pixel clock is stored in units of 1KHz.
  5440. * Hence the bw of each lane in terms of the mode signal
  5441. * is:
  5442. */
  5443. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5444. fdi_dotclock = adjusted_mode->crtc_clock;
  5445. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5446. pipe_config->pipe_bpp);
  5447. pipe_config->fdi_lanes = lane;
  5448. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5449. link_bw, &pipe_config->fdi_m_n);
  5450. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5451. intel_crtc->pipe, pipe_config);
  5452. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5453. pipe_config->pipe_bpp -= 2*3;
  5454. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5455. pipe_config->pipe_bpp);
  5456. needs_recompute = true;
  5457. pipe_config->bw_constrained = true;
  5458. goto retry;
  5459. }
  5460. if (needs_recompute)
  5461. return RETRY;
  5462. return ret;
  5463. }
  5464. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5465. struct intel_crtc_state *pipe_config)
  5466. {
  5467. if (pipe_config->pipe_bpp > 24)
  5468. return false;
  5469. /* HSW can handle pixel rate up to cdclk? */
  5470. if (IS_HASWELL(dev_priv->dev))
  5471. return true;
  5472. /*
  5473. * We compare against max which means we must take
  5474. * the increased cdclk requirement into account when
  5475. * calculating the new cdclk.
  5476. *
  5477. * Should measure whether using a lower cdclk w/o IPS
  5478. */
  5479. return ilk_pipe_pixel_rate(pipe_config) <=
  5480. dev_priv->max_cdclk_freq * 95 / 100;
  5481. }
  5482. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5483. struct intel_crtc_state *pipe_config)
  5484. {
  5485. struct drm_device *dev = crtc->base.dev;
  5486. struct drm_i915_private *dev_priv = dev->dev_private;
  5487. pipe_config->ips_enabled = i915.enable_ips &&
  5488. hsw_crtc_supports_ips(crtc) &&
  5489. pipe_config_supports_ips(dev_priv, pipe_config);
  5490. }
  5491. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5492. struct intel_crtc_state *pipe_config)
  5493. {
  5494. struct drm_device *dev = crtc->base.dev;
  5495. struct drm_i915_private *dev_priv = dev->dev_private;
  5496. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5497. /* FIXME should check pixel clock limits on all platforms */
  5498. if (INTEL_INFO(dev)->gen < 4) {
  5499. int clock_limit = dev_priv->max_cdclk_freq;
  5500. /*
  5501. * Enable pixel doubling when the dot clock
  5502. * is > 90% of the (display) core speed.
  5503. *
  5504. * GDG double wide on either pipe,
  5505. * otherwise pipe A only.
  5506. */
  5507. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5508. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5509. clock_limit *= 2;
  5510. pipe_config->double_wide = true;
  5511. }
  5512. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5513. return -EINVAL;
  5514. }
  5515. /*
  5516. * Pipe horizontal size must be even in:
  5517. * - DVO ganged mode
  5518. * - LVDS dual channel mode
  5519. * - Double wide pipe
  5520. */
  5521. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5522. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5523. pipe_config->pipe_src_w &= ~1;
  5524. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5525. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5526. */
  5527. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5528. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5529. return -EINVAL;
  5530. if (HAS_IPS(dev))
  5531. hsw_compute_ips_config(crtc, pipe_config);
  5532. if (pipe_config->has_pch_encoder)
  5533. return ironlake_fdi_compute_config(crtc, pipe_config);
  5534. return 0;
  5535. }
  5536. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5537. {
  5538. struct drm_i915_private *dev_priv = to_i915(dev);
  5539. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5540. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5541. uint32_t linkrate;
  5542. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5543. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5544. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5545. return 540000;
  5546. linkrate = (I915_READ(DPLL_CTRL1) &
  5547. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5548. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5549. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5550. /* vco 8640 */
  5551. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5552. case CDCLK_FREQ_450_432:
  5553. return 432000;
  5554. case CDCLK_FREQ_337_308:
  5555. return 308570;
  5556. case CDCLK_FREQ_675_617:
  5557. return 617140;
  5558. default:
  5559. WARN(1, "Unknown cd freq selection\n");
  5560. }
  5561. } else {
  5562. /* vco 8100 */
  5563. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5564. case CDCLK_FREQ_450_432:
  5565. return 450000;
  5566. case CDCLK_FREQ_337_308:
  5567. return 337500;
  5568. case CDCLK_FREQ_675_617:
  5569. return 675000;
  5570. default:
  5571. WARN(1, "Unknown cd freq selection\n");
  5572. }
  5573. }
  5574. /* error case, do as if DPLL0 isn't enabled */
  5575. return 24000;
  5576. }
  5577. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5578. {
  5579. struct drm_i915_private *dev_priv = to_i915(dev);
  5580. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5581. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5582. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5583. int cdclk;
  5584. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5585. return 19200;
  5586. cdclk = 19200 * pll_ratio / 2;
  5587. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5588. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5589. return cdclk; /* 576MHz or 624MHz */
  5590. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5591. return cdclk * 2 / 3; /* 384MHz */
  5592. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5593. return cdclk / 2; /* 288MHz */
  5594. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5595. return cdclk / 4; /* 144MHz */
  5596. }
  5597. /* error case, do as if DE PLL isn't enabled */
  5598. return 19200;
  5599. }
  5600. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5601. {
  5602. struct drm_i915_private *dev_priv = dev->dev_private;
  5603. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5604. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5605. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5606. return 800000;
  5607. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5608. return 450000;
  5609. else if (freq == LCPLL_CLK_FREQ_450)
  5610. return 450000;
  5611. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5612. return 540000;
  5613. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5614. return 337500;
  5615. else
  5616. return 675000;
  5617. }
  5618. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5619. {
  5620. struct drm_i915_private *dev_priv = dev->dev_private;
  5621. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5622. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5623. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5624. return 800000;
  5625. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5626. return 450000;
  5627. else if (freq == LCPLL_CLK_FREQ_450)
  5628. return 450000;
  5629. else if (IS_HSW_ULT(dev))
  5630. return 337500;
  5631. else
  5632. return 540000;
  5633. }
  5634. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5635. {
  5636. struct drm_i915_private *dev_priv = dev->dev_private;
  5637. u32 val;
  5638. int divider;
  5639. if (dev_priv->hpll_freq == 0)
  5640. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5641. mutex_lock(&dev_priv->sb_lock);
  5642. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5643. mutex_unlock(&dev_priv->sb_lock);
  5644. divider = val & DISPLAY_FREQUENCY_VALUES;
  5645. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5646. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5647. "cdclk change in progress\n");
  5648. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5649. }
  5650. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5651. {
  5652. return 450000;
  5653. }
  5654. static int i945_get_display_clock_speed(struct drm_device *dev)
  5655. {
  5656. return 400000;
  5657. }
  5658. static int i915_get_display_clock_speed(struct drm_device *dev)
  5659. {
  5660. return 333333;
  5661. }
  5662. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5663. {
  5664. return 200000;
  5665. }
  5666. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5667. {
  5668. u16 gcfgc = 0;
  5669. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5670. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5671. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5672. return 266667;
  5673. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5674. return 333333;
  5675. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5676. return 444444;
  5677. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5678. return 200000;
  5679. default:
  5680. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5681. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5682. return 133333;
  5683. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5684. return 166667;
  5685. }
  5686. }
  5687. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5688. {
  5689. u16 gcfgc = 0;
  5690. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5691. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5692. return 133333;
  5693. else {
  5694. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5695. case GC_DISPLAY_CLOCK_333_MHZ:
  5696. return 333333;
  5697. default:
  5698. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5699. return 190000;
  5700. }
  5701. }
  5702. }
  5703. static int i865_get_display_clock_speed(struct drm_device *dev)
  5704. {
  5705. return 266667;
  5706. }
  5707. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5708. {
  5709. u16 hpllcc = 0;
  5710. /*
  5711. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5712. * encoding is different :(
  5713. * FIXME is this the right way to detect 852GM/852GMV?
  5714. */
  5715. if (dev->pdev->revision == 0x1)
  5716. return 133333;
  5717. pci_bus_read_config_word(dev->pdev->bus,
  5718. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5719. /* Assume that the hardware is in the high speed state. This
  5720. * should be the default.
  5721. */
  5722. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5723. case GC_CLOCK_133_200:
  5724. case GC_CLOCK_133_200_2:
  5725. case GC_CLOCK_100_200:
  5726. return 200000;
  5727. case GC_CLOCK_166_250:
  5728. return 250000;
  5729. case GC_CLOCK_100_133:
  5730. return 133333;
  5731. case GC_CLOCK_133_266:
  5732. case GC_CLOCK_133_266_2:
  5733. case GC_CLOCK_166_266:
  5734. return 266667;
  5735. }
  5736. /* Shouldn't happen */
  5737. return 0;
  5738. }
  5739. static int i830_get_display_clock_speed(struct drm_device *dev)
  5740. {
  5741. return 133333;
  5742. }
  5743. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5744. {
  5745. struct drm_i915_private *dev_priv = dev->dev_private;
  5746. static const unsigned int blb_vco[8] = {
  5747. [0] = 3200000,
  5748. [1] = 4000000,
  5749. [2] = 5333333,
  5750. [3] = 4800000,
  5751. [4] = 6400000,
  5752. };
  5753. static const unsigned int pnv_vco[8] = {
  5754. [0] = 3200000,
  5755. [1] = 4000000,
  5756. [2] = 5333333,
  5757. [3] = 4800000,
  5758. [4] = 2666667,
  5759. };
  5760. static const unsigned int cl_vco[8] = {
  5761. [0] = 3200000,
  5762. [1] = 4000000,
  5763. [2] = 5333333,
  5764. [3] = 6400000,
  5765. [4] = 3333333,
  5766. [5] = 3566667,
  5767. [6] = 4266667,
  5768. };
  5769. static const unsigned int elk_vco[8] = {
  5770. [0] = 3200000,
  5771. [1] = 4000000,
  5772. [2] = 5333333,
  5773. [3] = 4800000,
  5774. };
  5775. static const unsigned int ctg_vco[8] = {
  5776. [0] = 3200000,
  5777. [1] = 4000000,
  5778. [2] = 5333333,
  5779. [3] = 6400000,
  5780. [4] = 2666667,
  5781. [5] = 4266667,
  5782. };
  5783. const unsigned int *vco_table;
  5784. unsigned int vco;
  5785. uint8_t tmp = 0;
  5786. /* FIXME other chipsets? */
  5787. if (IS_GM45(dev))
  5788. vco_table = ctg_vco;
  5789. else if (IS_G4X(dev))
  5790. vco_table = elk_vco;
  5791. else if (IS_CRESTLINE(dev))
  5792. vco_table = cl_vco;
  5793. else if (IS_PINEVIEW(dev))
  5794. vco_table = pnv_vco;
  5795. else if (IS_G33(dev))
  5796. vco_table = blb_vco;
  5797. else
  5798. return 0;
  5799. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5800. vco = vco_table[tmp & 0x7];
  5801. if (vco == 0)
  5802. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5803. else
  5804. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5805. return vco;
  5806. }
  5807. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5808. {
  5809. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5810. uint16_t tmp = 0;
  5811. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5812. cdclk_sel = (tmp >> 12) & 0x1;
  5813. switch (vco) {
  5814. case 2666667:
  5815. case 4000000:
  5816. case 5333333:
  5817. return cdclk_sel ? 333333 : 222222;
  5818. case 3200000:
  5819. return cdclk_sel ? 320000 : 228571;
  5820. default:
  5821. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5822. return 222222;
  5823. }
  5824. }
  5825. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5826. {
  5827. static const uint8_t div_3200[] = { 16, 10, 8 };
  5828. static const uint8_t div_4000[] = { 20, 12, 10 };
  5829. static const uint8_t div_5333[] = { 24, 16, 14 };
  5830. const uint8_t *div_table;
  5831. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5832. uint16_t tmp = 0;
  5833. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5834. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5835. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5836. goto fail;
  5837. switch (vco) {
  5838. case 3200000:
  5839. div_table = div_3200;
  5840. break;
  5841. case 4000000:
  5842. div_table = div_4000;
  5843. break;
  5844. case 5333333:
  5845. div_table = div_5333;
  5846. break;
  5847. default:
  5848. goto fail;
  5849. }
  5850. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5851. fail:
  5852. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5853. return 200000;
  5854. }
  5855. static int g33_get_display_clock_speed(struct drm_device *dev)
  5856. {
  5857. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5858. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5859. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5860. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5861. const uint8_t *div_table;
  5862. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5863. uint16_t tmp = 0;
  5864. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5865. cdclk_sel = (tmp >> 4) & 0x7;
  5866. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5867. goto fail;
  5868. switch (vco) {
  5869. case 3200000:
  5870. div_table = div_3200;
  5871. break;
  5872. case 4000000:
  5873. div_table = div_4000;
  5874. break;
  5875. case 4800000:
  5876. div_table = div_4800;
  5877. break;
  5878. case 5333333:
  5879. div_table = div_5333;
  5880. break;
  5881. default:
  5882. goto fail;
  5883. }
  5884. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5885. fail:
  5886. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5887. return 190476;
  5888. }
  5889. static void
  5890. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5891. {
  5892. while (*num > DATA_LINK_M_N_MASK ||
  5893. *den > DATA_LINK_M_N_MASK) {
  5894. *num >>= 1;
  5895. *den >>= 1;
  5896. }
  5897. }
  5898. static void compute_m_n(unsigned int m, unsigned int n,
  5899. uint32_t *ret_m, uint32_t *ret_n)
  5900. {
  5901. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5902. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5903. intel_reduce_m_n_ratio(ret_m, ret_n);
  5904. }
  5905. void
  5906. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5907. int pixel_clock, int link_clock,
  5908. struct intel_link_m_n *m_n)
  5909. {
  5910. m_n->tu = 64;
  5911. compute_m_n(bits_per_pixel * pixel_clock,
  5912. link_clock * nlanes * 8,
  5913. &m_n->gmch_m, &m_n->gmch_n);
  5914. compute_m_n(pixel_clock, link_clock,
  5915. &m_n->link_m, &m_n->link_n);
  5916. }
  5917. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5918. {
  5919. if (i915.panel_use_ssc >= 0)
  5920. return i915.panel_use_ssc != 0;
  5921. return dev_priv->vbt.lvds_use_ssc
  5922. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5923. }
  5924. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5925. int num_connectors)
  5926. {
  5927. struct drm_device *dev = crtc_state->base.crtc->dev;
  5928. struct drm_i915_private *dev_priv = dev->dev_private;
  5929. int refclk;
  5930. WARN_ON(!crtc_state->base.state);
  5931. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5932. refclk = 100000;
  5933. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5934. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5935. refclk = dev_priv->vbt.lvds_ssc_freq;
  5936. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5937. } else if (!IS_GEN2(dev)) {
  5938. refclk = 96000;
  5939. } else {
  5940. refclk = 48000;
  5941. }
  5942. return refclk;
  5943. }
  5944. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5945. {
  5946. return (1 << dpll->n) << 16 | dpll->m2;
  5947. }
  5948. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5949. {
  5950. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5951. }
  5952. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5953. struct intel_crtc_state *crtc_state,
  5954. intel_clock_t *reduced_clock)
  5955. {
  5956. struct drm_device *dev = crtc->base.dev;
  5957. u32 fp, fp2 = 0;
  5958. if (IS_PINEVIEW(dev)) {
  5959. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5960. if (reduced_clock)
  5961. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5962. } else {
  5963. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5964. if (reduced_clock)
  5965. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5966. }
  5967. crtc_state->dpll_hw_state.fp0 = fp;
  5968. crtc->lowfreq_avail = false;
  5969. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5970. reduced_clock) {
  5971. crtc_state->dpll_hw_state.fp1 = fp2;
  5972. crtc->lowfreq_avail = true;
  5973. } else {
  5974. crtc_state->dpll_hw_state.fp1 = fp;
  5975. }
  5976. }
  5977. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5978. pipe)
  5979. {
  5980. u32 reg_val;
  5981. /*
  5982. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5983. * and set it to a reasonable value instead.
  5984. */
  5985. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5986. reg_val &= 0xffffff00;
  5987. reg_val |= 0x00000030;
  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 &= 0x8cffffff;
  5991. reg_val = 0x8c000000;
  5992. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5993. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5994. reg_val &= 0xffffff00;
  5995. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5996. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5997. reg_val &= 0x00ffffff;
  5998. reg_val |= 0xb0000000;
  5999. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6000. }
  6001. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6002. struct intel_link_m_n *m_n)
  6003. {
  6004. struct drm_device *dev = crtc->base.dev;
  6005. struct drm_i915_private *dev_priv = dev->dev_private;
  6006. int pipe = crtc->pipe;
  6007. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6008. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6009. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6010. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6011. }
  6012. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6013. struct intel_link_m_n *m_n,
  6014. struct intel_link_m_n *m2_n2)
  6015. {
  6016. struct drm_device *dev = crtc->base.dev;
  6017. struct drm_i915_private *dev_priv = dev->dev_private;
  6018. int pipe = crtc->pipe;
  6019. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6020. if (INTEL_INFO(dev)->gen >= 5) {
  6021. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6022. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6023. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6024. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6025. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6026. * for gen < 8) and if DRRS is supported (to make sure the
  6027. * registers are not unnecessarily accessed).
  6028. */
  6029. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6030. crtc->config->has_drrs) {
  6031. I915_WRITE(PIPE_DATA_M2(transcoder),
  6032. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6033. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6034. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6035. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6036. }
  6037. } else {
  6038. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6039. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6040. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6041. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6042. }
  6043. }
  6044. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6045. {
  6046. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6047. if (m_n == M1_N1) {
  6048. dp_m_n = &crtc->config->dp_m_n;
  6049. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6050. } else if (m_n == M2_N2) {
  6051. /*
  6052. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6053. * needs to be programmed into M1_N1.
  6054. */
  6055. dp_m_n = &crtc->config->dp_m2_n2;
  6056. } else {
  6057. DRM_ERROR("Unsupported divider value\n");
  6058. return;
  6059. }
  6060. if (crtc->config->has_pch_encoder)
  6061. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6062. else
  6063. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6064. }
  6065. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6066. struct intel_crtc_state *pipe_config)
  6067. {
  6068. u32 dpll, dpll_md;
  6069. /*
  6070. * Enable DPIO clock input. We should never disable the reference
  6071. * clock for pipe B, since VGA hotplug / manual detection depends
  6072. * on it.
  6073. */
  6074. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6075. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6076. /* We should never disable this, set it here for state tracking */
  6077. if (crtc->pipe == PIPE_B)
  6078. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6079. dpll |= DPLL_VCO_ENABLE;
  6080. pipe_config->dpll_hw_state.dpll = dpll;
  6081. dpll_md = (pipe_config->pixel_multiplier - 1)
  6082. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6083. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6084. }
  6085. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6086. const struct intel_crtc_state *pipe_config)
  6087. {
  6088. struct drm_device *dev = crtc->base.dev;
  6089. struct drm_i915_private *dev_priv = dev->dev_private;
  6090. int pipe = crtc->pipe;
  6091. u32 mdiv;
  6092. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6093. u32 coreclk, reg_val;
  6094. mutex_lock(&dev_priv->sb_lock);
  6095. bestn = pipe_config->dpll.n;
  6096. bestm1 = pipe_config->dpll.m1;
  6097. bestm2 = pipe_config->dpll.m2;
  6098. bestp1 = pipe_config->dpll.p1;
  6099. bestp2 = pipe_config->dpll.p2;
  6100. /* See eDP HDMI DPIO driver vbios notes doc */
  6101. /* PLL B needs special handling */
  6102. if (pipe == PIPE_B)
  6103. vlv_pllb_recal_opamp(dev_priv, pipe);
  6104. /* Set up Tx target for periodic Rcomp update */
  6105. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6106. /* Disable target IRef on PLL */
  6107. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6108. reg_val &= 0x00ffffff;
  6109. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6110. /* Disable fast lock */
  6111. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6112. /* Set idtafcrecal before PLL is enabled */
  6113. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6114. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6115. mdiv |= ((bestn << DPIO_N_SHIFT));
  6116. mdiv |= (1 << DPIO_K_SHIFT);
  6117. /*
  6118. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6119. * but we don't support that).
  6120. * Note: don't use the DAC post divider as it seems unstable.
  6121. */
  6122. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6123. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6124. mdiv |= DPIO_ENABLE_CALIBRATION;
  6125. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6126. /* Set HBR and RBR LPF coefficients */
  6127. if (pipe_config->port_clock == 162000 ||
  6128. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6129. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6130. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6131. 0x009f0003);
  6132. else
  6133. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6134. 0x00d0000f);
  6135. if (pipe_config->has_dp_encoder) {
  6136. /* Use SSC source */
  6137. if (pipe == PIPE_A)
  6138. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6139. 0x0df40000);
  6140. else
  6141. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6142. 0x0df70000);
  6143. } else { /* HDMI or VGA */
  6144. /* Use bend source */
  6145. if (pipe == PIPE_A)
  6146. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6147. 0x0df70000);
  6148. else
  6149. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6150. 0x0df40000);
  6151. }
  6152. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6153. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6154. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6155. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6156. coreclk |= 0x01000000;
  6157. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6158. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6159. mutex_unlock(&dev_priv->sb_lock);
  6160. }
  6161. static void chv_compute_dpll(struct intel_crtc *crtc,
  6162. struct intel_crtc_state *pipe_config)
  6163. {
  6164. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6165. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6166. DPLL_VCO_ENABLE;
  6167. if (crtc->pipe != PIPE_A)
  6168. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6169. pipe_config->dpll_hw_state.dpll_md =
  6170. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6171. }
  6172. static void chv_prepare_pll(struct intel_crtc *crtc,
  6173. const struct intel_crtc_state *pipe_config)
  6174. {
  6175. struct drm_device *dev = crtc->base.dev;
  6176. struct drm_i915_private *dev_priv = dev->dev_private;
  6177. int pipe = crtc->pipe;
  6178. int dpll_reg = DPLL(crtc->pipe);
  6179. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6180. u32 loopfilter, tribuf_calcntr;
  6181. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6182. u32 dpio_val;
  6183. int vco;
  6184. bestn = pipe_config->dpll.n;
  6185. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6186. bestm1 = pipe_config->dpll.m1;
  6187. bestm2 = pipe_config->dpll.m2 >> 22;
  6188. bestp1 = pipe_config->dpll.p1;
  6189. bestp2 = pipe_config->dpll.p2;
  6190. vco = pipe_config->dpll.vco;
  6191. dpio_val = 0;
  6192. loopfilter = 0;
  6193. /*
  6194. * Enable Refclk and SSC
  6195. */
  6196. I915_WRITE(dpll_reg,
  6197. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6198. mutex_lock(&dev_priv->sb_lock);
  6199. /* p1 and p2 divider */
  6200. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6201. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6202. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6203. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6204. 1 << DPIO_CHV_K_DIV_SHIFT);
  6205. /* Feedback post-divider - m2 */
  6206. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6207. /* Feedback refclk divider - n and m1 */
  6208. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6209. DPIO_CHV_M1_DIV_BY_2 |
  6210. 1 << DPIO_CHV_N_DIV_SHIFT);
  6211. /* M2 fraction division */
  6212. if (bestm2_frac)
  6213. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6214. /* M2 fraction division enable */
  6215. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6216. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6217. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6218. if (bestm2_frac)
  6219. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6220. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6221. /* Program digital lock detect threshold */
  6222. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6223. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6224. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6225. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6226. if (!bestm2_frac)
  6227. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6228. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6229. /* Loop filter */
  6230. if (vco == 5400000) {
  6231. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6232. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6233. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6234. tribuf_calcntr = 0x9;
  6235. } else if (vco <= 6200000) {
  6236. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6237. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6238. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6239. tribuf_calcntr = 0x9;
  6240. } else if (vco <= 6480000) {
  6241. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6242. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6243. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6244. tribuf_calcntr = 0x8;
  6245. } else {
  6246. /* Not supported. Apply the same limits as in the max case */
  6247. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6248. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6249. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6250. tribuf_calcntr = 0;
  6251. }
  6252. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6253. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6254. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6255. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6256. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6257. /* AFC Recal */
  6258. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6259. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6260. DPIO_AFC_RECAL);
  6261. mutex_unlock(&dev_priv->sb_lock);
  6262. }
  6263. /**
  6264. * vlv_force_pll_on - forcibly enable just the PLL
  6265. * @dev_priv: i915 private structure
  6266. * @pipe: pipe PLL to enable
  6267. * @dpll: PLL configuration
  6268. *
  6269. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6270. * in cases where we need the PLL enabled even when @pipe is not going to
  6271. * be enabled.
  6272. */
  6273. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6274. const struct dpll *dpll)
  6275. {
  6276. struct intel_crtc *crtc =
  6277. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6278. struct intel_crtc_state pipe_config = {
  6279. .base.crtc = &crtc->base,
  6280. .pixel_multiplier = 1,
  6281. .dpll = *dpll,
  6282. };
  6283. if (IS_CHERRYVIEW(dev)) {
  6284. chv_compute_dpll(crtc, &pipe_config);
  6285. chv_prepare_pll(crtc, &pipe_config);
  6286. chv_enable_pll(crtc, &pipe_config);
  6287. } else {
  6288. vlv_compute_dpll(crtc, &pipe_config);
  6289. vlv_prepare_pll(crtc, &pipe_config);
  6290. vlv_enable_pll(crtc, &pipe_config);
  6291. }
  6292. }
  6293. /**
  6294. * vlv_force_pll_off - forcibly disable just the PLL
  6295. * @dev_priv: i915 private structure
  6296. * @pipe: pipe PLL to disable
  6297. *
  6298. * Disable the PLL for @pipe. To be used in cases where we need
  6299. * the PLL enabled even when @pipe is not going to be enabled.
  6300. */
  6301. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6302. {
  6303. if (IS_CHERRYVIEW(dev))
  6304. chv_disable_pll(to_i915(dev), pipe);
  6305. else
  6306. vlv_disable_pll(to_i915(dev), pipe);
  6307. }
  6308. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6309. struct intel_crtc_state *crtc_state,
  6310. intel_clock_t *reduced_clock,
  6311. int num_connectors)
  6312. {
  6313. struct drm_device *dev = crtc->base.dev;
  6314. struct drm_i915_private *dev_priv = dev->dev_private;
  6315. u32 dpll;
  6316. bool is_sdvo;
  6317. struct dpll *clock = &crtc_state->dpll;
  6318. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6319. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6320. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6321. dpll = DPLL_VGA_MODE_DIS;
  6322. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6323. dpll |= DPLLB_MODE_LVDS;
  6324. else
  6325. dpll |= DPLLB_MODE_DAC_SERIAL;
  6326. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6327. dpll |= (crtc_state->pixel_multiplier - 1)
  6328. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6329. }
  6330. if (is_sdvo)
  6331. dpll |= DPLL_SDVO_HIGH_SPEED;
  6332. if (crtc_state->has_dp_encoder)
  6333. dpll |= DPLL_SDVO_HIGH_SPEED;
  6334. /* compute bitmask from p1 value */
  6335. if (IS_PINEVIEW(dev))
  6336. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6337. else {
  6338. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6339. if (IS_G4X(dev) && reduced_clock)
  6340. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6341. }
  6342. switch (clock->p2) {
  6343. case 5:
  6344. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6345. break;
  6346. case 7:
  6347. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6348. break;
  6349. case 10:
  6350. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6351. break;
  6352. case 14:
  6353. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6354. break;
  6355. }
  6356. if (INTEL_INFO(dev)->gen >= 4)
  6357. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6358. if (crtc_state->sdvo_tv_clock)
  6359. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6360. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6361. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6362. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6363. else
  6364. dpll |= PLL_REF_INPUT_DREFCLK;
  6365. dpll |= DPLL_VCO_ENABLE;
  6366. crtc_state->dpll_hw_state.dpll = dpll;
  6367. if (INTEL_INFO(dev)->gen >= 4) {
  6368. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6369. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6370. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6371. }
  6372. }
  6373. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6374. struct intel_crtc_state *crtc_state,
  6375. intel_clock_t *reduced_clock,
  6376. int num_connectors)
  6377. {
  6378. struct drm_device *dev = crtc->base.dev;
  6379. struct drm_i915_private *dev_priv = dev->dev_private;
  6380. u32 dpll;
  6381. struct dpll *clock = &crtc_state->dpll;
  6382. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6383. dpll = DPLL_VGA_MODE_DIS;
  6384. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6385. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6386. } else {
  6387. if (clock->p1 == 2)
  6388. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6389. else
  6390. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6391. if (clock->p2 == 4)
  6392. dpll |= PLL_P2_DIVIDE_BY_4;
  6393. }
  6394. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6395. dpll |= DPLL_DVO_2X_MODE;
  6396. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6397. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6398. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6399. else
  6400. dpll |= PLL_REF_INPUT_DREFCLK;
  6401. dpll |= DPLL_VCO_ENABLE;
  6402. crtc_state->dpll_hw_state.dpll = dpll;
  6403. }
  6404. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6405. {
  6406. struct drm_device *dev = intel_crtc->base.dev;
  6407. struct drm_i915_private *dev_priv = dev->dev_private;
  6408. enum pipe pipe = intel_crtc->pipe;
  6409. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6410. struct drm_display_mode *adjusted_mode =
  6411. &intel_crtc->config->base.adjusted_mode;
  6412. uint32_t crtc_vtotal, crtc_vblank_end;
  6413. int vsyncshift = 0;
  6414. /* We need to be careful not to changed the adjusted mode, for otherwise
  6415. * the hw state checker will get angry at the mismatch. */
  6416. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6417. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6418. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6419. /* the chip adds 2 halflines automatically */
  6420. crtc_vtotal -= 1;
  6421. crtc_vblank_end -= 1;
  6422. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6423. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6424. else
  6425. vsyncshift = adjusted_mode->crtc_hsync_start -
  6426. adjusted_mode->crtc_htotal / 2;
  6427. if (vsyncshift < 0)
  6428. vsyncshift += adjusted_mode->crtc_htotal;
  6429. }
  6430. if (INTEL_INFO(dev)->gen > 3)
  6431. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6432. I915_WRITE(HTOTAL(cpu_transcoder),
  6433. (adjusted_mode->crtc_hdisplay - 1) |
  6434. ((adjusted_mode->crtc_htotal - 1) << 16));
  6435. I915_WRITE(HBLANK(cpu_transcoder),
  6436. (adjusted_mode->crtc_hblank_start - 1) |
  6437. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6438. I915_WRITE(HSYNC(cpu_transcoder),
  6439. (adjusted_mode->crtc_hsync_start - 1) |
  6440. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6441. I915_WRITE(VTOTAL(cpu_transcoder),
  6442. (adjusted_mode->crtc_vdisplay - 1) |
  6443. ((crtc_vtotal - 1) << 16));
  6444. I915_WRITE(VBLANK(cpu_transcoder),
  6445. (adjusted_mode->crtc_vblank_start - 1) |
  6446. ((crtc_vblank_end - 1) << 16));
  6447. I915_WRITE(VSYNC(cpu_transcoder),
  6448. (adjusted_mode->crtc_vsync_start - 1) |
  6449. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6450. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6451. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6452. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6453. * bits. */
  6454. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6455. (pipe == PIPE_B || pipe == PIPE_C))
  6456. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6457. /* pipesrc controls the size that is scaled from, which should
  6458. * always be the user's requested size.
  6459. */
  6460. I915_WRITE(PIPESRC(pipe),
  6461. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6462. (intel_crtc->config->pipe_src_h - 1));
  6463. }
  6464. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6465. struct intel_crtc_state *pipe_config)
  6466. {
  6467. struct drm_device *dev = crtc->base.dev;
  6468. struct drm_i915_private *dev_priv = dev->dev_private;
  6469. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6470. uint32_t tmp;
  6471. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6472. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6473. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6474. tmp = I915_READ(HBLANK(cpu_transcoder));
  6475. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6476. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6477. tmp = I915_READ(HSYNC(cpu_transcoder));
  6478. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6479. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6480. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6481. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6482. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6483. tmp = I915_READ(VBLANK(cpu_transcoder));
  6484. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6485. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6486. tmp = I915_READ(VSYNC(cpu_transcoder));
  6487. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6488. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6489. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6490. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6491. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6492. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6493. }
  6494. tmp = I915_READ(PIPESRC(crtc->pipe));
  6495. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6496. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6497. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6498. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6499. }
  6500. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6501. struct intel_crtc_state *pipe_config)
  6502. {
  6503. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6504. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6505. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6506. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6507. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6508. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6509. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6510. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6511. mode->flags = pipe_config->base.adjusted_mode.flags;
  6512. mode->type = DRM_MODE_TYPE_DRIVER;
  6513. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6514. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6515. mode->hsync = drm_mode_hsync(mode);
  6516. mode->vrefresh = drm_mode_vrefresh(mode);
  6517. drm_mode_set_name(mode);
  6518. }
  6519. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6520. {
  6521. struct drm_device *dev = intel_crtc->base.dev;
  6522. struct drm_i915_private *dev_priv = dev->dev_private;
  6523. uint32_t pipeconf;
  6524. pipeconf = 0;
  6525. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6526. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6527. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6528. if (intel_crtc->config->double_wide)
  6529. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6530. /* only g4x and later have fancy bpc/dither controls */
  6531. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6532. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6533. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6534. pipeconf |= PIPECONF_DITHER_EN |
  6535. PIPECONF_DITHER_TYPE_SP;
  6536. switch (intel_crtc->config->pipe_bpp) {
  6537. case 18:
  6538. pipeconf |= PIPECONF_6BPC;
  6539. break;
  6540. case 24:
  6541. pipeconf |= PIPECONF_8BPC;
  6542. break;
  6543. case 30:
  6544. pipeconf |= PIPECONF_10BPC;
  6545. break;
  6546. default:
  6547. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6548. BUG();
  6549. }
  6550. }
  6551. if (HAS_PIPE_CXSR(dev)) {
  6552. if (intel_crtc->lowfreq_avail) {
  6553. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6554. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6555. } else {
  6556. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6557. }
  6558. }
  6559. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6560. if (INTEL_INFO(dev)->gen < 4 ||
  6561. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6562. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6563. else
  6564. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6565. } else
  6566. pipeconf |= PIPECONF_PROGRESSIVE;
  6567. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6568. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6569. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6570. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6571. }
  6572. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6573. struct intel_crtc_state *crtc_state)
  6574. {
  6575. struct drm_device *dev = crtc->base.dev;
  6576. struct drm_i915_private *dev_priv = dev->dev_private;
  6577. int refclk, num_connectors = 0;
  6578. intel_clock_t clock;
  6579. bool ok;
  6580. bool is_dsi = false;
  6581. struct intel_encoder *encoder;
  6582. const intel_limit_t *limit;
  6583. struct drm_atomic_state *state = crtc_state->base.state;
  6584. struct drm_connector *connector;
  6585. struct drm_connector_state *connector_state;
  6586. int i;
  6587. memset(&crtc_state->dpll_hw_state, 0,
  6588. sizeof(crtc_state->dpll_hw_state));
  6589. for_each_connector_in_state(state, connector, connector_state, i) {
  6590. if (connector_state->crtc != &crtc->base)
  6591. continue;
  6592. encoder = to_intel_encoder(connector_state->best_encoder);
  6593. switch (encoder->type) {
  6594. case INTEL_OUTPUT_DSI:
  6595. is_dsi = true;
  6596. break;
  6597. default:
  6598. break;
  6599. }
  6600. num_connectors++;
  6601. }
  6602. if (is_dsi)
  6603. return 0;
  6604. if (!crtc_state->clock_set) {
  6605. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6606. /*
  6607. * Returns a set of divisors for the desired target clock with
  6608. * the given refclk, or FALSE. The returned values represent
  6609. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6610. * 2) / p1 / p2.
  6611. */
  6612. limit = intel_limit(crtc_state, refclk);
  6613. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6614. crtc_state->port_clock,
  6615. refclk, NULL, &clock);
  6616. if (!ok) {
  6617. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6618. return -EINVAL;
  6619. }
  6620. /* Compat-code for transition, will disappear. */
  6621. crtc_state->dpll.n = clock.n;
  6622. crtc_state->dpll.m1 = clock.m1;
  6623. crtc_state->dpll.m2 = clock.m2;
  6624. crtc_state->dpll.p1 = clock.p1;
  6625. crtc_state->dpll.p2 = clock.p2;
  6626. }
  6627. if (IS_GEN2(dev)) {
  6628. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6629. num_connectors);
  6630. } else if (IS_CHERRYVIEW(dev)) {
  6631. chv_compute_dpll(crtc, crtc_state);
  6632. } else if (IS_VALLEYVIEW(dev)) {
  6633. vlv_compute_dpll(crtc, crtc_state);
  6634. } else {
  6635. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6636. num_connectors);
  6637. }
  6638. return 0;
  6639. }
  6640. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6641. struct intel_crtc_state *pipe_config)
  6642. {
  6643. struct drm_device *dev = crtc->base.dev;
  6644. struct drm_i915_private *dev_priv = dev->dev_private;
  6645. uint32_t tmp;
  6646. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6647. return;
  6648. tmp = I915_READ(PFIT_CONTROL);
  6649. if (!(tmp & PFIT_ENABLE))
  6650. return;
  6651. /* Check whether the pfit is attached to our pipe. */
  6652. if (INTEL_INFO(dev)->gen < 4) {
  6653. if (crtc->pipe != PIPE_B)
  6654. return;
  6655. } else {
  6656. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6657. return;
  6658. }
  6659. pipe_config->gmch_pfit.control = tmp;
  6660. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6661. if (INTEL_INFO(dev)->gen < 5)
  6662. pipe_config->gmch_pfit.lvds_border_bits =
  6663. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6664. }
  6665. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6666. struct intel_crtc_state *pipe_config)
  6667. {
  6668. struct drm_device *dev = crtc->base.dev;
  6669. struct drm_i915_private *dev_priv = dev->dev_private;
  6670. int pipe = pipe_config->cpu_transcoder;
  6671. intel_clock_t clock;
  6672. u32 mdiv;
  6673. int refclk = 100000;
  6674. /* In case of MIPI DPLL will not even be used */
  6675. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6676. return;
  6677. mutex_lock(&dev_priv->sb_lock);
  6678. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6679. mutex_unlock(&dev_priv->sb_lock);
  6680. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6681. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6682. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6683. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6684. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6685. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6686. }
  6687. static void
  6688. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6689. struct intel_initial_plane_config *plane_config)
  6690. {
  6691. struct drm_device *dev = crtc->base.dev;
  6692. struct drm_i915_private *dev_priv = dev->dev_private;
  6693. u32 val, base, offset;
  6694. int pipe = crtc->pipe, plane = crtc->plane;
  6695. int fourcc, pixel_format;
  6696. unsigned int aligned_height;
  6697. struct drm_framebuffer *fb;
  6698. struct intel_framebuffer *intel_fb;
  6699. val = I915_READ(DSPCNTR(plane));
  6700. if (!(val & DISPLAY_PLANE_ENABLE))
  6701. return;
  6702. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6703. if (!intel_fb) {
  6704. DRM_DEBUG_KMS("failed to alloc fb\n");
  6705. return;
  6706. }
  6707. fb = &intel_fb->base;
  6708. if (INTEL_INFO(dev)->gen >= 4) {
  6709. if (val & DISPPLANE_TILED) {
  6710. plane_config->tiling = I915_TILING_X;
  6711. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6712. }
  6713. }
  6714. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6715. fourcc = i9xx_format_to_fourcc(pixel_format);
  6716. fb->pixel_format = fourcc;
  6717. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6718. if (INTEL_INFO(dev)->gen >= 4) {
  6719. if (plane_config->tiling)
  6720. offset = I915_READ(DSPTILEOFF(plane));
  6721. else
  6722. offset = I915_READ(DSPLINOFF(plane));
  6723. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6724. } else {
  6725. base = I915_READ(DSPADDR(plane));
  6726. }
  6727. plane_config->base = base;
  6728. val = I915_READ(PIPESRC(pipe));
  6729. fb->width = ((val >> 16) & 0xfff) + 1;
  6730. fb->height = ((val >> 0) & 0xfff) + 1;
  6731. val = I915_READ(DSPSTRIDE(pipe));
  6732. fb->pitches[0] = val & 0xffffffc0;
  6733. aligned_height = intel_fb_align_height(dev, fb->height,
  6734. fb->pixel_format,
  6735. fb->modifier[0]);
  6736. plane_config->size = fb->pitches[0] * aligned_height;
  6737. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6738. pipe_name(pipe), plane, fb->width, fb->height,
  6739. fb->bits_per_pixel, base, fb->pitches[0],
  6740. plane_config->size);
  6741. plane_config->fb = intel_fb;
  6742. }
  6743. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6744. struct intel_crtc_state *pipe_config)
  6745. {
  6746. struct drm_device *dev = crtc->base.dev;
  6747. struct drm_i915_private *dev_priv = dev->dev_private;
  6748. int pipe = pipe_config->cpu_transcoder;
  6749. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6750. intel_clock_t clock;
  6751. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6752. int refclk = 100000;
  6753. mutex_lock(&dev_priv->sb_lock);
  6754. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6755. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6756. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6757. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6758. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6759. mutex_unlock(&dev_priv->sb_lock);
  6760. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6761. clock.m2 = (pll_dw0 & 0xff) << 22;
  6762. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6763. clock.m2 |= pll_dw2 & 0x3fffff;
  6764. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6765. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6766. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6767. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6768. }
  6769. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6770. struct intel_crtc_state *pipe_config)
  6771. {
  6772. struct drm_device *dev = crtc->base.dev;
  6773. struct drm_i915_private *dev_priv = dev->dev_private;
  6774. uint32_t tmp;
  6775. if (!intel_display_power_is_enabled(dev_priv,
  6776. POWER_DOMAIN_PIPE(crtc->pipe)))
  6777. return false;
  6778. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6779. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6780. tmp = I915_READ(PIPECONF(crtc->pipe));
  6781. if (!(tmp & PIPECONF_ENABLE))
  6782. return false;
  6783. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6784. switch (tmp & PIPECONF_BPC_MASK) {
  6785. case PIPECONF_6BPC:
  6786. pipe_config->pipe_bpp = 18;
  6787. break;
  6788. case PIPECONF_8BPC:
  6789. pipe_config->pipe_bpp = 24;
  6790. break;
  6791. case PIPECONF_10BPC:
  6792. pipe_config->pipe_bpp = 30;
  6793. break;
  6794. default:
  6795. break;
  6796. }
  6797. }
  6798. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6799. pipe_config->limited_color_range = true;
  6800. if (INTEL_INFO(dev)->gen < 4)
  6801. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6802. intel_get_pipe_timings(crtc, pipe_config);
  6803. i9xx_get_pfit_config(crtc, pipe_config);
  6804. if (INTEL_INFO(dev)->gen >= 4) {
  6805. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6806. pipe_config->pixel_multiplier =
  6807. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6808. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6809. pipe_config->dpll_hw_state.dpll_md = tmp;
  6810. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6811. tmp = I915_READ(DPLL(crtc->pipe));
  6812. pipe_config->pixel_multiplier =
  6813. ((tmp & SDVO_MULTIPLIER_MASK)
  6814. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6815. } else {
  6816. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6817. * port and will be fixed up in the encoder->get_config
  6818. * function. */
  6819. pipe_config->pixel_multiplier = 1;
  6820. }
  6821. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6822. if (!IS_VALLEYVIEW(dev)) {
  6823. /*
  6824. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6825. * on 830. Filter it out here so that we don't
  6826. * report errors due to that.
  6827. */
  6828. if (IS_I830(dev))
  6829. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6830. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6831. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6832. } else {
  6833. /* Mask out read-only status bits. */
  6834. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6835. DPLL_PORTC_READY_MASK |
  6836. DPLL_PORTB_READY_MASK);
  6837. }
  6838. if (IS_CHERRYVIEW(dev))
  6839. chv_crtc_clock_get(crtc, pipe_config);
  6840. else if (IS_VALLEYVIEW(dev))
  6841. vlv_crtc_clock_get(crtc, pipe_config);
  6842. else
  6843. i9xx_crtc_clock_get(crtc, pipe_config);
  6844. return true;
  6845. }
  6846. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6847. {
  6848. struct drm_i915_private *dev_priv = dev->dev_private;
  6849. struct intel_encoder *encoder;
  6850. u32 val, final;
  6851. bool has_lvds = false;
  6852. bool has_cpu_edp = false;
  6853. bool has_panel = false;
  6854. bool has_ck505 = false;
  6855. bool can_ssc = false;
  6856. /* We need to take the global config into account */
  6857. for_each_intel_encoder(dev, encoder) {
  6858. switch (encoder->type) {
  6859. case INTEL_OUTPUT_LVDS:
  6860. has_panel = true;
  6861. has_lvds = true;
  6862. break;
  6863. case INTEL_OUTPUT_EDP:
  6864. has_panel = true;
  6865. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6866. has_cpu_edp = true;
  6867. break;
  6868. default:
  6869. break;
  6870. }
  6871. }
  6872. if (HAS_PCH_IBX(dev)) {
  6873. has_ck505 = dev_priv->vbt.display_clock_mode;
  6874. can_ssc = has_ck505;
  6875. } else {
  6876. has_ck505 = false;
  6877. can_ssc = true;
  6878. }
  6879. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6880. has_panel, has_lvds, has_ck505);
  6881. /* Ironlake: try to setup display ref clock before DPLL
  6882. * enabling. This is only under driver's control after
  6883. * PCH B stepping, previous chipset stepping should be
  6884. * ignoring this setting.
  6885. */
  6886. val = I915_READ(PCH_DREF_CONTROL);
  6887. /* As we must carefully and slowly disable/enable each source in turn,
  6888. * compute the final state we want first and check if we need to
  6889. * make any changes at all.
  6890. */
  6891. final = val;
  6892. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6893. if (has_ck505)
  6894. final |= DREF_NONSPREAD_CK505_ENABLE;
  6895. else
  6896. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6897. final &= ~DREF_SSC_SOURCE_MASK;
  6898. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6899. final &= ~DREF_SSC1_ENABLE;
  6900. if (has_panel) {
  6901. final |= DREF_SSC_SOURCE_ENABLE;
  6902. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6903. final |= DREF_SSC1_ENABLE;
  6904. if (has_cpu_edp) {
  6905. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6906. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6907. else
  6908. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6909. } else
  6910. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6911. } else {
  6912. final |= DREF_SSC_SOURCE_DISABLE;
  6913. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6914. }
  6915. if (final == val)
  6916. return;
  6917. /* Always enable nonspread source */
  6918. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6919. if (has_ck505)
  6920. val |= DREF_NONSPREAD_CK505_ENABLE;
  6921. else
  6922. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6923. if (has_panel) {
  6924. val &= ~DREF_SSC_SOURCE_MASK;
  6925. val |= DREF_SSC_SOURCE_ENABLE;
  6926. /* SSC must be turned on before enabling the CPU output */
  6927. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6928. DRM_DEBUG_KMS("Using SSC on panel\n");
  6929. val |= DREF_SSC1_ENABLE;
  6930. } else
  6931. val &= ~DREF_SSC1_ENABLE;
  6932. /* Get SSC going before enabling the outputs */
  6933. I915_WRITE(PCH_DREF_CONTROL, val);
  6934. POSTING_READ(PCH_DREF_CONTROL);
  6935. udelay(200);
  6936. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6937. /* Enable CPU source on CPU attached eDP */
  6938. if (has_cpu_edp) {
  6939. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6940. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6941. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6942. } else
  6943. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6944. } else
  6945. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6946. I915_WRITE(PCH_DREF_CONTROL, val);
  6947. POSTING_READ(PCH_DREF_CONTROL);
  6948. udelay(200);
  6949. } else {
  6950. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6951. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6952. /* Turn off CPU output */
  6953. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6954. I915_WRITE(PCH_DREF_CONTROL, val);
  6955. POSTING_READ(PCH_DREF_CONTROL);
  6956. udelay(200);
  6957. /* Turn off the SSC source */
  6958. val &= ~DREF_SSC_SOURCE_MASK;
  6959. val |= DREF_SSC_SOURCE_DISABLE;
  6960. /* Turn off SSC1 */
  6961. val &= ~DREF_SSC1_ENABLE;
  6962. I915_WRITE(PCH_DREF_CONTROL, val);
  6963. POSTING_READ(PCH_DREF_CONTROL);
  6964. udelay(200);
  6965. }
  6966. BUG_ON(val != final);
  6967. }
  6968. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6969. {
  6970. uint32_t tmp;
  6971. tmp = I915_READ(SOUTH_CHICKEN2);
  6972. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6973. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6974. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6975. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6976. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6977. tmp = I915_READ(SOUTH_CHICKEN2);
  6978. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6979. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6980. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6981. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6982. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6983. }
  6984. /* WaMPhyProgramming:hsw */
  6985. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6986. {
  6987. uint32_t tmp;
  6988. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6989. tmp &= ~(0xFF << 24);
  6990. tmp |= (0x12 << 24);
  6991. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6992. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6993. tmp |= (1 << 11);
  6994. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6995. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6996. tmp |= (1 << 11);
  6997. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6998. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6999. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7000. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7001. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7002. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7003. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7004. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7005. tmp &= ~(7 << 13);
  7006. tmp |= (5 << 13);
  7007. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7008. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7009. tmp &= ~(7 << 13);
  7010. tmp |= (5 << 13);
  7011. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7012. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7013. tmp &= ~0xFF;
  7014. tmp |= 0x1C;
  7015. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7016. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7017. tmp &= ~0xFF;
  7018. tmp |= 0x1C;
  7019. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7020. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7021. tmp &= ~(0xFF << 16);
  7022. tmp |= (0x1C << 16);
  7023. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7024. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7025. tmp &= ~(0xFF << 16);
  7026. tmp |= (0x1C << 16);
  7027. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7028. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7029. tmp |= (1 << 27);
  7030. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7031. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7032. tmp |= (1 << 27);
  7033. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7034. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7035. tmp &= ~(0xF << 28);
  7036. tmp |= (4 << 28);
  7037. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7038. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7039. tmp &= ~(0xF << 28);
  7040. tmp |= (4 << 28);
  7041. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7042. }
  7043. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7044. * Programming" based on the parameters passed:
  7045. * - Sequence to enable CLKOUT_DP
  7046. * - Sequence to enable CLKOUT_DP without spread
  7047. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7048. */
  7049. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7050. bool with_fdi)
  7051. {
  7052. struct drm_i915_private *dev_priv = dev->dev_private;
  7053. uint32_t reg, tmp;
  7054. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7055. with_spread = true;
  7056. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7057. with_fdi, "LP PCH doesn't have FDI\n"))
  7058. with_fdi = false;
  7059. mutex_lock(&dev_priv->sb_lock);
  7060. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7061. tmp &= ~SBI_SSCCTL_DISABLE;
  7062. tmp |= SBI_SSCCTL_PATHALT;
  7063. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7064. udelay(24);
  7065. if (with_spread) {
  7066. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7067. tmp &= ~SBI_SSCCTL_PATHALT;
  7068. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7069. if (with_fdi) {
  7070. lpt_reset_fdi_mphy(dev_priv);
  7071. lpt_program_fdi_mphy(dev_priv);
  7072. }
  7073. }
  7074. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7075. SBI_GEN0 : SBI_DBUFF0;
  7076. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7077. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7078. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7079. mutex_unlock(&dev_priv->sb_lock);
  7080. }
  7081. /* Sequence to disable CLKOUT_DP */
  7082. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7083. {
  7084. struct drm_i915_private *dev_priv = dev->dev_private;
  7085. uint32_t reg, tmp;
  7086. mutex_lock(&dev_priv->sb_lock);
  7087. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7088. SBI_GEN0 : SBI_DBUFF0;
  7089. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7090. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7091. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7092. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7093. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7094. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7095. tmp |= SBI_SSCCTL_PATHALT;
  7096. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7097. udelay(32);
  7098. }
  7099. tmp |= SBI_SSCCTL_DISABLE;
  7100. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7101. }
  7102. mutex_unlock(&dev_priv->sb_lock);
  7103. }
  7104. static void lpt_init_pch_refclk(struct drm_device *dev)
  7105. {
  7106. struct intel_encoder *encoder;
  7107. bool has_vga = false;
  7108. for_each_intel_encoder(dev, encoder) {
  7109. switch (encoder->type) {
  7110. case INTEL_OUTPUT_ANALOG:
  7111. has_vga = true;
  7112. break;
  7113. default:
  7114. break;
  7115. }
  7116. }
  7117. if (has_vga)
  7118. lpt_enable_clkout_dp(dev, true, true);
  7119. else
  7120. lpt_disable_clkout_dp(dev);
  7121. }
  7122. /*
  7123. * Initialize reference clocks when the driver loads
  7124. */
  7125. void intel_init_pch_refclk(struct drm_device *dev)
  7126. {
  7127. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7128. ironlake_init_pch_refclk(dev);
  7129. else if (HAS_PCH_LPT(dev))
  7130. lpt_init_pch_refclk(dev);
  7131. }
  7132. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7133. {
  7134. struct drm_device *dev = crtc_state->base.crtc->dev;
  7135. struct drm_i915_private *dev_priv = dev->dev_private;
  7136. struct drm_atomic_state *state = crtc_state->base.state;
  7137. struct drm_connector *connector;
  7138. struct drm_connector_state *connector_state;
  7139. struct intel_encoder *encoder;
  7140. int num_connectors = 0, i;
  7141. bool is_lvds = false;
  7142. for_each_connector_in_state(state, connector, connector_state, i) {
  7143. if (connector_state->crtc != crtc_state->base.crtc)
  7144. continue;
  7145. encoder = to_intel_encoder(connector_state->best_encoder);
  7146. switch (encoder->type) {
  7147. case INTEL_OUTPUT_LVDS:
  7148. is_lvds = true;
  7149. break;
  7150. default:
  7151. break;
  7152. }
  7153. num_connectors++;
  7154. }
  7155. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7156. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7157. dev_priv->vbt.lvds_ssc_freq);
  7158. return dev_priv->vbt.lvds_ssc_freq;
  7159. }
  7160. return 120000;
  7161. }
  7162. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7163. {
  7164. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7165. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7166. int pipe = intel_crtc->pipe;
  7167. uint32_t val;
  7168. val = 0;
  7169. switch (intel_crtc->config->pipe_bpp) {
  7170. case 18:
  7171. val |= PIPECONF_6BPC;
  7172. break;
  7173. case 24:
  7174. val |= PIPECONF_8BPC;
  7175. break;
  7176. case 30:
  7177. val |= PIPECONF_10BPC;
  7178. break;
  7179. case 36:
  7180. val |= PIPECONF_12BPC;
  7181. break;
  7182. default:
  7183. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7184. BUG();
  7185. }
  7186. if (intel_crtc->config->dither)
  7187. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7188. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7189. val |= PIPECONF_INTERLACED_ILK;
  7190. else
  7191. val |= PIPECONF_PROGRESSIVE;
  7192. if (intel_crtc->config->limited_color_range)
  7193. val |= PIPECONF_COLOR_RANGE_SELECT;
  7194. I915_WRITE(PIPECONF(pipe), val);
  7195. POSTING_READ(PIPECONF(pipe));
  7196. }
  7197. /*
  7198. * Set up the pipe CSC unit.
  7199. *
  7200. * Currently only full range RGB to limited range RGB conversion
  7201. * is supported, but eventually this should handle various
  7202. * RGB<->YCbCr scenarios as well.
  7203. */
  7204. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7205. {
  7206. struct drm_device *dev = crtc->dev;
  7207. struct drm_i915_private *dev_priv = dev->dev_private;
  7208. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7209. int pipe = intel_crtc->pipe;
  7210. uint16_t coeff = 0x7800; /* 1.0 */
  7211. /*
  7212. * TODO: Check what kind of values actually come out of the pipe
  7213. * with these coeff/postoff values and adjust to get the best
  7214. * accuracy. Perhaps we even need to take the bpc value into
  7215. * consideration.
  7216. */
  7217. if (intel_crtc->config->limited_color_range)
  7218. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7219. /*
  7220. * GY/GU and RY/RU should be the other way around according
  7221. * to BSpec, but reality doesn't agree. Just set them up in
  7222. * a way that results in the correct picture.
  7223. */
  7224. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7225. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7226. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7227. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7228. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7229. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7230. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7231. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7232. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7233. if (INTEL_INFO(dev)->gen > 6) {
  7234. uint16_t postoff = 0;
  7235. if (intel_crtc->config->limited_color_range)
  7236. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7237. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7238. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7239. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7240. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7241. } else {
  7242. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7243. if (intel_crtc->config->limited_color_range)
  7244. mode |= CSC_BLACK_SCREEN_OFFSET;
  7245. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7246. }
  7247. }
  7248. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7249. {
  7250. struct drm_device *dev = crtc->dev;
  7251. struct drm_i915_private *dev_priv = dev->dev_private;
  7252. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7253. enum pipe pipe = intel_crtc->pipe;
  7254. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7255. uint32_t val;
  7256. val = 0;
  7257. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7258. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7259. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7260. val |= PIPECONF_INTERLACED_ILK;
  7261. else
  7262. val |= PIPECONF_PROGRESSIVE;
  7263. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7264. POSTING_READ(PIPECONF(cpu_transcoder));
  7265. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7266. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7267. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7268. val = 0;
  7269. switch (intel_crtc->config->pipe_bpp) {
  7270. case 18:
  7271. val |= PIPEMISC_DITHER_6_BPC;
  7272. break;
  7273. case 24:
  7274. val |= PIPEMISC_DITHER_8_BPC;
  7275. break;
  7276. case 30:
  7277. val |= PIPEMISC_DITHER_10_BPC;
  7278. break;
  7279. case 36:
  7280. val |= PIPEMISC_DITHER_12_BPC;
  7281. break;
  7282. default:
  7283. /* Case prevented by pipe_config_set_bpp. */
  7284. BUG();
  7285. }
  7286. if (intel_crtc->config->dither)
  7287. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7288. I915_WRITE(PIPEMISC(pipe), val);
  7289. }
  7290. }
  7291. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7292. struct intel_crtc_state *crtc_state,
  7293. intel_clock_t *clock,
  7294. bool *has_reduced_clock,
  7295. intel_clock_t *reduced_clock)
  7296. {
  7297. struct drm_device *dev = crtc->dev;
  7298. struct drm_i915_private *dev_priv = dev->dev_private;
  7299. int refclk;
  7300. const intel_limit_t *limit;
  7301. bool ret;
  7302. refclk = ironlake_get_refclk(crtc_state);
  7303. /*
  7304. * Returns a set of divisors for the desired target clock with the given
  7305. * refclk, or FALSE. The returned values represent the clock equation:
  7306. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7307. */
  7308. limit = intel_limit(crtc_state, refclk);
  7309. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7310. crtc_state->port_clock,
  7311. refclk, NULL, clock);
  7312. if (!ret)
  7313. return false;
  7314. return true;
  7315. }
  7316. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7317. {
  7318. /*
  7319. * Account for spread spectrum to avoid
  7320. * oversubscribing the link. Max center spread
  7321. * is 2.5%; use 5% for safety's sake.
  7322. */
  7323. u32 bps = target_clock * bpp * 21 / 20;
  7324. return DIV_ROUND_UP(bps, link_bw * 8);
  7325. }
  7326. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7327. {
  7328. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7329. }
  7330. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7331. struct intel_crtc_state *crtc_state,
  7332. u32 *fp,
  7333. intel_clock_t *reduced_clock, u32 *fp2)
  7334. {
  7335. struct drm_crtc *crtc = &intel_crtc->base;
  7336. struct drm_device *dev = crtc->dev;
  7337. struct drm_i915_private *dev_priv = dev->dev_private;
  7338. struct drm_atomic_state *state = crtc_state->base.state;
  7339. struct drm_connector *connector;
  7340. struct drm_connector_state *connector_state;
  7341. struct intel_encoder *encoder;
  7342. uint32_t dpll;
  7343. int factor, num_connectors = 0, i;
  7344. bool is_lvds = false, is_sdvo = false;
  7345. for_each_connector_in_state(state, connector, connector_state, i) {
  7346. if (connector_state->crtc != crtc_state->base.crtc)
  7347. continue;
  7348. encoder = to_intel_encoder(connector_state->best_encoder);
  7349. switch (encoder->type) {
  7350. case INTEL_OUTPUT_LVDS:
  7351. is_lvds = true;
  7352. break;
  7353. case INTEL_OUTPUT_SDVO:
  7354. case INTEL_OUTPUT_HDMI:
  7355. is_sdvo = true;
  7356. break;
  7357. default:
  7358. break;
  7359. }
  7360. num_connectors++;
  7361. }
  7362. /* Enable autotuning of the PLL clock (if permissible) */
  7363. factor = 21;
  7364. if (is_lvds) {
  7365. if ((intel_panel_use_ssc(dev_priv) &&
  7366. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7367. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7368. factor = 25;
  7369. } else if (crtc_state->sdvo_tv_clock)
  7370. factor = 20;
  7371. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7372. *fp |= FP_CB_TUNE;
  7373. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7374. *fp2 |= FP_CB_TUNE;
  7375. dpll = 0;
  7376. if (is_lvds)
  7377. dpll |= DPLLB_MODE_LVDS;
  7378. else
  7379. dpll |= DPLLB_MODE_DAC_SERIAL;
  7380. dpll |= (crtc_state->pixel_multiplier - 1)
  7381. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7382. if (is_sdvo)
  7383. dpll |= DPLL_SDVO_HIGH_SPEED;
  7384. if (crtc_state->has_dp_encoder)
  7385. dpll |= DPLL_SDVO_HIGH_SPEED;
  7386. /* compute bitmask from p1 value */
  7387. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7388. /* also FPA1 */
  7389. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7390. switch (crtc_state->dpll.p2) {
  7391. case 5:
  7392. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7393. break;
  7394. case 7:
  7395. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7396. break;
  7397. case 10:
  7398. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7399. break;
  7400. case 14:
  7401. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7402. break;
  7403. }
  7404. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7405. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7406. else
  7407. dpll |= PLL_REF_INPUT_DREFCLK;
  7408. return dpll | DPLL_VCO_ENABLE;
  7409. }
  7410. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7411. struct intel_crtc_state *crtc_state)
  7412. {
  7413. struct drm_device *dev = crtc->base.dev;
  7414. intel_clock_t clock, reduced_clock;
  7415. u32 dpll = 0, fp = 0, fp2 = 0;
  7416. bool ok, has_reduced_clock = false;
  7417. bool is_lvds = false;
  7418. struct intel_shared_dpll *pll;
  7419. memset(&crtc_state->dpll_hw_state, 0,
  7420. sizeof(crtc_state->dpll_hw_state));
  7421. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7422. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7423. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7424. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7425. &has_reduced_clock, &reduced_clock);
  7426. if (!ok && !crtc_state->clock_set) {
  7427. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7428. return -EINVAL;
  7429. }
  7430. /* Compat-code for transition, will disappear. */
  7431. if (!crtc_state->clock_set) {
  7432. crtc_state->dpll.n = clock.n;
  7433. crtc_state->dpll.m1 = clock.m1;
  7434. crtc_state->dpll.m2 = clock.m2;
  7435. crtc_state->dpll.p1 = clock.p1;
  7436. crtc_state->dpll.p2 = clock.p2;
  7437. }
  7438. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7439. if (crtc_state->has_pch_encoder) {
  7440. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7441. if (has_reduced_clock)
  7442. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7443. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7444. &fp, &reduced_clock,
  7445. has_reduced_clock ? &fp2 : NULL);
  7446. crtc_state->dpll_hw_state.dpll = dpll;
  7447. crtc_state->dpll_hw_state.fp0 = fp;
  7448. if (has_reduced_clock)
  7449. crtc_state->dpll_hw_state.fp1 = fp2;
  7450. else
  7451. crtc_state->dpll_hw_state.fp1 = fp;
  7452. pll = intel_get_shared_dpll(crtc, crtc_state);
  7453. if (pll == NULL) {
  7454. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7455. pipe_name(crtc->pipe));
  7456. return -EINVAL;
  7457. }
  7458. }
  7459. if (is_lvds && has_reduced_clock)
  7460. crtc->lowfreq_avail = true;
  7461. else
  7462. crtc->lowfreq_avail = false;
  7463. return 0;
  7464. }
  7465. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7466. struct intel_link_m_n *m_n)
  7467. {
  7468. struct drm_device *dev = crtc->base.dev;
  7469. struct drm_i915_private *dev_priv = dev->dev_private;
  7470. enum pipe pipe = crtc->pipe;
  7471. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7472. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7473. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7474. & ~TU_SIZE_MASK;
  7475. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7476. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7477. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7478. }
  7479. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7480. enum transcoder transcoder,
  7481. struct intel_link_m_n *m_n,
  7482. struct intel_link_m_n *m2_n2)
  7483. {
  7484. struct drm_device *dev = crtc->base.dev;
  7485. struct drm_i915_private *dev_priv = dev->dev_private;
  7486. enum pipe pipe = crtc->pipe;
  7487. if (INTEL_INFO(dev)->gen >= 5) {
  7488. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7489. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7490. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7491. & ~TU_SIZE_MASK;
  7492. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7493. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7494. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7495. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7496. * gen < 8) and if DRRS is supported (to make sure the
  7497. * registers are not unnecessarily read).
  7498. */
  7499. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7500. crtc->config->has_drrs) {
  7501. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7502. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7503. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7504. & ~TU_SIZE_MASK;
  7505. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7506. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7507. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7508. }
  7509. } else {
  7510. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7511. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7512. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7513. & ~TU_SIZE_MASK;
  7514. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7515. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7516. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7517. }
  7518. }
  7519. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7520. struct intel_crtc_state *pipe_config)
  7521. {
  7522. if (pipe_config->has_pch_encoder)
  7523. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7524. else
  7525. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7526. &pipe_config->dp_m_n,
  7527. &pipe_config->dp_m2_n2);
  7528. }
  7529. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7530. struct intel_crtc_state *pipe_config)
  7531. {
  7532. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7533. &pipe_config->fdi_m_n, NULL);
  7534. }
  7535. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7536. struct intel_crtc_state *pipe_config)
  7537. {
  7538. struct drm_device *dev = crtc->base.dev;
  7539. struct drm_i915_private *dev_priv = dev->dev_private;
  7540. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7541. uint32_t ps_ctrl = 0;
  7542. int id = -1;
  7543. int i;
  7544. /* find scaler attached to this pipe */
  7545. for (i = 0; i < crtc->num_scalers; i++) {
  7546. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7547. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7548. id = i;
  7549. pipe_config->pch_pfit.enabled = true;
  7550. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7551. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7552. break;
  7553. }
  7554. }
  7555. scaler_state->scaler_id = id;
  7556. if (id >= 0) {
  7557. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7558. } else {
  7559. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7560. }
  7561. }
  7562. static void
  7563. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7564. struct intel_initial_plane_config *plane_config)
  7565. {
  7566. struct drm_device *dev = crtc->base.dev;
  7567. struct drm_i915_private *dev_priv = dev->dev_private;
  7568. u32 val, base, offset, stride_mult, tiling;
  7569. int pipe = crtc->pipe;
  7570. int fourcc, pixel_format;
  7571. unsigned int aligned_height;
  7572. struct drm_framebuffer *fb;
  7573. struct intel_framebuffer *intel_fb;
  7574. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7575. if (!intel_fb) {
  7576. DRM_DEBUG_KMS("failed to alloc fb\n");
  7577. return;
  7578. }
  7579. fb = &intel_fb->base;
  7580. val = I915_READ(PLANE_CTL(pipe, 0));
  7581. if (!(val & PLANE_CTL_ENABLE))
  7582. goto error;
  7583. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7584. fourcc = skl_format_to_fourcc(pixel_format,
  7585. val & PLANE_CTL_ORDER_RGBX,
  7586. val & PLANE_CTL_ALPHA_MASK);
  7587. fb->pixel_format = fourcc;
  7588. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7589. tiling = val & PLANE_CTL_TILED_MASK;
  7590. switch (tiling) {
  7591. case PLANE_CTL_TILED_LINEAR:
  7592. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7593. break;
  7594. case PLANE_CTL_TILED_X:
  7595. plane_config->tiling = I915_TILING_X;
  7596. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7597. break;
  7598. case PLANE_CTL_TILED_Y:
  7599. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7600. break;
  7601. case PLANE_CTL_TILED_YF:
  7602. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7603. break;
  7604. default:
  7605. MISSING_CASE(tiling);
  7606. goto error;
  7607. }
  7608. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7609. plane_config->base = base;
  7610. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7611. val = I915_READ(PLANE_SIZE(pipe, 0));
  7612. fb->height = ((val >> 16) & 0xfff) + 1;
  7613. fb->width = ((val >> 0) & 0x1fff) + 1;
  7614. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7615. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7616. fb->pixel_format);
  7617. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7618. aligned_height = intel_fb_align_height(dev, fb->height,
  7619. fb->pixel_format,
  7620. fb->modifier[0]);
  7621. plane_config->size = fb->pitches[0] * aligned_height;
  7622. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7623. pipe_name(pipe), fb->width, fb->height,
  7624. fb->bits_per_pixel, base, fb->pitches[0],
  7625. plane_config->size);
  7626. plane_config->fb = intel_fb;
  7627. return;
  7628. error:
  7629. kfree(fb);
  7630. }
  7631. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7632. struct intel_crtc_state *pipe_config)
  7633. {
  7634. struct drm_device *dev = crtc->base.dev;
  7635. struct drm_i915_private *dev_priv = dev->dev_private;
  7636. uint32_t tmp;
  7637. tmp = I915_READ(PF_CTL(crtc->pipe));
  7638. if (tmp & PF_ENABLE) {
  7639. pipe_config->pch_pfit.enabled = true;
  7640. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7641. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7642. /* We currently do not free assignements of panel fitters on
  7643. * ivb/hsw (since we don't use the higher upscaling modes which
  7644. * differentiates them) so just WARN about this case for now. */
  7645. if (IS_GEN7(dev)) {
  7646. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7647. PF_PIPE_SEL_IVB(crtc->pipe));
  7648. }
  7649. }
  7650. }
  7651. static void
  7652. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7653. struct intel_initial_plane_config *plane_config)
  7654. {
  7655. struct drm_device *dev = crtc->base.dev;
  7656. struct drm_i915_private *dev_priv = dev->dev_private;
  7657. u32 val, base, offset;
  7658. int pipe = crtc->pipe;
  7659. int fourcc, pixel_format;
  7660. unsigned int aligned_height;
  7661. struct drm_framebuffer *fb;
  7662. struct intel_framebuffer *intel_fb;
  7663. val = I915_READ(DSPCNTR(pipe));
  7664. if (!(val & DISPLAY_PLANE_ENABLE))
  7665. return;
  7666. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7667. if (!intel_fb) {
  7668. DRM_DEBUG_KMS("failed to alloc fb\n");
  7669. return;
  7670. }
  7671. fb = &intel_fb->base;
  7672. if (INTEL_INFO(dev)->gen >= 4) {
  7673. if (val & DISPPLANE_TILED) {
  7674. plane_config->tiling = I915_TILING_X;
  7675. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7676. }
  7677. }
  7678. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7679. fourcc = i9xx_format_to_fourcc(pixel_format);
  7680. fb->pixel_format = fourcc;
  7681. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7682. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7683. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7684. offset = I915_READ(DSPOFFSET(pipe));
  7685. } else {
  7686. if (plane_config->tiling)
  7687. offset = I915_READ(DSPTILEOFF(pipe));
  7688. else
  7689. offset = I915_READ(DSPLINOFF(pipe));
  7690. }
  7691. plane_config->base = base;
  7692. val = I915_READ(PIPESRC(pipe));
  7693. fb->width = ((val >> 16) & 0xfff) + 1;
  7694. fb->height = ((val >> 0) & 0xfff) + 1;
  7695. val = I915_READ(DSPSTRIDE(pipe));
  7696. fb->pitches[0] = val & 0xffffffc0;
  7697. aligned_height = intel_fb_align_height(dev, fb->height,
  7698. fb->pixel_format,
  7699. fb->modifier[0]);
  7700. plane_config->size = fb->pitches[0] * aligned_height;
  7701. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7702. pipe_name(pipe), fb->width, fb->height,
  7703. fb->bits_per_pixel, base, fb->pitches[0],
  7704. plane_config->size);
  7705. plane_config->fb = intel_fb;
  7706. }
  7707. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7708. struct intel_crtc_state *pipe_config)
  7709. {
  7710. struct drm_device *dev = crtc->base.dev;
  7711. struct drm_i915_private *dev_priv = dev->dev_private;
  7712. uint32_t tmp;
  7713. if (!intel_display_power_is_enabled(dev_priv,
  7714. POWER_DOMAIN_PIPE(crtc->pipe)))
  7715. return false;
  7716. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7717. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7718. tmp = I915_READ(PIPECONF(crtc->pipe));
  7719. if (!(tmp & PIPECONF_ENABLE))
  7720. return false;
  7721. switch (tmp & PIPECONF_BPC_MASK) {
  7722. case PIPECONF_6BPC:
  7723. pipe_config->pipe_bpp = 18;
  7724. break;
  7725. case PIPECONF_8BPC:
  7726. pipe_config->pipe_bpp = 24;
  7727. break;
  7728. case PIPECONF_10BPC:
  7729. pipe_config->pipe_bpp = 30;
  7730. break;
  7731. case PIPECONF_12BPC:
  7732. pipe_config->pipe_bpp = 36;
  7733. break;
  7734. default:
  7735. break;
  7736. }
  7737. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7738. pipe_config->limited_color_range = true;
  7739. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7740. struct intel_shared_dpll *pll;
  7741. pipe_config->has_pch_encoder = true;
  7742. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7743. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7744. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7745. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7746. if (HAS_PCH_IBX(dev_priv->dev)) {
  7747. pipe_config->shared_dpll =
  7748. (enum intel_dpll_id) crtc->pipe;
  7749. } else {
  7750. tmp = I915_READ(PCH_DPLL_SEL);
  7751. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7752. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7753. else
  7754. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7755. }
  7756. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7757. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7758. &pipe_config->dpll_hw_state));
  7759. tmp = pipe_config->dpll_hw_state.dpll;
  7760. pipe_config->pixel_multiplier =
  7761. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7762. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7763. ironlake_pch_clock_get(crtc, pipe_config);
  7764. } else {
  7765. pipe_config->pixel_multiplier = 1;
  7766. }
  7767. intel_get_pipe_timings(crtc, pipe_config);
  7768. ironlake_get_pfit_config(crtc, pipe_config);
  7769. return true;
  7770. }
  7771. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7772. {
  7773. struct drm_device *dev = dev_priv->dev;
  7774. struct intel_crtc *crtc;
  7775. for_each_intel_crtc(dev, crtc)
  7776. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7777. pipe_name(crtc->pipe));
  7778. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7779. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7780. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7781. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7782. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7783. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7784. "CPU PWM1 enabled\n");
  7785. if (IS_HASWELL(dev))
  7786. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7787. "CPU PWM2 enabled\n");
  7788. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7789. "PCH PWM1 enabled\n");
  7790. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7791. "Utility pin enabled\n");
  7792. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7793. /*
  7794. * In theory we can still leave IRQs enabled, as long as only the HPD
  7795. * interrupts remain enabled. We used to check for that, but since it's
  7796. * gen-specific and since we only disable LCPLL after we fully disable
  7797. * the interrupts, the check below should be enough.
  7798. */
  7799. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7800. }
  7801. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7802. {
  7803. struct drm_device *dev = dev_priv->dev;
  7804. if (IS_HASWELL(dev))
  7805. return I915_READ(D_COMP_HSW);
  7806. else
  7807. return I915_READ(D_COMP_BDW);
  7808. }
  7809. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7810. {
  7811. struct drm_device *dev = dev_priv->dev;
  7812. if (IS_HASWELL(dev)) {
  7813. mutex_lock(&dev_priv->rps.hw_lock);
  7814. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7815. val))
  7816. DRM_ERROR("Failed to write to D_COMP\n");
  7817. mutex_unlock(&dev_priv->rps.hw_lock);
  7818. } else {
  7819. I915_WRITE(D_COMP_BDW, val);
  7820. POSTING_READ(D_COMP_BDW);
  7821. }
  7822. }
  7823. /*
  7824. * This function implements pieces of two sequences from BSpec:
  7825. * - Sequence for display software to disable LCPLL
  7826. * - Sequence for display software to allow package C8+
  7827. * The steps implemented here are just the steps that actually touch the LCPLL
  7828. * register. Callers should take care of disabling all the display engine
  7829. * functions, doing the mode unset, fixing interrupts, etc.
  7830. */
  7831. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7832. bool switch_to_fclk, bool allow_power_down)
  7833. {
  7834. uint32_t val;
  7835. assert_can_disable_lcpll(dev_priv);
  7836. val = I915_READ(LCPLL_CTL);
  7837. if (switch_to_fclk) {
  7838. val |= LCPLL_CD_SOURCE_FCLK;
  7839. I915_WRITE(LCPLL_CTL, val);
  7840. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7841. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7842. DRM_ERROR("Switching to FCLK failed\n");
  7843. val = I915_READ(LCPLL_CTL);
  7844. }
  7845. val |= LCPLL_PLL_DISABLE;
  7846. I915_WRITE(LCPLL_CTL, val);
  7847. POSTING_READ(LCPLL_CTL);
  7848. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7849. DRM_ERROR("LCPLL still locked\n");
  7850. val = hsw_read_dcomp(dev_priv);
  7851. val |= D_COMP_COMP_DISABLE;
  7852. hsw_write_dcomp(dev_priv, val);
  7853. ndelay(100);
  7854. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7855. 1))
  7856. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7857. if (allow_power_down) {
  7858. val = I915_READ(LCPLL_CTL);
  7859. val |= LCPLL_POWER_DOWN_ALLOW;
  7860. I915_WRITE(LCPLL_CTL, val);
  7861. POSTING_READ(LCPLL_CTL);
  7862. }
  7863. }
  7864. /*
  7865. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7866. * source.
  7867. */
  7868. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7869. {
  7870. uint32_t val;
  7871. val = I915_READ(LCPLL_CTL);
  7872. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7873. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7874. return;
  7875. /*
  7876. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7877. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7878. */
  7879. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7880. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7881. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7882. I915_WRITE(LCPLL_CTL, val);
  7883. POSTING_READ(LCPLL_CTL);
  7884. }
  7885. val = hsw_read_dcomp(dev_priv);
  7886. val |= D_COMP_COMP_FORCE;
  7887. val &= ~D_COMP_COMP_DISABLE;
  7888. hsw_write_dcomp(dev_priv, val);
  7889. val = I915_READ(LCPLL_CTL);
  7890. val &= ~LCPLL_PLL_DISABLE;
  7891. I915_WRITE(LCPLL_CTL, val);
  7892. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7893. DRM_ERROR("LCPLL not locked yet\n");
  7894. if (val & LCPLL_CD_SOURCE_FCLK) {
  7895. val = I915_READ(LCPLL_CTL);
  7896. val &= ~LCPLL_CD_SOURCE_FCLK;
  7897. I915_WRITE(LCPLL_CTL, val);
  7898. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7899. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7900. DRM_ERROR("Switching back to LCPLL failed\n");
  7901. }
  7902. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7903. intel_update_cdclk(dev_priv->dev);
  7904. }
  7905. /*
  7906. * Package states C8 and deeper are really deep PC states that can only be
  7907. * reached when all the devices on the system allow it, so even if the graphics
  7908. * device allows PC8+, it doesn't mean the system will actually get to these
  7909. * states. Our driver only allows PC8+ when going into runtime PM.
  7910. *
  7911. * The requirements for PC8+ are that all the outputs are disabled, the power
  7912. * well is disabled and most interrupts are disabled, and these are also
  7913. * requirements for runtime PM. When these conditions are met, we manually do
  7914. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7915. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7916. * hang the machine.
  7917. *
  7918. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7919. * the state of some registers, so when we come back from PC8+ we need to
  7920. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7921. * need to take care of the registers kept by RC6. Notice that this happens even
  7922. * if we don't put the device in PCI D3 state (which is what currently happens
  7923. * because of the runtime PM support).
  7924. *
  7925. * For more, read "Display Sequences for Package C8" on the hardware
  7926. * documentation.
  7927. */
  7928. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7929. {
  7930. struct drm_device *dev = dev_priv->dev;
  7931. uint32_t val;
  7932. DRM_DEBUG_KMS("Enabling package C8+\n");
  7933. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7934. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7935. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7936. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7937. }
  7938. lpt_disable_clkout_dp(dev);
  7939. hsw_disable_lcpll(dev_priv, true, true);
  7940. }
  7941. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7942. {
  7943. struct drm_device *dev = dev_priv->dev;
  7944. uint32_t val;
  7945. DRM_DEBUG_KMS("Disabling package C8+\n");
  7946. hsw_restore_lcpll(dev_priv);
  7947. lpt_init_pch_refclk(dev);
  7948. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7949. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7950. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7951. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7952. }
  7953. intel_prepare_ddi(dev);
  7954. }
  7955. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7956. {
  7957. struct drm_device *dev = old_state->dev;
  7958. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  7959. broxton_set_cdclk(dev, req_cdclk);
  7960. }
  7961. /* compute the max rate for new configuration */
  7962. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  7963. {
  7964. struct intel_crtc *intel_crtc;
  7965. struct intel_crtc_state *crtc_state;
  7966. int max_pixel_rate = 0;
  7967. for_each_intel_crtc(state->dev, intel_crtc) {
  7968. int pixel_rate;
  7969. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  7970. if (IS_ERR(crtc_state))
  7971. return PTR_ERR(crtc_state);
  7972. if (!crtc_state->base.enable)
  7973. continue;
  7974. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  7975. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  7976. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  7977. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  7978. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  7979. }
  7980. return max_pixel_rate;
  7981. }
  7982. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  7983. {
  7984. struct drm_i915_private *dev_priv = dev->dev_private;
  7985. uint32_t val, data;
  7986. int ret;
  7987. if (WARN((I915_READ(LCPLL_CTL) &
  7988. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  7989. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  7990. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  7991. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  7992. "trying to change cdclk frequency with cdclk not enabled\n"))
  7993. return;
  7994. mutex_lock(&dev_priv->rps.hw_lock);
  7995. ret = sandybridge_pcode_write(dev_priv,
  7996. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  7997. mutex_unlock(&dev_priv->rps.hw_lock);
  7998. if (ret) {
  7999. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8000. return;
  8001. }
  8002. val = I915_READ(LCPLL_CTL);
  8003. val |= LCPLL_CD_SOURCE_FCLK;
  8004. I915_WRITE(LCPLL_CTL, val);
  8005. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8006. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8007. DRM_ERROR("Switching to FCLK failed\n");
  8008. val = I915_READ(LCPLL_CTL);
  8009. val &= ~LCPLL_CLK_FREQ_MASK;
  8010. switch (cdclk) {
  8011. case 450000:
  8012. val |= LCPLL_CLK_FREQ_450;
  8013. data = 0;
  8014. break;
  8015. case 540000:
  8016. val |= LCPLL_CLK_FREQ_54O_BDW;
  8017. data = 1;
  8018. break;
  8019. case 337500:
  8020. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8021. data = 2;
  8022. break;
  8023. case 675000:
  8024. val |= LCPLL_CLK_FREQ_675_BDW;
  8025. data = 3;
  8026. break;
  8027. default:
  8028. WARN(1, "invalid cdclk frequency\n");
  8029. return;
  8030. }
  8031. I915_WRITE(LCPLL_CTL, val);
  8032. val = I915_READ(LCPLL_CTL);
  8033. val &= ~LCPLL_CD_SOURCE_FCLK;
  8034. I915_WRITE(LCPLL_CTL, val);
  8035. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8036. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8037. DRM_ERROR("Switching back to LCPLL failed\n");
  8038. mutex_lock(&dev_priv->rps.hw_lock);
  8039. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8040. mutex_unlock(&dev_priv->rps.hw_lock);
  8041. intel_update_cdclk(dev);
  8042. WARN(cdclk != dev_priv->cdclk_freq,
  8043. "cdclk requested %d kHz but got %d kHz\n",
  8044. cdclk, dev_priv->cdclk_freq);
  8045. }
  8046. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8047. {
  8048. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8049. int max_pixclk = ilk_max_pixel_rate(state);
  8050. int cdclk;
  8051. /*
  8052. * FIXME should also account for plane ratio
  8053. * once 64bpp pixel formats are supported.
  8054. */
  8055. if (max_pixclk > 540000)
  8056. cdclk = 675000;
  8057. else if (max_pixclk > 450000)
  8058. cdclk = 540000;
  8059. else if (max_pixclk > 337500)
  8060. cdclk = 450000;
  8061. else
  8062. cdclk = 337500;
  8063. /*
  8064. * FIXME move the cdclk caclulation to
  8065. * compute_config() so we can fail gracegully.
  8066. */
  8067. if (cdclk > dev_priv->max_cdclk_freq) {
  8068. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8069. cdclk, dev_priv->max_cdclk_freq);
  8070. cdclk = dev_priv->max_cdclk_freq;
  8071. }
  8072. to_intel_atomic_state(state)->cdclk = cdclk;
  8073. return 0;
  8074. }
  8075. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8076. {
  8077. struct drm_device *dev = old_state->dev;
  8078. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8079. broadwell_set_cdclk(dev, req_cdclk);
  8080. }
  8081. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8082. struct intel_crtc_state *crtc_state)
  8083. {
  8084. if (!intel_ddi_pll_select(crtc, crtc_state))
  8085. return -EINVAL;
  8086. crtc->lowfreq_avail = false;
  8087. return 0;
  8088. }
  8089. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8090. enum port port,
  8091. struct intel_crtc_state *pipe_config)
  8092. {
  8093. switch (port) {
  8094. case PORT_A:
  8095. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8096. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8097. break;
  8098. case PORT_B:
  8099. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8100. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8101. break;
  8102. case PORT_C:
  8103. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8104. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8105. break;
  8106. default:
  8107. DRM_ERROR("Incorrect port type\n");
  8108. }
  8109. }
  8110. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8111. enum port port,
  8112. struct intel_crtc_state *pipe_config)
  8113. {
  8114. u32 temp, dpll_ctl1;
  8115. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8116. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8117. switch (pipe_config->ddi_pll_sel) {
  8118. case SKL_DPLL0:
  8119. /*
  8120. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8121. * of the shared DPLL framework and thus needs to be read out
  8122. * separately
  8123. */
  8124. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8125. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8126. break;
  8127. case SKL_DPLL1:
  8128. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8129. break;
  8130. case SKL_DPLL2:
  8131. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8132. break;
  8133. case SKL_DPLL3:
  8134. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8135. break;
  8136. }
  8137. }
  8138. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8139. enum port port,
  8140. struct intel_crtc_state *pipe_config)
  8141. {
  8142. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8143. switch (pipe_config->ddi_pll_sel) {
  8144. case PORT_CLK_SEL_WRPLL1:
  8145. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8146. break;
  8147. case PORT_CLK_SEL_WRPLL2:
  8148. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8149. break;
  8150. }
  8151. }
  8152. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8153. struct intel_crtc_state *pipe_config)
  8154. {
  8155. struct drm_device *dev = crtc->base.dev;
  8156. struct drm_i915_private *dev_priv = dev->dev_private;
  8157. struct intel_shared_dpll *pll;
  8158. enum port port;
  8159. uint32_t tmp;
  8160. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8161. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8162. if (IS_SKYLAKE(dev))
  8163. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8164. else if (IS_BROXTON(dev))
  8165. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8166. else
  8167. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8168. if (pipe_config->shared_dpll >= 0) {
  8169. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8170. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8171. &pipe_config->dpll_hw_state));
  8172. }
  8173. /*
  8174. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8175. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8176. * the PCH transcoder is on.
  8177. */
  8178. if (INTEL_INFO(dev)->gen < 9 &&
  8179. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8180. pipe_config->has_pch_encoder = true;
  8181. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8182. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8183. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8184. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8185. }
  8186. }
  8187. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8188. struct intel_crtc_state *pipe_config)
  8189. {
  8190. struct drm_device *dev = crtc->base.dev;
  8191. struct drm_i915_private *dev_priv = dev->dev_private;
  8192. enum intel_display_power_domain pfit_domain;
  8193. uint32_t tmp;
  8194. if (!intel_display_power_is_enabled(dev_priv,
  8195. POWER_DOMAIN_PIPE(crtc->pipe)))
  8196. return false;
  8197. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8198. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8199. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8200. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8201. enum pipe trans_edp_pipe;
  8202. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8203. default:
  8204. WARN(1, "unknown pipe linked to edp transcoder\n");
  8205. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8206. case TRANS_DDI_EDP_INPUT_A_ON:
  8207. trans_edp_pipe = PIPE_A;
  8208. break;
  8209. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8210. trans_edp_pipe = PIPE_B;
  8211. break;
  8212. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8213. trans_edp_pipe = PIPE_C;
  8214. break;
  8215. }
  8216. if (trans_edp_pipe == crtc->pipe)
  8217. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8218. }
  8219. if (!intel_display_power_is_enabled(dev_priv,
  8220. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8221. return false;
  8222. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8223. if (!(tmp & PIPECONF_ENABLE))
  8224. return false;
  8225. haswell_get_ddi_port_state(crtc, pipe_config);
  8226. intel_get_pipe_timings(crtc, pipe_config);
  8227. if (INTEL_INFO(dev)->gen >= 9) {
  8228. skl_init_scalers(dev, crtc, pipe_config);
  8229. }
  8230. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8231. if (INTEL_INFO(dev)->gen >= 9) {
  8232. pipe_config->scaler_state.scaler_id = -1;
  8233. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8234. }
  8235. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8236. if (INTEL_INFO(dev)->gen == 9)
  8237. skylake_get_pfit_config(crtc, pipe_config);
  8238. else if (INTEL_INFO(dev)->gen < 9)
  8239. ironlake_get_pfit_config(crtc, pipe_config);
  8240. else
  8241. MISSING_CASE(INTEL_INFO(dev)->gen);
  8242. }
  8243. if (IS_HASWELL(dev))
  8244. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8245. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8246. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8247. pipe_config->pixel_multiplier =
  8248. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8249. } else {
  8250. pipe_config->pixel_multiplier = 1;
  8251. }
  8252. return true;
  8253. }
  8254. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8255. {
  8256. struct drm_device *dev = crtc->dev;
  8257. struct drm_i915_private *dev_priv = dev->dev_private;
  8258. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8259. uint32_t cntl = 0, size = 0;
  8260. if (base) {
  8261. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8262. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8263. unsigned int stride = roundup_pow_of_two(width) * 4;
  8264. switch (stride) {
  8265. default:
  8266. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8267. width, stride);
  8268. stride = 256;
  8269. /* fallthrough */
  8270. case 256:
  8271. case 512:
  8272. case 1024:
  8273. case 2048:
  8274. break;
  8275. }
  8276. cntl |= CURSOR_ENABLE |
  8277. CURSOR_GAMMA_ENABLE |
  8278. CURSOR_FORMAT_ARGB |
  8279. CURSOR_STRIDE(stride);
  8280. size = (height << 12) | width;
  8281. }
  8282. if (intel_crtc->cursor_cntl != 0 &&
  8283. (intel_crtc->cursor_base != base ||
  8284. intel_crtc->cursor_size != size ||
  8285. intel_crtc->cursor_cntl != cntl)) {
  8286. /* On these chipsets we can only modify the base/size/stride
  8287. * whilst the cursor is disabled.
  8288. */
  8289. I915_WRITE(_CURACNTR, 0);
  8290. POSTING_READ(_CURACNTR);
  8291. intel_crtc->cursor_cntl = 0;
  8292. }
  8293. if (intel_crtc->cursor_base != base) {
  8294. I915_WRITE(_CURABASE, base);
  8295. intel_crtc->cursor_base = base;
  8296. }
  8297. if (intel_crtc->cursor_size != size) {
  8298. I915_WRITE(CURSIZE, size);
  8299. intel_crtc->cursor_size = size;
  8300. }
  8301. if (intel_crtc->cursor_cntl != cntl) {
  8302. I915_WRITE(_CURACNTR, cntl);
  8303. POSTING_READ(_CURACNTR);
  8304. intel_crtc->cursor_cntl = cntl;
  8305. }
  8306. }
  8307. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8308. {
  8309. struct drm_device *dev = crtc->dev;
  8310. struct drm_i915_private *dev_priv = dev->dev_private;
  8311. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8312. int pipe = intel_crtc->pipe;
  8313. uint32_t cntl;
  8314. cntl = 0;
  8315. if (base) {
  8316. cntl = MCURSOR_GAMMA_ENABLE;
  8317. switch (intel_crtc->base.cursor->state->crtc_w) {
  8318. case 64:
  8319. cntl |= CURSOR_MODE_64_ARGB_AX;
  8320. break;
  8321. case 128:
  8322. cntl |= CURSOR_MODE_128_ARGB_AX;
  8323. break;
  8324. case 256:
  8325. cntl |= CURSOR_MODE_256_ARGB_AX;
  8326. break;
  8327. default:
  8328. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8329. return;
  8330. }
  8331. cntl |= pipe << 28; /* Connect to correct pipe */
  8332. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8333. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8334. }
  8335. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8336. cntl |= CURSOR_ROTATE_180;
  8337. if (intel_crtc->cursor_cntl != cntl) {
  8338. I915_WRITE(CURCNTR(pipe), cntl);
  8339. POSTING_READ(CURCNTR(pipe));
  8340. intel_crtc->cursor_cntl = cntl;
  8341. }
  8342. /* and commit changes on next vblank */
  8343. I915_WRITE(CURBASE(pipe), base);
  8344. POSTING_READ(CURBASE(pipe));
  8345. intel_crtc->cursor_base = base;
  8346. }
  8347. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8348. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8349. bool on)
  8350. {
  8351. struct drm_device *dev = crtc->dev;
  8352. struct drm_i915_private *dev_priv = dev->dev_private;
  8353. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8354. int pipe = intel_crtc->pipe;
  8355. int x = crtc->cursor_x;
  8356. int y = crtc->cursor_y;
  8357. u32 base = 0, pos = 0;
  8358. if (on)
  8359. base = intel_crtc->cursor_addr;
  8360. if (x >= intel_crtc->config->pipe_src_w)
  8361. base = 0;
  8362. if (y >= intel_crtc->config->pipe_src_h)
  8363. base = 0;
  8364. if (x < 0) {
  8365. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8366. base = 0;
  8367. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8368. x = -x;
  8369. }
  8370. pos |= x << CURSOR_X_SHIFT;
  8371. if (y < 0) {
  8372. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8373. base = 0;
  8374. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8375. y = -y;
  8376. }
  8377. pos |= y << CURSOR_Y_SHIFT;
  8378. if (base == 0 && intel_crtc->cursor_base == 0)
  8379. return;
  8380. I915_WRITE(CURPOS(pipe), pos);
  8381. /* ILK+ do this automagically */
  8382. if (HAS_GMCH_DISPLAY(dev) &&
  8383. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8384. base += (intel_crtc->base.cursor->state->crtc_h *
  8385. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8386. }
  8387. if (IS_845G(dev) || IS_I865G(dev))
  8388. i845_update_cursor(crtc, base);
  8389. else
  8390. i9xx_update_cursor(crtc, base);
  8391. }
  8392. static bool cursor_size_ok(struct drm_device *dev,
  8393. uint32_t width, uint32_t height)
  8394. {
  8395. if (width == 0 || height == 0)
  8396. return false;
  8397. /*
  8398. * 845g/865g are special in that they are only limited by
  8399. * the width of their cursors, the height is arbitrary up to
  8400. * the precision of the register. Everything else requires
  8401. * square cursors, limited to a few power-of-two sizes.
  8402. */
  8403. if (IS_845G(dev) || IS_I865G(dev)) {
  8404. if ((width & 63) != 0)
  8405. return false;
  8406. if (width > (IS_845G(dev) ? 64 : 512))
  8407. return false;
  8408. if (height > 1023)
  8409. return false;
  8410. } else {
  8411. switch (width | height) {
  8412. case 256:
  8413. case 128:
  8414. if (IS_GEN2(dev))
  8415. return false;
  8416. case 64:
  8417. break;
  8418. default:
  8419. return false;
  8420. }
  8421. }
  8422. return true;
  8423. }
  8424. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8425. u16 *blue, uint32_t start, uint32_t size)
  8426. {
  8427. int end = (start + size > 256) ? 256 : start + size, i;
  8428. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8429. for (i = start; i < end; i++) {
  8430. intel_crtc->lut_r[i] = red[i] >> 8;
  8431. intel_crtc->lut_g[i] = green[i] >> 8;
  8432. intel_crtc->lut_b[i] = blue[i] >> 8;
  8433. }
  8434. intel_crtc_load_lut(crtc);
  8435. }
  8436. /* VESA 640x480x72Hz mode to set on the pipe */
  8437. static struct drm_display_mode load_detect_mode = {
  8438. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8439. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8440. };
  8441. struct drm_framebuffer *
  8442. __intel_framebuffer_create(struct drm_device *dev,
  8443. struct drm_mode_fb_cmd2 *mode_cmd,
  8444. struct drm_i915_gem_object *obj)
  8445. {
  8446. struct intel_framebuffer *intel_fb;
  8447. int ret;
  8448. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8449. if (!intel_fb) {
  8450. drm_gem_object_unreference(&obj->base);
  8451. return ERR_PTR(-ENOMEM);
  8452. }
  8453. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8454. if (ret)
  8455. goto err;
  8456. return &intel_fb->base;
  8457. err:
  8458. drm_gem_object_unreference(&obj->base);
  8459. kfree(intel_fb);
  8460. return ERR_PTR(ret);
  8461. }
  8462. static struct drm_framebuffer *
  8463. intel_framebuffer_create(struct drm_device *dev,
  8464. struct drm_mode_fb_cmd2 *mode_cmd,
  8465. struct drm_i915_gem_object *obj)
  8466. {
  8467. struct drm_framebuffer *fb;
  8468. int ret;
  8469. ret = i915_mutex_lock_interruptible(dev);
  8470. if (ret)
  8471. return ERR_PTR(ret);
  8472. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8473. mutex_unlock(&dev->struct_mutex);
  8474. return fb;
  8475. }
  8476. static u32
  8477. intel_framebuffer_pitch_for_width(int width, int bpp)
  8478. {
  8479. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8480. return ALIGN(pitch, 64);
  8481. }
  8482. static u32
  8483. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8484. {
  8485. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8486. return PAGE_ALIGN(pitch * mode->vdisplay);
  8487. }
  8488. static struct drm_framebuffer *
  8489. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8490. struct drm_display_mode *mode,
  8491. int depth, int bpp)
  8492. {
  8493. struct drm_i915_gem_object *obj;
  8494. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8495. obj = i915_gem_alloc_object(dev,
  8496. intel_framebuffer_size_for_mode(mode, bpp));
  8497. if (obj == NULL)
  8498. return ERR_PTR(-ENOMEM);
  8499. mode_cmd.width = mode->hdisplay;
  8500. mode_cmd.height = mode->vdisplay;
  8501. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8502. bpp);
  8503. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8504. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8505. }
  8506. static struct drm_framebuffer *
  8507. mode_fits_in_fbdev(struct drm_device *dev,
  8508. struct drm_display_mode *mode)
  8509. {
  8510. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8511. struct drm_i915_private *dev_priv = dev->dev_private;
  8512. struct drm_i915_gem_object *obj;
  8513. struct drm_framebuffer *fb;
  8514. if (!dev_priv->fbdev)
  8515. return NULL;
  8516. if (!dev_priv->fbdev->fb)
  8517. return NULL;
  8518. obj = dev_priv->fbdev->fb->obj;
  8519. BUG_ON(!obj);
  8520. fb = &dev_priv->fbdev->fb->base;
  8521. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8522. fb->bits_per_pixel))
  8523. return NULL;
  8524. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8525. return NULL;
  8526. return fb;
  8527. #else
  8528. return NULL;
  8529. #endif
  8530. }
  8531. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8532. struct drm_crtc *crtc,
  8533. struct drm_display_mode *mode,
  8534. struct drm_framebuffer *fb,
  8535. int x, int y)
  8536. {
  8537. struct drm_plane_state *plane_state;
  8538. int hdisplay, vdisplay;
  8539. int ret;
  8540. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8541. if (IS_ERR(plane_state))
  8542. return PTR_ERR(plane_state);
  8543. if (mode)
  8544. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8545. else
  8546. hdisplay = vdisplay = 0;
  8547. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8548. if (ret)
  8549. return ret;
  8550. drm_atomic_set_fb_for_plane(plane_state, fb);
  8551. plane_state->crtc_x = 0;
  8552. plane_state->crtc_y = 0;
  8553. plane_state->crtc_w = hdisplay;
  8554. plane_state->crtc_h = vdisplay;
  8555. plane_state->src_x = x << 16;
  8556. plane_state->src_y = y << 16;
  8557. plane_state->src_w = hdisplay << 16;
  8558. plane_state->src_h = vdisplay << 16;
  8559. return 0;
  8560. }
  8561. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8562. struct drm_display_mode *mode,
  8563. struct intel_load_detect_pipe *old,
  8564. struct drm_modeset_acquire_ctx *ctx)
  8565. {
  8566. struct intel_crtc *intel_crtc;
  8567. struct intel_encoder *intel_encoder =
  8568. intel_attached_encoder(connector);
  8569. struct drm_crtc *possible_crtc;
  8570. struct drm_encoder *encoder = &intel_encoder->base;
  8571. struct drm_crtc *crtc = NULL;
  8572. struct drm_device *dev = encoder->dev;
  8573. struct drm_framebuffer *fb;
  8574. struct drm_mode_config *config = &dev->mode_config;
  8575. struct drm_atomic_state *state = NULL;
  8576. struct drm_connector_state *connector_state;
  8577. struct intel_crtc_state *crtc_state;
  8578. int ret, i = -1;
  8579. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8580. connector->base.id, connector->name,
  8581. encoder->base.id, encoder->name);
  8582. retry:
  8583. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8584. if (ret)
  8585. goto fail;
  8586. /*
  8587. * Algorithm gets a little messy:
  8588. *
  8589. * - if the connector already has an assigned crtc, use it (but make
  8590. * sure it's on first)
  8591. *
  8592. * - try to find the first unused crtc that can drive this connector,
  8593. * and use that if we find one
  8594. */
  8595. /* See if we already have a CRTC for this connector */
  8596. if (encoder->crtc) {
  8597. crtc = encoder->crtc;
  8598. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8599. if (ret)
  8600. goto fail;
  8601. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8602. if (ret)
  8603. goto fail;
  8604. old->dpms_mode = connector->dpms;
  8605. old->load_detect_temp = false;
  8606. /* Make sure the crtc and connector are running */
  8607. if (connector->dpms != DRM_MODE_DPMS_ON)
  8608. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8609. return true;
  8610. }
  8611. /* Find an unused one (if possible) */
  8612. for_each_crtc(dev, possible_crtc) {
  8613. i++;
  8614. if (!(encoder->possible_crtcs & (1 << i)))
  8615. continue;
  8616. if (possible_crtc->state->enable)
  8617. continue;
  8618. crtc = possible_crtc;
  8619. break;
  8620. }
  8621. /*
  8622. * If we didn't find an unused CRTC, don't use any.
  8623. */
  8624. if (!crtc) {
  8625. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8626. goto fail;
  8627. }
  8628. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8629. if (ret)
  8630. goto fail;
  8631. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8632. if (ret)
  8633. goto fail;
  8634. intel_crtc = to_intel_crtc(crtc);
  8635. old->dpms_mode = connector->dpms;
  8636. old->load_detect_temp = true;
  8637. old->release_fb = NULL;
  8638. state = drm_atomic_state_alloc(dev);
  8639. if (!state)
  8640. return false;
  8641. state->acquire_ctx = ctx;
  8642. connector_state = drm_atomic_get_connector_state(state, connector);
  8643. if (IS_ERR(connector_state)) {
  8644. ret = PTR_ERR(connector_state);
  8645. goto fail;
  8646. }
  8647. connector_state->crtc = crtc;
  8648. connector_state->best_encoder = &intel_encoder->base;
  8649. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8650. if (IS_ERR(crtc_state)) {
  8651. ret = PTR_ERR(crtc_state);
  8652. goto fail;
  8653. }
  8654. crtc_state->base.active = crtc_state->base.enable = true;
  8655. if (!mode)
  8656. mode = &load_detect_mode;
  8657. /* We need a framebuffer large enough to accommodate all accesses
  8658. * that the plane may generate whilst we perform load detection.
  8659. * We can not rely on the fbcon either being present (we get called
  8660. * during its initialisation to detect all boot displays, or it may
  8661. * not even exist) or that it is large enough to satisfy the
  8662. * requested mode.
  8663. */
  8664. fb = mode_fits_in_fbdev(dev, mode);
  8665. if (fb == NULL) {
  8666. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8667. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8668. old->release_fb = fb;
  8669. } else
  8670. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8671. if (IS_ERR(fb)) {
  8672. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8673. goto fail;
  8674. }
  8675. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8676. if (ret)
  8677. goto fail;
  8678. drm_mode_copy(&crtc_state->base.mode, mode);
  8679. if (drm_atomic_commit(state)) {
  8680. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8681. if (old->release_fb)
  8682. old->release_fb->funcs->destroy(old->release_fb);
  8683. goto fail;
  8684. }
  8685. crtc->primary->crtc = crtc;
  8686. /* let the connector get through one full cycle before testing */
  8687. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8688. return true;
  8689. fail:
  8690. drm_atomic_state_free(state);
  8691. state = NULL;
  8692. if (ret == -EDEADLK) {
  8693. drm_modeset_backoff(ctx);
  8694. goto retry;
  8695. }
  8696. return false;
  8697. }
  8698. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8699. struct intel_load_detect_pipe *old,
  8700. struct drm_modeset_acquire_ctx *ctx)
  8701. {
  8702. struct drm_device *dev = connector->dev;
  8703. struct intel_encoder *intel_encoder =
  8704. intel_attached_encoder(connector);
  8705. struct drm_encoder *encoder = &intel_encoder->base;
  8706. struct drm_crtc *crtc = encoder->crtc;
  8707. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8708. struct drm_atomic_state *state;
  8709. struct drm_connector_state *connector_state;
  8710. struct intel_crtc_state *crtc_state;
  8711. int ret;
  8712. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8713. connector->base.id, connector->name,
  8714. encoder->base.id, encoder->name);
  8715. if (old->load_detect_temp) {
  8716. state = drm_atomic_state_alloc(dev);
  8717. if (!state)
  8718. goto fail;
  8719. state->acquire_ctx = ctx;
  8720. connector_state = drm_atomic_get_connector_state(state, connector);
  8721. if (IS_ERR(connector_state))
  8722. goto fail;
  8723. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8724. if (IS_ERR(crtc_state))
  8725. goto fail;
  8726. connector_state->best_encoder = NULL;
  8727. connector_state->crtc = NULL;
  8728. crtc_state->base.enable = crtc_state->base.active = false;
  8729. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8730. 0, 0);
  8731. if (ret)
  8732. goto fail;
  8733. ret = drm_atomic_commit(state);
  8734. if (ret)
  8735. goto fail;
  8736. if (old->release_fb) {
  8737. drm_framebuffer_unregister_private(old->release_fb);
  8738. drm_framebuffer_unreference(old->release_fb);
  8739. }
  8740. return;
  8741. }
  8742. /* Switch crtc and encoder back off if necessary */
  8743. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8744. connector->funcs->dpms(connector, old->dpms_mode);
  8745. return;
  8746. fail:
  8747. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8748. drm_atomic_state_free(state);
  8749. }
  8750. static int i9xx_pll_refclk(struct drm_device *dev,
  8751. const struct intel_crtc_state *pipe_config)
  8752. {
  8753. struct drm_i915_private *dev_priv = dev->dev_private;
  8754. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8755. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8756. return dev_priv->vbt.lvds_ssc_freq;
  8757. else if (HAS_PCH_SPLIT(dev))
  8758. return 120000;
  8759. else if (!IS_GEN2(dev))
  8760. return 96000;
  8761. else
  8762. return 48000;
  8763. }
  8764. /* Returns the clock of the currently programmed mode of the given pipe. */
  8765. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8766. struct intel_crtc_state *pipe_config)
  8767. {
  8768. struct drm_device *dev = crtc->base.dev;
  8769. struct drm_i915_private *dev_priv = dev->dev_private;
  8770. int pipe = pipe_config->cpu_transcoder;
  8771. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8772. u32 fp;
  8773. intel_clock_t clock;
  8774. int port_clock;
  8775. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8776. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8777. fp = pipe_config->dpll_hw_state.fp0;
  8778. else
  8779. fp = pipe_config->dpll_hw_state.fp1;
  8780. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8781. if (IS_PINEVIEW(dev)) {
  8782. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8783. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8784. } else {
  8785. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8786. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8787. }
  8788. if (!IS_GEN2(dev)) {
  8789. if (IS_PINEVIEW(dev))
  8790. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8791. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8792. else
  8793. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8794. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8795. switch (dpll & DPLL_MODE_MASK) {
  8796. case DPLLB_MODE_DAC_SERIAL:
  8797. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8798. 5 : 10;
  8799. break;
  8800. case DPLLB_MODE_LVDS:
  8801. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8802. 7 : 14;
  8803. break;
  8804. default:
  8805. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8806. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8807. return;
  8808. }
  8809. if (IS_PINEVIEW(dev))
  8810. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8811. else
  8812. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8813. } else {
  8814. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8815. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8816. if (is_lvds) {
  8817. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8818. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8819. if (lvds & LVDS_CLKB_POWER_UP)
  8820. clock.p2 = 7;
  8821. else
  8822. clock.p2 = 14;
  8823. } else {
  8824. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8825. clock.p1 = 2;
  8826. else {
  8827. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8828. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8829. }
  8830. if (dpll & PLL_P2_DIVIDE_BY_4)
  8831. clock.p2 = 4;
  8832. else
  8833. clock.p2 = 2;
  8834. }
  8835. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8836. }
  8837. /*
  8838. * This value includes pixel_multiplier. We will use
  8839. * port_clock to compute adjusted_mode.crtc_clock in the
  8840. * encoder's get_config() function.
  8841. */
  8842. pipe_config->port_clock = port_clock;
  8843. }
  8844. int intel_dotclock_calculate(int link_freq,
  8845. const struct intel_link_m_n *m_n)
  8846. {
  8847. /*
  8848. * The calculation for the data clock is:
  8849. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8850. * But we want to avoid losing precison if possible, so:
  8851. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8852. *
  8853. * and the link clock is simpler:
  8854. * link_clock = (m * link_clock) / n
  8855. */
  8856. if (!m_n->link_n)
  8857. return 0;
  8858. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8859. }
  8860. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8861. struct intel_crtc_state *pipe_config)
  8862. {
  8863. struct drm_device *dev = crtc->base.dev;
  8864. /* read out port_clock from the DPLL */
  8865. i9xx_crtc_clock_get(crtc, pipe_config);
  8866. /*
  8867. * This value does not include pixel_multiplier.
  8868. * We will check that port_clock and adjusted_mode.crtc_clock
  8869. * agree once we know their relationship in the encoder's
  8870. * get_config() function.
  8871. */
  8872. pipe_config->base.adjusted_mode.crtc_clock =
  8873. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8874. &pipe_config->fdi_m_n);
  8875. }
  8876. /** Returns the currently programmed mode of the given pipe. */
  8877. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8878. struct drm_crtc *crtc)
  8879. {
  8880. struct drm_i915_private *dev_priv = dev->dev_private;
  8881. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8882. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8883. struct drm_display_mode *mode;
  8884. struct intel_crtc_state pipe_config;
  8885. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8886. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8887. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8888. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8889. enum pipe pipe = intel_crtc->pipe;
  8890. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8891. if (!mode)
  8892. return NULL;
  8893. /*
  8894. * Construct a pipe_config sufficient for getting the clock info
  8895. * back out of crtc_clock_get.
  8896. *
  8897. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8898. * to use a real value here instead.
  8899. */
  8900. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8901. pipe_config.pixel_multiplier = 1;
  8902. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8903. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8904. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8905. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8906. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8907. mode->hdisplay = (htot & 0xffff) + 1;
  8908. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8909. mode->hsync_start = (hsync & 0xffff) + 1;
  8910. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8911. mode->vdisplay = (vtot & 0xffff) + 1;
  8912. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8913. mode->vsync_start = (vsync & 0xffff) + 1;
  8914. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8915. drm_mode_set_name(mode);
  8916. return mode;
  8917. }
  8918. void intel_mark_busy(struct drm_device *dev)
  8919. {
  8920. struct drm_i915_private *dev_priv = dev->dev_private;
  8921. if (dev_priv->mm.busy)
  8922. return;
  8923. intel_runtime_pm_get(dev_priv);
  8924. i915_update_gfx_val(dev_priv);
  8925. if (INTEL_INFO(dev)->gen >= 6)
  8926. gen6_rps_busy(dev_priv);
  8927. dev_priv->mm.busy = true;
  8928. }
  8929. void intel_mark_idle(struct drm_device *dev)
  8930. {
  8931. struct drm_i915_private *dev_priv = dev->dev_private;
  8932. if (!dev_priv->mm.busy)
  8933. return;
  8934. dev_priv->mm.busy = false;
  8935. if (INTEL_INFO(dev)->gen >= 6)
  8936. gen6_rps_idle(dev->dev_private);
  8937. intel_runtime_pm_put(dev_priv);
  8938. }
  8939. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8940. {
  8941. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8942. struct drm_device *dev = crtc->dev;
  8943. struct intel_unpin_work *work;
  8944. spin_lock_irq(&dev->event_lock);
  8945. work = intel_crtc->unpin_work;
  8946. intel_crtc->unpin_work = NULL;
  8947. spin_unlock_irq(&dev->event_lock);
  8948. if (work) {
  8949. cancel_work_sync(&work->work);
  8950. kfree(work);
  8951. }
  8952. drm_crtc_cleanup(crtc);
  8953. kfree(intel_crtc);
  8954. }
  8955. static void intel_unpin_work_fn(struct work_struct *__work)
  8956. {
  8957. struct intel_unpin_work *work =
  8958. container_of(__work, struct intel_unpin_work, work);
  8959. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8960. struct drm_device *dev = crtc->base.dev;
  8961. struct drm_plane *primary = crtc->base.primary;
  8962. mutex_lock(&dev->struct_mutex);
  8963. intel_unpin_fb_obj(work->old_fb, primary->state);
  8964. drm_gem_object_unreference(&work->pending_flip_obj->base);
  8965. if (work->flip_queued_req)
  8966. i915_gem_request_assign(&work->flip_queued_req, NULL);
  8967. mutex_unlock(&dev->struct_mutex);
  8968. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  8969. drm_framebuffer_unreference(work->old_fb);
  8970. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8971. atomic_dec(&crtc->unpin_work_count);
  8972. kfree(work);
  8973. }
  8974. static void do_intel_finish_page_flip(struct drm_device *dev,
  8975. struct drm_crtc *crtc)
  8976. {
  8977. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8978. struct intel_unpin_work *work;
  8979. unsigned long flags;
  8980. /* Ignore early vblank irqs */
  8981. if (intel_crtc == NULL)
  8982. return;
  8983. /*
  8984. * This is called both by irq handlers and the reset code (to complete
  8985. * lost pageflips) so needs the full irqsave spinlocks.
  8986. */
  8987. spin_lock_irqsave(&dev->event_lock, flags);
  8988. work = intel_crtc->unpin_work;
  8989. /* Ensure we don't miss a work->pending update ... */
  8990. smp_rmb();
  8991. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  8992. spin_unlock_irqrestore(&dev->event_lock, flags);
  8993. return;
  8994. }
  8995. page_flip_completed(intel_crtc);
  8996. spin_unlock_irqrestore(&dev->event_lock, flags);
  8997. }
  8998. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  8999. {
  9000. struct drm_i915_private *dev_priv = dev->dev_private;
  9001. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9002. do_intel_finish_page_flip(dev, crtc);
  9003. }
  9004. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9005. {
  9006. struct drm_i915_private *dev_priv = dev->dev_private;
  9007. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9008. do_intel_finish_page_flip(dev, crtc);
  9009. }
  9010. /* Is 'a' after or equal to 'b'? */
  9011. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9012. {
  9013. return !((a - b) & 0x80000000);
  9014. }
  9015. static bool page_flip_finished(struct intel_crtc *crtc)
  9016. {
  9017. struct drm_device *dev = crtc->base.dev;
  9018. struct drm_i915_private *dev_priv = dev->dev_private;
  9019. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9020. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9021. return true;
  9022. /*
  9023. * The relevant registers doen't exist on pre-ctg.
  9024. * As the flip done interrupt doesn't trigger for mmio
  9025. * flips on gmch platforms, a flip count check isn't
  9026. * really needed there. But since ctg has the registers,
  9027. * include it in the check anyway.
  9028. */
  9029. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9030. return true;
  9031. /*
  9032. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9033. * used the same base address. In that case the mmio flip might
  9034. * have completed, but the CS hasn't even executed the flip yet.
  9035. *
  9036. * A flip count check isn't enough as the CS might have updated
  9037. * the base address just after start of vblank, but before we
  9038. * managed to process the interrupt. This means we'd complete the
  9039. * CS flip too soon.
  9040. *
  9041. * Combining both checks should get us a good enough result. It may
  9042. * still happen that the CS flip has been executed, but has not
  9043. * yet actually completed. But in case the base address is the same
  9044. * anyway, we don't really care.
  9045. */
  9046. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9047. crtc->unpin_work->gtt_offset &&
  9048. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9049. crtc->unpin_work->flip_count);
  9050. }
  9051. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9052. {
  9053. struct drm_i915_private *dev_priv = dev->dev_private;
  9054. struct intel_crtc *intel_crtc =
  9055. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9056. unsigned long flags;
  9057. /*
  9058. * This is called both by irq handlers and the reset code (to complete
  9059. * lost pageflips) so needs the full irqsave spinlocks.
  9060. *
  9061. * NB: An MMIO update of the plane base pointer will also
  9062. * generate a page-flip completion irq, i.e. every modeset
  9063. * is also accompanied by a spurious intel_prepare_page_flip().
  9064. */
  9065. spin_lock_irqsave(&dev->event_lock, flags);
  9066. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9067. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9068. spin_unlock_irqrestore(&dev->event_lock, flags);
  9069. }
  9070. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9071. {
  9072. /* Ensure that the work item is consistent when activating it ... */
  9073. smp_wmb();
  9074. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9075. /* and that it is marked active as soon as the irq could fire. */
  9076. smp_wmb();
  9077. }
  9078. static int intel_gen2_queue_flip(struct drm_device *dev,
  9079. struct drm_crtc *crtc,
  9080. struct drm_framebuffer *fb,
  9081. struct drm_i915_gem_object *obj,
  9082. struct drm_i915_gem_request *req,
  9083. uint32_t flags)
  9084. {
  9085. struct intel_engine_cs *ring = req->ring;
  9086. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9087. u32 flip_mask;
  9088. int ret;
  9089. ret = intel_ring_begin(req, 6);
  9090. if (ret)
  9091. return ret;
  9092. /* Can't queue multiple flips, so wait for the previous
  9093. * one to finish before executing the next.
  9094. */
  9095. if (intel_crtc->plane)
  9096. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9097. else
  9098. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9099. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9100. intel_ring_emit(ring, MI_NOOP);
  9101. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9102. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9103. intel_ring_emit(ring, fb->pitches[0]);
  9104. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9105. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9106. intel_mark_page_flip_active(intel_crtc);
  9107. return 0;
  9108. }
  9109. static int intel_gen3_queue_flip(struct drm_device *dev,
  9110. struct drm_crtc *crtc,
  9111. struct drm_framebuffer *fb,
  9112. struct drm_i915_gem_object *obj,
  9113. struct drm_i915_gem_request *req,
  9114. uint32_t flags)
  9115. {
  9116. struct intel_engine_cs *ring = req->ring;
  9117. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9118. u32 flip_mask;
  9119. int ret;
  9120. ret = intel_ring_begin(req, 6);
  9121. if (ret)
  9122. return ret;
  9123. if (intel_crtc->plane)
  9124. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9125. else
  9126. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9127. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9128. intel_ring_emit(ring, MI_NOOP);
  9129. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9130. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9131. intel_ring_emit(ring, fb->pitches[0]);
  9132. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9133. intel_ring_emit(ring, MI_NOOP);
  9134. intel_mark_page_flip_active(intel_crtc);
  9135. return 0;
  9136. }
  9137. static int intel_gen4_queue_flip(struct drm_device *dev,
  9138. struct drm_crtc *crtc,
  9139. struct drm_framebuffer *fb,
  9140. struct drm_i915_gem_object *obj,
  9141. struct drm_i915_gem_request *req,
  9142. uint32_t flags)
  9143. {
  9144. struct intel_engine_cs *ring = req->ring;
  9145. struct drm_i915_private *dev_priv = dev->dev_private;
  9146. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9147. uint32_t pf, pipesrc;
  9148. int ret;
  9149. ret = intel_ring_begin(req, 4);
  9150. if (ret)
  9151. return ret;
  9152. /* i965+ uses the linear or tiled offsets from the
  9153. * Display Registers (which do not change across a page-flip)
  9154. * so we need only reprogram the base address.
  9155. */
  9156. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9157. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9158. intel_ring_emit(ring, fb->pitches[0]);
  9159. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9160. obj->tiling_mode);
  9161. /* XXX Enabling the panel-fitter across page-flip is so far
  9162. * untested on non-native modes, so ignore it for now.
  9163. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9164. */
  9165. pf = 0;
  9166. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9167. intel_ring_emit(ring, pf | pipesrc);
  9168. intel_mark_page_flip_active(intel_crtc);
  9169. return 0;
  9170. }
  9171. static int intel_gen6_queue_flip(struct drm_device *dev,
  9172. struct drm_crtc *crtc,
  9173. struct drm_framebuffer *fb,
  9174. struct drm_i915_gem_object *obj,
  9175. struct drm_i915_gem_request *req,
  9176. uint32_t flags)
  9177. {
  9178. struct intel_engine_cs *ring = req->ring;
  9179. struct drm_i915_private *dev_priv = dev->dev_private;
  9180. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9181. uint32_t pf, pipesrc;
  9182. int ret;
  9183. ret = intel_ring_begin(req, 4);
  9184. if (ret)
  9185. return ret;
  9186. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9187. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9188. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9189. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9190. /* Contrary to the suggestions in the documentation,
  9191. * "Enable Panel Fitter" does not seem to be required when page
  9192. * flipping with a non-native mode, and worse causes a normal
  9193. * modeset to fail.
  9194. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9195. */
  9196. pf = 0;
  9197. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9198. intel_ring_emit(ring, pf | pipesrc);
  9199. intel_mark_page_flip_active(intel_crtc);
  9200. return 0;
  9201. }
  9202. static int intel_gen7_queue_flip(struct drm_device *dev,
  9203. struct drm_crtc *crtc,
  9204. struct drm_framebuffer *fb,
  9205. struct drm_i915_gem_object *obj,
  9206. struct drm_i915_gem_request *req,
  9207. uint32_t flags)
  9208. {
  9209. struct intel_engine_cs *ring = req->ring;
  9210. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9211. uint32_t plane_bit = 0;
  9212. int len, ret;
  9213. switch (intel_crtc->plane) {
  9214. case PLANE_A:
  9215. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9216. break;
  9217. case PLANE_B:
  9218. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9219. break;
  9220. case PLANE_C:
  9221. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9222. break;
  9223. default:
  9224. WARN_ONCE(1, "unknown plane in flip command\n");
  9225. return -ENODEV;
  9226. }
  9227. len = 4;
  9228. if (ring->id == RCS) {
  9229. len += 6;
  9230. /*
  9231. * On Gen 8, SRM is now taking an extra dword to accommodate
  9232. * 48bits addresses, and we need a NOOP for the batch size to
  9233. * stay even.
  9234. */
  9235. if (IS_GEN8(dev))
  9236. len += 2;
  9237. }
  9238. /*
  9239. * BSpec MI_DISPLAY_FLIP for IVB:
  9240. * "The full packet must be contained within the same cache line."
  9241. *
  9242. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9243. * cacheline, if we ever start emitting more commands before
  9244. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9245. * then do the cacheline alignment, and finally emit the
  9246. * MI_DISPLAY_FLIP.
  9247. */
  9248. ret = intel_ring_cacheline_align(req);
  9249. if (ret)
  9250. return ret;
  9251. ret = intel_ring_begin(req, len);
  9252. if (ret)
  9253. return ret;
  9254. /* Unmask the flip-done completion message. Note that the bspec says that
  9255. * we should do this for both the BCS and RCS, and that we must not unmask
  9256. * more than one flip event at any time (or ensure that one flip message
  9257. * can be sent by waiting for flip-done prior to queueing new flips).
  9258. * Experimentation says that BCS works despite DERRMR masking all
  9259. * flip-done completion events and that unmasking all planes at once
  9260. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9261. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9262. */
  9263. if (ring->id == RCS) {
  9264. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9265. intel_ring_emit(ring, DERRMR);
  9266. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9267. DERRMR_PIPEB_PRI_FLIP_DONE |
  9268. DERRMR_PIPEC_PRI_FLIP_DONE));
  9269. if (IS_GEN8(dev))
  9270. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9271. MI_SRM_LRM_GLOBAL_GTT);
  9272. else
  9273. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9274. MI_SRM_LRM_GLOBAL_GTT);
  9275. intel_ring_emit(ring, DERRMR);
  9276. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9277. if (IS_GEN8(dev)) {
  9278. intel_ring_emit(ring, 0);
  9279. intel_ring_emit(ring, MI_NOOP);
  9280. }
  9281. }
  9282. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9283. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9284. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9285. intel_ring_emit(ring, (MI_NOOP));
  9286. intel_mark_page_flip_active(intel_crtc);
  9287. return 0;
  9288. }
  9289. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9290. struct drm_i915_gem_object *obj)
  9291. {
  9292. /*
  9293. * This is not being used for older platforms, because
  9294. * non-availability of flip done interrupt forces us to use
  9295. * CS flips. Older platforms derive flip done using some clever
  9296. * tricks involving the flip_pending status bits and vblank irqs.
  9297. * So using MMIO flips there would disrupt this mechanism.
  9298. */
  9299. if (ring == NULL)
  9300. return true;
  9301. if (INTEL_INFO(ring->dev)->gen < 5)
  9302. return false;
  9303. if (i915.use_mmio_flip < 0)
  9304. return false;
  9305. else if (i915.use_mmio_flip > 0)
  9306. return true;
  9307. else if (i915.enable_execlists)
  9308. return true;
  9309. else
  9310. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9311. }
  9312. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9313. {
  9314. struct drm_device *dev = intel_crtc->base.dev;
  9315. struct drm_i915_private *dev_priv = dev->dev_private;
  9316. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9317. const enum pipe pipe = intel_crtc->pipe;
  9318. u32 ctl, stride;
  9319. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9320. ctl &= ~PLANE_CTL_TILED_MASK;
  9321. switch (fb->modifier[0]) {
  9322. case DRM_FORMAT_MOD_NONE:
  9323. break;
  9324. case I915_FORMAT_MOD_X_TILED:
  9325. ctl |= PLANE_CTL_TILED_X;
  9326. break;
  9327. case I915_FORMAT_MOD_Y_TILED:
  9328. ctl |= PLANE_CTL_TILED_Y;
  9329. break;
  9330. case I915_FORMAT_MOD_Yf_TILED:
  9331. ctl |= PLANE_CTL_TILED_YF;
  9332. break;
  9333. default:
  9334. MISSING_CASE(fb->modifier[0]);
  9335. }
  9336. /*
  9337. * The stride is either expressed as a multiple of 64 bytes chunks for
  9338. * linear buffers or in number of tiles for tiled buffers.
  9339. */
  9340. stride = fb->pitches[0] /
  9341. intel_fb_stride_alignment(dev, fb->modifier[0],
  9342. fb->pixel_format);
  9343. /*
  9344. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9345. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9346. */
  9347. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9348. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9349. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9350. POSTING_READ(PLANE_SURF(pipe, 0));
  9351. }
  9352. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9353. {
  9354. struct drm_device *dev = intel_crtc->base.dev;
  9355. struct drm_i915_private *dev_priv = dev->dev_private;
  9356. struct intel_framebuffer *intel_fb =
  9357. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9358. struct drm_i915_gem_object *obj = intel_fb->obj;
  9359. u32 dspcntr;
  9360. u32 reg;
  9361. reg = DSPCNTR(intel_crtc->plane);
  9362. dspcntr = I915_READ(reg);
  9363. if (obj->tiling_mode != I915_TILING_NONE)
  9364. dspcntr |= DISPPLANE_TILED;
  9365. else
  9366. dspcntr &= ~DISPPLANE_TILED;
  9367. I915_WRITE(reg, dspcntr);
  9368. I915_WRITE(DSPSURF(intel_crtc->plane),
  9369. intel_crtc->unpin_work->gtt_offset);
  9370. POSTING_READ(DSPSURF(intel_crtc->plane));
  9371. }
  9372. /*
  9373. * XXX: This is the temporary way to update the plane registers until we get
  9374. * around to using the usual plane update functions for MMIO flips
  9375. */
  9376. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9377. {
  9378. struct drm_device *dev = intel_crtc->base.dev;
  9379. u32 start_vbl_count;
  9380. intel_mark_page_flip_active(intel_crtc);
  9381. intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9382. if (INTEL_INFO(dev)->gen >= 9)
  9383. skl_do_mmio_flip(intel_crtc);
  9384. else
  9385. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9386. ilk_do_mmio_flip(intel_crtc);
  9387. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9388. }
  9389. static void intel_mmio_flip_work_func(struct work_struct *work)
  9390. {
  9391. struct intel_mmio_flip *mmio_flip =
  9392. container_of(work, struct intel_mmio_flip, work);
  9393. if (mmio_flip->req)
  9394. WARN_ON(__i915_wait_request(mmio_flip->req,
  9395. mmio_flip->crtc->reset_counter,
  9396. false, NULL,
  9397. &mmio_flip->i915->rps.mmioflips));
  9398. intel_do_mmio_flip(mmio_flip->crtc);
  9399. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9400. kfree(mmio_flip);
  9401. }
  9402. static int intel_queue_mmio_flip(struct drm_device *dev,
  9403. struct drm_crtc *crtc,
  9404. struct drm_framebuffer *fb,
  9405. struct drm_i915_gem_object *obj,
  9406. struct intel_engine_cs *ring,
  9407. uint32_t flags)
  9408. {
  9409. struct intel_mmio_flip *mmio_flip;
  9410. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9411. if (mmio_flip == NULL)
  9412. return -ENOMEM;
  9413. mmio_flip->i915 = to_i915(dev);
  9414. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9415. mmio_flip->crtc = to_intel_crtc(crtc);
  9416. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9417. schedule_work(&mmio_flip->work);
  9418. return 0;
  9419. }
  9420. static int intel_default_queue_flip(struct drm_device *dev,
  9421. struct drm_crtc *crtc,
  9422. struct drm_framebuffer *fb,
  9423. struct drm_i915_gem_object *obj,
  9424. struct drm_i915_gem_request *req,
  9425. uint32_t flags)
  9426. {
  9427. return -ENODEV;
  9428. }
  9429. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9430. struct drm_crtc *crtc)
  9431. {
  9432. struct drm_i915_private *dev_priv = dev->dev_private;
  9433. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9434. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9435. u32 addr;
  9436. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9437. return true;
  9438. if (!work->enable_stall_check)
  9439. return false;
  9440. if (work->flip_ready_vblank == 0) {
  9441. if (work->flip_queued_req &&
  9442. !i915_gem_request_completed(work->flip_queued_req, true))
  9443. return false;
  9444. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9445. }
  9446. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9447. return false;
  9448. /* Potential stall - if we see that the flip has happened,
  9449. * assume a missed interrupt. */
  9450. if (INTEL_INFO(dev)->gen >= 4)
  9451. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9452. else
  9453. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9454. /* There is a potential issue here with a false positive after a flip
  9455. * to the same address. We could address this by checking for a
  9456. * non-incrementing frame counter.
  9457. */
  9458. return addr == work->gtt_offset;
  9459. }
  9460. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9461. {
  9462. struct drm_i915_private *dev_priv = dev->dev_private;
  9463. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9464. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9465. struct intel_unpin_work *work;
  9466. WARN_ON(!in_interrupt());
  9467. if (crtc == NULL)
  9468. return;
  9469. spin_lock(&dev->event_lock);
  9470. work = intel_crtc->unpin_work;
  9471. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9472. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9473. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9474. page_flip_completed(intel_crtc);
  9475. work = NULL;
  9476. }
  9477. if (work != NULL &&
  9478. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9479. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9480. spin_unlock(&dev->event_lock);
  9481. }
  9482. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9483. struct drm_framebuffer *fb,
  9484. struct drm_pending_vblank_event *event,
  9485. uint32_t page_flip_flags)
  9486. {
  9487. struct drm_device *dev = crtc->dev;
  9488. struct drm_i915_private *dev_priv = dev->dev_private;
  9489. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9490. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9491. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9492. struct drm_plane *primary = crtc->primary;
  9493. enum pipe pipe = intel_crtc->pipe;
  9494. struct intel_unpin_work *work;
  9495. struct intel_engine_cs *ring;
  9496. bool mmio_flip;
  9497. struct drm_i915_gem_request *request = NULL;
  9498. int ret;
  9499. /*
  9500. * drm_mode_page_flip_ioctl() should already catch this, but double
  9501. * check to be safe. In the future we may enable pageflipping from
  9502. * a disabled primary plane.
  9503. */
  9504. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9505. return -EBUSY;
  9506. /* Can't change pixel format via MI display flips. */
  9507. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9508. return -EINVAL;
  9509. /*
  9510. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9511. * Note that pitch changes could also affect these register.
  9512. */
  9513. if (INTEL_INFO(dev)->gen > 3 &&
  9514. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9515. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9516. return -EINVAL;
  9517. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9518. goto out_hang;
  9519. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9520. if (work == NULL)
  9521. return -ENOMEM;
  9522. work->event = event;
  9523. work->crtc = crtc;
  9524. work->old_fb = old_fb;
  9525. INIT_WORK(&work->work, intel_unpin_work_fn);
  9526. ret = drm_crtc_vblank_get(crtc);
  9527. if (ret)
  9528. goto free_work;
  9529. /* We borrow the event spin lock for protecting unpin_work */
  9530. spin_lock_irq(&dev->event_lock);
  9531. if (intel_crtc->unpin_work) {
  9532. /* Before declaring the flip queue wedged, check if
  9533. * the hardware completed the operation behind our backs.
  9534. */
  9535. if (__intel_pageflip_stall_check(dev, crtc)) {
  9536. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9537. page_flip_completed(intel_crtc);
  9538. } else {
  9539. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9540. spin_unlock_irq(&dev->event_lock);
  9541. drm_crtc_vblank_put(crtc);
  9542. kfree(work);
  9543. return -EBUSY;
  9544. }
  9545. }
  9546. intel_crtc->unpin_work = work;
  9547. spin_unlock_irq(&dev->event_lock);
  9548. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9549. flush_workqueue(dev_priv->wq);
  9550. /* Reference the objects for the scheduled work. */
  9551. drm_framebuffer_reference(work->old_fb);
  9552. drm_gem_object_reference(&obj->base);
  9553. crtc->primary->fb = fb;
  9554. update_state_fb(crtc->primary);
  9555. work->pending_flip_obj = obj;
  9556. ret = i915_mutex_lock_interruptible(dev);
  9557. if (ret)
  9558. goto cleanup;
  9559. atomic_inc(&intel_crtc->unpin_work_count);
  9560. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9561. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9562. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9563. if (IS_VALLEYVIEW(dev)) {
  9564. ring = &dev_priv->ring[BCS];
  9565. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9566. /* vlv: DISPLAY_FLIP fails to change tiling */
  9567. ring = NULL;
  9568. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9569. ring = &dev_priv->ring[BCS];
  9570. } else if (INTEL_INFO(dev)->gen >= 7) {
  9571. ring = i915_gem_request_get_ring(obj->last_write_req);
  9572. if (ring == NULL || ring->id != RCS)
  9573. ring = &dev_priv->ring[BCS];
  9574. } else {
  9575. ring = &dev_priv->ring[RCS];
  9576. }
  9577. mmio_flip = use_mmio_flip(ring, obj);
  9578. /* When using CS flips, we want to emit semaphores between rings.
  9579. * However, when using mmio flips we will create a task to do the
  9580. * synchronisation, so all we want here is to pin the framebuffer
  9581. * into the display plane and skip any waits.
  9582. */
  9583. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9584. crtc->primary->state,
  9585. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
  9586. if (ret)
  9587. goto cleanup_pending;
  9588. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9589. + intel_crtc->dspaddr_offset;
  9590. if (mmio_flip) {
  9591. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9592. page_flip_flags);
  9593. if (ret)
  9594. goto cleanup_unpin;
  9595. i915_gem_request_assign(&work->flip_queued_req,
  9596. obj->last_write_req);
  9597. } else {
  9598. if (!request) {
  9599. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9600. if (ret)
  9601. goto cleanup_unpin;
  9602. }
  9603. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9604. page_flip_flags);
  9605. if (ret)
  9606. goto cleanup_unpin;
  9607. i915_gem_request_assign(&work->flip_queued_req, request);
  9608. }
  9609. if (request)
  9610. i915_add_request_no_flush(request);
  9611. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9612. work->enable_stall_check = true;
  9613. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9614. to_intel_plane(primary)->frontbuffer_bit);
  9615. mutex_unlock(&dev->struct_mutex);
  9616. intel_fbc_disable_crtc(intel_crtc);
  9617. intel_frontbuffer_flip_prepare(dev,
  9618. to_intel_plane(primary)->frontbuffer_bit);
  9619. trace_i915_flip_request(intel_crtc->plane, obj);
  9620. return 0;
  9621. cleanup_unpin:
  9622. intel_unpin_fb_obj(fb, crtc->primary->state);
  9623. cleanup_pending:
  9624. if (request)
  9625. i915_gem_request_cancel(request);
  9626. atomic_dec(&intel_crtc->unpin_work_count);
  9627. mutex_unlock(&dev->struct_mutex);
  9628. cleanup:
  9629. crtc->primary->fb = old_fb;
  9630. update_state_fb(crtc->primary);
  9631. drm_gem_object_unreference_unlocked(&obj->base);
  9632. drm_framebuffer_unreference(work->old_fb);
  9633. spin_lock_irq(&dev->event_lock);
  9634. intel_crtc->unpin_work = NULL;
  9635. spin_unlock_irq(&dev->event_lock);
  9636. drm_crtc_vblank_put(crtc);
  9637. free_work:
  9638. kfree(work);
  9639. if (ret == -EIO) {
  9640. struct drm_atomic_state *state;
  9641. struct drm_plane_state *plane_state;
  9642. out_hang:
  9643. state = drm_atomic_state_alloc(dev);
  9644. if (!state)
  9645. return -ENOMEM;
  9646. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9647. retry:
  9648. plane_state = drm_atomic_get_plane_state(state, primary);
  9649. ret = PTR_ERR_OR_ZERO(plane_state);
  9650. if (!ret) {
  9651. drm_atomic_set_fb_for_plane(plane_state, fb);
  9652. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9653. if (!ret)
  9654. ret = drm_atomic_commit(state);
  9655. }
  9656. if (ret == -EDEADLK) {
  9657. drm_modeset_backoff(state->acquire_ctx);
  9658. drm_atomic_state_clear(state);
  9659. goto retry;
  9660. }
  9661. if (ret)
  9662. drm_atomic_state_free(state);
  9663. if (ret == 0 && event) {
  9664. spin_lock_irq(&dev->event_lock);
  9665. drm_send_vblank_event(dev, pipe, event);
  9666. spin_unlock_irq(&dev->event_lock);
  9667. }
  9668. }
  9669. return ret;
  9670. }
  9671. /**
  9672. * intel_wm_need_update - Check whether watermarks need updating
  9673. * @plane: drm plane
  9674. * @state: new plane state
  9675. *
  9676. * Check current plane state versus the new one to determine whether
  9677. * watermarks need to be recalculated.
  9678. *
  9679. * Returns true or false.
  9680. */
  9681. static bool intel_wm_need_update(struct drm_plane *plane,
  9682. struct drm_plane_state *state)
  9683. {
  9684. /* Update watermarks on tiling changes. */
  9685. if (!plane->state->fb || !state->fb ||
  9686. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9687. plane->state->rotation != state->rotation)
  9688. return true;
  9689. if (plane->state->crtc_w != state->crtc_w)
  9690. return true;
  9691. return false;
  9692. }
  9693. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9694. struct drm_plane_state *plane_state)
  9695. {
  9696. struct drm_crtc *crtc = crtc_state->crtc;
  9697. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9698. struct drm_plane *plane = plane_state->plane;
  9699. struct drm_device *dev = crtc->dev;
  9700. struct drm_i915_private *dev_priv = dev->dev_private;
  9701. struct intel_plane_state *old_plane_state =
  9702. to_intel_plane_state(plane->state);
  9703. int idx = intel_crtc->base.base.id, ret;
  9704. int i = drm_plane_index(plane);
  9705. bool mode_changed = needs_modeset(crtc_state);
  9706. bool was_crtc_enabled = crtc->state->active;
  9707. bool is_crtc_enabled = crtc_state->active;
  9708. bool turn_off, turn_on, visible, was_visible;
  9709. struct drm_framebuffer *fb = plane_state->fb;
  9710. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9711. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9712. ret = skl_update_scaler_plane(
  9713. to_intel_crtc_state(crtc_state),
  9714. to_intel_plane_state(plane_state));
  9715. if (ret)
  9716. return ret;
  9717. }
  9718. /*
  9719. * Disabling a plane is always okay; we just need to update
  9720. * fb tracking in a special way since cleanup_fb() won't
  9721. * get called by the plane helpers.
  9722. */
  9723. if (old_plane_state->base.fb && !fb)
  9724. intel_crtc->atomic.disabled_planes |= 1 << i;
  9725. was_visible = old_plane_state->visible;
  9726. visible = to_intel_plane_state(plane_state)->visible;
  9727. if (!was_crtc_enabled && WARN_ON(was_visible))
  9728. was_visible = false;
  9729. if (!is_crtc_enabled && WARN_ON(visible))
  9730. visible = false;
  9731. if (!was_visible && !visible)
  9732. return 0;
  9733. turn_off = was_visible && (!visible || mode_changed);
  9734. turn_on = visible && (!was_visible || mode_changed);
  9735. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9736. plane->base.id, fb ? fb->base.id : -1);
  9737. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9738. plane->base.id, was_visible, visible,
  9739. turn_off, turn_on, mode_changed);
  9740. if (turn_on) {
  9741. intel_crtc->atomic.update_wm_pre = true;
  9742. /* must disable cxsr around plane enable/disable */
  9743. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9744. intel_crtc->atomic.disable_cxsr = true;
  9745. /* to potentially re-enable cxsr */
  9746. intel_crtc->atomic.wait_vblank = true;
  9747. intel_crtc->atomic.update_wm_post = true;
  9748. }
  9749. } else if (turn_off) {
  9750. intel_crtc->atomic.update_wm_post = true;
  9751. /* must disable cxsr around plane enable/disable */
  9752. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9753. if (is_crtc_enabled)
  9754. intel_crtc->atomic.wait_vblank = true;
  9755. intel_crtc->atomic.disable_cxsr = true;
  9756. }
  9757. } else if (intel_wm_need_update(plane, plane_state)) {
  9758. intel_crtc->atomic.update_wm_pre = true;
  9759. }
  9760. if (visible)
  9761. intel_crtc->atomic.fb_bits |=
  9762. to_intel_plane(plane)->frontbuffer_bit;
  9763. switch (plane->type) {
  9764. case DRM_PLANE_TYPE_PRIMARY:
  9765. intel_crtc->atomic.wait_for_flips = true;
  9766. intel_crtc->atomic.pre_disable_primary = turn_off;
  9767. intel_crtc->atomic.post_enable_primary = turn_on;
  9768. if (turn_off) {
  9769. /*
  9770. * FIXME: Actually if we will still have any other
  9771. * plane enabled on the pipe we could let IPS enabled
  9772. * still, but for now lets consider that when we make
  9773. * primary invisible by setting DSPCNTR to 0 on
  9774. * update_primary_plane function IPS needs to be
  9775. * disable.
  9776. */
  9777. intel_crtc->atomic.disable_ips = true;
  9778. intel_crtc->atomic.disable_fbc = true;
  9779. }
  9780. /*
  9781. * FBC does not work on some platforms for rotated
  9782. * planes, so disable it when rotation is not 0 and
  9783. * update it when rotation is set back to 0.
  9784. *
  9785. * FIXME: This is redundant with the fbc update done in
  9786. * the primary plane enable function except that that
  9787. * one is done too late. We eventually need to unify
  9788. * this.
  9789. */
  9790. if (visible &&
  9791. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9792. dev_priv->fbc.crtc == intel_crtc &&
  9793. plane_state->rotation != BIT(DRM_ROTATE_0))
  9794. intel_crtc->atomic.disable_fbc = true;
  9795. /*
  9796. * BDW signals flip done immediately if the plane
  9797. * is disabled, even if the plane enable is already
  9798. * armed to occur at the next vblank :(
  9799. */
  9800. if (turn_on && IS_BROADWELL(dev))
  9801. intel_crtc->atomic.wait_vblank = true;
  9802. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9803. break;
  9804. case DRM_PLANE_TYPE_CURSOR:
  9805. break;
  9806. case DRM_PLANE_TYPE_OVERLAY:
  9807. if (turn_off && !mode_changed) {
  9808. intel_crtc->atomic.wait_vblank = true;
  9809. intel_crtc->atomic.update_sprite_watermarks |=
  9810. 1 << i;
  9811. }
  9812. }
  9813. return 0;
  9814. }
  9815. static bool encoders_cloneable(const struct intel_encoder *a,
  9816. const struct intel_encoder *b)
  9817. {
  9818. /* masks could be asymmetric, so check both ways */
  9819. return a == b || (a->cloneable & (1 << b->type) &&
  9820. b->cloneable & (1 << a->type));
  9821. }
  9822. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9823. struct intel_crtc *crtc,
  9824. struct intel_encoder *encoder)
  9825. {
  9826. struct intel_encoder *source_encoder;
  9827. struct drm_connector *connector;
  9828. struct drm_connector_state *connector_state;
  9829. int i;
  9830. for_each_connector_in_state(state, connector, connector_state, i) {
  9831. if (connector_state->crtc != &crtc->base)
  9832. continue;
  9833. source_encoder =
  9834. to_intel_encoder(connector_state->best_encoder);
  9835. if (!encoders_cloneable(encoder, source_encoder))
  9836. return false;
  9837. }
  9838. return true;
  9839. }
  9840. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9841. struct intel_crtc *crtc)
  9842. {
  9843. struct intel_encoder *encoder;
  9844. struct drm_connector *connector;
  9845. struct drm_connector_state *connector_state;
  9846. int i;
  9847. for_each_connector_in_state(state, connector, connector_state, i) {
  9848. if (connector_state->crtc != &crtc->base)
  9849. continue;
  9850. encoder = to_intel_encoder(connector_state->best_encoder);
  9851. if (!check_single_encoder_cloning(state, crtc, encoder))
  9852. return false;
  9853. }
  9854. return true;
  9855. }
  9856. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9857. struct drm_crtc_state *crtc_state)
  9858. {
  9859. struct drm_device *dev = crtc->dev;
  9860. struct drm_i915_private *dev_priv = dev->dev_private;
  9861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9862. struct intel_crtc_state *pipe_config =
  9863. to_intel_crtc_state(crtc_state);
  9864. struct drm_atomic_state *state = crtc_state->state;
  9865. int ret;
  9866. bool mode_changed = needs_modeset(crtc_state);
  9867. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9868. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9869. return -EINVAL;
  9870. }
  9871. if (mode_changed && !crtc_state->active)
  9872. intel_crtc->atomic.update_wm_post = true;
  9873. if (mode_changed && crtc_state->enable &&
  9874. dev_priv->display.crtc_compute_clock &&
  9875. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9876. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9877. pipe_config);
  9878. if (ret)
  9879. return ret;
  9880. }
  9881. ret = 0;
  9882. if (INTEL_INFO(dev)->gen >= 9) {
  9883. if (mode_changed)
  9884. ret = skl_update_scaler_crtc(pipe_config);
  9885. if (!ret)
  9886. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9887. pipe_config);
  9888. }
  9889. return ret;
  9890. }
  9891. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9892. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9893. .load_lut = intel_crtc_load_lut,
  9894. .atomic_begin = intel_begin_crtc_commit,
  9895. .atomic_flush = intel_finish_crtc_commit,
  9896. .atomic_check = intel_crtc_atomic_check,
  9897. };
  9898. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9899. {
  9900. struct intel_connector *connector;
  9901. for_each_intel_connector(dev, connector) {
  9902. if (connector->base.encoder) {
  9903. connector->base.state->best_encoder =
  9904. connector->base.encoder;
  9905. connector->base.state->crtc =
  9906. connector->base.encoder->crtc;
  9907. } else {
  9908. connector->base.state->best_encoder = NULL;
  9909. connector->base.state->crtc = NULL;
  9910. }
  9911. }
  9912. }
  9913. static void
  9914. connected_sink_compute_bpp(struct intel_connector *connector,
  9915. struct intel_crtc_state *pipe_config)
  9916. {
  9917. int bpp = pipe_config->pipe_bpp;
  9918. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9919. connector->base.base.id,
  9920. connector->base.name);
  9921. /* Don't use an invalid EDID bpc value */
  9922. if (connector->base.display_info.bpc &&
  9923. connector->base.display_info.bpc * 3 < bpp) {
  9924. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9925. bpp, connector->base.display_info.bpc*3);
  9926. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9927. }
  9928. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9929. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9930. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9931. bpp);
  9932. pipe_config->pipe_bpp = 24;
  9933. }
  9934. }
  9935. static int
  9936. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9937. struct intel_crtc_state *pipe_config)
  9938. {
  9939. struct drm_device *dev = crtc->base.dev;
  9940. struct drm_atomic_state *state;
  9941. struct drm_connector *connector;
  9942. struct drm_connector_state *connector_state;
  9943. int bpp, i;
  9944. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9945. bpp = 10*3;
  9946. else if (INTEL_INFO(dev)->gen >= 5)
  9947. bpp = 12*3;
  9948. else
  9949. bpp = 8*3;
  9950. pipe_config->pipe_bpp = bpp;
  9951. state = pipe_config->base.state;
  9952. /* Clamp display bpp to EDID value */
  9953. for_each_connector_in_state(state, connector, connector_state, i) {
  9954. if (connector_state->crtc != &crtc->base)
  9955. continue;
  9956. connected_sink_compute_bpp(to_intel_connector(connector),
  9957. pipe_config);
  9958. }
  9959. return bpp;
  9960. }
  9961. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9962. {
  9963. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9964. "type: 0x%x flags: 0x%x\n",
  9965. mode->crtc_clock,
  9966. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9967. mode->crtc_hsync_end, mode->crtc_htotal,
  9968. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9969. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9970. }
  9971. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9972. struct intel_crtc_state *pipe_config,
  9973. const char *context)
  9974. {
  9975. struct drm_device *dev = crtc->base.dev;
  9976. struct drm_plane *plane;
  9977. struct intel_plane *intel_plane;
  9978. struct intel_plane_state *state;
  9979. struct drm_framebuffer *fb;
  9980. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9981. context, pipe_config, pipe_name(crtc->pipe));
  9982. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9983. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9984. pipe_config->pipe_bpp, pipe_config->dither);
  9985. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9986. pipe_config->has_pch_encoder,
  9987. pipe_config->fdi_lanes,
  9988. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9989. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9990. pipe_config->fdi_m_n.tu);
  9991. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9992. pipe_config->has_dp_encoder,
  9993. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9994. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9995. pipe_config->dp_m_n.tu);
  9996. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9997. pipe_config->has_dp_encoder,
  9998. pipe_config->dp_m2_n2.gmch_m,
  9999. pipe_config->dp_m2_n2.gmch_n,
  10000. pipe_config->dp_m2_n2.link_m,
  10001. pipe_config->dp_m2_n2.link_n,
  10002. pipe_config->dp_m2_n2.tu);
  10003. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10004. pipe_config->has_audio,
  10005. pipe_config->has_infoframe);
  10006. DRM_DEBUG_KMS("requested mode:\n");
  10007. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10008. DRM_DEBUG_KMS("adjusted mode:\n");
  10009. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10010. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10011. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10012. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10013. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10014. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10015. crtc->num_scalers,
  10016. pipe_config->scaler_state.scaler_users,
  10017. pipe_config->scaler_state.scaler_id);
  10018. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10019. pipe_config->gmch_pfit.control,
  10020. pipe_config->gmch_pfit.pgm_ratios,
  10021. pipe_config->gmch_pfit.lvds_border_bits);
  10022. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10023. pipe_config->pch_pfit.pos,
  10024. pipe_config->pch_pfit.size,
  10025. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10026. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10027. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10028. if (IS_BROXTON(dev)) {
  10029. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10030. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10031. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10032. pipe_config->ddi_pll_sel,
  10033. pipe_config->dpll_hw_state.ebb0,
  10034. pipe_config->dpll_hw_state.ebb4,
  10035. pipe_config->dpll_hw_state.pll0,
  10036. pipe_config->dpll_hw_state.pll1,
  10037. pipe_config->dpll_hw_state.pll2,
  10038. pipe_config->dpll_hw_state.pll3,
  10039. pipe_config->dpll_hw_state.pll6,
  10040. pipe_config->dpll_hw_state.pll8,
  10041. pipe_config->dpll_hw_state.pll9,
  10042. pipe_config->dpll_hw_state.pll10,
  10043. pipe_config->dpll_hw_state.pcsdw12);
  10044. } else if (IS_SKYLAKE(dev)) {
  10045. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10046. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10047. pipe_config->ddi_pll_sel,
  10048. pipe_config->dpll_hw_state.ctrl1,
  10049. pipe_config->dpll_hw_state.cfgcr1,
  10050. pipe_config->dpll_hw_state.cfgcr2);
  10051. } else if (HAS_DDI(dev)) {
  10052. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10053. pipe_config->ddi_pll_sel,
  10054. pipe_config->dpll_hw_state.wrpll);
  10055. } else {
  10056. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10057. "fp0: 0x%x, fp1: 0x%x\n",
  10058. pipe_config->dpll_hw_state.dpll,
  10059. pipe_config->dpll_hw_state.dpll_md,
  10060. pipe_config->dpll_hw_state.fp0,
  10061. pipe_config->dpll_hw_state.fp1);
  10062. }
  10063. DRM_DEBUG_KMS("planes on this crtc\n");
  10064. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10065. intel_plane = to_intel_plane(plane);
  10066. if (intel_plane->pipe != crtc->pipe)
  10067. continue;
  10068. state = to_intel_plane_state(plane->state);
  10069. fb = state->base.fb;
  10070. if (!fb) {
  10071. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10072. "disabled, scaler_id = %d\n",
  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), state->scaler_id);
  10077. continue;
  10078. }
  10079. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10080. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10081. plane->base.id, intel_plane->pipe,
  10082. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10083. drm_plane_index(plane));
  10084. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10085. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10086. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10087. state->scaler_id,
  10088. state->src.x1 >> 16, state->src.y1 >> 16,
  10089. drm_rect_width(&state->src) >> 16,
  10090. drm_rect_height(&state->src) >> 16,
  10091. state->dst.x1, state->dst.y1,
  10092. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10093. }
  10094. }
  10095. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10096. {
  10097. struct drm_device *dev = state->dev;
  10098. struct intel_encoder *encoder;
  10099. struct drm_connector *connector;
  10100. struct drm_connector_state *connector_state;
  10101. unsigned int used_ports = 0;
  10102. int i;
  10103. /*
  10104. * Walk the connector list instead of the encoder
  10105. * list to detect the problem on ddi platforms
  10106. * where there's just one encoder per digital port.
  10107. */
  10108. for_each_connector_in_state(state, connector, connector_state, i) {
  10109. if (!connector_state->best_encoder)
  10110. continue;
  10111. encoder = to_intel_encoder(connector_state->best_encoder);
  10112. WARN_ON(!connector_state->crtc);
  10113. switch (encoder->type) {
  10114. unsigned int port_mask;
  10115. case INTEL_OUTPUT_UNKNOWN:
  10116. if (WARN_ON(!HAS_DDI(dev)))
  10117. break;
  10118. case INTEL_OUTPUT_DISPLAYPORT:
  10119. case INTEL_OUTPUT_HDMI:
  10120. case INTEL_OUTPUT_EDP:
  10121. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10122. /* the same port mustn't appear more than once */
  10123. if (used_ports & port_mask)
  10124. return false;
  10125. used_ports |= port_mask;
  10126. default:
  10127. break;
  10128. }
  10129. }
  10130. return true;
  10131. }
  10132. static void
  10133. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10134. {
  10135. struct drm_crtc_state tmp_state;
  10136. struct intel_crtc_scaler_state scaler_state;
  10137. struct intel_dpll_hw_state dpll_hw_state;
  10138. enum intel_dpll_id shared_dpll;
  10139. uint32_t ddi_pll_sel;
  10140. bool force_thru;
  10141. /* FIXME: before the switch to atomic started, a new pipe_config was
  10142. * kzalloc'd. Code that depends on any field being zero should be
  10143. * fixed, so that the crtc_state can be safely duplicated. For now,
  10144. * only fields that are know to not cause problems are preserved. */
  10145. tmp_state = crtc_state->base;
  10146. scaler_state = crtc_state->scaler_state;
  10147. shared_dpll = crtc_state->shared_dpll;
  10148. dpll_hw_state = crtc_state->dpll_hw_state;
  10149. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10150. force_thru = crtc_state->pch_pfit.force_thru;
  10151. memset(crtc_state, 0, sizeof *crtc_state);
  10152. crtc_state->base = tmp_state;
  10153. crtc_state->scaler_state = scaler_state;
  10154. crtc_state->shared_dpll = shared_dpll;
  10155. crtc_state->dpll_hw_state = dpll_hw_state;
  10156. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10157. crtc_state->pch_pfit.force_thru = force_thru;
  10158. }
  10159. static int
  10160. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10161. struct intel_crtc_state *pipe_config)
  10162. {
  10163. struct drm_atomic_state *state = pipe_config->base.state;
  10164. struct intel_encoder *encoder;
  10165. struct drm_connector *connector;
  10166. struct drm_connector_state *connector_state;
  10167. int base_bpp, ret = -EINVAL;
  10168. int i;
  10169. bool retry = true;
  10170. clear_intel_crtc_state(pipe_config);
  10171. pipe_config->cpu_transcoder =
  10172. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10173. /*
  10174. * Sanitize sync polarity flags based on requested ones. If neither
  10175. * positive or negative polarity is requested, treat this as meaning
  10176. * negative polarity.
  10177. */
  10178. if (!(pipe_config->base.adjusted_mode.flags &
  10179. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10180. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10181. if (!(pipe_config->base.adjusted_mode.flags &
  10182. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10183. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10184. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10185. * plane pixel format and any sink constraints into account. Returns the
  10186. * source plane bpp so that dithering can be selected on mismatches
  10187. * after encoders and crtc also have had their say. */
  10188. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10189. pipe_config);
  10190. if (base_bpp < 0)
  10191. goto fail;
  10192. /*
  10193. * Determine the real pipe dimensions. Note that stereo modes can
  10194. * increase the actual pipe size due to the frame doubling and
  10195. * insertion of additional space for blanks between the frame. This
  10196. * is stored in the crtc timings. We use the requested mode to do this
  10197. * computation to clearly distinguish it from the adjusted mode, which
  10198. * can be changed by the connectors in the below retry loop.
  10199. */
  10200. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10201. &pipe_config->pipe_src_w,
  10202. &pipe_config->pipe_src_h);
  10203. encoder_retry:
  10204. /* Ensure the port clock defaults are reset when retrying. */
  10205. pipe_config->port_clock = 0;
  10206. pipe_config->pixel_multiplier = 1;
  10207. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10208. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10209. CRTC_STEREO_DOUBLE);
  10210. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10211. * adjust it according to limitations or connector properties, and also
  10212. * a chance to reject the mode entirely.
  10213. */
  10214. for_each_connector_in_state(state, connector, connector_state, i) {
  10215. if (connector_state->crtc != crtc)
  10216. continue;
  10217. encoder = to_intel_encoder(connector_state->best_encoder);
  10218. if (!(encoder->compute_config(encoder, pipe_config))) {
  10219. DRM_DEBUG_KMS("Encoder config failure\n");
  10220. goto fail;
  10221. }
  10222. }
  10223. /* Set default port clock if not overwritten by the encoder. Needs to be
  10224. * done afterwards in case the encoder adjusts the mode. */
  10225. if (!pipe_config->port_clock)
  10226. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10227. * pipe_config->pixel_multiplier;
  10228. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10229. if (ret < 0) {
  10230. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10231. goto fail;
  10232. }
  10233. if (ret == RETRY) {
  10234. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10235. ret = -EINVAL;
  10236. goto fail;
  10237. }
  10238. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10239. retry = false;
  10240. goto encoder_retry;
  10241. }
  10242. /* Dithering seems to not pass-through bits correctly when it should, so
  10243. * only enable it on 6bpc panels. */
  10244. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10245. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10246. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10247. fail:
  10248. return ret;
  10249. }
  10250. static void
  10251. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10252. {
  10253. struct drm_crtc *crtc;
  10254. struct drm_crtc_state *crtc_state;
  10255. int i;
  10256. /* Double check state. */
  10257. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10258. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10259. /* Update hwmode for vblank functions */
  10260. if (crtc->state->active)
  10261. crtc->hwmode = crtc->state->adjusted_mode;
  10262. else
  10263. crtc->hwmode.crtc_clock = 0;
  10264. }
  10265. }
  10266. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10267. {
  10268. int diff;
  10269. if (clock1 == clock2)
  10270. return true;
  10271. if (!clock1 || !clock2)
  10272. return false;
  10273. diff = abs(clock1 - clock2);
  10274. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10275. return true;
  10276. return false;
  10277. }
  10278. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10279. list_for_each_entry((intel_crtc), \
  10280. &(dev)->mode_config.crtc_list, \
  10281. base.head) \
  10282. if (mask & (1 <<(intel_crtc)->pipe))
  10283. static bool
  10284. intel_compare_m_n(unsigned int m, unsigned int n,
  10285. unsigned int m2, unsigned int n2,
  10286. bool exact)
  10287. {
  10288. if (m == m2 && n == n2)
  10289. return true;
  10290. if (exact || !m || !n || !m2 || !n2)
  10291. return false;
  10292. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10293. if (m > m2) {
  10294. while (m > m2) {
  10295. m2 <<= 1;
  10296. n2 <<= 1;
  10297. }
  10298. } else if (m < m2) {
  10299. while (m < m2) {
  10300. m <<= 1;
  10301. n <<= 1;
  10302. }
  10303. }
  10304. return m == m2 && n == n2;
  10305. }
  10306. static bool
  10307. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10308. struct intel_link_m_n *m2_n2,
  10309. bool adjust)
  10310. {
  10311. if (m_n->tu == m2_n2->tu &&
  10312. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10313. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10314. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10315. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10316. if (adjust)
  10317. *m2_n2 = *m_n;
  10318. return true;
  10319. }
  10320. return false;
  10321. }
  10322. static bool
  10323. intel_pipe_config_compare(struct drm_device *dev,
  10324. struct intel_crtc_state *current_config,
  10325. struct intel_crtc_state *pipe_config,
  10326. bool adjust)
  10327. {
  10328. bool ret = true;
  10329. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10330. do { \
  10331. if (!adjust) \
  10332. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10333. else \
  10334. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10335. } while (0)
  10336. #define PIPE_CONF_CHECK_X(name) \
  10337. if (current_config->name != pipe_config->name) { \
  10338. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10339. "(expected 0x%08x, found 0x%08x)\n", \
  10340. current_config->name, \
  10341. pipe_config->name); \
  10342. ret = false; \
  10343. }
  10344. #define PIPE_CONF_CHECK_I(name) \
  10345. if (current_config->name != pipe_config->name) { \
  10346. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10347. "(expected %i, found %i)\n", \
  10348. current_config->name, \
  10349. pipe_config->name); \
  10350. ret = false; \
  10351. }
  10352. #define PIPE_CONF_CHECK_M_N(name) \
  10353. if (!intel_compare_link_m_n(&current_config->name, \
  10354. &pipe_config->name,\
  10355. adjust)) { \
  10356. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10357. "(expected tu %i gmch %i/%i link %i/%i, " \
  10358. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10359. current_config->name.tu, \
  10360. current_config->name.gmch_m, \
  10361. current_config->name.gmch_n, \
  10362. current_config->name.link_m, \
  10363. current_config->name.link_n, \
  10364. pipe_config->name.tu, \
  10365. pipe_config->name.gmch_m, \
  10366. pipe_config->name.gmch_n, \
  10367. pipe_config->name.link_m, \
  10368. pipe_config->name.link_n); \
  10369. ret = false; \
  10370. }
  10371. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10372. if (!intel_compare_link_m_n(&current_config->name, \
  10373. &pipe_config->name, adjust) && \
  10374. !intel_compare_link_m_n(&current_config->alt_name, \
  10375. &pipe_config->name, adjust)) { \
  10376. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10377. "(expected tu %i gmch %i/%i link %i/%i, " \
  10378. "or tu %i gmch %i/%i link %i/%i, " \
  10379. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10380. current_config->name.tu, \
  10381. current_config->name.gmch_m, \
  10382. current_config->name.gmch_n, \
  10383. current_config->name.link_m, \
  10384. current_config->name.link_n, \
  10385. current_config->alt_name.tu, \
  10386. current_config->alt_name.gmch_m, \
  10387. current_config->alt_name.gmch_n, \
  10388. current_config->alt_name.link_m, \
  10389. current_config->alt_name.link_n, \
  10390. pipe_config->name.tu, \
  10391. pipe_config->name.gmch_m, \
  10392. pipe_config->name.gmch_n, \
  10393. pipe_config->name.link_m, \
  10394. pipe_config->name.link_n); \
  10395. ret = false; \
  10396. }
  10397. /* This is required for BDW+ where there is only one set of registers for
  10398. * switching between high and low RR.
  10399. * This macro can be used whenever a comparison has to be made between one
  10400. * hw state and multiple sw state variables.
  10401. */
  10402. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10403. if ((current_config->name != pipe_config->name) && \
  10404. (current_config->alt_name != pipe_config->name)) { \
  10405. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10406. "(expected %i or %i, found %i)\n", \
  10407. current_config->name, \
  10408. current_config->alt_name, \
  10409. pipe_config->name); \
  10410. ret = false; \
  10411. }
  10412. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10413. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10414. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10415. "(expected %i, found %i)\n", \
  10416. current_config->name & (mask), \
  10417. pipe_config->name & (mask)); \
  10418. ret = false; \
  10419. }
  10420. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10421. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10422. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10423. "(expected %i, found %i)\n", \
  10424. current_config->name, \
  10425. pipe_config->name); \
  10426. ret = false; \
  10427. }
  10428. #define PIPE_CONF_QUIRK(quirk) \
  10429. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10430. PIPE_CONF_CHECK_I(cpu_transcoder);
  10431. PIPE_CONF_CHECK_I(has_pch_encoder);
  10432. PIPE_CONF_CHECK_I(fdi_lanes);
  10433. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10434. PIPE_CONF_CHECK_I(has_dp_encoder);
  10435. if (INTEL_INFO(dev)->gen < 8) {
  10436. PIPE_CONF_CHECK_M_N(dp_m_n);
  10437. PIPE_CONF_CHECK_I(has_drrs);
  10438. if (current_config->has_drrs)
  10439. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10440. } else
  10441. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10442. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10443. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10444. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10445. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10446. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10447. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10448. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10449. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10450. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10451. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10452. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10453. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10454. PIPE_CONF_CHECK_I(pixel_multiplier);
  10455. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10456. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10457. IS_VALLEYVIEW(dev))
  10458. PIPE_CONF_CHECK_I(limited_color_range);
  10459. PIPE_CONF_CHECK_I(has_infoframe);
  10460. PIPE_CONF_CHECK_I(has_audio);
  10461. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10462. DRM_MODE_FLAG_INTERLACE);
  10463. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10464. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10465. DRM_MODE_FLAG_PHSYNC);
  10466. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10467. DRM_MODE_FLAG_NHSYNC);
  10468. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10469. DRM_MODE_FLAG_PVSYNC);
  10470. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10471. DRM_MODE_FLAG_NVSYNC);
  10472. }
  10473. PIPE_CONF_CHECK_I(pipe_src_w);
  10474. PIPE_CONF_CHECK_I(pipe_src_h);
  10475. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10476. /* pfit ratios are autocomputed by the hw on gen4+ */
  10477. if (INTEL_INFO(dev)->gen < 4)
  10478. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10479. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10480. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10481. if (current_config->pch_pfit.enabled) {
  10482. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10483. PIPE_CONF_CHECK_I(pch_pfit.size);
  10484. }
  10485. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10486. /* BDW+ don't expose a synchronous way to read the state */
  10487. if (IS_HASWELL(dev))
  10488. PIPE_CONF_CHECK_I(ips_enabled);
  10489. PIPE_CONF_CHECK_I(double_wide);
  10490. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10491. PIPE_CONF_CHECK_I(shared_dpll);
  10492. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10493. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10494. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10495. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10496. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10497. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10498. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10499. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10500. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10501. PIPE_CONF_CHECK_I(pipe_bpp);
  10502. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10503. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10504. #undef PIPE_CONF_CHECK_X
  10505. #undef PIPE_CONF_CHECK_I
  10506. #undef PIPE_CONF_CHECK_I_ALT
  10507. #undef PIPE_CONF_CHECK_FLAGS
  10508. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10509. #undef PIPE_CONF_QUIRK
  10510. #undef INTEL_ERR_OR_DBG_KMS
  10511. return ret;
  10512. }
  10513. static void check_wm_state(struct drm_device *dev)
  10514. {
  10515. struct drm_i915_private *dev_priv = dev->dev_private;
  10516. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10517. struct intel_crtc *intel_crtc;
  10518. int plane;
  10519. if (INTEL_INFO(dev)->gen < 9)
  10520. return;
  10521. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10522. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10523. for_each_intel_crtc(dev, intel_crtc) {
  10524. struct skl_ddb_entry *hw_entry, *sw_entry;
  10525. const enum pipe pipe = intel_crtc->pipe;
  10526. if (!intel_crtc->active)
  10527. continue;
  10528. /* planes */
  10529. for_each_plane(dev_priv, pipe, plane) {
  10530. hw_entry = &hw_ddb.plane[pipe][plane];
  10531. sw_entry = &sw_ddb->plane[pipe][plane];
  10532. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10533. continue;
  10534. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10535. "(expected (%u,%u), found (%u,%u))\n",
  10536. pipe_name(pipe), plane + 1,
  10537. sw_entry->start, sw_entry->end,
  10538. hw_entry->start, hw_entry->end);
  10539. }
  10540. /* cursor */
  10541. hw_entry = &hw_ddb.cursor[pipe];
  10542. sw_entry = &sw_ddb->cursor[pipe];
  10543. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10544. continue;
  10545. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10546. "(expected (%u,%u), found (%u,%u))\n",
  10547. pipe_name(pipe),
  10548. sw_entry->start, sw_entry->end,
  10549. hw_entry->start, hw_entry->end);
  10550. }
  10551. }
  10552. static void
  10553. check_connector_state(struct drm_device *dev,
  10554. struct drm_atomic_state *old_state)
  10555. {
  10556. struct drm_connector_state *old_conn_state;
  10557. struct drm_connector *connector;
  10558. int i;
  10559. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10560. struct drm_encoder *encoder = connector->encoder;
  10561. struct drm_connector_state *state = connector->state;
  10562. /* This also checks the encoder/connector hw state with the
  10563. * ->get_hw_state callbacks. */
  10564. intel_connector_check_state(to_intel_connector(connector));
  10565. I915_STATE_WARN(state->best_encoder != encoder,
  10566. "connector's atomic encoder doesn't match legacy encoder\n");
  10567. }
  10568. }
  10569. static void
  10570. check_encoder_state(struct drm_device *dev)
  10571. {
  10572. struct intel_encoder *encoder;
  10573. struct intel_connector *connector;
  10574. for_each_intel_encoder(dev, encoder) {
  10575. bool enabled = false;
  10576. enum pipe pipe;
  10577. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10578. encoder->base.base.id,
  10579. encoder->base.name);
  10580. for_each_intel_connector(dev, connector) {
  10581. if (connector->base.state->best_encoder != &encoder->base)
  10582. continue;
  10583. enabled = true;
  10584. I915_STATE_WARN(connector->base.state->crtc !=
  10585. encoder->base.crtc,
  10586. "connector's crtc doesn't match encoder crtc\n");
  10587. }
  10588. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10589. "encoder's enabled state mismatch "
  10590. "(expected %i, found %i)\n",
  10591. !!encoder->base.crtc, enabled);
  10592. if (!encoder->base.crtc) {
  10593. bool active;
  10594. active = encoder->get_hw_state(encoder, &pipe);
  10595. I915_STATE_WARN(active,
  10596. "encoder detached but still enabled on pipe %c.\n",
  10597. pipe_name(pipe));
  10598. }
  10599. }
  10600. }
  10601. static void
  10602. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10603. {
  10604. struct drm_i915_private *dev_priv = dev->dev_private;
  10605. struct intel_encoder *encoder;
  10606. struct drm_crtc_state *old_crtc_state;
  10607. struct drm_crtc *crtc;
  10608. int i;
  10609. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10610. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10611. struct intel_crtc_state *pipe_config, *sw_config;
  10612. bool active;
  10613. if (!needs_modeset(crtc->state))
  10614. continue;
  10615. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10616. pipe_config = to_intel_crtc_state(old_crtc_state);
  10617. memset(pipe_config, 0, sizeof(*pipe_config));
  10618. pipe_config->base.crtc = crtc;
  10619. pipe_config->base.state = old_state;
  10620. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10621. crtc->base.id);
  10622. active = dev_priv->display.get_pipe_config(intel_crtc,
  10623. pipe_config);
  10624. /* hw state is inconsistent with the pipe quirk */
  10625. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10626. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10627. active = crtc->state->active;
  10628. I915_STATE_WARN(crtc->state->active != active,
  10629. "crtc active state doesn't match with hw state "
  10630. "(expected %i, found %i)\n", crtc->state->active, active);
  10631. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10632. "transitional active state does not match atomic hw state "
  10633. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10634. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10635. enum pipe pipe;
  10636. active = encoder->get_hw_state(encoder, &pipe);
  10637. I915_STATE_WARN(active != crtc->state->active,
  10638. "[ENCODER:%i] active %i with crtc active %i\n",
  10639. encoder->base.base.id, active, crtc->state->active);
  10640. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10641. "Encoder connected to wrong pipe %c\n",
  10642. pipe_name(pipe));
  10643. if (active)
  10644. encoder->get_config(encoder, pipe_config);
  10645. }
  10646. if (!crtc->state->active)
  10647. continue;
  10648. sw_config = to_intel_crtc_state(crtc->state);
  10649. if (!intel_pipe_config_compare(dev, sw_config,
  10650. pipe_config, false)) {
  10651. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10652. intel_dump_pipe_config(intel_crtc, pipe_config,
  10653. "[hw state]");
  10654. intel_dump_pipe_config(intel_crtc, sw_config,
  10655. "[sw state]");
  10656. }
  10657. }
  10658. }
  10659. static void
  10660. check_shared_dpll_state(struct drm_device *dev)
  10661. {
  10662. struct drm_i915_private *dev_priv = dev->dev_private;
  10663. struct intel_crtc *crtc;
  10664. struct intel_dpll_hw_state dpll_hw_state;
  10665. int i;
  10666. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10667. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10668. int enabled_crtcs = 0, active_crtcs = 0;
  10669. bool active;
  10670. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10671. DRM_DEBUG_KMS("%s\n", pll->name);
  10672. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10673. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10674. "more active pll users than references: %i vs %i\n",
  10675. pll->active, hweight32(pll->config.crtc_mask));
  10676. I915_STATE_WARN(pll->active && !pll->on,
  10677. "pll in active use but not on in sw tracking\n");
  10678. I915_STATE_WARN(pll->on && !pll->active,
  10679. "pll in on but not on in use in sw tracking\n");
  10680. I915_STATE_WARN(pll->on != active,
  10681. "pll on state mismatch (expected %i, found %i)\n",
  10682. pll->on, active);
  10683. for_each_intel_crtc(dev, crtc) {
  10684. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10685. enabled_crtcs++;
  10686. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10687. active_crtcs++;
  10688. }
  10689. I915_STATE_WARN(pll->active != active_crtcs,
  10690. "pll active crtcs mismatch (expected %i, found %i)\n",
  10691. pll->active, active_crtcs);
  10692. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10693. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10694. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10695. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10696. sizeof(dpll_hw_state)),
  10697. "pll hw state mismatch\n");
  10698. }
  10699. }
  10700. static void
  10701. intel_modeset_check_state(struct drm_device *dev,
  10702. struct drm_atomic_state *old_state)
  10703. {
  10704. check_wm_state(dev);
  10705. check_connector_state(dev, old_state);
  10706. check_encoder_state(dev);
  10707. check_crtc_state(dev, old_state);
  10708. check_shared_dpll_state(dev);
  10709. }
  10710. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10711. int dotclock)
  10712. {
  10713. /*
  10714. * FDI already provided one idea for the dotclock.
  10715. * Yell if the encoder disagrees.
  10716. */
  10717. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10718. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10719. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10720. }
  10721. static void update_scanline_offset(struct intel_crtc *crtc)
  10722. {
  10723. struct drm_device *dev = crtc->base.dev;
  10724. /*
  10725. * The scanline counter increments at the leading edge of hsync.
  10726. *
  10727. * On most platforms it starts counting from vtotal-1 on the
  10728. * first active line. That means the scanline counter value is
  10729. * always one less than what we would expect. Ie. just after
  10730. * start of vblank, which also occurs at start of hsync (on the
  10731. * last active line), the scanline counter will read vblank_start-1.
  10732. *
  10733. * On gen2 the scanline counter starts counting from 1 instead
  10734. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10735. * to keep the value positive), instead of adding one.
  10736. *
  10737. * On HSW+ the behaviour of the scanline counter depends on the output
  10738. * type. For DP ports it behaves like most other platforms, but on HDMI
  10739. * there's an extra 1 line difference. So we need to add two instead of
  10740. * one to the value.
  10741. */
  10742. if (IS_GEN2(dev)) {
  10743. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10744. int vtotal;
  10745. vtotal = mode->crtc_vtotal;
  10746. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10747. vtotal /= 2;
  10748. crtc->scanline_offset = vtotal - 1;
  10749. } else if (HAS_DDI(dev) &&
  10750. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10751. crtc->scanline_offset = 2;
  10752. } else
  10753. crtc->scanline_offset = 1;
  10754. }
  10755. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10756. {
  10757. struct drm_device *dev = state->dev;
  10758. struct drm_i915_private *dev_priv = to_i915(dev);
  10759. struct intel_shared_dpll_config *shared_dpll = NULL;
  10760. struct intel_crtc *intel_crtc;
  10761. struct intel_crtc_state *intel_crtc_state;
  10762. struct drm_crtc *crtc;
  10763. struct drm_crtc_state *crtc_state;
  10764. int i;
  10765. if (!dev_priv->display.crtc_compute_clock)
  10766. return;
  10767. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10768. int dpll;
  10769. intel_crtc = to_intel_crtc(crtc);
  10770. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10771. dpll = intel_crtc_state->shared_dpll;
  10772. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10773. continue;
  10774. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10775. if (!shared_dpll)
  10776. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10777. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10778. }
  10779. }
  10780. /*
  10781. * This implements the workaround described in the "notes" section of the mode
  10782. * set sequence documentation. When going from no pipes or single pipe to
  10783. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10784. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10785. */
  10786. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10787. {
  10788. struct drm_crtc_state *crtc_state;
  10789. struct intel_crtc *intel_crtc;
  10790. struct drm_crtc *crtc;
  10791. struct intel_crtc_state *first_crtc_state = NULL;
  10792. struct intel_crtc_state *other_crtc_state = NULL;
  10793. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10794. int i;
  10795. /* look at all crtc's that are going to be enabled in during modeset */
  10796. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10797. intel_crtc = to_intel_crtc(crtc);
  10798. if (!crtc_state->active || !needs_modeset(crtc_state))
  10799. continue;
  10800. if (first_crtc_state) {
  10801. other_crtc_state = to_intel_crtc_state(crtc_state);
  10802. break;
  10803. } else {
  10804. first_crtc_state = to_intel_crtc_state(crtc_state);
  10805. first_pipe = intel_crtc->pipe;
  10806. }
  10807. }
  10808. /* No workaround needed? */
  10809. if (!first_crtc_state)
  10810. return 0;
  10811. /* w/a possibly needed, check how many crtc's are already enabled. */
  10812. for_each_intel_crtc(state->dev, intel_crtc) {
  10813. struct intel_crtc_state *pipe_config;
  10814. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10815. if (IS_ERR(pipe_config))
  10816. return PTR_ERR(pipe_config);
  10817. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10818. if (!pipe_config->base.active ||
  10819. needs_modeset(&pipe_config->base))
  10820. continue;
  10821. /* 2 or more enabled crtcs means no need for w/a */
  10822. if (enabled_pipe != INVALID_PIPE)
  10823. return 0;
  10824. enabled_pipe = intel_crtc->pipe;
  10825. }
  10826. if (enabled_pipe != INVALID_PIPE)
  10827. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10828. else if (other_crtc_state)
  10829. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10830. return 0;
  10831. }
  10832. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10833. {
  10834. struct drm_crtc *crtc;
  10835. struct drm_crtc_state *crtc_state;
  10836. int ret = 0;
  10837. /* add all active pipes to the state */
  10838. for_each_crtc(state->dev, crtc) {
  10839. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10840. if (IS_ERR(crtc_state))
  10841. return PTR_ERR(crtc_state);
  10842. if (!crtc_state->active || needs_modeset(crtc_state))
  10843. continue;
  10844. crtc_state->mode_changed = true;
  10845. ret = drm_atomic_add_affected_connectors(state, crtc);
  10846. if (ret)
  10847. break;
  10848. ret = drm_atomic_add_affected_planes(state, crtc);
  10849. if (ret)
  10850. break;
  10851. }
  10852. return ret;
  10853. }
  10854. static int intel_modeset_checks(struct drm_atomic_state *state)
  10855. {
  10856. struct drm_device *dev = state->dev;
  10857. struct drm_i915_private *dev_priv = dev->dev_private;
  10858. int ret;
  10859. if (!check_digital_port_conflicts(state)) {
  10860. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10861. return -EINVAL;
  10862. }
  10863. /*
  10864. * See if the config requires any additional preparation, e.g.
  10865. * to adjust global state with pipes off. We need to do this
  10866. * here so we can get the modeset_pipe updated config for the new
  10867. * mode set on this crtc. For other crtcs we need to use the
  10868. * adjusted_mode bits in the crtc directly.
  10869. */
  10870. if (dev_priv->display.modeset_calc_cdclk) {
  10871. unsigned int cdclk;
  10872. ret = dev_priv->display.modeset_calc_cdclk(state);
  10873. cdclk = to_intel_atomic_state(state)->cdclk;
  10874. if (!ret && cdclk != dev_priv->cdclk_freq)
  10875. ret = intel_modeset_all_pipes(state);
  10876. if (ret < 0)
  10877. return ret;
  10878. } else
  10879. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10880. intel_modeset_clear_plls(state);
  10881. if (IS_HASWELL(dev))
  10882. return haswell_mode_set_planes_workaround(state);
  10883. return 0;
  10884. }
  10885. /**
  10886. * intel_atomic_check - validate state object
  10887. * @dev: drm device
  10888. * @state: state to validate
  10889. */
  10890. static int intel_atomic_check(struct drm_device *dev,
  10891. struct drm_atomic_state *state)
  10892. {
  10893. struct drm_crtc *crtc;
  10894. struct drm_crtc_state *crtc_state;
  10895. int ret, i;
  10896. bool any_ms = false;
  10897. ret = drm_atomic_helper_check_modeset(dev, state);
  10898. if (ret)
  10899. return ret;
  10900. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10901. struct intel_crtc_state *pipe_config =
  10902. to_intel_crtc_state(crtc_state);
  10903. /* Catch I915_MODE_FLAG_INHERITED */
  10904. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10905. crtc_state->mode_changed = true;
  10906. if (!crtc_state->enable) {
  10907. if (needs_modeset(crtc_state))
  10908. any_ms = true;
  10909. continue;
  10910. }
  10911. if (!needs_modeset(crtc_state))
  10912. continue;
  10913. /* FIXME: For only active_changed we shouldn't need to do any
  10914. * state recomputation at all. */
  10915. ret = drm_atomic_add_affected_connectors(state, crtc);
  10916. if (ret)
  10917. return ret;
  10918. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10919. if (ret)
  10920. return ret;
  10921. if (i915.fastboot &&
  10922. intel_pipe_config_compare(state->dev,
  10923. to_intel_crtc_state(crtc->state),
  10924. pipe_config, true)) {
  10925. crtc_state->mode_changed = false;
  10926. }
  10927. if (needs_modeset(crtc_state)) {
  10928. any_ms = true;
  10929. ret = drm_atomic_add_affected_planes(state, crtc);
  10930. if (ret)
  10931. return ret;
  10932. }
  10933. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10934. needs_modeset(crtc_state) ?
  10935. "[modeset]" : "[fastset]");
  10936. }
  10937. if (any_ms) {
  10938. ret = intel_modeset_checks(state);
  10939. if (ret)
  10940. return ret;
  10941. } else
  10942. to_intel_atomic_state(state)->cdclk =
  10943. to_i915(state->dev)->cdclk_freq;
  10944. return drm_atomic_helper_check_planes(state->dev, state);
  10945. }
  10946. /**
  10947. * intel_atomic_commit - commit validated state object
  10948. * @dev: DRM device
  10949. * @state: the top-level driver state object
  10950. * @async: asynchronous commit
  10951. *
  10952. * This function commits a top-level state object that has been validated
  10953. * with drm_atomic_helper_check().
  10954. *
  10955. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  10956. * we can only handle plane-related operations and do not yet support
  10957. * asynchronous commit.
  10958. *
  10959. * RETURNS
  10960. * Zero for success or -errno.
  10961. */
  10962. static int intel_atomic_commit(struct drm_device *dev,
  10963. struct drm_atomic_state *state,
  10964. bool async)
  10965. {
  10966. struct drm_i915_private *dev_priv = dev->dev_private;
  10967. struct drm_crtc *crtc;
  10968. struct drm_crtc_state *crtc_state;
  10969. int ret = 0;
  10970. int i;
  10971. bool any_ms = false;
  10972. if (async) {
  10973. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  10974. return -EINVAL;
  10975. }
  10976. ret = drm_atomic_helper_prepare_planes(dev, state);
  10977. if (ret)
  10978. return ret;
  10979. drm_atomic_helper_swap_state(dev, state);
  10980. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10981. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10982. if (!needs_modeset(crtc->state))
  10983. continue;
  10984. any_ms = true;
  10985. intel_pre_plane_update(intel_crtc);
  10986. if (crtc_state->active) {
  10987. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  10988. dev_priv->display.crtc_disable(crtc);
  10989. intel_crtc->active = false;
  10990. intel_disable_shared_dpll(intel_crtc);
  10991. }
  10992. }
  10993. /* Only after disabling all output pipelines that will be changed can we
  10994. * update the the output configuration. */
  10995. intel_modeset_update_crtc_state(state);
  10996. if (any_ms) {
  10997. intel_shared_dpll_commit(state);
  10998. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10999. modeset_update_crtc_power_domains(state);
  11000. }
  11001. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11002. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11003. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11004. bool modeset = needs_modeset(crtc->state);
  11005. if (modeset && crtc->state->active) {
  11006. update_scanline_offset(to_intel_crtc(crtc));
  11007. dev_priv->display.crtc_enable(crtc);
  11008. }
  11009. if (!modeset)
  11010. intel_pre_plane_update(intel_crtc);
  11011. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11012. intel_post_plane_update(intel_crtc);
  11013. }
  11014. /* FIXME: add subpixel order */
  11015. drm_atomic_helper_wait_for_vblanks(dev, state);
  11016. drm_atomic_helper_cleanup_planes(dev, state);
  11017. if (any_ms)
  11018. intel_modeset_check_state(dev, state);
  11019. drm_atomic_state_free(state);
  11020. return 0;
  11021. }
  11022. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11023. {
  11024. struct drm_device *dev = crtc->dev;
  11025. struct drm_atomic_state *state;
  11026. struct drm_crtc_state *crtc_state;
  11027. int ret;
  11028. state = drm_atomic_state_alloc(dev);
  11029. if (!state) {
  11030. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11031. crtc->base.id);
  11032. return;
  11033. }
  11034. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11035. retry:
  11036. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11037. ret = PTR_ERR_OR_ZERO(crtc_state);
  11038. if (!ret) {
  11039. if (!crtc_state->active)
  11040. goto out;
  11041. crtc_state->mode_changed = true;
  11042. ret = drm_atomic_commit(state);
  11043. }
  11044. if (ret == -EDEADLK) {
  11045. drm_atomic_state_clear(state);
  11046. drm_modeset_backoff(state->acquire_ctx);
  11047. goto retry;
  11048. }
  11049. if (ret)
  11050. out:
  11051. drm_atomic_state_free(state);
  11052. }
  11053. #undef for_each_intel_crtc_masked
  11054. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11055. .gamma_set = intel_crtc_gamma_set,
  11056. .set_config = drm_atomic_helper_set_config,
  11057. .destroy = intel_crtc_destroy,
  11058. .page_flip = intel_crtc_page_flip,
  11059. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11060. .atomic_destroy_state = intel_crtc_destroy_state,
  11061. };
  11062. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11063. struct intel_shared_dpll *pll,
  11064. struct intel_dpll_hw_state *hw_state)
  11065. {
  11066. uint32_t val;
  11067. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11068. return false;
  11069. val = I915_READ(PCH_DPLL(pll->id));
  11070. hw_state->dpll = val;
  11071. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11072. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11073. return val & DPLL_VCO_ENABLE;
  11074. }
  11075. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11076. struct intel_shared_dpll *pll)
  11077. {
  11078. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11079. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11080. }
  11081. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11082. struct intel_shared_dpll *pll)
  11083. {
  11084. /* PCH refclock must be enabled first */
  11085. ibx_assert_pch_refclk_enabled(dev_priv);
  11086. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11087. /* Wait for the clocks to stabilize. */
  11088. POSTING_READ(PCH_DPLL(pll->id));
  11089. udelay(150);
  11090. /* The pixel multiplier can only be updated once the
  11091. * DPLL is enabled and the clocks are stable.
  11092. *
  11093. * So write it again.
  11094. */
  11095. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11096. POSTING_READ(PCH_DPLL(pll->id));
  11097. udelay(200);
  11098. }
  11099. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11100. struct intel_shared_dpll *pll)
  11101. {
  11102. struct drm_device *dev = dev_priv->dev;
  11103. struct intel_crtc *crtc;
  11104. /* Make sure no transcoder isn't still depending on us. */
  11105. for_each_intel_crtc(dev, crtc) {
  11106. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11107. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11108. }
  11109. I915_WRITE(PCH_DPLL(pll->id), 0);
  11110. POSTING_READ(PCH_DPLL(pll->id));
  11111. udelay(200);
  11112. }
  11113. static char *ibx_pch_dpll_names[] = {
  11114. "PCH DPLL A",
  11115. "PCH DPLL B",
  11116. };
  11117. static void ibx_pch_dpll_init(struct drm_device *dev)
  11118. {
  11119. struct drm_i915_private *dev_priv = dev->dev_private;
  11120. int i;
  11121. dev_priv->num_shared_dpll = 2;
  11122. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11123. dev_priv->shared_dplls[i].id = i;
  11124. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11125. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11126. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11127. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11128. dev_priv->shared_dplls[i].get_hw_state =
  11129. ibx_pch_dpll_get_hw_state;
  11130. }
  11131. }
  11132. static void intel_shared_dpll_init(struct drm_device *dev)
  11133. {
  11134. struct drm_i915_private *dev_priv = dev->dev_private;
  11135. intel_update_cdclk(dev);
  11136. if (HAS_DDI(dev))
  11137. intel_ddi_pll_init(dev);
  11138. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11139. ibx_pch_dpll_init(dev);
  11140. else
  11141. dev_priv->num_shared_dpll = 0;
  11142. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11143. }
  11144. /**
  11145. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11146. * @plane: drm plane to prepare for
  11147. * @fb: framebuffer to prepare for presentation
  11148. *
  11149. * Prepares a framebuffer for usage on a display plane. Generally this
  11150. * involves pinning the underlying object and updating the frontbuffer tracking
  11151. * bits. Some older platforms need special physical address handling for
  11152. * cursor planes.
  11153. *
  11154. * Returns 0 on success, negative error code on failure.
  11155. */
  11156. int
  11157. intel_prepare_plane_fb(struct drm_plane *plane,
  11158. struct drm_framebuffer *fb,
  11159. const struct drm_plane_state *new_state)
  11160. {
  11161. struct drm_device *dev = plane->dev;
  11162. struct intel_plane *intel_plane = to_intel_plane(plane);
  11163. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11164. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11165. int ret = 0;
  11166. if (!obj)
  11167. return 0;
  11168. mutex_lock(&dev->struct_mutex);
  11169. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11170. INTEL_INFO(dev)->cursor_needs_physical) {
  11171. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11172. ret = i915_gem_object_attach_phys(obj, align);
  11173. if (ret)
  11174. DRM_DEBUG_KMS("failed to attach phys object\n");
  11175. } else {
  11176. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
  11177. }
  11178. if (ret == 0)
  11179. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11180. mutex_unlock(&dev->struct_mutex);
  11181. return ret;
  11182. }
  11183. /**
  11184. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11185. * @plane: drm plane to clean up for
  11186. * @fb: old framebuffer that was on plane
  11187. *
  11188. * Cleans up a framebuffer that has just been removed from a plane.
  11189. */
  11190. void
  11191. intel_cleanup_plane_fb(struct drm_plane *plane,
  11192. struct drm_framebuffer *fb,
  11193. const struct drm_plane_state *old_state)
  11194. {
  11195. struct drm_device *dev = plane->dev;
  11196. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11197. if (WARN_ON(!obj))
  11198. return;
  11199. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11200. !INTEL_INFO(dev)->cursor_needs_physical) {
  11201. mutex_lock(&dev->struct_mutex);
  11202. intel_unpin_fb_obj(fb, old_state);
  11203. mutex_unlock(&dev->struct_mutex);
  11204. }
  11205. }
  11206. int
  11207. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11208. {
  11209. int max_scale;
  11210. struct drm_device *dev;
  11211. struct drm_i915_private *dev_priv;
  11212. int crtc_clock, cdclk;
  11213. if (!intel_crtc || !crtc_state)
  11214. return DRM_PLANE_HELPER_NO_SCALING;
  11215. dev = intel_crtc->base.dev;
  11216. dev_priv = dev->dev_private;
  11217. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11218. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11219. if (!crtc_clock || !cdclk)
  11220. return DRM_PLANE_HELPER_NO_SCALING;
  11221. /*
  11222. * skl max scale is lower of:
  11223. * close to 3 but not 3, -1 is for that purpose
  11224. * or
  11225. * cdclk/crtc_clock
  11226. */
  11227. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11228. return max_scale;
  11229. }
  11230. static int
  11231. intel_check_primary_plane(struct drm_plane *plane,
  11232. struct intel_crtc_state *crtc_state,
  11233. struct intel_plane_state *state)
  11234. {
  11235. struct drm_crtc *crtc = state->base.crtc;
  11236. struct drm_framebuffer *fb = state->base.fb;
  11237. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11238. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11239. bool can_position = false;
  11240. /* use scaler when colorkey is not required */
  11241. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11242. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11243. min_scale = 1;
  11244. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11245. can_position = true;
  11246. }
  11247. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11248. &state->dst, &state->clip,
  11249. min_scale, max_scale,
  11250. can_position, true,
  11251. &state->visible);
  11252. }
  11253. static void
  11254. intel_commit_primary_plane(struct drm_plane *plane,
  11255. struct intel_plane_state *state)
  11256. {
  11257. struct drm_crtc *crtc = state->base.crtc;
  11258. struct drm_framebuffer *fb = state->base.fb;
  11259. struct drm_device *dev = plane->dev;
  11260. struct drm_i915_private *dev_priv = dev->dev_private;
  11261. struct intel_crtc *intel_crtc;
  11262. struct drm_rect *src = &state->src;
  11263. crtc = crtc ? crtc : plane->crtc;
  11264. intel_crtc = to_intel_crtc(crtc);
  11265. plane->fb = fb;
  11266. crtc->x = src->x1 >> 16;
  11267. crtc->y = src->y1 >> 16;
  11268. if (!crtc->state->active)
  11269. return;
  11270. if (state->visible)
  11271. /* FIXME: kill this fastboot hack */
  11272. intel_update_pipe_size(intel_crtc);
  11273. dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
  11274. }
  11275. static void
  11276. intel_disable_primary_plane(struct drm_plane *plane,
  11277. struct drm_crtc *crtc)
  11278. {
  11279. struct drm_device *dev = plane->dev;
  11280. struct drm_i915_private *dev_priv = dev->dev_private;
  11281. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11282. }
  11283. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11284. struct drm_crtc_state *old_crtc_state)
  11285. {
  11286. struct drm_device *dev = crtc->dev;
  11287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11288. if (intel_crtc->atomic.update_wm_pre)
  11289. intel_update_watermarks(crtc);
  11290. /* Perform vblank evasion around commit operation */
  11291. if (crtc->state->active)
  11292. intel_pipe_update_start(intel_crtc, &intel_crtc->start_vbl_count);
  11293. if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
  11294. skl_detach_scalers(intel_crtc);
  11295. }
  11296. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11297. struct drm_crtc_state *old_crtc_state)
  11298. {
  11299. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11300. if (crtc->state->active)
  11301. intel_pipe_update_end(intel_crtc, intel_crtc->start_vbl_count);
  11302. }
  11303. /**
  11304. * intel_plane_destroy - destroy a plane
  11305. * @plane: plane to destroy
  11306. *
  11307. * Common destruction function for all types of planes (primary, cursor,
  11308. * sprite).
  11309. */
  11310. void intel_plane_destroy(struct drm_plane *plane)
  11311. {
  11312. struct intel_plane *intel_plane = to_intel_plane(plane);
  11313. drm_plane_cleanup(plane);
  11314. kfree(intel_plane);
  11315. }
  11316. const struct drm_plane_funcs intel_plane_funcs = {
  11317. .update_plane = drm_atomic_helper_update_plane,
  11318. .disable_plane = drm_atomic_helper_disable_plane,
  11319. .destroy = intel_plane_destroy,
  11320. .set_property = drm_atomic_helper_plane_set_property,
  11321. .atomic_get_property = intel_plane_atomic_get_property,
  11322. .atomic_set_property = intel_plane_atomic_set_property,
  11323. .atomic_duplicate_state = intel_plane_duplicate_state,
  11324. .atomic_destroy_state = intel_plane_destroy_state,
  11325. };
  11326. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11327. int pipe)
  11328. {
  11329. struct intel_plane *primary;
  11330. struct intel_plane_state *state;
  11331. const uint32_t *intel_primary_formats;
  11332. unsigned int num_formats;
  11333. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11334. if (primary == NULL)
  11335. return NULL;
  11336. state = intel_create_plane_state(&primary->base);
  11337. if (!state) {
  11338. kfree(primary);
  11339. return NULL;
  11340. }
  11341. primary->base.state = &state->base;
  11342. primary->can_scale = false;
  11343. primary->max_downscale = 1;
  11344. if (INTEL_INFO(dev)->gen >= 9) {
  11345. primary->can_scale = true;
  11346. state->scaler_id = -1;
  11347. }
  11348. primary->pipe = pipe;
  11349. primary->plane = pipe;
  11350. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11351. primary->check_plane = intel_check_primary_plane;
  11352. primary->commit_plane = intel_commit_primary_plane;
  11353. primary->disable_plane = intel_disable_primary_plane;
  11354. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11355. primary->plane = !pipe;
  11356. if (INTEL_INFO(dev)->gen >= 9) {
  11357. intel_primary_formats = skl_primary_formats;
  11358. num_formats = ARRAY_SIZE(skl_primary_formats);
  11359. } else if (INTEL_INFO(dev)->gen >= 4) {
  11360. intel_primary_formats = i965_primary_formats;
  11361. num_formats = ARRAY_SIZE(i965_primary_formats);
  11362. } else {
  11363. intel_primary_formats = i8xx_primary_formats;
  11364. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11365. }
  11366. drm_universal_plane_init(dev, &primary->base, 0,
  11367. &intel_plane_funcs,
  11368. intel_primary_formats, num_formats,
  11369. DRM_PLANE_TYPE_PRIMARY);
  11370. if (INTEL_INFO(dev)->gen >= 4)
  11371. intel_create_rotation_property(dev, primary);
  11372. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11373. return &primary->base;
  11374. }
  11375. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11376. {
  11377. if (!dev->mode_config.rotation_property) {
  11378. unsigned long flags = BIT(DRM_ROTATE_0) |
  11379. BIT(DRM_ROTATE_180);
  11380. if (INTEL_INFO(dev)->gen >= 9)
  11381. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11382. dev->mode_config.rotation_property =
  11383. drm_mode_create_rotation_property(dev, flags);
  11384. }
  11385. if (dev->mode_config.rotation_property)
  11386. drm_object_attach_property(&plane->base.base,
  11387. dev->mode_config.rotation_property,
  11388. plane->base.state->rotation);
  11389. }
  11390. static int
  11391. intel_check_cursor_plane(struct drm_plane *plane,
  11392. struct intel_crtc_state *crtc_state,
  11393. struct intel_plane_state *state)
  11394. {
  11395. struct drm_crtc *crtc = crtc_state->base.crtc;
  11396. struct drm_framebuffer *fb = state->base.fb;
  11397. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11398. unsigned stride;
  11399. int ret;
  11400. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11401. &state->dst, &state->clip,
  11402. DRM_PLANE_HELPER_NO_SCALING,
  11403. DRM_PLANE_HELPER_NO_SCALING,
  11404. true, true, &state->visible);
  11405. if (ret)
  11406. return ret;
  11407. /* if we want to turn off the cursor ignore width and height */
  11408. if (!obj)
  11409. return 0;
  11410. /* Check for which cursor types we support */
  11411. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11412. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11413. state->base.crtc_w, state->base.crtc_h);
  11414. return -EINVAL;
  11415. }
  11416. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11417. if (obj->base.size < stride * state->base.crtc_h) {
  11418. DRM_DEBUG_KMS("buffer is too small\n");
  11419. return -ENOMEM;
  11420. }
  11421. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11422. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11423. return -EINVAL;
  11424. }
  11425. return 0;
  11426. }
  11427. static void
  11428. intel_disable_cursor_plane(struct drm_plane *plane,
  11429. struct drm_crtc *crtc)
  11430. {
  11431. intel_crtc_update_cursor(crtc, false);
  11432. }
  11433. static void
  11434. intel_commit_cursor_plane(struct drm_plane *plane,
  11435. struct intel_plane_state *state)
  11436. {
  11437. struct drm_crtc *crtc = state->base.crtc;
  11438. struct drm_device *dev = plane->dev;
  11439. struct intel_crtc *intel_crtc;
  11440. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11441. uint32_t addr;
  11442. crtc = crtc ? crtc : plane->crtc;
  11443. intel_crtc = to_intel_crtc(crtc);
  11444. plane->fb = state->base.fb;
  11445. crtc->cursor_x = state->base.crtc_x;
  11446. crtc->cursor_y = state->base.crtc_y;
  11447. if (intel_crtc->cursor_bo == obj)
  11448. goto update;
  11449. if (!obj)
  11450. addr = 0;
  11451. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11452. addr = i915_gem_obj_ggtt_offset(obj);
  11453. else
  11454. addr = obj->phys_handle->busaddr;
  11455. intel_crtc->cursor_addr = addr;
  11456. intel_crtc->cursor_bo = obj;
  11457. update:
  11458. if (crtc->state->active)
  11459. intel_crtc_update_cursor(crtc, state->visible);
  11460. }
  11461. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11462. int pipe)
  11463. {
  11464. struct intel_plane *cursor;
  11465. struct intel_plane_state *state;
  11466. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11467. if (cursor == NULL)
  11468. return NULL;
  11469. state = intel_create_plane_state(&cursor->base);
  11470. if (!state) {
  11471. kfree(cursor);
  11472. return NULL;
  11473. }
  11474. cursor->base.state = &state->base;
  11475. cursor->can_scale = false;
  11476. cursor->max_downscale = 1;
  11477. cursor->pipe = pipe;
  11478. cursor->plane = pipe;
  11479. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11480. cursor->check_plane = intel_check_cursor_plane;
  11481. cursor->commit_plane = intel_commit_cursor_plane;
  11482. cursor->disable_plane = intel_disable_cursor_plane;
  11483. drm_universal_plane_init(dev, &cursor->base, 0,
  11484. &intel_plane_funcs,
  11485. intel_cursor_formats,
  11486. ARRAY_SIZE(intel_cursor_formats),
  11487. DRM_PLANE_TYPE_CURSOR);
  11488. if (INTEL_INFO(dev)->gen >= 4) {
  11489. if (!dev->mode_config.rotation_property)
  11490. dev->mode_config.rotation_property =
  11491. drm_mode_create_rotation_property(dev,
  11492. BIT(DRM_ROTATE_0) |
  11493. BIT(DRM_ROTATE_180));
  11494. if (dev->mode_config.rotation_property)
  11495. drm_object_attach_property(&cursor->base.base,
  11496. dev->mode_config.rotation_property,
  11497. state->base.rotation);
  11498. }
  11499. if (INTEL_INFO(dev)->gen >=9)
  11500. state->scaler_id = -1;
  11501. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11502. return &cursor->base;
  11503. }
  11504. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11505. struct intel_crtc_state *crtc_state)
  11506. {
  11507. int i;
  11508. struct intel_scaler *intel_scaler;
  11509. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11510. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11511. intel_scaler = &scaler_state->scalers[i];
  11512. intel_scaler->in_use = 0;
  11513. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11514. }
  11515. scaler_state->scaler_id = -1;
  11516. }
  11517. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11518. {
  11519. struct drm_i915_private *dev_priv = dev->dev_private;
  11520. struct intel_crtc *intel_crtc;
  11521. struct intel_crtc_state *crtc_state = NULL;
  11522. struct drm_plane *primary = NULL;
  11523. struct drm_plane *cursor = NULL;
  11524. int i, ret;
  11525. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11526. if (intel_crtc == NULL)
  11527. return;
  11528. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11529. if (!crtc_state)
  11530. goto fail;
  11531. intel_crtc->config = crtc_state;
  11532. intel_crtc->base.state = &crtc_state->base;
  11533. crtc_state->base.crtc = &intel_crtc->base;
  11534. /* initialize shared scalers */
  11535. if (INTEL_INFO(dev)->gen >= 9) {
  11536. if (pipe == PIPE_C)
  11537. intel_crtc->num_scalers = 1;
  11538. else
  11539. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11540. skl_init_scalers(dev, intel_crtc, crtc_state);
  11541. }
  11542. primary = intel_primary_plane_create(dev, pipe);
  11543. if (!primary)
  11544. goto fail;
  11545. cursor = intel_cursor_plane_create(dev, pipe);
  11546. if (!cursor)
  11547. goto fail;
  11548. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11549. cursor, &intel_crtc_funcs);
  11550. if (ret)
  11551. goto fail;
  11552. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11553. for (i = 0; i < 256; i++) {
  11554. intel_crtc->lut_r[i] = i;
  11555. intel_crtc->lut_g[i] = i;
  11556. intel_crtc->lut_b[i] = i;
  11557. }
  11558. /*
  11559. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11560. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11561. */
  11562. intel_crtc->pipe = pipe;
  11563. intel_crtc->plane = pipe;
  11564. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11565. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11566. intel_crtc->plane = !pipe;
  11567. }
  11568. intel_crtc->cursor_base = ~0;
  11569. intel_crtc->cursor_cntl = ~0;
  11570. intel_crtc->cursor_size = ~0;
  11571. intel_crtc->wm.cxsr_allowed = true;
  11572. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11573. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11574. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11575. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11576. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11577. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11578. return;
  11579. fail:
  11580. if (primary)
  11581. drm_plane_cleanup(primary);
  11582. if (cursor)
  11583. drm_plane_cleanup(cursor);
  11584. kfree(crtc_state);
  11585. kfree(intel_crtc);
  11586. }
  11587. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11588. {
  11589. struct drm_encoder *encoder = connector->base.encoder;
  11590. struct drm_device *dev = connector->base.dev;
  11591. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11592. if (!encoder || WARN_ON(!encoder->crtc))
  11593. return INVALID_PIPE;
  11594. return to_intel_crtc(encoder->crtc)->pipe;
  11595. }
  11596. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11597. struct drm_file *file)
  11598. {
  11599. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11600. struct drm_crtc *drmmode_crtc;
  11601. struct intel_crtc *crtc;
  11602. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11603. if (!drmmode_crtc) {
  11604. DRM_ERROR("no such CRTC id\n");
  11605. return -ENOENT;
  11606. }
  11607. crtc = to_intel_crtc(drmmode_crtc);
  11608. pipe_from_crtc_id->pipe = crtc->pipe;
  11609. return 0;
  11610. }
  11611. static int intel_encoder_clones(struct intel_encoder *encoder)
  11612. {
  11613. struct drm_device *dev = encoder->base.dev;
  11614. struct intel_encoder *source_encoder;
  11615. int index_mask = 0;
  11616. int entry = 0;
  11617. for_each_intel_encoder(dev, source_encoder) {
  11618. if (encoders_cloneable(encoder, source_encoder))
  11619. index_mask |= (1 << entry);
  11620. entry++;
  11621. }
  11622. return index_mask;
  11623. }
  11624. static bool has_edp_a(struct drm_device *dev)
  11625. {
  11626. struct drm_i915_private *dev_priv = dev->dev_private;
  11627. if (!IS_MOBILE(dev))
  11628. return false;
  11629. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11630. return false;
  11631. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11632. return false;
  11633. return true;
  11634. }
  11635. static bool intel_crt_present(struct drm_device *dev)
  11636. {
  11637. struct drm_i915_private *dev_priv = dev->dev_private;
  11638. if (INTEL_INFO(dev)->gen >= 9)
  11639. return false;
  11640. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11641. return false;
  11642. if (IS_CHERRYVIEW(dev))
  11643. return false;
  11644. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11645. return false;
  11646. return true;
  11647. }
  11648. static void intel_setup_outputs(struct drm_device *dev)
  11649. {
  11650. struct drm_i915_private *dev_priv = dev->dev_private;
  11651. struct intel_encoder *encoder;
  11652. bool dpd_is_edp = false;
  11653. intel_lvds_init(dev);
  11654. if (intel_crt_present(dev))
  11655. intel_crt_init(dev);
  11656. if (IS_BROXTON(dev)) {
  11657. /*
  11658. * FIXME: Broxton doesn't support port detection via the
  11659. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11660. * detect the ports.
  11661. */
  11662. intel_ddi_init(dev, PORT_A);
  11663. intel_ddi_init(dev, PORT_B);
  11664. intel_ddi_init(dev, PORT_C);
  11665. } else if (HAS_DDI(dev)) {
  11666. int found;
  11667. /*
  11668. * Haswell uses DDI functions to detect digital outputs.
  11669. * On SKL pre-D0 the strap isn't connected, so we assume
  11670. * it's there.
  11671. */
  11672. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11673. /* WaIgnoreDDIAStrap: skl */
  11674. if (found || IS_SKYLAKE(dev))
  11675. intel_ddi_init(dev, PORT_A);
  11676. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11677. * register */
  11678. found = I915_READ(SFUSE_STRAP);
  11679. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11680. intel_ddi_init(dev, PORT_B);
  11681. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11682. intel_ddi_init(dev, PORT_C);
  11683. if (found & SFUSE_STRAP_DDID_DETECTED)
  11684. intel_ddi_init(dev, PORT_D);
  11685. /*
  11686. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11687. */
  11688. if (IS_SKYLAKE(dev) &&
  11689. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11690. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11691. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11692. intel_ddi_init(dev, PORT_E);
  11693. } else if (HAS_PCH_SPLIT(dev)) {
  11694. int found;
  11695. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11696. if (has_edp_a(dev))
  11697. intel_dp_init(dev, DP_A, PORT_A);
  11698. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11699. /* PCH SDVOB multiplex with HDMIB */
  11700. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11701. if (!found)
  11702. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11703. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11704. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11705. }
  11706. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11707. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11708. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11709. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11710. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11711. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11712. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11713. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11714. } else if (IS_VALLEYVIEW(dev)) {
  11715. /*
  11716. * The DP_DETECTED bit is the latched state of the DDC
  11717. * SDA pin at boot. However since eDP doesn't require DDC
  11718. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11719. * eDP ports may have been muxed to an alternate function.
  11720. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11721. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11722. * detect eDP ports.
  11723. */
  11724. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11725. !intel_dp_is_edp(dev, PORT_B))
  11726. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11727. PORT_B);
  11728. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11729. intel_dp_is_edp(dev, PORT_B))
  11730. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11731. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11732. !intel_dp_is_edp(dev, PORT_C))
  11733. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11734. PORT_C);
  11735. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11736. intel_dp_is_edp(dev, PORT_C))
  11737. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11738. if (IS_CHERRYVIEW(dev)) {
  11739. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11740. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11741. PORT_D);
  11742. /* eDP not supported on port D, so don't check VBT */
  11743. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11744. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11745. }
  11746. intel_dsi_init(dev);
  11747. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11748. bool found = false;
  11749. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11750. DRM_DEBUG_KMS("probing SDVOB\n");
  11751. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11752. if (!found && IS_G4X(dev)) {
  11753. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11754. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11755. }
  11756. if (!found && IS_G4X(dev))
  11757. intel_dp_init(dev, DP_B, PORT_B);
  11758. }
  11759. /* Before G4X SDVOC doesn't have its own detect register */
  11760. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11761. DRM_DEBUG_KMS("probing SDVOC\n");
  11762. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11763. }
  11764. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11765. if (IS_G4X(dev)) {
  11766. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11767. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11768. }
  11769. if (IS_G4X(dev))
  11770. intel_dp_init(dev, DP_C, PORT_C);
  11771. }
  11772. if (IS_G4X(dev) &&
  11773. (I915_READ(DP_D) & DP_DETECTED))
  11774. intel_dp_init(dev, DP_D, PORT_D);
  11775. } else if (IS_GEN2(dev))
  11776. intel_dvo_init(dev);
  11777. if (SUPPORTS_TV(dev))
  11778. intel_tv_init(dev);
  11779. intel_psr_init(dev);
  11780. for_each_intel_encoder(dev, encoder) {
  11781. encoder->base.possible_crtcs = encoder->crtc_mask;
  11782. encoder->base.possible_clones =
  11783. intel_encoder_clones(encoder);
  11784. }
  11785. intel_init_pch_refclk(dev);
  11786. drm_helper_move_panel_connectors_to_head(dev);
  11787. }
  11788. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11789. {
  11790. struct drm_device *dev = fb->dev;
  11791. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11792. drm_framebuffer_cleanup(fb);
  11793. mutex_lock(&dev->struct_mutex);
  11794. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11795. drm_gem_object_unreference(&intel_fb->obj->base);
  11796. mutex_unlock(&dev->struct_mutex);
  11797. kfree(intel_fb);
  11798. }
  11799. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11800. struct drm_file *file,
  11801. unsigned int *handle)
  11802. {
  11803. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11804. struct drm_i915_gem_object *obj = intel_fb->obj;
  11805. if (obj->userptr.mm) {
  11806. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11807. return -EINVAL;
  11808. }
  11809. return drm_gem_handle_create(file, &obj->base, handle);
  11810. }
  11811. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11812. struct drm_file *file,
  11813. unsigned flags, unsigned color,
  11814. struct drm_clip_rect *clips,
  11815. unsigned num_clips)
  11816. {
  11817. struct drm_device *dev = fb->dev;
  11818. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11819. struct drm_i915_gem_object *obj = intel_fb->obj;
  11820. mutex_lock(&dev->struct_mutex);
  11821. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11822. mutex_unlock(&dev->struct_mutex);
  11823. return 0;
  11824. }
  11825. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11826. .destroy = intel_user_framebuffer_destroy,
  11827. .create_handle = intel_user_framebuffer_create_handle,
  11828. .dirty = intel_user_framebuffer_dirty,
  11829. };
  11830. static
  11831. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11832. uint32_t pixel_format)
  11833. {
  11834. u32 gen = INTEL_INFO(dev)->gen;
  11835. if (gen >= 9) {
  11836. /* "The stride in bytes must not exceed the of the size of 8K
  11837. * pixels and 32K bytes."
  11838. */
  11839. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11840. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11841. return 32*1024;
  11842. } else if (gen >= 4) {
  11843. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11844. return 16*1024;
  11845. else
  11846. return 32*1024;
  11847. } else if (gen >= 3) {
  11848. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11849. return 8*1024;
  11850. else
  11851. return 16*1024;
  11852. } else {
  11853. /* XXX DSPC is limited to 4k tiled */
  11854. return 8*1024;
  11855. }
  11856. }
  11857. static int intel_framebuffer_init(struct drm_device *dev,
  11858. struct intel_framebuffer *intel_fb,
  11859. struct drm_mode_fb_cmd2 *mode_cmd,
  11860. struct drm_i915_gem_object *obj)
  11861. {
  11862. unsigned int aligned_height;
  11863. int ret;
  11864. u32 pitch_limit, stride_alignment;
  11865. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11866. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11867. /* Enforce that fb modifier and tiling mode match, but only for
  11868. * X-tiled. This is needed for FBC. */
  11869. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11870. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11871. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11872. return -EINVAL;
  11873. }
  11874. } else {
  11875. if (obj->tiling_mode == I915_TILING_X)
  11876. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11877. else if (obj->tiling_mode == I915_TILING_Y) {
  11878. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11879. return -EINVAL;
  11880. }
  11881. }
  11882. /* Passed in modifier sanity checking. */
  11883. switch (mode_cmd->modifier[0]) {
  11884. case I915_FORMAT_MOD_Y_TILED:
  11885. case I915_FORMAT_MOD_Yf_TILED:
  11886. if (INTEL_INFO(dev)->gen < 9) {
  11887. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11888. mode_cmd->modifier[0]);
  11889. return -EINVAL;
  11890. }
  11891. case DRM_FORMAT_MOD_NONE:
  11892. case I915_FORMAT_MOD_X_TILED:
  11893. break;
  11894. default:
  11895. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11896. mode_cmd->modifier[0]);
  11897. return -EINVAL;
  11898. }
  11899. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  11900. mode_cmd->pixel_format);
  11901. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11902. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11903. mode_cmd->pitches[0], stride_alignment);
  11904. return -EINVAL;
  11905. }
  11906. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11907. mode_cmd->pixel_format);
  11908. if (mode_cmd->pitches[0] > pitch_limit) {
  11909. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11910. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11911. "tiled" : "linear",
  11912. mode_cmd->pitches[0], pitch_limit);
  11913. return -EINVAL;
  11914. }
  11915. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11916. mode_cmd->pitches[0] != obj->stride) {
  11917. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11918. mode_cmd->pitches[0], obj->stride);
  11919. return -EINVAL;
  11920. }
  11921. /* Reject formats not supported by any plane early. */
  11922. switch (mode_cmd->pixel_format) {
  11923. case DRM_FORMAT_C8:
  11924. case DRM_FORMAT_RGB565:
  11925. case DRM_FORMAT_XRGB8888:
  11926. case DRM_FORMAT_ARGB8888:
  11927. break;
  11928. case DRM_FORMAT_XRGB1555:
  11929. if (INTEL_INFO(dev)->gen > 3) {
  11930. DRM_DEBUG("unsupported pixel format: %s\n",
  11931. drm_get_format_name(mode_cmd->pixel_format));
  11932. return -EINVAL;
  11933. }
  11934. break;
  11935. case DRM_FORMAT_ABGR8888:
  11936. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  11937. DRM_DEBUG("unsupported pixel format: %s\n",
  11938. drm_get_format_name(mode_cmd->pixel_format));
  11939. return -EINVAL;
  11940. }
  11941. break;
  11942. case DRM_FORMAT_XBGR8888:
  11943. case DRM_FORMAT_XRGB2101010:
  11944. case DRM_FORMAT_XBGR2101010:
  11945. if (INTEL_INFO(dev)->gen < 4) {
  11946. DRM_DEBUG("unsupported pixel format: %s\n",
  11947. drm_get_format_name(mode_cmd->pixel_format));
  11948. return -EINVAL;
  11949. }
  11950. break;
  11951. case DRM_FORMAT_ABGR2101010:
  11952. if (!IS_VALLEYVIEW(dev)) {
  11953. DRM_DEBUG("unsupported pixel format: %s\n",
  11954. drm_get_format_name(mode_cmd->pixel_format));
  11955. return -EINVAL;
  11956. }
  11957. break;
  11958. case DRM_FORMAT_YUYV:
  11959. case DRM_FORMAT_UYVY:
  11960. case DRM_FORMAT_YVYU:
  11961. case DRM_FORMAT_VYUY:
  11962. if (INTEL_INFO(dev)->gen < 5) {
  11963. DRM_DEBUG("unsupported pixel format: %s\n",
  11964. drm_get_format_name(mode_cmd->pixel_format));
  11965. return -EINVAL;
  11966. }
  11967. break;
  11968. default:
  11969. DRM_DEBUG("unsupported pixel format: %s\n",
  11970. drm_get_format_name(mode_cmd->pixel_format));
  11971. return -EINVAL;
  11972. }
  11973. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11974. if (mode_cmd->offsets[0] != 0)
  11975. return -EINVAL;
  11976. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11977. mode_cmd->pixel_format,
  11978. mode_cmd->modifier[0]);
  11979. /* FIXME drm helper for size checks (especially planar formats)? */
  11980. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11981. return -EINVAL;
  11982. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11983. intel_fb->obj = obj;
  11984. intel_fb->obj->framebuffer_references++;
  11985. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  11986. if (ret) {
  11987. DRM_ERROR("framebuffer init failed %d\n", ret);
  11988. return ret;
  11989. }
  11990. return 0;
  11991. }
  11992. static struct drm_framebuffer *
  11993. intel_user_framebuffer_create(struct drm_device *dev,
  11994. struct drm_file *filp,
  11995. struct drm_mode_fb_cmd2 *mode_cmd)
  11996. {
  11997. struct drm_i915_gem_object *obj;
  11998. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  11999. mode_cmd->handles[0]));
  12000. if (&obj->base == NULL)
  12001. return ERR_PTR(-ENOENT);
  12002. return intel_framebuffer_create(dev, mode_cmd, obj);
  12003. }
  12004. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12005. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12006. {
  12007. }
  12008. #endif
  12009. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12010. .fb_create = intel_user_framebuffer_create,
  12011. .output_poll_changed = intel_fbdev_output_poll_changed,
  12012. .atomic_check = intel_atomic_check,
  12013. .atomic_commit = intel_atomic_commit,
  12014. .atomic_state_alloc = intel_atomic_state_alloc,
  12015. .atomic_state_clear = intel_atomic_state_clear,
  12016. };
  12017. /* Set up chip specific display functions */
  12018. static void intel_init_display(struct drm_device *dev)
  12019. {
  12020. struct drm_i915_private *dev_priv = dev->dev_private;
  12021. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12022. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12023. else if (IS_CHERRYVIEW(dev))
  12024. dev_priv->display.find_dpll = chv_find_best_dpll;
  12025. else if (IS_VALLEYVIEW(dev))
  12026. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12027. else if (IS_PINEVIEW(dev))
  12028. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12029. else
  12030. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12031. if (INTEL_INFO(dev)->gen >= 9) {
  12032. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12033. dev_priv->display.get_initial_plane_config =
  12034. skylake_get_initial_plane_config;
  12035. dev_priv->display.crtc_compute_clock =
  12036. haswell_crtc_compute_clock;
  12037. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12038. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12039. dev_priv->display.update_primary_plane =
  12040. skylake_update_primary_plane;
  12041. } else if (HAS_DDI(dev)) {
  12042. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12043. dev_priv->display.get_initial_plane_config =
  12044. ironlake_get_initial_plane_config;
  12045. dev_priv->display.crtc_compute_clock =
  12046. haswell_crtc_compute_clock;
  12047. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12048. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12049. dev_priv->display.update_primary_plane =
  12050. ironlake_update_primary_plane;
  12051. } else if (HAS_PCH_SPLIT(dev)) {
  12052. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12053. dev_priv->display.get_initial_plane_config =
  12054. ironlake_get_initial_plane_config;
  12055. dev_priv->display.crtc_compute_clock =
  12056. ironlake_crtc_compute_clock;
  12057. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12058. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12059. dev_priv->display.update_primary_plane =
  12060. ironlake_update_primary_plane;
  12061. } else if (IS_VALLEYVIEW(dev)) {
  12062. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12063. dev_priv->display.get_initial_plane_config =
  12064. i9xx_get_initial_plane_config;
  12065. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12066. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12067. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12068. dev_priv->display.update_primary_plane =
  12069. i9xx_update_primary_plane;
  12070. } else {
  12071. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12072. dev_priv->display.get_initial_plane_config =
  12073. i9xx_get_initial_plane_config;
  12074. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12075. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12076. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12077. dev_priv->display.update_primary_plane =
  12078. i9xx_update_primary_plane;
  12079. }
  12080. /* Returns the core display clock speed */
  12081. if (IS_SKYLAKE(dev))
  12082. dev_priv->display.get_display_clock_speed =
  12083. skylake_get_display_clock_speed;
  12084. else if (IS_BROXTON(dev))
  12085. dev_priv->display.get_display_clock_speed =
  12086. broxton_get_display_clock_speed;
  12087. else if (IS_BROADWELL(dev))
  12088. dev_priv->display.get_display_clock_speed =
  12089. broadwell_get_display_clock_speed;
  12090. else if (IS_HASWELL(dev))
  12091. dev_priv->display.get_display_clock_speed =
  12092. haswell_get_display_clock_speed;
  12093. else if (IS_VALLEYVIEW(dev))
  12094. dev_priv->display.get_display_clock_speed =
  12095. valleyview_get_display_clock_speed;
  12096. else if (IS_GEN5(dev))
  12097. dev_priv->display.get_display_clock_speed =
  12098. ilk_get_display_clock_speed;
  12099. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12100. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12101. dev_priv->display.get_display_clock_speed =
  12102. i945_get_display_clock_speed;
  12103. else if (IS_GM45(dev))
  12104. dev_priv->display.get_display_clock_speed =
  12105. gm45_get_display_clock_speed;
  12106. else if (IS_CRESTLINE(dev))
  12107. dev_priv->display.get_display_clock_speed =
  12108. i965gm_get_display_clock_speed;
  12109. else if (IS_PINEVIEW(dev))
  12110. dev_priv->display.get_display_clock_speed =
  12111. pnv_get_display_clock_speed;
  12112. else if (IS_G33(dev) || IS_G4X(dev))
  12113. dev_priv->display.get_display_clock_speed =
  12114. g33_get_display_clock_speed;
  12115. else if (IS_I915G(dev))
  12116. dev_priv->display.get_display_clock_speed =
  12117. i915_get_display_clock_speed;
  12118. else if (IS_I945GM(dev) || IS_845G(dev))
  12119. dev_priv->display.get_display_clock_speed =
  12120. i9xx_misc_get_display_clock_speed;
  12121. else if (IS_PINEVIEW(dev))
  12122. dev_priv->display.get_display_clock_speed =
  12123. pnv_get_display_clock_speed;
  12124. else if (IS_I915GM(dev))
  12125. dev_priv->display.get_display_clock_speed =
  12126. i915gm_get_display_clock_speed;
  12127. else if (IS_I865G(dev))
  12128. dev_priv->display.get_display_clock_speed =
  12129. i865_get_display_clock_speed;
  12130. else if (IS_I85X(dev))
  12131. dev_priv->display.get_display_clock_speed =
  12132. i85x_get_display_clock_speed;
  12133. else { /* 830 */
  12134. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12135. dev_priv->display.get_display_clock_speed =
  12136. i830_get_display_clock_speed;
  12137. }
  12138. if (IS_GEN5(dev)) {
  12139. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12140. } else if (IS_GEN6(dev)) {
  12141. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12142. } else if (IS_IVYBRIDGE(dev)) {
  12143. /* FIXME: detect B0+ stepping and use auto training */
  12144. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12145. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12146. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12147. if (IS_BROADWELL(dev)) {
  12148. dev_priv->display.modeset_commit_cdclk =
  12149. broadwell_modeset_commit_cdclk;
  12150. dev_priv->display.modeset_calc_cdclk =
  12151. broadwell_modeset_calc_cdclk;
  12152. }
  12153. } else if (IS_VALLEYVIEW(dev)) {
  12154. dev_priv->display.modeset_commit_cdclk =
  12155. valleyview_modeset_commit_cdclk;
  12156. dev_priv->display.modeset_calc_cdclk =
  12157. valleyview_modeset_calc_cdclk;
  12158. } else if (IS_BROXTON(dev)) {
  12159. dev_priv->display.modeset_commit_cdclk =
  12160. broxton_modeset_commit_cdclk;
  12161. dev_priv->display.modeset_calc_cdclk =
  12162. broxton_modeset_calc_cdclk;
  12163. }
  12164. switch (INTEL_INFO(dev)->gen) {
  12165. case 2:
  12166. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12167. break;
  12168. case 3:
  12169. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12170. break;
  12171. case 4:
  12172. case 5:
  12173. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12174. break;
  12175. case 6:
  12176. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12177. break;
  12178. case 7:
  12179. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12180. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12181. break;
  12182. case 9:
  12183. /* Drop through - unsupported since execlist only. */
  12184. default:
  12185. /* Default just returns -ENODEV to indicate unsupported */
  12186. dev_priv->display.queue_flip = intel_default_queue_flip;
  12187. }
  12188. intel_panel_init_backlight_funcs(dev);
  12189. mutex_init(&dev_priv->pps_mutex);
  12190. }
  12191. /*
  12192. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12193. * resume, or other times. This quirk makes sure that's the case for
  12194. * affected systems.
  12195. */
  12196. static void quirk_pipea_force(struct drm_device *dev)
  12197. {
  12198. struct drm_i915_private *dev_priv = dev->dev_private;
  12199. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12200. DRM_INFO("applying pipe a force quirk\n");
  12201. }
  12202. static void quirk_pipeb_force(struct drm_device *dev)
  12203. {
  12204. struct drm_i915_private *dev_priv = dev->dev_private;
  12205. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12206. DRM_INFO("applying pipe b force quirk\n");
  12207. }
  12208. /*
  12209. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12210. */
  12211. static void quirk_ssc_force_disable(struct drm_device *dev)
  12212. {
  12213. struct drm_i915_private *dev_priv = dev->dev_private;
  12214. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12215. DRM_INFO("applying lvds SSC disable quirk\n");
  12216. }
  12217. /*
  12218. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12219. * brightness value
  12220. */
  12221. static void quirk_invert_brightness(struct drm_device *dev)
  12222. {
  12223. struct drm_i915_private *dev_priv = dev->dev_private;
  12224. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12225. DRM_INFO("applying inverted panel brightness quirk\n");
  12226. }
  12227. /* Some VBT's incorrectly indicate no backlight is present */
  12228. static void quirk_backlight_present(struct drm_device *dev)
  12229. {
  12230. struct drm_i915_private *dev_priv = dev->dev_private;
  12231. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12232. DRM_INFO("applying backlight present quirk\n");
  12233. }
  12234. struct intel_quirk {
  12235. int device;
  12236. int subsystem_vendor;
  12237. int subsystem_device;
  12238. void (*hook)(struct drm_device *dev);
  12239. };
  12240. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12241. struct intel_dmi_quirk {
  12242. void (*hook)(struct drm_device *dev);
  12243. const struct dmi_system_id (*dmi_id_list)[];
  12244. };
  12245. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12246. {
  12247. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12248. return 1;
  12249. }
  12250. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12251. {
  12252. .dmi_id_list = &(const struct dmi_system_id[]) {
  12253. {
  12254. .callback = intel_dmi_reverse_brightness,
  12255. .ident = "NCR Corporation",
  12256. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12257. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12258. },
  12259. },
  12260. { } /* terminating entry */
  12261. },
  12262. .hook = quirk_invert_brightness,
  12263. },
  12264. };
  12265. static struct intel_quirk intel_quirks[] = {
  12266. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12267. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12268. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12269. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12270. /* 830 needs to leave pipe A & dpll A up */
  12271. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12272. /* 830 needs to leave pipe B & dpll B up */
  12273. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12274. /* Lenovo U160 cannot use SSC on LVDS */
  12275. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12276. /* Sony Vaio Y cannot use SSC on LVDS */
  12277. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12278. /* Acer Aspire 5734Z must invert backlight brightness */
  12279. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12280. /* Acer/eMachines G725 */
  12281. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12282. /* Acer/eMachines e725 */
  12283. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12284. /* Acer/Packard Bell NCL20 */
  12285. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12286. /* Acer Aspire 4736Z */
  12287. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12288. /* Acer Aspire 5336 */
  12289. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12290. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12291. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12292. /* Acer C720 Chromebook (Core i3 4005U) */
  12293. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12294. /* Apple Macbook 2,1 (Core 2 T7400) */
  12295. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12296. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12297. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12298. /* HP Chromebook 14 (Celeron 2955U) */
  12299. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12300. /* Dell Chromebook 11 */
  12301. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12302. };
  12303. static void intel_init_quirks(struct drm_device *dev)
  12304. {
  12305. struct pci_dev *d = dev->pdev;
  12306. int i;
  12307. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12308. struct intel_quirk *q = &intel_quirks[i];
  12309. if (d->device == q->device &&
  12310. (d->subsystem_vendor == q->subsystem_vendor ||
  12311. q->subsystem_vendor == PCI_ANY_ID) &&
  12312. (d->subsystem_device == q->subsystem_device ||
  12313. q->subsystem_device == PCI_ANY_ID))
  12314. q->hook(dev);
  12315. }
  12316. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12317. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12318. intel_dmi_quirks[i].hook(dev);
  12319. }
  12320. }
  12321. /* Disable the VGA plane that we never use */
  12322. static void i915_disable_vga(struct drm_device *dev)
  12323. {
  12324. struct drm_i915_private *dev_priv = dev->dev_private;
  12325. u8 sr1;
  12326. u32 vga_reg = i915_vgacntrl_reg(dev);
  12327. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12328. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12329. outb(SR01, VGA_SR_INDEX);
  12330. sr1 = inb(VGA_SR_DATA);
  12331. outb(sr1 | 1<<5, VGA_SR_DATA);
  12332. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12333. udelay(300);
  12334. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12335. POSTING_READ(vga_reg);
  12336. }
  12337. void intel_modeset_init_hw(struct drm_device *dev)
  12338. {
  12339. intel_update_cdclk(dev);
  12340. intel_prepare_ddi(dev);
  12341. intel_init_clock_gating(dev);
  12342. intel_enable_gt_powersave(dev);
  12343. }
  12344. void intel_modeset_init(struct drm_device *dev)
  12345. {
  12346. struct drm_i915_private *dev_priv = dev->dev_private;
  12347. int sprite, ret;
  12348. enum pipe pipe;
  12349. struct intel_crtc *crtc;
  12350. drm_mode_config_init(dev);
  12351. dev->mode_config.min_width = 0;
  12352. dev->mode_config.min_height = 0;
  12353. dev->mode_config.preferred_depth = 24;
  12354. dev->mode_config.prefer_shadow = 1;
  12355. dev->mode_config.allow_fb_modifiers = true;
  12356. dev->mode_config.funcs = &intel_mode_funcs;
  12357. intel_init_quirks(dev);
  12358. intel_init_pm(dev);
  12359. if (INTEL_INFO(dev)->num_pipes == 0)
  12360. return;
  12361. /*
  12362. * There may be no VBT; and if the BIOS enabled SSC we can
  12363. * just keep using it to avoid unnecessary flicker. Whereas if the
  12364. * BIOS isn't using it, don't assume it will work even if the VBT
  12365. * indicates as much.
  12366. */
  12367. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12368. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12369. DREF_SSC1_ENABLE);
  12370. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12371. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12372. bios_lvds_use_ssc ? "en" : "dis",
  12373. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12374. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12375. }
  12376. }
  12377. intel_init_display(dev);
  12378. intel_init_audio(dev);
  12379. if (IS_GEN2(dev)) {
  12380. dev->mode_config.max_width = 2048;
  12381. dev->mode_config.max_height = 2048;
  12382. } else if (IS_GEN3(dev)) {
  12383. dev->mode_config.max_width = 4096;
  12384. dev->mode_config.max_height = 4096;
  12385. } else {
  12386. dev->mode_config.max_width = 8192;
  12387. dev->mode_config.max_height = 8192;
  12388. }
  12389. if (IS_845G(dev) || IS_I865G(dev)) {
  12390. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12391. dev->mode_config.cursor_height = 1023;
  12392. } else if (IS_GEN2(dev)) {
  12393. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12394. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12395. } else {
  12396. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12397. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12398. }
  12399. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12400. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12401. INTEL_INFO(dev)->num_pipes,
  12402. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12403. for_each_pipe(dev_priv, pipe) {
  12404. intel_crtc_init(dev, pipe);
  12405. for_each_sprite(dev_priv, pipe, sprite) {
  12406. ret = intel_plane_init(dev, pipe, sprite);
  12407. if (ret)
  12408. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12409. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12410. }
  12411. }
  12412. intel_init_dpio(dev);
  12413. intel_shared_dpll_init(dev);
  12414. /* Just disable it once at startup */
  12415. i915_disable_vga(dev);
  12416. intel_setup_outputs(dev);
  12417. /* Just in case the BIOS is doing something questionable. */
  12418. intel_fbc_disable(dev_priv);
  12419. drm_modeset_lock_all(dev);
  12420. intel_modeset_setup_hw_state(dev);
  12421. drm_modeset_unlock_all(dev);
  12422. for_each_intel_crtc(dev, crtc) {
  12423. struct intel_initial_plane_config plane_config = {};
  12424. if (!crtc->active)
  12425. continue;
  12426. /*
  12427. * Note that reserving the BIOS fb up front prevents us
  12428. * from stuffing other stolen allocations like the ring
  12429. * on top. This prevents some ugliness at boot time, and
  12430. * can even allow for smooth boot transitions if the BIOS
  12431. * fb is large enough for the active pipe configuration.
  12432. */
  12433. dev_priv->display.get_initial_plane_config(crtc,
  12434. &plane_config);
  12435. /*
  12436. * If the fb is shared between multiple heads, we'll
  12437. * just get the first one.
  12438. */
  12439. intel_find_initial_plane_obj(crtc, &plane_config);
  12440. }
  12441. }
  12442. static void intel_enable_pipe_a(struct drm_device *dev)
  12443. {
  12444. struct intel_connector *connector;
  12445. struct drm_connector *crt = NULL;
  12446. struct intel_load_detect_pipe load_detect_temp;
  12447. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12448. /* We can't just switch on the pipe A, we need to set things up with a
  12449. * proper mode and output configuration. As a gross hack, enable pipe A
  12450. * by enabling the load detect pipe once. */
  12451. for_each_intel_connector(dev, connector) {
  12452. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12453. crt = &connector->base;
  12454. break;
  12455. }
  12456. }
  12457. if (!crt)
  12458. return;
  12459. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12460. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12461. }
  12462. static bool
  12463. intel_check_plane_mapping(struct intel_crtc *crtc)
  12464. {
  12465. struct drm_device *dev = crtc->base.dev;
  12466. struct drm_i915_private *dev_priv = dev->dev_private;
  12467. u32 reg, val;
  12468. if (INTEL_INFO(dev)->num_pipes == 1)
  12469. return true;
  12470. reg = DSPCNTR(!crtc->plane);
  12471. val = I915_READ(reg);
  12472. if ((val & DISPLAY_PLANE_ENABLE) &&
  12473. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12474. return false;
  12475. return true;
  12476. }
  12477. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12478. {
  12479. struct drm_device *dev = crtc->base.dev;
  12480. struct drm_i915_private *dev_priv = dev->dev_private;
  12481. struct intel_encoder *encoder;
  12482. u32 reg;
  12483. bool enable;
  12484. /* Clear any frame start delays used for debugging left by the BIOS */
  12485. reg = PIPECONF(crtc->config->cpu_transcoder);
  12486. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12487. /* restore vblank interrupts to correct state */
  12488. drm_crtc_vblank_reset(&crtc->base);
  12489. if (crtc->active) {
  12490. struct intel_plane *plane;
  12491. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12492. update_scanline_offset(crtc);
  12493. drm_crtc_vblank_on(&crtc->base);
  12494. /* Disable everything but the primary plane */
  12495. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12496. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12497. continue;
  12498. plane->disable_plane(&plane->base, &crtc->base);
  12499. }
  12500. }
  12501. /* We need to sanitize the plane -> pipe mapping first because this will
  12502. * disable the crtc (and hence change the state) if it is wrong. Note
  12503. * that gen4+ has a fixed plane -> pipe mapping. */
  12504. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12505. bool plane;
  12506. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12507. crtc->base.base.id);
  12508. /* Pipe has the wrong plane attached and the plane is active.
  12509. * Temporarily change the plane mapping and disable everything
  12510. * ... */
  12511. plane = crtc->plane;
  12512. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12513. crtc->plane = !plane;
  12514. intel_crtc_disable_noatomic(&crtc->base);
  12515. crtc->plane = plane;
  12516. }
  12517. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12518. crtc->pipe == PIPE_A && !crtc->active) {
  12519. /* BIOS forgot to enable pipe A, this mostly happens after
  12520. * resume. Force-enable the pipe to fix this, the update_dpms
  12521. * call below we restore the pipe to the right state, but leave
  12522. * the required bits on. */
  12523. intel_enable_pipe_a(dev);
  12524. }
  12525. /* Adjust the state of the output pipe according to whether we
  12526. * have active connectors/encoders. */
  12527. enable = false;
  12528. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12529. enable = true;
  12530. break;
  12531. }
  12532. if (!enable)
  12533. intel_crtc_disable_noatomic(&crtc->base);
  12534. if (crtc->active != crtc->base.state->active) {
  12535. /* This can happen either due to bugs in the get_hw_state
  12536. * functions or because of calls to intel_crtc_disable_noatomic,
  12537. * or because the pipe is force-enabled due to the
  12538. * pipe A quirk. */
  12539. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12540. crtc->base.base.id,
  12541. crtc->base.state->enable ? "enabled" : "disabled",
  12542. crtc->active ? "enabled" : "disabled");
  12543. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12544. crtc->base.state->active = crtc->active;
  12545. crtc->base.enabled = crtc->active;
  12546. /* Because we only establish the connector -> encoder ->
  12547. * crtc links if something is active, this means the
  12548. * crtc is now deactivated. Break the links. connector
  12549. * -> encoder links are only establish when things are
  12550. * actually up, hence no need to break them. */
  12551. WARN_ON(crtc->active);
  12552. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12553. encoder->base.crtc = NULL;
  12554. }
  12555. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12556. /*
  12557. * We start out with underrun reporting disabled to avoid races.
  12558. * For correct bookkeeping mark this on active crtcs.
  12559. *
  12560. * Also on gmch platforms we dont have any hardware bits to
  12561. * disable the underrun reporting. Which means we need to start
  12562. * out with underrun reporting disabled also on inactive pipes,
  12563. * since otherwise we'll complain about the garbage we read when
  12564. * e.g. coming up after runtime pm.
  12565. *
  12566. * No protection against concurrent access is required - at
  12567. * worst a fifo underrun happens which also sets this to false.
  12568. */
  12569. crtc->cpu_fifo_underrun_disabled = true;
  12570. crtc->pch_fifo_underrun_disabled = true;
  12571. }
  12572. }
  12573. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12574. {
  12575. struct intel_connector *connector;
  12576. struct drm_device *dev = encoder->base.dev;
  12577. bool active = false;
  12578. /* We need to check both for a crtc link (meaning that the
  12579. * encoder is active and trying to read from a pipe) and the
  12580. * pipe itself being active. */
  12581. bool has_active_crtc = encoder->base.crtc &&
  12582. to_intel_crtc(encoder->base.crtc)->active;
  12583. for_each_intel_connector(dev, connector) {
  12584. if (connector->base.encoder != &encoder->base)
  12585. continue;
  12586. active = true;
  12587. break;
  12588. }
  12589. if (active && !has_active_crtc) {
  12590. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12591. encoder->base.base.id,
  12592. encoder->base.name);
  12593. /* Connector is active, but has no active pipe. This is
  12594. * fallout from our resume register restoring. Disable
  12595. * the encoder manually again. */
  12596. if (encoder->base.crtc) {
  12597. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12598. encoder->base.base.id,
  12599. encoder->base.name);
  12600. encoder->disable(encoder);
  12601. if (encoder->post_disable)
  12602. encoder->post_disable(encoder);
  12603. }
  12604. encoder->base.crtc = NULL;
  12605. /* Inconsistent output/port/pipe state happens presumably due to
  12606. * a bug in one of the get_hw_state functions. Or someplace else
  12607. * in our code, like the register restore mess on resume. Clamp
  12608. * things to off as a safer default. */
  12609. for_each_intel_connector(dev, connector) {
  12610. if (connector->encoder != encoder)
  12611. continue;
  12612. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12613. connector->base.encoder = NULL;
  12614. }
  12615. }
  12616. /* Enabled encoders without active connectors will be fixed in
  12617. * the crtc fixup. */
  12618. }
  12619. void i915_redisable_vga_power_on(struct drm_device *dev)
  12620. {
  12621. struct drm_i915_private *dev_priv = dev->dev_private;
  12622. u32 vga_reg = i915_vgacntrl_reg(dev);
  12623. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12624. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12625. i915_disable_vga(dev);
  12626. }
  12627. }
  12628. void i915_redisable_vga(struct drm_device *dev)
  12629. {
  12630. struct drm_i915_private *dev_priv = dev->dev_private;
  12631. /* This function can be called both from intel_modeset_setup_hw_state or
  12632. * at a very early point in our resume sequence, where the power well
  12633. * structures are not yet restored. Since this function is at a very
  12634. * paranoid "someone might have enabled VGA while we were not looking"
  12635. * level, just check if the power well is enabled instead of trying to
  12636. * follow the "don't touch the power well if we don't need it" policy
  12637. * the rest of the driver uses. */
  12638. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12639. return;
  12640. i915_redisable_vga_power_on(dev);
  12641. }
  12642. static bool primary_get_hw_state(struct intel_plane *plane)
  12643. {
  12644. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12645. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12646. }
  12647. /* FIXME read out full plane state for all planes */
  12648. static void readout_plane_state(struct intel_crtc *crtc)
  12649. {
  12650. struct drm_plane *primary = crtc->base.primary;
  12651. struct intel_plane_state *plane_state =
  12652. to_intel_plane_state(primary->state);
  12653. plane_state->visible =
  12654. primary_get_hw_state(to_intel_plane(primary));
  12655. if (plane_state->visible)
  12656. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  12657. }
  12658. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12659. {
  12660. struct drm_i915_private *dev_priv = dev->dev_private;
  12661. enum pipe pipe;
  12662. struct intel_crtc *crtc;
  12663. struct intel_encoder *encoder;
  12664. struct intel_connector *connector;
  12665. int i;
  12666. for_each_intel_crtc(dev, crtc) {
  12667. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12668. memset(crtc->config, 0, sizeof(*crtc->config));
  12669. crtc->config->base.crtc = &crtc->base;
  12670. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12671. crtc->config);
  12672. crtc->base.state->active = crtc->active;
  12673. crtc->base.enabled = crtc->active;
  12674. readout_plane_state(crtc);
  12675. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12676. crtc->base.base.id,
  12677. crtc->active ? "enabled" : "disabled");
  12678. }
  12679. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12680. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12681. pll->on = pll->get_hw_state(dev_priv, pll,
  12682. &pll->config.hw_state);
  12683. pll->active = 0;
  12684. pll->config.crtc_mask = 0;
  12685. for_each_intel_crtc(dev, crtc) {
  12686. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12687. pll->active++;
  12688. pll->config.crtc_mask |= 1 << crtc->pipe;
  12689. }
  12690. }
  12691. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12692. pll->name, pll->config.crtc_mask, pll->on);
  12693. if (pll->config.crtc_mask)
  12694. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12695. }
  12696. for_each_intel_encoder(dev, encoder) {
  12697. pipe = 0;
  12698. if (encoder->get_hw_state(encoder, &pipe)) {
  12699. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12700. encoder->base.crtc = &crtc->base;
  12701. encoder->get_config(encoder, crtc->config);
  12702. } else {
  12703. encoder->base.crtc = NULL;
  12704. }
  12705. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12706. encoder->base.base.id,
  12707. encoder->base.name,
  12708. encoder->base.crtc ? "enabled" : "disabled",
  12709. pipe_name(pipe));
  12710. }
  12711. for_each_intel_connector(dev, connector) {
  12712. if (connector->get_hw_state(connector)) {
  12713. connector->base.dpms = DRM_MODE_DPMS_ON;
  12714. connector->base.encoder = &connector->encoder->base;
  12715. } else {
  12716. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12717. connector->base.encoder = NULL;
  12718. }
  12719. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12720. connector->base.base.id,
  12721. connector->base.name,
  12722. connector->base.encoder ? "enabled" : "disabled");
  12723. }
  12724. for_each_intel_crtc(dev, crtc) {
  12725. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12726. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12727. if (crtc->base.state->active) {
  12728. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12729. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12730. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12731. /*
  12732. * The initial mode needs to be set in order to keep
  12733. * the atomic core happy. It wants a valid mode if the
  12734. * crtc's enabled, so we do the above call.
  12735. *
  12736. * At this point some state updated by the connectors
  12737. * in their ->detect() callback has not run yet, so
  12738. * no recalculation can be done yet.
  12739. *
  12740. * Even if we could do a recalculation and modeset
  12741. * right now it would cause a double modeset if
  12742. * fbdev or userspace chooses a different initial mode.
  12743. *
  12744. * If that happens, someone indicated they wanted a
  12745. * mode change, which means it's safe to do a full
  12746. * recalculation.
  12747. */
  12748. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12749. }
  12750. }
  12751. }
  12752. /* Scan out the current hw modeset state,
  12753. * and sanitizes it to the current state
  12754. */
  12755. static void
  12756. intel_modeset_setup_hw_state(struct drm_device *dev)
  12757. {
  12758. struct drm_i915_private *dev_priv = dev->dev_private;
  12759. enum pipe pipe;
  12760. struct intel_crtc *crtc;
  12761. struct intel_encoder *encoder;
  12762. int i;
  12763. intel_modeset_readout_hw_state(dev);
  12764. /* HW state is read out, now we need to sanitize this mess. */
  12765. for_each_intel_encoder(dev, encoder) {
  12766. intel_sanitize_encoder(encoder);
  12767. }
  12768. for_each_pipe(dev_priv, pipe) {
  12769. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12770. intel_sanitize_crtc(crtc);
  12771. intel_dump_pipe_config(crtc, crtc->config,
  12772. "[setup_hw_state]");
  12773. }
  12774. intel_modeset_update_connector_atomic_state(dev);
  12775. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12776. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12777. if (!pll->on || pll->active)
  12778. continue;
  12779. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12780. pll->disable(dev_priv, pll);
  12781. pll->on = false;
  12782. }
  12783. if (IS_VALLEYVIEW(dev))
  12784. vlv_wm_get_hw_state(dev);
  12785. else if (IS_GEN9(dev))
  12786. skl_wm_get_hw_state(dev);
  12787. else if (HAS_PCH_SPLIT(dev))
  12788. ilk_wm_get_hw_state(dev);
  12789. for_each_intel_crtc(dev, crtc) {
  12790. unsigned long put_domains;
  12791. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  12792. if (WARN_ON(put_domains))
  12793. modeset_put_power_domains(dev_priv, put_domains);
  12794. }
  12795. intel_display_set_init_power(dev_priv, false);
  12796. }
  12797. void intel_display_resume(struct drm_device *dev)
  12798. {
  12799. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  12800. struct intel_connector *conn;
  12801. struct intel_plane *plane;
  12802. struct drm_crtc *crtc;
  12803. int ret;
  12804. if (!state)
  12805. return;
  12806. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12807. /* preserve complete old state, including dpll */
  12808. intel_atomic_get_shared_dpll_state(state);
  12809. for_each_crtc(dev, crtc) {
  12810. struct drm_crtc_state *crtc_state =
  12811. drm_atomic_get_crtc_state(state, crtc);
  12812. ret = PTR_ERR_OR_ZERO(crtc_state);
  12813. if (ret)
  12814. goto err;
  12815. /* force a restore */
  12816. crtc_state->mode_changed = true;
  12817. }
  12818. for_each_intel_plane(dev, plane) {
  12819. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  12820. if (ret)
  12821. goto err;
  12822. }
  12823. for_each_intel_connector(dev, conn) {
  12824. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  12825. if (ret)
  12826. goto err;
  12827. }
  12828. intel_modeset_setup_hw_state(dev);
  12829. i915_redisable_vga(dev);
  12830. ret = drm_atomic_commit(state);
  12831. if (!ret)
  12832. return;
  12833. err:
  12834. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12835. drm_atomic_state_free(state);
  12836. }
  12837. void intel_modeset_gem_init(struct drm_device *dev)
  12838. {
  12839. struct drm_crtc *c;
  12840. struct drm_i915_gem_object *obj;
  12841. int ret;
  12842. mutex_lock(&dev->struct_mutex);
  12843. intel_init_gt_powersave(dev);
  12844. mutex_unlock(&dev->struct_mutex);
  12845. intel_modeset_init_hw(dev);
  12846. intel_setup_overlay(dev);
  12847. /*
  12848. * Make sure any fbs we allocated at startup are properly
  12849. * pinned & fenced. When we do the allocation it's too early
  12850. * for this.
  12851. */
  12852. for_each_crtc(dev, c) {
  12853. obj = intel_fb_obj(c->primary->fb);
  12854. if (obj == NULL)
  12855. continue;
  12856. mutex_lock(&dev->struct_mutex);
  12857. ret = intel_pin_and_fence_fb_obj(c->primary,
  12858. c->primary->fb,
  12859. c->primary->state,
  12860. NULL, NULL);
  12861. mutex_unlock(&dev->struct_mutex);
  12862. if (ret) {
  12863. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12864. to_intel_crtc(c)->pipe);
  12865. drm_framebuffer_unreference(c->primary->fb);
  12866. c->primary->fb = NULL;
  12867. c->primary->crtc = c->primary->state->crtc = NULL;
  12868. update_state_fb(c->primary);
  12869. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12870. }
  12871. }
  12872. intel_backlight_register(dev);
  12873. }
  12874. void intel_connector_unregister(struct intel_connector *intel_connector)
  12875. {
  12876. struct drm_connector *connector = &intel_connector->base;
  12877. intel_panel_destroy_backlight(connector);
  12878. drm_connector_unregister(connector);
  12879. }
  12880. void intel_modeset_cleanup(struct drm_device *dev)
  12881. {
  12882. struct drm_i915_private *dev_priv = dev->dev_private;
  12883. struct drm_connector *connector;
  12884. intel_disable_gt_powersave(dev);
  12885. intel_backlight_unregister(dev);
  12886. /*
  12887. * Interrupts and polling as the first thing to avoid creating havoc.
  12888. * Too much stuff here (turning of connectors, ...) would
  12889. * experience fancy races otherwise.
  12890. */
  12891. intel_irq_uninstall(dev_priv);
  12892. /*
  12893. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12894. * poll handlers. Hence disable polling after hpd handling is shut down.
  12895. */
  12896. drm_kms_helper_poll_fini(dev);
  12897. intel_unregister_dsm_handler();
  12898. intel_fbc_disable(dev_priv);
  12899. /* flush any delayed tasks or pending work */
  12900. flush_scheduled_work();
  12901. /* destroy the backlight and sysfs files before encoders/connectors */
  12902. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  12903. struct intel_connector *intel_connector;
  12904. intel_connector = to_intel_connector(connector);
  12905. intel_connector->unregister(intel_connector);
  12906. }
  12907. drm_mode_config_cleanup(dev);
  12908. intel_cleanup_overlay(dev);
  12909. mutex_lock(&dev->struct_mutex);
  12910. intel_cleanup_gt_powersave(dev);
  12911. mutex_unlock(&dev->struct_mutex);
  12912. }
  12913. /*
  12914. * Return which encoder is currently attached for connector.
  12915. */
  12916. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  12917. {
  12918. return &intel_attached_encoder(connector)->base;
  12919. }
  12920. void intel_connector_attach_encoder(struct intel_connector *connector,
  12921. struct intel_encoder *encoder)
  12922. {
  12923. connector->encoder = encoder;
  12924. drm_mode_connector_attach_encoder(&connector->base,
  12925. &encoder->base);
  12926. }
  12927. /*
  12928. * set vga decode state - true == enable VGA decode
  12929. */
  12930. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  12931. {
  12932. struct drm_i915_private *dev_priv = dev->dev_private;
  12933. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  12934. u16 gmch_ctrl;
  12935. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  12936. DRM_ERROR("failed to read control word\n");
  12937. return -EIO;
  12938. }
  12939. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  12940. return 0;
  12941. if (state)
  12942. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  12943. else
  12944. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  12945. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  12946. DRM_ERROR("failed to write control word\n");
  12947. return -EIO;
  12948. }
  12949. return 0;
  12950. }
  12951. struct intel_display_error_state {
  12952. u32 power_well_driver;
  12953. int num_transcoders;
  12954. struct intel_cursor_error_state {
  12955. u32 control;
  12956. u32 position;
  12957. u32 base;
  12958. u32 size;
  12959. } cursor[I915_MAX_PIPES];
  12960. struct intel_pipe_error_state {
  12961. bool power_domain_on;
  12962. u32 source;
  12963. u32 stat;
  12964. } pipe[I915_MAX_PIPES];
  12965. struct intel_plane_error_state {
  12966. u32 control;
  12967. u32 stride;
  12968. u32 size;
  12969. u32 pos;
  12970. u32 addr;
  12971. u32 surface;
  12972. u32 tile_offset;
  12973. } plane[I915_MAX_PIPES];
  12974. struct intel_transcoder_error_state {
  12975. bool power_domain_on;
  12976. enum transcoder cpu_transcoder;
  12977. u32 conf;
  12978. u32 htotal;
  12979. u32 hblank;
  12980. u32 hsync;
  12981. u32 vtotal;
  12982. u32 vblank;
  12983. u32 vsync;
  12984. } transcoder[4];
  12985. };
  12986. struct intel_display_error_state *
  12987. intel_display_capture_error_state(struct drm_device *dev)
  12988. {
  12989. struct drm_i915_private *dev_priv = dev->dev_private;
  12990. struct intel_display_error_state *error;
  12991. int transcoders[] = {
  12992. TRANSCODER_A,
  12993. TRANSCODER_B,
  12994. TRANSCODER_C,
  12995. TRANSCODER_EDP,
  12996. };
  12997. int i;
  12998. if (INTEL_INFO(dev)->num_pipes == 0)
  12999. return NULL;
  13000. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13001. if (error == NULL)
  13002. return NULL;
  13003. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13004. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13005. for_each_pipe(dev_priv, i) {
  13006. error->pipe[i].power_domain_on =
  13007. __intel_display_power_is_enabled(dev_priv,
  13008. POWER_DOMAIN_PIPE(i));
  13009. if (!error->pipe[i].power_domain_on)
  13010. continue;
  13011. error->cursor[i].control = I915_READ(CURCNTR(i));
  13012. error->cursor[i].position = I915_READ(CURPOS(i));
  13013. error->cursor[i].base = I915_READ(CURBASE(i));
  13014. error->plane[i].control = I915_READ(DSPCNTR(i));
  13015. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13016. if (INTEL_INFO(dev)->gen <= 3) {
  13017. error->plane[i].size = I915_READ(DSPSIZE(i));
  13018. error->plane[i].pos = I915_READ(DSPPOS(i));
  13019. }
  13020. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13021. error->plane[i].addr = I915_READ(DSPADDR(i));
  13022. if (INTEL_INFO(dev)->gen >= 4) {
  13023. error->plane[i].surface = I915_READ(DSPSURF(i));
  13024. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13025. }
  13026. error->pipe[i].source = I915_READ(PIPESRC(i));
  13027. if (HAS_GMCH_DISPLAY(dev))
  13028. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13029. }
  13030. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13031. if (HAS_DDI(dev_priv->dev))
  13032. error->num_transcoders++; /* Account for eDP. */
  13033. for (i = 0; i < error->num_transcoders; i++) {
  13034. enum transcoder cpu_transcoder = transcoders[i];
  13035. error->transcoder[i].power_domain_on =
  13036. __intel_display_power_is_enabled(dev_priv,
  13037. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13038. if (!error->transcoder[i].power_domain_on)
  13039. continue;
  13040. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13041. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13042. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13043. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13044. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13045. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13046. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13047. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13048. }
  13049. return error;
  13050. }
  13051. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13052. void
  13053. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13054. struct drm_device *dev,
  13055. struct intel_display_error_state *error)
  13056. {
  13057. struct drm_i915_private *dev_priv = dev->dev_private;
  13058. int i;
  13059. if (!error)
  13060. return;
  13061. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13062. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13063. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13064. error->power_well_driver);
  13065. for_each_pipe(dev_priv, i) {
  13066. err_printf(m, "Pipe [%d]:\n", i);
  13067. err_printf(m, " Power: %s\n",
  13068. error->pipe[i].power_domain_on ? "on" : "off");
  13069. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13070. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13071. err_printf(m, "Plane [%d]:\n", i);
  13072. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13073. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13074. if (INTEL_INFO(dev)->gen <= 3) {
  13075. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13076. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13077. }
  13078. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13079. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13080. if (INTEL_INFO(dev)->gen >= 4) {
  13081. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13082. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13083. }
  13084. err_printf(m, "Cursor [%d]:\n", i);
  13085. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13086. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13087. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13088. }
  13089. for (i = 0; i < error->num_transcoders; i++) {
  13090. err_printf(m, "CPU transcoder: %c\n",
  13091. transcoder_name(error->transcoder[i].cpu_transcoder));
  13092. err_printf(m, " Power: %s\n",
  13093. error->transcoder[i].power_domain_on ? "on" : "off");
  13094. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13095. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13096. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13097. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13098. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13099. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13100. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13101. }
  13102. }
  13103. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13104. {
  13105. struct intel_crtc *crtc;
  13106. for_each_intel_crtc(dev, crtc) {
  13107. struct intel_unpin_work *work;
  13108. spin_lock_irq(&dev->event_lock);
  13109. work = crtc->unpin_work;
  13110. if (work && work->event &&
  13111. work->event->base.file_priv == file) {
  13112. kfree(work->event);
  13113. work->event = NULL;
  13114. }
  13115. spin_unlock_irq(&dev->event_lock);
  13116. }
  13117. }