intel_display.c 396 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. /* Primary plane formats supported by all gen */
  47. #define COMMON_PRIMARY_FORMATS \
  48. DRM_FORMAT_C8, \
  49. DRM_FORMAT_RGB565, \
  50. DRM_FORMAT_XRGB8888, \
  51. DRM_FORMAT_ARGB8888
  52. /* Primary plane formats for gen <= 3 */
  53. static const uint32_t intel_primary_formats_gen2[] = {
  54. COMMON_PRIMARY_FORMATS,
  55. DRM_FORMAT_XRGB1555,
  56. DRM_FORMAT_ARGB1555,
  57. };
  58. /* Primary plane formats for gen >= 4 */
  59. static const uint32_t intel_primary_formats_gen4[] = {
  60. COMMON_PRIMARY_FORMATS, \
  61. DRM_FORMAT_XBGR8888,
  62. DRM_FORMAT_ABGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_ARGB2101010,
  65. DRM_FORMAT_XBGR2101010,
  66. DRM_FORMAT_ABGR2101010,
  67. };
  68. /* Cursor formats */
  69. static const uint32_t intel_cursor_formats[] = {
  70. DRM_FORMAT_ARGB8888,
  71. };
  72. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  73. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_state *pipe_config);
  75. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  76. struct intel_crtc_state *pipe_config);
  77. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  78. int x, int y, struct drm_framebuffer *old_fb);
  79. static int intel_framebuffer_init(struct drm_device *dev,
  80. struct intel_framebuffer *ifb,
  81. struct drm_mode_fb_cmd2 *mode_cmd,
  82. struct drm_i915_gem_object *obj);
  83. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  84. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  85. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  86. struct intel_link_m_n *m_n,
  87. struct intel_link_m_n *m2_n2);
  88. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  89. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  90. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  91. static void vlv_prepare_pll(struct intel_crtc *crtc,
  92. const struct intel_crtc_state *pipe_config);
  93. static void chv_prepare_pll(struct intel_crtc *crtc,
  94. const struct intel_crtc_state *pipe_config);
  95. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  96. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  97. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  98. {
  99. if (!connector->mst_port)
  100. return connector->encoder;
  101. else
  102. return &connector->mst_port->mst_encoders[pipe]->base;
  103. }
  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 void vlv_clock(int refclk, intel_clock_t *clock)
  367. {
  368. clock->m = clock->m1 * clock->m2;
  369. clock->p = clock->p1 * clock->p2;
  370. if (WARN_ON(clock->n == 0 || clock->p == 0))
  371. return;
  372. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  373. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  374. }
  375. /**
  376. * Returns whether any output on the specified pipe is of the specified type
  377. */
  378. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  379. {
  380. struct drm_device *dev = crtc->base.dev;
  381. struct intel_encoder *encoder;
  382. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  383. if (encoder->type == type)
  384. return true;
  385. return false;
  386. }
  387. /**
  388. * Returns whether any output on the specified pipe will have the specified
  389. * type after a staged modeset is complete, i.e., the same as
  390. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  391. * encoder->crtc.
  392. */
  393. static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
  394. {
  395. struct drm_device *dev = crtc->base.dev;
  396. struct intel_encoder *encoder;
  397. for_each_intel_encoder(dev, encoder)
  398. if (encoder->new_crtc == crtc && encoder->type == type)
  399. return true;
  400. return false;
  401. }
  402. static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
  403. int refclk)
  404. {
  405. struct drm_device *dev = crtc->base.dev;
  406. const intel_limit_t *limit;
  407. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  408. if (intel_is_dual_link_lvds(dev)) {
  409. if (refclk == 100000)
  410. limit = &intel_limits_ironlake_dual_lvds_100m;
  411. else
  412. limit = &intel_limits_ironlake_dual_lvds;
  413. } else {
  414. if (refclk == 100000)
  415. limit = &intel_limits_ironlake_single_lvds_100m;
  416. else
  417. limit = &intel_limits_ironlake_single_lvds;
  418. }
  419. } else
  420. limit = &intel_limits_ironlake_dac;
  421. return limit;
  422. }
  423. static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
  424. {
  425. struct drm_device *dev = crtc->base.dev;
  426. const intel_limit_t *limit;
  427. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  428. if (intel_is_dual_link_lvds(dev))
  429. limit = &intel_limits_g4x_dual_channel_lvds;
  430. else
  431. limit = &intel_limits_g4x_single_channel_lvds;
  432. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
  433. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
  434. limit = &intel_limits_g4x_hdmi;
  435. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
  436. limit = &intel_limits_g4x_sdvo;
  437. } else /* The option is for other outputs */
  438. limit = &intel_limits_i9xx_sdvo;
  439. return limit;
  440. }
  441. static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
  442. {
  443. struct drm_device *dev = crtc->base.dev;
  444. const intel_limit_t *limit;
  445. if (HAS_PCH_SPLIT(dev))
  446. limit = intel_ironlake_limit(crtc, refclk);
  447. else if (IS_G4X(dev)) {
  448. limit = intel_g4x_limit(crtc);
  449. } else if (IS_PINEVIEW(dev)) {
  450. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  451. limit = &intel_limits_pineview_lvds;
  452. else
  453. limit = &intel_limits_pineview_sdvo;
  454. } else if (IS_CHERRYVIEW(dev)) {
  455. limit = &intel_limits_chv;
  456. } else if (IS_VALLEYVIEW(dev)) {
  457. limit = &intel_limits_vlv;
  458. } else if (!IS_GEN2(dev)) {
  459. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  460. limit = &intel_limits_i9xx_lvds;
  461. else
  462. limit = &intel_limits_i9xx_sdvo;
  463. } else {
  464. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  465. limit = &intel_limits_i8xx_lvds;
  466. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  467. limit = &intel_limits_i8xx_dvo;
  468. else
  469. limit = &intel_limits_i8xx_dac;
  470. }
  471. return limit;
  472. }
  473. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  474. static void pineview_clock(int refclk, intel_clock_t *clock)
  475. {
  476. clock->m = clock->m2 + 2;
  477. clock->p = clock->p1 * clock->p2;
  478. if (WARN_ON(clock->n == 0 || clock->p == 0))
  479. return;
  480. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  481. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  482. }
  483. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  484. {
  485. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  486. }
  487. static void i9xx_clock(int refclk, intel_clock_t *clock)
  488. {
  489. clock->m = i9xx_dpll_compute_m(clock);
  490. clock->p = clock->p1 * clock->p2;
  491. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  492. return;
  493. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  494. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  495. }
  496. static void chv_clock(int refclk, intel_clock_t *clock)
  497. {
  498. clock->m = clock->m1 * clock->m2;
  499. clock->p = clock->p1 * clock->p2;
  500. if (WARN_ON(clock->n == 0 || clock->p == 0))
  501. return;
  502. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  503. clock->n << 22);
  504. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  505. }
  506. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  507. /**
  508. * Returns whether the given set of divisors are valid for a given refclk with
  509. * the given connectors.
  510. */
  511. static bool intel_PLL_is_valid(struct drm_device *dev,
  512. const intel_limit_t *limit,
  513. const intel_clock_t *clock)
  514. {
  515. if (clock->n < limit->n.min || limit->n.max < clock->n)
  516. INTELPllInvalid("n out of range\n");
  517. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  518. INTELPllInvalid("p1 out of range\n");
  519. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  520. INTELPllInvalid("m2 out of range\n");
  521. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  522. INTELPllInvalid("m1 out of range\n");
  523. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  524. if (clock->m1 <= clock->m2)
  525. INTELPllInvalid("m1 <= m2\n");
  526. if (!IS_VALLEYVIEW(dev)) {
  527. if (clock->p < limit->p.min || limit->p.max < clock->p)
  528. INTELPllInvalid("p out of range\n");
  529. if (clock->m < limit->m.min || limit->m.max < clock->m)
  530. INTELPllInvalid("m out of range\n");
  531. }
  532. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  533. INTELPllInvalid("vco out of range\n");
  534. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  535. * connector, etc., rather than just a single range.
  536. */
  537. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  538. INTELPllInvalid("dot out of range\n");
  539. return true;
  540. }
  541. static bool
  542. i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  543. int target, int refclk, intel_clock_t *match_clock,
  544. intel_clock_t *best_clock)
  545. {
  546. struct drm_device *dev = crtc->base.dev;
  547. intel_clock_t clock;
  548. int err = target;
  549. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  550. /*
  551. * For LVDS just rely on its current settings for dual-channel.
  552. * We haven't figured out how to reliably set up different
  553. * single/dual channel state, if we even can.
  554. */
  555. if (intel_is_dual_link_lvds(dev))
  556. clock.p2 = limit->p2.p2_fast;
  557. else
  558. clock.p2 = limit->p2.p2_slow;
  559. } else {
  560. if (target < limit->p2.dot_limit)
  561. clock.p2 = limit->p2.p2_slow;
  562. else
  563. clock.p2 = limit->p2.p2_fast;
  564. }
  565. memset(best_clock, 0, sizeof(*best_clock));
  566. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  567. clock.m1++) {
  568. for (clock.m2 = limit->m2.min;
  569. clock.m2 <= limit->m2.max; clock.m2++) {
  570. if (clock.m2 >= clock.m1)
  571. break;
  572. for (clock.n = limit->n.min;
  573. clock.n <= limit->n.max; clock.n++) {
  574. for (clock.p1 = limit->p1.min;
  575. clock.p1 <= limit->p1.max; clock.p1++) {
  576. int this_err;
  577. i9xx_clock(refclk, &clock);
  578. if (!intel_PLL_is_valid(dev, limit,
  579. &clock))
  580. continue;
  581. if (match_clock &&
  582. clock.p != match_clock->p)
  583. continue;
  584. this_err = abs(clock.dot - target);
  585. if (this_err < err) {
  586. *best_clock = clock;
  587. err = this_err;
  588. }
  589. }
  590. }
  591. }
  592. }
  593. return (err != target);
  594. }
  595. static bool
  596. pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  597. int target, int refclk, intel_clock_t *match_clock,
  598. intel_clock_t *best_clock)
  599. {
  600. struct drm_device *dev = crtc->base.dev;
  601. intel_clock_t clock;
  602. int err = target;
  603. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  604. /*
  605. * For LVDS just rely on its current settings for dual-channel.
  606. * We haven't figured out how to reliably set up different
  607. * single/dual channel state, if we even can.
  608. */
  609. if (intel_is_dual_link_lvds(dev))
  610. clock.p2 = limit->p2.p2_fast;
  611. else
  612. clock.p2 = limit->p2.p2_slow;
  613. } else {
  614. if (target < limit->p2.dot_limit)
  615. clock.p2 = limit->p2.p2_slow;
  616. else
  617. clock.p2 = limit->p2.p2_fast;
  618. }
  619. memset(best_clock, 0, sizeof(*best_clock));
  620. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  621. clock.m1++) {
  622. for (clock.m2 = limit->m2.min;
  623. clock.m2 <= limit->m2.max; clock.m2++) {
  624. for (clock.n = limit->n.min;
  625. clock.n <= limit->n.max; clock.n++) {
  626. for (clock.p1 = limit->p1.min;
  627. clock.p1 <= limit->p1.max; clock.p1++) {
  628. int this_err;
  629. pineview_clock(refclk, &clock);
  630. if (!intel_PLL_is_valid(dev, limit,
  631. &clock))
  632. continue;
  633. if (match_clock &&
  634. clock.p != match_clock->p)
  635. continue;
  636. this_err = abs(clock.dot - target);
  637. if (this_err < err) {
  638. *best_clock = clock;
  639. err = this_err;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. return (err != target);
  646. }
  647. static bool
  648. g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  649. int target, int refclk, intel_clock_t *match_clock,
  650. intel_clock_t *best_clock)
  651. {
  652. struct drm_device *dev = crtc->base.dev;
  653. intel_clock_t clock;
  654. int max_n;
  655. bool found;
  656. /* approximately equals target * 0.00585 */
  657. int err_most = (target >> 8) + (target >> 9);
  658. found = false;
  659. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  660. if (intel_is_dual_link_lvds(dev))
  661. clock.p2 = limit->p2.p2_fast;
  662. else
  663. clock.p2 = limit->p2.p2_slow;
  664. } else {
  665. if (target < limit->p2.dot_limit)
  666. clock.p2 = limit->p2.p2_slow;
  667. else
  668. clock.p2 = limit->p2.p2_fast;
  669. }
  670. memset(best_clock, 0, sizeof(*best_clock));
  671. max_n = limit->n.max;
  672. /* based on hardware requirement, prefer smaller n to precision */
  673. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  674. /* based on hardware requirement, prefere larger m1,m2 */
  675. for (clock.m1 = limit->m1.max;
  676. clock.m1 >= limit->m1.min; clock.m1--) {
  677. for (clock.m2 = limit->m2.max;
  678. clock.m2 >= limit->m2.min; clock.m2--) {
  679. for (clock.p1 = limit->p1.max;
  680. clock.p1 >= limit->p1.min; clock.p1--) {
  681. int this_err;
  682. i9xx_clock(refclk, &clock);
  683. if (!intel_PLL_is_valid(dev, limit,
  684. &clock))
  685. continue;
  686. this_err = abs(clock.dot - target);
  687. if (this_err < err_most) {
  688. *best_clock = clock;
  689. err_most = this_err;
  690. max_n = clock.n;
  691. found = true;
  692. }
  693. }
  694. }
  695. }
  696. }
  697. return found;
  698. }
  699. /*
  700. * Check if the calculated PLL configuration is more optimal compared to the
  701. * best configuration and error found so far. Return the calculated error.
  702. */
  703. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  704. const intel_clock_t *calculated_clock,
  705. const intel_clock_t *best_clock,
  706. unsigned int best_error_ppm,
  707. unsigned int *error_ppm)
  708. {
  709. /*
  710. * For CHV ignore the error and consider only the P value.
  711. * Prefer a bigger P value based on HW requirements.
  712. */
  713. if (IS_CHERRYVIEW(dev)) {
  714. *error_ppm = 0;
  715. return calculated_clock->p > best_clock->p;
  716. }
  717. if (WARN_ON_ONCE(!target_freq))
  718. return false;
  719. *error_ppm = div_u64(1000000ULL *
  720. abs(target_freq - calculated_clock->dot),
  721. target_freq);
  722. /*
  723. * Prefer a better P value over a better (smaller) error if the error
  724. * is small. Ensure this preference for future configurations too by
  725. * setting the error to 0.
  726. */
  727. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  728. *error_ppm = 0;
  729. return true;
  730. }
  731. return *error_ppm + 10 < best_error_ppm;
  732. }
  733. static bool
  734. vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  735. int target, int refclk, intel_clock_t *match_clock,
  736. intel_clock_t *best_clock)
  737. {
  738. struct drm_device *dev = crtc->base.dev;
  739. intel_clock_t clock;
  740. unsigned int bestppm = 1000000;
  741. /* min update 19.2 MHz */
  742. int max_n = min(limit->n.max, refclk / 19200);
  743. bool found = false;
  744. target *= 5; /* fast clock */
  745. memset(best_clock, 0, sizeof(*best_clock));
  746. /* based on hardware requirement, prefer smaller n to precision */
  747. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  748. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  749. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  750. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  751. clock.p = clock.p1 * clock.p2;
  752. /* based on hardware requirement, prefer bigger m1,m2 values */
  753. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  754. unsigned int ppm;
  755. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  756. refclk * clock.m1);
  757. vlv_clock(refclk, &clock);
  758. if (!intel_PLL_is_valid(dev, limit,
  759. &clock))
  760. continue;
  761. if (!vlv_PLL_is_optimal(dev, target,
  762. &clock,
  763. best_clock,
  764. bestppm, &ppm))
  765. continue;
  766. *best_clock = clock;
  767. bestppm = ppm;
  768. found = true;
  769. }
  770. }
  771. }
  772. }
  773. return found;
  774. }
  775. static bool
  776. chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  777. int target, int refclk, intel_clock_t *match_clock,
  778. intel_clock_t *best_clock)
  779. {
  780. struct drm_device *dev = crtc->base.dev;
  781. unsigned int best_error_ppm;
  782. intel_clock_t clock;
  783. uint64_t m2;
  784. int found = false;
  785. memset(best_clock, 0, sizeof(*best_clock));
  786. best_error_ppm = 1000000;
  787. /*
  788. * Based on hardware doc, the n always set to 1, and m1 always
  789. * set to 2. If requires to support 200Mhz refclk, we need to
  790. * revisit this because n may not 1 anymore.
  791. */
  792. clock.n = 1, clock.m1 = 2;
  793. target *= 5; /* fast clock */
  794. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  795. for (clock.p2 = limit->p2.p2_fast;
  796. clock.p2 >= limit->p2.p2_slow;
  797. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  798. unsigned int error_ppm;
  799. clock.p = clock.p1 * clock.p2;
  800. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  801. clock.n) << 22, refclk * clock.m1);
  802. if (m2 > INT_MAX/clock.m1)
  803. continue;
  804. clock.m2 = m2;
  805. chv_clock(refclk, &clock);
  806. if (!intel_PLL_is_valid(dev, limit, &clock))
  807. continue;
  808. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  809. best_error_ppm, &error_ppm))
  810. continue;
  811. *best_clock = clock;
  812. best_error_ppm = error_ppm;
  813. found = true;
  814. }
  815. }
  816. return found;
  817. }
  818. bool intel_crtc_active(struct drm_crtc *crtc)
  819. {
  820. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  821. /* Be paranoid as we can arrive here with only partial
  822. * state retrieved from the hardware during setup.
  823. *
  824. * We can ditch the adjusted_mode.crtc_clock check as soon
  825. * as Haswell has gained clock readout/fastboot support.
  826. *
  827. * We can ditch the crtc->primary->fb check as soon as we can
  828. * properly reconstruct framebuffers.
  829. *
  830. * FIXME: The intel_crtc->active here should be switched to
  831. * crtc->state->active once we have proper CRTC states wired up
  832. * for atomic.
  833. */
  834. return intel_crtc->active && crtc->primary->state->fb &&
  835. intel_crtc->config->base.adjusted_mode.crtc_clock;
  836. }
  837. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  838. enum pipe pipe)
  839. {
  840. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  841. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  842. return intel_crtc->config->cpu_transcoder;
  843. }
  844. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  845. {
  846. struct drm_i915_private *dev_priv = dev->dev_private;
  847. u32 reg = PIPEDSL(pipe);
  848. u32 line1, line2;
  849. u32 line_mask;
  850. if (IS_GEN2(dev))
  851. line_mask = DSL_LINEMASK_GEN2;
  852. else
  853. line_mask = DSL_LINEMASK_GEN3;
  854. line1 = I915_READ(reg) & line_mask;
  855. mdelay(5);
  856. line2 = I915_READ(reg) & line_mask;
  857. return line1 == line2;
  858. }
  859. /*
  860. * intel_wait_for_pipe_off - wait for pipe to turn off
  861. * @crtc: crtc whose pipe to wait for
  862. *
  863. * After disabling a pipe, we can't wait for vblank in the usual way,
  864. * spinning on the vblank interrupt status bit, since we won't actually
  865. * see an interrupt when the pipe is disabled.
  866. *
  867. * On Gen4 and above:
  868. * wait for the pipe register state bit to turn off
  869. *
  870. * Otherwise:
  871. * wait for the display line value to settle (it usually
  872. * ends up stopping at the start of the next frame).
  873. *
  874. */
  875. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  876. {
  877. struct drm_device *dev = crtc->base.dev;
  878. struct drm_i915_private *dev_priv = dev->dev_private;
  879. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  880. enum pipe pipe = crtc->pipe;
  881. if (INTEL_INFO(dev)->gen >= 4) {
  882. int reg = PIPECONF(cpu_transcoder);
  883. /* Wait for the Pipe State to go off */
  884. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  885. 100))
  886. WARN(1, "pipe_off wait timed out\n");
  887. } else {
  888. /* Wait for the display line to settle */
  889. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  890. WARN(1, "pipe_off wait timed out\n");
  891. }
  892. }
  893. /*
  894. * ibx_digital_port_connected - is the specified port connected?
  895. * @dev_priv: i915 private structure
  896. * @port: the port to test
  897. *
  898. * Returns true if @port is connected, false otherwise.
  899. */
  900. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  901. struct intel_digital_port *port)
  902. {
  903. u32 bit;
  904. if (HAS_PCH_IBX(dev_priv->dev)) {
  905. switch (port->port) {
  906. case PORT_B:
  907. bit = SDE_PORTB_HOTPLUG;
  908. break;
  909. case PORT_C:
  910. bit = SDE_PORTC_HOTPLUG;
  911. break;
  912. case PORT_D:
  913. bit = SDE_PORTD_HOTPLUG;
  914. break;
  915. default:
  916. return true;
  917. }
  918. } else {
  919. switch (port->port) {
  920. case PORT_B:
  921. bit = SDE_PORTB_HOTPLUG_CPT;
  922. break;
  923. case PORT_C:
  924. bit = SDE_PORTC_HOTPLUG_CPT;
  925. break;
  926. case PORT_D:
  927. bit = SDE_PORTD_HOTPLUG_CPT;
  928. break;
  929. default:
  930. return true;
  931. }
  932. }
  933. return I915_READ(SDEISR) & bit;
  934. }
  935. static const char *state_string(bool enabled)
  936. {
  937. return enabled ? "on" : "off";
  938. }
  939. /* Only for pre-ILK configs */
  940. void assert_pll(struct drm_i915_private *dev_priv,
  941. enum pipe pipe, bool state)
  942. {
  943. int reg;
  944. u32 val;
  945. bool cur_state;
  946. reg = DPLL(pipe);
  947. val = I915_READ(reg);
  948. cur_state = !!(val & DPLL_VCO_ENABLE);
  949. I915_STATE_WARN(cur_state != state,
  950. "PLL state assertion failure (expected %s, current %s)\n",
  951. state_string(state), state_string(cur_state));
  952. }
  953. /* XXX: the dsi pll is shared between MIPI DSI ports */
  954. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  955. {
  956. u32 val;
  957. bool cur_state;
  958. mutex_lock(&dev_priv->dpio_lock);
  959. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  960. mutex_unlock(&dev_priv->dpio_lock);
  961. cur_state = val & DSI_PLL_VCO_EN;
  962. I915_STATE_WARN(cur_state != state,
  963. "DSI PLL state assertion failure (expected %s, current %s)\n",
  964. state_string(state), state_string(cur_state));
  965. }
  966. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  967. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  968. struct intel_shared_dpll *
  969. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  970. {
  971. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  972. if (crtc->config->shared_dpll < 0)
  973. return NULL;
  974. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  975. }
  976. /* For ILK+ */
  977. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  978. struct intel_shared_dpll *pll,
  979. bool state)
  980. {
  981. bool cur_state;
  982. struct intel_dpll_hw_state hw_state;
  983. if (WARN (!pll,
  984. "asserting DPLL %s with no DPLL\n", state_string(state)))
  985. return;
  986. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  987. I915_STATE_WARN(cur_state != state,
  988. "%s assertion failure (expected %s, current %s)\n",
  989. pll->name, state_string(state), state_string(cur_state));
  990. }
  991. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  992. enum pipe pipe, bool state)
  993. {
  994. int reg;
  995. u32 val;
  996. bool cur_state;
  997. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  998. pipe);
  999. if (HAS_DDI(dev_priv->dev)) {
  1000. /* DDI does not have a specific FDI_TX register */
  1001. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1002. val = I915_READ(reg);
  1003. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1004. } else {
  1005. reg = FDI_TX_CTL(pipe);
  1006. val = I915_READ(reg);
  1007. cur_state = !!(val & FDI_TX_ENABLE);
  1008. }
  1009. I915_STATE_WARN(cur_state != state,
  1010. "FDI TX state assertion failure (expected %s, current %s)\n",
  1011. state_string(state), state_string(cur_state));
  1012. }
  1013. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1014. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1015. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1016. enum pipe pipe, bool state)
  1017. {
  1018. int reg;
  1019. u32 val;
  1020. bool cur_state;
  1021. reg = FDI_RX_CTL(pipe);
  1022. val = I915_READ(reg);
  1023. cur_state = !!(val & FDI_RX_ENABLE);
  1024. I915_STATE_WARN(cur_state != state,
  1025. "FDI RX state assertion failure (expected %s, current %s)\n",
  1026. state_string(state), state_string(cur_state));
  1027. }
  1028. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1029. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1030. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1031. enum pipe pipe)
  1032. {
  1033. int reg;
  1034. u32 val;
  1035. /* ILK FDI PLL is always enabled */
  1036. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1037. return;
  1038. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1039. if (HAS_DDI(dev_priv->dev))
  1040. return;
  1041. reg = FDI_TX_CTL(pipe);
  1042. val = I915_READ(reg);
  1043. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1044. }
  1045. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1046. enum pipe pipe, bool state)
  1047. {
  1048. int reg;
  1049. u32 val;
  1050. bool cur_state;
  1051. reg = FDI_RX_CTL(pipe);
  1052. val = I915_READ(reg);
  1053. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1054. I915_STATE_WARN(cur_state != state,
  1055. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1056. state_string(state), state_string(cur_state));
  1057. }
  1058. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1059. enum pipe pipe)
  1060. {
  1061. struct drm_device *dev = dev_priv->dev;
  1062. int pp_reg;
  1063. u32 val;
  1064. enum pipe panel_pipe = PIPE_A;
  1065. bool locked = true;
  1066. if (WARN_ON(HAS_DDI(dev)))
  1067. return;
  1068. if (HAS_PCH_SPLIT(dev)) {
  1069. u32 port_sel;
  1070. pp_reg = PCH_PP_CONTROL;
  1071. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1072. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1073. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1074. panel_pipe = PIPE_B;
  1075. /* XXX: else fix for eDP */
  1076. } else if (IS_VALLEYVIEW(dev)) {
  1077. /* presumably write lock depends on pipe, not port select */
  1078. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1079. panel_pipe = pipe;
  1080. } else {
  1081. pp_reg = PP_CONTROL;
  1082. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1083. panel_pipe = PIPE_B;
  1084. }
  1085. val = I915_READ(pp_reg);
  1086. if (!(val & PANEL_POWER_ON) ||
  1087. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1088. locked = false;
  1089. I915_STATE_WARN(panel_pipe == pipe && locked,
  1090. "panel assertion failure, pipe %c regs locked\n",
  1091. pipe_name(pipe));
  1092. }
  1093. static void assert_cursor(struct drm_i915_private *dev_priv,
  1094. enum pipe pipe, bool state)
  1095. {
  1096. struct drm_device *dev = dev_priv->dev;
  1097. bool cur_state;
  1098. if (IS_845G(dev) || IS_I865G(dev))
  1099. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1100. else
  1101. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1102. I915_STATE_WARN(cur_state != state,
  1103. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1104. pipe_name(pipe), state_string(state), state_string(cur_state));
  1105. }
  1106. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1107. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1108. void assert_pipe(struct drm_i915_private *dev_priv,
  1109. enum pipe pipe, bool state)
  1110. {
  1111. int reg;
  1112. u32 val;
  1113. bool cur_state;
  1114. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1115. pipe);
  1116. /* if we need the pipe quirk it must be always on */
  1117. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1118. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1119. state = true;
  1120. if (!intel_display_power_is_enabled(dev_priv,
  1121. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1122. cur_state = false;
  1123. } else {
  1124. reg = PIPECONF(cpu_transcoder);
  1125. val = I915_READ(reg);
  1126. cur_state = !!(val & PIPECONF_ENABLE);
  1127. }
  1128. I915_STATE_WARN(cur_state != state,
  1129. "pipe %c assertion failure (expected %s, current %s)\n",
  1130. pipe_name(pipe), state_string(state), state_string(cur_state));
  1131. }
  1132. static void assert_plane(struct drm_i915_private *dev_priv,
  1133. enum plane plane, bool state)
  1134. {
  1135. int reg;
  1136. u32 val;
  1137. bool cur_state;
  1138. reg = DSPCNTR(plane);
  1139. val = I915_READ(reg);
  1140. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1141. I915_STATE_WARN(cur_state != state,
  1142. "plane %c assertion failure (expected %s, current %s)\n",
  1143. plane_name(plane), state_string(state), state_string(cur_state));
  1144. }
  1145. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1146. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1147. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1148. enum pipe pipe)
  1149. {
  1150. struct drm_device *dev = dev_priv->dev;
  1151. int reg, i;
  1152. u32 val;
  1153. int cur_pipe;
  1154. /* Primary planes are fixed to pipes on gen4+ */
  1155. if (INTEL_INFO(dev)->gen >= 4) {
  1156. reg = DSPCNTR(pipe);
  1157. val = I915_READ(reg);
  1158. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1159. "plane %c assertion failure, should be disabled but not\n",
  1160. plane_name(pipe));
  1161. return;
  1162. }
  1163. /* Need to check both planes against the pipe */
  1164. for_each_pipe(dev_priv, i) {
  1165. reg = DSPCNTR(i);
  1166. val = I915_READ(reg);
  1167. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1168. DISPPLANE_SEL_PIPE_SHIFT;
  1169. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1170. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1171. plane_name(i), pipe_name(pipe));
  1172. }
  1173. }
  1174. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1175. enum pipe pipe)
  1176. {
  1177. struct drm_device *dev = dev_priv->dev;
  1178. int reg, sprite;
  1179. u32 val;
  1180. if (INTEL_INFO(dev)->gen >= 9) {
  1181. for_each_sprite(dev_priv, pipe, sprite) {
  1182. val = I915_READ(PLANE_CTL(pipe, sprite));
  1183. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1184. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1185. sprite, pipe_name(pipe));
  1186. }
  1187. } else if (IS_VALLEYVIEW(dev)) {
  1188. for_each_sprite(dev_priv, pipe, sprite) {
  1189. reg = SPCNTR(pipe, sprite);
  1190. val = I915_READ(reg);
  1191. I915_STATE_WARN(val & SP_ENABLE,
  1192. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1193. sprite_name(pipe, sprite), pipe_name(pipe));
  1194. }
  1195. } else if (INTEL_INFO(dev)->gen >= 7) {
  1196. reg = SPRCTL(pipe);
  1197. val = I915_READ(reg);
  1198. I915_STATE_WARN(val & SPRITE_ENABLE,
  1199. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1200. plane_name(pipe), pipe_name(pipe));
  1201. } else if (INTEL_INFO(dev)->gen >= 5) {
  1202. reg = DVSCNTR(pipe);
  1203. val = I915_READ(reg);
  1204. I915_STATE_WARN(val & DVS_ENABLE,
  1205. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1206. plane_name(pipe), pipe_name(pipe));
  1207. }
  1208. }
  1209. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1210. {
  1211. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1212. drm_crtc_vblank_put(crtc);
  1213. }
  1214. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1215. {
  1216. u32 val;
  1217. bool enabled;
  1218. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1219. val = I915_READ(PCH_DREF_CONTROL);
  1220. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1221. DREF_SUPERSPREAD_SOURCE_MASK));
  1222. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1223. }
  1224. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1225. enum pipe pipe)
  1226. {
  1227. int reg;
  1228. u32 val;
  1229. bool enabled;
  1230. reg = PCH_TRANSCONF(pipe);
  1231. val = I915_READ(reg);
  1232. enabled = !!(val & TRANS_ENABLE);
  1233. I915_STATE_WARN(enabled,
  1234. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1235. pipe_name(pipe));
  1236. }
  1237. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1238. enum pipe pipe, u32 port_sel, u32 val)
  1239. {
  1240. if ((val & DP_PORT_EN) == 0)
  1241. return false;
  1242. if (HAS_PCH_CPT(dev_priv->dev)) {
  1243. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1244. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1245. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1246. return false;
  1247. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1248. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1249. return false;
  1250. } else {
  1251. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1252. return false;
  1253. }
  1254. return true;
  1255. }
  1256. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1257. enum pipe pipe, u32 val)
  1258. {
  1259. if ((val & SDVO_ENABLE) == 0)
  1260. return false;
  1261. if (HAS_PCH_CPT(dev_priv->dev)) {
  1262. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1263. return false;
  1264. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1265. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1266. return false;
  1267. } else {
  1268. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1269. return false;
  1270. }
  1271. return true;
  1272. }
  1273. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1274. enum pipe pipe, u32 val)
  1275. {
  1276. if ((val & LVDS_PORT_EN) == 0)
  1277. return false;
  1278. if (HAS_PCH_CPT(dev_priv->dev)) {
  1279. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1280. return false;
  1281. } else {
  1282. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1283. return false;
  1284. }
  1285. return true;
  1286. }
  1287. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1288. enum pipe pipe, u32 val)
  1289. {
  1290. if ((val & ADPA_DAC_ENABLE) == 0)
  1291. return false;
  1292. if (HAS_PCH_CPT(dev_priv->dev)) {
  1293. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1294. return false;
  1295. } else {
  1296. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1297. return false;
  1298. }
  1299. return true;
  1300. }
  1301. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1302. enum pipe pipe, int reg, u32 port_sel)
  1303. {
  1304. u32 val = I915_READ(reg);
  1305. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1306. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1307. reg, pipe_name(pipe));
  1308. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1309. && (val & DP_PIPEB_SELECT),
  1310. "IBX PCH dp port still using transcoder B\n");
  1311. }
  1312. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1313. enum pipe pipe, int reg)
  1314. {
  1315. u32 val = I915_READ(reg);
  1316. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1317. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1318. reg, pipe_name(pipe));
  1319. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1320. && (val & SDVO_PIPE_B_SELECT),
  1321. "IBX PCH hdmi port still using transcoder B\n");
  1322. }
  1323. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1324. enum pipe pipe)
  1325. {
  1326. int reg;
  1327. u32 val;
  1328. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1329. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1330. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1331. reg = PCH_ADPA;
  1332. val = I915_READ(reg);
  1333. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1334. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1335. pipe_name(pipe));
  1336. reg = PCH_LVDS;
  1337. val = I915_READ(reg);
  1338. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1339. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1340. pipe_name(pipe));
  1341. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1342. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1343. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1344. }
  1345. static void intel_init_dpio(struct drm_device *dev)
  1346. {
  1347. struct drm_i915_private *dev_priv = dev->dev_private;
  1348. if (!IS_VALLEYVIEW(dev))
  1349. return;
  1350. /*
  1351. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1352. * CHV x1 PHY (DP/HDMI D)
  1353. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1354. */
  1355. if (IS_CHERRYVIEW(dev)) {
  1356. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1357. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1358. } else {
  1359. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1360. }
  1361. }
  1362. static void vlv_enable_pll(struct intel_crtc *crtc,
  1363. const struct intel_crtc_state *pipe_config)
  1364. {
  1365. struct drm_device *dev = crtc->base.dev;
  1366. struct drm_i915_private *dev_priv = dev->dev_private;
  1367. int reg = DPLL(crtc->pipe);
  1368. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1369. assert_pipe_disabled(dev_priv, crtc->pipe);
  1370. /* No really, not for ILK+ */
  1371. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1372. /* PLL is protected by panel, make sure we can write it */
  1373. if (IS_MOBILE(dev_priv->dev))
  1374. assert_panel_unlocked(dev_priv, crtc->pipe);
  1375. I915_WRITE(reg, dpll);
  1376. POSTING_READ(reg);
  1377. udelay(150);
  1378. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1379. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1380. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1381. POSTING_READ(DPLL_MD(crtc->pipe));
  1382. /* We do this three times for luck */
  1383. I915_WRITE(reg, dpll);
  1384. POSTING_READ(reg);
  1385. udelay(150); /* wait for warmup */
  1386. I915_WRITE(reg, dpll);
  1387. POSTING_READ(reg);
  1388. udelay(150); /* wait for warmup */
  1389. I915_WRITE(reg, dpll);
  1390. POSTING_READ(reg);
  1391. udelay(150); /* wait for warmup */
  1392. }
  1393. static void chv_enable_pll(struct intel_crtc *crtc,
  1394. const struct intel_crtc_state *pipe_config)
  1395. {
  1396. struct drm_device *dev = crtc->base.dev;
  1397. struct drm_i915_private *dev_priv = dev->dev_private;
  1398. int pipe = crtc->pipe;
  1399. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1400. u32 tmp;
  1401. assert_pipe_disabled(dev_priv, crtc->pipe);
  1402. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1403. mutex_lock(&dev_priv->dpio_lock);
  1404. /* Enable back the 10bit clock to display controller */
  1405. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1406. tmp |= DPIO_DCLKP_EN;
  1407. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1408. /*
  1409. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1410. */
  1411. udelay(1);
  1412. /* Enable PLL */
  1413. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1414. /* Check PLL is locked */
  1415. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1416. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1417. /* not sure when this should be written */
  1418. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1419. POSTING_READ(DPLL_MD(pipe));
  1420. mutex_unlock(&dev_priv->dpio_lock);
  1421. }
  1422. static int intel_num_dvo_pipes(struct drm_device *dev)
  1423. {
  1424. struct intel_crtc *crtc;
  1425. int count = 0;
  1426. for_each_intel_crtc(dev, crtc)
  1427. count += crtc->active &&
  1428. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1429. return count;
  1430. }
  1431. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1432. {
  1433. struct drm_device *dev = crtc->base.dev;
  1434. struct drm_i915_private *dev_priv = dev->dev_private;
  1435. int reg = DPLL(crtc->pipe);
  1436. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1437. assert_pipe_disabled(dev_priv, crtc->pipe);
  1438. /* No really, not for ILK+ */
  1439. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1440. /* PLL is protected by panel, make sure we can write it */
  1441. if (IS_MOBILE(dev) && !IS_I830(dev))
  1442. assert_panel_unlocked(dev_priv, crtc->pipe);
  1443. /* Enable DVO 2x clock on both PLLs if necessary */
  1444. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1445. /*
  1446. * It appears to be important that we don't enable this
  1447. * for the current pipe before otherwise configuring the
  1448. * PLL. No idea how this should be handled if multiple
  1449. * DVO outputs are enabled simultaneosly.
  1450. */
  1451. dpll |= DPLL_DVO_2X_MODE;
  1452. I915_WRITE(DPLL(!crtc->pipe),
  1453. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1454. }
  1455. /* Wait for the clocks to stabilize. */
  1456. POSTING_READ(reg);
  1457. udelay(150);
  1458. if (INTEL_INFO(dev)->gen >= 4) {
  1459. I915_WRITE(DPLL_MD(crtc->pipe),
  1460. crtc->config->dpll_hw_state.dpll_md);
  1461. } else {
  1462. /* The pixel multiplier can only be updated once the
  1463. * DPLL is enabled and the clocks are stable.
  1464. *
  1465. * So write it again.
  1466. */
  1467. I915_WRITE(reg, dpll);
  1468. }
  1469. /* We do this three times for luck */
  1470. I915_WRITE(reg, dpll);
  1471. POSTING_READ(reg);
  1472. udelay(150); /* wait for warmup */
  1473. I915_WRITE(reg, dpll);
  1474. POSTING_READ(reg);
  1475. udelay(150); /* wait for warmup */
  1476. I915_WRITE(reg, dpll);
  1477. POSTING_READ(reg);
  1478. udelay(150); /* wait for warmup */
  1479. }
  1480. /**
  1481. * i9xx_disable_pll - disable a PLL
  1482. * @dev_priv: i915 private structure
  1483. * @pipe: pipe PLL to disable
  1484. *
  1485. * Disable the PLL for @pipe, making sure the pipe is off first.
  1486. *
  1487. * Note! This is for pre-ILK only.
  1488. */
  1489. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1490. {
  1491. struct drm_device *dev = crtc->base.dev;
  1492. struct drm_i915_private *dev_priv = dev->dev_private;
  1493. enum pipe pipe = crtc->pipe;
  1494. /* Disable DVO 2x clock on both PLLs if necessary */
  1495. if (IS_I830(dev) &&
  1496. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1497. intel_num_dvo_pipes(dev) == 1) {
  1498. I915_WRITE(DPLL(PIPE_B),
  1499. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1500. I915_WRITE(DPLL(PIPE_A),
  1501. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1502. }
  1503. /* Don't disable pipe or pipe PLLs if needed */
  1504. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1505. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1506. return;
  1507. /* Make sure the pipe isn't still relying on us */
  1508. assert_pipe_disabled(dev_priv, pipe);
  1509. I915_WRITE(DPLL(pipe), 0);
  1510. POSTING_READ(DPLL(pipe));
  1511. }
  1512. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1513. {
  1514. u32 val = 0;
  1515. /* Make sure the pipe isn't still relying on us */
  1516. assert_pipe_disabled(dev_priv, pipe);
  1517. /*
  1518. * Leave integrated clock source and reference clock enabled for pipe B.
  1519. * The latter is needed for VGA hotplug / manual detection.
  1520. */
  1521. if (pipe == PIPE_B)
  1522. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1523. I915_WRITE(DPLL(pipe), val);
  1524. POSTING_READ(DPLL(pipe));
  1525. }
  1526. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1527. {
  1528. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1529. u32 val;
  1530. /* Make sure the pipe isn't still relying on us */
  1531. assert_pipe_disabled(dev_priv, pipe);
  1532. /* Set PLL en = 0 */
  1533. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1534. if (pipe != PIPE_A)
  1535. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1536. I915_WRITE(DPLL(pipe), val);
  1537. POSTING_READ(DPLL(pipe));
  1538. mutex_lock(&dev_priv->dpio_lock);
  1539. /* Disable 10bit clock to display controller */
  1540. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1541. val &= ~DPIO_DCLKP_EN;
  1542. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1543. /* disable left/right clock distribution */
  1544. if (pipe != PIPE_B) {
  1545. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1546. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1547. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1548. } else {
  1549. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1550. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1551. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1552. }
  1553. mutex_unlock(&dev_priv->dpio_lock);
  1554. }
  1555. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1556. struct intel_digital_port *dport)
  1557. {
  1558. u32 port_mask;
  1559. int dpll_reg;
  1560. switch (dport->port) {
  1561. case PORT_B:
  1562. port_mask = DPLL_PORTB_READY_MASK;
  1563. dpll_reg = DPLL(0);
  1564. break;
  1565. case PORT_C:
  1566. port_mask = DPLL_PORTC_READY_MASK;
  1567. dpll_reg = DPLL(0);
  1568. break;
  1569. case PORT_D:
  1570. port_mask = DPLL_PORTD_READY_MASK;
  1571. dpll_reg = DPIO_PHY_STATUS;
  1572. break;
  1573. default:
  1574. BUG();
  1575. }
  1576. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1577. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1578. port_name(dport->port), I915_READ(dpll_reg));
  1579. }
  1580. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1581. {
  1582. struct drm_device *dev = crtc->base.dev;
  1583. struct drm_i915_private *dev_priv = dev->dev_private;
  1584. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1585. if (WARN_ON(pll == NULL))
  1586. return;
  1587. WARN_ON(!pll->config.crtc_mask);
  1588. if (pll->active == 0) {
  1589. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1590. WARN_ON(pll->on);
  1591. assert_shared_dpll_disabled(dev_priv, pll);
  1592. pll->mode_set(dev_priv, pll);
  1593. }
  1594. }
  1595. /**
  1596. * intel_enable_shared_dpll - enable PCH PLL
  1597. * @dev_priv: i915 private structure
  1598. * @pipe: pipe PLL to enable
  1599. *
  1600. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1601. * drives the transcoder clock.
  1602. */
  1603. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1604. {
  1605. struct drm_device *dev = crtc->base.dev;
  1606. struct drm_i915_private *dev_priv = dev->dev_private;
  1607. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1608. if (WARN_ON(pll == NULL))
  1609. return;
  1610. if (WARN_ON(pll->config.crtc_mask == 0))
  1611. return;
  1612. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1613. pll->name, pll->active, pll->on,
  1614. crtc->base.base.id);
  1615. if (pll->active++) {
  1616. WARN_ON(!pll->on);
  1617. assert_shared_dpll_enabled(dev_priv, pll);
  1618. return;
  1619. }
  1620. WARN_ON(pll->on);
  1621. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1622. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1623. pll->enable(dev_priv, pll);
  1624. pll->on = true;
  1625. }
  1626. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1627. {
  1628. struct drm_device *dev = crtc->base.dev;
  1629. struct drm_i915_private *dev_priv = dev->dev_private;
  1630. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1631. /* PCH only available on ILK+ */
  1632. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1633. if (WARN_ON(pll == NULL))
  1634. return;
  1635. if (WARN_ON(pll->config.crtc_mask == 0))
  1636. return;
  1637. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1638. pll->name, pll->active, pll->on,
  1639. crtc->base.base.id);
  1640. if (WARN_ON(pll->active == 0)) {
  1641. assert_shared_dpll_disabled(dev_priv, pll);
  1642. return;
  1643. }
  1644. assert_shared_dpll_enabled(dev_priv, pll);
  1645. WARN_ON(!pll->on);
  1646. if (--pll->active)
  1647. return;
  1648. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1649. pll->disable(dev_priv, pll);
  1650. pll->on = false;
  1651. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1652. }
  1653. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1654. enum pipe pipe)
  1655. {
  1656. struct drm_device *dev = dev_priv->dev;
  1657. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1658. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1659. uint32_t reg, val, pipeconf_val;
  1660. /* PCH only available on ILK+ */
  1661. BUG_ON(!HAS_PCH_SPLIT(dev));
  1662. /* Make sure PCH DPLL is enabled */
  1663. assert_shared_dpll_enabled(dev_priv,
  1664. intel_crtc_to_shared_dpll(intel_crtc));
  1665. /* FDI must be feeding us bits for PCH ports */
  1666. assert_fdi_tx_enabled(dev_priv, pipe);
  1667. assert_fdi_rx_enabled(dev_priv, pipe);
  1668. if (HAS_PCH_CPT(dev)) {
  1669. /* Workaround: Set the timing override bit before enabling the
  1670. * pch transcoder. */
  1671. reg = TRANS_CHICKEN2(pipe);
  1672. val = I915_READ(reg);
  1673. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1674. I915_WRITE(reg, val);
  1675. }
  1676. reg = PCH_TRANSCONF(pipe);
  1677. val = I915_READ(reg);
  1678. pipeconf_val = I915_READ(PIPECONF(pipe));
  1679. if (HAS_PCH_IBX(dev_priv->dev)) {
  1680. /*
  1681. * make the BPC in transcoder be consistent with
  1682. * that in pipeconf reg.
  1683. */
  1684. val &= ~PIPECONF_BPC_MASK;
  1685. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1686. }
  1687. val &= ~TRANS_INTERLACE_MASK;
  1688. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1689. if (HAS_PCH_IBX(dev_priv->dev) &&
  1690. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1691. val |= TRANS_LEGACY_INTERLACED_ILK;
  1692. else
  1693. val |= TRANS_INTERLACED;
  1694. else
  1695. val |= TRANS_PROGRESSIVE;
  1696. I915_WRITE(reg, val | TRANS_ENABLE);
  1697. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1698. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1699. }
  1700. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1701. enum transcoder cpu_transcoder)
  1702. {
  1703. u32 val, pipeconf_val;
  1704. /* PCH only available on ILK+ */
  1705. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1706. /* FDI must be feeding us bits for PCH ports */
  1707. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1708. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1709. /* Workaround: set timing override bit. */
  1710. val = I915_READ(_TRANSA_CHICKEN2);
  1711. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1712. I915_WRITE(_TRANSA_CHICKEN2, val);
  1713. val = TRANS_ENABLE;
  1714. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1715. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1716. PIPECONF_INTERLACED_ILK)
  1717. val |= TRANS_INTERLACED;
  1718. else
  1719. val |= TRANS_PROGRESSIVE;
  1720. I915_WRITE(LPT_TRANSCONF, val);
  1721. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1722. DRM_ERROR("Failed to enable PCH transcoder\n");
  1723. }
  1724. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1725. enum pipe pipe)
  1726. {
  1727. struct drm_device *dev = dev_priv->dev;
  1728. uint32_t reg, val;
  1729. /* FDI relies on the transcoder */
  1730. assert_fdi_tx_disabled(dev_priv, pipe);
  1731. assert_fdi_rx_disabled(dev_priv, pipe);
  1732. /* Ports must be off as well */
  1733. assert_pch_ports_disabled(dev_priv, pipe);
  1734. reg = PCH_TRANSCONF(pipe);
  1735. val = I915_READ(reg);
  1736. val &= ~TRANS_ENABLE;
  1737. I915_WRITE(reg, val);
  1738. /* wait for PCH transcoder off, transcoder state */
  1739. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1740. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1741. if (!HAS_PCH_IBX(dev)) {
  1742. /* Workaround: Clear the timing override chicken bit again. */
  1743. reg = TRANS_CHICKEN2(pipe);
  1744. val = I915_READ(reg);
  1745. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1746. I915_WRITE(reg, val);
  1747. }
  1748. }
  1749. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1750. {
  1751. u32 val;
  1752. val = I915_READ(LPT_TRANSCONF);
  1753. val &= ~TRANS_ENABLE;
  1754. I915_WRITE(LPT_TRANSCONF, val);
  1755. /* wait for PCH transcoder off, transcoder state */
  1756. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1757. DRM_ERROR("Failed to disable PCH transcoder\n");
  1758. /* Workaround: clear timing override bit. */
  1759. val = I915_READ(_TRANSA_CHICKEN2);
  1760. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1761. I915_WRITE(_TRANSA_CHICKEN2, val);
  1762. }
  1763. /**
  1764. * intel_enable_pipe - enable a pipe, asserting requirements
  1765. * @crtc: crtc responsible for the pipe
  1766. *
  1767. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1768. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1769. */
  1770. static void intel_enable_pipe(struct intel_crtc *crtc)
  1771. {
  1772. struct drm_device *dev = crtc->base.dev;
  1773. struct drm_i915_private *dev_priv = dev->dev_private;
  1774. enum pipe pipe = crtc->pipe;
  1775. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1776. pipe);
  1777. enum pipe pch_transcoder;
  1778. int reg;
  1779. u32 val;
  1780. assert_planes_disabled(dev_priv, pipe);
  1781. assert_cursor_disabled(dev_priv, pipe);
  1782. assert_sprites_disabled(dev_priv, pipe);
  1783. if (HAS_PCH_LPT(dev_priv->dev))
  1784. pch_transcoder = TRANSCODER_A;
  1785. else
  1786. pch_transcoder = pipe;
  1787. /*
  1788. * A pipe without a PLL won't actually be able to drive bits from
  1789. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1790. * need the check.
  1791. */
  1792. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1793. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1794. assert_dsi_pll_enabled(dev_priv);
  1795. else
  1796. assert_pll_enabled(dev_priv, pipe);
  1797. else {
  1798. if (crtc->config->has_pch_encoder) {
  1799. /* if driving the PCH, we need FDI enabled */
  1800. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1801. assert_fdi_tx_pll_enabled(dev_priv,
  1802. (enum pipe) cpu_transcoder);
  1803. }
  1804. /* FIXME: assert CPU port conditions for SNB+ */
  1805. }
  1806. reg = PIPECONF(cpu_transcoder);
  1807. val = I915_READ(reg);
  1808. if (val & PIPECONF_ENABLE) {
  1809. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1810. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1811. return;
  1812. }
  1813. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1814. POSTING_READ(reg);
  1815. }
  1816. /**
  1817. * intel_disable_pipe - disable a pipe, asserting requirements
  1818. * @crtc: crtc whose pipes is to be disabled
  1819. *
  1820. * Disable the pipe of @crtc, making sure that various hardware
  1821. * specific requirements are met, if applicable, e.g. plane
  1822. * disabled, panel fitter off, etc.
  1823. *
  1824. * Will wait until the pipe has shut down before returning.
  1825. */
  1826. static void intel_disable_pipe(struct intel_crtc *crtc)
  1827. {
  1828. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1829. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1830. enum pipe pipe = crtc->pipe;
  1831. int reg;
  1832. u32 val;
  1833. /*
  1834. * Make sure planes won't keep trying to pump pixels to us,
  1835. * or we might hang the display.
  1836. */
  1837. assert_planes_disabled(dev_priv, pipe);
  1838. assert_cursor_disabled(dev_priv, pipe);
  1839. assert_sprites_disabled(dev_priv, pipe);
  1840. reg = PIPECONF(cpu_transcoder);
  1841. val = I915_READ(reg);
  1842. if ((val & PIPECONF_ENABLE) == 0)
  1843. return;
  1844. /*
  1845. * Double wide has implications for planes
  1846. * so best keep it disabled when not needed.
  1847. */
  1848. if (crtc->config->double_wide)
  1849. val &= ~PIPECONF_DOUBLE_WIDE;
  1850. /* Don't disable pipe or pipe PLLs if needed */
  1851. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1852. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1853. val &= ~PIPECONF_ENABLE;
  1854. I915_WRITE(reg, val);
  1855. if ((val & PIPECONF_ENABLE) == 0)
  1856. intel_wait_for_pipe_off(crtc);
  1857. }
  1858. /*
  1859. * Plane regs are double buffered, going from enabled->disabled needs a
  1860. * trigger in order to latch. The display address reg provides this.
  1861. */
  1862. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1863. enum plane plane)
  1864. {
  1865. struct drm_device *dev = dev_priv->dev;
  1866. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1867. I915_WRITE(reg, I915_READ(reg));
  1868. POSTING_READ(reg);
  1869. }
  1870. /**
  1871. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1872. * @plane: plane to be enabled
  1873. * @crtc: crtc for the plane
  1874. *
  1875. * Enable @plane on @crtc, making sure that the pipe is running first.
  1876. */
  1877. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1878. struct drm_crtc *crtc)
  1879. {
  1880. struct drm_device *dev = plane->dev;
  1881. struct drm_i915_private *dev_priv = dev->dev_private;
  1882. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1883. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1884. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1885. if (intel_crtc->primary_enabled)
  1886. return;
  1887. intel_crtc->primary_enabled = true;
  1888. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1889. crtc->x, crtc->y);
  1890. /*
  1891. * BDW signals flip done immediately if the plane
  1892. * is disabled, even if the plane enable is already
  1893. * armed to occur at the next vblank :(
  1894. */
  1895. if (IS_BROADWELL(dev))
  1896. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1897. }
  1898. /**
  1899. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1900. * @plane: plane to be disabled
  1901. * @crtc: crtc for the plane
  1902. *
  1903. * Disable @plane on @crtc, making sure that the pipe is running first.
  1904. */
  1905. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1906. struct drm_crtc *crtc)
  1907. {
  1908. struct drm_device *dev = plane->dev;
  1909. struct drm_i915_private *dev_priv = dev->dev_private;
  1910. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1911. if (WARN_ON(!intel_crtc->active))
  1912. return;
  1913. if (!intel_crtc->primary_enabled)
  1914. return;
  1915. intel_crtc->primary_enabled = false;
  1916. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1917. crtc->x, crtc->y);
  1918. }
  1919. static bool need_vtd_wa(struct drm_device *dev)
  1920. {
  1921. #ifdef CONFIG_INTEL_IOMMU
  1922. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1923. return true;
  1924. #endif
  1925. return false;
  1926. }
  1927. unsigned int
  1928. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1929. uint64_t fb_format_modifier)
  1930. {
  1931. unsigned int tile_height;
  1932. uint32_t pixel_bytes;
  1933. switch (fb_format_modifier) {
  1934. case DRM_FORMAT_MOD_NONE:
  1935. tile_height = 1;
  1936. break;
  1937. case I915_FORMAT_MOD_X_TILED:
  1938. tile_height = IS_GEN2(dev) ? 16 : 8;
  1939. break;
  1940. case I915_FORMAT_MOD_Y_TILED:
  1941. tile_height = 32;
  1942. break;
  1943. case I915_FORMAT_MOD_Yf_TILED:
  1944. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1945. switch (pixel_bytes) {
  1946. default:
  1947. case 1:
  1948. tile_height = 64;
  1949. break;
  1950. case 2:
  1951. case 4:
  1952. tile_height = 32;
  1953. break;
  1954. case 8:
  1955. tile_height = 16;
  1956. break;
  1957. case 16:
  1958. WARN_ONCE(1,
  1959. "128-bit pixels are not supported for display!");
  1960. tile_height = 16;
  1961. break;
  1962. }
  1963. break;
  1964. default:
  1965. MISSING_CASE(fb_format_modifier);
  1966. tile_height = 1;
  1967. break;
  1968. }
  1969. return tile_height;
  1970. }
  1971. unsigned int
  1972. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1973. uint32_t pixel_format, uint64_t fb_format_modifier)
  1974. {
  1975. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1976. fb_format_modifier));
  1977. }
  1978. static int
  1979. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1980. const struct drm_plane_state *plane_state)
  1981. {
  1982. struct intel_rotation_info *info = &view->rotation_info;
  1983. static const struct i915_ggtt_view rotated_view =
  1984. { .type = I915_GGTT_VIEW_ROTATED };
  1985. *view = i915_ggtt_view_normal;
  1986. if (!plane_state)
  1987. return 0;
  1988. if (!intel_rotation_90_or_270(plane_state->rotation))
  1989. return 0;
  1990. *view = rotated_view;
  1991. info->height = fb->height;
  1992. info->pixel_format = fb->pixel_format;
  1993. info->pitch = fb->pitches[0];
  1994. info->fb_modifier = fb->modifier[0];
  1995. if (!(info->fb_modifier == I915_FORMAT_MOD_Y_TILED ||
  1996. info->fb_modifier == I915_FORMAT_MOD_Yf_TILED)) {
  1997. DRM_DEBUG_KMS(
  1998. "Y or Yf tiling is needed for 90/270 rotation!\n");
  1999. return -EINVAL;
  2000. }
  2001. return 0;
  2002. }
  2003. int
  2004. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2005. struct drm_framebuffer *fb,
  2006. const struct drm_plane_state *plane_state,
  2007. struct intel_engine_cs *pipelined)
  2008. {
  2009. struct drm_device *dev = fb->dev;
  2010. struct drm_i915_private *dev_priv = dev->dev_private;
  2011. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2012. struct i915_ggtt_view view;
  2013. u32 alignment;
  2014. int ret;
  2015. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2016. switch (fb->modifier[0]) {
  2017. case DRM_FORMAT_MOD_NONE:
  2018. if (INTEL_INFO(dev)->gen >= 9)
  2019. alignment = 256 * 1024;
  2020. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  2021. alignment = 128 * 1024;
  2022. else if (INTEL_INFO(dev)->gen >= 4)
  2023. alignment = 4 * 1024;
  2024. else
  2025. alignment = 64 * 1024;
  2026. break;
  2027. case I915_FORMAT_MOD_X_TILED:
  2028. if (INTEL_INFO(dev)->gen >= 9)
  2029. alignment = 256 * 1024;
  2030. else {
  2031. /* pin() will align the object as required by fence */
  2032. alignment = 0;
  2033. }
  2034. break;
  2035. case I915_FORMAT_MOD_Y_TILED:
  2036. case I915_FORMAT_MOD_Yf_TILED:
  2037. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2038. "Y tiling bo slipped through, driver bug!\n"))
  2039. return -EINVAL;
  2040. alignment = 1 * 1024 * 1024;
  2041. break;
  2042. default:
  2043. MISSING_CASE(fb->modifier[0]);
  2044. return -EINVAL;
  2045. }
  2046. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2047. if (ret)
  2048. return ret;
  2049. /* Note that the w/a also requires 64 PTE of padding following the
  2050. * bo. We currently fill all unused PTE with the shadow page and so
  2051. * we should always have valid PTE following the scanout preventing
  2052. * the VT-d warning.
  2053. */
  2054. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2055. alignment = 256 * 1024;
  2056. /*
  2057. * Global gtt pte registers are special registers which actually forward
  2058. * writes to a chunk of system memory. Which means that there is no risk
  2059. * that the register values disappear as soon as we call
  2060. * intel_runtime_pm_put(), so it is correct to wrap only the
  2061. * pin/unpin/fence and not more.
  2062. */
  2063. intel_runtime_pm_get(dev_priv);
  2064. dev_priv->mm.interruptible = false;
  2065. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2066. &view);
  2067. if (ret)
  2068. goto err_interruptible;
  2069. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2070. * fence, whereas 965+ only requires a fence if using
  2071. * framebuffer compression. For simplicity, we always install
  2072. * a fence as the cost is not that onerous.
  2073. */
  2074. ret = i915_gem_object_get_fence(obj);
  2075. if (ret)
  2076. goto err_unpin;
  2077. i915_gem_object_pin_fence(obj);
  2078. dev_priv->mm.interruptible = true;
  2079. intel_runtime_pm_put(dev_priv);
  2080. return 0;
  2081. err_unpin:
  2082. i915_gem_object_unpin_from_display_plane(obj, &view);
  2083. err_interruptible:
  2084. dev_priv->mm.interruptible = true;
  2085. intel_runtime_pm_put(dev_priv);
  2086. return ret;
  2087. }
  2088. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2089. const struct drm_plane_state *plane_state)
  2090. {
  2091. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2092. struct i915_ggtt_view view;
  2093. int ret;
  2094. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2095. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2096. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2097. i915_gem_object_unpin_fence(obj);
  2098. i915_gem_object_unpin_from_display_plane(obj, &view);
  2099. }
  2100. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2101. * is assumed to be a power-of-two. */
  2102. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2103. unsigned int tiling_mode,
  2104. unsigned int cpp,
  2105. unsigned int pitch)
  2106. {
  2107. if (tiling_mode != I915_TILING_NONE) {
  2108. unsigned int tile_rows, tiles;
  2109. tile_rows = *y / 8;
  2110. *y %= 8;
  2111. tiles = *x / (512/cpp);
  2112. *x %= 512/cpp;
  2113. return tile_rows * pitch * 8 + tiles * 4096;
  2114. } else {
  2115. unsigned int offset;
  2116. offset = *y * pitch + *x * cpp;
  2117. *y = 0;
  2118. *x = (offset & 4095) / cpp;
  2119. return offset & -4096;
  2120. }
  2121. }
  2122. static int i9xx_format_to_fourcc(int format)
  2123. {
  2124. switch (format) {
  2125. case DISPPLANE_8BPP:
  2126. return DRM_FORMAT_C8;
  2127. case DISPPLANE_BGRX555:
  2128. return DRM_FORMAT_XRGB1555;
  2129. case DISPPLANE_BGRX565:
  2130. return DRM_FORMAT_RGB565;
  2131. default:
  2132. case DISPPLANE_BGRX888:
  2133. return DRM_FORMAT_XRGB8888;
  2134. case DISPPLANE_RGBX888:
  2135. return DRM_FORMAT_XBGR8888;
  2136. case DISPPLANE_BGRX101010:
  2137. return DRM_FORMAT_XRGB2101010;
  2138. case DISPPLANE_RGBX101010:
  2139. return DRM_FORMAT_XBGR2101010;
  2140. }
  2141. }
  2142. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2143. {
  2144. switch (format) {
  2145. case PLANE_CTL_FORMAT_RGB_565:
  2146. return DRM_FORMAT_RGB565;
  2147. default:
  2148. case PLANE_CTL_FORMAT_XRGB_8888:
  2149. if (rgb_order) {
  2150. if (alpha)
  2151. return DRM_FORMAT_ABGR8888;
  2152. else
  2153. return DRM_FORMAT_XBGR8888;
  2154. } else {
  2155. if (alpha)
  2156. return DRM_FORMAT_ARGB8888;
  2157. else
  2158. return DRM_FORMAT_XRGB8888;
  2159. }
  2160. case PLANE_CTL_FORMAT_XRGB_2101010:
  2161. if (rgb_order)
  2162. return DRM_FORMAT_XBGR2101010;
  2163. else
  2164. return DRM_FORMAT_XRGB2101010;
  2165. }
  2166. }
  2167. static bool
  2168. intel_alloc_plane_obj(struct intel_crtc *crtc,
  2169. struct intel_initial_plane_config *plane_config)
  2170. {
  2171. struct drm_device *dev = crtc->base.dev;
  2172. struct drm_i915_gem_object *obj = NULL;
  2173. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2174. struct drm_framebuffer *fb = &plane_config->fb->base;
  2175. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2176. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2177. PAGE_SIZE);
  2178. size_aligned -= base_aligned;
  2179. if (plane_config->size == 0)
  2180. return false;
  2181. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2182. base_aligned,
  2183. base_aligned,
  2184. size_aligned);
  2185. if (!obj)
  2186. return false;
  2187. obj->tiling_mode = plane_config->tiling;
  2188. if (obj->tiling_mode == I915_TILING_X)
  2189. obj->stride = fb->pitches[0];
  2190. mode_cmd.pixel_format = fb->pixel_format;
  2191. mode_cmd.width = fb->width;
  2192. mode_cmd.height = fb->height;
  2193. mode_cmd.pitches[0] = fb->pitches[0];
  2194. mode_cmd.modifier[0] = fb->modifier[0];
  2195. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2196. mutex_lock(&dev->struct_mutex);
  2197. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2198. &mode_cmd, obj)) {
  2199. DRM_DEBUG_KMS("intel fb init failed\n");
  2200. goto out_unref_obj;
  2201. }
  2202. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2203. mutex_unlock(&dev->struct_mutex);
  2204. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2205. return true;
  2206. out_unref_obj:
  2207. drm_gem_object_unreference(&obj->base);
  2208. mutex_unlock(&dev->struct_mutex);
  2209. return false;
  2210. }
  2211. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2212. static void
  2213. update_state_fb(struct drm_plane *plane)
  2214. {
  2215. if (plane->fb == plane->state->fb)
  2216. return;
  2217. if (plane->state->fb)
  2218. drm_framebuffer_unreference(plane->state->fb);
  2219. plane->state->fb = plane->fb;
  2220. if (plane->state->fb)
  2221. drm_framebuffer_reference(plane->state->fb);
  2222. }
  2223. static void
  2224. intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2225. struct intel_initial_plane_config *plane_config)
  2226. {
  2227. struct drm_device *dev = intel_crtc->base.dev;
  2228. struct drm_i915_private *dev_priv = dev->dev_private;
  2229. struct drm_crtc *c;
  2230. struct intel_crtc *i;
  2231. struct drm_i915_gem_object *obj;
  2232. if (!plane_config->fb)
  2233. return;
  2234. if (intel_alloc_plane_obj(intel_crtc, plane_config)) {
  2235. struct drm_plane *primary = intel_crtc->base.primary;
  2236. primary->fb = &plane_config->fb->base;
  2237. primary->state->crtc = &intel_crtc->base;
  2238. update_state_fb(primary);
  2239. return;
  2240. }
  2241. kfree(plane_config->fb);
  2242. /*
  2243. * Failed to alloc the obj, check to see if we should share
  2244. * an fb with another CRTC instead
  2245. */
  2246. for_each_crtc(dev, c) {
  2247. i = to_intel_crtc(c);
  2248. if (c == &intel_crtc->base)
  2249. continue;
  2250. if (!i->active)
  2251. continue;
  2252. obj = intel_fb_obj(c->primary->fb);
  2253. if (obj == NULL)
  2254. continue;
  2255. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2256. struct drm_plane *primary = intel_crtc->base.primary;
  2257. if (obj->tiling_mode != I915_TILING_NONE)
  2258. dev_priv->preserve_bios_swizzle = true;
  2259. drm_framebuffer_reference(c->primary->fb);
  2260. primary->fb = c->primary->fb;
  2261. primary->state->crtc = &intel_crtc->base;
  2262. update_state_fb(intel_crtc->base.primary);
  2263. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2264. break;
  2265. }
  2266. }
  2267. }
  2268. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2269. struct drm_framebuffer *fb,
  2270. int x, int y)
  2271. {
  2272. struct drm_device *dev = crtc->dev;
  2273. struct drm_i915_private *dev_priv = dev->dev_private;
  2274. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2275. struct drm_i915_gem_object *obj;
  2276. int plane = intel_crtc->plane;
  2277. unsigned long linear_offset;
  2278. u32 dspcntr;
  2279. u32 reg = DSPCNTR(plane);
  2280. int pixel_size;
  2281. if (!intel_crtc->primary_enabled) {
  2282. I915_WRITE(reg, 0);
  2283. if (INTEL_INFO(dev)->gen >= 4)
  2284. I915_WRITE(DSPSURF(plane), 0);
  2285. else
  2286. I915_WRITE(DSPADDR(plane), 0);
  2287. POSTING_READ(reg);
  2288. return;
  2289. }
  2290. obj = intel_fb_obj(fb);
  2291. if (WARN_ON(obj == NULL))
  2292. return;
  2293. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2294. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2295. dspcntr |= DISPLAY_PLANE_ENABLE;
  2296. if (INTEL_INFO(dev)->gen < 4) {
  2297. if (intel_crtc->pipe == PIPE_B)
  2298. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2299. /* pipesrc and dspsize control the size that is scaled from,
  2300. * which should always be the user's requested size.
  2301. */
  2302. I915_WRITE(DSPSIZE(plane),
  2303. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2304. (intel_crtc->config->pipe_src_w - 1));
  2305. I915_WRITE(DSPPOS(plane), 0);
  2306. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2307. I915_WRITE(PRIMSIZE(plane),
  2308. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2309. (intel_crtc->config->pipe_src_w - 1));
  2310. I915_WRITE(PRIMPOS(plane), 0);
  2311. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2312. }
  2313. switch (fb->pixel_format) {
  2314. case DRM_FORMAT_C8:
  2315. dspcntr |= DISPPLANE_8BPP;
  2316. break;
  2317. case DRM_FORMAT_XRGB1555:
  2318. case DRM_FORMAT_ARGB1555:
  2319. dspcntr |= DISPPLANE_BGRX555;
  2320. break;
  2321. case DRM_FORMAT_RGB565:
  2322. dspcntr |= DISPPLANE_BGRX565;
  2323. break;
  2324. case DRM_FORMAT_XRGB8888:
  2325. case DRM_FORMAT_ARGB8888:
  2326. dspcntr |= DISPPLANE_BGRX888;
  2327. break;
  2328. case DRM_FORMAT_XBGR8888:
  2329. case DRM_FORMAT_ABGR8888:
  2330. dspcntr |= DISPPLANE_RGBX888;
  2331. break;
  2332. case DRM_FORMAT_XRGB2101010:
  2333. case DRM_FORMAT_ARGB2101010:
  2334. dspcntr |= DISPPLANE_BGRX101010;
  2335. break;
  2336. case DRM_FORMAT_XBGR2101010:
  2337. case DRM_FORMAT_ABGR2101010:
  2338. dspcntr |= DISPPLANE_RGBX101010;
  2339. break;
  2340. default:
  2341. BUG();
  2342. }
  2343. if (INTEL_INFO(dev)->gen >= 4 &&
  2344. obj->tiling_mode != I915_TILING_NONE)
  2345. dspcntr |= DISPPLANE_TILED;
  2346. if (IS_G4X(dev))
  2347. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2348. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2349. if (INTEL_INFO(dev)->gen >= 4) {
  2350. intel_crtc->dspaddr_offset =
  2351. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2352. pixel_size,
  2353. fb->pitches[0]);
  2354. linear_offset -= intel_crtc->dspaddr_offset;
  2355. } else {
  2356. intel_crtc->dspaddr_offset = linear_offset;
  2357. }
  2358. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2359. dspcntr |= DISPPLANE_ROTATE_180;
  2360. x += (intel_crtc->config->pipe_src_w - 1);
  2361. y += (intel_crtc->config->pipe_src_h - 1);
  2362. /* Finding the last pixel of the last line of the display
  2363. data and adding to linear_offset*/
  2364. linear_offset +=
  2365. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2366. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2367. }
  2368. I915_WRITE(reg, dspcntr);
  2369. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2370. if (INTEL_INFO(dev)->gen >= 4) {
  2371. I915_WRITE(DSPSURF(plane),
  2372. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2373. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2374. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2375. } else
  2376. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2377. POSTING_READ(reg);
  2378. }
  2379. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2380. struct drm_framebuffer *fb,
  2381. int x, int y)
  2382. {
  2383. struct drm_device *dev = crtc->dev;
  2384. struct drm_i915_private *dev_priv = dev->dev_private;
  2385. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2386. struct drm_i915_gem_object *obj;
  2387. int plane = intel_crtc->plane;
  2388. unsigned long linear_offset;
  2389. u32 dspcntr;
  2390. u32 reg = DSPCNTR(plane);
  2391. int pixel_size;
  2392. if (!intel_crtc->primary_enabled) {
  2393. I915_WRITE(reg, 0);
  2394. I915_WRITE(DSPSURF(plane), 0);
  2395. POSTING_READ(reg);
  2396. return;
  2397. }
  2398. obj = intel_fb_obj(fb);
  2399. if (WARN_ON(obj == NULL))
  2400. return;
  2401. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2402. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2403. dspcntr |= DISPLAY_PLANE_ENABLE;
  2404. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2405. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2406. switch (fb->pixel_format) {
  2407. case DRM_FORMAT_C8:
  2408. dspcntr |= DISPPLANE_8BPP;
  2409. break;
  2410. case DRM_FORMAT_RGB565:
  2411. dspcntr |= DISPPLANE_BGRX565;
  2412. break;
  2413. case DRM_FORMAT_XRGB8888:
  2414. case DRM_FORMAT_ARGB8888:
  2415. dspcntr |= DISPPLANE_BGRX888;
  2416. break;
  2417. case DRM_FORMAT_XBGR8888:
  2418. case DRM_FORMAT_ABGR8888:
  2419. dspcntr |= DISPPLANE_RGBX888;
  2420. break;
  2421. case DRM_FORMAT_XRGB2101010:
  2422. case DRM_FORMAT_ARGB2101010:
  2423. dspcntr |= DISPPLANE_BGRX101010;
  2424. break;
  2425. case DRM_FORMAT_XBGR2101010:
  2426. case DRM_FORMAT_ABGR2101010:
  2427. dspcntr |= DISPPLANE_RGBX101010;
  2428. break;
  2429. default:
  2430. BUG();
  2431. }
  2432. if (obj->tiling_mode != I915_TILING_NONE)
  2433. dspcntr |= DISPPLANE_TILED;
  2434. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2435. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2436. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2437. intel_crtc->dspaddr_offset =
  2438. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2439. pixel_size,
  2440. fb->pitches[0]);
  2441. linear_offset -= intel_crtc->dspaddr_offset;
  2442. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2443. dspcntr |= DISPPLANE_ROTATE_180;
  2444. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2445. x += (intel_crtc->config->pipe_src_w - 1);
  2446. y += (intel_crtc->config->pipe_src_h - 1);
  2447. /* Finding the last pixel of the last line of the display
  2448. data and adding to linear_offset*/
  2449. linear_offset +=
  2450. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2451. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2452. }
  2453. }
  2454. I915_WRITE(reg, dspcntr);
  2455. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2456. I915_WRITE(DSPSURF(plane),
  2457. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2458. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2459. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2460. } else {
  2461. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2462. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2463. }
  2464. POSTING_READ(reg);
  2465. }
  2466. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2467. uint32_t pixel_format)
  2468. {
  2469. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2470. /*
  2471. * The stride is either expressed as a multiple of 64 bytes
  2472. * chunks for linear buffers or in number of tiles for tiled
  2473. * buffers.
  2474. */
  2475. switch (fb_modifier) {
  2476. case DRM_FORMAT_MOD_NONE:
  2477. return 64;
  2478. case I915_FORMAT_MOD_X_TILED:
  2479. if (INTEL_INFO(dev)->gen == 2)
  2480. return 128;
  2481. return 512;
  2482. case I915_FORMAT_MOD_Y_TILED:
  2483. /* No need to check for old gens and Y tiling since this is
  2484. * about the display engine and those will be blocked before
  2485. * we get here.
  2486. */
  2487. return 128;
  2488. case I915_FORMAT_MOD_Yf_TILED:
  2489. if (bits_per_pixel == 8)
  2490. return 64;
  2491. else
  2492. return 128;
  2493. default:
  2494. MISSING_CASE(fb_modifier);
  2495. return 64;
  2496. }
  2497. }
  2498. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2499. struct drm_i915_gem_object *obj)
  2500. {
  2501. enum i915_ggtt_view_type view = I915_GGTT_VIEW_NORMAL;
  2502. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2503. view = I915_GGTT_VIEW_ROTATED;
  2504. return i915_gem_obj_ggtt_offset_view(obj, view);
  2505. }
  2506. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2507. struct drm_framebuffer *fb,
  2508. int x, int y)
  2509. {
  2510. struct drm_device *dev = crtc->dev;
  2511. struct drm_i915_private *dev_priv = dev->dev_private;
  2512. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2513. struct drm_i915_gem_object *obj;
  2514. int pipe = intel_crtc->pipe;
  2515. u32 plane_ctl, stride_div;
  2516. unsigned long surf_addr;
  2517. if (!intel_crtc->primary_enabled) {
  2518. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2519. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2520. POSTING_READ(PLANE_CTL(pipe, 0));
  2521. return;
  2522. }
  2523. plane_ctl = PLANE_CTL_ENABLE |
  2524. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2525. PLANE_CTL_PIPE_CSC_ENABLE;
  2526. switch (fb->pixel_format) {
  2527. case DRM_FORMAT_RGB565:
  2528. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2529. break;
  2530. case DRM_FORMAT_XRGB8888:
  2531. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2532. break;
  2533. case DRM_FORMAT_ARGB8888:
  2534. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2535. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2536. break;
  2537. case DRM_FORMAT_XBGR8888:
  2538. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2539. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2540. break;
  2541. case DRM_FORMAT_ABGR8888:
  2542. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2543. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2544. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2545. break;
  2546. case DRM_FORMAT_XRGB2101010:
  2547. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2548. break;
  2549. case DRM_FORMAT_XBGR2101010:
  2550. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2551. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2552. break;
  2553. default:
  2554. BUG();
  2555. }
  2556. switch (fb->modifier[0]) {
  2557. case DRM_FORMAT_MOD_NONE:
  2558. break;
  2559. case I915_FORMAT_MOD_X_TILED:
  2560. plane_ctl |= PLANE_CTL_TILED_X;
  2561. break;
  2562. case I915_FORMAT_MOD_Y_TILED:
  2563. plane_ctl |= PLANE_CTL_TILED_Y;
  2564. break;
  2565. case I915_FORMAT_MOD_Yf_TILED:
  2566. plane_ctl |= PLANE_CTL_TILED_YF;
  2567. break;
  2568. default:
  2569. MISSING_CASE(fb->modifier[0]);
  2570. }
  2571. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2572. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
  2573. plane_ctl |= PLANE_CTL_ROTATE_180;
  2574. obj = intel_fb_obj(fb);
  2575. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2576. fb->pixel_format);
  2577. surf_addr = intel_plane_obj_offset(to_intel_plane(crtc->primary), obj);
  2578. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2579. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2580. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2581. I915_WRITE(PLANE_SIZE(pipe, 0),
  2582. (intel_crtc->config->pipe_src_h - 1) << 16 |
  2583. (intel_crtc->config->pipe_src_w - 1));
  2584. I915_WRITE(PLANE_STRIDE(pipe, 0), fb->pitches[0] / stride_div);
  2585. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2586. POSTING_READ(PLANE_SURF(pipe, 0));
  2587. }
  2588. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2589. static int
  2590. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2591. int x, int y, enum mode_set_atomic state)
  2592. {
  2593. struct drm_device *dev = crtc->dev;
  2594. struct drm_i915_private *dev_priv = dev->dev_private;
  2595. if (dev_priv->display.disable_fbc)
  2596. dev_priv->display.disable_fbc(dev);
  2597. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2598. return 0;
  2599. }
  2600. static void intel_complete_page_flips(struct drm_device *dev)
  2601. {
  2602. struct drm_crtc *crtc;
  2603. for_each_crtc(dev, crtc) {
  2604. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2605. enum plane plane = intel_crtc->plane;
  2606. intel_prepare_page_flip(dev, plane);
  2607. intel_finish_page_flip_plane(dev, plane);
  2608. }
  2609. }
  2610. static void intel_update_primary_planes(struct drm_device *dev)
  2611. {
  2612. struct drm_i915_private *dev_priv = dev->dev_private;
  2613. struct drm_crtc *crtc;
  2614. for_each_crtc(dev, crtc) {
  2615. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2616. drm_modeset_lock(&crtc->mutex, NULL);
  2617. /*
  2618. * FIXME: Once we have proper support for primary planes (and
  2619. * disabling them without disabling the entire crtc) allow again
  2620. * a NULL crtc->primary->fb.
  2621. */
  2622. if (intel_crtc->active && crtc->primary->fb)
  2623. dev_priv->display.update_primary_plane(crtc,
  2624. crtc->primary->fb,
  2625. crtc->x,
  2626. crtc->y);
  2627. drm_modeset_unlock(&crtc->mutex);
  2628. }
  2629. }
  2630. void intel_prepare_reset(struct drm_device *dev)
  2631. {
  2632. struct drm_i915_private *dev_priv = to_i915(dev);
  2633. struct intel_crtc *crtc;
  2634. /* no reset support for gen2 */
  2635. if (IS_GEN2(dev))
  2636. return;
  2637. /* reset doesn't touch the display */
  2638. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2639. return;
  2640. drm_modeset_lock_all(dev);
  2641. /*
  2642. * Disabling the crtcs gracefully seems nicer. Also the
  2643. * g33 docs say we should at least disable all the planes.
  2644. */
  2645. for_each_intel_crtc(dev, crtc) {
  2646. if (crtc->active)
  2647. dev_priv->display.crtc_disable(&crtc->base);
  2648. }
  2649. }
  2650. void intel_finish_reset(struct drm_device *dev)
  2651. {
  2652. struct drm_i915_private *dev_priv = to_i915(dev);
  2653. /*
  2654. * Flips in the rings will be nuked by the reset,
  2655. * so complete all pending flips so that user space
  2656. * will get its events and not get stuck.
  2657. */
  2658. intel_complete_page_flips(dev);
  2659. /* no reset support for gen2 */
  2660. if (IS_GEN2(dev))
  2661. return;
  2662. /* reset doesn't touch the display */
  2663. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2664. /*
  2665. * Flips in the rings have been nuked by the reset,
  2666. * so update the base address of all primary
  2667. * planes to the the last fb to make sure we're
  2668. * showing the correct fb after a reset.
  2669. */
  2670. intel_update_primary_planes(dev);
  2671. return;
  2672. }
  2673. /*
  2674. * The display has been reset as well,
  2675. * so need a full re-initialization.
  2676. */
  2677. intel_runtime_pm_disable_interrupts(dev_priv);
  2678. intel_runtime_pm_enable_interrupts(dev_priv);
  2679. intel_modeset_init_hw(dev);
  2680. spin_lock_irq(&dev_priv->irq_lock);
  2681. if (dev_priv->display.hpd_irq_setup)
  2682. dev_priv->display.hpd_irq_setup(dev);
  2683. spin_unlock_irq(&dev_priv->irq_lock);
  2684. intel_modeset_setup_hw_state(dev, true);
  2685. intel_hpd_init(dev_priv);
  2686. drm_modeset_unlock_all(dev);
  2687. }
  2688. static int
  2689. intel_finish_fb(struct drm_framebuffer *old_fb)
  2690. {
  2691. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2692. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2693. bool was_interruptible = dev_priv->mm.interruptible;
  2694. int ret;
  2695. /* Big Hammer, we also need to ensure that any pending
  2696. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2697. * current scanout is retired before unpinning the old
  2698. * framebuffer.
  2699. *
  2700. * This should only fail upon a hung GPU, in which case we
  2701. * can safely continue.
  2702. */
  2703. dev_priv->mm.interruptible = false;
  2704. ret = i915_gem_object_finish_gpu(obj);
  2705. dev_priv->mm.interruptible = was_interruptible;
  2706. return ret;
  2707. }
  2708. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2709. {
  2710. struct drm_device *dev = crtc->dev;
  2711. struct drm_i915_private *dev_priv = dev->dev_private;
  2712. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2713. bool pending;
  2714. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2715. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2716. return false;
  2717. spin_lock_irq(&dev->event_lock);
  2718. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2719. spin_unlock_irq(&dev->event_lock);
  2720. return pending;
  2721. }
  2722. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2723. {
  2724. struct drm_device *dev = crtc->base.dev;
  2725. struct drm_i915_private *dev_priv = dev->dev_private;
  2726. const struct drm_display_mode *adjusted_mode;
  2727. if (!i915.fastboot)
  2728. return;
  2729. /*
  2730. * Update pipe size and adjust fitter if needed: the reason for this is
  2731. * that in compute_mode_changes we check the native mode (not the pfit
  2732. * mode) to see if we can flip rather than do a full mode set. In the
  2733. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2734. * pfit state, we'll end up with a big fb scanned out into the wrong
  2735. * sized surface.
  2736. *
  2737. * To fix this properly, we need to hoist the checks up into
  2738. * compute_mode_changes (or above), check the actual pfit state and
  2739. * whether the platform allows pfit disable with pipe active, and only
  2740. * then update the pipesrc and pfit state, even on the flip path.
  2741. */
  2742. adjusted_mode = &crtc->config->base.adjusted_mode;
  2743. I915_WRITE(PIPESRC(crtc->pipe),
  2744. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2745. (adjusted_mode->crtc_vdisplay - 1));
  2746. if (!crtc->config->pch_pfit.enabled &&
  2747. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2748. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2749. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2750. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2751. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2752. }
  2753. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2754. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2755. }
  2756. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2757. {
  2758. struct drm_device *dev = crtc->dev;
  2759. struct drm_i915_private *dev_priv = dev->dev_private;
  2760. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2761. int pipe = intel_crtc->pipe;
  2762. u32 reg, temp;
  2763. /* enable normal train */
  2764. reg = FDI_TX_CTL(pipe);
  2765. temp = I915_READ(reg);
  2766. if (IS_IVYBRIDGE(dev)) {
  2767. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2768. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2769. } else {
  2770. temp &= ~FDI_LINK_TRAIN_NONE;
  2771. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2772. }
  2773. I915_WRITE(reg, temp);
  2774. reg = FDI_RX_CTL(pipe);
  2775. temp = I915_READ(reg);
  2776. if (HAS_PCH_CPT(dev)) {
  2777. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2778. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2779. } else {
  2780. temp &= ~FDI_LINK_TRAIN_NONE;
  2781. temp |= FDI_LINK_TRAIN_NONE;
  2782. }
  2783. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2784. /* wait one idle pattern time */
  2785. POSTING_READ(reg);
  2786. udelay(1000);
  2787. /* IVB wants error correction enabled */
  2788. if (IS_IVYBRIDGE(dev))
  2789. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2790. FDI_FE_ERRC_ENABLE);
  2791. }
  2792. /* The FDI link training functions for ILK/Ibexpeak. */
  2793. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2794. {
  2795. struct drm_device *dev = crtc->dev;
  2796. struct drm_i915_private *dev_priv = dev->dev_private;
  2797. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2798. int pipe = intel_crtc->pipe;
  2799. u32 reg, temp, tries;
  2800. /* FDI needs bits from pipe first */
  2801. assert_pipe_enabled(dev_priv, pipe);
  2802. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2803. for train result */
  2804. reg = FDI_RX_IMR(pipe);
  2805. temp = I915_READ(reg);
  2806. temp &= ~FDI_RX_SYMBOL_LOCK;
  2807. temp &= ~FDI_RX_BIT_LOCK;
  2808. I915_WRITE(reg, temp);
  2809. I915_READ(reg);
  2810. udelay(150);
  2811. /* enable CPU FDI TX and PCH FDI RX */
  2812. reg = FDI_TX_CTL(pipe);
  2813. temp = I915_READ(reg);
  2814. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2815. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2816. temp &= ~FDI_LINK_TRAIN_NONE;
  2817. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2818. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2819. reg = FDI_RX_CTL(pipe);
  2820. temp = I915_READ(reg);
  2821. temp &= ~FDI_LINK_TRAIN_NONE;
  2822. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2823. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2824. POSTING_READ(reg);
  2825. udelay(150);
  2826. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2827. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2828. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2829. FDI_RX_PHASE_SYNC_POINTER_EN);
  2830. reg = FDI_RX_IIR(pipe);
  2831. for (tries = 0; tries < 5; tries++) {
  2832. temp = I915_READ(reg);
  2833. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2834. if ((temp & FDI_RX_BIT_LOCK)) {
  2835. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2836. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2837. break;
  2838. }
  2839. }
  2840. if (tries == 5)
  2841. DRM_ERROR("FDI train 1 fail!\n");
  2842. /* Train 2 */
  2843. reg = FDI_TX_CTL(pipe);
  2844. temp = I915_READ(reg);
  2845. temp &= ~FDI_LINK_TRAIN_NONE;
  2846. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2847. I915_WRITE(reg, temp);
  2848. reg = FDI_RX_CTL(pipe);
  2849. temp = I915_READ(reg);
  2850. temp &= ~FDI_LINK_TRAIN_NONE;
  2851. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2852. I915_WRITE(reg, temp);
  2853. POSTING_READ(reg);
  2854. udelay(150);
  2855. reg = FDI_RX_IIR(pipe);
  2856. for (tries = 0; tries < 5; tries++) {
  2857. temp = I915_READ(reg);
  2858. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2859. if (temp & FDI_RX_SYMBOL_LOCK) {
  2860. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2861. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2862. break;
  2863. }
  2864. }
  2865. if (tries == 5)
  2866. DRM_ERROR("FDI train 2 fail!\n");
  2867. DRM_DEBUG_KMS("FDI train done\n");
  2868. }
  2869. static const int snb_b_fdi_train_param[] = {
  2870. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2871. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2872. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2873. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2874. };
  2875. /* The FDI link training functions for SNB/Cougarpoint. */
  2876. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2877. {
  2878. struct drm_device *dev = crtc->dev;
  2879. struct drm_i915_private *dev_priv = dev->dev_private;
  2880. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2881. int pipe = intel_crtc->pipe;
  2882. u32 reg, temp, i, retry;
  2883. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2884. for train result */
  2885. reg = FDI_RX_IMR(pipe);
  2886. temp = I915_READ(reg);
  2887. temp &= ~FDI_RX_SYMBOL_LOCK;
  2888. temp &= ~FDI_RX_BIT_LOCK;
  2889. I915_WRITE(reg, temp);
  2890. POSTING_READ(reg);
  2891. udelay(150);
  2892. /* enable CPU FDI TX and PCH FDI RX */
  2893. reg = FDI_TX_CTL(pipe);
  2894. temp = I915_READ(reg);
  2895. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2896. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2897. temp &= ~FDI_LINK_TRAIN_NONE;
  2898. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2899. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2900. /* SNB-B */
  2901. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2902. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2903. I915_WRITE(FDI_RX_MISC(pipe),
  2904. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2905. reg = FDI_RX_CTL(pipe);
  2906. temp = I915_READ(reg);
  2907. if (HAS_PCH_CPT(dev)) {
  2908. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2909. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2910. } else {
  2911. temp &= ~FDI_LINK_TRAIN_NONE;
  2912. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2913. }
  2914. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2915. POSTING_READ(reg);
  2916. udelay(150);
  2917. for (i = 0; i < 4; i++) {
  2918. reg = FDI_TX_CTL(pipe);
  2919. temp = I915_READ(reg);
  2920. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2921. temp |= snb_b_fdi_train_param[i];
  2922. I915_WRITE(reg, temp);
  2923. POSTING_READ(reg);
  2924. udelay(500);
  2925. for (retry = 0; retry < 5; retry++) {
  2926. reg = FDI_RX_IIR(pipe);
  2927. temp = I915_READ(reg);
  2928. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2929. if (temp & FDI_RX_BIT_LOCK) {
  2930. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2931. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2932. break;
  2933. }
  2934. udelay(50);
  2935. }
  2936. if (retry < 5)
  2937. break;
  2938. }
  2939. if (i == 4)
  2940. DRM_ERROR("FDI train 1 fail!\n");
  2941. /* Train 2 */
  2942. reg = FDI_TX_CTL(pipe);
  2943. temp = I915_READ(reg);
  2944. temp &= ~FDI_LINK_TRAIN_NONE;
  2945. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2946. if (IS_GEN6(dev)) {
  2947. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2948. /* SNB-B */
  2949. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2950. }
  2951. I915_WRITE(reg, temp);
  2952. reg = FDI_RX_CTL(pipe);
  2953. temp = I915_READ(reg);
  2954. if (HAS_PCH_CPT(dev)) {
  2955. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2956. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2957. } else {
  2958. temp &= ~FDI_LINK_TRAIN_NONE;
  2959. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2960. }
  2961. I915_WRITE(reg, temp);
  2962. POSTING_READ(reg);
  2963. udelay(150);
  2964. for (i = 0; i < 4; i++) {
  2965. reg = FDI_TX_CTL(pipe);
  2966. temp = I915_READ(reg);
  2967. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2968. temp |= snb_b_fdi_train_param[i];
  2969. I915_WRITE(reg, temp);
  2970. POSTING_READ(reg);
  2971. udelay(500);
  2972. for (retry = 0; retry < 5; retry++) {
  2973. reg = FDI_RX_IIR(pipe);
  2974. temp = I915_READ(reg);
  2975. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2976. if (temp & FDI_RX_SYMBOL_LOCK) {
  2977. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2978. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2979. break;
  2980. }
  2981. udelay(50);
  2982. }
  2983. if (retry < 5)
  2984. break;
  2985. }
  2986. if (i == 4)
  2987. DRM_ERROR("FDI train 2 fail!\n");
  2988. DRM_DEBUG_KMS("FDI train done.\n");
  2989. }
  2990. /* Manual link training for Ivy Bridge A0 parts */
  2991. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2992. {
  2993. struct drm_device *dev = crtc->dev;
  2994. struct drm_i915_private *dev_priv = dev->dev_private;
  2995. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2996. int pipe = intel_crtc->pipe;
  2997. u32 reg, temp, i, j;
  2998. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2999. for train result */
  3000. reg = FDI_RX_IMR(pipe);
  3001. temp = I915_READ(reg);
  3002. temp &= ~FDI_RX_SYMBOL_LOCK;
  3003. temp &= ~FDI_RX_BIT_LOCK;
  3004. I915_WRITE(reg, temp);
  3005. POSTING_READ(reg);
  3006. udelay(150);
  3007. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3008. I915_READ(FDI_RX_IIR(pipe)));
  3009. /* Try each vswing and preemphasis setting twice before moving on */
  3010. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3011. /* disable first in case we need to retry */
  3012. reg = FDI_TX_CTL(pipe);
  3013. temp = I915_READ(reg);
  3014. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3015. temp &= ~FDI_TX_ENABLE;
  3016. I915_WRITE(reg, temp);
  3017. reg = FDI_RX_CTL(pipe);
  3018. temp = I915_READ(reg);
  3019. temp &= ~FDI_LINK_TRAIN_AUTO;
  3020. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3021. temp &= ~FDI_RX_ENABLE;
  3022. I915_WRITE(reg, temp);
  3023. /* enable CPU FDI TX and PCH FDI RX */
  3024. reg = FDI_TX_CTL(pipe);
  3025. temp = I915_READ(reg);
  3026. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3027. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3028. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3029. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3030. temp |= snb_b_fdi_train_param[j/2];
  3031. temp |= FDI_COMPOSITE_SYNC;
  3032. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3033. I915_WRITE(FDI_RX_MISC(pipe),
  3034. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3035. reg = FDI_RX_CTL(pipe);
  3036. temp = I915_READ(reg);
  3037. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3038. temp |= FDI_COMPOSITE_SYNC;
  3039. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3040. POSTING_READ(reg);
  3041. udelay(1); /* should be 0.5us */
  3042. for (i = 0; i < 4; i++) {
  3043. reg = FDI_RX_IIR(pipe);
  3044. temp = I915_READ(reg);
  3045. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3046. if (temp & FDI_RX_BIT_LOCK ||
  3047. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3048. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3049. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3050. i);
  3051. break;
  3052. }
  3053. udelay(1); /* should be 0.5us */
  3054. }
  3055. if (i == 4) {
  3056. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3057. continue;
  3058. }
  3059. /* Train 2 */
  3060. reg = FDI_TX_CTL(pipe);
  3061. temp = I915_READ(reg);
  3062. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3063. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3064. I915_WRITE(reg, temp);
  3065. reg = FDI_RX_CTL(pipe);
  3066. temp = I915_READ(reg);
  3067. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3068. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3069. I915_WRITE(reg, temp);
  3070. POSTING_READ(reg);
  3071. udelay(2); /* should be 1.5us */
  3072. for (i = 0; i < 4; i++) {
  3073. reg = FDI_RX_IIR(pipe);
  3074. temp = I915_READ(reg);
  3075. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3076. if (temp & FDI_RX_SYMBOL_LOCK ||
  3077. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3078. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3079. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3080. i);
  3081. goto train_done;
  3082. }
  3083. udelay(2); /* should be 1.5us */
  3084. }
  3085. if (i == 4)
  3086. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3087. }
  3088. train_done:
  3089. DRM_DEBUG_KMS("FDI train done.\n");
  3090. }
  3091. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3092. {
  3093. struct drm_device *dev = intel_crtc->base.dev;
  3094. struct drm_i915_private *dev_priv = dev->dev_private;
  3095. int pipe = intel_crtc->pipe;
  3096. u32 reg, temp;
  3097. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3098. reg = FDI_RX_CTL(pipe);
  3099. temp = I915_READ(reg);
  3100. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3101. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3102. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3103. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3104. POSTING_READ(reg);
  3105. udelay(200);
  3106. /* Switch from Rawclk to PCDclk */
  3107. temp = I915_READ(reg);
  3108. I915_WRITE(reg, temp | FDI_PCDCLK);
  3109. POSTING_READ(reg);
  3110. udelay(200);
  3111. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3112. reg = FDI_TX_CTL(pipe);
  3113. temp = I915_READ(reg);
  3114. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3115. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3116. POSTING_READ(reg);
  3117. udelay(100);
  3118. }
  3119. }
  3120. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3121. {
  3122. struct drm_device *dev = intel_crtc->base.dev;
  3123. struct drm_i915_private *dev_priv = dev->dev_private;
  3124. int pipe = intel_crtc->pipe;
  3125. u32 reg, temp;
  3126. /* Switch from PCDclk to Rawclk */
  3127. reg = FDI_RX_CTL(pipe);
  3128. temp = I915_READ(reg);
  3129. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3130. /* Disable CPU FDI TX PLL */
  3131. reg = FDI_TX_CTL(pipe);
  3132. temp = I915_READ(reg);
  3133. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3134. POSTING_READ(reg);
  3135. udelay(100);
  3136. reg = FDI_RX_CTL(pipe);
  3137. temp = I915_READ(reg);
  3138. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3139. /* Wait for the clocks to turn off. */
  3140. POSTING_READ(reg);
  3141. udelay(100);
  3142. }
  3143. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3144. {
  3145. struct drm_device *dev = crtc->dev;
  3146. struct drm_i915_private *dev_priv = dev->dev_private;
  3147. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3148. int pipe = intel_crtc->pipe;
  3149. u32 reg, temp;
  3150. /* disable CPU FDI tx and PCH FDI rx */
  3151. reg = FDI_TX_CTL(pipe);
  3152. temp = I915_READ(reg);
  3153. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3154. POSTING_READ(reg);
  3155. reg = FDI_RX_CTL(pipe);
  3156. temp = I915_READ(reg);
  3157. temp &= ~(0x7 << 16);
  3158. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3159. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3160. POSTING_READ(reg);
  3161. udelay(100);
  3162. /* Ironlake workaround, disable clock pointer after downing FDI */
  3163. if (HAS_PCH_IBX(dev))
  3164. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3165. /* still set train pattern 1 */
  3166. reg = FDI_TX_CTL(pipe);
  3167. temp = I915_READ(reg);
  3168. temp &= ~FDI_LINK_TRAIN_NONE;
  3169. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3170. I915_WRITE(reg, temp);
  3171. reg = FDI_RX_CTL(pipe);
  3172. temp = I915_READ(reg);
  3173. if (HAS_PCH_CPT(dev)) {
  3174. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3175. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3176. } else {
  3177. temp &= ~FDI_LINK_TRAIN_NONE;
  3178. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3179. }
  3180. /* BPC in FDI rx is consistent with that in PIPECONF */
  3181. temp &= ~(0x07 << 16);
  3182. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3183. I915_WRITE(reg, temp);
  3184. POSTING_READ(reg);
  3185. udelay(100);
  3186. }
  3187. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3188. {
  3189. struct intel_crtc *crtc;
  3190. /* Note that we don't need to be called with mode_config.lock here
  3191. * as our list of CRTC objects is static for the lifetime of the
  3192. * device and so cannot disappear as we iterate. Similarly, we can
  3193. * happily treat the predicates as racy, atomic checks as userspace
  3194. * cannot claim and pin a new fb without at least acquring the
  3195. * struct_mutex and so serialising with us.
  3196. */
  3197. for_each_intel_crtc(dev, crtc) {
  3198. if (atomic_read(&crtc->unpin_work_count) == 0)
  3199. continue;
  3200. if (crtc->unpin_work)
  3201. intel_wait_for_vblank(dev, crtc->pipe);
  3202. return true;
  3203. }
  3204. return false;
  3205. }
  3206. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3207. {
  3208. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3209. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3210. /* ensure that the unpin work is consistent wrt ->pending. */
  3211. smp_rmb();
  3212. intel_crtc->unpin_work = NULL;
  3213. if (work->event)
  3214. drm_send_vblank_event(intel_crtc->base.dev,
  3215. intel_crtc->pipe,
  3216. work->event);
  3217. drm_crtc_vblank_put(&intel_crtc->base);
  3218. wake_up_all(&dev_priv->pending_flip_queue);
  3219. queue_work(dev_priv->wq, &work->work);
  3220. trace_i915_flip_complete(intel_crtc->plane,
  3221. work->pending_flip_obj);
  3222. }
  3223. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3224. {
  3225. struct drm_device *dev = crtc->dev;
  3226. struct drm_i915_private *dev_priv = dev->dev_private;
  3227. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3228. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3229. !intel_crtc_has_pending_flip(crtc),
  3230. 60*HZ) == 0)) {
  3231. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3232. spin_lock_irq(&dev->event_lock);
  3233. if (intel_crtc->unpin_work) {
  3234. WARN_ONCE(1, "Removing stuck page flip\n");
  3235. page_flip_completed(intel_crtc);
  3236. }
  3237. spin_unlock_irq(&dev->event_lock);
  3238. }
  3239. if (crtc->primary->fb) {
  3240. mutex_lock(&dev->struct_mutex);
  3241. intel_finish_fb(crtc->primary->fb);
  3242. mutex_unlock(&dev->struct_mutex);
  3243. }
  3244. }
  3245. /* Program iCLKIP clock to the desired frequency */
  3246. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3247. {
  3248. struct drm_device *dev = crtc->dev;
  3249. struct drm_i915_private *dev_priv = dev->dev_private;
  3250. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3251. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3252. u32 temp;
  3253. mutex_lock(&dev_priv->dpio_lock);
  3254. /* It is necessary to ungate the pixclk gate prior to programming
  3255. * the divisors, and gate it back when it is done.
  3256. */
  3257. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3258. /* Disable SSCCTL */
  3259. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3260. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3261. SBI_SSCCTL_DISABLE,
  3262. SBI_ICLK);
  3263. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3264. if (clock == 20000) {
  3265. auxdiv = 1;
  3266. divsel = 0x41;
  3267. phaseinc = 0x20;
  3268. } else {
  3269. /* The iCLK virtual clock root frequency is in MHz,
  3270. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3271. * divisors, it is necessary to divide one by another, so we
  3272. * convert the virtual clock precision to KHz here for higher
  3273. * precision.
  3274. */
  3275. u32 iclk_virtual_root_freq = 172800 * 1000;
  3276. u32 iclk_pi_range = 64;
  3277. u32 desired_divisor, msb_divisor_value, pi_value;
  3278. desired_divisor = (iclk_virtual_root_freq / clock);
  3279. msb_divisor_value = desired_divisor / iclk_pi_range;
  3280. pi_value = desired_divisor % iclk_pi_range;
  3281. auxdiv = 0;
  3282. divsel = msb_divisor_value - 2;
  3283. phaseinc = pi_value;
  3284. }
  3285. /* This should not happen with any sane values */
  3286. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3287. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3288. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3289. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3290. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3291. clock,
  3292. auxdiv,
  3293. divsel,
  3294. phasedir,
  3295. phaseinc);
  3296. /* Program SSCDIVINTPHASE6 */
  3297. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3298. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3299. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3300. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3301. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3302. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3303. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3304. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3305. /* Program SSCAUXDIV */
  3306. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3307. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3308. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3309. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3310. /* Enable modulator and associated divider */
  3311. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3312. temp &= ~SBI_SSCCTL_DISABLE;
  3313. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3314. /* Wait for initialization time */
  3315. udelay(24);
  3316. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3317. mutex_unlock(&dev_priv->dpio_lock);
  3318. }
  3319. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3320. enum pipe pch_transcoder)
  3321. {
  3322. struct drm_device *dev = crtc->base.dev;
  3323. struct drm_i915_private *dev_priv = dev->dev_private;
  3324. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3325. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3326. I915_READ(HTOTAL(cpu_transcoder)));
  3327. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3328. I915_READ(HBLANK(cpu_transcoder)));
  3329. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3330. I915_READ(HSYNC(cpu_transcoder)));
  3331. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3332. I915_READ(VTOTAL(cpu_transcoder)));
  3333. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3334. I915_READ(VBLANK(cpu_transcoder)));
  3335. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3336. I915_READ(VSYNC(cpu_transcoder)));
  3337. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3338. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3339. }
  3340. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3341. {
  3342. struct drm_i915_private *dev_priv = dev->dev_private;
  3343. uint32_t temp;
  3344. temp = I915_READ(SOUTH_CHICKEN1);
  3345. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3346. return;
  3347. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3348. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3349. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3350. if (enable)
  3351. temp |= FDI_BC_BIFURCATION_SELECT;
  3352. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3353. I915_WRITE(SOUTH_CHICKEN1, temp);
  3354. POSTING_READ(SOUTH_CHICKEN1);
  3355. }
  3356. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3357. {
  3358. struct drm_device *dev = intel_crtc->base.dev;
  3359. switch (intel_crtc->pipe) {
  3360. case PIPE_A:
  3361. break;
  3362. case PIPE_B:
  3363. if (intel_crtc->config->fdi_lanes > 2)
  3364. cpt_set_fdi_bc_bifurcation(dev, false);
  3365. else
  3366. cpt_set_fdi_bc_bifurcation(dev, true);
  3367. break;
  3368. case PIPE_C:
  3369. cpt_set_fdi_bc_bifurcation(dev, true);
  3370. break;
  3371. default:
  3372. BUG();
  3373. }
  3374. }
  3375. /*
  3376. * Enable PCH resources required for PCH ports:
  3377. * - PCH PLLs
  3378. * - FDI training & RX/TX
  3379. * - update transcoder timings
  3380. * - DP transcoding bits
  3381. * - transcoder
  3382. */
  3383. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3384. {
  3385. struct drm_device *dev = crtc->dev;
  3386. struct drm_i915_private *dev_priv = dev->dev_private;
  3387. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3388. int pipe = intel_crtc->pipe;
  3389. u32 reg, temp;
  3390. assert_pch_transcoder_disabled(dev_priv, pipe);
  3391. if (IS_IVYBRIDGE(dev))
  3392. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3393. /* Write the TU size bits before fdi link training, so that error
  3394. * detection works. */
  3395. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3396. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3397. /* For PCH output, training FDI link */
  3398. dev_priv->display.fdi_link_train(crtc);
  3399. /* We need to program the right clock selection before writing the pixel
  3400. * mutliplier into the DPLL. */
  3401. if (HAS_PCH_CPT(dev)) {
  3402. u32 sel;
  3403. temp = I915_READ(PCH_DPLL_SEL);
  3404. temp |= TRANS_DPLL_ENABLE(pipe);
  3405. sel = TRANS_DPLLB_SEL(pipe);
  3406. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3407. temp |= sel;
  3408. else
  3409. temp &= ~sel;
  3410. I915_WRITE(PCH_DPLL_SEL, temp);
  3411. }
  3412. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3413. * transcoder, and we actually should do this to not upset any PCH
  3414. * transcoder that already use the clock when we share it.
  3415. *
  3416. * Note that enable_shared_dpll tries to do the right thing, but
  3417. * get_shared_dpll unconditionally resets the pll - we need that to have
  3418. * the right LVDS enable sequence. */
  3419. intel_enable_shared_dpll(intel_crtc);
  3420. /* set transcoder timing, panel must allow it */
  3421. assert_panel_unlocked(dev_priv, pipe);
  3422. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3423. intel_fdi_normal_train(crtc);
  3424. /* For PCH DP, enable TRANS_DP_CTL */
  3425. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3426. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3427. reg = TRANS_DP_CTL(pipe);
  3428. temp = I915_READ(reg);
  3429. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3430. TRANS_DP_SYNC_MASK |
  3431. TRANS_DP_BPC_MASK);
  3432. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3433. TRANS_DP_ENH_FRAMING);
  3434. temp |= bpc << 9; /* same format but at 11:9 */
  3435. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3436. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3437. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3438. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3439. switch (intel_trans_dp_port_sel(crtc)) {
  3440. case PCH_DP_B:
  3441. temp |= TRANS_DP_PORT_SEL_B;
  3442. break;
  3443. case PCH_DP_C:
  3444. temp |= TRANS_DP_PORT_SEL_C;
  3445. break;
  3446. case PCH_DP_D:
  3447. temp |= TRANS_DP_PORT_SEL_D;
  3448. break;
  3449. default:
  3450. BUG();
  3451. }
  3452. I915_WRITE(reg, temp);
  3453. }
  3454. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3455. }
  3456. static void lpt_pch_enable(struct drm_crtc *crtc)
  3457. {
  3458. struct drm_device *dev = crtc->dev;
  3459. struct drm_i915_private *dev_priv = dev->dev_private;
  3460. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3461. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3462. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3463. lpt_program_iclkip(crtc);
  3464. /* Set transcoder timing. */
  3465. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3466. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3467. }
  3468. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3469. {
  3470. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3471. if (pll == NULL)
  3472. return;
  3473. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3474. WARN(1, "bad %s crtc mask\n", pll->name);
  3475. return;
  3476. }
  3477. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3478. if (pll->config.crtc_mask == 0) {
  3479. WARN_ON(pll->on);
  3480. WARN_ON(pll->active);
  3481. }
  3482. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3483. }
  3484. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3485. struct intel_crtc_state *crtc_state)
  3486. {
  3487. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3488. struct intel_shared_dpll *pll;
  3489. enum intel_dpll_id i;
  3490. if (HAS_PCH_IBX(dev_priv->dev)) {
  3491. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3492. i = (enum intel_dpll_id) crtc->pipe;
  3493. pll = &dev_priv->shared_dplls[i];
  3494. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3495. crtc->base.base.id, pll->name);
  3496. WARN_ON(pll->new_config->crtc_mask);
  3497. goto found;
  3498. }
  3499. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3500. pll = &dev_priv->shared_dplls[i];
  3501. /* Only want to check enabled timings first */
  3502. if (pll->new_config->crtc_mask == 0)
  3503. continue;
  3504. if (memcmp(&crtc_state->dpll_hw_state,
  3505. &pll->new_config->hw_state,
  3506. sizeof(pll->new_config->hw_state)) == 0) {
  3507. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3508. crtc->base.base.id, pll->name,
  3509. pll->new_config->crtc_mask,
  3510. pll->active);
  3511. goto found;
  3512. }
  3513. }
  3514. /* Ok no matching timings, maybe there's a free one? */
  3515. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3516. pll = &dev_priv->shared_dplls[i];
  3517. if (pll->new_config->crtc_mask == 0) {
  3518. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3519. crtc->base.base.id, pll->name);
  3520. goto found;
  3521. }
  3522. }
  3523. return NULL;
  3524. found:
  3525. if (pll->new_config->crtc_mask == 0)
  3526. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3527. crtc_state->shared_dpll = i;
  3528. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3529. pipe_name(crtc->pipe));
  3530. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3531. return pll;
  3532. }
  3533. /**
  3534. * intel_shared_dpll_start_config - start a new PLL staged config
  3535. * @dev_priv: DRM device
  3536. * @clear_pipes: mask of pipes that will have their PLLs freed
  3537. *
  3538. * Starts a new PLL staged config, copying the current config but
  3539. * releasing the references of pipes specified in clear_pipes.
  3540. */
  3541. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3542. unsigned clear_pipes)
  3543. {
  3544. struct intel_shared_dpll *pll;
  3545. enum intel_dpll_id i;
  3546. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3547. pll = &dev_priv->shared_dplls[i];
  3548. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3549. GFP_KERNEL);
  3550. if (!pll->new_config)
  3551. goto cleanup;
  3552. pll->new_config->crtc_mask &= ~clear_pipes;
  3553. }
  3554. return 0;
  3555. cleanup:
  3556. while (--i >= 0) {
  3557. pll = &dev_priv->shared_dplls[i];
  3558. kfree(pll->new_config);
  3559. pll->new_config = NULL;
  3560. }
  3561. return -ENOMEM;
  3562. }
  3563. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3564. {
  3565. struct intel_shared_dpll *pll;
  3566. enum intel_dpll_id i;
  3567. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3568. pll = &dev_priv->shared_dplls[i];
  3569. WARN_ON(pll->new_config == &pll->config);
  3570. pll->config = *pll->new_config;
  3571. kfree(pll->new_config);
  3572. pll->new_config = NULL;
  3573. }
  3574. }
  3575. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3576. {
  3577. struct intel_shared_dpll *pll;
  3578. enum intel_dpll_id i;
  3579. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3580. pll = &dev_priv->shared_dplls[i];
  3581. WARN_ON(pll->new_config == &pll->config);
  3582. kfree(pll->new_config);
  3583. pll->new_config = NULL;
  3584. }
  3585. }
  3586. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3587. {
  3588. struct drm_i915_private *dev_priv = dev->dev_private;
  3589. int dslreg = PIPEDSL(pipe);
  3590. u32 temp;
  3591. temp = I915_READ(dslreg);
  3592. udelay(500);
  3593. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3594. if (wait_for(I915_READ(dslreg) != temp, 5))
  3595. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3596. }
  3597. }
  3598. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3599. {
  3600. struct drm_device *dev = crtc->base.dev;
  3601. struct drm_i915_private *dev_priv = dev->dev_private;
  3602. int pipe = crtc->pipe;
  3603. if (crtc->config->pch_pfit.enabled) {
  3604. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3605. I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3606. I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3607. }
  3608. }
  3609. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3610. {
  3611. struct drm_device *dev = crtc->base.dev;
  3612. struct drm_i915_private *dev_priv = dev->dev_private;
  3613. int pipe = crtc->pipe;
  3614. if (crtc->config->pch_pfit.enabled) {
  3615. /* Force use of hard-coded filter coefficients
  3616. * as some pre-programmed values are broken,
  3617. * e.g. x201.
  3618. */
  3619. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3620. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3621. PF_PIPE_SEL_IVB(pipe));
  3622. else
  3623. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3624. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3625. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3626. }
  3627. }
  3628. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3629. {
  3630. struct drm_device *dev = crtc->dev;
  3631. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3632. struct drm_plane *plane;
  3633. struct intel_plane *intel_plane;
  3634. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3635. intel_plane = to_intel_plane(plane);
  3636. if (intel_plane->pipe == pipe)
  3637. intel_plane_restore(&intel_plane->base);
  3638. }
  3639. }
  3640. /*
  3641. * Disable a plane internally without actually modifying the plane's state.
  3642. * This will allow us to easily restore the plane later by just reprogramming
  3643. * its state.
  3644. */
  3645. static void disable_plane_internal(struct drm_plane *plane)
  3646. {
  3647. struct intel_plane *intel_plane = to_intel_plane(plane);
  3648. struct drm_plane_state *state =
  3649. plane->funcs->atomic_duplicate_state(plane);
  3650. struct intel_plane_state *intel_state = to_intel_plane_state(state);
  3651. intel_state->visible = false;
  3652. intel_plane->commit_plane(plane, intel_state);
  3653. intel_plane_destroy_state(plane, state);
  3654. }
  3655. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3656. {
  3657. struct drm_device *dev = crtc->dev;
  3658. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3659. struct drm_plane *plane;
  3660. struct intel_plane *intel_plane;
  3661. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3662. intel_plane = to_intel_plane(plane);
  3663. if (plane->fb && intel_plane->pipe == pipe)
  3664. disable_plane_internal(plane);
  3665. }
  3666. }
  3667. void hsw_enable_ips(struct intel_crtc *crtc)
  3668. {
  3669. struct drm_device *dev = crtc->base.dev;
  3670. struct drm_i915_private *dev_priv = dev->dev_private;
  3671. if (!crtc->config->ips_enabled)
  3672. return;
  3673. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3674. intel_wait_for_vblank(dev, crtc->pipe);
  3675. assert_plane_enabled(dev_priv, crtc->plane);
  3676. if (IS_BROADWELL(dev)) {
  3677. mutex_lock(&dev_priv->rps.hw_lock);
  3678. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3679. mutex_unlock(&dev_priv->rps.hw_lock);
  3680. /* Quoting Art Runyan: "its not safe to expect any particular
  3681. * value in IPS_CTL bit 31 after enabling IPS through the
  3682. * mailbox." Moreover, the mailbox may return a bogus state,
  3683. * so we need to just enable it and continue on.
  3684. */
  3685. } else {
  3686. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3687. /* The bit only becomes 1 in the next vblank, so this wait here
  3688. * is essentially intel_wait_for_vblank. If we don't have this
  3689. * and don't wait for vblanks until the end of crtc_enable, then
  3690. * the HW state readout code will complain that the expected
  3691. * IPS_CTL value is not the one we read. */
  3692. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3693. DRM_ERROR("Timed out waiting for IPS enable\n");
  3694. }
  3695. }
  3696. void hsw_disable_ips(struct intel_crtc *crtc)
  3697. {
  3698. struct drm_device *dev = crtc->base.dev;
  3699. struct drm_i915_private *dev_priv = dev->dev_private;
  3700. if (!crtc->config->ips_enabled)
  3701. return;
  3702. assert_plane_enabled(dev_priv, crtc->plane);
  3703. if (IS_BROADWELL(dev)) {
  3704. mutex_lock(&dev_priv->rps.hw_lock);
  3705. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3706. mutex_unlock(&dev_priv->rps.hw_lock);
  3707. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3708. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3709. DRM_ERROR("Timed out waiting for IPS disable\n");
  3710. } else {
  3711. I915_WRITE(IPS_CTL, 0);
  3712. POSTING_READ(IPS_CTL);
  3713. }
  3714. /* We need to wait for a vblank before we can disable the plane. */
  3715. intel_wait_for_vblank(dev, crtc->pipe);
  3716. }
  3717. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3718. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3719. {
  3720. struct drm_device *dev = crtc->dev;
  3721. struct drm_i915_private *dev_priv = dev->dev_private;
  3722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3723. enum pipe pipe = intel_crtc->pipe;
  3724. int palreg = PALETTE(pipe);
  3725. int i;
  3726. bool reenable_ips = false;
  3727. /* The clocks have to be on to load the palette. */
  3728. if (!crtc->state->enable || !intel_crtc->active)
  3729. return;
  3730. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3731. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3732. assert_dsi_pll_enabled(dev_priv);
  3733. else
  3734. assert_pll_enabled(dev_priv, pipe);
  3735. }
  3736. /* use legacy palette for Ironlake */
  3737. if (!HAS_GMCH_DISPLAY(dev))
  3738. palreg = LGC_PALETTE(pipe);
  3739. /* Workaround : Do not read or write the pipe palette/gamma data while
  3740. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3741. */
  3742. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3743. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3744. GAMMA_MODE_MODE_SPLIT)) {
  3745. hsw_disable_ips(intel_crtc);
  3746. reenable_ips = true;
  3747. }
  3748. for (i = 0; i < 256; i++) {
  3749. I915_WRITE(palreg + 4 * i,
  3750. (intel_crtc->lut_r[i] << 16) |
  3751. (intel_crtc->lut_g[i] << 8) |
  3752. intel_crtc->lut_b[i]);
  3753. }
  3754. if (reenable_ips)
  3755. hsw_enable_ips(intel_crtc);
  3756. }
  3757. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3758. {
  3759. if (!enable && intel_crtc->overlay) {
  3760. struct drm_device *dev = intel_crtc->base.dev;
  3761. struct drm_i915_private *dev_priv = dev->dev_private;
  3762. mutex_lock(&dev->struct_mutex);
  3763. dev_priv->mm.interruptible = false;
  3764. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3765. dev_priv->mm.interruptible = true;
  3766. mutex_unlock(&dev->struct_mutex);
  3767. }
  3768. /* Let userspace switch the overlay on again. In most cases userspace
  3769. * has to recompute where to put it anyway.
  3770. */
  3771. }
  3772. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3773. {
  3774. struct drm_device *dev = crtc->dev;
  3775. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3776. int pipe = intel_crtc->pipe;
  3777. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3778. intel_enable_sprite_planes(crtc);
  3779. intel_crtc_update_cursor(crtc, true);
  3780. intel_crtc_dpms_overlay(intel_crtc, true);
  3781. hsw_enable_ips(intel_crtc);
  3782. mutex_lock(&dev->struct_mutex);
  3783. intel_fbc_update(dev);
  3784. mutex_unlock(&dev->struct_mutex);
  3785. /*
  3786. * FIXME: Once we grow proper nuclear flip support out of this we need
  3787. * to compute the mask of flip planes precisely. For the time being
  3788. * consider this a flip from a NULL plane.
  3789. */
  3790. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3791. }
  3792. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3793. {
  3794. struct drm_device *dev = crtc->dev;
  3795. struct drm_i915_private *dev_priv = dev->dev_private;
  3796. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3797. int pipe = intel_crtc->pipe;
  3798. intel_crtc_wait_for_pending_flips(crtc);
  3799. if (dev_priv->fbc.crtc == intel_crtc)
  3800. intel_fbc_disable(dev);
  3801. hsw_disable_ips(intel_crtc);
  3802. intel_crtc_dpms_overlay(intel_crtc, false);
  3803. intel_crtc_update_cursor(crtc, false);
  3804. intel_disable_sprite_planes(crtc);
  3805. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3806. /*
  3807. * FIXME: Once we grow proper nuclear flip support out of this we need
  3808. * to compute the mask of flip planes precisely. For the time being
  3809. * consider this a flip to a NULL plane.
  3810. */
  3811. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3812. }
  3813. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3814. {
  3815. struct drm_device *dev = crtc->dev;
  3816. struct drm_i915_private *dev_priv = dev->dev_private;
  3817. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3818. struct intel_encoder *encoder;
  3819. int pipe = intel_crtc->pipe;
  3820. WARN_ON(!crtc->state->enable);
  3821. if (intel_crtc->active)
  3822. return;
  3823. if (intel_crtc->config->has_pch_encoder)
  3824. intel_prepare_shared_dpll(intel_crtc);
  3825. if (intel_crtc->config->has_dp_encoder)
  3826. intel_dp_set_m_n(intel_crtc, M1_N1);
  3827. intel_set_pipe_timings(intel_crtc);
  3828. if (intel_crtc->config->has_pch_encoder) {
  3829. intel_cpu_transcoder_set_m_n(intel_crtc,
  3830. &intel_crtc->config->fdi_m_n, NULL);
  3831. }
  3832. ironlake_set_pipeconf(crtc);
  3833. intel_crtc->active = true;
  3834. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3835. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3836. for_each_encoder_on_crtc(dev, crtc, encoder)
  3837. if (encoder->pre_enable)
  3838. encoder->pre_enable(encoder);
  3839. if (intel_crtc->config->has_pch_encoder) {
  3840. /* Note: FDI PLL enabling _must_ be done before we enable the
  3841. * cpu pipes, hence this is separate from all the other fdi/pch
  3842. * enabling. */
  3843. ironlake_fdi_pll_enable(intel_crtc);
  3844. } else {
  3845. assert_fdi_tx_disabled(dev_priv, pipe);
  3846. assert_fdi_rx_disabled(dev_priv, pipe);
  3847. }
  3848. ironlake_pfit_enable(intel_crtc);
  3849. /*
  3850. * On ILK+ LUT must be loaded before the pipe is running but with
  3851. * clocks enabled
  3852. */
  3853. intel_crtc_load_lut(crtc);
  3854. intel_update_watermarks(crtc);
  3855. intel_enable_pipe(intel_crtc);
  3856. if (intel_crtc->config->has_pch_encoder)
  3857. ironlake_pch_enable(crtc);
  3858. assert_vblank_disabled(crtc);
  3859. drm_crtc_vblank_on(crtc);
  3860. for_each_encoder_on_crtc(dev, crtc, encoder)
  3861. encoder->enable(encoder);
  3862. if (HAS_PCH_CPT(dev))
  3863. cpt_verify_modeset(dev, intel_crtc->pipe);
  3864. intel_crtc_enable_planes(crtc);
  3865. }
  3866. /* IPS only exists on ULT machines and is tied to pipe A. */
  3867. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3868. {
  3869. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3870. }
  3871. /*
  3872. * This implements the workaround described in the "notes" section of the mode
  3873. * set sequence documentation. When going from no pipes or single pipe to
  3874. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3875. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3876. */
  3877. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3878. {
  3879. struct drm_device *dev = crtc->base.dev;
  3880. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3881. /* We want to get the other_active_crtc only if there's only 1 other
  3882. * active crtc. */
  3883. for_each_intel_crtc(dev, crtc_it) {
  3884. if (!crtc_it->active || crtc_it == crtc)
  3885. continue;
  3886. if (other_active_crtc)
  3887. return;
  3888. other_active_crtc = crtc_it;
  3889. }
  3890. if (!other_active_crtc)
  3891. return;
  3892. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3893. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3894. }
  3895. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3896. {
  3897. struct drm_device *dev = crtc->dev;
  3898. struct drm_i915_private *dev_priv = dev->dev_private;
  3899. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3900. struct intel_encoder *encoder;
  3901. int pipe = intel_crtc->pipe;
  3902. WARN_ON(!crtc->state->enable);
  3903. if (intel_crtc->active)
  3904. return;
  3905. if (intel_crtc_to_shared_dpll(intel_crtc))
  3906. intel_enable_shared_dpll(intel_crtc);
  3907. if (intel_crtc->config->has_dp_encoder)
  3908. intel_dp_set_m_n(intel_crtc, M1_N1);
  3909. intel_set_pipe_timings(intel_crtc);
  3910. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  3911. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  3912. intel_crtc->config->pixel_multiplier - 1);
  3913. }
  3914. if (intel_crtc->config->has_pch_encoder) {
  3915. intel_cpu_transcoder_set_m_n(intel_crtc,
  3916. &intel_crtc->config->fdi_m_n, NULL);
  3917. }
  3918. haswell_set_pipeconf(crtc);
  3919. intel_set_pipe_csc(crtc);
  3920. intel_crtc->active = true;
  3921. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3922. for_each_encoder_on_crtc(dev, crtc, encoder)
  3923. if (encoder->pre_enable)
  3924. encoder->pre_enable(encoder);
  3925. if (intel_crtc->config->has_pch_encoder) {
  3926. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3927. true);
  3928. dev_priv->display.fdi_link_train(crtc);
  3929. }
  3930. intel_ddi_enable_pipe_clock(intel_crtc);
  3931. if (IS_SKYLAKE(dev))
  3932. skylake_pfit_enable(intel_crtc);
  3933. else
  3934. ironlake_pfit_enable(intel_crtc);
  3935. /*
  3936. * On ILK+ LUT must be loaded before the pipe is running but with
  3937. * clocks enabled
  3938. */
  3939. intel_crtc_load_lut(crtc);
  3940. intel_ddi_set_pipe_settings(crtc);
  3941. intel_ddi_enable_transcoder_func(crtc);
  3942. intel_update_watermarks(crtc);
  3943. intel_enable_pipe(intel_crtc);
  3944. if (intel_crtc->config->has_pch_encoder)
  3945. lpt_pch_enable(crtc);
  3946. if (intel_crtc->config->dp_encoder_is_mst)
  3947. intel_ddi_set_vc_payload_alloc(crtc, true);
  3948. assert_vblank_disabled(crtc);
  3949. drm_crtc_vblank_on(crtc);
  3950. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3951. encoder->enable(encoder);
  3952. intel_opregion_notify_encoder(encoder, true);
  3953. }
  3954. /* If we change the relative order between pipe/planes enabling, we need
  3955. * to change the workaround. */
  3956. haswell_mode_set_planes_workaround(intel_crtc);
  3957. intel_crtc_enable_planes(crtc);
  3958. }
  3959. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3960. {
  3961. struct drm_device *dev = crtc->base.dev;
  3962. struct drm_i915_private *dev_priv = dev->dev_private;
  3963. int pipe = crtc->pipe;
  3964. /* To avoid upsetting the power well on haswell only disable the pfit if
  3965. * it's in use. The hw state code will make sure we get this right. */
  3966. if (crtc->config->pch_pfit.enabled) {
  3967. I915_WRITE(PS_CTL(pipe), 0);
  3968. I915_WRITE(PS_WIN_POS(pipe), 0);
  3969. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3970. }
  3971. }
  3972. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3973. {
  3974. struct drm_device *dev = crtc->base.dev;
  3975. struct drm_i915_private *dev_priv = dev->dev_private;
  3976. int pipe = crtc->pipe;
  3977. /* To avoid upsetting the power well on haswell only disable the pfit if
  3978. * it's in use. The hw state code will make sure we get this right. */
  3979. if (crtc->config->pch_pfit.enabled) {
  3980. I915_WRITE(PF_CTL(pipe), 0);
  3981. I915_WRITE(PF_WIN_POS(pipe), 0);
  3982. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3983. }
  3984. }
  3985. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3986. {
  3987. struct drm_device *dev = crtc->dev;
  3988. struct drm_i915_private *dev_priv = dev->dev_private;
  3989. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3990. struct intel_encoder *encoder;
  3991. int pipe = intel_crtc->pipe;
  3992. u32 reg, temp;
  3993. if (!intel_crtc->active)
  3994. return;
  3995. intel_crtc_disable_planes(crtc);
  3996. for_each_encoder_on_crtc(dev, crtc, encoder)
  3997. encoder->disable(encoder);
  3998. drm_crtc_vblank_off(crtc);
  3999. assert_vblank_disabled(crtc);
  4000. if (intel_crtc->config->has_pch_encoder)
  4001. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4002. intel_disable_pipe(intel_crtc);
  4003. ironlake_pfit_disable(intel_crtc);
  4004. for_each_encoder_on_crtc(dev, crtc, encoder)
  4005. if (encoder->post_disable)
  4006. encoder->post_disable(encoder);
  4007. if (intel_crtc->config->has_pch_encoder) {
  4008. ironlake_fdi_disable(crtc);
  4009. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4010. if (HAS_PCH_CPT(dev)) {
  4011. /* disable TRANS_DP_CTL */
  4012. reg = TRANS_DP_CTL(pipe);
  4013. temp = I915_READ(reg);
  4014. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4015. TRANS_DP_PORT_SEL_MASK);
  4016. temp |= TRANS_DP_PORT_SEL_NONE;
  4017. I915_WRITE(reg, temp);
  4018. /* disable DPLL_SEL */
  4019. temp = I915_READ(PCH_DPLL_SEL);
  4020. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4021. I915_WRITE(PCH_DPLL_SEL, temp);
  4022. }
  4023. /* disable PCH DPLL */
  4024. intel_disable_shared_dpll(intel_crtc);
  4025. ironlake_fdi_pll_disable(intel_crtc);
  4026. }
  4027. intel_crtc->active = false;
  4028. intel_update_watermarks(crtc);
  4029. mutex_lock(&dev->struct_mutex);
  4030. intel_fbc_update(dev);
  4031. mutex_unlock(&dev->struct_mutex);
  4032. }
  4033. static void haswell_crtc_disable(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. struct intel_encoder *encoder;
  4039. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4040. if (!intel_crtc->active)
  4041. return;
  4042. intel_crtc_disable_planes(crtc);
  4043. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4044. intel_opregion_notify_encoder(encoder, false);
  4045. encoder->disable(encoder);
  4046. }
  4047. drm_crtc_vblank_off(crtc);
  4048. assert_vblank_disabled(crtc);
  4049. if (intel_crtc->config->has_pch_encoder)
  4050. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4051. false);
  4052. intel_disable_pipe(intel_crtc);
  4053. if (intel_crtc->config->dp_encoder_is_mst)
  4054. intel_ddi_set_vc_payload_alloc(crtc, false);
  4055. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4056. if (IS_SKYLAKE(dev))
  4057. skylake_pfit_disable(intel_crtc);
  4058. else
  4059. ironlake_pfit_disable(intel_crtc);
  4060. intel_ddi_disable_pipe_clock(intel_crtc);
  4061. if (intel_crtc->config->has_pch_encoder) {
  4062. lpt_disable_pch_transcoder(dev_priv);
  4063. intel_ddi_fdi_disable(crtc);
  4064. }
  4065. for_each_encoder_on_crtc(dev, crtc, encoder)
  4066. if (encoder->post_disable)
  4067. encoder->post_disable(encoder);
  4068. intel_crtc->active = false;
  4069. intel_update_watermarks(crtc);
  4070. mutex_lock(&dev->struct_mutex);
  4071. intel_fbc_update(dev);
  4072. mutex_unlock(&dev->struct_mutex);
  4073. if (intel_crtc_to_shared_dpll(intel_crtc))
  4074. intel_disable_shared_dpll(intel_crtc);
  4075. }
  4076. static void ironlake_crtc_off(struct drm_crtc *crtc)
  4077. {
  4078. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4079. intel_put_shared_dpll(intel_crtc);
  4080. }
  4081. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4082. {
  4083. struct drm_device *dev = crtc->base.dev;
  4084. struct drm_i915_private *dev_priv = dev->dev_private;
  4085. struct intel_crtc_state *pipe_config = crtc->config;
  4086. if (!pipe_config->gmch_pfit.control)
  4087. return;
  4088. /*
  4089. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4090. * according to register description and PRM.
  4091. */
  4092. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4093. assert_pipe_disabled(dev_priv, crtc->pipe);
  4094. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4095. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4096. /* Border color in case we don't scale up to the full screen. Black by
  4097. * default, change to something else for debugging. */
  4098. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4099. }
  4100. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4101. {
  4102. switch (port) {
  4103. case PORT_A:
  4104. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4105. case PORT_B:
  4106. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4107. case PORT_C:
  4108. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4109. case PORT_D:
  4110. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4111. default:
  4112. WARN_ON_ONCE(1);
  4113. return POWER_DOMAIN_PORT_OTHER;
  4114. }
  4115. }
  4116. #define for_each_power_domain(domain, mask) \
  4117. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4118. if ((1 << (domain)) & (mask))
  4119. enum intel_display_power_domain
  4120. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4121. {
  4122. struct drm_device *dev = intel_encoder->base.dev;
  4123. struct intel_digital_port *intel_dig_port;
  4124. switch (intel_encoder->type) {
  4125. case INTEL_OUTPUT_UNKNOWN:
  4126. /* Only DDI platforms should ever use this output type */
  4127. WARN_ON_ONCE(!HAS_DDI(dev));
  4128. case INTEL_OUTPUT_DISPLAYPORT:
  4129. case INTEL_OUTPUT_HDMI:
  4130. case INTEL_OUTPUT_EDP:
  4131. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4132. return port_to_power_domain(intel_dig_port->port);
  4133. case INTEL_OUTPUT_DP_MST:
  4134. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4135. return port_to_power_domain(intel_dig_port->port);
  4136. case INTEL_OUTPUT_ANALOG:
  4137. return POWER_DOMAIN_PORT_CRT;
  4138. case INTEL_OUTPUT_DSI:
  4139. return POWER_DOMAIN_PORT_DSI;
  4140. default:
  4141. return POWER_DOMAIN_PORT_OTHER;
  4142. }
  4143. }
  4144. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4145. {
  4146. struct drm_device *dev = crtc->dev;
  4147. struct intel_encoder *intel_encoder;
  4148. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4149. enum pipe pipe = intel_crtc->pipe;
  4150. unsigned long mask;
  4151. enum transcoder transcoder;
  4152. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4153. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4154. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4155. if (intel_crtc->config->pch_pfit.enabled ||
  4156. intel_crtc->config->pch_pfit.force_thru)
  4157. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4158. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4159. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4160. return mask;
  4161. }
  4162. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  4163. {
  4164. struct drm_i915_private *dev_priv = dev->dev_private;
  4165. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4166. struct intel_crtc *crtc;
  4167. /*
  4168. * First get all needed power domains, then put all unneeded, to avoid
  4169. * any unnecessary toggling of the power wells.
  4170. */
  4171. for_each_intel_crtc(dev, crtc) {
  4172. enum intel_display_power_domain domain;
  4173. if (!crtc->base.state->enable)
  4174. continue;
  4175. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4176. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4177. intel_display_power_get(dev_priv, domain);
  4178. }
  4179. if (dev_priv->display.modeset_global_resources)
  4180. dev_priv->display.modeset_global_resources(dev);
  4181. for_each_intel_crtc(dev, crtc) {
  4182. enum intel_display_power_domain domain;
  4183. for_each_power_domain(domain, crtc->enabled_power_domains)
  4184. intel_display_power_put(dev_priv, domain);
  4185. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4186. }
  4187. intel_display_set_init_power(dev_priv, false);
  4188. }
  4189. /* returns HPLL frequency in kHz */
  4190. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4191. {
  4192. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4193. /* Obtain SKU information */
  4194. mutex_lock(&dev_priv->dpio_lock);
  4195. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4196. CCK_FUSE_HPLL_FREQ_MASK;
  4197. mutex_unlock(&dev_priv->dpio_lock);
  4198. return vco_freq[hpll_freq] * 1000;
  4199. }
  4200. static void vlv_update_cdclk(struct drm_device *dev)
  4201. {
  4202. struct drm_i915_private *dev_priv = dev->dev_private;
  4203. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4204. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4205. dev_priv->vlv_cdclk_freq);
  4206. /*
  4207. * Program the gmbus_freq based on the cdclk frequency.
  4208. * BSpec erroneously claims we should aim for 4MHz, but
  4209. * in fact 1MHz is the correct frequency.
  4210. */
  4211. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4212. }
  4213. /* Adjust CDclk dividers to allow high res or save power if possible */
  4214. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4215. {
  4216. struct drm_i915_private *dev_priv = dev->dev_private;
  4217. u32 val, cmd;
  4218. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4219. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4220. cmd = 2;
  4221. else if (cdclk == 266667)
  4222. cmd = 1;
  4223. else
  4224. cmd = 0;
  4225. mutex_lock(&dev_priv->rps.hw_lock);
  4226. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4227. val &= ~DSPFREQGUAR_MASK;
  4228. val |= (cmd << DSPFREQGUAR_SHIFT);
  4229. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4230. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4231. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4232. 50)) {
  4233. DRM_ERROR("timed out waiting for CDclk change\n");
  4234. }
  4235. mutex_unlock(&dev_priv->rps.hw_lock);
  4236. if (cdclk == 400000) {
  4237. u32 divider;
  4238. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4239. mutex_lock(&dev_priv->dpio_lock);
  4240. /* adjust cdclk divider */
  4241. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4242. val &= ~DISPLAY_FREQUENCY_VALUES;
  4243. val |= divider;
  4244. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4245. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4246. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4247. 50))
  4248. DRM_ERROR("timed out waiting for CDclk change\n");
  4249. mutex_unlock(&dev_priv->dpio_lock);
  4250. }
  4251. mutex_lock(&dev_priv->dpio_lock);
  4252. /* adjust self-refresh exit latency value */
  4253. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4254. val &= ~0x7f;
  4255. /*
  4256. * For high bandwidth configs, we set a higher latency in the bunit
  4257. * so that the core display fetch happens in time to avoid underruns.
  4258. */
  4259. if (cdclk == 400000)
  4260. val |= 4500 / 250; /* 4.5 usec */
  4261. else
  4262. val |= 3000 / 250; /* 3.0 usec */
  4263. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4264. mutex_unlock(&dev_priv->dpio_lock);
  4265. vlv_update_cdclk(dev);
  4266. }
  4267. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4268. {
  4269. struct drm_i915_private *dev_priv = dev->dev_private;
  4270. u32 val, cmd;
  4271. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4272. switch (cdclk) {
  4273. case 333333:
  4274. case 320000:
  4275. case 266667:
  4276. case 200000:
  4277. break;
  4278. default:
  4279. MISSING_CASE(cdclk);
  4280. return;
  4281. }
  4282. /*
  4283. * Specs are full of misinformation, but testing on actual
  4284. * hardware has shown that we just need to write the desired
  4285. * CCK divider into the Punit register.
  4286. */
  4287. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4288. mutex_lock(&dev_priv->rps.hw_lock);
  4289. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4290. val &= ~DSPFREQGUAR_MASK_CHV;
  4291. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4292. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4293. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4294. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4295. 50)) {
  4296. DRM_ERROR("timed out waiting for CDclk change\n");
  4297. }
  4298. mutex_unlock(&dev_priv->rps.hw_lock);
  4299. vlv_update_cdclk(dev);
  4300. }
  4301. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4302. int max_pixclk)
  4303. {
  4304. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4305. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4306. /*
  4307. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4308. * 200MHz
  4309. * 267MHz
  4310. * 320/333MHz (depends on HPLL freq)
  4311. * 400MHz (VLV only)
  4312. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4313. * of the lower bin and adjust if needed.
  4314. *
  4315. * We seem to get an unstable or solid color picture at 200MHz.
  4316. * Not sure what's wrong. For now use 200MHz only when all pipes
  4317. * are off.
  4318. */
  4319. if (!IS_CHERRYVIEW(dev_priv) &&
  4320. max_pixclk > freq_320*limit/100)
  4321. return 400000;
  4322. else if (max_pixclk > 266667*limit/100)
  4323. return freq_320;
  4324. else if (max_pixclk > 0)
  4325. return 266667;
  4326. else
  4327. return 200000;
  4328. }
  4329. /* compute the max pixel clock for new configuration */
  4330. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4331. {
  4332. struct drm_device *dev = dev_priv->dev;
  4333. struct intel_crtc *intel_crtc;
  4334. int max_pixclk = 0;
  4335. for_each_intel_crtc(dev, intel_crtc) {
  4336. if (intel_crtc->new_enabled)
  4337. max_pixclk = max(max_pixclk,
  4338. intel_crtc->new_config->base.adjusted_mode.crtc_clock);
  4339. }
  4340. return max_pixclk;
  4341. }
  4342. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4343. unsigned *prepare_pipes)
  4344. {
  4345. struct drm_i915_private *dev_priv = dev->dev_private;
  4346. struct intel_crtc *intel_crtc;
  4347. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4348. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4349. dev_priv->vlv_cdclk_freq)
  4350. return;
  4351. /* disable/enable all currently active pipes while we change cdclk */
  4352. for_each_intel_crtc(dev, intel_crtc)
  4353. if (intel_crtc->base.state->enable)
  4354. *prepare_pipes |= (1 << intel_crtc->pipe);
  4355. }
  4356. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  4357. {
  4358. unsigned int credits, default_credits;
  4359. if (IS_CHERRYVIEW(dev_priv))
  4360. default_credits = PFI_CREDIT(12);
  4361. else
  4362. default_credits = PFI_CREDIT(8);
  4363. if (DIV_ROUND_CLOSEST(dev_priv->vlv_cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  4364. /* CHV suggested value is 31 or 63 */
  4365. if (IS_CHERRYVIEW(dev_priv))
  4366. credits = PFI_CREDIT_31;
  4367. else
  4368. credits = PFI_CREDIT(15);
  4369. } else {
  4370. credits = default_credits;
  4371. }
  4372. /*
  4373. * WA - write default credits before re-programming
  4374. * FIXME: should we also set the resend bit here?
  4375. */
  4376. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4377. default_credits);
  4378. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  4379. credits | PFI_CREDIT_RESEND);
  4380. /*
  4381. * FIXME is this guaranteed to clear
  4382. * immediately or should we poll for it?
  4383. */
  4384. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  4385. }
  4386. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4387. {
  4388. struct drm_i915_private *dev_priv = dev->dev_private;
  4389. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4390. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4391. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4392. /*
  4393. * FIXME: We can end up here with all power domains off, yet
  4394. * with a CDCLK frequency other than the minimum. To account
  4395. * for this take the PIPE-A power domain, which covers the HW
  4396. * blocks needed for the following programming. This can be
  4397. * removed once it's guaranteed that we get here either with
  4398. * the minimum CDCLK set, or the required power domains
  4399. * enabled.
  4400. */
  4401. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4402. if (IS_CHERRYVIEW(dev))
  4403. cherryview_set_cdclk(dev, req_cdclk);
  4404. else
  4405. valleyview_set_cdclk(dev, req_cdclk);
  4406. vlv_program_pfi_credits(dev_priv);
  4407. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4408. }
  4409. }
  4410. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4411. {
  4412. struct drm_device *dev = crtc->dev;
  4413. struct drm_i915_private *dev_priv = to_i915(dev);
  4414. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4415. struct intel_encoder *encoder;
  4416. int pipe = intel_crtc->pipe;
  4417. bool is_dsi;
  4418. WARN_ON(!crtc->state->enable);
  4419. if (intel_crtc->active)
  4420. return;
  4421. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4422. if (!is_dsi) {
  4423. if (IS_CHERRYVIEW(dev))
  4424. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4425. else
  4426. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4427. }
  4428. if (intel_crtc->config->has_dp_encoder)
  4429. intel_dp_set_m_n(intel_crtc, M1_N1);
  4430. intel_set_pipe_timings(intel_crtc);
  4431. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4432. struct drm_i915_private *dev_priv = dev->dev_private;
  4433. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4434. I915_WRITE(CHV_CANVAS(pipe), 0);
  4435. }
  4436. i9xx_set_pipeconf(intel_crtc);
  4437. intel_crtc->active = true;
  4438. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4439. for_each_encoder_on_crtc(dev, crtc, encoder)
  4440. if (encoder->pre_pll_enable)
  4441. encoder->pre_pll_enable(encoder);
  4442. if (!is_dsi) {
  4443. if (IS_CHERRYVIEW(dev))
  4444. chv_enable_pll(intel_crtc, intel_crtc->config);
  4445. else
  4446. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4447. }
  4448. for_each_encoder_on_crtc(dev, crtc, encoder)
  4449. if (encoder->pre_enable)
  4450. encoder->pre_enable(encoder);
  4451. i9xx_pfit_enable(intel_crtc);
  4452. intel_crtc_load_lut(crtc);
  4453. intel_update_watermarks(crtc);
  4454. intel_enable_pipe(intel_crtc);
  4455. assert_vblank_disabled(crtc);
  4456. drm_crtc_vblank_on(crtc);
  4457. for_each_encoder_on_crtc(dev, crtc, encoder)
  4458. encoder->enable(encoder);
  4459. intel_crtc_enable_planes(crtc);
  4460. /* Underruns don't raise interrupts, so check manually. */
  4461. i9xx_check_fifo_underruns(dev_priv);
  4462. }
  4463. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4464. {
  4465. struct drm_device *dev = crtc->base.dev;
  4466. struct drm_i915_private *dev_priv = dev->dev_private;
  4467. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4468. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4469. }
  4470. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4471. {
  4472. struct drm_device *dev = crtc->dev;
  4473. struct drm_i915_private *dev_priv = to_i915(dev);
  4474. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4475. struct intel_encoder *encoder;
  4476. int pipe = intel_crtc->pipe;
  4477. WARN_ON(!crtc->state->enable);
  4478. if (intel_crtc->active)
  4479. return;
  4480. i9xx_set_pll_dividers(intel_crtc);
  4481. if (intel_crtc->config->has_dp_encoder)
  4482. intel_dp_set_m_n(intel_crtc, M1_N1);
  4483. intel_set_pipe_timings(intel_crtc);
  4484. i9xx_set_pipeconf(intel_crtc);
  4485. intel_crtc->active = true;
  4486. if (!IS_GEN2(dev))
  4487. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4488. for_each_encoder_on_crtc(dev, crtc, encoder)
  4489. if (encoder->pre_enable)
  4490. encoder->pre_enable(encoder);
  4491. i9xx_enable_pll(intel_crtc);
  4492. i9xx_pfit_enable(intel_crtc);
  4493. intel_crtc_load_lut(crtc);
  4494. intel_update_watermarks(crtc);
  4495. intel_enable_pipe(intel_crtc);
  4496. assert_vblank_disabled(crtc);
  4497. drm_crtc_vblank_on(crtc);
  4498. for_each_encoder_on_crtc(dev, crtc, encoder)
  4499. encoder->enable(encoder);
  4500. intel_crtc_enable_planes(crtc);
  4501. /*
  4502. * Gen2 reports pipe underruns whenever all planes are disabled.
  4503. * So don't enable underrun reporting before at least some planes
  4504. * are enabled.
  4505. * FIXME: Need to fix the logic to work when we turn off all planes
  4506. * but leave the pipe running.
  4507. */
  4508. if (IS_GEN2(dev))
  4509. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4510. /* Underruns don't raise interrupts, so check manually. */
  4511. i9xx_check_fifo_underruns(dev_priv);
  4512. }
  4513. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4514. {
  4515. struct drm_device *dev = crtc->base.dev;
  4516. struct drm_i915_private *dev_priv = dev->dev_private;
  4517. if (!crtc->config->gmch_pfit.control)
  4518. return;
  4519. assert_pipe_disabled(dev_priv, crtc->pipe);
  4520. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4521. I915_READ(PFIT_CONTROL));
  4522. I915_WRITE(PFIT_CONTROL, 0);
  4523. }
  4524. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4525. {
  4526. struct drm_device *dev = crtc->dev;
  4527. struct drm_i915_private *dev_priv = dev->dev_private;
  4528. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4529. struct intel_encoder *encoder;
  4530. int pipe = intel_crtc->pipe;
  4531. if (!intel_crtc->active)
  4532. return;
  4533. /*
  4534. * Gen2 reports pipe underruns whenever all planes are disabled.
  4535. * So diasble underrun reporting before all the planes get disabled.
  4536. * FIXME: Need to fix the logic to work when we turn off all planes
  4537. * but leave the pipe running.
  4538. */
  4539. if (IS_GEN2(dev))
  4540. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4541. /*
  4542. * Vblank time updates from the shadow to live plane control register
  4543. * are blocked if the memory self-refresh mode is active at that
  4544. * moment. So to make sure the plane gets truly disabled, disable
  4545. * first the self-refresh mode. The self-refresh enable bit in turn
  4546. * will be checked/applied by the HW only at the next frame start
  4547. * event which is after the vblank start event, so we need to have a
  4548. * wait-for-vblank between disabling the plane and the pipe.
  4549. */
  4550. intel_set_memory_cxsr(dev_priv, false);
  4551. intel_crtc_disable_planes(crtc);
  4552. /*
  4553. * On gen2 planes are double buffered but the pipe isn't, so we must
  4554. * wait for planes to fully turn off before disabling the pipe.
  4555. * We also need to wait on all gmch platforms because of the
  4556. * self-refresh mode constraint explained above.
  4557. */
  4558. intel_wait_for_vblank(dev, pipe);
  4559. for_each_encoder_on_crtc(dev, crtc, encoder)
  4560. encoder->disable(encoder);
  4561. drm_crtc_vblank_off(crtc);
  4562. assert_vblank_disabled(crtc);
  4563. intel_disable_pipe(intel_crtc);
  4564. i9xx_pfit_disable(intel_crtc);
  4565. for_each_encoder_on_crtc(dev, crtc, encoder)
  4566. if (encoder->post_disable)
  4567. encoder->post_disable(encoder);
  4568. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4569. if (IS_CHERRYVIEW(dev))
  4570. chv_disable_pll(dev_priv, pipe);
  4571. else if (IS_VALLEYVIEW(dev))
  4572. vlv_disable_pll(dev_priv, pipe);
  4573. else
  4574. i9xx_disable_pll(intel_crtc);
  4575. }
  4576. if (!IS_GEN2(dev))
  4577. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4578. intel_crtc->active = false;
  4579. intel_update_watermarks(crtc);
  4580. mutex_lock(&dev->struct_mutex);
  4581. intel_fbc_update(dev);
  4582. mutex_unlock(&dev->struct_mutex);
  4583. }
  4584. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4585. {
  4586. }
  4587. /* Master function to enable/disable CRTC and corresponding power wells */
  4588. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4589. {
  4590. struct drm_device *dev = crtc->dev;
  4591. struct drm_i915_private *dev_priv = dev->dev_private;
  4592. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4593. enum intel_display_power_domain domain;
  4594. unsigned long domains;
  4595. if (enable) {
  4596. if (!intel_crtc->active) {
  4597. domains = get_crtc_power_domains(crtc);
  4598. for_each_power_domain(domain, domains)
  4599. intel_display_power_get(dev_priv, domain);
  4600. intel_crtc->enabled_power_domains = domains;
  4601. dev_priv->display.crtc_enable(crtc);
  4602. }
  4603. } else {
  4604. if (intel_crtc->active) {
  4605. dev_priv->display.crtc_disable(crtc);
  4606. domains = intel_crtc->enabled_power_domains;
  4607. for_each_power_domain(domain, domains)
  4608. intel_display_power_put(dev_priv, domain);
  4609. intel_crtc->enabled_power_domains = 0;
  4610. }
  4611. }
  4612. }
  4613. /**
  4614. * Sets the power management mode of the pipe and plane.
  4615. */
  4616. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4617. {
  4618. struct drm_device *dev = crtc->dev;
  4619. struct intel_encoder *intel_encoder;
  4620. bool enable = false;
  4621. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4622. enable |= intel_encoder->connectors_active;
  4623. intel_crtc_control(crtc, enable);
  4624. }
  4625. static void intel_crtc_disable(struct drm_crtc *crtc)
  4626. {
  4627. struct drm_device *dev = crtc->dev;
  4628. struct drm_connector *connector;
  4629. struct drm_i915_private *dev_priv = dev->dev_private;
  4630. /* crtc should still be enabled when we disable it. */
  4631. WARN_ON(!crtc->state->enable);
  4632. dev_priv->display.crtc_disable(crtc);
  4633. dev_priv->display.off(crtc);
  4634. crtc->primary->funcs->disable_plane(crtc->primary);
  4635. /* Update computed state. */
  4636. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4637. if (!connector->encoder || !connector->encoder->crtc)
  4638. continue;
  4639. if (connector->encoder->crtc != crtc)
  4640. continue;
  4641. connector->dpms = DRM_MODE_DPMS_OFF;
  4642. to_intel_encoder(connector->encoder)->connectors_active = false;
  4643. }
  4644. }
  4645. void intel_encoder_destroy(struct drm_encoder *encoder)
  4646. {
  4647. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4648. drm_encoder_cleanup(encoder);
  4649. kfree(intel_encoder);
  4650. }
  4651. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4652. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4653. * state of the entire output pipe. */
  4654. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4655. {
  4656. if (mode == DRM_MODE_DPMS_ON) {
  4657. encoder->connectors_active = true;
  4658. intel_crtc_update_dpms(encoder->base.crtc);
  4659. } else {
  4660. encoder->connectors_active = false;
  4661. intel_crtc_update_dpms(encoder->base.crtc);
  4662. }
  4663. }
  4664. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4665. * internal consistency). */
  4666. static void intel_connector_check_state(struct intel_connector *connector)
  4667. {
  4668. if (connector->get_hw_state(connector)) {
  4669. struct intel_encoder *encoder = connector->encoder;
  4670. struct drm_crtc *crtc;
  4671. bool encoder_enabled;
  4672. enum pipe pipe;
  4673. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4674. connector->base.base.id,
  4675. connector->base.name);
  4676. /* there is no real hw state for MST connectors */
  4677. if (connector->mst_port)
  4678. return;
  4679. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4680. "wrong connector dpms state\n");
  4681. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4682. "active connector not linked to encoder\n");
  4683. if (encoder) {
  4684. I915_STATE_WARN(!encoder->connectors_active,
  4685. "encoder->connectors_active not set\n");
  4686. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4687. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4688. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4689. return;
  4690. crtc = encoder->base.crtc;
  4691. I915_STATE_WARN(!crtc->state->enable,
  4692. "crtc not enabled\n");
  4693. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4694. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4695. "encoder active on the wrong pipe\n");
  4696. }
  4697. }
  4698. }
  4699. /* Even simpler default implementation, if there's really no special case to
  4700. * consider. */
  4701. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4702. {
  4703. /* All the simple cases only support two dpms states. */
  4704. if (mode != DRM_MODE_DPMS_ON)
  4705. mode = DRM_MODE_DPMS_OFF;
  4706. if (mode == connector->dpms)
  4707. return;
  4708. connector->dpms = mode;
  4709. /* Only need to change hw state when actually enabled */
  4710. if (connector->encoder)
  4711. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4712. intel_modeset_check_state(connector->dev);
  4713. }
  4714. /* Simple connector->get_hw_state implementation for encoders that support only
  4715. * one connector and no cloning and hence the encoder state determines the state
  4716. * of the connector. */
  4717. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4718. {
  4719. enum pipe pipe = 0;
  4720. struct intel_encoder *encoder = connector->encoder;
  4721. return encoder->get_hw_state(encoder, &pipe);
  4722. }
  4723. static int pipe_required_fdi_lanes(struct drm_device *dev, enum pipe pipe)
  4724. {
  4725. struct intel_crtc *crtc =
  4726. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  4727. if (crtc->base.state->enable &&
  4728. crtc->config->has_pch_encoder)
  4729. return crtc->config->fdi_lanes;
  4730. return 0;
  4731. }
  4732. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4733. struct intel_crtc_state *pipe_config)
  4734. {
  4735. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4736. pipe_name(pipe), pipe_config->fdi_lanes);
  4737. if (pipe_config->fdi_lanes > 4) {
  4738. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4739. pipe_name(pipe), pipe_config->fdi_lanes);
  4740. return false;
  4741. }
  4742. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4743. if (pipe_config->fdi_lanes > 2) {
  4744. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4745. pipe_config->fdi_lanes);
  4746. return false;
  4747. } else {
  4748. return true;
  4749. }
  4750. }
  4751. if (INTEL_INFO(dev)->num_pipes == 2)
  4752. return true;
  4753. /* Ivybridge 3 pipe is really complicated */
  4754. switch (pipe) {
  4755. case PIPE_A:
  4756. return true;
  4757. case PIPE_B:
  4758. if (pipe_config->fdi_lanes > 2 &&
  4759. pipe_required_fdi_lanes(dev, PIPE_C) > 0) {
  4760. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4761. pipe_name(pipe), pipe_config->fdi_lanes);
  4762. return false;
  4763. }
  4764. return true;
  4765. case PIPE_C:
  4766. if (pipe_config->fdi_lanes > 2) {
  4767. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  4768. pipe_name(pipe), pipe_config->fdi_lanes);
  4769. return false;
  4770. }
  4771. if (pipe_required_fdi_lanes(dev, PIPE_B) > 2) {
  4772. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4773. return false;
  4774. }
  4775. return true;
  4776. default:
  4777. BUG();
  4778. }
  4779. }
  4780. #define RETRY 1
  4781. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4782. struct intel_crtc_state *pipe_config)
  4783. {
  4784. struct drm_device *dev = intel_crtc->base.dev;
  4785. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4786. int lane, link_bw, fdi_dotclock;
  4787. bool setup_ok, needs_recompute = false;
  4788. retry:
  4789. /* FDI is a binary signal running at ~2.7GHz, encoding
  4790. * each output octet as 10 bits. The actual frequency
  4791. * is stored as a divider into a 100MHz clock, and the
  4792. * mode pixel clock is stored in units of 1KHz.
  4793. * Hence the bw of each lane in terms of the mode signal
  4794. * is:
  4795. */
  4796. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4797. fdi_dotclock = adjusted_mode->crtc_clock;
  4798. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4799. pipe_config->pipe_bpp);
  4800. pipe_config->fdi_lanes = lane;
  4801. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4802. link_bw, &pipe_config->fdi_m_n);
  4803. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4804. intel_crtc->pipe, pipe_config);
  4805. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4806. pipe_config->pipe_bpp -= 2*3;
  4807. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4808. pipe_config->pipe_bpp);
  4809. needs_recompute = true;
  4810. pipe_config->bw_constrained = true;
  4811. goto retry;
  4812. }
  4813. if (needs_recompute)
  4814. return RETRY;
  4815. return setup_ok ? 0 : -EINVAL;
  4816. }
  4817. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4818. struct intel_crtc_state *pipe_config)
  4819. {
  4820. pipe_config->ips_enabled = i915.enable_ips &&
  4821. hsw_crtc_supports_ips(crtc) &&
  4822. pipe_config->pipe_bpp <= 24;
  4823. }
  4824. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4825. struct intel_crtc_state *pipe_config)
  4826. {
  4827. struct drm_device *dev = crtc->base.dev;
  4828. struct drm_i915_private *dev_priv = dev->dev_private;
  4829. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4830. /* FIXME should check pixel clock limits on all platforms */
  4831. if (INTEL_INFO(dev)->gen < 4) {
  4832. int clock_limit =
  4833. dev_priv->display.get_display_clock_speed(dev);
  4834. /*
  4835. * Enable pixel doubling when the dot clock
  4836. * is > 90% of the (display) core speed.
  4837. *
  4838. * GDG double wide on either pipe,
  4839. * otherwise pipe A only.
  4840. */
  4841. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4842. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4843. clock_limit *= 2;
  4844. pipe_config->double_wide = true;
  4845. }
  4846. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4847. return -EINVAL;
  4848. }
  4849. /*
  4850. * Pipe horizontal size must be even in:
  4851. * - DVO ganged mode
  4852. * - LVDS dual channel mode
  4853. * - Double wide pipe
  4854. */
  4855. if ((intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4856. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4857. pipe_config->pipe_src_w &= ~1;
  4858. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4859. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4860. */
  4861. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4862. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4863. return -EINVAL;
  4864. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4865. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4866. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4867. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4868. * for lvds. */
  4869. pipe_config->pipe_bpp = 8*3;
  4870. }
  4871. if (HAS_IPS(dev))
  4872. hsw_compute_ips_config(crtc, pipe_config);
  4873. if (pipe_config->has_pch_encoder)
  4874. return ironlake_fdi_compute_config(crtc, pipe_config);
  4875. return 0;
  4876. }
  4877. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4878. {
  4879. struct drm_i915_private *dev_priv = dev->dev_private;
  4880. u32 val;
  4881. int divider;
  4882. if (dev_priv->hpll_freq == 0)
  4883. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4884. mutex_lock(&dev_priv->dpio_lock);
  4885. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4886. mutex_unlock(&dev_priv->dpio_lock);
  4887. divider = val & DISPLAY_FREQUENCY_VALUES;
  4888. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4889. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4890. "cdclk change in progress\n");
  4891. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4892. }
  4893. static int i945_get_display_clock_speed(struct drm_device *dev)
  4894. {
  4895. return 400000;
  4896. }
  4897. static int i915_get_display_clock_speed(struct drm_device *dev)
  4898. {
  4899. return 333000;
  4900. }
  4901. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4902. {
  4903. return 200000;
  4904. }
  4905. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4906. {
  4907. u16 gcfgc = 0;
  4908. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4909. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4910. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4911. return 267000;
  4912. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4913. return 333000;
  4914. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4915. return 444000;
  4916. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4917. return 200000;
  4918. default:
  4919. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4920. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4921. return 133000;
  4922. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4923. return 167000;
  4924. }
  4925. }
  4926. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4927. {
  4928. u16 gcfgc = 0;
  4929. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4930. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4931. return 133000;
  4932. else {
  4933. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4934. case GC_DISPLAY_CLOCK_333_MHZ:
  4935. return 333000;
  4936. default:
  4937. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4938. return 190000;
  4939. }
  4940. }
  4941. }
  4942. static int i865_get_display_clock_speed(struct drm_device *dev)
  4943. {
  4944. return 266000;
  4945. }
  4946. static int i855_get_display_clock_speed(struct drm_device *dev)
  4947. {
  4948. u16 hpllcc = 0;
  4949. /* Assume that the hardware is in the high speed state. This
  4950. * should be the default.
  4951. */
  4952. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4953. case GC_CLOCK_133_200:
  4954. case GC_CLOCK_100_200:
  4955. return 200000;
  4956. case GC_CLOCK_166_250:
  4957. return 250000;
  4958. case GC_CLOCK_100_133:
  4959. return 133000;
  4960. }
  4961. /* Shouldn't happen */
  4962. return 0;
  4963. }
  4964. static int i830_get_display_clock_speed(struct drm_device *dev)
  4965. {
  4966. return 133000;
  4967. }
  4968. static void
  4969. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4970. {
  4971. while (*num > DATA_LINK_M_N_MASK ||
  4972. *den > DATA_LINK_M_N_MASK) {
  4973. *num >>= 1;
  4974. *den >>= 1;
  4975. }
  4976. }
  4977. static void compute_m_n(unsigned int m, unsigned int n,
  4978. uint32_t *ret_m, uint32_t *ret_n)
  4979. {
  4980. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4981. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4982. intel_reduce_m_n_ratio(ret_m, ret_n);
  4983. }
  4984. void
  4985. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4986. int pixel_clock, int link_clock,
  4987. struct intel_link_m_n *m_n)
  4988. {
  4989. m_n->tu = 64;
  4990. compute_m_n(bits_per_pixel * pixel_clock,
  4991. link_clock * nlanes * 8,
  4992. &m_n->gmch_m, &m_n->gmch_n);
  4993. compute_m_n(pixel_clock, link_clock,
  4994. &m_n->link_m, &m_n->link_n);
  4995. }
  4996. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4997. {
  4998. if (i915.panel_use_ssc >= 0)
  4999. return i915.panel_use_ssc != 0;
  5000. return dev_priv->vbt.lvds_use_ssc
  5001. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5002. }
  5003. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  5004. {
  5005. struct drm_device *dev = crtc->base.dev;
  5006. struct drm_i915_private *dev_priv = dev->dev_private;
  5007. int refclk;
  5008. if (IS_VALLEYVIEW(dev)) {
  5009. refclk = 100000;
  5010. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5011. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5012. refclk = dev_priv->vbt.lvds_ssc_freq;
  5013. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5014. } else if (!IS_GEN2(dev)) {
  5015. refclk = 96000;
  5016. } else {
  5017. refclk = 48000;
  5018. }
  5019. return refclk;
  5020. }
  5021. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5022. {
  5023. return (1 << dpll->n) << 16 | dpll->m2;
  5024. }
  5025. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5026. {
  5027. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5028. }
  5029. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5030. struct intel_crtc_state *crtc_state,
  5031. intel_clock_t *reduced_clock)
  5032. {
  5033. struct drm_device *dev = crtc->base.dev;
  5034. u32 fp, fp2 = 0;
  5035. if (IS_PINEVIEW(dev)) {
  5036. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5037. if (reduced_clock)
  5038. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5039. } else {
  5040. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5041. if (reduced_clock)
  5042. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5043. }
  5044. crtc_state->dpll_hw_state.fp0 = fp;
  5045. crtc->lowfreq_avail = false;
  5046. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5047. reduced_clock && i915.powersave) {
  5048. crtc_state->dpll_hw_state.fp1 = fp2;
  5049. crtc->lowfreq_avail = true;
  5050. } else {
  5051. crtc_state->dpll_hw_state.fp1 = fp;
  5052. }
  5053. }
  5054. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5055. pipe)
  5056. {
  5057. u32 reg_val;
  5058. /*
  5059. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5060. * and set it to a reasonable value instead.
  5061. */
  5062. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5063. reg_val &= 0xffffff00;
  5064. reg_val |= 0x00000030;
  5065. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5066. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5067. reg_val &= 0x8cffffff;
  5068. reg_val = 0x8c000000;
  5069. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5070. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5071. reg_val &= 0xffffff00;
  5072. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5073. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5074. reg_val &= 0x00ffffff;
  5075. reg_val |= 0xb0000000;
  5076. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5077. }
  5078. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5079. struct intel_link_m_n *m_n)
  5080. {
  5081. struct drm_device *dev = crtc->base.dev;
  5082. struct drm_i915_private *dev_priv = dev->dev_private;
  5083. int pipe = crtc->pipe;
  5084. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5085. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5086. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5087. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5088. }
  5089. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5090. struct intel_link_m_n *m_n,
  5091. struct intel_link_m_n *m2_n2)
  5092. {
  5093. struct drm_device *dev = crtc->base.dev;
  5094. struct drm_i915_private *dev_priv = dev->dev_private;
  5095. int pipe = crtc->pipe;
  5096. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5097. if (INTEL_INFO(dev)->gen >= 5) {
  5098. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5099. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5100. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5101. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5102. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5103. * for gen < 8) and if DRRS is supported (to make sure the
  5104. * registers are not unnecessarily accessed).
  5105. */
  5106. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  5107. crtc->config->has_drrs) {
  5108. I915_WRITE(PIPE_DATA_M2(transcoder),
  5109. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5110. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5111. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5112. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5113. }
  5114. } else {
  5115. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5116. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5117. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5118. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5119. }
  5120. }
  5121. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5122. {
  5123. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5124. if (m_n == M1_N1) {
  5125. dp_m_n = &crtc->config->dp_m_n;
  5126. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5127. } else if (m_n == M2_N2) {
  5128. /*
  5129. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5130. * needs to be programmed into M1_N1.
  5131. */
  5132. dp_m_n = &crtc->config->dp_m2_n2;
  5133. } else {
  5134. DRM_ERROR("Unsupported divider value\n");
  5135. return;
  5136. }
  5137. if (crtc->config->has_pch_encoder)
  5138. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5139. else
  5140. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5141. }
  5142. static void vlv_update_pll(struct intel_crtc *crtc,
  5143. struct intel_crtc_state *pipe_config)
  5144. {
  5145. u32 dpll, dpll_md;
  5146. /*
  5147. * Enable DPIO clock input. We should never disable the reference
  5148. * clock for pipe B, since VGA hotplug / manual detection depends
  5149. * on it.
  5150. */
  5151. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  5152. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  5153. /* We should never disable this, set it here for state tracking */
  5154. if (crtc->pipe == PIPE_B)
  5155. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5156. dpll |= DPLL_VCO_ENABLE;
  5157. pipe_config->dpll_hw_state.dpll = dpll;
  5158. dpll_md = (pipe_config->pixel_multiplier - 1)
  5159. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5160. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  5161. }
  5162. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5163. const struct intel_crtc_state *pipe_config)
  5164. {
  5165. struct drm_device *dev = crtc->base.dev;
  5166. struct drm_i915_private *dev_priv = dev->dev_private;
  5167. int pipe = crtc->pipe;
  5168. u32 mdiv;
  5169. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5170. u32 coreclk, reg_val;
  5171. mutex_lock(&dev_priv->dpio_lock);
  5172. bestn = pipe_config->dpll.n;
  5173. bestm1 = pipe_config->dpll.m1;
  5174. bestm2 = pipe_config->dpll.m2;
  5175. bestp1 = pipe_config->dpll.p1;
  5176. bestp2 = pipe_config->dpll.p2;
  5177. /* See eDP HDMI DPIO driver vbios notes doc */
  5178. /* PLL B needs special handling */
  5179. if (pipe == PIPE_B)
  5180. vlv_pllb_recal_opamp(dev_priv, pipe);
  5181. /* Set up Tx target for periodic Rcomp update */
  5182. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5183. /* Disable target IRef on PLL */
  5184. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5185. reg_val &= 0x00ffffff;
  5186. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5187. /* Disable fast lock */
  5188. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5189. /* Set idtafcrecal before PLL is enabled */
  5190. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5191. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5192. mdiv |= ((bestn << DPIO_N_SHIFT));
  5193. mdiv |= (1 << DPIO_K_SHIFT);
  5194. /*
  5195. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5196. * but we don't support that).
  5197. * Note: don't use the DAC post divider as it seems unstable.
  5198. */
  5199. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5200. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5201. mdiv |= DPIO_ENABLE_CALIBRATION;
  5202. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5203. /* Set HBR and RBR LPF coefficients */
  5204. if (pipe_config->port_clock == 162000 ||
  5205. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  5206. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  5207. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5208. 0x009f0003);
  5209. else
  5210. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5211. 0x00d0000f);
  5212. if (pipe_config->has_dp_encoder) {
  5213. /* Use SSC source */
  5214. if (pipe == PIPE_A)
  5215. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5216. 0x0df40000);
  5217. else
  5218. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5219. 0x0df70000);
  5220. } else { /* HDMI or VGA */
  5221. /* Use bend source */
  5222. if (pipe == PIPE_A)
  5223. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5224. 0x0df70000);
  5225. else
  5226. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5227. 0x0df40000);
  5228. }
  5229. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5230. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5231. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5232. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5233. coreclk |= 0x01000000;
  5234. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5235. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5236. mutex_unlock(&dev_priv->dpio_lock);
  5237. }
  5238. static void chv_update_pll(struct intel_crtc *crtc,
  5239. struct intel_crtc_state *pipe_config)
  5240. {
  5241. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5242. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5243. DPLL_VCO_ENABLE;
  5244. if (crtc->pipe != PIPE_A)
  5245. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5246. pipe_config->dpll_hw_state.dpll_md =
  5247. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5248. }
  5249. static void chv_prepare_pll(struct intel_crtc *crtc,
  5250. const struct intel_crtc_state *pipe_config)
  5251. {
  5252. struct drm_device *dev = crtc->base.dev;
  5253. struct drm_i915_private *dev_priv = dev->dev_private;
  5254. int pipe = crtc->pipe;
  5255. int dpll_reg = DPLL(crtc->pipe);
  5256. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5257. u32 loopfilter, tribuf_calcntr;
  5258. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5259. u32 dpio_val;
  5260. int vco;
  5261. bestn = pipe_config->dpll.n;
  5262. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5263. bestm1 = pipe_config->dpll.m1;
  5264. bestm2 = pipe_config->dpll.m2 >> 22;
  5265. bestp1 = pipe_config->dpll.p1;
  5266. bestp2 = pipe_config->dpll.p2;
  5267. vco = pipe_config->dpll.vco;
  5268. dpio_val = 0;
  5269. loopfilter = 0;
  5270. /*
  5271. * Enable Refclk and SSC
  5272. */
  5273. I915_WRITE(dpll_reg,
  5274. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5275. mutex_lock(&dev_priv->dpio_lock);
  5276. /* p1 and p2 divider */
  5277. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5278. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5279. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5280. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5281. 1 << DPIO_CHV_K_DIV_SHIFT);
  5282. /* Feedback post-divider - m2 */
  5283. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5284. /* Feedback refclk divider - n and m1 */
  5285. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5286. DPIO_CHV_M1_DIV_BY_2 |
  5287. 1 << DPIO_CHV_N_DIV_SHIFT);
  5288. /* M2 fraction division */
  5289. if (bestm2_frac)
  5290. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5291. /* M2 fraction division enable */
  5292. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5293. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5294. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5295. if (bestm2_frac)
  5296. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5297. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5298. /* Program digital lock detect threshold */
  5299. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5300. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5301. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5302. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5303. if (!bestm2_frac)
  5304. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5305. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5306. /* Loop filter */
  5307. if (vco == 5400000) {
  5308. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5309. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5310. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5311. tribuf_calcntr = 0x9;
  5312. } else if (vco <= 6200000) {
  5313. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5314. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5315. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5316. tribuf_calcntr = 0x9;
  5317. } else if (vco <= 6480000) {
  5318. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5319. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5320. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5321. tribuf_calcntr = 0x8;
  5322. } else {
  5323. /* Not supported. Apply the same limits as in the max case */
  5324. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5325. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5326. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5327. tribuf_calcntr = 0;
  5328. }
  5329. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5330. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5331. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5332. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5333. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5334. /* AFC Recal */
  5335. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5336. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5337. DPIO_AFC_RECAL);
  5338. mutex_unlock(&dev_priv->dpio_lock);
  5339. }
  5340. /**
  5341. * vlv_force_pll_on - forcibly enable just the PLL
  5342. * @dev_priv: i915 private structure
  5343. * @pipe: pipe PLL to enable
  5344. * @dpll: PLL configuration
  5345. *
  5346. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5347. * in cases where we need the PLL enabled even when @pipe is not going to
  5348. * be enabled.
  5349. */
  5350. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5351. const struct dpll *dpll)
  5352. {
  5353. struct intel_crtc *crtc =
  5354. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5355. struct intel_crtc_state pipe_config = {
  5356. .pixel_multiplier = 1,
  5357. .dpll = *dpll,
  5358. };
  5359. if (IS_CHERRYVIEW(dev)) {
  5360. chv_update_pll(crtc, &pipe_config);
  5361. chv_prepare_pll(crtc, &pipe_config);
  5362. chv_enable_pll(crtc, &pipe_config);
  5363. } else {
  5364. vlv_update_pll(crtc, &pipe_config);
  5365. vlv_prepare_pll(crtc, &pipe_config);
  5366. vlv_enable_pll(crtc, &pipe_config);
  5367. }
  5368. }
  5369. /**
  5370. * vlv_force_pll_off - forcibly disable just the PLL
  5371. * @dev_priv: i915 private structure
  5372. * @pipe: pipe PLL to disable
  5373. *
  5374. * Disable the PLL for @pipe. To be used in cases where we need
  5375. * the PLL enabled even when @pipe is not going to be enabled.
  5376. */
  5377. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5378. {
  5379. if (IS_CHERRYVIEW(dev))
  5380. chv_disable_pll(to_i915(dev), pipe);
  5381. else
  5382. vlv_disable_pll(to_i915(dev), pipe);
  5383. }
  5384. static void i9xx_update_pll(struct intel_crtc *crtc,
  5385. struct intel_crtc_state *crtc_state,
  5386. intel_clock_t *reduced_clock,
  5387. int num_connectors)
  5388. {
  5389. struct drm_device *dev = crtc->base.dev;
  5390. struct drm_i915_private *dev_priv = dev->dev_private;
  5391. u32 dpll;
  5392. bool is_sdvo;
  5393. struct dpll *clock = &crtc_state->dpll;
  5394. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5395. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5396. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5397. dpll = DPLL_VGA_MODE_DIS;
  5398. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5399. dpll |= DPLLB_MODE_LVDS;
  5400. else
  5401. dpll |= DPLLB_MODE_DAC_SERIAL;
  5402. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5403. dpll |= (crtc_state->pixel_multiplier - 1)
  5404. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5405. }
  5406. if (is_sdvo)
  5407. dpll |= DPLL_SDVO_HIGH_SPEED;
  5408. if (crtc_state->has_dp_encoder)
  5409. dpll |= DPLL_SDVO_HIGH_SPEED;
  5410. /* compute bitmask from p1 value */
  5411. if (IS_PINEVIEW(dev))
  5412. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5413. else {
  5414. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5415. if (IS_G4X(dev) && reduced_clock)
  5416. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5417. }
  5418. switch (clock->p2) {
  5419. case 5:
  5420. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5421. break;
  5422. case 7:
  5423. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5424. break;
  5425. case 10:
  5426. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5427. break;
  5428. case 14:
  5429. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5430. break;
  5431. }
  5432. if (INTEL_INFO(dev)->gen >= 4)
  5433. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5434. if (crtc_state->sdvo_tv_clock)
  5435. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5436. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5437. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5438. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5439. else
  5440. dpll |= PLL_REF_INPUT_DREFCLK;
  5441. dpll |= DPLL_VCO_ENABLE;
  5442. crtc_state->dpll_hw_state.dpll = dpll;
  5443. if (INTEL_INFO(dev)->gen >= 4) {
  5444. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5445. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5446. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5447. }
  5448. }
  5449. static void i8xx_update_pll(struct intel_crtc *crtc,
  5450. struct intel_crtc_state *crtc_state,
  5451. intel_clock_t *reduced_clock,
  5452. int num_connectors)
  5453. {
  5454. struct drm_device *dev = crtc->base.dev;
  5455. struct drm_i915_private *dev_priv = dev->dev_private;
  5456. u32 dpll;
  5457. struct dpll *clock = &crtc_state->dpll;
  5458. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5459. dpll = DPLL_VGA_MODE_DIS;
  5460. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5461. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5462. } else {
  5463. if (clock->p1 == 2)
  5464. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5465. else
  5466. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5467. if (clock->p2 == 4)
  5468. dpll |= PLL_P2_DIVIDE_BY_4;
  5469. }
  5470. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5471. dpll |= DPLL_DVO_2X_MODE;
  5472. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5473. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5474. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5475. else
  5476. dpll |= PLL_REF_INPUT_DREFCLK;
  5477. dpll |= DPLL_VCO_ENABLE;
  5478. crtc_state->dpll_hw_state.dpll = dpll;
  5479. }
  5480. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5481. {
  5482. struct drm_device *dev = intel_crtc->base.dev;
  5483. struct drm_i915_private *dev_priv = dev->dev_private;
  5484. enum pipe pipe = intel_crtc->pipe;
  5485. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5486. struct drm_display_mode *adjusted_mode =
  5487. &intel_crtc->config->base.adjusted_mode;
  5488. uint32_t crtc_vtotal, crtc_vblank_end;
  5489. int vsyncshift = 0;
  5490. /* We need to be careful not to changed the adjusted mode, for otherwise
  5491. * the hw state checker will get angry at the mismatch. */
  5492. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5493. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5494. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5495. /* the chip adds 2 halflines automatically */
  5496. crtc_vtotal -= 1;
  5497. crtc_vblank_end -= 1;
  5498. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5499. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5500. else
  5501. vsyncshift = adjusted_mode->crtc_hsync_start -
  5502. adjusted_mode->crtc_htotal / 2;
  5503. if (vsyncshift < 0)
  5504. vsyncshift += adjusted_mode->crtc_htotal;
  5505. }
  5506. if (INTEL_INFO(dev)->gen > 3)
  5507. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5508. I915_WRITE(HTOTAL(cpu_transcoder),
  5509. (adjusted_mode->crtc_hdisplay - 1) |
  5510. ((adjusted_mode->crtc_htotal - 1) << 16));
  5511. I915_WRITE(HBLANK(cpu_transcoder),
  5512. (adjusted_mode->crtc_hblank_start - 1) |
  5513. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5514. I915_WRITE(HSYNC(cpu_transcoder),
  5515. (adjusted_mode->crtc_hsync_start - 1) |
  5516. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5517. I915_WRITE(VTOTAL(cpu_transcoder),
  5518. (adjusted_mode->crtc_vdisplay - 1) |
  5519. ((crtc_vtotal - 1) << 16));
  5520. I915_WRITE(VBLANK(cpu_transcoder),
  5521. (adjusted_mode->crtc_vblank_start - 1) |
  5522. ((crtc_vblank_end - 1) << 16));
  5523. I915_WRITE(VSYNC(cpu_transcoder),
  5524. (adjusted_mode->crtc_vsync_start - 1) |
  5525. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5526. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5527. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5528. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5529. * bits. */
  5530. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5531. (pipe == PIPE_B || pipe == PIPE_C))
  5532. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5533. /* pipesrc controls the size that is scaled from, which should
  5534. * always be the user's requested size.
  5535. */
  5536. I915_WRITE(PIPESRC(pipe),
  5537. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5538. (intel_crtc->config->pipe_src_h - 1));
  5539. }
  5540. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5541. struct intel_crtc_state *pipe_config)
  5542. {
  5543. struct drm_device *dev = crtc->base.dev;
  5544. struct drm_i915_private *dev_priv = dev->dev_private;
  5545. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5546. uint32_t tmp;
  5547. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5548. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5549. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5550. tmp = I915_READ(HBLANK(cpu_transcoder));
  5551. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5552. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5553. tmp = I915_READ(HSYNC(cpu_transcoder));
  5554. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5555. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5556. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5557. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5558. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5559. tmp = I915_READ(VBLANK(cpu_transcoder));
  5560. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5561. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5562. tmp = I915_READ(VSYNC(cpu_transcoder));
  5563. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5564. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5565. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5566. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5567. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5568. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5569. }
  5570. tmp = I915_READ(PIPESRC(crtc->pipe));
  5571. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5572. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5573. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5574. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5575. }
  5576. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5577. struct intel_crtc_state *pipe_config)
  5578. {
  5579. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5580. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5581. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5582. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5583. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5584. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5585. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5586. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5587. mode->flags = pipe_config->base.adjusted_mode.flags;
  5588. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5589. mode->flags |= pipe_config->base.adjusted_mode.flags;
  5590. }
  5591. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5592. {
  5593. struct drm_device *dev = intel_crtc->base.dev;
  5594. struct drm_i915_private *dev_priv = dev->dev_private;
  5595. uint32_t pipeconf;
  5596. pipeconf = 0;
  5597. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5598. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5599. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5600. if (intel_crtc->config->double_wide)
  5601. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5602. /* only g4x and later have fancy bpc/dither controls */
  5603. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5604. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5605. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5606. pipeconf |= PIPECONF_DITHER_EN |
  5607. PIPECONF_DITHER_TYPE_SP;
  5608. switch (intel_crtc->config->pipe_bpp) {
  5609. case 18:
  5610. pipeconf |= PIPECONF_6BPC;
  5611. break;
  5612. case 24:
  5613. pipeconf |= PIPECONF_8BPC;
  5614. break;
  5615. case 30:
  5616. pipeconf |= PIPECONF_10BPC;
  5617. break;
  5618. default:
  5619. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5620. BUG();
  5621. }
  5622. }
  5623. if (HAS_PIPE_CXSR(dev)) {
  5624. if (intel_crtc->lowfreq_avail) {
  5625. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5626. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5627. } else {
  5628. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5629. }
  5630. }
  5631. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5632. if (INTEL_INFO(dev)->gen < 4 ||
  5633. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5634. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5635. else
  5636. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5637. } else
  5638. pipeconf |= PIPECONF_PROGRESSIVE;
  5639. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  5640. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5641. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5642. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5643. }
  5644. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  5645. struct intel_crtc_state *crtc_state)
  5646. {
  5647. struct drm_device *dev = crtc->base.dev;
  5648. struct drm_i915_private *dev_priv = dev->dev_private;
  5649. int refclk, num_connectors = 0;
  5650. intel_clock_t clock, reduced_clock;
  5651. bool ok, has_reduced_clock = false;
  5652. bool is_lvds = false, is_dsi = false;
  5653. struct intel_encoder *encoder;
  5654. const intel_limit_t *limit;
  5655. for_each_intel_encoder(dev, encoder) {
  5656. if (encoder->new_crtc != crtc)
  5657. continue;
  5658. switch (encoder->type) {
  5659. case INTEL_OUTPUT_LVDS:
  5660. is_lvds = true;
  5661. break;
  5662. case INTEL_OUTPUT_DSI:
  5663. is_dsi = true;
  5664. break;
  5665. default:
  5666. break;
  5667. }
  5668. num_connectors++;
  5669. }
  5670. if (is_dsi)
  5671. return 0;
  5672. if (!crtc_state->clock_set) {
  5673. refclk = i9xx_get_refclk(crtc, num_connectors);
  5674. /*
  5675. * Returns a set of divisors for the desired target clock with
  5676. * the given refclk, or FALSE. The returned values represent
  5677. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5678. * 2) / p1 / p2.
  5679. */
  5680. limit = intel_limit(crtc, refclk);
  5681. ok = dev_priv->display.find_dpll(limit, crtc,
  5682. crtc_state->port_clock,
  5683. refclk, NULL, &clock);
  5684. if (!ok) {
  5685. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5686. return -EINVAL;
  5687. }
  5688. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5689. /*
  5690. * Ensure we match the reduced clock's P to the target
  5691. * clock. If the clocks don't match, we can't switch
  5692. * the display clock by using the FP0/FP1. In such case
  5693. * we will disable the LVDS downclock feature.
  5694. */
  5695. has_reduced_clock =
  5696. dev_priv->display.find_dpll(limit, crtc,
  5697. dev_priv->lvds_downclock,
  5698. refclk, &clock,
  5699. &reduced_clock);
  5700. }
  5701. /* Compat-code for transition, will disappear. */
  5702. crtc_state->dpll.n = clock.n;
  5703. crtc_state->dpll.m1 = clock.m1;
  5704. crtc_state->dpll.m2 = clock.m2;
  5705. crtc_state->dpll.p1 = clock.p1;
  5706. crtc_state->dpll.p2 = clock.p2;
  5707. }
  5708. if (IS_GEN2(dev)) {
  5709. i8xx_update_pll(crtc, crtc_state,
  5710. has_reduced_clock ? &reduced_clock : NULL,
  5711. num_connectors);
  5712. } else if (IS_CHERRYVIEW(dev)) {
  5713. chv_update_pll(crtc, crtc_state);
  5714. } else if (IS_VALLEYVIEW(dev)) {
  5715. vlv_update_pll(crtc, crtc_state);
  5716. } else {
  5717. i9xx_update_pll(crtc, crtc_state,
  5718. has_reduced_clock ? &reduced_clock : NULL,
  5719. num_connectors);
  5720. }
  5721. return 0;
  5722. }
  5723. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5724. struct intel_crtc_state *pipe_config)
  5725. {
  5726. struct drm_device *dev = crtc->base.dev;
  5727. struct drm_i915_private *dev_priv = dev->dev_private;
  5728. uint32_t tmp;
  5729. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5730. return;
  5731. tmp = I915_READ(PFIT_CONTROL);
  5732. if (!(tmp & PFIT_ENABLE))
  5733. return;
  5734. /* Check whether the pfit is attached to our pipe. */
  5735. if (INTEL_INFO(dev)->gen < 4) {
  5736. if (crtc->pipe != PIPE_B)
  5737. return;
  5738. } else {
  5739. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5740. return;
  5741. }
  5742. pipe_config->gmch_pfit.control = tmp;
  5743. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5744. if (INTEL_INFO(dev)->gen < 5)
  5745. pipe_config->gmch_pfit.lvds_border_bits =
  5746. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5747. }
  5748. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5749. struct intel_crtc_state *pipe_config)
  5750. {
  5751. struct drm_device *dev = crtc->base.dev;
  5752. struct drm_i915_private *dev_priv = dev->dev_private;
  5753. int pipe = pipe_config->cpu_transcoder;
  5754. intel_clock_t clock;
  5755. u32 mdiv;
  5756. int refclk = 100000;
  5757. /* In case of MIPI DPLL will not even be used */
  5758. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5759. return;
  5760. mutex_lock(&dev_priv->dpio_lock);
  5761. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5762. mutex_unlock(&dev_priv->dpio_lock);
  5763. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5764. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5765. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5766. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5767. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5768. vlv_clock(refclk, &clock);
  5769. /* clock.dot is the fast clock */
  5770. pipe_config->port_clock = clock.dot / 5;
  5771. }
  5772. static void
  5773. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  5774. struct intel_initial_plane_config *plane_config)
  5775. {
  5776. struct drm_device *dev = crtc->base.dev;
  5777. struct drm_i915_private *dev_priv = dev->dev_private;
  5778. u32 val, base, offset;
  5779. int pipe = crtc->pipe, plane = crtc->plane;
  5780. int fourcc, pixel_format;
  5781. unsigned int aligned_height;
  5782. struct drm_framebuffer *fb;
  5783. struct intel_framebuffer *intel_fb;
  5784. val = I915_READ(DSPCNTR(plane));
  5785. if (!(val & DISPLAY_PLANE_ENABLE))
  5786. return;
  5787. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5788. if (!intel_fb) {
  5789. DRM_DEBUG_KMS("failed to alloc fb\n");
  5790. return;
  5791. }
  5792. fb = &intel_fb->base;
  5793. if (INTEL_INFO(dev)->gen >= 4) {
  5794. if (val & DISPPLANE_TILED) {
  5795. plane_config->tiling = I915_TILING_X;
  5796. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  5797. }
  5798. }
  5799. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5800. fourcc = i9xx_format_to_fourcc(pixel_format);
  5801. fb->pixel_format = fourcc;
  5802. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  5803. if (INTEL_INFO(dev)->gen >= 4) {
  5804. if (plane_config->tiling)
  5805. offset = I915_READ(DSPTILEOFF(plane));
  5806. else
  5807. offset = I915_READ(DSPLINOFF(plane));
  5808. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5809. } else {
  5810. base = I915_READ(DSPADDR(plane));
  5811. }
  5812. plane_config->base = base;
  5813. val = I915_READ(PIPESRC(pipe));
  5814. fb->width = ((val >> 16) & 0xfff) + 1;
  5815. fb->height = ((val >> 0) & 0xfff) + 1;
  5816. val = I915_READ(DSPSTRIDE(pipe));
  5817. fb->pitches[0] = val & 0xffffffc0;
  5818. aligned_height = intel_fb_align_height(dev, fb->height,
  5819. fb->pixel_format,
  5820. fb->modifier[0]);
  5821. plane_config->size = fb->pitches[0] * aligned_height;
  5822. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5823. pipe_name(pipe), plane, fb->width, fb->height,
  5824. fb->bits_per_pixel, base, fb->pitches[0],
  5825. plane_config->size);
  5826. plane_config->fb = intel_fb;
  5827. }
  5828. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5829. struct intel_crtc_state *pipe_config)
  5830. {
  5831. struct drm_device *dev = crtc->base.dev;
  5832. struct drm_i915_private *dev_priv = dev->dev_private;
  5833. int pipe = pipe_config->cpu_transcoder;
  5834. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5835. intel_clock_t clock;
  5836. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5837. int refclk = 100000;
  5838. mutex_lock(&dev_priv->dpio_lock);
  5839. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5840. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5841. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5842. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5843. mutex_unlock(&dev_priv->dpio_lock);
  5844. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5845. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5846. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5847. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5848. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5849. chv_clock(refclk, &clock);
  5850. /* clock.dot is the fast clock */
  5851. pipe_config->port_clock = clock.dot / 5;
  5852. }
  5853. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5854. struct intel_crtc_state *pipe_config)
  5855. {
  5856. struct drm_device *dev = crtc->base.dev;
  5857. struct drm_i915_private *dev_priv = dev->dev_private;
  5858. uint32_t tmp;
  5859. if (!intel_display_power_is_enabled(dev_priv,
  5860. POWER_DOMAIN_PIPE(crtc->pipe)))
  5861. return false;
  5862. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5863. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5864. tmp = I915_READ(PIPECONF(crtc->pipe));
  5865. if (!(tmp & PIPECONF_ENABLE))
  5866. return false;
  5867. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5868. switch (tmp & PIPECONF_BPC_MASK) {
  5869. case PIPECONF_6BPC:
  5870. pipe_config->pipe_bpp = 18;
  5871. break;
  5872. case PIPECONF_8BPC:
  5873. pipe_config->pipe_bpp = 24;
  5874. break;
  5875. case PIPECONF_10BPC:
  5876. pipe_config->pipe_bpp = 30;
  5877. break;
  5878. default:
  5879. break;
  5880. }
  5881. }
  5882. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5883. pipe_config->limited_color_range = true;
  5884. if (INTEL_INFO(dev)->gen < 4)
  5885. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5886. intel_get_pipe_timings(crtc, pipe_config);
  5887. i9xx_get_pfit_config(crtc, pipe_config);
  5888. if (INTEL_INFO(dev)->gen >= 4) {
  5889. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5890. pipe_config->pixel_multiplier =
  5891. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5892. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5893. pipe_config->dpll_hw_state.dpll_md = tmp;
  5894. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5895. tmp = I915_READ(DPLL(crtc->pipe));
  5896. pipe_config->pixel_multiplier =
  5897. ((tmp & SDVO_MULTIPLIER_MASK)
  5898. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5899. } else {
  5900. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5901. * port and will be fixed up in the encoder->get_config
  5902. * function. */
  5903. pipe_config->pixel_multiplier = 1;
  5904. }
  5905. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5906. if (!IS_VALLEYVIEW(dev)) {
  5907. /*
  5908. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5909. * on 830. Filter it out here so that we don't
  5910. * report errors due to that.
  5911. */
  5912. if (IS_I830(dev))
  5913. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5914. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5915. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5916. } else {
  5917. /* Mask out read-only status bits. */
  5918. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5919. DPLL_PORTC_READY_MASK |
  5920. DPLL_PORTB_READY_MASK);
  5921. }
  5922. if (IS_CHERRYVIEW(dev))
  5923. chv_crtc_clock_get(crtc, pipe_config);
  5924. else if (IS_VALLEYVIEW(dev))
  5925. vlv_crtc_clock_get(crtc, pipe_config);
  5926. else
  5927. i9xx_crtc_clock_get(crtc, pipe_config);
  5928. return true;
  5929. }
  5930. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5931. {
  5932. struct drm_i915_private *dev_priv = dev->dev_private;
  5933. struct intel_encoder *encoder;
  5934. u32 val, final;
  5935. bool has_lvds = false;
  5936. bool has_cpu_edp = false;
  5937. bool has_panel = false;
  5938. bool has_ck505 = false;
  5939. bool can_ssc = false;
  5940. /* We need to take the global config into account */
  5941. for_each_intel_encoder(dev, encoder) {
  5942. switch (encoder->type) {
  5943. case INTEL_OUTPUT_LVDS:
  5944. has_panel = true;
  5945. has_lvds = true;
  5946. break;
  5947. case INTEL_OUTPUT_EDP:
  5948. has_panel = true;
  5949. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5950. has_cpu_edp = true;
  5951. break;
  5952. default:
  5953. break;
  5954. }
  5955. }
  5956. if (HAS_PCH_IBX(dev)) {
  5957. has_ck505 = dev_priv->vbt.display_clock_mode;
  5958. can_ssc = has_ck505;
  5959. } else {
  5960. has_ck505 = false;
  5961. can_ssc = true;
  5962. }
  5963. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5964. has_panel, has_lvds, has_ck505);
  5965. /* Ironlake: try to setup display ref clock before DPLL
  5966. * enabling. This is only under driver's control after
  5967. * PCH B stepping, previous chipset stepping should be
  5968. * ignoring this setting.
  5969. */
  5970. val = I915_READ(PCH_DREF_CONTROL);
  5971. /* As we must carefully and slowly disable/enable each source in turn,
  5972. * compute the final state we want first and check if we need to
  5973. * make any changes at all.
  5974. */
  5975. final = val;
  5976. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5977. if (has_ck505)
  5978. final |= DREF_NONSPREAD_CK505_ENABLE;
  5979. else
  5980. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5981. final &= ~DREF_SSC_SOURCE_MASK;
  5982. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5983. final &= ~DREF_SSC1_ENABLE;
  5984. if (has_panel) {
  5985. final |= DREF_SSC_SOURCE_ENABLE;
  5986. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5987. final |= DREF_SSC1_ENABLE;
  5988. if (has_cpu_edp) {
  5989. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5990. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5991. else
  5992. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5993. } else
  5994. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5995. } else {
  5996. final |= DREF_SSC_SOURCE_DISABLE;
  5997. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5998. }
  5999. if (final == val)
  6000. return;
  6001. /* Always enable nonspread source */
  6002. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6003. if (has_ck505)
  6004. val |= DREF_NONSPREAD_CK505_ENABLE;
  6005. else
  6006. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6007. if (has_panel) {
  6008. val &= ~DREF_SSC_SOURCE_MASK;
  6009. val |= DREF_SSC_SOURCE_ENABLE;
  6010. /* SSC must be turned on before enabling the CPU output */
  6011. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6012. DRM_DEBUG_KMS("Using SSC on panel\n");
  6013. val |= DREF_SSC1_ENABLE;
  6014. } else
  6015. val &= ~DREF_SSC1_ENABLE;
  6016. /* Get SSC going before enabling the outputs */
  6017. I915_WRITE(PCH_DREF_CONTROL, val);
  6018. POSTING_READ(PCH_DREF_CONTROL);
  6019. udelay(200);
  6020. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6021. /* Enable CPU source on CPU attached eDP */
  6022. if (has_cpu_edp) {
  6023. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6024. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6025. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6026. } else
  6027. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6028. } else
  6029. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6030. I915_WRITE(PCH_DREF_CONTROL, val);
  6031. POSTING_READ(PCH_DREF_CONTROL);
  6032. udelay(200);
  6033. } else {
  6034. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6035. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6036. /* Turn off CPU output */
  6037. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6038. I915_WRITE(PCH_DREF_CONTROL, val);
  6039. POSTING_READ(PCH_DREF_CONTROL);
  6040. udelay(200);
  6041. /* Turn off the SSC source */
  6042. val &= ~DREF_SSC_SOURCE_MASK;
  6043. val |= DREF_SSC_SOURCE_DISABLE;
  6044. /* Turn off SSC1 */
  6045. val &= ~DREF_SSC1_ENABLE;
  6046. I915_WRITE(PCH_DREF_CONTROL, val);
  6047. POSTING_READ(PCH_DREF_CONTROL);
  6048. udelay(200);
  6049. }
  6050. BUG_ON(val != final);
  6051. }
  6052. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6053. {
  6054. uint32_t tmp;
  6055. tmp = I915_READ(SOUTH_CHICKEN2);
  6056. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6057. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6058. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  6059. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6060. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6061. tmp = I915_READ(SOUTH_CHICKEN2);
  6062. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6063. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6064. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  6065. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6066. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6067. }
  6068. /* WaMPhyProgramming:hsw */
  6069. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6070. {
  6071. uint32_t tmp;
  6072. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6073. tmp &= ~(0xFF << 24);
  6074. tmp |= (0x12 << 24);
  6075. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6076. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6077. tmp |= (1 << 11);
  6078. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6079. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6080. tmp |= (1 << 11);
  6081. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6082. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6083. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6084. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6085. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6086. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6087. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6088. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6089. tmp &= ~(7 << 13);
  6090. tmp |= (5 << 13);
  6091. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6092. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6093. tmp &= ~(7 << 13);
  6094. tmp |= (5 << 13);
  6095. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6096. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6097. tmp &= ~0xFF;
  6098. tmp |= 0x1C;
  6099. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6100. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6101. tmp &= ~0xFF;
  6102. tmp |= 0x1C;
  6103. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6104. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6105. tmp &= ~(0xFF << 16);
  6106. tmp |= (0x1C << 16);
  6107. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6108. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6109. tmp &= ~(0xFF << 16);
  6110. tmp |= (0x1C << 16);
  6111. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6112. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6113. tmp |= (1 << 27);
  6114. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6115. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6116. tmp |= (1 << 27);
  6117. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6118. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6119. tmp &= ~(0xF << 28);
  6120. tmp |= (4 << 28);
  6121. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6122. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6123. tmp &= ~(0xF << 28);
  6124. tmp |= (4 << 28);
  6125. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6126. }
  6127. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6128. * Programming" based on the parameters passed:
  6129. * - Sequence to enable CLKOUT_DP
  6130. * - Sequence to enable CLKOUT_DP without spread
  6131. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6132. */
  6133. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  6134. bool with_fdi)
  6135. {
  6136. struct drm_i915_private *dev_priv = dev->dev_private;
  6137. uint32_t reg, tmp;
  6138. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6139. with_spread = true;
  6140. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  6141. with_fdi, "LP PCH doesn't have FDI\n"))
  6142. with_fdi = false;
  6143. mutex_lock(&dev_priv->dpio_lock);
  6144. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6145. tmp &= ~SBI_SSCCTL_DISABLE;
  6146. tmp |= SBI_SSCCTL_PATHALT;
  6147. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6148. udelay(24);
  6149. if (with_spread) {
  6150. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6151. tmp &= ~SBI_SSCCTL_PATHALT;
  6152. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6153. if (with_fdi) {
  6154. lpt_reset_fdi_mphy(dev_priv);
  6155. lpt_program_fdi_mphy(dev_priv);
  6156. }
  6157. }
  6158. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6159. SBI_GEN0 : SBI_DBUFF0;
  6160. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6161. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6162. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6163. mutex_unlock(&dev_priv->dpio_lock);
  6164. }
  6165. /* Sequence to disable CLKOUT_DP */
  6166. static void lpt_disable_clkout_dp(struct drm_device *dev)
  6167. {
  6168. struct drm_i915_private *dev_priv = dev->dev_private;
  6169. uint32_t reg, tmp;
  6170. mutex_lock(&dev_priv->dpio_lock);
  6171. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  6172. SBI_GEN0 : SBI_DBUFF0;
  6173. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6174. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6175. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6176. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6177. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6178. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6179. tmp |= SBI_SSCCTL_PATHALT;
  6180. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6181. udelay(32);
  6182. }
  6183. tmp |= SBI_SSCCTL_DISABLE;
  6184. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6185. }
  6186. mutex_unlock(&dev_priv->dpio_lock);
  6187. }
  6188. static void lpt_init_pch_refclk(struct drm_device *dev)
  6189. {
  6190. struct intel_encoder *encoder;
  6191. bool has_vga = false;
  6192. for_each_intel_encoder(dev, encoder) {
  6193. switch (encoder->type) {
  6194. case INTEL_OUTPUT_ANALOG:
  6195. has_vga = true;
  6196. break;
  6197. default:
  6198. break;
  6199. }
  6200. }
  6201. if (has_vga)
  6202. lpt_enable_clkout_dp(dev, true, true);
  6203. else
  6204. lpt_disable_clkout_dp(dev);
  6205. }
  6206. /*
  6207. * Initialize reference clocks when the driver loads
  6208. */
  6209. void intel_init_pch_refclk(struct drm_device *dev)
  6210. {
  6211. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  6212. ironlake_init_pch_refclk(dev);
  6213. else if (HAS_PCH_LPT(dev))
  6214. lpt_init_pch_refclk(dev);
  6215. }
  6216. static int ironlake_get_refclk(struct drm_crtc *crtc)
  6217. {
  6218. struct drm_device *dev = crtc->dev;
  6219. struct drm_i915_private *dev_priv = dev->dev_private;
  6220. struct intel_encoder *encoder;
  6221. int num_connectors = 0;
  6222. bool is_lvds = false;
  6223. for_each_intel_encoder(dev, encoder) {
  6224. if (encoder->new_crtc != to_intel_crtc(crtc))
  6225. continue;
  6226. switch (encoder->type) {
  6227. case INTEL_OUTPUT_LVDS:
  6228. is_lvds = true;
  6229. break;
  6230. default:
  6231. break;
  6232. }
  6233. num_connectors++;
  6234. }
  6235. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6236. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6237. dev_priv->vbt.lvds_ssc_freq);
  6238. return dev_priv->vbt.lvds_ssc_freq;
  6239. }
  6240. return 120000;
  6241. }
  6242. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6243. {
  6244. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  6245. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6246. int pipe = intel_crtc->pipe;
  6247. uint32_t val;
  6248. val = 0;
  6249. switch (intel_crtc->config->pipe_bpp) {
  6250. case 18:
  6251. val |= PIPECONF_6BPC;
  6252. break;
  6253. case 24:
  6254. val |= PIPECONF_8BPC;
  6255. break;
  6256. case 30:
  6257. val |= PIPECONF_10BPC;
  6258. break;
  6259. case 36:
  6260. val |= PIPECONF_12BPC;
  6261. break;
  6262. default:
  6263. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6264. BUG();
  6265. }
  6266. if (intel_crtc->config->dither)
  6267. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6268. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6269. val |= PIPECONF_INTERLACED_ILK;
  6270. else
  6271. val |= PIPECONF_PROGRESSIVE;
  6272. if (intel_crtc->config->limited_color_range)
  6273. val |= PIPECONF_COLOR_RANGE_SELECT;
  6274. I915_WRITE(PIPECONF(pipe), val);
  6275. POSTING_READ(PIPECONF(pipe));
  6276. }
  6277. /*
  6278. * Set up the pipe CSC unit.
  6279. *
  6280. * Currently only full range RGB to limited range RGB conversion
  6281. * is supported, but eventually this should handle various
  6282. * RGB<->YCbCr scenarios as well.
  6283. */
  6284. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6285. {
  6286. struct drm_device *dev = crtc->dev;
  6287. struct drm_i915_private *dev_priv = dev->dev_private;
  6288. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6289. int pipe = intel_crtc->pipe;
  6290. uint16_t coeff = 0x7800; /* 1.0 */
  6291. /*
  6292. * TODO: Check what kind of values actually come out of the pipe
  6293. * with these coeff/postoff values and adjust to get the best
  6294. * accuracy. Perhaps we even need to take the bpc value into
  6295. * consideration.
  6296. */
  6297. if (intel_crtc->config->limited_color_range)
  6298. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6299. /*
  6300. * GY/GU and RY/RU should be the other way around according
  6301. * to BSpec, but reality doesn't agree. Just set them up in
  6302. * a way that results in the correct picture.
  6303. */
  6304. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6305. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6306. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6307. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6308. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6309. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6310. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6311. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6312. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6313. if (INTEL_INFO(dev)->gen > 6) {
  6314. uint16_t postoff = 0;
  6315. if (intel_crtc->config->limited_color_range)
  6316. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6317. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6318. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6319. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6320. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6321. } else {
  6322. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6323. if (intel_crtc->config->limited_color_range)
  6324. mode |= CSC_BLACK_SCREEN_OFFSET;
  6325. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6326. }
  6327. }
  6328. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6329. {
  6330. struct drm_device *dev = crtc->dev;
  6331. struct drm_i915_private *dev_priv = dev->dev_private;
  6332. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6333. enum pipe pipe = intel_crtc->pipe;
  6334. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6335. uint32_t val;
  6336. val = 0;
  6337. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6338. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6339. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6340. val |= PIPECONF_INTERLACED_ILK;
  6341. else
  6342. val |= PIPECONF_PROGRESSIVE;
  6343. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6344. POSTING_READ(PIPECONF(cpu_transcoder));
  6345. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6346. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6347. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6348. val = 0;
  6349. switch (intel_crtc->config->pipe_bpp) {
  6350. case 18:
  6351. val |= PIPEMISC_DITHER_6_BPC;
  6352. break;
  6353. case 24:
  6354. val |= PIPEMISC_DITHER_8_BPC;
  6355. break;
  6356. case 30:
  6357. val |= PIPEMISC_DITHER_10_BPC;
  6358. break;
  6359. case 36:
  6360. val |= PIPEMISC_DITHER_12_BPC;
  6361. break;
  6362. default:
  6363. /* Case prevented by pipe_config_set_bpp. */
  6364. BUG();
  6365. }
  6366. if (intel_crtc->config->dither)
  6367. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6368. I915_WRITE(PIPEMISC(pipe), val);
  6369. }
  6370. }
  6371. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6372. struct intel_crtc_state *crtc_state,
  6373. intel_clock_t *clock,
  6374. bool *has_reduced_clock,
  6375. intel_clock_t *reduced_clock)
  6376. {
  6377. struct drm_device *dev = crtc->dev;
  6378. struct drm_i915_private *dev_priv = dev->dev_private;
  6379. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6380. int refclk;
  6381. const intel_limit_t *limit;
  6382. bool ret, is_lvds = false;
  6383. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6384. refclk = ironlake_get_refclk(crtc);
  6385. /*
  6386. * Returns a set of divisors for the desired target clock with the given
  6387. * refclk, or FALSE. The returned values represent the clock equation:
  6388. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6389. */
  6390. limit = intel_limit(intel_crtc, refclk);
  6391. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6392. crtc_state->port_clock,
  6393. refclk, NULL, clock);
  6394. if (!ret)
  6395. return false;
  6396. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6397. /*
  6398. * Ensure we match the reduced clock's P to the target clock.
  6399. * If the clocks don't match, we can't switch the display clock
  6400. * by using the FP0/FP1. In such case we will disable the LVDS
  6401. * downclock feature.
  6402. */
  6403. *has_reduced_clock =
  6404. dev_priv->display.find_dpll(limit, intel_crtc,
  6405. dev_priv->lvds_downclock,
  6406. refclk, clock,
  6407. reduced_clock);
  6408. }
  6409. return true;
  6410. }
  6411. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6412. {
  6413. /*
  6414. * Account for spread spectrum to avoid
  6415. * oversubscribing the link. Max center spread
  6416. * is 2.5%; use 5% for safety's sake.
  6417. */
  6418. u32 bps = target_clock * bpp * 21 / 20;
  6419. return DIV_ROUND_UP(bps, link_bw * 8);
  6420. }
  6421. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6422. {
  6423. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6424. }
  6425. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6426. struct intel_crtc_state *crtc_state,
  6427. u32 *fp,
  6428. intel_clock_t *reduced_clock, u32 *fp2)
  6429. {
  6430. struct drm_crtc *crtc = &intel_crtc->base;
  6431. struct drm_device *dev = crtc->dev;
  6432. struct drm_i915_private *dev_priv = dev->dev_private;
  6433. struct intel_encoder *intel_encoder;
  6434. uint32_t dpll;
  6435. int factor, num_connectors = 0;
  6436. bool is_lvds = false, is_sdvo = false;
  6437. for_each_intel_encoder(dev, intel_encoder) {
  6438. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6439. continue;
  6440. switch (intel_encoder->type) {
  6441. case INTEL_OUTPUT_LVDS:
  6442. is_lvds = true;
  6443. break;
  6444. case INTEL_OUTPUT_SDVO:
  6445. case INTEL_OUTPUT_HDMI:
  6446. is_sdvo = true;
  6447. break;
  6448. default:
  6449. break;
  6450. }
  6451. num_connectors++;
  6452. }
  6453. /* Enable autotuning of the PLL clock (if permissible) */
  6454. factor = 21;
  6455. if (is_lvds) {
  6456. if ((intel_panel_use_ssc(dev_priv) &&
  6457. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6458. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6459. factor = 25;
  6460. } else if (crtc_state->sdvo_tv_clock)
  6461. factor = 20;
  6462. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6463. *fp |= FP_CB_TUNE;
  6464. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6465. *fp2 |= FP_CB_TUNE;
  6466. dpll = 0;
  6467. if (is_lvds)
  6468. dpll |= DPLLB_MODE_LVDS;
  6469. else
  6470. dpll |= DPLLB_MODE_DAC_SERIAL;
  6471. dpll |= (crtc_state->pixel_multiplier - 1)
  6472. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6473. if (is_sdvo)
  6474. dpll |= DPLL_SDVO_HIGH_SPEED;
  6475. if (crtc_state->has_dp_encoder)
  6476. dpll |= DPLL_SDVO_HIGH_SPEED;
  6477. /* compute bitmask from p1 value */
  6478. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6479. /* also FPA1 */
  6480. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6481. switch (crtc_state->dpll.p2) {
  6482. case 5:
  6483. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6484. break;
  6485. case 7:
  6486. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6487. break;
  6488. case 10:
  6489. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6490. break;
  6491. case 14:
  6492. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6493. break;
  6494. }
  6495. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6496. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6497. else
  6498. dpll |= PLL_REF_INPUT_DREFCLK;
  6499. return dpll | DPLL_VCO_ENABLE;
  6500. }
  6501. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6502. struct intel_crtc_state *crtc_state)
  6503. {
  6504. struct drm_device *dev = crtc->base.dev;
  6505. intel_clock_t clock, reduced_clock;
  6506. u32 dpll = 0, fp = 0, fp2 = 0;
  6507. bool ok, has_reduced_clock = false;
  6508. bool is_lvds = false;
  6509. struct intel_shared_dpll *pll;
  6510. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6511. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6512. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6513. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  6514. &has_reduced_clock, &reduced_clock);
  6515. if (!ok && !crtc_state->clock_set) {
  6516. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6517. return -EINVAL;
  6518. }
  6519. /* Compat-code for transition, will disappear. */
  6520. if (!crtc_state->clock_set) {
  6521. crtc_state->dpll.n = clock.n;
  6522. crtc_state->dpll.m1 = clock.m1;
  6523. crtc_state->dpll.m2 = clock.m2;
  6524. crtc_state->dpll.p1 = clock.p1;
  6525. crtc_state->dpll.p2 = clock.p2;
  6526. }
  6527. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6528. if (crtc_state->has_pch_encoder) {
  6529. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6530. if (has_reduced_clock)
  6531. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6532. dpll = ironlake_compute_dpll(crtc, crtc_state,
  6533. &fp, &reduced_clock,
  6534. has_reduced_clock ? &fp2 : NULL);
  6535. crtc_state->dpll_hw_state.dpll = dpll;
  6536. crtc_state->dpll_hw_state.fp0 = fp;
  6537. if (has_reduced_clock)
  6538. crtc_state->dpll_hw_state.fp1 = fp2;
  6539. else
  6540. crtc_state->dpll_hw_state.fp1 = fp;
  6541. pll = intel_get_shared_dpll(crtc, crtc_state);
  6542. if (pll == NULL) {
  6543. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6544. pipe_name(crtc->pipe));
  6545. return -EINVAL;
  6546. }
  6547. }
  6548. if (is_lvds && has_reduced_clock && i915.powersave)
  6549. crtc->lowfreq_avail = true;
  6550. else
  6551. crtc->lowfreq_avail = false;
  6552. return 0;
  6553. }
  6554. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6555. struct intel_link_m_n *m_n)
  6556. {
  6557. struct drm_device *dev = crtc->base.dev;
  6558. struct drm_i915_private *dev_priv = dev->dev_private;
  6559. enum pipe pipe = crtc->pipe;
  6560. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6561. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6562. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6563. & ~TU_SIZE_MASK;
  6564. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6565. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6566. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6567. }
  6568. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6569. enum transcoder transcoder,
  6570. struct intel_link_m_n *m_n,
  6571. struct intel_link_m_n *m2_n2)
  6572. {
  6573. struct drm_device *dev = crtc->base.dev;
  6574. struct drm_i915_private *dev_priv = dev->dev_private;
  6575. enum pipe pipe = crtc->pipe;
  6576. if (INTEL_INFO(dev)->gen >= 5) {
  6577. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6578. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6579. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6580. & ~TU_SIZE_MASK;
  6581. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6582. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6583. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6584. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6585. * gen < 8) and if DRRS is supported (to make sure the
  6586. * registers are not unnecessarily read).
  6587. */
  6588. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6589. crtc->config->has_drrs) {
  6590. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6591. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6592. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6593. & ~TU_SIZE_MASK;
  6594. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6595. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6596. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6597. }
  6598. } else {
  6599. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6600. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6601. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6602. & ~TU_SIZE_MASK;
  6603. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6604. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6605. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6606. }
  6607. }
  6608. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6609. struct intel_crtc_state *pipe_config)
  6610. {
  6611. if (pipe_config->has_pch_encoder)
  6612. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6613. else
  6614. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6615. &pipe_config->dp_m_n,
  6616. &pipe_config->dp_m2_n2);
  6617. }
  6618. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6619. struct intel_crtc_state *pipe_config)
  6620. {
  6621. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6622. &pipe_config->fdi_m_n, NULL);
  6623. }
  6624. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6625. struct intel_crtc_state *pipe_config)
  6626. {
  6627. struct drm_device *dev = crtc->base.dev;
  6628. struct drm_i915_private *dev_priv = dev->dev_private;
  6629. uint32_t tmp;
  6630. tmp = I915_READ(PS_CTL(crtc->pipe));
  6631. if (tmp & PS_ENABLE) {
  6632. pipe_config->pch_pfit.enabled = true;
  6633. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6634. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6635. }
  6636. }
  6637. static void
  6638. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6639. struct intel_initial_plane_config *plane_config)
  6640. {
  6641. struct drm_device *dev = crtc->base.dev;
  6642. struct drm_i915_private *dev_priv = dev->dev_private;
  6643. u32 val, base, offset, stride_mult, tiling;
  6644. int pipe = crtc->pipe;
  6645. int fourcc, pixel_format;
  6646. unsigned int aligned_height;
  6647. struct drm_framebuffer *fb;
  6648. struct intel_framebuffer *intel_fb;
  6649. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6650. if (!intel_fb) {
  6651. DRM_DEBUG_KMS("failed to alloc fb\n");
  6652. return;
  6653. }
  6654. fb = &intel_fb->base;
  6655. val = I915_READ(PLANE_CTL(pipe, 0));
  6656. if (!(val & PLANE_CTL_ENABLE))
  6657. goto error;
  6658. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  6659. fourcc = skl_format_to_fourcc(pixel_format,
  6660. val & PLANE_CTL_ORDER_RGBX,
  6661. val & PLANE_CTL_ALPHA_MASK);
  6662. fb->pixel_format = fourcc;
  6663. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6664. tiling = val & PLANE_CTL_TILED_MASK;
  6665. switch (tiling) {
  6666. case PLANE_CTL_TILED_LINEAR:
  6667. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  6668. break;
  6669. case PLANE_CTL_TILED_X:
  6670. plane_config->tiling = I915_TILING_X;
  6671. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6672. break;
  6673. case PLANE_CTL_TILED_Y:
  6674. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  6675. break;
  6676. case PLANE_CTL_TILED_YF:
  6677. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  6678. break;
  6679. default:
  6680. MISSING_CASE(tiling);
  6681. goto error;
  6682. }
  6683. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  6684. plane_config->base = base;
  6685. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  6686. val = I915_READ(PLANE_SIZE(pipe, 0));
  6687. fb->height = ((val >> 16) & 0xfff) + 1;
  6688. fb->width = ((val >> 0) & 0x1fff) + 1;
  6689. val = I915_READ(PLANE_STRIDE(pipe, 0));
  6690. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  6691. fb->pixel_format);
  6692. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  6693. aligned_height = intel_fb_align_height(dev, fb->height,
  6694. fb->pixel_format,
  6695. fb->modifier[0]);
  6696. plane_config->size = fb->pitches[0] * aligned_height;
  6697. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6698. pipe_name(pipe), fb->width, fb->height,
  6699. fb->bits_per_pixel, base, fb->pitches[0],
  6700. plane_config->size);
  6701. plane_config->fb = intel_fb;
  6702. return;
  6703. error:
  6704. kfree(fb);
  6705. }
  6706. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6707. struct intel_crtc_state *pipe_config)
  6708. {
  6709. struct drm_device *dev = crtc->base.dev;
  6710. struct drm_i915_private *dev_priv = dev->dev_private;
  6711. uint32_t tmp;
  6712. tmp = I915_READ(PF_CTL(crtc->pipe));
  6713. if (tmp & PF_ENABLE) {
  6714. pipe_config->pch_pfit.enabled = true;
  6715. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6716. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6717. /* We currently do not free assignements of panel fitters on
  6718. * ivb/hsw (since we don't use the higher upscaling modes which
  6719. * differentiates them) so just WARN about this case for now. */
  6720. if (IS_GEN7(dev)) {
  6721. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6722. PF_PIPE_SEL_IVB(crtc->pipe));
  6723. }
  6724. }
  6725. }
  6726. static void
  6727. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  6728. struct intel_initial_plane_config *plane_config)
  6729. {
  6730. struct drm_device *dev = crtc->base.dev;
  6731. struct drm_i915_private *dev_priv = dev->dev_private;
  6732. u32 val, base, offset;
  6733. int pipe = crtc->pipe;
  6734. int fourcc, pixel_format;
  6735. unsigned int aligned_height;
  6736. struct drm_framebuffer *fb;
  6737. struct intel_framebuffer *intel_fb;
  6738. val = I915_READ(DSPCNTR(pipe));
  6739. if (!(val & DISPLAY_PLANE_ENABLE))
  6740. return;
  6741. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6742. if (!intel_fb) {
  6743. DRM_DEBUG_KMS("failed to alloc fb\n");
  6744. return;
  6745. }
  6746. fb = &intel_fb->base;
  6747. if (INTEL_INFO(dev)->gen >= 4) {
  6748. if (val & DISPPLANE_TILED) {
  6749. plane_config->tiling = I915_TILING_X;
  6750. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6751. }
  6752. }
  6753. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6754. fourcc = i9xx_format_to_fourcc(pixel_format);
  6755. fb->pixel_format = fourcc;
  6756. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6757. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  6758. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6759. offset = I915_READ(DSPOFFSET(pipe));
  6760. } else {
  6761. if (plane_config->tiling)
  6762. offset = I915_READ(DSPTILEOFF(pipe));
  6763. else
  6764. offset = I915_READ(DSPLINOFF(pipe));
  6765. }
  6766. plane_config->base = base;
  6767. val = I915_READ(PIPESRC(pipe));
  6768. fb->width = ((val >> 16) & 0xfff) + 1;
  6769. fb->height = ((val >> 0) & 0xfff) + 1;
  6770. val = I915_READ(DSPSTRIDE(pipe));
  6771. fb->pitches[0] = val & 0xffffffc0;
  6772. aligned_height = intel_fb_align_height(dev, fb->height,
  6773. fb->pixel_format,
  6774. fb->modifier[0]);
  6775. plane_config->size = fb->pitches[0] * aligned_height;
  6776. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6777. pipe_name(pipe), fb->width, fb->height,
  6778. fb->bits_per_pixel, base, fb->pitches[0],
  6779. plane_config->size);
  6780. plane_config->fb = intel_fb;
  6781. }
  6782. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6783. struct intel_crtc_state *pipe_config)
  6784. {
  6785. struct drm_device *dev = crtc->base.dev;
  6786. struct drm_i915_private *dev_priv = dev->dev_private;
  6787. uint32_t tmp;
  6788. if (!intel_display_power_is_enabled(dev_priv,
  6789. POWER_DOMAIN_PIPE(crtc->pipe)))
  6790. return false;
  6791. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6792. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6793. tmp = I915_READ(PIPECONF(crtc->pipe));
  6794. if (!(tmp & PIPECONF_ENABLE))
  6795. return false;
  6796. switch (tmp & PIPECONF_BPC_MASK) {
  6797. case PIPECONF_6BPC:
  6798. pipe_config->pipe_bpp = 18;
  6799. break;
  6800. case PIPECONF_8BPC:
  6801. pipe_config->pipe_bpp = 24;
  6802. break;
  6803. case PIPECONF_10BPC:
  6804. pipe_config->pipe_bpp = 30;
  6805. break;
  6806. case PIPECONF_12BPC:
  6807. pipe_config->pipe_bpp = 36;
  6808. break;
  6809. default:
  6810. break;
  6811. }
  6812. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6813. pipe_config->limited_color_range = true;
  6814. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6815. struct intel_shared_dpll *pll;
  6816. pipe_config->has_pch_encoder = true;
  6817. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6818. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6819. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6820. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6821. if (HAS_PCH_IBX(dev_priv->dev)) {
  6822. pipe_config->shared_dpll =
  6823. (enum intel_dpll_id) crtc->pipe;
  6824. } else {
  6825. tmp = I915_READ(PCH_DPLL_SEL);
  6826. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6827. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6828. else
  6829. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6830. }
  6831. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6832. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6833. &pipe_config->dpll_hw_state));
  6834. tmp = pipe_config->dpll_hw_state.dpll;
  6835. pipe_config->pixel_multiplier =
  6836. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6837. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6838. ironlake_pch_clock_get(crtc, pipe_config);
  6839. } else {
  6840. pipe_config->pixel_multiplier = 1;
  6841. }
  6842. intel_get_pipe_timings(crtc, pipe_config);
  6843. ironlake_get_pfit_config(crtc, pipe_config);
  6844. return true;
  6845. }
  6846. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6847. {
  6848. struct drm_device *dev = dev_priv->dev;
  6849. struct intel_crtc *crtc;
  6850. for_each_intel_crtc(dev, crtc)
  6851. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6852. pipe_name(crtc->pipe));
  6853. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6854. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6855. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6856. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6857. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6858. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6859. "CPU PWM1 enabled\n");
  6860. if (IS_HASWELL(dev))
  6861. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6862. "CPU PWM2 enabled\n");
  6863. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6864. "PCH PWM1 enabled\n");
  6865. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6866. "Utility pin enabled\n");
  6867. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6868. /*
  6869. * In theory we can still leave IRQs enabled, as long as only the HPD
  6870. * interrupts remain enabled. We used to check for that, but since it's
  6871. * gen-specific and since we only disable LCPLL after we fully disable
  6872. * the interrupts, the check below should be enough.
  6873. */
  6874. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6875. }
  6876. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6877. {
  6878. struct drm_device *dev = dev_priv->dev;
  6879. if (IS_HASWELL(dev))
  6880. return I915_READ(D_COMP_HSW);
  6881. else
  6882. return I915_READ(D_COMP_BDW);
  6883. }
  6884. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6885. {
  6886. struct drm_device *dev = dev_priv->dev;
  6887. if (IS_HASWELL(dev)) {
  6888. mutex_lock(&dev_priv->rps.hw_lock);
  6889. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6890. val))
  6891. DRM_ERROR("Failed to write to D_COMP\n");
  6892. mutex_unlock(&dev_priv->rps.hw_lock);
  6893. } else {
  6894. I915_WRITE(D_COMP_BDW, val);
  6895. POSTING_READ(D_COMP_BDW);
  6896. }
  6897. }
  6898. /*
  6899. * This function implements pieces of two sequences from BSpec:
  6900. * - Sequence for display software to disable LCPLL
  6901. * - Sequence for display software to allow package C8+
  6902. * The steps implemented here are just the steps that actually touch the LCPLL
  6903. * register. Callers should take care of disabling all the display engine
  6904. * functions, doing the mode unset, fixing interrupts, etc.
  6905. */
  6906. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6907. bool switch_to_fclk, bool allow_power_down)
  6908. {
  6909. uint32_t val;
  6910. assert_can_disable_lcpll(dev_priv);
  6911. val = I915_READ(LCPLL_CTL);
  6912. if (switch_to_fclk) {
  6913. val |= LCPLL_CD_SOURCE_FCLK;
  6914. I915_WRITE(LCPLL_CTL, val);
  6915. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6916. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6917. DRM_ERROR("Switching to FCLK failed\n");
  6918. val = I915_READ(LCPLL_CTL);
  6919. }
  6920. val |= LCPLL_PLL_DISABLE;
  6921. I915_WRITE(LCPLL_CTL, val);
  6922. POSTING_READ(LCPLL_CTL);
  6923. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6924. DRM_ERROR("LCPLL still locked\n");
  6925. val = hsw_read_dcomp(dev_priv);
  6926. val |= D_COMP_COMP_DISABLE;
  6927. hsw_write_dcomp(dev_priv, val);
  6928. ndelay(100);
  6929. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6930. 1))
  6931. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6932. if (allow_power_down) {
  6933. val = I915_READ(LCPLL_CTL);
  6934. val |= LCPLL_POWER_DOWN_ALLOW;
  6935. I915_WRITE(LCPLL_CTL, val);
  6936. POSTING_READ(LCPLL_CTL);
  6937. }
  6938. }
  6939. /*
  6940. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6941. * source.
  6942. */
  6943. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6944. {
  6945. uint32_t val;
  6946. val = I915_READ(LCPLL_CTL);
  6947. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6948. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6949. return;
  6950. /*
  6951. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6952. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6953. */
  6954. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6955. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6956. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6957. I915_WRITE(LCPLL_CTL, val);
  6958. POSTING_READ(LCPLL_CTL);
  6959. }
  6960. val = hsw_read_dcomp(dev_priv);
  6961. val |= D_COMP_COMP_FORCE;
  6962. val &= ~D_COMP_COMP_DISABLE;
  6963. hsw_write_dcomp(dev_priv, val);
  6964. val = I915_READ(LCPLL_CTL);
  6965. val &= ~LCPLL_PLL_DISABLE;
  6966. I915_WRITE(LCPLL_CTL, val);
  6967. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6968. DRM_ERROR("LCPLL not locked yet\n");
  6969. if (val & LCPLL_CD_SOURCE_FCLK) {
  6970. val = I915_READ(LCPLL_CTL);
  6971. val &= ~LCPLL_CD_SOURCE_FCLK;
  6972. I915_WRITE(LCPLL_CTL, val);
  6973. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6974. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6975. DRM_ERROR("Switching back to LCPLL failed\n");
  6976. }
  6977. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6978. }
  6979. /*
  6980. * Package states C8 and deeper are really deep PC states that can only be
  6981. * reached when all the devices on the system allow it, so even if the graphics
  6982. * device allows PC8+, it doesn't mean the system will actually get to these
  6983. * states. Our driver only allows PC8+ when going into runtime PM.
  6984. *
  6985. * The requirements for PC8+ are that all the outputs are disabled, the power
  6986. * well is disabled and most interrupts are disabled, and these are also
  6987. * requirements for runtime PM. When these conditions are met, we manually do
  6988. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6989. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6990. * hang the machine.
  6991. *
  6992. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6993. * the state of some registers, so when we come back from PC8+ we need to
  6994. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6995. * need to take care of the registers kept by RC6. Notice that this happens even
  6996. * if we don't put the device in PCI D3 state (which is what currently happens
  6997. * because of the runtime PM support).
  6998. *
  6999. * For more, read "Display Sequences for Package C8" on the hardware
  7000. * documentation.
  7001. */
  7002. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7003. {
  7004. struct drm_device *dev = dev_priv->dev;
  7005. uint32_t val;
  7006. DRM_DEBUG_KMS("Enabling package C8+\n");
  7007. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7008. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7009. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7010. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7011. }
  7012. lpt_disable_clkout_dp(dev);
  7013. hsw_disable_lcpll(dev_priv, true, true);
  7014. }
  7015. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7016. {
  7017. struct drm_device *dev = dev_priv->dev;
  7018. uint32_t val;
  7019. DRM_DEBUG_KMS("Disabling package C8+\n");
  7020. hsw_restore_lcpll(dev_priv);
  7021. lpt_init_pch_refclk(dev);
  7022. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7023. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7024. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7025. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7026. }
  7027. intel_prepare_ddi(dev);
  7028. }
  7029. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7030. struct intel_crtc_state *crtc_state)
  7031. {
  7032. if (!intel_ddi_pll_select(crtc, crtc_state))
  7033. return -EINVAL;
  7034. crtc->lowfreq_avail = false;
  7035. return 0;
  7036. }
  7037. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7038. enum port port,
  7039. struct intel_crtc_state *pipe_config)
  7040. {
  7041. u32 temp, dpll_ctl1;
  7042. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7043. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  7044. switch (pipe_config->ddi_pll_sel) {
  7045. case SKL_DPLL0:
  7046. /*
  7047. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  7048. * of the shared DPLL framework and thus needs to be read out
  7049. * separately
  7050. */
  7051. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  7052. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  7053. break;
  7054. case SKL_DPLL1:
  7055. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  7056. break;
  7057. case SKL_DPLL2:
  7058. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  7059. break;
  7060. case SKL_DPLL3:
  7061. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  7062. break;
  7063. }
  7064. }
  7065. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7066. enum port port,
  7067. struct intel_crtc_state *pipe_config)
  7068. {
  7069. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7070. switch (pipe_config->ddi_pll_sel) {
  7071. case PORT_CLK_SEL_WRPLL1:
  7072. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  7073. break;
  7074. case PORT_CLK_SEL_WRPLL2:
  7075. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  7076. break;
  7077. }
  7078. }
  7079. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7080. struct intel_crtc_state *pipe_config)
  7081. {
  7082. struct drm_device *dev = crtc->base.dev;
  7083. struct drm_i915_private *dev_priv = dev->dev_private;
  7084. struct intel_shared_dpll *pll;
  7085. enum port port;
  7086. uint32_t tmp;
  7087. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7088. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7089. if (IS_SKYLAKE(dev))
  7090. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7091. else
  7092. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7093. if (pipe_config->shared_dpll >= 0) {
  7094. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7095. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7096. &pipe_config->dpll_hw_state));
  7097. }
  7098. /*
  7099. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7100. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7101. * the PCH transcoder is on.
  7102. */
  7103. if (INTEL_INFO(dev)->gen < 9 &&
  7104. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7105. pipe_config->has_pch_encoder = true;
  7106. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7107. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7108. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7109. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7110. }
  7111. }
  7112. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7113. struct intel_crtc_state *pipe_config)
  7114. {
  7115. struct drm_device *dev = crtc->base.dev;
  7116. struct drm_i915_private *dev_priv = dev->dev_private;
  7117. enum intel_display_power_domain pfit_domain;
  7118. uint32_t tmp;
  7119. if (!intel_display_power_is_enabled(dev_priv,
  7120. POWER_DOMAIN_PIPE(crtc->pipe)))
  7121. return false;
  7122. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7123. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7124. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7125. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7126. enum pipe trans_edp_pipe;
  7127. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7128. default:
  7129. WARN(1, "unknown pipe linked to edp transcoder\n");
  7130. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7131. case TRANS_DDI_EDP_INPUT_A_ON:
  7132. trans_edp_pipe = PIPE_A;
  7133. break;
  7134. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7135. trans_edp_pipe = PIPE_B;
  7136. break;
  7137. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7138. trans_edp_pipe = PIPE_C;
  7139. break;
  7140. }
  7141. if (trans_edp_pipe == crtc->pipe)
  7142. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7143. }
  7144. if (!intel_display_power_is_enabled(dev_priv,
  7145. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  7146. return false;
  7147. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7148. if (!(tmp & PIPECONF_ENABLE))
  7149. return false;
  7150. haswell_get_ddi_port_state(crtc, pipe_config);
  7151. intel_get_pipe_timings(crtc, pipe_config);
  7152. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7153. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  7154. if (IS_SKYLAKE(dev))
  7155. skylake_get_pfit_config(crtc, pipe_config);
  7156. else
  7157. ironlake_get_pfit_config(crtc, pipe_config);
  7158. }
  7159. if (IS_HASWELL(dev))
  7160. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7161. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7162. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  7163. pipe_config->pixel_multiplier =
  7164. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7165. } else {
  7166. pipe_config->pixel_multiplier = 1;
  7167. }
  7168. return true;
  7169. }
  7170. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  7171. {
  7172. struct drm_device *dev = crtc->dev;
  7173. struct drm_i915_private *dev_priv = dev->dev_private;
  7174. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7175. uint32_t cntl = 0, size = 0;
  7176. if (base) {
  7177. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  7178. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  7179. unsigned int stride = roundup_pow_of_two(width) * 4;
  7180. switch (stride) {
  7181. default:
  7182. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7183. width, stride);
  7184. stride = 256;
  7185. /* fallthrough */
  7186. case 256:
  7187. case 512:
  7188. case 1024:
  7189. case 2048:
  7190. break;
  7191. }
  7192. cntl |= CURSOR_ENABLE |
  7193. CURSOR_GAMMA_ENABLE |
  7194. CURSOR_FORMAT_ARGB |
  7195. CURSOR_STRIDE(stride);
  7196. size = (height << 12) | width;
  7197. }
  7198. if (intel_crtc->cursor_cntl != 0 &&
  7199. (intel_crtc->cursor_base != base ||
  7200. intel_crtc->cursor_size != size ||
  7201. intel_crtc->cursor_cntl != cntl)) {
  7202. /* On these chipsets we can only modify the base/size/stride
  7203. * whilst the cursor is disabled.
  7204. */
  7205. I915_WRITE(_CURACNTR, 0);
  7206. POSTING_READ(_CURACNTR);
  7207. intel_crtc->cursor_cntl = 0;
  7208. }
  7209. if (intel_crtc->cursor_base != base) {
  7210. I915_WRITE(_CURABASE, base);
  7211. intel_crtc->cursor_base = base;
  7212. }
  7213. if (intel_crtc->cursor_size != size) {
  7214. I915_WRITE(CURSIZE, size);
  7215. intel_crtc->cursor_size = size;
  7216. }
  7217. if (intel_crtc->cursor_cntl != cntl) {
  7218. I915_WRITE(_CURACNTR, cntl);
  7219. POSTING_READ(_CURACNTR);
  7220. intel_crtc->cursor_cntl = cntl;
  7221. }
  7222. }
  7223. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  7224. {
  7225. struct drm_device *dev = crtc->dev;
  7226. struct drm_i915_private *dev_priv = dev->dev_private;
  7227. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7228. int pipe = intel_crtc->pipe;
  7229. uint32_t cntl;
  7230. cntl = 0;
  7231. if (base) {
  7232. cntl = MCURSOR_GAMMA_ENABLE;
  7233. switch (intel_crtc->base.cursor->state->crtc_w) {
  7234. case 64:
  7235. cntl |= CURSOR_MODE_64_ARGB_AX;
  7236. break;
  7237. case 128:
  7238. cntl |= CURSOR_MODE_128_ARGB_AX;
  7239. break;
  7240. case 256:
  7241. cntl |= CURSOR_MODE_256_ARGB_AX;
  7242. break;
  7243. default:
  7244. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  7245. return;
  7246. }
  7247. cntl |= pipe << 28; /* Connect to correct pipe */
  7248. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  7249. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7250. }
  7251. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  7252. cntl |= CURSOR_ROTATE_180;
  7253. if (intel_crtc->cursor_cntl != cntl) {
  7254. I915_WRITE(CURCNTR(pipe), cntl);
  7255. POSTING_READ(CURCNTR(pipe));
  7256. intel_crtc->cursor_cntl = cntl;
  7257. }
  7258. /* and commit changes on next vblank */
  7259. I915_WRITE(CURBASE(pipe), base);
  7260. POSTING_READ(CURBASE(pipe));
  7261. intel_crtc->cursor_base = base;
  7262. }
  7263. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7264. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7265. bool on)
  7266. {
  7267. struct drm_device *dev = crtc->dev;
  7268. struct drm_i915_private *dev_priv = dev->dev_private;
  7269. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7270. int pipe = intel_crtc->pipe;
  7271. int x = crtc->cursor_x;
  7272. int y = crtc->cursor_y;
  7273. u32 base = 0, pos = 0;
  7274. if (on)
  7275. base = intel_crtc->cursor_addr;
  7276. if (x >= intel_crtc->config->pipe_src_w)
  7277. base = 0;
  7278. if (y >= intel_crtc->config->pipe_src_h)
  7279. base = 0;
  7280. if (x < 0) {
  7281. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  7282. base = 0;
  7283. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7284. x = -x;
  7285. }
  7286. pos |= x << CURSOR_X_SHIFT;
  7287. if (y < 0) {
  7288. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  7289. base = 0;
  7290. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7291. y = -y;
  7292. }
  7293. pos |= y << CURSOR_Y_SHIFT;
  7294. if (base == 0 && intel_crtc->cursor_base == 0)
  7295. return;
  7296. I915_WRITE(CURPOS(pipe), pos);
  7297. /* ILK+ do this automagically */
  7298. if (HAS_GMCH_DISPLAY(dev) &&
  7299. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  7300. base += (intel_crtc->base.cursor->state->crtc_h *
  7301. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  7302. }
  7303. if (IS_845G(dev) || IS_I865G(dev))
  7304. i845_update_cursor(crtc, base);
  7305. else
  7306. i9xx_update_cursor(crtc, base);
  7307. }
  7308. static bool cursor_size_ok(struct drm_device *dev,
  7309. uint32_t width, uint32_t height)
  7310. {
  7311. if (width == 0 || height == 0)
  7312. return false;
  7313. /*
  7314. * 845g/865g are special in that they are only limited by
  7315. * the width of their cursors, the height is arbitrary up to
  7316. * the precision of the register. Everything else requires
  7317. * square cursors, limited to a few power-of-two sizes.
  7318. */
  7319. if (IS_845G(dev) || IS_I865G(dev)) {
  7320. if ((width & 63) != 0)
  7321. return false;
  7322. if (width > (IS_845G(dev) ? 64 : 512))
  7323. return false;
  7324. if (height > 1023)
  7325. return false;
  7326. } else {
  7327. switch (width | height) {
  7328. case 256:
  7329. case 128:
  7330. if (IS_GEN2(dev))
  7331. return false;
  7332. case 64:
  7333. break;
  7334. default:
  7335. return false;
  7336. }
  7337. }
  7338. return true;
  7339. }
  7340. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7341. u16 *blue, uint32_t start, uint32_t size)
  7342. {
  7343. int end = (start + size > 256) ? 256 : start + size, i;
  7344. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7345. for (i = start; i < end; i++) {
  7346. intel_crtc->lut_r[i] = red[i] >> 8;
  7347. intel_crtc->lut_g[i] = green[i] >> 8;
  7348. intel_crtc->lut_b[i] = blue[i] >> 8;
  7349. }
  7350. intel_crtc_load_lut(crtc);
  7351. }
  7352. /* VESA 640x480x72Hz mode to set on the pipe */
  7353. static struct drm_display_mode load_detect_mode = {
  7354. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7355. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7356. };
  7357. struct drm_framebuffer *
  7358. __intel_framebuffer_create(struct drm_device *dev,
  7359. struct drm_mode_fb_cmd2 *mode_cmd,
  7360. struct drm_i915_gem_object *obj)
  7361. {
  7362. struct intel_framebuffer *intel_fb;
  7363. int ret;
  7364. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7365. if (!intel_fb) {
  7366. drm_gem_object_unreference(&obj->base);
  7367. return ERR_PTR(-ENOMEM);
  7368. }
  7369. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7370. if (ret)
  7371. goto err;
  7372. return &intel_fb->base;
  7373. err:
  7374. drm_gem_object_unreference(&obj->base);
  7375. kfree(intel_fb);
  7376. return ERR_PTR(ret);
  7377. }
  7378. static struct drm_framebuffer *
  7379. intel_framebuffer_create(struct drm_device *dev,
  7380. struct drm_mode_fb_cmd2 *mode_cmd,
  7381. struct drm_i915_gem_object *obj)
  7382. {
  7383. struct drm_framebuffer *fb;
  7384. int ret;
  7385. ret = i915_mutex_lock_interruptible(dev);
  7386. if (ret)
  7387. return ERR_PTR(ret);
  7388. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7389. mutex_unlock(&dev->struct_mutex);
  7390. return fb;
  7391. }
  7392. static u32
  7393. intel_framebuffer_pitch_for_width(int width, int bpp)
  7394. {
  7395. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7396. return ALIGN(pitch, 64);
  7397. }
  7398. static u32
  7399. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7400. {
  7401. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7402. return PAGE_ALIGN(pitch * mode->vdisplay);
  7403. }
  7404. static struct drm_framebuffer *
  7405. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7406. struct drm_display_mode *mode,
  7407. int depth, int bpp)
  7408. {
  7409. struct drm_i915_gem_object *obj;
  7410. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7411. obj = i915_gem_alloc_object(dev,
  7412. intel_framebuffer_size_for_mode(mode, bpp));
  7413. if (obj == NULL)
  7414. return ERR_PTR(-ENOMEM);
  7415. mode_cmd.width = mode->hdisplay;
  7416. mode_cmd.height = mode->vdisplay;
  7417. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7418. bpp);
  7419. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7420. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7421. }
  7422. static struct drm_framebuffer *
  7423. mode_fits_in_fbdev(struct drm_device *dev,
  7424. struct drm_display_mode *mode)
  7425. {
  7426. #ifdef CONFIG_DRM_I915_FBDEV
  7427. struct drm_i915_private *dev_priv = dev->dev_private;
  7428. struct drm_i915_gem_object *obj;
  7429. struct drm_framebuffer *fb;
  7430. if (!dev_priv->fbdev)
  7431. return NULL;
  7432. if (!dev_priv->fbdev->fb)
  7433. return NULL;
  7434. obj = dev_priv->fbdev->fb->obj;
  7435. BUG_ON(!obj);
  7436. fb = &dev_priv->fbdev->fb->base;
  7437. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7438. fb->bits_per_pixel))
  7439. return NULL;
  7440. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7441. return NULL;
  7442. return fb;
  7443. #else
  7444. return NULL;
  7445. #endif
  7446. }
  7447. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7448. struct drm_display_mode *mode,
  7449. struct intel_load_detect_pipe *old,
  7450. struct drm_modeset_acquire_ctx *ctx)
  7451. {
  7452. struct intel_crtc *intel_crtc;
  7453. struct intel_encoder *intel_encoder =
  7454. intel_attached_encoder(connector);
  7455. struct drm_crtc *possible_crtc;
  7456. struct drm_encoder *encoder = &intel_encoder->base;
  7457. struct drm_crtc *crtc = NULL;
  7458. struct drm_device *dev = encoder->dev;
  7459. struct drm_framebuffer *fb;
  7460. struct drm_mode_config *config = &dev->mode_config;
  7461. int ret, i = -1;
  7462. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7463. connector->base.id, connector->name,
  7464. encoder->base.id, encoder->name);
  7465. retry:
  7466. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7467. if (ret)
  7468. goto fail_unlock;
  7469. /*
  7470. * Algorithm gets a little messy:
  7471. *
  7472. * - if the connector already has an assigned crtc, use it (but make
  7473. * sure it's on first)
  7474. *
  7475. * - try to find the first unused crtc that can drive this connector,
  7476. * and use that if we find one
  7477. */
  7478. /* See if we already have a CRTC for this connector */
  7479. if (encoder->crtc) {
  7480. crtc = encoder->crtc;
  7481. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7482. if (ret)
  7483. goto fail_unlock;
  7484. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7485. if (ret)
  7486. goto fail_unlock;
  7487. old->dpms_mode = connector->dpms;
  7488. old->load_detect_temp = false;
  7489. /* Make sure the crtc and connector are running */
  7490. if (connector->dpms != DRM_MODE_DPMS_ON)
  7491. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7492. return true;
  7493. }
  7494. /* Find an unused one (if possible) */
  7495. for_each_crtc(dev, possible_crtc) {
  7496. i++;
  7497. if (!(encoder->possible_crtcs & (1 << i)))
  7498. continue;
  7499. if (possible_crtc->state->enable)
  7500. continue;
  7501. /* This can occur when applying the pipe A quirk on resume. */
  7502. if (to_intel_crtc(possible_crtc)->new_enabled)
  7503. continue;
  7504. crtc = possible_crtc;
  7505. break;
  7506. }
  7507. /*
  7508. * If we didn't find an unused CRTC, don't use any.
  7509. */
  7510. if (!crtc) {
  7511. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7512. goto fail_unlock;
  7513. }
  7514. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7515. if (ret)
  7516. goto fail_unlock;
  7517. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7518. if (ret)
  7519. goto fail_unlock;
  7520. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7521. to_intel_connector(connector)->new_encoder = intel_encoder;
  7522. intel_crtc = to_intel_crtc(crtc);
  7523. intel_crtc->new_enabled = true;
  7524. intel_crtc->new_config = intel_crtc->config;
  7525. old->dpms_mode = connector->dpms;
  7526. old->load_detect_temp = true;
  7527. old->release_fb = NULL;
  7528. if (!mode)
  7529. mode = &load_detect_mode;
  7530. /* We need a framebuffer large enough to accommodate all accesses
  7531. * that the plane may generate whilst we perform load detection.
  7532. * We can not rely on the fbcon either being present (we get called
  7533. * during its initialisation to detect all boot displays, or it may
  7534. * not even exist) or that it is large enough to satisfy the
  7535. * requested mode.
  7536. */
  7537. fb = mode_fits_in_fbdev(dev, mode);
  7538. if (fb == NULL) {
  7539. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7540. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7541. old->release_fb = fb;
  7542. } else
  7543. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7544. if (IS_ERR(fb)) {
  7545. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7546. goto fail;
  7547. }
  7548. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7549. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7550. if (old->release_fb)
  7551. old->release_fb->funcs->destroy(old->release_fb);
  7552. goto fail;
  7553. }
  7554. crtc->primary->crtc = crtc;
  7555. /* let the connector get through one full cycle before testing */
  7556. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7557. return true;
  7558. fail:
  7559. intel_crtc->new_enabled = crtc->state->enable;
  7560. if (intel_crtc->new_enabled)
  7561. intel_crtc->new_config = intel_crtc->config;
  7562. else
  7563. intel_crtc->new_config = NULL;
  7564. fail_unlock:
  7565. if (ret == -EDEADLK) {
  7566. drm_modeset_backoff(ctx);
  7567. goto retry;
  7568. }
  7569. return false;
  7570. }
  7571. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7572. struct intel_load_detect_pipe *old)
  7573. {
  7574. struct intel_encoder *intel_encoder =
  7575. intel_attached_encoder(connector);
  7576. struct drm_encoder *encoder = &intel_encoder->base;
  7577. struct drm_crtc *crtc = encoder->crtc;
  7578. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7579. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7580. connector->base.id, connector->name,
  7581. encoder->base.id, encoder->name);
  7582. if (old->load_detect_temp) {
  7583. to_intel_connector(connector)->new_encoder = NULL;
  7584. intel_encoder->new_crtc = NULL;
  7585. intel_crtc->new_enabled = false;
  7586. intel_crtc->new_config = NULL;
  7587. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7588. if (old->release_fb) {
  7589. drm_framebuffer_unregister_private(old->release_fb);
  7590. drm_framebuffer_unreference(old->release_fb);
  7591. }
  7592. return;
  7593. }
  7594. /* Switch crtc and encoder back off if necessary */
  7595. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7596. connector->funcs->dpms(connector, old->dpms_mode);
  7597. }
  7598. static int i9xx_pll_refclk(struct drm_device *dev,
  7599. const struct intel_crtc_state *pipe_config)
  7600. {
  7601. struct drm_i915_private *dev_priv = dev->dev_private;
  7602. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7603. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7604. return dev_priv->vbt.lvds_ssc_freq;
  7605. else if (HAS_PCH_SPLIT(dev))
  7606. return 120000;
  7607. else if (!IS_GEN2(dev))
  7608. return 96000;
  7609. else
  7610. return 48000;
  7611. }
  7612. /* Returns the clock of the currently programmed mode of the given pipe. */
  7613. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7614. struct intel_crtc_state *pipe_config)
  7615. {
  7616. struct drm_device *dev = crtc->base.dev;
  7617. struct drm_i915_private *dev_priv = dev->dev_private;
  7618. int pipe = pipe_config->cpu_transcoder;
  7619. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7620. u32 fp;
  7621. intel_clock_t clock;
  7622. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7623. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7624. fp = pipe_config->dpll_hw_state.fp0;
  7625. else
  7626. fp = pipe_config->dpll_hw_state.fp1;
  7627. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7628. if (IS_PINEVIEW(dev)) {
  7629. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7630. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7631. } else {
  7632. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7633. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7634. }
  7635. if (!IS_GEN2(dev)) {
  7636. if (IS_PINEVIEW(dev))
  7637. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7638. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7639. else
  7640. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7641. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7642. switch (dpll & DPLL_MODE_MASK) {
  7643. case DPLLB_MODE_DAC_SERIAL:
  7644. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7645. 5 : 10;
  7646. break;
  7647. case DPLLB_MODE_LVDS:
  7648. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7649. 7 : 14;
  7650. break;
  7651. default:
  7652. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7653. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7654. return;
  7655. }
  7656. if (IS_PINEVIEW(dev))
  7657. pineview_clock(refclk, &clock);
  7658. else
  7659. i9xx_clock(refclk, &clock);
  7660. } else {
  7661. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7662. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7663. if (is_lvds) {
  7664. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7665. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7666. if (lvds & LVDS_CLKB_POWER_UP)
  7667. clock.p2 = 7;
  7668. else
  7669. clock.p2 = 14;
  7670. } else {
  7671. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7672. clock.p1 = 2;
  7673. else {
  7674. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7675. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7676. }
  7677. if (dpll & PLL_P2_DIVIDE_BY_4)
  7678. clock.p2 = 4;
  7679. else
  7680. clock.p2 = 2;
  7681. }
  7682. i9xx_clock(refclk, &clock);
  7683. }
  7684. /*
  7685. * This value includes pixel_multiplier. We will use
  7686. * port_clock to compute adjusted_mode.crtc_clock in the
  7687. * encoder's get_config() function.
  7688. */
  7689. pipe_config->port_clock = clock.dot;
  7690. }
  7691. int intel_dotclock_calculate(int link_freq,
  7692. const struct intel_link_m_n *m_n)
  7693. {
  7694. /*
  7695. * The calculation for the data clock is:
  7696. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7697. * But we want to avoid losing precison if possible, so:
  7698. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7699. *
  7700. * and the link clock is simpler:
  7701. * link_clock = (m * link_clock) / n
  7702. */
  7703. if (!m_n->link_n)
  7704. return 0;
  7705. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7706. }
  7707. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7708. struct intel_crtc_state *pipe_config)
  7709. {
  7710. struct drm_device *dev = crtc->base.dev;
  7711. /* read out port_clock from the DPLL */
  7712. i9xx_crtc_clock_get(crtc, pipe_config);
  7713. /*
  7714. * This value does not include pixel_multiplier.
  7715. * We will check that port_clock and adjusted_mode.crtc_clock
  7716. * agree once we know their relationship in the encoder's
  7717. * get_config() function.
  7718. */
  7719. pipe_config->base.adjusted_mode.crtc_clock =
  7720. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7721. &pipe_config->fdi_m_n);
  7722. }
  7723. /** Returns the currently programmed mode of the given pipe. */
  7724. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7725. struct drm_crtc *crtc)
  7726. {
  7727. struct drm_i915_private *dev_priv = dev->dev_private;
  7728. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7729. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7730. struct drm_display_mode *mode;
  7731. struct intel_crtc_state pipe_config;
  7732. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7733. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7734. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7735. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7736. enum pipe pipe = intel_crtc->pipe;
  7737. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7738. if (!mode)
  7739. return NULL;
  7740. /*
  7741. * Construct a pipe_config sufficient for getting the clock info
  7742. * back out of crtc_clock_get.
  7743. *
  7744. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7745. * to use a real value here instead.
  7746. */
  7747. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7748. pipe_config.pixel_multiplier = 1;
  7749. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7750. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7751. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7752. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7753. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7754. mode->hdisplay = (htot & 0xffff) + 1;
  7755. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7756. mode->hsync_start = (hsync & 0xffff) + 1;
  7757. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7758. mode->vdisplay = (vtot & 0xffff) + 1;
  7759. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7760. mode->vsync_start = (vsync & 0xffff) + 1;
  7761. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7762. drm_mode_set_name(mode);
  7763. return mode;
  7764. }
  7765. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7766. {
  7767. struct drm_device *dev = crtc->dev;
  7768. struct drm_i915_private *dev_priv = dev->dev_private;
  7769. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7770. if (!HAS_GMCH_DISPLAY(dev))
  7771. return;
  7772. if (!dev_priv->lvds_downclock_avail)
  7773. return;
  7774. /*
  7775. * Since this is called by a timer, we should never get here in
  7776. * the manual case.
  7777. */
  7778. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7779. int pipe = intel_crtc->pipe;
  7780. int dpll_reg = DPLL(pipe);
  7781. int dpll;
  7782. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7783. assert_panel_unlocked(dev_priv, pipe);
  7784. dpll = I915_READ(dpll_reg);
  7785. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7786. I915_WRITE(dpll_reg, dpll);
  7787. intel_wait_for_vblank(dev, pipe);
  7788. dpll = I915_READ(dpll_reg);
  7789. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7790. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7791. }
  7792. }
  7793. void intel_mark_busy(struct drm_device *dev)
  7794. {
  7795. struct drm_i915_private *dev_priv = dev->dev_private;
  7796. if (dev_priv->mm.busy)
  7797. return;
  7798. intel_runtime_pm_get(dev_priv);
  7799. i915_update_gfx_val(dev_priv);
  7800. if (INTEL_INFO(dev)->gen >= 6)
  7801. gen6_rps_busy(dev_priv);
  7802. dev_priv->mm.busy = true;
  7803. }
  7804. void intel_mark_idle(struct drm_device *dev)
  7805. {
  7806. struct drm_i915_private *dev_priv = dev->dev_private;
  7807. struct drm_crtc *crtc;
  7808. if (!dev_priv->mm.busy)
  7809. return;
  7810. dev_priv->mm.busy = false;
  7811. if (!i915.powersave)
  7812. goto out;
  7813. for_each_crtc(dev, crtc) {
  7814. if (!crtc->primary->fb)
  7815. continue;
  7816. intel_decrease_pllclock(crtc);
  7817. }
  7818. if (INTEL_INFO(dev)->gen >= 6)
  7819. gen6_rps_idle(dev->dev_private);
  7820. out:
  7821. intel_runtime_pm_put(dev_priv);
  7822. }
  7823. static void intel_crtc_set_state(struct intel_crtc *crtc,
  7824. struct intel_crtc_state *crtc_state)
  7825. {
  7826. kfree(crtc->config);
  7827. crtc->config = crtc_state;
  7828. crtc->base.state = &crtc_state->base;
  7829. }
  7830. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7831. {
  7832. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7833. struct drm_device *dev = crtc->dev;
  7834. struct intel_unpin_work *work;
  7835. spin_lock_irq(&dev->event_lock);
  7836. work = intel_crtc->unpin_work;
  7837. intel_crtc->unpin_work = NULL;
  7838. spin_unlock_irq(&dev->event_lock);
  7839. if (work) {
  7840. cancel_work_sync(&work->work);
  7841. kfree(work);
  7842. }
  7843. intel_crtc_set_state(intel_crtc, NULL);
  7844. drm_crtc_cleanup(crtc);
  7845. kfree(intel_crtc);
  7846. }
  7847. static void intel_unpin_work_fn(struct work_struct *__work)
  7848. {
  7849. struct intel_unpin_work *work =
  7850. container_of(__work, struct intel_unpin_work, work);
  7851. struct drm_device *dev = work->crtc->dev;
  7852. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7853. mutex_lock(&dev->struct_mutex);
  7854. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  7855. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7856. intel_fbc_update(dev);
  7857. if (work->flip_queued_req)
  7858. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7859. mutex_unlock(&dev->struct_mutex);
  7860. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7861. drm_framebuffer_unreference(work->old_fb);
  7862. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7863. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7864. kfree(work);
  7865. }
  7866. static void do_intel_finish_page_flip(struct drm_device *dev,
  7867. struct drm_crtc *crtc)
  7868. {
  7869. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7870. struct intel_unpin_work *work;
  7871. unsigned long flags;
  7872. /* Ignore early vblank irqs */
  7873. if (intel_crtc == NULL)
  7874. return;
  7875. /*
  7876. * This is called both by irq handlers and the reset code (to complete
  7877. * lost pageflips) so needs the full irqsave spinlocks.
  7878. */
  7879. spin_lock_irqsave(&dev->event_lock, flags);
  7880. work = intel_crtc->unpin_work;
  7881. /* Ensure we don't miss a work->pending update ... */
  7882. smp_rmb();
  7883. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7884. spin_unlock_irqrestore(&dev->event_lock, flags);
  7885. return;
  7886. }
  7887. page_flip_completed(intel_crtc);
  7888. spin_unlock_irqrestore(&dev->event_lock, flags);
  7889. }
  7890. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7891. {
  7892. struct drm_i915_private *dev_priv = dev->dev_private;
  7893. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7894. do_intel_finish_page_flip(dev, crtc);
  7895. }
  7896. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7897. {
  7898. struct drm_i915_private *dev_priv = dev->dev_private;
  7899. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7900. do_intel_finish_page_flip(dev, crtc);
  7901. }
  7902. /* Is 'a' after or equal to 'b'? */
  7903. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7904. {
  7905. return !((a - b) & 0x80000000);
  7906. }
  7907. static bool page_flip_finished(struct intel_crtc *crtc)
  7908. {
  7909. struct drm_device *dev = crtc->base.dev;
  7910. struct drm_i915_private *dev_priv = dev->dev_private;
  7911. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7912. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7913. return true;
  7914. /*
  7915. * The relevant registers doen't exist on pre-ctg.
  7916. * As the flip done interrupt doesn't trigger for mmio
  7917. * flips on gmch platforms, a flip count check isn't
  7918. * really needed there. But since ctg has the registers,
  7919. * include it in the check anyway.
  7920. */
  7921. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7922. return true;
  7923. /*
  7924. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7925. * used the same base address. In that case the mmio flip might
  7926. * have completed, but the CS hasn't even executed the flip yet.
  7927. *
  7928. * A flip count check isn't enough as the CS might have updated
  7929. * the base address just after start of vblank, but before we
  7930. * managed to process the interrupt. This means we'd complete the
  7931. * CS flip too soon.
  7932. *
  7933. * Combining both checks should get us a good enough result. It may
  7934. * still happen that the CS flip has been executed, but has not
  7935. * yet actually completed. But in case the base address is the same
  7936. * anyway, we don't really care.
  7937. */
  7938. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7939. crtc->unpin_work->gtt_offset &&
  7940. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7941. crtc->unpin_work->flip_count);
  7942. }
  7943. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7944. {
  7945. struct drm_i915_private *dev_priv = dev->dev_private;
  7946. struct intel_crtc *intel_crtc =
  7947. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7948. unsigned long flags;
  7949. /*
  7950. * This is called both by irq handlers and the reset code (to complete
  7951. * lost pageflips) so needs the full irqsave spinlocks.
  7952. *
  7953. * NB: An MMIO update of the plane base pointer will also
  7954. * generate a page-flip completion irq, i.e. every modeset
  7955. * is also accompanied by a spurious intel_prepare_page_flip().
  7956. */
  7957. spin_lock_irqsave(&dev->event_lock, flags);
  7958. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7959. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7960. spin_unlock_irqrestore(&dev->event_lock, flags);
  7961. }
  7962. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7963. {
  7964. /* Ensure that the work item is consistent when activating it ... */
  7965. smp_wmb();
  7966. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7967. /* and that it is marked active as soon as the irq could fire. */
  7968. smp_wmb();
  7969. }
  7970. static int intel_gen2_queue_flip(struct drm_device *dev,
  7971. struct drm_crtc *crtc,
  7972. struct drm_framebuffer *fb,
  7973. struct drm_i915_gem_object *obj,
  7974. struct intel_engine_cs *ring,
  7975. uint32_t flags)
  7976. {
  7977. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7978. u32 flip_mask;
  7979. int ret;
  7980. ret = intel_ring_begin(ring, 6);
  7981. if (ret)
  7982. return ret;
  7983. /* Can't queue multiple flips, so wait for the previous
  7984. * one to finish before executing the next.
  7985. */
  7986. if (intel_crtc->plane)
  7987. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7988. else
  7989. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7990. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7991. intel_ring_emit(ring, MI_NOOP);
  7992. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7993. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7994. intel_ring_emit(ring, fb->pitches[0]);
  7995. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7996. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7997. intel_mark_page_flip_active(intel_crtc);
  7998. __intel_ring_advance(ring);
  7999. return 0;
  8000. }
  8001. static int intel_gen3_queue_flip(struct drm_device *dev,
  8002. struct drm_crtc *crtc,
  8003. struct drm_framebuffer *fb,
  8004. struct drm_i915_gem_object *obj,
  8005. struct intel_engine_cs *ring,
  8006. uint32_t flags)
  8007. {
  8008. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8009. u32 flip_mask;
  8010. int ret;
  8011. ret = intel_ring_begin(ring, 6);
  8012. if (ret)
  8013. return ret;
  8014. if (intel_crtc->plane)
  8015. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8016. else
  8017. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8018. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  8019. intel_ring_emit(ring, MI_NOOP);
  8020. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  8021. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8022. intel_ring_emit(ring, fb->pitches[0]);
  8023. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8024. intel_ring_emit(ring, MI_NOOP);
  8025. intel_mark_page_flip_active(intel_crtc);
  8026. __intel_ring_advance(ring);
  8027. return 0;
  8028. }
  8029. static int intel_gen4_queue_flip(struct drm_device *dev,
  8030. struct drm_crtc *crtc,
  8031. struct drm_framebuffer *fb,
  8032. struct drm_i915_gem_object *obj,
  8033. struct intel_engine_cs *ring,
  8034. uint32_t flags)
  8035. {
  8036. struct drm_i915_private *dev_priv = dev->dev_private;
  8037. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8038. uint32_t pf, pipesrc;
  8039. int ret;
  8040. ret = intel_ring_begin(ring, 4);
  8041. if (ret)
  8042. return ret;
  8043. /* i965+ uses the linear or tiled offsets from the
  8044. * Display Registers (which do not change across a page-flip)
  8045. * so we need only reprogram the base address.
  8046. */
  8047. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8048. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8049. intel_ring_emit(ring, fb->pitches[0]);
  8050. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  8051. obj->tiling_mode);
  8052. /* XXX Enabling the panel-fitter across page-flip is so far
  8053. * untested on non-native modes, so ignore it for now.
  8054. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8055. */
  8056. pf = 0;
  8057. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8058. intel_ring_emit(ring, pf | pipesrc);
  8059. intel_mark_page_flip_active(intel_crtc);
  8060. __intel_ring_advance(ring);
  8061. return 0;
  8062. }
  8063. static int intel_gen6_queue_flip(struct drm_device *dev,
  8064. struct drm_crtc *crtc,
  8065. struct drm_framebuffer *fb,
  8066. struct drm_i915_gem_object *obj,
  8067. struct intel_engine_cs *ring,
  8068. uint32_t flags)
  8069. {
  8070. struct drm_i915_private *dev_priv = dev->dev_private;
  8071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8072. uint32_t pf, pipesrc;
  8073. int ret;
  8074. ret = intel_ring_begin(ring, 4);
  8075. if (ret)
  8076. return ret;
  8077. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  8078. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  8079. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  8080. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8081. /* Contrary to the suggestions in the documentation,
  8082. * "Enable Panel Fitter" does not seem to be required when page
  8083. * flipping with a non-native mode, and worse causes a normal
  8084. * modeset to fail.
  8085. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8086. */
  8087. pf = 0;
  8088. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8089. intel_ring_emit(ring, pf | pipesrc);
  8090. intel_mark_page_flip_active(intel_crtc);
  8091. __intel_ring_advance(ring);
  8092. return 0;
  8093. }
  8094. static int intel_gen7_queue_flip(struct drm_device *dev,
  8095. struct drm_crtc *crtc,
  8096. struct drm_framebuffer *fb,
  8097. struct drm_i915_gem_object *obj,
  8098. struct intel_engine_cs *ring,
  8099. uint32_t flags)
  8100. {
  8101. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8102. uint32_t plane_bit = 0;
  8103. int len, ret;
  8104. switch (intel_crtc->plane) {
  8105. case PLANE_A:
  8106. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8107. break;
  8108. case PLANE_B:
  8109. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8110. break;
  8111. case PLANE_C:
  8112. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8113. break;
  8114. default:
  8115. WARN_ONCE(1, "unknown plane in flip command\n");
  8116. return -ENODEV;
  8117. }
  8118. len = 4;
  8119. if (ring->id == RCS) {
  8120. len += 6;
  8121. /*
  8122. * On Gen 8, SRM is now taking an extra dword to accommodate
  8123. * 48bits addresses, and we need a NOOP for the batch size to
  8124. * stay even.
  8125. */
  8126. if (IS_GEN8(dev))
  8127. len += 2;
  8128. }
  8129. /*
  8130. * BSpec MI_DISPLAY_FLIP for IVB:
  8131. * "The full packet must be contained within the same cache line."
  8132. *
  8133. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8134. * cacheline, if we ever start emitting more commands before
  8135. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8136. * then do the cacheline alignment, and finally emit the
  8137. * MI_DISPLAY_FLIP.
  8138. */
  8139. ret = intel_ring_cacheline_align(ring);
  8140. if (ret)
  8141. return ret;
  8142. ret = intel_ring_begin(ring, len);
  8143. if (ret)
  8144. return ret;
  8145. /* Unmask the flip-done completion message. Note that the bspec says that
  8146. * we should do this for both the BCS and RCS, and that we must not unmask
  8147. * more than one flip event at any time (or ensure that one flip message
  8148. * can be sent by waiting for flip-done prior to queueing new flips).
  8149. * Experimentation says that BCS works despite DERRMR masking all
  8150. * flip-done completion events and that unmasking all planes at once
  8151. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8152. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8153. */
  8154. if (ring->id == RCS) {
  8155. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8156. intel_ring_emit(ring, DERRMR);
  8157. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8158. DERRMR_PIPEB_PRI_FLIP_DONE |
  8159. DERRMR_PIPEC_PRI_FLIP_DONE));
  8160. if (IS_GEN8(dev))
  8161. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8162. MI_SRM_LRM_GLOBAL_GTT);
  8163. else
  8164. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8165. MI_SRM_LRM_GLOBAL_GTT);
  8166. intel_ring_emit(ring, DERRMR);
  8167. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8168. if (IS_GEN8(dev)) {
  8169. intel_ring_emit(ring, 0);
  8170. intel_ring_emit(ring, MI_NOOP);
  8171. }
  8172. }
  8173. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8174. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8175. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8176. intel_ring_emit(ring, (MI_NOOP));
  8177. intel_mark_page_flip_active(intel_crtc);
  8178. __intel_ring_advance(ring);
  8179. return 0;
  8180. }
  8181. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8182. struct drm_i915_gem_object *obj)
  8183. {
  8184. /*
  8185. * This is not being used for older platforms, because
  8186. * non-availability of flip done interrupt forces us to use
  8187. * CS flips. Older platforms derive flip done using some clever
  8188. * tricks involving the flip_pending status bits and vblank irqs.
  8189. * So using MMIO flips there would disrupt this mechanism.
  8190. */
  8191. if (ring == NULL)
  8192. return true;
  8193. if (INTEL_INFO(ring->dev)->gen < 5)
  8194. return false;
  8195. if (i915.use_mmio_flip < 0)
  8196. return false;
  8197. else if (i915.use_mmio_flip > 0)
  8198. return true;
  8199. else if (i915.enable_execlists)
  8200. return true;
  8201. else
  8202. return ring != i915_gem_request_get_ring(obj->last_read_req);
  8203. }
  8204. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  8205. {
  8206. struct drm_device *dev = intel_crtc->base.dev;
  8207. struct drm_i915_private *dev_priv = dev->dev_private;
  8208. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8209. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  8210. struct drm_i915_gem_object *obj = intel_fb->obj;
  8211. const enum pipe pipe = intel_crtc->pipe;
  8212. u32 ctl, stride;
  8213. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8214. ctl &= ~PLANE_CTL_TILED_MASK;
  8215. if (obj->tiling_mode == I915_TILING_X)
  8216. ctl |= PLANE_CTL_TILED_X;
  8217. /*
  8218. * The stride is either expressed as a multiple of 64 bytes chunks for
  8219. * linear buffers or in number of tiles for tiled buffers.
  8220. */
  8221. stride = fb->pitches[0] >> 6;
  8222. if (obj->tiling_mode == I915_TILING_X)
  8223. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  8224. /*
  8225. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8226. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8227. */
  8228. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8229. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8230. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  8231. POSTING_READ(PLANE_SURF(pipe, 0));
  8232. }
  8233. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  8234. {
  8235. struct drm_device *dev = intel_crtc->base.dev;
  8236. struct drm_i915_private *dev_priv = dev->dev_private;
  8237. struct intel_framebuffer *intel_fb =
  8238. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8239. struct drm_i915_gem_object *obj = intel_fb->obj;
  8240. u32 dspcntr;
  8241. u32 reg;
  8242. reg = DSPCNTR(intel_crtc->plane);
  8243. dspcntr = I915_READ(reg);
  8244. if (obj->tiling_mode != I915_TILING_NONE)
  8245. dspcntr |= DISPPLANE_TILED;
  8246. else
  8247. dspcntr &= ~DISPPLANE_TILED;
  8248. I915_WRITE(reg, dspcntr);
  8249. I915_WRITE(DSPSURF(intel_crtc->plane),
  8250. intel_crtc->unpin_work->gtt_offset);
  8251. POSTING_READ(DSPSURF(intel_crtc->plane));
  8252. }
  8253. /*
  8254. * XXX: This is the temporary way to update the plane registers until we get
  8255. * around to using the usual plane update functions for MMIO flips
  8256. */
  8257. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  8258. {
  8259. struct drm_device *dev = intel_crtc->base.dev;
  8260. bool atomic_update;
  8261. u32 start_vbl_count;
  8262. intel_mark_page_flip_active(intel_crtc);
  8263. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  8264. if (INTEL_INFO(dev)->gen >= 9)
  8265. skl_do_mmio_flip(intel_crtc);
  8266. else
  8267. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8268. ilk_do_mmio_flip(intel_crtc);
  8269. if (atomic_update)
  8270. intel_pipe_update_end(intel_crtc, start_vbl_count);
  8271. }
  8272. static void intel_mmio_flip_work_func(struct work_struct *work)
  8273. {
  8274. struct intel_crtc *crtc =
  8275. container_of(work, struct intel_crtc, mmio_flip.work);
  8276. struct intel_mmio_flip *mmio_flip;
  8277. mmio_flip = &crtc->mmio_flip;
  8278. if (mmio_flip->req)
  8279. WARN_ON(__i915_wait_request(mmio_flip->req,
  8280. crtc->reset_counter,
  8281. false, NULL, NULL) != 0);
  8282. intel_do_mmio_flip(crtc);
  8283. if (mmio_flip->req) {
  8284. mutex_lock(&crtc->base.dev->struct_mutex);
  8285. i915_gem_request_assign(&mmio_flip->req, NULL);
  8286. mutex_unlock(&crtc->base.dev->struct_mutex);
  8287. }
  8288. }
  8289. static int intel_queue_mmio_flip(struct drm_device *dev,
  8290. struct drm_crtc *crtc,
  8291. struct drm_framebuffer *fb,
  8292. struct drm_i915_gem_object *obj,
  8293. struct intel_engine_cs *ring,
  8294. uint32_t flags)
  8295. {
  8296. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8297. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  8298. obj->last_write_req);
  8299. schedule_work(&intel_crtc->mmio_flip.work);
  8300. return 0;
  8301. }
  8302. static int intel_default_queue_flip(struct drm_device *dev,
  8303. struct drm_crtc *crtc,
  8304. struct drm_framebuffer *fb,
  8305. struct drm_i915_gem_object *obj,
  8306. struct intel_engine_cs *ring,
  8307. uint32_t flags)
  8308. {
  8309. return -ENODEV;
  8310. }
  8311. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8312. struct drm_crtc *crtc)
  8313. {
  8314. struct drm_i915_private *dev_priv = dev->dev_private;
  8315. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8316. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8317. u32 addr;
  8318. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8319. return true;
  8320. if (!work->enable_stall_check)
  8321. return false;
  8322. if (work->flip_ready_vblank == 0) {
  8323. if (work->flip_queued_req &&
  8324. !i915_gem_request_completed(work->flip_queued_req, true))
  8325. return false;
  8326. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  8327. }
  8328. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  8329. return false;
  8330. /* Potential stall - if we see that the flip has happened,
  8331. * assume a missed interrupt. */
  8332. if (INTEL_INFO(dev)->gen >= 4)
  8333. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8334. else
  8335. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8336. /* There is a potential issue here with a false positive after a flip
  8337. * to the same address. We could address this by checking for a
  8338. * non-incrementing frame counter.
  8339. */
  8340. return addr == work->gtt_offset;
  8341. }
  8342. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8343. {
  8344. struct drm_i915_private *dev_priv = dev->dev_private;
  8345. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8346. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8347. WARN_ON(!in_interrupt());
  8348. if (crtc == NULL)
  8349. return;
  8350. spin_lock(&dev->event_lock);
  8351. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8352. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8353. intel_crtc->unpin_work->flip_queued_vblank,
  8354. drm_vblank_count(dev, pipe));
  8355. page_flip_completed(intel_crtc);
  8356. }
  8357. spin_unlock(&dev->event_lock);
  8358. }
  8359. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8360. struct drm_framebuffer *fb,
  8361. struct drm_pending_vblank_event *event,
  8362. uint32_t page_flip_flags)
  8363. {
  8364. struct drm_device *dev = crtc->dev;
  8365. struct drm_i915_private *dev_priv = dev->dev_private;
  8366. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8367. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8368. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8369. struct drm_plane *primary = crtc->primary;
  8370. enum pipe pipe = intel_crtc->pipe;
  8371. struct intel_unpin_work *work;
  8372. struct intel_engine_cs *ring;
  8373. int ret;
  8374. /*
  8375. * drm_mode_page_flip_ioctl() should already catch this, but double
  8376. * check to be safe. In the future we may enable pageflipping from
  8377. * a disabled primary plane.
  8378. */
  8379. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8380. return -EBUSY;
  8381. /* Can't change pixel format via MI display flips. */
  8382. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8383. return -EINVAL;
  8384. /*
  8385. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8386. * Note that pitch changes could also affect these register.
  8387. */
  8388. if (INTEL_INFO(dev)->gen > 3 &&
  8389. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8390. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8391. return -EINVAL;
  8392. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8393. goto out_hang;
  8394. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8395. if (work == NULL)
  8396. return -ENOMEM;
  8397. work->event = event;
  8398. work->crtc = crtc;
  8399. work->old_fb = old_fb;
  8400. INIT_WORK(&work->work, intel_unpin_work_fn);
  8401. ret = drm_crtc_vblank_get(crtc);
  8402. if (ret)
  8403. goto free_work;
  8404. /* We borrow the event spin lock for protecting unpin_work */
  8405. spin_lock_irq(&dev->event_lock);
  8406. if (intel_crtc->unpin_work) {
  8407. /* Before declaring the flip queue wedged, check if
  8408. * the hardware completed the operation behind our backs.
  8409. */
  8410. if (__intel_pageflip_stall_check(dev, crtc)) {
  8411. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8412. page_flip_completed(intel_crtc);
  8413. } else {
  8414. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8415. spin_unlock_irq(&dev->event_lock);
  8416. drm_crtc_vblank_put(crtc);
  8417. kfree(work);
  8418. return -EBUSY;
  8419. }
  8420. }
  8421. intel_crtc->unpin_work = work;
  8422. spin_unlock_irq(&dev->event_lock);
  8423. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8424. flush_workqueue(dev_priv->wq);
  8425. /* Reference the objects for the scheduled work. */
  8426. drm_framebuffer_reference(work->old_fb);
  8427. drm_gem_object_reference(&obj->base);
  8428. crtc->primary->fb = fb;
  8429. update_state_fb(crtc->primary);
  8430. work->pending_flip_obj = obj;
  8431. ret = i915_mutex_lock_interruptible(dev);
  8432. if (ret)
  8433. goto cleanup;
  8434. atomic_inc(&intel_crtc->unpin_work_count);
  8435. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8436. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8437. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8438. if (IS_VALLEYVIEW(dev)) {
  8439. ring = &dev_priv->ring[BCS];
  8440. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  8441. /* vlv: DISPLAY_FLIP fails to change tiling */
  8442. ring = NULL;
  8443. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  8444. ring = &dev_priv->ring[BCS];
  8445. } else if (INTEL_INFO(dev)->gen >= 7) {
  8446. ring = i915_gem_request_get_ring(obj->last_read_req);
  8447. if (ring == NULL || ring->id != RCS)
  8448. ring = &dev_priv->ring[BCS];
  8449. } else {
  8450. ring = &dev_priv->ring[RCS];
  8451. }
  8452. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  8453. crtc->primary->state, ring);
  8454. if (ret)
  8455. goto cleanup_pending;
  8456. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  8457. + intel_crtc->dspaddr_offset;
  8458. if (use_mmio_flip(ring, obj)) {
  8459. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8460. page_flip_flags);
  8461. if (ret)
  8462. goto cleanup_unpin;
  8463. i915_gem_request_assign(&work->flip_queued_req,
  8464. obj->last_write_req);
  8465. } else {
  8466. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8467. page_flip_flags);
  8468. if (ret)
  8469. goto cleanup_unpin;
  8470. i915_gem_request_assign(&work->flip_queued_req,
  8471. intel_ring_get_request(ring));
  8472. }
  8473. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  8474. work->enable_stall_check = true;
  8475. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  8476. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8477. intel_fbc_disable(dev);
  8478. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8479. mutex_unlock(&dev->struct_mutex);
  8480. trace_i915_flip_request(intel_crtc->plane, obj);
  8481. return 0;
  8482. cleanup_unpin:
  8483. intel_unpin_fb_obj(fb, crtc->primary->state);
  8484. cleanup_pending:
  8485. atomic_dec(&intel_crtc->unpin_work_count);
  8486. mutex_unlock(&dev->struct_mutex);
  8487. cleanup:
  8488. crtc->primary->fb = old_fb;
  8489. update_state_fb(crtc->primary);
  8490. drm_gem_object_unreference_unlocked(&obj->base);
  8491. drm_framebuffer_unreference(work->old_fb);
  8492. spin_lock_irq(&dev->event_lock);
  8493. intel_crtc->unpin_work = NULL;
  8494. spin_unlock_irq(&dev->event_lock);
  8495. drm_crtc_vblank_put(crtc);
  8496. free_work:
  8497. kfree(work);
  8498. if (ret == -EIO) {
  8499. out_hang:
  8500. ret = intel_plane_restore(primary);
  8501. if (ret == 0 && event) {
  8502. spin_lock_irq(&dev->event_lock);
  8503. drm_send_vblank_event(dev, pipe, event);
  8504. spin_unlock_irq(&dev->event_lock);
  8505. }
  8506. }
  8507. return ret;
  8508. }
  8509. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8510. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8511. .load_lut = intel_crtc_load_lut,
  8512. .atomic_begin = intel_begin_crtc_commit,
  8513. .atomic_flush = intel_finish_crtc_commit,
  8514. };
  8515. /**
  8516. * intel_modeset_update_staged_output_state
  8517. *
  8518. * Updates the staged output configuration state, e.g. after we've read out the
  8519. * current hw state.
  8520. */
  8521. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8522. {
  8523. struct intel_crtc *crtc;
  8524. struct intel_encoder *encoder;
  8525. struct intel_connector *connector;
  8526. for_each_intel_connector(dev, connector) {
  8527. connector->new_encoder =
  8528. to_intel_encoder(connector->base.encoder);
  8529. }
  8530. for_each_intel_encoder(dev, encoder) {
  8531. encoder->new_crtc =
  8532. to_intel_crtc(encoder->base.crtc);
  8533. }
  8534. for_each_intel_crtc(dev, crtc) {
  8535. crtc->new_enabled = crtc->base.state->enable;
  8536. if (crtc->new_enabled)
  8537. crtc->new_config = crtc->config;
  8538. else
  8539. crtc->new_config = NULL;
  8540. }
  8541. }
  8542. /**
  8543. * intel_modeset_commit_output_state
  8544. *
  8545. * This function copies the stage display pipe configuration to the real one.
  8546. */
  8547. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8548. {
  8549. struct intel_crtc *crtc;
  8550. struct intel_encoder *encoder;
  8551. struct intel_connector *connector;
  8552. for_each_intel_connector(dev, connector) {
  8553. connector->base.encoder = &connector->new_encoder->base;
  8554. }
  8555. for_each_intel_encoder(dev, encoder) {
  8556. encoder->base.crtc = &encoder->new_crtc->base;
  8557. }
  8558. for_each_intel_crtc(dev, crtc) {
  8559. crtc->base.state->enable = crtc->new_enabled;
  8560. crtc->base.enabled = crtc->new_enabled;
  8561. }
  8562. }
  8563. static void
  8564. connected_sink_compute_bpp(struct intel_connector *connector,
  8565. struct intel_crtc_state *pipe_config)
  8566. {
  8567. int bpp = pipe_config->pipe_bpp;
  8568. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8569. connector->base.base.id,
  8570. connector->base.name);
  8571. /* Don't use an invalid EDID bpc value */
  8572. if (connector->base.display_info.bpc &&
  8573. connector->base.display_info.bpc * 3 < bpp) {
  8574. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8575. bpp, connector->base.display_info.bpc*3);
  8576. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8577. }
  8578. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8579. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8580. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8581. bpp);
  8582. pipe_config->pipe_bpp = 24;
  8583. }
  8584. }
  8585. static int
  8586. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8587. struct drm_framebuffer *fb,
  8588. struct intel_crtc_state *pipe_config)
  8589. {
  8590. struct drm_device *dev = crtc->base.dev;
  8591. struct intel_connector *connector;
  8592. int bpp;
  8593. switch (fb->pixel_format) {
  8594. case DRM_FORMAT_C8:
  8595. bpp = 8*3; /* since we go through a colormap */
  8596. break;
  8597. case DRM_FORMAT_XRGB1555:
  8598. case DRM_FORMAT_ARGB1555:
  8599. /* checked in intel_framebuffer_init already */
  8600. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8601. return -EINVAL;
  8602. case DRM_FORMAT_RGB565:
  8603. bpp = 6*3; /* min is 18bpp */
  8604. break;
  8605. case DRM_FORMAT_XBGR8888:
  8606. case DRM_FORMAT_ABGR8888:
  8607. /* checked in intel_framebuffer_init already */
  8608. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8609. return -EINVAL;
  8610. case DRM_FORMAT_XRGB8888:
  8611. case DRM_FORMAT_ARGB8888:
  8612. bpp = 8*3;
  8613. break;
  8614. case DRM_FORMAT_XRGB2101010:
  8615. case DRM_FORMAT_ARGB2101010:
  8616. case DRM_FORMAT_XBGR2101010:
  8617. case DRM_FORMAT_ABGR2101010:
  8618. /* checked in intel_framebuffer_init already */
  8619. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8620. return -EINVAL;
  8621. bpp = 10*3;
  8622. break;
  8623. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8624. default:
  8625. DRM_DEBUG_KMS("unsupported depth\n");
  8626. return -EINVAL;
  8627. }
  8628. pipe_config->pipe_bpp = bpp;
  8629. /* Clamp display bpp to EDID value */
  8630. for_each_intel_connector(dev, connector) {
  8631. if (!connector->new_encoder ||
  8632. connector->new_encoder->new_crtc != crtc)
  8633. continue;
  8634. connected_sink_compute_bpp(connector, pipe_config);
  8635. }
  8636. return bpp;
  8637. }
  8638. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8639. {
  8640. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8641. "type: 0x%x flags: 0x%x\n",
  8642. mode->crtc_clock,
  8643. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8644. mode->crtc_hsync_end, mode->crtc_htotal,
  8645. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8646. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8647. }
  8648. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8649. struct intel_crtc_state *pipe_config,
  8650. const char *context)
  8651. {
  8652. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8653. context, pipe_name(crtc->pipe));
  8654. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8655. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8656. pipe_config->pipe_bpp, pipe_config->dither);
  8657. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8658. pipe_config->has_pch_encoder,
  8659. pipe_config->fdi_lanes,
  8660. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8661. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8662. pipe_config->fdi_m_n.tu);
  8663. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8664. pipe_config->has_dp_encoder,
  8665. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8666. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8667. pipe_config->dp_m_n.tu);
  8668. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8669. pipe_config->has_dp_encoder,
  8670. pipe_config->dp_m2_n2.gmch_m,
  8671. pipe_config->dp_m2_n2.gmch_n,
  8672. pipe_config->dp_m2_n2.link_m,
  8673. pipe_config->dp_m2_n2.link_n,
  8674. pipe_config->dp_m2_n2.tu);
  8675. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8676. pipe_config->has_audio,
  8677. pipe_config->has_infoframe);
  8678. DRM_DEBUG_KMS("requested mode:\n");
  8679. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8680. DRM_DEBUG_KMS("adjusted mode:\n");
  8681. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8682. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8683. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8684. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8685. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8686. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8687. pipe_config->gmch_pfit.control,
  8688. pipe_config->gmch_pfit.pgm_ratios,
  8689. pipe_config->gmch_pfit.lvds_border_bits);
  8690. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8691. pipe_config->pch_pfit.pos,
  8692. pipe_config->pch_pfit.size,
  8693. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8694. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8695. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8696. }
  8697. static bool encoders_cloneable(const struct intel_encoder *a,
  8698. const struct intel_encoder *b)
  8699. {
  8700. /* masks could be asymmetric, so check both ways */
  8701. return a == b || (a->cloneable & (1 << b->type) &&
  8702. b->cloneable & (1 << a->type));
  8703. }
  8704. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8705. struct intel_encoder *encoder)
  8706. {
  8707. struct drm_device *dev = crtc->base.dev;
  8708. struct intel_encoder *source_encoder;
  8709. for_each_intel_encoder(dev, source_encoder) {
  8710. if (source_encoder->new_crtc != crtc)
  8711. continue;
  8712. if (!encoders_cloneable(encoder, source_encoder))
  8713. return false;
  8714. }
  8715. return true;
  8716. }
  8717. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8718. {
  8719. struct drm_device *dev = crtc->base.dev;
  8720. struct intel_encoder *encoder;
  8721. for_each_intel_encoder(dev, encoder) {
  8722. if (encoder->new_crtc != crtc)
  8723. continue;
  8724. if (!check_single_encoder_cloning(crtc, encoder))
  8725. return false;
  8726. }
  8727. return true;
  8728. }
  8729. static bool check_digital_port_conflicts(struct drm_device *dev)
  8730. {
  8731. struct intel_connector *connector;
  8732. unsigned int used_ports = 0;
  8733. /*
  8734. * Walk the connector list instead of the encoder
  8735. * list to detect the problem on ddi platforms
  8736. * where there's just one encoder per digital port.
  8737. */
  8738. for_each_intel_connector(dev, connector) {
  8739. struct intel_encoder *encoder = connector->new_encoder;
  8740. if (!encoder)
  8741. continue;
  8742. WARN_ON(!encoder->new_crtc);
  8743. switch (encoder->type) {
  8744. unsigned int port_mask;
  8745. case INTEL_OUTPUT_UNKNOWN:
  8746. if (WARN_ON(!HAS_DDI(dev)))
  8747. break;
  8748. case INTEL_OUTPUT_DISPLAYPORT:
  8749. case INTEL_OUTPUT_HDMI:
  8750. case INTEL_OUTPUT_EDP:
  8751. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8752. /* the same port mustn't appear more than once */
  8753. if (used_ports & port_mask)
  8754. return false;
  8755. used_ports |= port_mask;
  8756. default:
  8757. break;
  8758. }
  8759. }
  8760. return true;
  8761. }
  8762. static struct intel_crtc_state *
  8763. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8764. struct drm_framebuffer *fb,
  8765. struct drm_display_mode *mode)
  8766. {
  8767. struct drm_device *dev = crtc->dev;
  8768. struct intel_encoder *encoder;
  8769. struct intel_crtc_state *pipe_config;
  8770. int plane_bpp, ret = -EINVAL;
  8771. bool retry = true;
  8772. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8773. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8774. return ERR_PTR(-EINVAL);
  8775. }
  8776. if (!check_digital_port_conflicts(dev)) {
  8777. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8778. return ERR_PTR(-EINVAL);
  8779. }
  8780. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8781. if (!pipe_config)
  8782. return ERR_PTR(-ENOMEM);
  8783. pipe_config->base.crtc = crtc;
  8784. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  8785. drm_mode_copy(&pipe_config->base.mode, mode);
  8786. pipe_config->cpu_transcoder =
  8787. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8788. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8789. /*
  8790. * Sanitize sync polarity flags based on requested ones. If neither
  8791. * positive or negative polarity is requested, treat this as meaning
  8792. * negative polarity.
  8793. */
  8794. if (!(pipe_config->base.adjusted_mode.flags &
  8795. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8796. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8797. if (!(pipe_config->base.adjusted_mode.flags &
  8798. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8799. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8800. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8801. * plane pixel format and any sink constraints into account. Returns the
  8802. * source plane bpp so that dithering can be selected on mismatches
  8803. * after encoders and crtc also have had their say. */
  8804. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8805. fb, pipe_config);
  8806. if (plane_bpp < 0)
  8807. goto fail;
  8808. /*
  8809. * Determine the real pipe dimensions. Note that stereo modes can
  8810. * increase the actual pipe size due to the frame doubling and
  8811. * insertion of additional space for blanks between the frame. This
  8812. * is stored in the crtc timings. We use the requested mode to do this
  8813. * computation to clearly distinguish it from the adjusted mode, which
  8814. * can be changed by the connectors in the below retry loop.
  8815. */
  8816. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  8817. &pipe_config->pipe_src_w,
  8818. &pipe_config->pipe_src_h);
  8819. encoder_retry:
  8820. /* Ensure the port clock defaults are reset when retrying. */
  8821. pipe_config->port_clock = 0;
  8822. pipe_config->pixel_multiplier = 1;
  8823. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8824. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  8825. CRTC_STEREO_DOUBLE);
  8826. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8827. * adjust it according to limitations or connector properties, and also
  8828. * a chance to reject the mode entirely.
  8829. */
  8830. for_each_intel_encoder(dev, encoder) {
  8831. if (&encoder->new_crtc->base != crtc)
  8832. continue;
  8833. if (!(encoder->compute_config(encoder, pipe_config))) {
  8834. DRM_DEBUG_KMS("Encoder config failure\n");
  8835. goto fail;
  8836. }
  8837. }
  8838. /* Set default port clock if not overwritten by the encoder. Needs to be
  8839. * done afterwards in case the encoder adjusts the mode. */
  8840. if (!pipe_config->port_clock)
  8841. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  8842. * pipe_config->pixel_multiplier;
  8843. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8844. if (ret < 0) {
  8845. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8846. goto fail;
  8847. }
  8848. if (ret == RETRY) {
  8849. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8850. ret = -EINVAL;
  8851. goto fail;
  8852. }
  8853. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8854. retry = false;
  8855. goto encoder_retry;
  8856. }
  8857. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8858. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8859. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8860. return pipe_config;
  8861. fail:
  8862. kfree(pipe_config);
  8863. return ERR_PTR(ret);
  8864. }
  8865. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8866. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8867. static void
  8868. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8869. unsigned *prepare_pipes, unsigned *disable_pipes)
  8870. {
  8871. struct intel_crtc *intel_crtc;
  8872. struct drm_device *dev = crtc->dev;
  8873. struct intel_encoder *encoder;
  8874. struct intel_connector *connector;
  8875. struct drm_crtc *tmp_crtc;
  8876. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8877. /* Check which crtcs have changed outputs connected to them, these need
  8878. * to be part of the prepare_pipes mask. We don't (yet) support global
  8879. * modeset across multiple crtcs, so modeset_pipes will only have one
  8880. * bit set at most. */
  8881. for_each_intel_connector(dev, connector) {
  8882. if (connector->base.encoder == &connector->new_encoder->base)
  8883. continue;
  8884. if (connector->base.encoder) {
  8885. tmp_crtc = connector->base.encoder->crtc;
  8886. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8887. }
  8888. if (connector->new_encoder)
  8889. *prepare_pipes |=
  8890. 1 << connector->new_encoder->new_crtc->pipe;
  8891. }
  8892. for_each_intel_encoder(dev, encoder) {
  8893. if (encoder->base.crtc == &encoder->new_crtc->base)
  8894. continue;
  8895. if (encoder->base.crtc) {
  8896. tmp_crtc = encoder->base.crtc;
  8897. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8898. }
  8899. if (encoder->new_crtc)
  8900. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8901. }
  8902. /* Check for pipes that will be enabled/disabled ... */
  8903. for_each_intel_crtc(dev, intel_crtc) {
  8904. if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
  8905. continue;
  8906. if (!intel_crtc->new_enabled)
  8907. *disable_pipes |= 1 << intel_crtc->pipe;
  8908. else
  8909. *prepare_pipes |= 1 << intel_crtc->pipe;
  8910. }
  8911. /* set_mode is also used to update properties on life display pipes. */
  8912. intel_crtc = to_intel_crtc(crtc);
  8913. if (intel_crtc->new_enabled)
  8914. *prepare_pipes |= 1 << intel_crtc->pipe;
  8915. /*
  8916. * For simplicity do a full modeset on any pipe where the output routing
  8917. * changed. We could be more clever, but that would require us to be
  8918. * more careful with calling the relevant encoder->mode_set functions.
  8919. */
  8920. if (*prepare_pipes)
  8921. *modeset_pipes = *prepare_pipes;
  8922. /* ... and mask these out. */
  8923. *modeset_pipes &= ~(*disable_pipes);
  8924. *prepare_pipes &= ~(*disable_pipes);
  8925. /*
  8926. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8927. * obies this rule, but the modeset restore mode of
  8928. * intel_modeset_setup_hw_state does not.
  8929. */
  8930. *modeset_pipes &= 1 << intel_crtc->pipe;
  8931. *prepare_pipes &= 1 << intel_crtc->pipe;
  8932. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8933. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8934. }
  8935. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8936. {
  8937. struct drm_encoder *encoder;
  8938. struct drm_device *dev = crtc->dev;
  8939. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8940. if (encoder->crtc == crtc)
  8941. return true;
  8942. return false;
  8943. }
  8944. static void
  8945. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8946. {
  8947. struct drm_i915_private *dev_priv = dev->dev_private;
  8948. struct intel_encoder *intel_encoder;
  8949. struct intel_crtc *intel_crtc;
  8950. struct drm_connector *connector;
  8951. intel_shared_dpll_commit(dev_priv);
  8952. for_each_intel_encoder(dev, intel_encoder) {
  8953. if (!intel_encoder->base.crtc)
  8954. continue;
  8955. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8956. if (prepare_pipes & (1 << intel_crtc->pipe))
  8957. intel_encoder->connectors_active = false;
  8958. }
  8959. intel_modeset_commit_output_state(dev);
  8960. /* Double check state. */
  8961. for_each_intel_crtc(dev, intel_crtc) {
  8962. WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
  8963. WARN_ON(intel_crtc->new_config &&
  8964. intel_crtc->new_config != intel_crtc->config);
  8965. WARN_ON(intel_crtc->base.state->enable != !!intel_crtc->new_config);
  8966. }
  8967. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8968. if (!connector->encoder || !connector->encoder->crtc)
  8969. continue;
  8970. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8971. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8972. struct drm_property *dpms_property =
  8973. dev->mode_config.dpms_property;
  8974. connector->dpms = DRM_MODE_DPMS_ON;
  8975. drm_object_property_set_value(&connector->base,
  8976. dpms_property,
  8977. DRM_MODE_DPMS_ON);
  8978. intel_encoder = to_intel_encoder(connector->encoder);
  8979. intel_encoder->connectors_active = true;
  8980. }
  8981. }
  8982. }
  8983. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8984. {
  8985. int diff;
  8986. if (clock1 == clock2)
  8987. return true;
  8988. if (!clock1 || !clock2)
  8989. return false;
  8990. diff = abs(clock1 - clock2);
  8991. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8992. return true;
  8993. return false;
  8994. }
  8995. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8996. list_for_each_entry((intel_crtc), \
  8997. &(dev)->mode_config.crtc_list, \
  8998. base.head) \
  8999. if (mask & (1 <<(intel_crtc)->pipe))
  9000. static bool
  9001. intel_pipe_config_compare(struct drm_device *dev,
  9002. struct intel_crtc_state *current_config,
  9003. struct intel_crtc_state *pipe_config)
  9004. {
  9005. #define PIPE_CONF_CHECK_X(name) \
  9006. if (current_config->name != pipe_config->name) { \
  9007. DRM_ERROR("mismatch in " #name " " \
  9008. "(expected 0x%08x, found 0x%08x)\n", \
  9009. current_config->name, \
  9010. pipe_config->name); \
  9011. return false; \
  9012. }
  9013. #define PIPE_CONF_CHECK_I(name) \
  9014. if (current_config->name != pipe_config->name) { \
  9015. DRM_ERROR("mismatch in " #name " " \
  9016. "(expected %i, found %i)\n", \
  9017. current_config->name, \
  9018. pipe_config->name); \
  9019. return false; \
  9020. }
  9021. /* This is required for BDW+ where there is only one set of registers for
  9022. * switching between high and low RR.
  9023. * This macro can be used whenever a comparison has to be made between one
  9024. * hw state and multiple sw state variables.
  9025. */
  9026. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  9027. if ((current_config->name != pipe_config->name) && \
  9028. (current_config->alt_name != pipe_config->name)) { \
  9029. DRM_ERROR("mismatch in " #name " " \
  9030. "(expected %i or %i, found %i)\n", \
  9031. current_config->name, \
  9032. current_config->alt_name, \
  9033. pipe_config->name); \
  9034. return false; \
  9035. }
  9036. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9037. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9038. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  9039. "(expected %i, found %i)\n", \
  9040. current_config->name & (mask), \
  9041. pipe_config->name & (mask)); \
  9042. return false; \
  9043. }
  9044. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9045. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9046. DRM_ERROR("mismatch in " #name " " \
  9047. "(expected %i, found %i)\n", \
  9048. current_config->name, \
  9049. pipe_config->name); \
  9050. return false; \
  9051. }
  9052. #define PIPE_CONF_QUIRK(quirk) \
  9053. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9054. PIPE_CONF_CHECK_I(cpu_transcoder);
  9055. PIPE_CONF_CHECK_I(has_pch_encoder);
  9056. PIPE_CONF_CHECK_I(fdi_lanes);
  9057. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  9058. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  9059. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  9060. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  9061. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  9062. PIPE_CONF_CHECK_I(has_dp_encoder);
  9063. if (INTEL_INFO(dev)->gen < 8) {
  9064. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  9065. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  9066. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  9067. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  9068. PIPE_CONF_CHECK_I(dp_m_n.tu);
  9069. if (current_config->has_drrs) {
  9070. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  9071. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  9072. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  9073. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  9074. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  9075. }
  9076. } else {
  9077. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  9078. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  9079. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  9080. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  9081. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  9082. }
  9083. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9084. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9085. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9086. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9087. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9088. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9089. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9090. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9091. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9092. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9093. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9094. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9095. PIPE_CONF_CHECK_I(pixel_multiplier);
  9096. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9097. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  9098. IS_VALLEYVIEW(dev))
  9099. PIPE_CONF_CHECK_I(limited_color_range);
  9100. PIPE_CONF_CHECK_I(has_infoframe);
  9101. PIPE_CONF_CHECK_I(has_audio);
  9102. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9103. DRM_MODE_FLAG_INTERLACE);
  9104. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9105. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9106. DRM_MODE_FLAG_PHSYNC);
  9107. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9108. DRM_MODE_FLAG_NHSYNC);
  9109. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9110. DRM_MODE_FLAG_PVSYNC);
  9111. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9112. DRM_MODE_FLAG_NVSYNC);
  9113. }
  9114. PIPE_CONF_CHECK_I(pipe_src_w);
  9115. PIPE_CONF_CHECK_I(pipe_src_h);
  9116. /*
  9117. * FIXME: BIOS likes to set up a cloned config with lvds+external
  9118. * screen. Since we don't yet re-compute the pipe config when moving
  9119. * just the lvds port away to another pipe the sw tracking won't match.
  9120. *
  9121. * Proper atomic modesets with recomputed global state will fix this.
  9122. * Until then just don't check gmch state for inherited modes.
  9123. */
  9124. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  9125. PIPE_CONF_CHECK_I(gmch_pfit.control);
  9126. /* pfit ratios are autocomputed by the hw on gen4+ */
  9127. if (INTEL_INFO(dev)->gen < 4)
  9128. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  9129. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  9130. }
  9131. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9132. if (current_config->pch_pfit.enabled) {
  9133. PIPE_CONF_CHECK_I(pch_pfit.pos);
  9134. PIPE_CONF_CHECK_I(pch_pfit.size);
  9135. }
  9136. /* BDW+ don't expose a synchronous way to read the state */
  9137. if (IS_HASWELL(dev))
  9138. PIPE_CONF_CHECK_I(ips_enabled);
  9139. PIPE_CONF_CHECK_I(double_wide);
  9140. PIPE_CONF_CHECK_X(ddi_pll_sel);
  9141. PIPE_CONF_CHECK_I(shared_dpll);
  9142. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9143. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9144. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9145. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9146. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9147. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9148. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9149. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9150. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  9151. PIPE_CONF_CHECK_I(pipe_bpp);
  9152. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9153. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9154. #undef PIPE_CONF_CHECK_X
  9155. #undef PIPE_CONF_CHECK_I
  9156. #undef PIPE_CONF_CHECK_I_ALT
  9157. #undef PIPE_CONF_CHECK_FLAGS
  9158. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9159. #undef PIPE_CONF_QUIRK
  9160. return true;
  9161. }
  9162. static void check_wm_state(struct drm_device *dev)
  9163. {
  9164. struct drm_i915_private *dev_priv = dev->dev_private;
  9165. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9166. struct intel_crtc *intel_crtc;
  9167. int plane;
  9168. if (INTEL_INFO(dev)->gen < 9)
  9169. return;
  9170. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9171. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9172. for_each_intel_crtc(dev, intel_crtc) {
  9173. struct skl_ddb_entry *hw_entry, *sw_entry;
  9174. const enum pipe pipe = intel_crtc->pipe;
  9175. if (!intel_crtc->active)
  9176. continue;
  9177. /* planes */
  9178. for_each_plane(dev_priv, pipe, plane) {
  9179. hw_entry = &hw_ddb.plane[pipe][plane];
  9180. sw_entry = &sw_ddb->plane[pipe][plane];
  9181. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9182. continue;
  9183. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  9184. "(expected (%u,%u), found (%u,%u))\n",
  9185. pipe_name(pipe), plane + 1,
  9186. sw_entry->start, sw_entry->end,
  9187. hw_entry->start, hw_entry->end);
  9188. }
  9189. /* cursor */
  9190. hw_entry = &hw_ddb.cursor[pipe];
  9191. sw_entry = &sw_ddb->cursor[pipe];
  9192. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  9193. continue;
  9194. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  9195. "(expected (%u,%u), found (%u,%u))\n",
  9196. pipe_name(pipe),
  9197. sw_entry->start, sw_entry->end,
  9198. hw_entry->start, hw_entry->end);
  9199. }
  9200. }
  9201. static void
  9202. check_connector_state(struct drm_device *dev)
  9203. {
  9204. struct intel_connector *connector;
  9205. for_each_intel_connector(dev, connector) {
  9206. /* This also checks the encoder/connector hw state with the
  9207. * ->get_hw_state callbacks. */
  9208. intel_connector_check_state(connector);
  9209. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  9210. "connector's staged encoder doesn't match current encoder\n");
  9211. }
  9212. }
  9213. static void
  9214. check_encoder_state(struct drm_device *dev)
  9215. {
  9216. struct intel_encoder *encoder;
  9217. struct intel_connector *connector;
  9218. for_each_intel_encoder(dev, encoder) {
  9219. bool enabled = false;
  9220. bool active = false;
  9221. enum pipe pipe, tracked_pipe;
  9222. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9223. encoder->base.base.id,
  9224. encoder->base.name);
  9225. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  9226. "encoder's stage crtc doesn't match current crtc\n");
  9227. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  9228. "encoder's active_connectors set, but no crtc\n");
  9229. for_each_intel_connector(dev, connector) {
  9230. if (connector->base.encoder != &encoder->base)
  9231. continue;
  9232. enabled = true;
  9233. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  9234. active = true;
  9235. }
  9236. /*
  9237. * for MST connectors if we unplug the connector is gone
  9238. * away but the encoder is still connected to a crtc
  9239. * until a modeset happens in response to the hotplug.
  9240. */
  9241. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  9242. continue;
  9243. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9244. "encoder's enabled state mismatch "
  9245. "(expected %i, found %i)\n",
  9246. !!encoder->base.crtc, enabled);
  9247. I915_STATE_WARN(active && !encoder->base.crtc,
  9248. "active encoder with no crtc\n");
  9249. I915_STATE_WARN(encoder->connectors_active != active,
  9250. "encoder's computed active state doesn't match tracked active state "
  9251. "(expected %i, found %i)\n", active, encoder->connectors_active);
  9252. active = encoder->get_hw_state(encoder, &pipe);
  9253. I915_STATE_WARN(active != encoder->connectors_active,
  9254. "encoder's hw state doesn't match sw tracking "
  9255. "(expected %i, found %i)\n",
  9256. encoder->connectors_active, active);
  9257. if (!encoder->base.crtc)
  9258. continue;
  9259. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  9260. I915_STATE_WARN(active && pipe != tracked_pipe,
  9261. "active encoder's pipe doesn't match"
  9262. "(expected %i, found %i)\n",
  9263. tracked_pipe, pipe);
  9264. }
  9265. }
  9266. static void
  9267. check_crtc_state(struct drm_device *dev)
  9268. {
  9269. struct drm_i915_private *dev_priv = dev->dev_private;
  9270. struct intel_crtc *crtc;
  9271. struct intel_encoder *encoder;
  9272. struct intel_crtc_state pipe_config;
  9273. for_each_intel_crtc(dev, crtc) {
  9274. bool enabled = false;
  9275. bool active = false;
  9276. memset(&pipe_config, 0, sizeof(pipe_config));
  9277. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9278. crtc->base.base.id);
  9279. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  9280. "active crtc, but not enabled in sw tracking\n");
  9281. for_each_intel_encoder(dev, encoder) {
  9282. if (encoder->base.crtc != &crtc->base)
  9283. continue;
  9284. enabled = true;
  9285. if (encoder->connectors_active)
  9286. active = true;
  9287. }
  9288. I915_STATE_WARN(active != crtc->active,
  9289. "crtc's computed active state doesn't match tracked active state "
  9290. "(expected %i, found %i)\n", active, crtc->active);
  9291. I915_STATE_WARN(enabled != crtc->base.state->enable,
  9292. "crtc's computed enabled state doesn't match tracked enabled state "
  9293. "(expected %i, found %i)\n", enabled,
  9294. crtc->base.state->enable);
  9295. active = dev_priv->display.get_pipe_config(crtc,
  9296. &pipe_config);
  9297. /* hw state is inconsistent with the pipe quirk */
  9298. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9299. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9300. active = crtc->active;
  9301. for_each_intel_encoder(dev, encoder) {
  9302. enum pipe pipe;
  9303. if (encoder->base.crtc != &crtc->base)
  9304. continue;
  9305. if (encoder->get_hw_state(encoder, &pipe))
  9306. encoder->get_config(encoder, &pipe_config);
  9307. }
  9308. I915_STATE_WARN(crtc->active != active,
  9309. "crtc active state doesn't match with hw state "
  9310. "(expected %i, found %i)\n", crtc->active, active);
  9311. if (active &&
  9312. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  9313. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9314. intel_dump_pipe_config(crtc, &pipe_config,
  9315. "[hw state]");
  9316. intel_dump_pipe_config(crtc, crtc->config,
  9317. "[sw state]");
  9318. }
  9319. }
  9320. }
  9321. static void
  9322. check_shared_dpll_state(struct drm_device *dev)
  9323. {
  9324. struct drm_i915_private *dev_priv = dev->dev_private;
  9325. struct intel_crtc *crtc;
  9326. struct intel_dpll_hw_state dpll_hw_state;
  9327. int i;
  9328. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9329. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9330. int enabled_crtcs = 0, active_crtcs = 0;
  9331. bool active;
  9332. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9333. DRM_DEBUG_KMS("%s\n", pll->name);
  9334. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9335. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9336. "more active pll users than references: %i vs %i\n",
  9337. pll->active, hweight32(pll->config.crtc_mask));
  9338. I915_STATE_WARN(pll->active && !pll->on,
  9339. "pll in active use but not on in sw tracking\n");
  9340. I915_STATE_WARN(pll->on && !pll->active,
  9341. "pll in on but not on in use in sw tracking\n");
  9342. I915_STATE_WARN(pll->on != active,
  9343. "pll on state mismatch (expected %i, found %i)\n",
  9344. pll->on, active);
  9345. for_each_intel_crtc(dev, crtc) {
  9346. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  9347. enabled_crtcs++;
  9348. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9349. active_crtcs++;
  9350. }
  9351. I915_STATE_WARN(pll->active != active_crtcs,
  9352. "pll active crtcs mismatch (expected %i, found %i)\n",
  9353. pll->active, active_crtcs);
  9354. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9355. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9356. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9357. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9358. sizeof(dpll_hw_state)),
  9359. "pll hw state mismatch\n");
  9360. }
  9361. }
  9362. void
  9363. intel_modeset_check_state(struct drm_device *dev)
  9364. {
  9365. check_wm_state(dev);
  9366. check_connector_state(dev);
  9367. check_encoder_state(dev);
  9368. check_crtc_state(dev);
  9369. check_shared_dpll_state(dev);
  9370. }
  9371. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  9372. int dotclock)
  9373. {
  9374. /*
  9375. * FDI already provided one idea for the dotclock.
  9376. * Yell if the encoder disagrees.
  9377. */
  9378. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  9379. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9380. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  9381. }
  9382. static void update_scanline_offset(struct intel_crtc *crtc)
  9383. {
  9384. struct drm_device *dev = crtc->base.dev;
  9385. /*
  9386. * The scanline counter increments at the leading edge of hsync.
  9387. *
  9388. * On most platforms it starts counting from vtotal-1 on the
  9389. * first active line. That means the scanline counter value is
  9390. * always one less than what we would expect. Ie. just after
  9391. * start of vblank, which also occurs at start of hsync (on the
  9392. * last active line), the scanline counter will read vblank_start-1.
  9393. *
  9394. * On gen2 the scanline counter starts counting from 1 instead
  9395. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9396. * to keep the value positive), instead of adding one.
  9397. *
  9398. * On HSW+ the behaviour of the scanline counter depends on the output
  9399. * type. For DP ports it behaves like most other platforms, but on HDMI
  9400. * there's an extra 1 line difference. So we need to add two instead of
  9401. * one to the value.
  9402. */
  9403. if (IS_GEN2(dev)) {
  9404. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  9405. int vtotal;
  9406. vtotal = mode->crtc_vtotal;
  9407. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9408. vtotal /= 2;
  9409. crtc->scanline_offset = vtotal - 1;
  9410. } else if (HAS_DDI(dev) &&
  9411. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9412. crtc->scanline_offset = 2;
  9413. } else
  9414. crtc->scanline_offset = 1;
  9415. }
  9416. static struct intel_crtc_state *
  9417. intel_modeset_compute_config(struct drm_crtc *crtc,
  9418. struct drm_display_mode *mode,
  9419. struct drm_framebuffer *fb,
  9420. unsigned *modeset_pipes,
  9421. unsigned *prepare_pipes,
  9422. unsigned *disable_pipes)
  9423. {
  9424. struct intel_crtc_state *pipe_config = NULL;
  9425. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9426. prepare_pipes, disable_pipes);
  9427. if ((*modeset_pipes) == 0)
  9428. goto out;
  9429. /*
  9430. * Note this needs changes when we start tracking multiple modes
  9431. * and crtcs. At that point we'll need to compute the whole config
  9432. * (i.e. one pipe_config for each crtc) rather than just the one
  9433. * for this crtc.
  9434. */
  9435. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9436. if (IS_ERR(pipe_config)) {
  9437. goto out;
  9438. }
  9439. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9440. "[modeset]");
  9441. out:
  9442. return pipe_config;
  9443. }
  9444. static int __intel_set_mode_setup_plls(struct drm_device *dev,
  9445. unsigned modeset_pipes,
  9446. unsigned disable_pipes)
  9447. {
  9448. struct drm_i915_private *dev_priv = to_i915(dev);
  9449. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9450. struct intel_crtc *intel_crtc;
  9451. int ret = 0;
  9452. if (!dev_priv->display.crtc_compute_clock)
  9453. return 0;
  9454. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9455. if (ret)
  9456. goto done;
  9457. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9458. struct intel_crtc_state *state = intel_crtc->new_config;
  9459. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9460. state);
  9461. if (ret) {
  9462. intel_shared_dpll_abort_config(dev_priv);
  9463. goto done;
  9464. }
  9465. }
  9466. done:
  9467. return ret;
  9468. }
  9469. static int __intel_set_mode(struct drm_crtc *crtc,
  9470. struct drm_display_mode *mode,
  9471. int x, int y, struct drm_framebuffer *fb,
  9472. struct intel_crtc_state *pipe_config,
  9473. unsigned modeset_pipes,
  9474. unsigned prepare_pipes,
  9475. unsigned disable_pipes)
  9476. {
  9477. struct drm_device *dev = crtc->dev;
  9478. struct drm_i915_private *dev_priv = dev->dev_private;
  9479. struct drm_display_mode *saved_mode;
  9480. struct intel_crtc *intel_crtc;
  9481. int ret = 0;
  9482. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9483. if (!saved_mode)
  9484. return -ENOMEM;
  9485. *saved_mode = crtc->mode;
  9486. if (modeset_pipes)
  9487. to_intel_crtc(crtc)->new_config = pipe_config;
  9488. /*
  9489. * See if the config requires any additional preparation, e.g.
  9490. * to adjust global state with pipes off. We need to do this
  9491. * here so we can get the modeset_pipe updated config for the new
  9492. * mode set on this crtc. For other crtcs we need to use the
  9493. * adjusted_mode bits in the crtc directly.
  9494. */
  9495. if (IS_VALLEYVIEW(dev)) {
  9496. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9497. /* may have added more to prepare_pipes than we should */
  9498. prepare_pipes &= ~disable_pipes;
  9499. }
  9500. ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
  9501. if (ret)
  9502. goto done;
  9503. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9504. intel_crtc_disable(&intel_crtc->base);
  9505. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9506. if (intel_crtc->base.state->enable)
  9507. dev_priv->display.crtc_disable(&intel_crtc->base);
  9508. }
  9509. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9510. * to set it here already despite that we pass it down the callchain.
  9511. *
  9512. * Note we'll need to fix this up when we start tracking multiple
  9513. * pipes; here we assume a single modeset_pipe and only track the
  9514. * single crtc and mode.
  9515. */
  9516. if (modeset_pipes) {
  9517. crtc->mode = *mode;
  9518. /* mode_set/enable/disable functions rely on a correct pipe
  9519. * config. */
  9520. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  9521. /*
  9522. * Calculate and store various constants which
  9523. * are later needed by vblank and swap-completion
  9524. * timestamping. They are derived from true hwmode.
  9525. */
  9526. drm_calc_timestamping_constants(crtc,
  9527. &pipe_config->base.adjusted_mode);
  9528. }
  9529. /* Only after disabling all output pipelines that will be changed can we
  9530. * update the the output configuration. */
  9531. intel_modeset_update_state(dev, prepare_pipes);
  9532. modeset_update_crtc_power_domains(dev);
  9533. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9534. * on the DPLL.
  9535. */
  9536. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9537. struct drm_plane *primary = intel_crtc->base.primary;
  9538. int vdisplay, hdisplay;
  9539. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9540. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9541. fb, 0, 0,
  9542. hdisplay, vdisplay,
  9543. x << 16, y << 16,
  9544. hdisplay << 16, vdisplay << 16);
  9545. }
  9546. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9547. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9548. update_scanline_offset(intel_crtc);
  9549. dev_priv->display.crtc_enable(&intel_crtc->base);
  9550. }
  9551. /* FIXME: add subpixel order */
  9552. done:
  9553. if (ret && crtc->state->enable)
  9554. crtc->mode = *saved_mode;
  9555. kfree(saved_mode);
  9556. return ret;
  9557. }
  9558. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9559. struct drm_display_mode *mode,
  9560. int x, int y, struct drm_framebuffer *fb,
  9561. struct intel_crtc_state *pipe_config,
  9562. unsigned modeset_pipes,
  9563. unsigned prepare_pipes,
  9564. unsigned disable_pipes)
  9565. {
  9566. int ret;
  9567. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9568. prepare_pipes, disable_pipes);
  9569. if (ret == 0)
  9570. intel_modeset_check_state(crtc->dev);
  9571. return ret;
  9572. }
  9573. static int intel_set_mode(struct drm_crtc *crtc,
  9574. struct drm_display_mode *mode,
  9575. int x, int y, struct drm_framebuffer *fb)
  9576. {
  9577. struct intel_crtc_state *pipe_config;
  9578. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9579. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9580. &modeset_pipes,
  9581. &prepare_pipes,
  9582. &disable_pipes);
  9583. if (IS_ERR(pipe_config))
  9584. return PTR_ERR(pipe_config);
  9585. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9586. modeset_pipes, prepare_pipes,
  9587. disable_pipes);
  9588. }
  9589. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9590. {
  9591. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9592. }
  9593. #undef for_each_intel_crtc_masked
  9594. static void intel_set_config_free(struct intel_set_config *config)
  9595. {
  9596. if (!config)
  9597. return;
  9598. kfree(config->save_connector_encoders);
  9599. kfree(config->save_encoder_crtcs);
  9600. kfree(config->save_crtc_enabled);
  9601. kfree(config);
  9602. }
  9603. static int intel_set_config_save_state(struct drm_device *dev,
  9604. struct intel_set_config *config)
  9605. {
  9606. struct drm_crtc *crtc;
  9607. struct drm_encoder *encoder;
  9608. struct drm_connector *connector;
  9609. int count;
  9610. config->save_crtc_enabled =
  9611. kcalloc(dev->mode_config.num_crtc,
  9612. sizeof(bool), GFP_KERNEL);
  9613. if (!config->save_crtc_enabled)
  9614. return -ENOMEM;
  9615. config->save_encoder_crtcs =
  9616. kcalloc(dev->mode_config.num_encoder,
  9617. sizeof(struct drm_crtc *), GFP_KERNEL);
  9618. if (!config->save_encoder_crtcs)
  9619. return -ENOMEM;
  9620. config->save_connector_encoders =
  9621. kcalloc(dev->mode_config.num_connector,
  9622. sizeof(struct drm_encoder *), GFP_KERNEL);
  9623. if (!config->save_connector_encoders)
  9624. return -ENOMEM;
  9625. /* Copy data. Note that driver private data is not affected.
  9626. * Should anything bad happen only the expected state is
  9627. * restored, not the drivers personal bookkeeping.
  9628. */
  9629. count = 0;
  9630. for_each_crtc(dev, crtc) {
  9631. config->save_crtc_enabled[count++] = crtc->state->enable;
  9632. }
  9633. count = 0;
  9634. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9635. config->save_encoder_crtcs[count++] = encoder->crtc;
  9636. }
  9637. count = 0;
  9638. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9639. config->save_connector_encoders[count++] = connector->encoder;
  9640. }
  9641. return 0;
  9642. }
  9643. static void intel_set_config_restore_state(struct drm_device *dev,
  9644. struct intel_set_config *config)
  9645. {
  9646. struct intel_crtc *crtc;
  9647. struct intel_encoder *encoder;
  9648. struct intel_connector *connector;
  9649. int count;
  9650. count = 0;
  9651. for_each_intel_crtc(dev, crtc) {
  9652. crtc->new_enabled = config->save_crtc_enabled[count++];
  9653. if (crtc->new_enabled)
  9654. crtc->new_config = crtc->config;
  9655. else
  9656. crtc->new_config = NULL;
  9657. }
  9658. count = 0;
  9659. for_each_intel_encoder(dev, encoder) {
  9660. encoder->new_crtc =
  9661. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9662. }
  9663. count = 0;
  9664. for_each_intel_connector(dev, connector) {
  9665. connector->new_encoder =
  9666. to_intel_encoder(config->save_connector_encoders[count++]);
  9667. }
  9668. }
  9669. static bool
  9670. is_crtc_connector_off(struct drm_mode_set *set)
  9671. {
  9672. int i;
  9673. if (set->num_connectors == 0)
  9674. return false;
  9675. if (WARN_ON(set->connectors == NULL))
  9676. return false;
  9677. for (i = 0; i < set->num_connectors; i++)
  9678. if (set->connectors[i]->encoder &&
  9679. set->connectors[i]->encoder->crtc == set->crtc &&
  9680. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9681. return true;
  9682. return false;
  9683. }
  9684. static void
  9685. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9686. struct intel_set_config *config)
  9687. {
  9688. /* We should be able to check here if the fb has the same properties
  9689. * and then just flip_or_move it */
  9690. if (is_crtc_connector_off(set)) {
  9691. config->mode_changed = true;
  9692. } else if (set->crtc->primary->fb != set->fb) {
  9693. /*
  9694. * If we have no fb, we can only flip as long as the crtc is
  9695. * active, otherwise we need a full mode set. The crtc may
  9696. * be active if we've only disabled the primary plane, or
  9697. * in fastboot situations.
  9698. */
  9699. if (set->crtc->primary->fb == NULL) {
  9700. struct intel_crtc *intel_crtc =
  9701. to_intel_crtc(set->crtc);
  9702. if (intel_crtc->active) {
  9703. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9704. config->fb_changed = true;
  9705. } else {
  9706. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9707. config->mode_changed = true;
  9708. }
  9709. } else if (set->fb == NULL) {
  9710. config->mode_changed = true;
  9711. } else if (set->fb->pixel_format !=
  9712. set->crtc->primary->fb->pixel_format) {
  9713. config->mode_changed = true;
  9714. } else {
  9715. config->fb_changed = true;
  9716. }
  9717. }
  9718. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9719. config->fb_changed = true;
  9720. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9721. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9722. drm_mode_debug_printmodeline(&set->crtc->mode);
  9723. drm_mode_debug_printmodeline(set->mode);
  9724. config->mode_changed = true;
  9725. }
  9726. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9727. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9728. }
  9729. static int
  9730. intel_modeset_stage_output_state(struct drm_device *dev,
  9731. struct drm_mode_set *set,
  9732. struct intel_set_config *config)
  9733. {
  9734. struct intel_connector *connector;
  9735. struct intel_encoder *encoder;
  9736. struct intel_crtc *crtc;
  9737. int ro;
  9738. /* The upper layers ensure that we either disable a crtc or have a list
  9739. * of connectors. For paranoia, double-check this. */
  9740. WARN_ON(!set->fb && (set->num_connectors != 0));
  9741. WARN_ON(set->fb && (set->num_connectors == 0));
  9742. for_each_intel_connector(dev, connector) {
  9743. /* Otherwise traverse passed in connector list and get encoders
  9744. * for them. */
  9745. for (ro = 0; ro < set->num_connectors; ro++) {
  9746. if (set->connectors[ro] == &connector->base) {
  9747. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9748. break;
  9749. }
  9750. }
  9751. /* If we disable the crtc, disable all its connectors. Also, if
  9752. * the connector is on the changing crtc but not on the new
  9753. * connector list, disable it. */
  9754. if ((!set->fb || ro == set->num_connectors) &&
  9755. connector->base.encoder &&
  9756. connector->base.encoder->crtc == set->crtc) {
  9757. connector->new_encoder = NULL;
  9758. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9759. connector->base.base.id,
  9760. connector->base.name);
  9761. }
  9762. if (&connector->new_encoder->base != connector->base.encoder) {
  9763. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
  9764. connector->base.base.id,
  9765. connector->base.name);
  9766. config->mode_changed = true;
  9767. }
  9768. }
  9769. /* connector->new_encoder is now updated for all connectors. */
  9770. /* Update crtc of enabled connectors. */
  9771. for_each_intel_connector(dev, connector) {
  9772. struct drm_crtc *new_crtc;
  9773. if (!connector->new_encoder)
  9774. continue;
  9775. new_crtc = connector->new_encoder->base.crtc;
  9776. for (ro = 0; ro < set->num_connectors; ro++) {
  9777. if (set->connectors[ro] == &connector->base)
  9778. new_crtc = set->crtc;
  9779. }
  9780. /* Make sure the new CRTC will work with the encoder */
  9781. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9782. new_crtc)) {
  9783. return -EINVAL;
  9784. }
  9785. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9786. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9787. connector->base.base.id,
  9788. connector->base.name,
  9789. new_crtc->base.id);
  9790. }
  9791. /* Check for any encoders that needs to be disabled. */
  9792. for_each_intel_encoder(dev, encoder) {
  9793. int num_connectors = 0;
  9794. for_each_intel_connector(dev, connector) {
  9795. if (connector->new_encoder == encoder) {
  9796. WARN_ON(!connector->new_encoder->new_crtc);
  9797. num_connectors++;
  9798. }
  9799. }
  9800. if (num_connectors == 0)
  9801. encoder->new_crtc = NULL;
  9802. else if (num_connectors > 1)
  9803. return -EINVAL;
  9804. /* Only now check for crtc changes so we don't miss encoders
  9805. * that will be disabled. */
  9806. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9807. DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
  9808. encoder->base.base.id,
  9809. encoder->base.name);
  9810. config->mode_changed = true;
  9811. }
  9812. }
  9813. /* Now we've also updated encoder->new_crtc for all encoders. */
  9814. for_each_intel_connector(dev, connector) {
  9815. if (connector->new_encoder)
  9816. if (connector->new_encoder != connector->encoder)
  9817. connector->encoder = connector->new_encoder;
  9818. }
  9819. for_each_intel_crtc(dev, crtc) {
  9820. crtc->new_enabled = false;
  9821. for_each_intel_encoder(dev, encoder) {
  9822. if (encoder->new_crtc == crtc) {
  9823. crtc->new_enabled = true;
  9824. break;
  9825. }
  9826. }
  9827. if (crtc->new_enabled != crtc->base.state->enable) {
  9828. DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
  9829. crtc->base.base.id,
  9830. crtc->new_enabled ? "en" : "dis");
  9831. config->mode_changed = true;
  9832. }
  9833. if (crtc->new_enabled)
  9834. crtc->new_config = crtc->config;
  9835. else
  9836. crtc->new_config = NULL;
  9837. }
  9838. return 0;
  9839. }
  9840. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9841. {
  9842. struct drm_device *dev = crtc->base.dev;
  9843. struct intel_encoder *encoder;
  9844. struct intel_connector *connector;
  9845. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9846. pipe_name(crtc->pipe));
  9847. for_each_intel_connector(dev, connector) {
  9848. if (connector->new_encoder &&
  9849. connector->new_encoder->new_crtc == crtc)
  9850. connector->new_encoder = NULL;
  9851. }
  9852. for_each_intel_encoder(dev, encoder) {
  9853. if (encoder->new_crtc == crtc)
  9854. encoder->new_crtc = NULL;
  9855. }
  9856. crtc->new_enabled = false;
  9857. crtc->new_config = NULL;
  9858. }
  9859. static int intel_crtc_set_config(struct drm_mode_set *set)
  9860. {
  9861. struct drm_device *dev;
  9862. struct drm_mode_set save_set;
  9863. struct intel_set_config *config;
  9864. struct intel_crtc_state *pipe_config;
  9865. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9866. int ret;
  9867. BUG_ON(!set);
  9868. BUG_ON(!set->crtc);
  9869. BUG_ON(!set->crtc->helper_private);
  9870. /* Enforce sane interface api - has been abused by the fb helper. */
  9871. BUG_ON(!set->mode && set->fb);
  9872. BUG_ON(set->fb && set->num_connectors == 0);
  9873. if (set->fb) {
  9874. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9875. set->crtc->base.id, set->fb->base.id,
  9876. (int)set->num_connectors, set->x, set->y);
  9877. } else {
  9878. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9879. }
  9880. dev = set->crtc->dev;
  9881. ret = -ENOMEM;
  9882. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9883. if (!config)
  9884. goto out_config;
  9885. ret = intel_set_config_save_state(dev, config);
  9886. if (ret)
  9887. goto out_config;
  9888. save_set.crtc = set->crtc;
  9889. save_set.mode = &set->crtc->mode;
  9890. save_set.x = set->crtc->x;
  9891. save_set.y = set->crtc->y;
  9892. save_set.fb = set->crtc->primary->fb;
  9893. /* Compute whether we need a full modeset, only an fb base update or no
  9894. * change at all. In the future we might also check whether only the
  9895. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9896. * such cases. */
  9897. intel_set_config_compute_mode_changes(set, config);
  9898. ret = intel_modeset_stage_output_state(dev, set, config);
  9899. if (ret)
  9900. goto fail;
  9901. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9902. set->fb,
  9903. &modeset_pipes,
  9904. &prepare_pipes,
  9905. &disable_pipes);
  9906. if (IS_ERR(pipe_config)) {
  9907. ret = PTR_ERR(pipe_config);
  9908. goto fail;
  9909. } else if (pipe_config) {
  9910. if (pipe_config->has_audio !=
  9911. to_intel_crtc(set->crtc)->config->has_audio)
  9912. config->mode_changed = true;
  9913. /*
  9914. * Note we have an issue here with infoframes: current code
  9915. * only updates them on the full mode set path per hw
  9916. * requirements. So here we should be checking for any
  9917. * required changes and forcing a mode set.
  9918. */
  9919. }
  9920. /* set_mode will free it in the mode_changed case */
  9921. if (!config->mode_changed)
  9922. kfree(pipe_config);
  9923. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9924. if (config->mode_changed) {
  9925. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9926. set->x, set->y, set->fb, pipe_config,
  9927. modeset_pipes, prepare_pipes,
  9928. disable_pipes);
  9929. } else if (config->fb_changed) {
  9930. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9931. struct drm_plane *primary = set->crtc->primary;
  9932. int vdisplay, hdisplay;
  9933. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9934. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9935. 0, 0, hdisplay, vdisplay,
  9936. set->x << 16, set->y << 16,
  9937. hdisplay << 16, vdisplay << 16);
  9938. /*
  9939. * We need to make sure the primary plane is re-enabled if it
  9940. * has previously been turned off.
  9941. */
  9942. if (!intel_crtc->primary_enabled && ret == 0) {
  9943. WARN_ON(!intel_crtc->active);
  9944. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9945. }
  9946. /*
  9947. * In the fastboot case this may be our only check of the
  9948. * state after boot. It would be better to only do it on
  9949. * the first update, but we don't have a nice way of doing that
  9950. * (and really, set_config isn't used much for high freq page
  9951. * flipping, so increasing its cost here shouldn't be a big
  9952. * deal).
  9953. */
  9954. if (i915.fastboot && ret == 0)
  9955. intel_modeset_check_state(set->crtc->dev);
  9956. }
  9957. if (ret) {
  9958. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9959. set->crtc->base.id, ret);
  9960. fail:
  9961. intel_set_config_restore_state(dev, config);
  9962. /*
  9963. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9964. * force the pipe off to avoid oopsing in the modeset code
  9965. * due to fb==NULL. This should only happen during boot since
  9966. * we don't yet reconstruct the FB from the hardware state.
  9967. */
  9968. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9969. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9970. /* Try to restore the config */
  9971. if (config->mode_changed &&
  9972. intel_set_mode(save_set.crtc, save_set.mode,
  9973. save_set.x, save_set.y, save_set.fb))
  9974. DRM_ERROR("failed to restore config after modeset failure\n");
  9975. }
  9976. out_config:
  9977. intel_set_config_free(config);
  9978. return ret;
  9979. }
  9980. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9981. .gamma_set = intel_crtc_gamma_set,
  9982. .set_config = intel_crtc_set_config,
  9983. .destroy = intel_crtc_destroy,
  9984. .page_flip = intel_crtc_page_flip,
  9985. .atomic_duplicate_state = intel_crtc_duplicate_state,
  9986. .atomic_destroy_state = intel_crtc_destroy_state,
  9987. };
  9988. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9989. struct intel_shared_dpll *pll,
  9990. struct intel_dpll_hw_state *hw_state)
  9991. {
  9992. uint32_t val;
  9993. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9994. return false;
  9995. val = I915_READ(PCH_DPLL(pll->id));
  9996. hw_state->dpll = val;
  9997. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9998. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9999. return val & DPLL_VCO_ENABLE;
  10000. }
  10001. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  10002. struct intel_shared_dpll *pll)
  10003. {
  10004. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  10005. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  10006. }
  10007. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  10008. struct intel_shared_dpll *pll)
  10009. {
  10010. /* PCH refclock must be enabled first */
  10011. ibx_assert_pch_refclk_enabled(dev_priv);
  10012. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10013. /* Wait for the clocks to stabilize. */
  10014. POSTING_READ(PCH_DPLL(pll->id));
  10015. udelay(150);
  10016. /* The pixel multiplier can only be updated once the
  10017. * DPLL is enabled and the clocks are stable.
  10018. *
  10019. * So write it again.
  10020. */
  10021. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  10022. POSTING_READ(PCH_DPLL(pll->id));
  10023. udelay(200);
  10024. }
  10025. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  10026. struct intel_shared_dpll *pll)
  10027. {
  10028. struct drm_device *dev = dev_priv->dev;
  10029. struct intel_crtc *crtc;
  10030. /* Make sure no transcoder isn't still depending on us. */
  10031. for_each_intel_crtc(dev, crtc) {
  10032. if (intel_crtc_to_shared_dpll(crtc) == pll)
  10033. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  10034. }
  10035. I915_WRITE(PCH_DPLL(pll->id), 0);
  10036. POSTING_READ(PCH_DPLL(pll->id));
  10037. udelay(200);
  10038. }
  10039. static char *ibx_pch_dpll_names[] = {
  10040. "PCH DPLL A",
  10041. "PCH DPLL B",
  10042. };
  10043. static void ibx_pch_dpll_init(struct drm_device *dev)
  10044. {
  10045. struct drm_i915_private *dev_priv = dev->dev_private;
  10046. int i;
  10047. dev_priv->num_shared_dpll = 2;
  10048. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10049. dev_priv->shared_dplls[i].id = i;
  10050. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  10051. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  10052. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  10053. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  10054. dev_priv->shared_dplls[i].get_hw_state =
  10055. ibx_pch_dpll_get_hw_state;
  10056. }
  10057. }
  10058. static void intel_shared_dpll_init(struct drm_device *dev)
  10059. {
  10060. struct drm_i915_private *dev_priv = dev->dev_private;
  10061. if (HAS_DDI(dev))
  10062. intel_ddi_pll_init(dev);
  10063. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  10064. ibx_pch_dpll_init(dev);
  10065. else
  10066. dev_priv->num_shared_dpll = 0;
  10067. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  10068. }
  10069. /**
  10070. * intel_wm_need_update - Check whether watermarks need updating
  10071. * @plane: drm plane
  10072. * @state: new plane state
  10073. *
  10074. * Check current plane state versus the new one to determine whether
  10075. * watermarks need to be recalculated.
  10076. *
  10077. * Returns true or false.
  10078. */
  10079. bool intel_wm_need_update(struct drm_plane *plane,
  10080. struct drm_plane_state *state)
  10081. {
  10082. /* Update watermarks on tiling changes. */
  10083. if (!plane->state->fb || !state->fb ||
  10084. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  10085. plane->state->rotation != state->rotation)
  10086. return true;
  10087. return false;
  10088. }
  10089. /**
  10090. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10091. * @plane: drm plane to prepare for
  10092. * @fb: framebuffer to prepare for presentation
  10093. *
  10094. * Prepares a framebuffer for usage on a display plane. Generally this
  10095. * involves pinning the underlying object and updating the frontbuffer tracking
  10096. * bits. Some older platforms need special physical address handling for
  10097. * cursor planes.
  10098. *
  10099. * Returns 0 on success, negative error code on failure.
  10100. */
  10101. int
  10102. intel_prepare_plane_fb(struct drm_plane *plane,
  10103. struct drm_framebuffer *fb,
  10104. const struct drm_plane_state *new_state)
  10105. {
  10106. struct drm_device *dev = plane->dev;
  10107. struct intel_plane *intel_plane = to_intel_plane(plane);
  10108. enum pipe pipe = intel_plane->pipe;
  10109. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10110. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  10111. unsigned frontbuffer_bits = 0;
  10112. int ret = 0;
  10113. if (!obj)
  10114. return 0;
  10115. switch (plane->type) {
  10116. case DRM_PLANE_TYPE_PRIMARY:
  10117. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  10118. break;
  10119. case DRM_PLANE_TYPE_CURSOR:
  10120. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  10121. break;
  10122. case DRM_PLANE_TYPE_OVERLAY:
  10123. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  10124. break;
  10125. }
  10126. mutex_lock(&dev->struct_mutex);
  10127. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10128. INTEL_INFO(dev)->cursor_needs_physical) {
  10129. int align = IS_I830(dev) ? 16 * 1024 : 256;
  10130. ret = i915_gem_object_attach_phys(obj, align);
  10131. if (ret)
  10132. DRM_DEBUG_KMS("failed to attach phys object\n");
  10133. } else {
  10134. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  10135. }
  10136. if (ret == 0)
  10137. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  10138. mutex_unlock(&dev->struct_mutex);
  10139. return ret;
  10140. }
  10141. /**
  10142. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10143. * @plane: drm plane to clean up for
  10144. * @fb: old framebuffer that was on plane
  10145. *
  10146. * Cleans up a framebuffer that has just been removed from a plane.
  10147. */
  10148. void
  10149. intel_cleanup_plane_fb(struct drm_plane *plane,
  10150. struct drm_framebuffer *fb,
  10151. const struct drm_plane_state *old_state)
  10152. {
  10153. struct drm_device *dev = plane->dev;
  10154. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10155. if (WARN_ON(!obj))
  10156. return;
  10157. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  10158. !INTEL_INFO(dev)->cursor_needs_physical) {
  10159. mutex_lock(&dev->struct_mutex);
  10160. intel_unpin_fb_obj(fb, old_state);
  10161. mutex_unlock(&dev->struct_mutex);
  10162. }
  10163. }
  10164. static int
  10165. intel_check_primary_plane(struct drm_plane *plane,
  10166. struct intel_plane_state *state)
  10167. {
  10168. struct drm_device *dev = plane->dev;
  10169. struct drm_i915_private *dev_priv = dev->dev_private;
  10170. struct drm_crtc *crtc = state->base.crtc;
  10171. struct intel_crtc *intel_crtc;
  10172. struct drm_framebuffer *fb = state->base.fb;
  10173. struct drm_rect *dest = &state->dst;
  10174. struct drm_rect *src = &state->src;
  10175. const struct drm_rect *clip = &state->clip;
  10176. int ret;
  10177. crtc = crtc ? crtc : plane->crtc;
  10178. intel_crtc = to_intel_crtc(crtc);
  10179. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10180. src, dest, clip,
  10181. DRM_PLANE_HELPER_NO_SCALING,
  10182. DRM_PLANE_HELPER_NO_SCALING,
  10183. false, true, &state->visible);
  10184. if (ret)
  10185. return ret;
  10186. if (intel_crtc->active) {
  10187. intel_crtc->atomic.wait_for_flips = true;
  10188. /*
  10189. * FBC does not work on some platforms for rotated
  10190. * planes, so disable it when rotation is not 0 and
  10191. * update it when rotation is set back to 0.
  10192. *
  10193. * FIXME: This is redundant with the fbc update done in
  10194. * the primary plane enable function except that that
  10195. * one is done too late. We eventually need to unify
  10196. * this.
  10197. */
  10198. if (intel_crtc->primary_enabled &&
  10199. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  10200. dev_priv->fbc.crtc == intel_crtc &&
  10201. state->base.rotation != BIT(DRM_ROTATE_0)) {
  10202. intel_crtc->atomic.disable_fbc = true;
  10203. }
  10204. if (state->visible) {
  10205. /*
  10206. * BDW signals flip done immediately if the plane
  10207. * is disabled, even if the plane enable is already
  10208. * armed to occur at the next vblank :(
  10209. */
  10210. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  10211. intel_crtc->atomic.wait_vblank = true;
  10212. }
  10213. intel_crtc->atomic.fb_bits |=
  10214. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  10215. intel_crtc->atomic.update_fbc = true;
  10216. if (intel_wm_need_update(plane, &state->base))
  10217. intel_crtc->atomic.update_wm = true;
  10218. }
  10219. return 0;
  10220. }
  10221. static void
  10222. intel_commit_primary_plane(struct drm_plane *plane,
  10223. struct intel_plane_state *state)
  10224. {
  10225. struct drm_crtc *crtc = state->base.crtc;
  10226. struct drm_framebuffer *fb = state->base.fb;
  10227. struct drm_device *dev = plane->dev;
  10228. struct drm_i915_private *dev_priv = dev->dev_private;
  10229. struct intel_crtc *intel_crtc;
  10230. struct drm_rect *src = &state->src;
  10231. crtc = crtc ? crtc : plane->crtc;
  10232. intel_crtc = to_intel_crtc(crtc);
  10233. plane->fb = fb;
  10234. crtc->x = src->x1 >> 16;
  10235. crtc->y = src->y1 >> 16;
  10236. if (intel_crtc->active) {
  10237. if (state->visible) {
  10238. /* FIXME: kill this fastboot hack */
  10239. intel_update_pipe_size(intel_crtc);
  10240. intel_crtc->primary_enabled = true;
  10241. dev_priv->display.update_primary_plane(crtc, plane->fb,
  10242. crtc->x, crtc->y);
  10243. } else {
  10244. /*
  10245. * If clipping results in a non-visible primary plane,
  10246. * we'll disable the primary plane. Note that this is
  10247. * a bit different than what happens if userspace
  10248. * explicitly disables the plane by passing fb=0
  10249. * because plane->fb still gets set and pinned.
  10250. */
  10251. intel_disable_primary_hw_plane(plane, crtc);
  10252. }
  10253. }
  10254. }
  10255. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  10256. {
  10257. struct drm_device *dev = crtc->dev;
  10258. struct drm_i915_private *dev_priv = dev->dev_private;
  10259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10260. struct intel_plane *intel_plane;
  10261. struct drm_plane *p;
  10262. unsigned fb_bits = 0;
  10263. /* Track fb's for any planes being disabled */
  10264. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  10265. intel_plane = to_intel_plane(p);
  10266. if (intel_crtc->atomic.disabled_planes &
  10267. (1 << drm_plane_index(p))) {
  10268. switch (p->type) {
  10269. case DRM_PLANE_TYPE_PRIMARY:
  10270. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  10271. break;
  10272. case DRM_PLANE_TYPE_CURSOR:
  10273. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  10274. break;
  10275. case DRM_PLANE_TYPE_OVERLAY:
  10276. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  10277. break;
  10278. }
  10279. mutex_lock(&dev->struct_mutex);
  10280. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  10281. mutex_unlock(&dev->struct_mutex);
  10282. }
  10283. }
  10284. if (intel_crtc->atomic.wait_for_flips)
  10285. intel_crtc_wait_for_pending_flips(crtc);
  10286. if (intel_crtc->atomic.disable_fbc)
  10287. intel_fbc_disable(dev);
  10288. if (intel_crtc->atomic.pre_disable_primary)
  10289. intel_pre_disable_primary(crtc);
  10290. if (intel_crtc->atomic.update_wm)
  10291. intel_update_watermarks(crtc);
  10292. intel_runtime_pm_get(dev_priv);
  10293. /* Perform vblank evasion around commit operation */
  10294. if (intel_crtc->active)
  10295. intel_crtc->atomic.evade =
  10296. intel_pipe_update_start(intel_crtc,
  10297. &intel_crtc->atomic.start_vbl_count);
  10298. }
  10299. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  10300. {
  10301. struct drm_device *dev = crtc->dev;
  10302. struct drm_i915_private *dev_priv = dev->dev_private;
  10303. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10304. struct drm_plane *p;
  10305. if (intel_crtc->atomic.evade)
  10306. intel_pipe_update_end(intel_crtc,
  10307. intel_crtc->atomic.start_vbl_count);
  10308. intel_runtime_pm_put(dev_priv);
  10309. if (intel_crtc->atomic.wait_vblank)
  10310. intel_wait_for_vblank(dev, intel_crtc->pipe);
  10311. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  10312. if (intel_crtc->atomic.update_fbc) {
  10313. mutex_lock(&dev->struct_mutex);
  10314. intel_fbc_update(dev);
  10315. mutex_unlock(&dev->struct_mutex);
  10316. }
  10317. if (intel_crtc->atomic.post_enable_primary)
  10318. intel_post_enable_primary(crtc);
  10319. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  10320. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  10321. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  10322. false, false);
  10323. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  10324. }
  10325. /**
  10326. * intel_plane_destroy - destroy a plane
  10327. * @plane: plane to destroy
  10328. *
  10329. * Common destruction function for all types of planes (primary, cursor,
  10330. * sprite).
  10331. */
  10332. void intel_plane_destroy(struct drm_plane *plane)
  10333. {
  10334. struct intel_plane *intel_plane = to_intel_plane(plane);
  10335. drm_plane_cleanup(plane);
  10336. kfree(intel_plane);
  10337. }
  10338. const struct drm_plane_funcs intel_plane_funcs = {
  10339. .update_plane = drm_plane_helper_update,
  10340. .disable_plane = drm_plane_helper_disable,
  10341. .destroy = intel_plane_destroy,
  10342. .set_property = drm_atomic_helper_plane_set_property,
  10343. .atomic_get_property = intel_plane_atomic_get_property,
  10344. .atomic_set_property = intel_plane_atomic_set_property,
  10345. .atomic_duplicate_state = intel_plane_duplicate_state,
  10346. .atomic_destroy_state = intel_plane_destroy_state,
  10347. };
  10348. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10349. int pipe)
  10350. {
  10351. struct intel_plane *primary;
  10352. struct intel_plane_state *state;
  10353. const uint32_t *intel_primary_formats;
  10354. int num_formats;
  10355. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10356. if (primary == NULL)
  10357. return NULL;
  10358. state = intel_create_plane_state(&primary->base);
  10359. if (!state) {
  10360. kfree(primary);
  10361. return NULL;
  10362. }
  10363. primary->base.state = &state->base;
  10364. primary->can_scale = false;
  10365. primary->max_downscale = 1;
  10366. primary->pipe = pipe;
  10367. primary->plane = pipe;
  10368. primary->check_plane = intel_check_primary_plane;
  10369. primary->commit_plane = intel_commit_primary_plane;
  10370. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10371. primary->plane = !pipe;
  10372. if (INTEL_INFO(dev)->gen <= 3) {
  10373. intel_primary_formats = intel_primary_formats_gen2;
  10374. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10375. } else {
  10376. intel_primary_formats = intel_primary_formats_gen4;
  10377. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10378. }
  10379. drm_universal_plane_init(dev, &primary->base, 0,
  10380. &intel_plane_funcs,
  10381. intel_primary_formats, num_formats,
  10382. DRM_PLANE_TYPE_PRIMARY);
  10383. if (INTEL_INFO(dev)->gen >= 4) {
  10384. if (!dev->mode_config.rotation_property)
  10385. dev->mode_config.rotation_property =
  10386. drm_mode_create_rotation_property(dev,
  10387. BIT(DRM_ROTATE_0) |
  10388. BIT(DRM_ROTATE_180));
  10389. if (dev->mode_config.rotation_property)
  10390. drm_object_attach_property(&primary->base.base,
  10391. dev->mode_config.rotation_property,
  10392. state->base.rotation);
  10393. }
  10394. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  10395. return &primary->base;
  10396. }
  10397. static int
  10398. intel_check_cursor_plane(struct drm_plane *plane,
  10399. struct intel_plane_state *state)
  10400. {
  10401. struct drm_crtc *crtc = state->base.crtc;
  10402. struct drm_device *dev = plane->dev;
  10403. struct drm_framebuffer *fb = state->base.fb;
  10404. struct drm_rect *dest = &state->dst;
  10405. struct drm_rect *src = &state->src;
  10406. const struct drm_rect *clip = &state->clip;
  10407. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10408. struct intel_crtc *intel_crtc;
  10409. unsigned stride;
  10410. int ret;
  10411. crtc = crtc ? crtc : plane->crtc;
  10412. intel_crtc = to_intel_crtc(crtc);
  10413. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10414. src, dest, clip,
  10415. DRM_PLANE_HELPER_NO_SCALING,
  10416. DRM_PLANE_HELPER_NO_SCALING,
  10417. true, true, &state->visible);
  10418. if (ret)
  10419. return ret;
  10420. /* if we want to turn off the cursor ignore width and height */
  10421. if (!obj)
  10422. goto finish;
  10423. /* Check for which cursor types we support */
  10424. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  10425. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  10426. state->base.crtc_w, state->base.crtc_h);
  10427. return -EINVAL;
  10428. }
  10429. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  10430. if (obj->base.size < stride * state->base.crtc_h) {
  10431. DRM_DEBUG_KMS("buffer is too small\n");
  10432. return -ENOMEM;
  10433. }
  10434. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  10435. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10436. ret = -EINVAL;
  10437. }
  10438. finish:
  10439. if (intel_crtc->active) {
  10440. if (plane->state->crtc_w != state->base.crtc_w)
  10441. intel_crtc->atomic.update_wm = true;
  10442. intel_crtc->atomic.fb_bits |=
  10443. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  10444. }
  10445. return ret;
  10446. }
  10447. static void
  10448. intel_commit_cursor_plane(struct drm_plane *plane,
  10449. struct intel_plane_state *state)
  10450. {
  10451. struct drm_crtc *crtc = state->base.crtc;
  10452. struct drm_device *dev = plane->dev;
  10453. struct intel_crtc *intel_crtc;
  10454. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10455. uint32_t addr;
  10456. crtc = crtc ? crtc : plane->crtc;
  10457. intel_crtc = to_intel_crtc(crtc);
  10458. plane->fb = state->base.fb;
  10459. crtc->cursor_x = state->base.crtc_x;
  10460. crtc->cursor_y = state->base.crtc_y;
  10461. if (intel_crtc->cursor_bo == obj)
  10462. goto update;
  10463. if (!obj)
  10464. addr = 0;
  10465. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10466. addr = i915_gem_obj_ggtt_offset(obj);
  10467. else
  10468. addr = obj->phys_handle->busaddr;
  10469. intel_crtc->cursor_addr = addr;
  10470. intel_crtc->cursor_bo = obj;
  10471. update:
  10472. if (intel_crtc->active)
  10473. intel_crtc_update_cursor(crtc, state->visible);
  10474. }
  10475. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10476. int pipe)
  10477. {
  10478. struct intel_plane *cursor;
  10479. struct intel_plane_state *state;
  10480. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10481. if (cursor == NULL)
  10482. return NULL;
  10483. state = intel_create_plane_state(&cursor->base);
  10484. if (!state) {
  10485. kfree(cursor);
  10486. return NULL;
  10487. }
  10488. cursor->base.state = &state->base;
  10489. cursor->can_scale = false;
  10490. cursor->max_downscale = 1;
  10491. cursor->pipe = pipe;
  10492. cursor->plane = pipe;
  10493. cursor->check_plane = intel_check_cursor_plane;
  10494. cursor->commit_plane = intel_commit_cursor_plane;
  10495. drm_universal_plane_init(dev, &cursor->base, 0,
  10496. &intel_plane_funcs,
  10497. intel_cursor_formats,
  10498. ARRAY_SIZE(intel_cursor_formats),
  10499. DRM_PLANE_TYPE_CURSOR);
  10500. if (INTEL_INFO(dev)->gen >= 4) {
  10501. if (!dev->mode_config.rotation_property)
  10502. dev->mode_config.rotation_property =
  10503. drm_mode_create_rotation_property(dev,
  10504. BIT(DRM_ROTATE_0) |
  10505. BIT(DRM_ROTATE_180));
  10506. if (dev->mode_config.rotation_property)
  10507. drm_object_attach_property(&cursor->base.base,
  10508. dev->mode_config.rotation_property,
  10509. state->base.rotation);
  10510. }
  10511. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  10512. return &cursor->base;
  10513. }
  10514. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10515. {
  10516. struct drm_i915_private *dev_priv = dev->dev_private;
  10517. struct intel_crtc *intel_crtc;
  10518. struct intel_crtc_state *crtc_state = NULL;
  10519. struct drm_plane *primary = NULL;
  10520. struct drm_plane *cursor = NULL;
  10521. int i, ret;
  10522. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10523. if (intel_crtc == NULL)
  10524. return;
  10525. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  10526. if (!crtc_state)
  10527. goto fail;
  10528. intel_crtc_set_state(intel_crtc, crtc_state);
  10529. crtc_state->base.crtc = &intel_crtc->base;
  10530. primary = intel_primary_plane_create(dev, pipe);
  10531. if (!primary)
  10532. goto fail;
  10533. cursor = intel_cursor_plane_create(dev, pipe);
  10534. if (!cursor)
  10535. goto fail;
  10536. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10537. cursor, &intel_crtc_funcs);
  10538. if (ret)
  10539. goto fail;
  10540. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10541. for (i = 0; i < 256; i++) {
  10542. intel_crtc->lut_r[i] = i;
  10543. intel_crtc->lut_g[i] = i;
  10544. intel_crtc->lut_b[i] = i;
  10545. }
  10546. /*
  10547. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10548. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10549. */
  10550. intel_crtc->pipe = pipe;
  10551. intel_crtc->plane = pipe;
  10552. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10553. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10554. intel_crtc->plane = !pipe;
  10555. }
  10556. intel_crtc->cursor_base = ~0;
  10557. intel_crtc->cursor_cntl = ~0;
  10558. intel_crtc->cursor_size = ~0;
  10559. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10560. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10561. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10562. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10563. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10564. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10565. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10566. return;
  10567. fail:
  10568. if (primary)
  10569. drm_plane_cleanup(primary);
  10570. if (cursor)
  10571. drm_plane_cleanup(cursor);
  10572. kfree(crtc_state);
  10573. kfree(intel_crtc);
  10574. }
  10575. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10576. {
  10577. struct drm_encoder *encoder = connector->base.encoder;
  10578. struct drm_device *dev = connector->base.dev;
  10579. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10580. if (!encoder || WARN_ON(!encoder->crtc))
  10581. return INVALID_PIPE;
  10582. return to_intel_crtc(encoder->crtc)->pipe;
  10583. }
  10584. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10585. struct drm_file *file)
  10586. {
  10587. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10588. struct drm_crtc *drmmode_crtc;
  10589. struct intel_crtc *crtc;
  10590. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10591. if (!drmmode_crtc) {
  10592. DRM_ERROR("no such CRTC id\n");
  10593. return -ENOENT;
  10594. }
  10595. crtc = to_intel_crtc(drmmode_crtc);
  10596. pipe_from_crtc_id->pipe = crtc->pipe;
  10597. return 0;
  10598. }
  10599. static int intel_encoder_clones(struct intel_encoder *encoder)
  10600. {
  10601. struct drm_device *dev = encoder->base.dev;
  10602. struct intel_encoder *source_encoder;
  10603. int index_mask = 0;
  10604. int entry = 0;
  10605. for_each_intel_encoder(dev, source_encoder) {
  10606. if (encoders_cloneable(encoder, source_encoder))
  10607. index_mask |= (1 << entry);
  10608. entry++;
  10609. }
  10610. return index_mask;
  10611. }
  10612. static bool has_edp_a(struct drm_device *dev)
  10613. {
  10614. struct drm_i915_private *dev_priv = dev->dev_private;
  10615. if (!IS_MOBILE(dev))
  10616. return false;
  10617. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10618. return false;
  10619. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10620. return false;
  10621. return true;
  10622. }
  10623. static bool intel_crt_present(struct drm_device *dev)
  10624. {
  10625. struct drm_i915_private *dev_priv = dev->dev_private;
  10626. if (INTEL_INFO(dev)->gen >= 9)
  10627. return false;
  10628. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10629. return false;
  10630. if (IS_CHERRYVIEW(dev))
  10631. return false;
  10632. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10633. return false;
  10634. return true;
  10635. }
  10636. static void intel_setup_outputs(struct drm_device *dev)
  10637. {
  10638. struct drm_i915_private *dev_priv = dev->dev_private;
  10639. struct intel_encoder *encoder;
  10640. struct drm_connector *connector;
  10641. bool dpd_is_edp = false;
  10642. intel_lvds_init(dev);
  10643. if (intel_crt_present(dev))
  10644. intel_crt_init(dev);
  10645. if (HAS_DDI(dev)) {
  10646. int found;
  10647. /*
  10648. * Haswell uses DDI functions to detect digital outputs.
  10649. * On SKL pre-D0 the strap isn't connected, so we assume
  10650. * it's there.
  10651. */
  10652. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10653. /* WaIgnoreDDIAStrap: skl */
  10654. if (found ||
  10655. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  10656. intel_ddi_init(dev, PORT_A);
  10657. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10658. * register */
  10659. found = I915_READ(SFUSE_STRAP);
  10660. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10661. intel_ddi_init(dev, PORT_B);
  10662. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10663. intel_ddi_init(dev, PORT_C);
  10664. if (found & SFUSE_STRAP_DDID_DETECTED)
  10665. intel_ddi_init(dev, PORT_D);
  10666. } else if (HAS_PCH_SPLIT(dev)) {
  10667. int found;
  10668. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10669. if (has_edp_a(dev))
  10670. intel_dp_init(dev, DP_A, PORT_A);
  10671. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10672. /* PCH SDVOB multiplex with HDMIB */
  10673. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10674. if (!found)
  10675. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10676. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10677. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10678. }
  10679. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10680. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10681. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10682. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10683. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10684. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10685. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10686. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10687. } else if (IS_VALLEYVIEW(dev)) {
  10688. /*
  10689. * The DP_DETECTED bit is the latched state of the DDC
  10690. * SDA pin at boot. However since eDP doesn't require DDC
  10691. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10692. * eDP ports may have been muxed to an alternate function.
  10693. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10694. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10695. * detect eDP ports.
  10696. */
  10697. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  10698. !intel_dp_is_edp(dev, PORT_B))
  10699. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10700. PORT_B);
  10701. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10702. intel_dp_is_edp(dev, PORT_B))
  10703. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10704. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  10705. !intel_dp_is_edp(dev, PORT_C))
  10706. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10707. PORT_C);
  10708. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10709. intel_dp_is_edp(dev, PORT_C))
  10710. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10711. if (IS_CHERRYVIEW(dev)) {
  10712. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10713. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10714. PORT_D);
  10715. /* eDP not supported on port D, so don't check VBT */
  10716. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10717. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10718. }
  10719. intel_dsi_init(dev);
  10720. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10721. bool found = false;
  10722. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10723. DRM_DEBUG_KMS("probing SDVOB\n");
  10724. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10725. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10726. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10727. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10728. }
  10729. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10730. intel_dp_init(dev, DP_B, PORT_B);
  10731. }
  10732. /* Before G4X SDVOC doesn't have its own detect register */
  10733. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10734. DRM_DEBUG_KMS("probing SDVOC\n");
  10735. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10736. }
  10737. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10738. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10739. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10740. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10741. }
  10742. if (SUPPORTS_INTEGRATED_DP(dev))
  10743. intel_dp_init(dev, DP_C, PORT_C);
  10744. }
  10745. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10746. (I915_READ(DP_D) & DP_DETECTED))
  10747. intel_dp_init(dev, DP_D, PORT_D);
  10748. } else if (IS_GEN2(dev))
  10749. intel_dvo_init(dev);
  10750. if (SUPPORTS_TV(dev))
  10751. intel_tv_init(dev);
  10752. /*
  10753. * FIXME: We don't have full atomic support yet, but we want to be
  10754. * able to enable/test plane updates via the atomic interface in the
  10755. * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
  10756. * will take some atomic codepaths to lookup properties during
  10757. * drmModeGetConnector() that unconditionally dereference
  10758. * connector->state.
  10759. *
  10760. * We create a dummy connector state here for each connector to ensure
  10761. * the DRM core doesn't try to dereference a NULL connector->state.
  10762. * The actual connector properties will never be updated or contain
  10763. * useful information, but since we're doing this specifically for
  10764. * testing/debug of the plane operations (and only when a specific
  10765. * kernel module option is given), that shouldn't really matter.
  10766. *
  10767. * Once atomic support for crtc's + connectors lands, this loop should
  10768. * be removed since we'll be setting up real connector state, which
  10769. * will contain Intel-specific properties.
  10770. */
  10771. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  10772. list_for_each_entry(connector,
  10773. &dev->mode_config.connector_list,
  10774. head) {
  10775. if (!WARN_ON(connector->state)) {
  10776. connector->state =
  10777. kzalloc(sizeof(*connector->state),
  10778. GFP_KERNEL);
  10779. }
  10780. }
  10781. }
  10782. intel_psr_init(dev);
  10783. for_each_intel_encoder(dev, encoder) {
  10784. encoder->base.possible_crtcs = encoder->crtc_mask;
  10785. encoder->base.possible_clones =
  10786. intel_encoder_clones(encoder);
  10787. }
  10788. intel_init_pch_refclk(dev);
  10789. drm_helper_move_panel_connectors_to_head(dev);
  10790. }
  10791. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10792. {
  10793. struct drm_device *dev = fb->dev;
  10794. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10795. drm_framebuffer_cleanup(fb);
  10796. mutex_lock(&dev->struct_mutex);
  10797. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10798. drm_gem_object_unreference(&intel_fb->obj->base);
  10799. mutex_unlock(&dev->struct_mutex);
  10800. kfree(intel_fb);
  10801. }
  10802. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10803. struct drm_file *file,
  10804. unsigned int *handle)
  10805. {
  10806. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10807. struct drm_i915_gem_object *obj = intel_fb->obj;
  10808. return drm_gem_handle_create(file, &obj->base, handle);
  10809. }
  10810. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10811. .destroy = intel_user_framebuffer_destroy,
  10812. .create_handle = intel_user_framebuffer_create_handle,
  10813. };
  10814. static
  10815. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  10816. uint32_t pixel_format)
  10817. {
  10818. u32 gen = INTEL_INFO(dev)->gen;
  10819. if (gen >= 9) {
  10820. /* "The stride in bytes must not exceed the of the size of 8K
  10821. * pixels and 32K bytes."
  10822. */
  10823. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  10824. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10825. return 32*1024;
  10826. } else if (gen >= 4) {
  10827. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  10828. return 16*1024;
  10829. else
  10830. return 32*1024;
  10831. } else if (gen >= 3) {
  10832. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  10833. return 8*1024;
  10834. else
  10835. return 16*1024;
  10836. } else {
  10837. /* XXX DSPC is limited to 4k tiled */
  10838. return 8*1024;
  10839. }
  10840. }
  10841. static int intel_framebuffer_init(struct drm_device *dev,
  10842. struct intel_framebuffer *intel_fb,
  10843. struct drm_mode_fb_cmd2 *mode_cmd,
  10844. struct drm_i915_gem_object *obj)
  10845. {
  10846. unsigned int aligned_height;
  10847. int ret;
  10848. u32 pitch_limit, stride_alignment;
  10849. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10850. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  10851. /* Enforce that fb modifier and tiling mode match, but only for
  10852. * X-tiled. This is needed for FBC. */
  10853. if (!!(obj->tiling_mode == I915_TILING_X) !=
  10854. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  10855. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  10856. return -EINVAL;
  10857. }
  10858. } else {
  10859. if (obj->tiling_mode == I915_TILING_X)
  10860. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  10861. else if (obj->tiling_mode == I915_TILING_Y) {
  10862. DRM_DEBUG("No Y tiling for legacy addfb\n");
  10863. return -EINVAL;
  10864. }
  10865. }
  10866. /* Passed in modifier sanity checking. */
  10867. switch (mode_cmd->modifier[0]) {
  10868. case I915_FORMAT_MOD_Y_TILED:
  10869. case I915_FORMAT_MOD_Yf_TILED:
  10870. if (INTEL_INFO(dev)->gen < 9) {
  10871. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  10872. mode_cmd->modifier[0]);
  10873. return -EINVAL;
  10874. }
  10875. case DRM_FORMAT_MOD_NONE:
  10876. case I915_FORMAT_MOD_X_TILED:
  10877. break;
  10878. default:
  10879. DRM_ERROR("Unsupported fb modifier 0x%llx!\n",
  10880. mode_cmd->modifier[0]);
  10881. return -EINVAL;
  10882. }
  10883. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  10884. mode_cmd->pixel_format);
  10885. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  10886. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  10887. mode_cmd->pitches[0], stride_alignment);
  10888. return -EINVAL;
  10889. }
  10890. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  10891. mode_cmd->pixel_format);
  10892. if (mode_cmd->pitches[0] > pitch_limit) {
  10893. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  10894. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  10895. "tiled" : "linear",
  10896. mode_cmd->pitches[0], pitch_limit);
  10897. return -EINVAL;
  10898. }
  10899. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  10900. mode_cmd->pitches[0] != obj->stride) {
  10901. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10902. mode_cmd->pitches[0], obj->stride);
  10903. return -EINVAL;
  10904. }
  10905. /* Reject formats not supported by any plane early. */
  10906. switch (mode_cmd->pixel_format) {
  10907. case DRM_FORMAT_C8:
  10908. case DRM_FORMAT_RGB565:
  10909. case DRM_FORMAT_XRGB8888:
  10910. case DRM_FORMAT_ARGB8888:
  10911. break;
  10912. case DRM_FORMAT_XRGB1555:
  10913. case DRM_FORMAT_ARGB1555:
  10914. if (INTEL_INFO(dev)->gen > 3) {
  10915. DRM_DEBUG("unsupported pixel format: %s\n",
  10916. drm_get_format_name(mode_cmd->pixel_format));
  10917. return -EINVAL;
  10918. }
  10919. break;
  10920. case DRM_FORMAT_XBGR8888:
  10921. case DRM_FORMAT_ABGR8888:
  10922. case DRM_FORMAT_XRGB2101010:
  10923. case DRM_FORMAT_ARGB2101010:
  10924. case DRM_FORMAT_XBGR2101010:
  10925. case DRM_FORMAT_ABGR2101010:
  10926. if (INTEL_INFO(dev)->gen < 4) {
  10927. DRM_DEBUG("unsupported pixel format: %s\n",
  10928. drm_get_format_name(mode_cmd->pixel_format));
  10929. return -EINVAL;
  10930. }
  10931. break;
  10932. case DRM_FORMAT_YUYV:
  10933. case DRM_FORMAT_UYVY:
  10934. case DRM_FORMAT_YVYU:
  10935. case DRM_FORMAT_VYUY:
  10936. if (INTEL_INFO(dev)->gen < 5) {
  10937. DRM_DEBUG("unsupported pixel format: %s\n",
  10938. drm_get_format_name(mode_cmd->pixel_format));
  10939. return -EINVAL;
  10940. }
  10941. break;
  10942. default:
  10943. DRM_DEBUG("unsupported pixel format: %s\n",
  10944. drm_get_format_name(mode_cmd->pixel_format));
  10945. return -EINVAL;
  10946. }
  10947. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10948. if (mode_cmd->offsets[0] != 0)
  10949. return -EINVAL;
  10950. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  10951. mode_cmd->pixel_format,
  10952. mode_cmd->modifier[0]);
  10953. /* FIXME drm helper for size checks (especially planar formats)? */
  10954. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10955. return -EINVAL;
  10956. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10957. intel_fb->obj = obj;
  10958. intel_fb->obj->framebuffer_references++;
  10959. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10960. if (ret) {
  10961. DRM_ERROR("framebuffer init failed %d\n", ret);
  10962. return ret;
  10963. }
  10964. return 0;
  10965. }
  10966. static struct drm_framebuffer *
  10967. intel_user_framebuffer_create(struct drm_device *dev,
  10968. struct drm_file *filp,
  10969. struct drm_mode_fb_cmd2 *mode_cmd)
  10970. {
  10971. struct drm_i915_gem_object *obj;
  10972. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10973. mode_cmd->handles[0]));
  10974. if (&obj->base == NULL)
  10975. return ERR_PTR(-ENOENT);
  10976. return intel_framebuffer_create(dev, mode_cmd, obj);
  10977. }
  10978. #ifndef CONFIG_DRM_I915_FBDEV
  10979. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10980. {
  10981. }
  10982. #endif
  10983. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10984. .fb_create = intel_user_framebuffer_create,
  10985. .output_poll_changed = intel_fbdev_output_poll_changed,
  10986. .atomic_check = intel_atomic_check,
  10987. .atomic_commit = intel_atomic_commit,
  10988. };
  10989. /* Set up chip specific display functions */
  10990. static void intel_init_display(struct drm_device *dev)
  10991. {
  10992. struct drm_i915_private *dev_priv = dev->dev_private;
  10993. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10994. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10995. else if (IS_CHERRYVIEW(dev))
  10996. dev_priv->display.find_dpll = chv_find_best_dpll;
  10997. else if (IS_VALLEYVIEW(dev))
  10998. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10999. else if (IS_PINEVIEW(dev))
  11000. dev_priv->display.find_dpll = pnv_find_best_dpll;
  11001. else
  11002. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  11003. if (INTEL_INFO(dev)->gen >= 9) {
  11004. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11005. dev_priv->display.get_initial_plane_config =
  11006. skylake_get_initial_plane_config;
  11007. dev_priv->display.crtc_compute_clock =
  11008. haswell_crtc_compute_clock;
  11009. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11010. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11011. dev_priv->display.off = ironlake_crtc_off;
  11012. dev_priv->display.update_primary_plane =
  11013. skylake_update_primary_plane;
  11014. } else if (HAS_DDI(dev)) {
  11015. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  11016. dev_priv->display.get_initial_plane_config =
  11017. ironlake_get_initial_plane_config;
  11018. dev_priv->display.crtc_compute_clock =
  11019. haswell_crtc_compute_clock;
  11020. dev_priv->display.crtc_enable = haswell_crtc_enable;
  11021. dev_priv->display.crtc_disable = haswell_crtc_disable;
  11022. dev_priv->display.off = ironlake_crtc_off;
  11023. dev_priv->display.update_primary_plane =
  11024. ironlake_update_primary_plane;
  11025. } else if (HAS_PCH_SPLIT(dev)) {
  11026. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  11027. dev_priv->display.get_initial_plane_config =
  11028. ironlake_get_initial_plane_config;
  11029. dev_priv->display.crtc_compute_clock =
  11030. ironlake_crtc_compute_clock;
  11031. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  11032. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  11033. dev_priv->display.off = ironlake_crtc_off;
  11034. dev_priv->display.update_primary_plane =
  11035. ironlake_update_primary_plane;
  11036. } else if (IS_VALLEYVIEW(dev)) {
  11037. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11038. dev_priv->display.get_initial_plane_config =
  11039. i9xx_get_initial_plane_config;
  11040. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11041. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  11042. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11043. dev_priv->display.off = i9xx_crtc_off;
  11044. dev_priv->display.update_primary_plane =
  11045. i9xx_update_primary_plane;
  11046. } else {
  11047. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  11048. dev_priv->display.get_initial_plane_config =
  11049. i9xx_get_initial_plane_config;
  11050. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  11051. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  11052. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  11053. dev_priv->display.off = i9xx_crtc_off;
  11054. dev_priv->display.update_primary_plane =
  11055. i9xx_update_primary_plane;
  11056. }
  11057. /* Returns the core display clock speed */
  11058. if (IS_VALLEYVIEW(dev))
  11059. dev_priv->display.get_display_clock_speed =
  11060. valleyview_get_display_clock_speed;
  11061. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  11062. dev_priv->display.get_display_clock_speed =
  11063. i945_get_display_clock_speed;
  11064. else if (IS_I915G(dev))
  11065. dev_priv->display.get_display_clock_speed =
  11066. i915_get_display_clock_speed;
  11067. else if (IS_I945GM(dev) || IS_845G(dev))
  11068. dev_priv->display.get_display_clock_speed =
  11069. i9xx_misc_get_display_clock_speed;
  11070. else if (IS_PINEVIEW(dev))
  11071. dev_priv->display.get_display_clock_speed =
  11072. pnv_get_display_clock_speed;
  11073. else if (IS_I915GM(dev))
  11074. dev_priv->display.get_display_clock_speed =
  11075. i915gm_get_display_clock_speed;
  11076. else if (IS_I865G(dev))
  11077. dev_priv->display.get_display_clock_speed =
  11078. i865_get_display_clock_speed;
  11079. else if (IS_I85X(dev))
  11080. dev_priv->display.get_display_clock_speed =
  11081. i855_get_display_clock_speed;
  11082. else /* 852, 830 */
  11083. dev_priv->display.get_display_clock_speed =
  11084. i830_get_display_clock_speed;
  11085. if (IS_GEN5(dev)) {
  11086. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  11087. } else if (IS_GEN6(dev)) {
  11088. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  11089. } else if (IS_IVYBRIDGE(dev)) {
  11090. /* FIXME: detect B0+ stepping and use auto training */
  11091. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  11092. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  11093. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  11094. } else if (IS_VALLEYVIEW(dev)) {
  11095. dev_priv->display.modeset_global_resources =
  11096. valleyview_modeset_global_resources;
  11097. }
  11098. switch (INTEL_INFO(dev)->gen) {
  11099. case 2:
  11100. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  11101. break;
  11102. case 3:
  11103. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  11104. break;
  11105. case 4:
  11106. case 5:
  11107. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  11108. break;
  11109. case 6:
  11110. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  11111. break;
  11112. case 7:
  11113. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  11114. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  11115. break;
  11116. case 9:
  11117. /* Drop through - unsupported since execlist only. */
  11118. default:
  11119. /* Default just returns -ENODEV to indicate unsupported */
  11120. dev_priv->display.queue_flip = intel_default_queue_flip;
  11121. }
  11122. intel_panel_init_backlight_funcs(dev);
  11123. mutex_init(&dev_priv->pps_mutex);
  11124. }
  11125. /*
  11126. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  11127. * resume, or other times. This quirk makes sure that's the case for
  11128. * affected systems.
  11129. */
  11130. static void quirk_pipea_force(struct drm_device *dev)
  11131. {
  11132. struct drm_i915_private *dev_priv = dev->dev_private;
  11133. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  11134. DRM_INFO("applying pipe a force quirk\n");
  11135. }
  11136. static void quirk_pipeb_force(struct drm_device *dev)
  11137. {
  11138. struct drm_i915_private *dev_priv = dev->dev_private;
  11139. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  11140. DRM_INFO("applying pipe b force quirk\n");
  11141. }
  11142. /*
  11143. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  11144. */
  11145. static void quirk_ssc_force_disable(struct drm_device *dev)
  11146. {
  11147. struct drm_i915_private *dev_priv = dev->dev_private;
  11148. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  11149. DRM_INFO("applying lvds SSC disable quirk\n");
  11150. }
  11151. /*
  11152. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  11153. * brightness value
  11154. */
  11155. static void quirk_invert_brightness(struct drm_device *dev)
  11156. {
  11157. struct drm_i915_private *dev_priv = dev->dev_private;
  11158. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  11159. DRM_INFO("applying inverted panel brightness quirk\n");
  11160. }
  11161. /* Some VBT's incorrectly indicate no backlight is present */
  11162. static void quirk_backlight_present(struct drm_device *dev)
  11163. {
  11164. struct drm_i915_private *dev_priv = dev->dev_private;
  11165. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  11166. DRM_INFO("applying backlight present quirk\n");
  11167. }
  11168. struct intel_quirk {
  11169. int device;
  11170. int subsystem_vendor;
  11171. int subsystem_device;
  11172. void (*hook)(struct drm_device *dev);
  11173. };
  11174. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  11175. struct intel_dmi_quirk {
  11176. void (*hook)(struct drm_device *dev);
  11177. const struct dmi_system_id (*dmi_id_list)[];
  11178. };
  11179. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  11180. {
  11181. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  11182. return 1;
  11183. }
  11184. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  11185. {
  11186. .dmi_id_list = &(const struct dmi_system_id[]) {
  11187. {
  11188. .callback = intel_dmi_reverse_brightness,
  11189. .ident = "NCR Corporation",
  11190. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  11191. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  11192. },
  11193. },
  11194. { } /* terminating entry */
  11195. },
  11196. .hook = quirk_invert_brightness,
  11197. },
  11198. };
  11199. static struct intel_quirk intel_quirks[] = {
  11200. /* HP Mini needs pipe A force quirk (LP: #322104) */
  11201. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  11202. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  11203. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  11204. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  11205. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  11206. /* 830 needs to leave pipe A & dpll A up */
  11207. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  11208. /* 830 needs to leave pipe B & dpll B up */
  11209. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  11210. /* Lenovo U160 cannot use SSC on LVDS */
  11211. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  11212. /* Sony Vaio Y cannot use SSC on LVDS */
  11213. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  11214. /* Acer Aspire 5734Z must invert backlight brightness */
  11215. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  11216. /* Acer/eMachines G725 */
  11217. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  11218. /* Acer/eMachines e725 */
  11219. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  11220. /* Acer/Packard Bell NCL20 */
  11221. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  11222. /* Acer Aspire 4736Z */
  11223. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  11224. /* Acer Aspire 5336 */
  11225. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  11226. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  11227. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  11228. /* Acer C720 Chromebook (Core i3 4005U) */
  11229. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  11230. /* Apple Macbook 2,1 (Core 2 T7400) */
  11231. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  11232. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  11233. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  11234. /* HP Chromebook 14 (Celeron 2955U) */
  11235. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  11236. /* Dell Chromebook 11 */
  11237. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  11238. };
  11239. static void intel_init_quirks(struct drm_device *dev)
  11240. {
  11241. struct pci_dev *d = dev->pdev;
  11242. int i;
  11243. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  11244. struct intel_quirk *q = &intel_quirks[i];
  11245. if (d->device == q->device &&
  11246. (d->subsystem_vendor == q->subsystem_vendor ||
  11247. q->subsystem_vendor == PCI_ANY_ID) &&
  11248. (d->subsystem_device == q->subsystem_device ||
  11249. q->subsystem_device == PCI_ANY_ID))
  11250. q->hook(dev);
  11251. }
  11252. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  11253. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  11254. intel_dmi_quirks[i].hook(dev);
  11255. }
  11256. }
  11257. /* Disable the VGA plane that we never use */
  11258. static void i915_disable_vga(struct drm_device *dev)
  11259. {
  11260. struct drm_i915_private *dev_priv = dev->dev_private;
  11261. u8 sr1;
  11262. u32 vga_reg = i915_vgacntrl_reg(dev);
  11263. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  11264. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  11265. outb(SR01, VGA_SR_INDEX);
  11266. sr1 = inb(VGA_SR_DATA);
  11267. outb(sr1 | 1<<5, VGA_SR_DATA);
  11268. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  11269. udelay(300);
  11270. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  11271. POSTING_READ(vga_reg);
  11272. }
  11273. void intel_modeset_init_hw(struct drm_device *dev)
  11274. {
  11275. intel_prepare_ddi(dev);
  11276. if (IS_VALLEYVIEW(dev))
  11277. vlv_update_cdclk(dev);
  11278. intel_init_clock_gating(dev);
  11279. intel_enable_gt_powersave(dev);
  11280. }
  11281. void intel_modeset_init(struct drm_device *dev)
  11282. {
  11283. struct drm_i915_private *dev_priv = dev->dev_private;
  11284. int sprite, ret;
  11285. enum pipe pipe;
  11286. struct intel_crtc *crtc;
  11287. drm_mode_config_init(dev);
  11288. dev->mode_config.min_width = 0;
  11289. dev->mode_config.min_height = 0;
  11290. dev->mode_config.preferred_depth = 24;
  11291. dev->mode_config.prefer_shadow = 1;
  11292. dev->mode_config.allow_fb_modifiers = true;
  11293. dev->mode_config.funcs = &intel_mode_funcs;
  11294. intel_init_quirks(dev);
  11295. intel_init_pm(dev);
  11296. if (INTEL_INFO(dev)->num_pipes == 0)
  11297. return;
  11298. intel_init_display(dev);
  11299. intel_init_audio(dev);
  11300. if (IS_GEN2(dev)) {
  11301. dev->mode_config.max_width = 2048;
  11302. dev->mode_config.max_height = 2048;
  11303. } else if (IS_GEN3(dev)) {
  11304. dev->mode_config.max_width = 4096;
  11305. dev->mode_config.max_height = 4096;
  11306. } else {
  11307. dev->mode_config.max_width = 8192;
  11308. dev->mode_config.max_height = 8192;
  11309. }
  11310. if (IS_845G(dev) || IS_I865G(dev)) {
  11311. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  11312. dev->mode_config.cursor_height = 1023;
  11313. } else if (IS_GEN2(dev)) {
  11314. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  11315. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  11316. } else {
  11317. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  11318. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  11319. }
  11320. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  11321. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  11322. INTEL_INFO(dev)->num_pipes,
  11323. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  11324. for_each_pipe(dev_priv, pipe) {
  11325. intel_crtc_init(dev, pipe);
  11326. for_each_sprite(dev_priv, pipe, sprite) {
  11327. ret = intel_plane_init(dev, pipe, sprite);
  11328. if (ret)
  11329. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  11330. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  11331. }
  11332. }
  11333. intel_init_dpio(dev);
  11334. intel_shared_dpll_init(dev);
  11335. /* Just disable it once at startup */
  11336. i915_disable_vga(dev);
  11337. intel_setup_outputs(dev);
  11338. /* Just in case the BIOS is doing something questionable. */
  11339. intel_fbc_disable(dev);
  11340. drm_modeset_lock_all(dev);
  11341. intel_modeset_setup_hw_state(dev, false);
  11342. drm_modeset_unlock_all(dev);
  11343. for_each_intel_crtc(dev, crtc) {
  11344. if (!crtc->active)
  11345. continue;
  11346. /*
  11347. * Note that reserving the BIOS fb up front prevents us
  11348. * from stuffing other stolen allocations like the ring
  11349. * on top. This prevents some ugliness at boot time, and
  11350. * can even allow for smooth boot transitions if the BIOS
  11351. * fb is large enough for the active pipe configuration.
  11352. */
  11353. if (dev_priv->display.get_initial_plane_config) {
  11354. dev_priv->display.get_initial_plane_config(crtc,
  11355. &crtc->plane_config);
  11356. /*
  11357. * If the fb is shared between multiple heads, we'll
  11358. * just get the first one.
  11359. */
  11360. intel_find_plane_obj(crtc, &crtc->plane_config);
  11361. }
  11362. }
  11363. }
  11364. static void intel_enable_pipe_a(struct drm_device *dev)
  11365. {
  11366. struct intel_connector *connector;
  11367. struct drm_connector *crt = NULL;
  11368. struct intel_load_detect_pipe load_detect_temp;
  11369. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  11370. /* We can't just switch on the pipe A, we need to set things up with a
  11371. * proper mode and output configuration. As a gross hack, enable pipe A
  11372. * by enabling the load detect pipe once. */
  11373. for_each_intel_connector(dev, connector) {
  11374. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  11375. crt = &connector->base;
  11376. break;
  11377. }
  11378. }
  11379. if (!crt)
  11380. return;
  11381. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  11382. intel_release_load_detect_pipe(crt, &load_detect_temp);
  11383. }
  11384. static bool
  11385. intel_check_plane_mapping(struct intel_crtc *crtc)
  11386. {
  11387. struct drm_device *dev = crtc->base.dev;
  11388. struct drm_i915_private *dev_priv = dev->dev_private;
  11389. u32 reg, val;
  11390. if (INTEL_INFO(dev)->num_pipes == 1)
  11391. return true;
  11392. reg = DSPCNTR(!crtc->plane);
  11393. val = I915_READ(reg);
  11394. if ((val & DISPLAY_PLANE_ENABLE) &&
  11395. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11396. return false;
  11397. return true;
  11398. }
  11399. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11400. {
  11401. struct drm_device *dev = crtc->base.dev;
  11402. struct drm_i915_private *dev_priv = dev->dev_private;
  11403. u32 reg;
  11404. /* Clear any frame start delays used for debugging left by the BIOS */
  11405. reg = PIPECONF(crtc->config->cpu_transcoder);
  11406. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11407. /* restore vblank interrupts to correct state */
  11408. drm_crtc_vblank_reset(&crtc->base);
  11409. if (crtc->active) {
  11410. update_scanline_offset(crtc);
  11411. drm_crtc_vblank_on(&crtc->base);
  11412. }
  11413. /* We need to sanitize the plane -> pipe mapping first because this will
  11414. * disable the crtc (and hence change the state) if it is wrong. Note
  11415. * that gen4+ has a fixed plane -> pipe mapping. */
  11416. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11417. struct intel_connector *connector;
  11418. bool plane;
  11419. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11420. crtc->base.base.id);
  11421. /* Pipe has the wrong plane attached and the plane is active.
  11422. * Temporarily change the plane mapping and disable everything
  11423. * ... */
  11424. plane = crtc->plane;
  11425. crtc->plane = !plane;
  11426. crtc->primary_enabled = true;
  11427. dev_priv->display.crtc_disable(&crtc->base);
  11428. crtc->plane = plane;
  11429. /* ... and break all links. */
  11430. for_each_intel_connector(dev, connector) {
  11431. if (connector->encoder->base.crtc != &crtc->base)
  11432. continue;
  11433. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11434. connector->base.encoder = NULL;
  11435. }
  11436. /* multiple connectors may have the same encoder:
  11437. * handle them and break crtc link separately */
  11438. for_each_intel_connector(dev, connector)
  11439. if (connector->encoder->base.crtc == &crtc->base) {
  11440. connector->encoder->base.crtc = NULL;
  11441. connector->encoder->connectors_active = false;
  11442. }
  11443. WARN_ON(crtc->active);
  11444. crtc->base.state->enable = false;
  11445. crtc->base.enabled = false;
  11446. }
  11447. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11448. crtc->pipe == PIPE_A && !crtc->active) {
  11449. /* BIOS forgot to enable pipe A, this mostly happens after
  11450. * resume. Force-enable the pipe to fix this, the update_dpms
  11451. * call below we restore the pipe to the right state, but leave
  11452. * the required bits on. */
  11453. intel_enable_pipe_a(dev);
  11454. }
  11455. /* Adjust the state of the output pipe according to whether we
  11456. * have active connectors/encoders. */
  11457. intel_crtc_update_dpms(&crtc->base);
  11458. if (crtc->active != crtc->base.state->enable) {
  11459. struct intel_encoder *encoder;
  11460. /* This can happen either due to bugs in the get_hw_state
  11461. * functions or because the pipe is force-enabled due to the
  11462. * pipe A quirk. */
  11463. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11464. crtc->base.base.id,
  11465. crtc->base.state->enable ? "enabled" : "disabled",
  11466. crtc->active ? "enabled" : "disabled");
  11467. crtc->base.state->enable = crtc->active;
  11468. crtc->base.enabled = crtc->active;
  11469. /* Because we only establish the connector -> encoder ->
  11470. * crtc links if something is active, this means the
  11471. * crtc is now deactivated. Break the links. connector
  11472. * -> encoder links are only establish when things are
  11473. * actually up, hence no need to break them. */
  11474. WARN_ON(crtc->active);
  11475. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11476. WARN_ON(encoder->connectors_active);
  11477. encoder->base.crtc = NULL;
  11478. }
  11479. }
  11480. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11481. /*
  11482. * We start out with underrun reporting disabled to avoid races.
  11483. * For correct bookkeeping mark this on active crtcs.
  11484. *
  11485. * Also on gmch platforms we dont have any hardware bits to
  11486. * disable the underrun reporting. Which means we need to start
  11487. * out with underrun reporting disabled also on inactive pipes,
  11488. * since otherwise we'll complain about the garbage we read when
  11489. * e.g. coming up after runtime pm.
  11490. *
  11491. * No protection against concurrent access is required - at
  11492. * worst a fifo underrun happens which also sets this to false.
  11493. */
  11494. crtc->cpu_fifo_underrun_disabled = true;
  11495. crtc->pch_fifo_underrun_disabled = true;
  11496. }
  11497. }
  11498. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11499. {
  11500. struct intel_connector *connector;
  11501. struct drm_device *dev = encoder->base.dev;
  11502. /* We need to check both for a crtc link (meaning that the
  11503. * encoder is active and trying to read from a pipe) and the
  11504. * pipe itself being active. */
  11505. bool has_active_crtc = encoder->base.crtc &&
  11506. to_intel_crtc(encoder->base.crtc)->active;
  11507. if (encoder->connectors_active && !has_active_crtc) {
  11508. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11509. encoder->base.base.id,
  11510. encoder->base.name);
  11511. /* Connector is active, but has no active pipe. This is
  11512. * fallout from our resume register restoring. Disable
  11513. * the encoder manually again. */
  11514. if (encoder->base.crtc) {
  11515. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11516. encoder->base.base.id,
  11517. encoder->base.name);
  11518. encoder->disable(encoder);
  11519. if (encoder->post_disable)
  11520. encoder->post_disable(encoder);
  11521. }
  11522. encoder->base.crtc = NULL;
  11523. encoder->connectors_active = false;
  11524. /* Inconsistent output/port/pipe state happens presumably due to
  11525. * a bug in one of the get_hw_state functions. Or someplace else
  11526. * in our code, like the register restore mess on resume. Clamp
  11527. * things to off as a safer default. */
  11528. for_each_intel_connector(dev, connector) {
  11529. if (connector->encoder != encoder)
  11530. continue;
  11531. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11532. connector->base.encoder = NULL;
  11533. }
  11534. }
  11535. /* Enabled encoders without active connectors will be fixed in
  11536. * the crtc fixup. */
  11537. }
  11538. void i915_redisable_vga_power_on(struct drm_device *dev)
  11539. {
  11540. struct drm_i915_private *dev_priv = dev->dev_private;
  11541. u32 vga_reg = i915_vgacntrl_reg(dev);
  11542. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11543. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11544. i915_disable_vga(dev);
  11545. }
  11546. }
  11547. void i915_redisable_vga(struct drm_device *dev)
  11548. {
  11549. struct drm_i915_private *dev_priv = dev->dev_private;
  11550. /* This function can be called both from intel_modeset_setup_hw_state or
  11551. * at a very early point in our resume sequence, where the power well
  11552. * structures are not yet restored. Since this function is at a very
  11553. * paranoid "someone might have enabled VGA while we were not looking"
  11554. * level, just check if the power well is enabled instead of trying to
  11555. * follow the "don't touch the power well if we don't need it" policy
  11556. * the rest of the driver uses. */
  11557. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11558. return;
  11559. i915_redisable_vga_power_on(dev);
  11560. }
  11561. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11562. {
  11563. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11564. if (!crtc->active)
  11565. return false;
  11566. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11567. }
  11568. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11569. {
  11570. struct drm_i915_private *dev_priv = dev->dev_private;
  11571. enum pipe pipe;
  11572. struct intel_crtc *crtc;
  11573. struct intel_encoder *encoder;
  11574. struct intel_connector *connector;
  11575. int i;
  11576. for_each_intel_crtc(dev, crtc) {
  11577. memset(crtc->config, 0, sizeof(*crtc->config));
  11578. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11579. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11580. crtc->config);
  11581. crtc->base.state->enable = crtc->active;
  11582. crtc->base.enabled = crtc->active;
  11583. crtc->primary_enabled = primary_get_hw_state(crtc);
  11584. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11585. crtc->base.base.id,
  11586. crtc->active ? "enabled" : "disabled");
  11587. }
  11588. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11589. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11590. pll->on = pll->get_hw_state(dev_priv, pll,
  11591. &pll->config.hw_state);
  11592. pll->active = 0;
  11593. pll->config.crtc_mask = 0;
  11594. for_each_intel_crtc(dev, crtc) {
  11595. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11596. pll->active++;
  11597. pll->config.crtc_mask |= 1 << crtc->pipe;
  11598. }
  11599. }
  11600. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11601. pll->name, pll->config.crtc_mask, pll->on);
  11602. if (pll->config.crtc_mask)
  11603. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11604. }
  11605. for_each_intel_encoder(dev, encoder) {
  11606. pipe = 0;
  11607. if (encoder->get_hw_state(encoder, &pipe)) {
  11608. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11609. encoder->base.crtc = &crtc->base;
  11610. encoder->get_config(encoder, crtc->config);
  11611. } else {
  11612. encoder->base.crtc = NULL;
  11613. }
  11614. encoder->connectors_active = false;
  11615. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11616. encoder->base.base.id,
  11617. encoder->base.name,
  11618. encoder->base.crtc ? "enabled" : "disabled",
  11619. pipe_name(pipe));
  11620. }
  11621. for_each_intel_connector(dev, connector) {
  11622. if (connector->get_hw_state(connector)) {
  11623. connector->base.dpms = DRM_MODE_DPMS_ON;
  11624. connector->encoder->connectors_active = true;
  11625. connector->base.encoder = &connector->encoder->base;
  11626. } else {
  11627. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11628. connector->base.encoder = NULL;
  11629. }
  11630. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11631. connector->base.base.id,
  11632. connector->base.name,
  11633. connector->base.encoder ? "enabled" : "disabled");
  11634. }
  11635. }
  11636. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11637. * and i915 state tracking structures. */
  11638. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11639. bool force_restore)
  11640. {
  11641. struct drm_i915_private *dev_priv = dev->dev_private;
  11642. enum pipe pipe;
  11643. struct intel_crtc *crtc;
  11644. struct intel_encoder *encoder;
  11645. int i;
  11646. intel_modeset_readout_hw_state(dev);
  11647. /*
  11648. * Now that we have the config, copy it to each CRTC struct
  11649. * Note that this could go away if we move to using crtc_config
  11650. * checking everywhere.
  11651. */
  11652. for_each_intel_crtc(dev, crtc) {
  11653. if (crtc->active && i915.fastboot) {
  11654. intel_mode_from_pipe_config(&crtc->base.mode,
  11655. crtc->config);
  11656. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11657. crtc->base.base.id);
  11658. drm_mode_debug_printmodeline(&crtc->base.mode);
  11659. }
  11660. }
  11661. /* HW state is read out, now we need to sanitize this mess. */
  11662. for_each_intel_encoder(dev, encoder) {
  11663. intel_sanitize_encoder(encoder);
  11664. }
  11665. for_each_pipe(dev_priv, pipe) {
  11666. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11667. intel_sanitize_crtc(crtc);
  11668. intel_dump_pipe_config(crtc, crtc->config,
  11669. "[setup_hw_state]");
  11670. }
  11671. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11672. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11673. if (!pll->on || pll->active)
  11674. continue;
  11675. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11676. pll->disable(dev_priv, pll);
  11677. pll->on = false;
  11678. }
  11679. if (IS_GEN9(dev))
  11680. skl_wm_get_hw_state(dev);
  11681. else if (HAS_PCH_SPLIT(dev))
  11682. ilk_wm_get_hw_state(dev);
  11683. if (force_restore) {
  11684. i915_redisable_vga(dev);
  11685. /*
  11686. * We need to use raw interfaces for restoring state to avoid
  11687. * checking (bogus) intermediate states.
  11688. */
  11689. for_each_pipe(dev_priv, pipe) {
  11690. struct drm_crtc *crtc =
  11691. dev_priv->pipe_to_crtc_mapping[pipe];
  11692. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11693. crtc->primary->fb);
  11694. }
  11695. } else {
  11696. intel_modeset_update_staged_output_state(dev);
  11697. }
  11698. intel_modeset_check_state(dev);
  11699. }
  11700. void intel_modeset_gem_init(struct drm_device *dev)
  11701. {
  11702. struct drm_i915_private *dev_priv = dev->dev_private;
  11703. struct drm_crtc *c;
  11704. struct drm_i915_gem_object *obj;
  11705. mutex_lock(&dev->struct_mutex);
  11706. intel_init_gt_powersave(dev);
  11707. mutex_unlock(&dev->struct_mutex);
  11708. /*
  11709. * There may be no VBT; and if the BIOS enabled SSC we can
  11710. * just keep using it to avoid unnecessary flicker. Whereas if the
  11711. * BIOS isn't using it, don't assume it will work even if the VBT
  11712. * indicates as much.
  11713. */
  11714. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11715. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11716. DREF_SSC1_ENABLE);
  11717. intel_modeset_init_hw(dev);
  11718. intel_setup_overlay(dev);
  11719. /*
  11720. * Make sure any fbs we allocated at startup are properly
  11721. * pinned & fenced. When we do the allocation it's too early
  11722. * for this.
  11723. */
  11724. mutex_lock(&dev->struct_mutex);
  11725. for_each_crtc(dev, c) {
  11726. obj = intel_fb_obj(c->primary->fb);
  11727. if (obj == NULL)
  11728. continue;
  11729. if (intel_pin_and_fence_fb_obj(c->primary,
  11730. c->primary->fb,
  11731. c->primary->state,
  11732. NULL)) {
  11733. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11734. to_intel_crtc(c)->pipe);
  11735. drm_framebuffer_unreference(c->primary->fb);
  11736. c->primary->fb = NULL;
  11737. update_state_fb(c->primary);
  11738. }
  11739. }
  11740. mutex_unlock(&dev->struct_mutex);
  11741. intel_backlight_register(dev);
  11742. }
  11743. void intel_connector_unregister(struct intel_connector *intel_connector)
  11744. {
  11745. struct drm_connector *connector = &intel_connector->base;
  11746. intel_panel_destroy_backlight(connector);
  11747. drm_connector_unregister(connector);
  11748. }
  11749. void intel_modeset_cleanup(struct drm_device *dev)
  11750. {
  11751. struct drm_i915_private *dev_priv = dev->dev_private;
  11752. struct drm_connector *connector;
  11753. intel_disable_gt_powersave(dev);
  11754. intel_backlight_unregister(dev);
  11755. /*
  11756. * Interrupts and polling as the first thing to avoid creating havoc.
  11757. * Too much stuff here (turning of connectors, ...) would
  11758. * experience fancy races otherwise.
  11759. */
  11760. intel_irq_uninstall(dev_priv);
  11761. /*
  11762. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11763. * poll handlers. Hence disable polling after hpd handling is shut down.
  11764. */
  11765. drm_kms_helper_poll_fini(dev);
  11766. mutex_lock(&dev->struct_mutex);
  11767. intel_unregister_dsm_handler();
  11768. intel_fbc_disable(dev);
  11769. mutex_unlock(&dev->struct_mutex);
  11770. /* flush any delayed tasks or pending work */
  11771. flush_scheduled_work();
  11772. /* destroy the backlight and sysfs files before encoders/connectors */
  11773. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11774. struct intel_connector *intel_connector;
  11775. intel_connector = to_intel_connector(connector);
  11776. intel_connector->unregister(intel_connector);
  11777. }
  11778. drm_mode_config_cleanup(dev);
  11779. intel_cleanup_overlay(dev);
  11780. mutex_lock(&dev->struct_mutex);
  11781. intel_cleanup_gt_powersave(dev);
  11782. mutex_unlock(&dev->struct_mutex);
  11783. }
  11784. /*
  11785. * Return which encoder is currently attached for connector.
  11786. */
  11787. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11788. {
  11789. return &intel_attached_encoder(connector)->base;
  11790. }
  11791. void intel_connector_attach_encoder(struct intel_connector *connector,
  11792. struct intel_encoder *encoder)
  11793. {
  11794. connector->encoder = encoder;
  11795. drm_mode_connector_attach_encoder(&connector->base,
  11796. &encoder->base);
  11797. }
  11798. /*
  11799. * set vga decode state - true == enable VGA decode
  11800. */
  11801. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11802. {
  11803. struct drm_i915_private *dev_priv = dev->dev_private;
  11804. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11805. u16 gmch_ctrl;
  11806. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11807. DRM_ERROR("failed to read control word\n");
  11808. return -EIO;
  11809. }
  11810. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11811. return 0;
  11812. if (state)
  11813. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11814. else
  11815. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11816. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11817. DRM_ERROR("failed to write control word\n");
  11818. return -EIO;
  11819. }
  11820. return 0;
  11821. }
  11822. struct intel_display_error_state {
  11823. u32 power_well_driver;
  11824. int num_transcoders;
  11825. struct intel_cursor_error_state {
  11826. u32 control;
  11827. u32 position;
  11828. u32 base;
  11829. u32 size;
  11830. } cursor[I915_MAX_PIPES];
  11831. struct intel_pipe_error_state {
  11832. bool power_domain_on;
  11833. u32 source;
  11834. u32 stat;
  11835. } pipe[I915_MAX_PIPES];
  11836. struct intel_plane_error_state {
  11837. u32 control;
  11838. u32 stride;
  11839. u32 size;
  11840. u32 pos;
  11841. u32 addr;
  11842. u32 surface;
  11843. u32 tile_offset;
  11844. } plane[I915_MAX_PIPES];
  11845. struct intel_transcoder_error_state {
  11846. bool power_domain_on;
  11847. enum transcoder cpu_transcoder;
  11848. u32 conf;
  11849. u32 htotal;
  11850. u32 hblank;
  11851. u32 hsync;
  11852. u32 vtotal;
  11853. u32 vblank;
  11854. u32 vsync;
  11855. } transcoder[4];
  11856. };
  11857. struct intel_display_error_state *
  11858. intel_display_capture_error_state(struct drm_device *dev)
  11859. {
  11860. struct drm_i915_private *dev_priv = dev->dev_private;
  11861. struct intel_display_error_state *error;
  11862. int transcoders[] = {
  11863. TRANSCODER_A,
  11864. TRANSCODER_B,
  11865. TRANSCODER_C,
  11866. TRANSCODER_EDP,
  11867. };
  11868. int i;
  11869. if (INTEL_INFO(dev)->num_pipes == 0)
  11870. return NULL;
  11871. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11872. if (error == NULL)
  11873. return NULL;
  11874. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11875. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11876. for_each_pipe(dev_priv, i) {
  11877. error->pipe[i].power_domain_on =
  11878. __intel_display_power_is_enabled(dev_priv,
  11879. POWER_DOMAIN_PIPE(i));
  11880. if (!error->pipe[i].power_domain_on)
  11881. continue;
  11882. error->cursor[i].control = I915_READ(CURCNTR(i));
  11883. error->cursor[i].position = I915_READ(CURPOS(i));
  11884. error->cursor[i].base = I915_READ(CURBASE(i));
  11885. error->plane[i].control = I915_READ(DSPCNTR(i));
  11886. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11887. if (INTEL_INFO(dev)->gen <= 3) {
  11888. error->plane[i].size = I915_READ(DSPSIZE(i));
  11889. error->plane[i].pos = I915_READ(DSPPOS(i));
  11890. }
  11891. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11892. error->plane[i].addr = I915_READ(DSPADDR(i));
  11893. if (INTEL_INFO(dev)->gen >= 4) {
  11894. error->plane[i].surface = I915_READ(DSPSURF(i));
  11895. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11896. }
  11897. error->pipe[i].source = I915_READ(PIPESRC(i));
  11898. if (HAS_GMCH_DISPLAY(dev))
  11899. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11900. }
  11901. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11902. if (HAS_DDI(dev_priv->dev))
  11903. error->num_transcoders++; /* Account for eDP. */
  11904. for (i = 0; i < error->num_transcoders; i++) {
  11905. enum transcoder cpu_transcoder = transcoders[i];
  11906. error->transcoder[i].power_domain_on =
  11907. __intel_display_power_is_enabled(dev_priv,
  11908. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11909. if (!error->transcoder[i].power_domain_on)
  11910. continue;
  11911. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11912. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11913. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11914. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11915. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11916. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11917. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11918. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11919. }
  11920. return error;
  11921. }
  11922. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11923. void
  11924. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11925. struct drm_device *dev,
  11926. struct intel_display_error_state *error)
  11927. {
  11928. struct drm_i915_private *dev_priv = dev->dev_private;
  11929. int i;
  11930. if (!error)
  11931. return;
  11932. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11933. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11934. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11935. error->power_well_driver);
  11936. for_each_pipe(dev_priv, i) {
  11937. err_printf(m, "Pipe [%d]:\n", i);
  11938. err_printf(m, " Power: %s\n",
  11939. error->pipe[i].power_domain_on ? "on" : "off");
  11940. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11941. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11942. err_printf(m, "Plane [%d]:\n", i);
  11943. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11944. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11945. if (INTEL_INFO(dev)->gen <= 3) {
  11946. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11947. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11948. }
  11949. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11950. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11951. if (INTEL_INFO(dev)->gen >= 4) {
  11952. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11953. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11954. }
  11955. err_printf(m, "Cursor [%d]:\n", i);
  11956. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11957. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11958. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11959. }
  11960. for (i = 0; i < error->num_transcoders; i++) {
  11961. err_printf(m, "CPU transcoder: %c\n",
  11962. transcoder_name(error->transcoder[i].cpu_transcoder));
  11963. err_printf(m, " Power: %s\n",
  11964. error->transcoder[i].power_domain_on ? "on" : "off");
  11965. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11966. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11967. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11968. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11969. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11970. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11971. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11972. }
  11973. }
  11974. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11975. {
  11976. struct intel_crtc *crtc;
  11977. for_each_intel_crtc(dev, crtc) {
  11978. struct intel_unpin_work *work;
  11979. spin_lock_irq(&dev->event_lock);
  11980. work = crtc->unpin_work;
  11981. if (work && work->event &&
  11982. work->event->base.file_priv == file) {
  11983. kfree(work->event);
  11984. work->event = NULL;
  11985. }
  11986. spin_unlock_irq(&dev->event_lock);
  11987. }
  11988. }