intel_display.c 383 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847
  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_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <drm/drm_plane_helper.h>
  42. #include <drm/drm_rect.h>
  43. #include <linux/dma_remapping.h>
  44. /* Primary plane formats supported by all gen */
  45. #define COMMON_PRIMARY_FORMATS \
  46. DRM_FORMAT_C8, \
  47. DRM_FORMAT_RGB565, \
  48. DRM_FORMAT_XRGB8888, \
  49. DRM_FORMAT_ARGB8888
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t intel_primary_formats_gen2[] = {
  52. COMMON_PRIMARY_FORMATS,
  53. DRM_FORMAT_XRGB1555,
  54. DRM_FORMAT_ARGB1555,
  55. };
  56. /* Primary plane formats for gen >= 4 */
  57. static const uint32_t intel_primary_formats_gen4[] = {
  58. COMMON_PRIMARY_FORMATS, \
  59. DRM_FORMAT_XBGR8888,
  60. DRM_FORMAT_ABGR8888,
  61. DRM_FORMAT_XRGB2101010,
  62. DRM_FORMAT_ARGB2101010,
  63. DRM_FORMAT_XBGR2101010,
  64. DRM_FORMAT_ABGR2101010,
  65. };
  66. /* Cursor formats */
  67. static const uint32_t intel_cursor_formats[] = {
  68. DRM_FORMAT_ARGB8888,
  69. };
  70. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  71. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  72. struct intel_crtc_config *pipe_config);
  73. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_config *pipe_config);
  75. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  76. int x, int y, struct drm_framebuffer *old_fb);
  77. static int intel_framebuffer_init(struct drm_device *dev,
  78. struct intel_framebuffer *ifb,
  79. struct drm_mode_fb_cmd2 *mode_cmd,
  80. struct drm_i915_gem_object *obj);
  81. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  82. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  83. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  84. struct intel_link_m_n *m_n,
  85. struct intel_link_m_n *m2_n2);
  86. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  87. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  88. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  89. static void vlv_prepare_pll(struct intel_crtc *crtc,
  90. const struct intel_crtc_config *pipe_config);
  91. static void chv_prepare_pll(struct intel_crtc *crtc,
  92. const struct intel_crtc_config *pipe_config);
  93. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  94. {
  95. if (!connector->mst_port)
  96. return connector->encoder;
  97. else
  98. return &connector->mst_port->mst_encoders[pipe]->base;
  99. }
  100. typedef struct {
  101. int min, max;
  102. } intel_range_t;
  103. typedef struct {
  104. int dot_limit;
  105. int p2_slow, p2_fast;
  106. } intel_p2_t;
  107. typedef struct intel_limit intel_limit_t;
  108. struct intel_limit {
  109. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  110. intel_p2_t p2;
  111. };
  112. int
  113. intel_pch_rawclk(struct drm_device *dev)
  114. {
  115. struct drm_i915_private *dev_priv = dev->dev_private;
  116. WARN_ON(!HAS_PCH_SPLIT(dev));
  117. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  118. }
  119. static inline u32 /* units of 100MHz */
  120. intel_fdi_link_freq(struct drm_device *dev)
  121. {
  122. if (IS_GEN5(dev)) {
  123. struct drm_i915_private *dev_priv = dev->dev_private;
  124. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  125. } else
  126. return 27;
  127. }
  128. static const intel_limit_t intel_limits_i8xx_dac = {
  129. .dot = { .min = 25000, .max = 350000 },
  130. .vco = { .min = 908000, .max = 1512000 },
  131. .n = { .min = 2, .max = 16 },
  132. .m = { .min = 96, .max = 140 },
  133. .m1 = { .min = 18, .max = 26 },
  134. .m2 = { .min = 6, .max = 16 },
  135. .p = { .min = 4, .max = 128 },
  136. .p1 = { .min = 2, .max = 33 },
  137. .p2 = { .dot_limit = 165000,
  138. .p2_slow = 4, .p2_fast = 2 },
  139. };
  140. static const intel_limit_t intel_limits_i8xx_dvo = {
  141. .dot = { .min = 25000, .max = 350000 },
  142. .vco = { .min = 908000, .max = 1512000 },
  143. .n = { .min = 2, .max = 16 },
  144. .m = { .min = 96, .max = 140 },
  145. .m1 = { .min = 18, .max = 26 },
  146. .m2 = { .min = 6, .max = 16 },
  147. .p = { .min = 4, .max = 128 },
  148. .p1 = { .min = 2, .max = 33 },
  149. .p2 = { .dot_limit = 165000,
  150. .p2_slow = 4, .p2_fast = 4 },
  151. };
  152. static const intel_limit_t intel_limits_i8xx_lvds = {
  153. .dot = { .min = 25000, .max = 350000 },
  154. .vco = { .min = 908000, .max = 1512000 },
  155. .n = { .min = 2, .max = 16 },
  156. .m = { .min = 96, .max = 140 },
  157. .m1 = { .min = 18, .max = 26 },
  158. .m2 = { .min = 6, .max = 16 },
  159. .p = { .min = 4, .max = 128 },
  160. .p1 = { .min = 1, .max = 6 },
  161. .p2 = { .dot_limit = 165000,
  162. .p2_slow = 14, .p2_fast = 7 },
  163. };
  164. static const intel_limit_t intel_limits_i9xx_sdvo = {
  165. .dot = { .min = 20000, .max = 400000 },
  166. .vco = { .min = 1400000, .max = 2800000 },
  167. .n = { .min = 1, .max = 6 },
  168. .m = { .min = 70, .max = 120 },
  169. .m1 = { .min = 8, .max = 18 },
  170. .m2 = { .min = 3, .max = 7 },
  171. .p = { .min = 5, .max = 80 },
  172. .p1 = { .min = 1, .max = 8 },
  173. .p2 = { .dot_limit = 200000,
  174. .p2_slow = 10, .p2_fast = 5 },
  175. };
  176. static const intel_limit_t intel_limits_i9xx_lvds = {
  177. .dot = { .min = 20000, .max = 400000 },
  178. .vco = { .min = 1400000, .max = 2800000 },
  179. .n = { .min = 1, .max = 6 },
  180. .m = { .min = 70, .max = 120 },
  181. .m1 = { .min = 8, .max = 18 },
  182. .m2 = { .min = 3, .max = 7 },
  183. .p = { .min = 7, .max = 98 },
  184. .p1 = { .min = 1, .max = 8 },
  185. .p2 = { .dot_limit = 112000,
  186. .p2_slow = 14, .p2_fast = 7 },
  187. };
  188. static const intel_limit_t intel_limits_g4x_sdvo = {
  189. .dot = { .min = 25000, .max = 270000 },
  190. .vco = { .min = 1750000, .max = 3500000},
  191. .n = { .min = 1, .max = 4 },
  192. .m = { .min = 104, .max = 138 },
  193. .m1 = { .min = 17, .max = 23 },
  194. .m2 = { .min = 5, .max = 11 },
  195. .p = { .min = 10, .max = 30 },
  196. .p1 = { .min = 1, .max = 3},
  197. .p2 = { .dot_limit = 270000,
  198. .p2_slow = 10,
  199. .p2_fast = 10
  200. },
  201. };
  202. static const intel_limit_t intel_limits_g4x_hdmi = {
  203. .dot = { .min = 22000, .max = 400000 },
  204. .vco = { .min = 1750000, .max = 3500000},
  205. .n = { .min = 1, .max = 4 },
  206. .m = { .min = 104, .max = 138 },
  207. .m1 = { .min = 16, .max = 23 },
  208. .m2 = { .min = 5, .max = 11 },
  209. .p = { .min = 5, .max = 80 },
  210. .p1 = { .min = 1, .max = 8},
  211. .p2 = { .dot_limit = 165000,
  212. .p2_slow = 10, .p2_fast = 5 },
  213. };
  214. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  215. .dot = { .min = 20000, .max = 115000 },
  216. .vco = { .min = 1750000, .max = 3500000 },
  217. .n = { .min = 1, .max = 3 },
  218. .m = { .min = 104, .max = 138 },
  219. .m1 = { .min = 17, .max = 23 },
  220. .m2 = { .min = 5, .max = 11 },
  221. .p = { .min = 28, .max = 112 },
  222. .p1 = { .min = 2, .max = 8 },
  223. .p2 = { .dot_limit = 0,
  224. .p2_slow = 14, .p2_fast = 14
  225. },
  226. };
  227. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  228. .dot = { .min = 80000, .max = 224000 },
  229. .vco = { .min = 1750000, .max = 3500000 },
  230. .n = { .min = 1, .max = 3 },
  231. .m = { .min = 104, .max = 138 },
  232. .m1 = { .min = 17, .max = 23 },
  233. .m2 = { .min = 5, .max = 11 },
  234. .p = { .min = 14, .max = 42 },
  235. .p1 = { .min = 2, .max = 6 },
  236. .p2 = { .dot_limit = 0,
  237. .p2_slow = 7, .p2_fast = 7
  238. },
  239. };
  240. static const intel_limit_t intel_limits_pineview_sdvo = {
  241. .dot = { .min = 20000, .max = 400000},
  242. .vco = { .min = 1700000, .max = 3500000 },
  243. /* Pineview's Ncounter is a ring counter */
  244. .n = { .min = 3, .max = 6 },
  245. .m = { .min = 2, .max = 256 },
  246. /* Pineview only has one combined m divider, which we treat as m2. */
  247. .m1 = { .min = 0, .max = 0 },
  248. .m2 = { .min = 0, .max = 254 },
  249. .p = { .min = 5, .max = 80 },
  250. .p1 = { .min = 1, .max = 8 },
  251. .p2 = { .dot_limit = 200000,
  252. .p2_slow = 10, .p2_fast = 5 },
  253. };
  254. static const intel_limit_t intel_limits_pineview_lvds = {
  255. .dot = { .min = 20000, .max = 400000 },
  256. .vco = { .min = 1700000, .max = 3500000 },
  257. .n = { .min = 3, .max = 6 },
  258. .m = { .min = 2, .max = 256 },
  259. .m1 = { .min = 0, .max = 0 },
  260. .m2 = { .min = 0, .max = 254 },
  261. .p = { .min = 7, .max = 112 },
  262. .p1 = { .min = 1, .max = 8 },
  263. .p2 = { .dot_limit = 112000,
  264. .p2_slow = 14, .p2_fast = 14 },
  265. };
  266. /* Ironlake / Sandybridge
  267. *
  268. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  269. * the range value for them is (actual_value - 2).
  270. */
  271. static const intel_limit_t intel_limits_ironlake_dac = {
  272. .dot = { .min = 25000, .max = 350000 },
  273. .vco = { .min = 1760000, .max = 3510000 },
  274. .n = { .min = 1, .max = 5 },
  275. .m = { .min = 79, .max = 127 },
  276. .m1 = { .min = 12, .max = 22 },
  277. .m2 = { .min = 5, .max = 9 },
  278. .p = { .min = 5, .max = 80 },
  279. .p1 = { .min = 1, .max = 8 },
  280. .p2 = { .dot_limit = 225000,
  281. .p2_slow = 10, .p2_fast = 5 },
  282. };
  283. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  284. .dot = { .min = 25000, .max = 350000 },
  285. .vco = { .min = 1760000, .max = 3510000 },
  286. .n = { .min = 1, .max = 3 },
  287. .m = { .min = 79, .max = 118 },
  288. .m1 = { .min = 12, .max = 22 },
  289. .m2 = { .min = 5, .max = 9 },
  290. .p = { .min = 28, .max = 112 },
  291. .p1 = { .min = 2, .max = 8 },
  292. .p2 = { .dot_limit = 225000,
  293. .p2_slow = 14, .p2_fast = 14 },
  294. };
  295. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  296. .dot = { .min = 25000, .max = 350000 },
  297. .vco = { .min = 1760000, .max = 3510000 },
  298. .n = { .min = 1, .max = 3 },
  299. .m = { .min = 79, .max = 127 },
  300. .m1 = { .min = 12, .max = 22 },
  301. .m2 = { .min = 5, .max = 9 },
  302. .p = { .min = 14, .max = 56 },
  303. .p1 = { .min = 2, .max = 8 },
  304. .p2 = { .dot_limit = 225000,
  305. .p2_slow = 7, .p2_fast = 7 },
  306. };
  307. /* LVDS 100mhz refclk limits. */
  308. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  309. .dot = { .min = 25000, .max = 350000 },
  310. .vco = { .min = 1760000, .max = 3510000 },
  311. .n = { .min = 1, .max = 2 },
  312. .m = { .min = 79, .max = 126 },
  313. .m1 = { .min = 12, .max = 22 },
  314. .m2 = { .min = 5, .max = 9 },
  315. .p = { .min = 28, .max = 112 },
  316. .p1 = { .min = 2, .max = 8 },
  317. .p2 = { .dot_limit = 225000,
  318. .p2_slow = 14, .p2_fast = 14 },
  319. };
  320. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  321. .dot = { .min = 25000, .max = 350000 },
  322. .vco = { .min = 1760000, .max = 3510000 },
  323. .n = { .min = 1, .max = 3 },
  324. .m = { .min = 79, .max = 126 },
  325. .m1 = { .min = 12, .max = 22 },
  326. .m2 = { .min = 5, .max = 9 },
  327. .p = { .min = 14, .max = 42 },
  328. .p1 = { .min = 2, .max = 6 },
  329. .p2 = { .dot_limit = 225000,
  330. .p2_slow = 7, .p2_fast = 7 },
  331. };
  332. static const intel_limit_t intel_limits_vlv = {
  333. /*
  334. * These are the data rate limits (measured in fast clocks)
  335. * since those are the strictest limits we have. The fast
  336. * clock and actual rate limits are more relaxed, so checking
  337. * them would make no difference.
  338. */
  339. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  340. .vco = { .min = 4000000, .max = 6000000 },
  341. .n = { .min = 1, .max = 7 },
  342. .m1 = { .min = 2, .max = 3 },
  343. .m2 = { .min = 11, .max = 156 },
  344. .p1 = { .min = 2, .max = 3 },
  345. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  346. };
  347. static const intel_limit_t intel_limits_chv = {
  348. /*
  349. * These are the data rate limits (measured in fast clocks)
  350. * since those are the strictest limits we have. The fast
  351. * clock and actual rate limits are more relaxed, so checking
  352. * them would make no difference.
  353. */
  354. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  355. .vco = { .min = 4860000, .max = 6700000 },
  356. .n = { .min = 1, .max = 1 },
  357. .m1 = { .min = 2, .max = 2 },
  358. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  359. .p1 = { .min = 2, .max = 4 },
  360. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  361. };
  362. static void vlv_clock(int refclk, intel_clock_t *clock)
  363. {
  364. clock->m = clock->m1 * clock->m2;
  365. clock->p = clock->p1 * clock->p2;
  366. if (WARN_ON(clock->n == 0 || clock->p == 0))
  367. return;
  368. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  369. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  370. }
  371. /**
  372. * Returns whether any output on the specified pipe is of the specified type
  373. */
  374. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  375. {
  376. struct drm_device *dev = crtc->base.dev;
  377. struct intel_encoder *encoder;
  378. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  379. if (encoder->type == type)
  380. return true;
  381. return false;
  382. }
  383. /**
  384. * Returns whether any output on the specified pipe will have the specified
  385. * type after a staged modeset is complete, i.e., the same as
  386. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  387. * encoder->crtc.
  388. */
  389. static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
  390. {
  391. struct drm_device *dev = crtc->base.dev;
  392. struct intel_encoder *encoder;
  393. for_each_intel_encoder(dev, encoder)
  394. if (encoder->new_crtc == crtc && encoder->type == type)
  395. return true;
  396. return false;
  397. }
  398. static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
  399. int refclk)
  400. {
  401. struct drm_device *dev = crtc->base.dev;
  402. const intel_limit_t *limit;
  403. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  404. if (intel_is_dual_link_lvds(dev)) {
  405. if (refclk == 100000)
  406. limit = &intel_limits_ironlake_dual_lvds_100m;
  407. else
  408. limit = &intel_limits_ironlake_dual_lvds;
  409. } else {
  410. if (refclk == 100000)
  411. limit = &intel_limits_ironlake_single_lvds_100m;
  412. else
  413. limit = &intel_limits_ironlake_single_lvds;
  414. }
  415. } else
  416. limit = &intel_limits_ironlake_dac;
  417. return limit;
  418. }
  419. static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
  420. {
  421. struct drm_device *dev = crtc->base.dev;
  422. const intel_limit_t *limit;
  423. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  424. if (intel_is_dual_link_lvds(dev))
  425. limit = &intel_limits_g4x_dual_channel_lvds;
  426. else
  427. limit = &intel_limits_g4x_single_channel_lvds;
  428. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
  429. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
  430. limit = &intel_limits_g4x_hdmi;
  431. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
  432. limit = &intel_limits_g4x_sdvo;
  433. } else /* The option is for other outputs */
  434. limit = &intel_limits_i9xx_sdvo;
  435. return limit;
  436. }
  437. static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
  438. {
  439. struct drm_device *dev = crtc->base.dev;
  440. const intel_limit_t *limit;
  441. if (HAS_PCH_SPLIT(dev))
  442. limit = intel_ironlake_limit(crtc, refclk);
  443. else if (IS_G4X(dev)) {
  444. limit = intel_g4x_limit(crtc);
  445. } else if (IS_PINEVIEW(dev)) {
  446. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  447. limit = &intel_limits_pineview_lvds;
  448. else
  449. limit = &intel_limits_pineview_sdvo;
  450. } else if (IS_CHERRYVIEW(dev)) {
  451. limit = &intel_limits_chv;
  452. } else if (IS_VALLEYVIEW(dev)) {
  453. limit = &intel_limits_vlv;
  454. } else if (!IS_GEN2(dev)) {
  455. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  456. limit = &intel_limits_i9xx_lvds;
  457. else
  458. limit = &intel_limits_i9xx_sdvo;
  459. } else {
  460. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  461. limit = &intel_limits_i8xx_lvds;
  462. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  463. limit = &intel_limits_i8xx_dvo;
  464. else
  465. limit = &intel_limits_i8xx_dac;
  466. }
  467. return limit;
  468. }
  469. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  470. static void pineview_clock(int refclk, intel_clock_t *clock)
  471. {
  472. clock->m = clock->m2 + 2;
  473. clock->p = clock->p1 * clock->p2;
  474. if (WARN_ON(clock->n == 0 || clock->p == 0))
  475. return;
  476. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  477. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  478. }
  479. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  480. {
  481. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  482. }
  483. static void i9xx_clock(int refclk, intel_clock_t *clock)
  484. {
  485. clock->m = i9xx_dpll_compute_m(clock);
  486. clock->p = clock->p1 * clock->p2;
  487. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  488. return;
  489. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  490. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  491. }
  492. static void chv_clock(int refclk, intel_clock_t *clock)
  493. {
  494. clock->m = clock->m1 * clock->m2;
  495. clock->p = clock->p1 * clock->p2;
  496. if (WARN_ON(clock->n == 0 || clock->p == 0))
  497. return;
  498. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  499. clock->n << 22);
  500. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  501. }
  502. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  503. /**
  504. * Returns whether the given set of divisors are valid for a given refclk with
  505. * the given connectors.
  506. */
  507. static bool intel_PLL_is_valid(struct drm_device *dev,
  508. const intel_limit_t *limit,
  509. const intel_clock_t *clock)
  510. {
  511. if (clock->n < limit->n.min || limit->n.max < clock->n)
  512. INTELPllInvalid("n out of range\n");
  513. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  514. INTELPllInvalid("p1 out of range\n");
  515. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  516. INTELPllInvalid("m2 out of range\n");
  517. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  518. INTELPllInvalid("m1 out of range\n");
  519. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  520. if (clock->m1 <= clock->m2)
  521. INTELPllInvalid("m1 <= m2\n");
  522. if (!IS_VALLEYVIEW(dev)) {
  523. if (clock->p < limit->p.min || limit->p.max < clock->p)
  524. INTELPllInvalid("p out of range\n");
  525. if (clock->m < limit->m.min || limit->m.max < clock->m)
  526. INTELPllInvalid("m out of range\n");
  527. }
  528. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  529. INTELPllInvalid("vco out of range\n");
  530. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  531. * connector, etc., rather than just a single range.
  532. */
  533. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  534. INTELPllInvalid("dot out of range\n");
  535. return true;
  536. }
  537. static bool
  538. i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  539. int target, int refclk, intel_clock_t *match_clock,
  540. intel_clock_t *best_clock)
  541. {
  542. struct drm_device *dev = crtc->base.dev;
  543. intel_clock_t clock;
  544. int err = target;
  545. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  546. /*
  547. * For LVDS just rely on its current settings for dual-channel.
  548. * We haven't figured out how to reliably set up different
  549. * single/dual channel state, if we even can.
  550. */
  551. if (intel_is_dual_link_lvds(dev))
  552. clock.p2 = limit->p2.p2_fast;
  553. else
  554. clock.p2 = limit->p2.p2_slow;
  555. } else {
  556. if (target < limit->p2.dot_limit)
  557. clock.p2 = limit->p2.p2_slow;
  558. else
  559. clock.p2 = limit->p2.p2_fast;
  560. }
  561. memset(best_clock, 0, sizeof(*best_clock));
  562. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  563. clock.m1++) {
  564. for (clock.m2 = limit->m2.min;
  565. clock.m2 <= limit->m2.max; clock.m2++) {
  566. if (clock.m2 >= clock.m1)
  567. break;
  568. for (clock.n = limit->n.min;
  569. clock.n <= limit->n.max; clock.n++) {
  570. for (clock.p1 = limit->p1.min;
  571. clock.p1 <= limit->p1.max; clock.p1++) {
  572. int this_err;
  573. i9xx_clock(refclk, &clock);
  574. if (!intel_PLL_is_valid(dev, limit,
  575. &clock))
  576. continue;
  577. if (match_clock &&
  578. clock.p != match_clock->p)
  579. continue;
  580. this_err = abs(clock.dot - target);
  581. if (this_err < err) {
  582. *best_clock = clock;
  583. err = this_err;
  584. }
  585. }
  586. }
  587. }
  588. }
  589. return (err != target);
  590. }
  591. static bool
  592. pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  593. int target, int refclk, intel_clock_t *match_clock,
  594. intel_clock_t *best_clock)
  595. {
  596. struct drm_device *dev = crtc->base.dev;
  597. intel_clock_t clock;
  598. int err = target;
  599. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  600. /*
  601. * For LVDS just rely on its current settings for dual-channel.
  602. * We haven't figured out how to reliably set up different
  603. * single/dual channel state, if we even can.
  604. */
  605. if (intel_is_dual_link_lvds(dev))
  606. clock.p2 = limit->p2.p2_fast;
  607. else
  608. clock.p2 = limit->p2.p2_slow;
  609. } else {
  610. if (target < limit->p2.dot_limit)
  611. clock.p2 = limit->p2.p2_slow;
  612. else
  613. clock.p2 = limit->p2.p2_fast;
  614. }
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  617. clock.m1++) {
  618. for (clock.m2 = limit->m2.min;
  619. clock.m2 <= limit->m2.max; clock.m2++) {
  620. for (clock.n = limit->n.min;
  621. clock.n <= limit->n.max; clock.n++) {
  622. for (clock.p1 = limit->p1.min;
  623. clock.p1 <= limit->p1.max; clock.p1++) {
  624. int this_err;
  625. pineview_clock(refclk, &clock);
  626. if (!intel_PLL_is_valid(dev, limit,
  627. &clock))
  628. continue;
  629. if (match_clock &&
  630. clock.p != match_clock->p)
  631. continue;
  632. this_err = abs(clock.dot - target);
  633. if (this_err < err) {
  634. *best_clock = clock;
  635. err = this_err;
  636. }
  637. }
  638. }
  639. }
  640. }
  641. return (err != target);
  642. }
  643. static bool
  644. g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  645. int target, int refclk, intel_clock_t *match_clock,
  646. intel_clock_t *best_clock)
  647. {
  648. struct drm_device *dev = crtc->base.dev;
  649. intel_clock_t clock;
  650. int max_n;
  651. bool found;
  652. /* approximately equals target * 0.00585 */
  653. int err_most = (target >> 8) + (target >> 9);
  654. found = false;
  655. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  656. if (intel_is_dual_link_lvds(dev))
  657. clock.p2 = limit->p2.p2_fast;
  658. else
  659. clock.p2 = limit->p2.p2_slow;
  660. } else {
  661. if (target < limit->p2.dot_limit)
  662. clock.p2 = limit->p2.p2_slow;
  663. else
  664. clock.p2 = limit->p2.p2_fast;
  665. }
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. max_n = limit->n.max;
  668. /* based on hardware requirement, prefer smaller n to precision */
  669. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  670. /* based on hardware requirement, prefere larger m1,m2 */
  671. for (clock.m1 = limit->m1.max;
  672. clock.m1 >= limit->m1.min; clock.m1--) {
  673. for (clock.m2 = limit->m2.max;
  674. clock.m2 >= limit->m2.min; clock.m2--) {
  675. for (clock.p1 = limit->p1.max;
  676. clock.p1 >= limit->p1.min; clock.p1--) {
  677. int this_err;
  678. i9xx_clock(refclk, &clock);
  679. if (!intel_PLL_is_valid(dev, limit,
  680. &clock))
  681. continue;
  682. this_err = abs(clock.dot - target);
  683. if (this_err < err_most) {
  684. *best_clock = clock;
  685. err_most = this_err;
  686. max_n = clock.n;
  687. found = true;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return found;
  694. }
  695. static bool
  696. vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  697. int target, int refclk, intel_clock_t *match_clock,
  698. intel_clock_t *best_clock)
  699. {
  700. struct drm_device *dev = crtc->base.dev;
  701. intel_clock_t clock;
  702. unsigned int bestppm = 1000000;
  703. /* min update 19.2 MHz */
  704. int max_n = min(limit->n.max, refclk / 19200);
  705. bool found = false;
  706. target *= 5; /* fast clock */
  707. memset(best_clock, 0, sizeof(*best_clock));
  708. /* based on hardware requirement, prefer smaller n to precision */
  709. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  710. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  711. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  712. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  713. clock.p = clock.p1 * clock.p2;
  714. /* based on hardware requirement, prefer bigger m1,m2 values */
  715. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  716. unsigned int ppm, diff;
  717. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  718. refclk * clock.m1);
  719. vlv_clock(refclk, &clock);
  720. if (!intel_PLL_is_valid(dev, limit,
  721. &clock))
  722. continue;
  723. diff = abs(clock.dot - target);
  724. ppm = div_u64(1000000ULL * diff, target);
  725. if (ppm < 100 && clock.p > best_clock->p) {
  726. bestppm = 0;
  727. *best_clock = clock;
  728. found = true;
  729. }
  730. if (bestppm >= 10 && ppm < bestppm - 10) {
  731. bestppm = ppm;
  732. *best_clock = clock;
  733. found = true;
  734. }
  735. }
  736. }
  737. }
  738. }
  739. return found;
  740. }
  741. static bool
  742. chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  743. int target, int refclk, intel_clock_t *match_clock,
  744. intel_clock_t *best_clock)
  745. {
  746. struct drm_device *dev = crtc->base.dev;
  747. intel_clock_t clock;
  748. uint64_t m2;
  749. int found = false;
  750. memset(best_clock, 0, sizeof(*best_clock));
  751. /*
  752. * Based on hardware doc, the n always set to 1, and m1 always
  753. * set to 2. If requires to support 200Mhz refclk, we need to
  754. * revisit this because n may not 1 anymore.
  755. */
  756. clock.n = 1, clock.m1 = 2;
  757. target *= 5; /* fast clock */
  758. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  759. for (clock.p2 = limit->p2.p2_fast;
  760. clock.p2 >= limit->p2.p2_slow;
  761. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  762. clock.p = clock.p1 * clock.p2;
  763. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  764. clock.n) << 22, refclk * clock.m1);
  765. if (m2 > INT_MAX/clock.m1)
  766. continue;
  767. clock.m2 = m2;
  768. chv_clock(refclk, &clock);
  769. if (!intel_PLL_is_valid(dev, limit, &clock))
  770. continue;
  771. /* based on hardware requirement, prefer bigger p
  772. */
  773. if (clock.p > best_clock->p) {
  774. *best_clock = clock;
  775. found = true;
  776. }
  777. }
  778. }
  779. return found;
  780. }
  781. bool intel_crtc_active(struct drm_crtc *crtc)
  782. {
  783. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  784. /* Be paranoid as we can arrive here with only partial
  785. * state retrieved from the hardware during setup.
  786. *
  787. * We can ditch the adjusted_mode.crtc_clock check as soon
  788. * as Haswell has gained clock readout/fastboot support.
  789. *
  790. * We can ditch the crtc->primary->fb check as soon as we can
  791. * properly reconstruct framebuffers.
  792. */
  793. return intel_crtc->active && crtc->primary->fb &&
  794. intel_crtc->config.adjusted_mode.crtc_clock;
  795. }
  796. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  797. enum pipe pipe)
  798. {
  799. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  801. return intel_crtc->config.cpu_transcoder;
  802. }
  803. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  804. {
  805. struct drm_i915_private *dev_priv = dev->dev_private;
  806. u32 reg = PIPEDSL(pipe);
  807. u32 line1, line2;
  808. u32 line_mask;
  809. if (IS_GEN2(dev))
  810. line_mask = DSL_LINEMASK_GEN2;
  811. else
  812. line_mask = DSL_LINEMASK_GEN3;
  813. line1 = I915_READ(reg) & line_mask;
  814. mdelay(5);
  815. line2 = I915_READ(reg) & line_mask;
  816. return line1 == line2;
  817. }
  818. /*
  819. * intel_wait_for_pipe_off - wait for pipe to turn off
  820. * @crtc: crtc whose pipe to wait for
  821. *
  822. * After disabling a pipe, we can't wait for vblank in the usual way,
  823. * spinning on the vblank interrupt status bit, since we won't actually
  824. * see an interrupt when the pipe is disabled.
  825. *
  826. * On Gen4 and above:
  827. * wait for the pipe register state bit to turn off
  828. *
  829. * Otherwise:
  830. * wait for the display line value to settle (it usually
  831. * ends up stopping at the start of the next frame).
  832. *
  833. */
  834. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  835. {
  836. struct drm_device *dev = crtc->base.dev;
  837. struct drm_i915_private *dev_priv = dev->dev_private;
  838. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  839. enum pipe pipe = crtc->pipe;
  840. if (INTEL_INFO(dev)->gen >= 4) {
  841. int reg = PIPECONF(cpu_transcoder);
  842. /* Wait for the Pipe State to go off */
  843. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  844. 100))
  845. WARN(1, "pipe_off wait timed out\n");
  846. } else {
  847. /* Wait for the display line to settle */
  848. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  849. WARN(1, "pipe_off wait timed out\n");
  850. }
  851. }
  852. /*
  853. * ibx_digital_port_connected - is the specified port connected?
  854. * @dev_priv: i915 private structure
  855. * @port: the port to test
  856. *
  857. * Returns true if @port is connected, false otherwise.
  858. */
  859. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  860. struct intel_digital_port *port)
  861. {
  862. u32 bit;
  863. if (HAS_PCH_IBX(dev_priv->dev)) {
  864. switch (port->port) {
  865. case PORT_B:
  866. bit = SDE_PORTB_HOTPLUG;
  867. break;
  868. case PORT_C:
  869. bit = SDE_PORTC_HOTPLUG;
  870. break;
  871. case PORT_D:
  872. bit = SDE_PORTD_HOTPLUG;
  873. break;
  874. default:
  875. return true;
  876. }
  877. } else {
  878. switch (port->port) {
  879. case PORT_B:
  880. bit = SDE_PORTB_HOTPLUG_CPT;
  881. break;
  882. case PORT_C:
  883. bit = SDE_PORTC_HOTPLUG_CPT;
  884. break;
  885. case PORT_D:
  886. bit = SDE_PORTD_HOTPLUG_CPT;
  887. break;
  888. default:
  889. return true;
  890. }
  891. }
  892. return I915_READ(SDEISR) & bit;
  893. }
  894. static const char *state_string(bool enabled)
  895. {
  896. return enabled ? "on" : "off";
  897. }
  898. /* Only for pre-ILK configs */
  899. void assert_pll(struct drm_i915_private *dev_priv,
  900. enum pipe pipe, bool state)
  901. {
  902. int reg;
  903. u32 val;
  904. bool cur_state;
  905. reg = DPLL(pipe);
  906. val = I915_READ(reg);
  907. cur_state = !!(val & DPLL_VCO_ENABLE);
  908. I915_STATE_WARN(cur_state != state,
  909. "PLL state assertion failure (expected %s, current %s)\n",
  910. state_string(state), state_string(cur_state));
  911. }
  912. /* XXX: the dsi pll is shared between MIPI DSI ports */
  913. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  914. {
  915. u32 val;
  916. bool cur_state;
  917. mutex_lock(&dev_priv->dpio_lock);
  918. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  919. mutex_unlock(&dev_priv->dpio_lock);
  920. cur_state = val & DSI_PLL_VCO_EN;
  921. I915_STATE_WARN(cur_state != state,
  922. "DSI PLL state assertion failure (expected %s, current %s)\n",
  923. state_string(state), state_string(cur_state));
  924. }
  925. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  926. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  927. struct intel_shared_dpll *
  928. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  929. {
  930. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  931. if (crtc->config.shared_dpll < 0)
  932. return NULL;
  933. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  934. }
  935. /* For ILK+ */
  936. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  937. struct intel_shared_dpll *pll,
  938. bool state)
  939. {
  940. bool cur_state;
  941. struct intel_dpll_hw_state hw_state;
  942. if (WARN (!pll,
  943. "asserting DPLL %s with no DPLL\n", state_string(state)))
  944. return;
  945. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  946. I915_STATE_WARN(cur_state != state,
  947. "%s assertion failure (expected %s, current %s)\n",
  948. pll->name, state_string(state), state_string(cur_state));
  949. }
  950. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  951. enum pipe pipe, bool state)
  952. {
  953. int reg;
  954. u32 val;
  955. bool cur_state;
  956. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  957. pipe);
  958. if (HAS_DDI(dev_priv->dev)) {
  959. /* DDI does not have a specific FDI_TX register */
  960. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  961. val = I915_READ(reg);
  962. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  963. } else {
  964. reg = FDI_TX_CTL(pipe);
  965. val = I915_READ(reg);
  966. cur_state = !!(val & FDI_TX_ENABLE);
  967. }
  968. I915_STATE_WARN(cur_state != state,
  969. "FDI TX state assertion failure (expected %s, current %s)\n",
  970. state_string(state), state_string(cur_state));
  971. }
  972. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  973. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  974. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  975. enum pipe pipe, bool state)
  976. {
  977. int reg;
  978. u32 val;
  979. bool cur_state;
  980. reg = FDI_RX_CTL(pipe);
  981. val = I915_READ(reg);
  982. cur_state = !!(val & FDI_RX_ENABLE);
  983. I915_STATE_WARN(cur_state != state,
  984. "FDI RX state assertion failure (expected %s, current %s)\n",
  985. state_string(state), state_string(cur_state));
  986. }
  987. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  988. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  989. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  990. enum pipe pipe)
  991. {
  992. int reg;
  993. u32 val;
  994. /* ILK FDI PLL is always enabled */
  995. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  996. return;
  997. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  998. if (HAS_DDI(dev_priv->dev))
  999. return;
  1000. reg = FDI_TX_CTL(pipe);
  1001. val = I915_READ(reg);
  1002. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1003. }
  1004. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1005. enum pipe pipe, bool state)
  1006. {
  1007. int reg;
  1008. u32 val;
  1009. bool cur_state;
  1010. reg = FDI_RX_CTL(pipe);
  1011. val = I915_READ(reg);
  1012. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1013. I915_STATE_WARN(cur_state != state,
  1014. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1015. state_string(state), state_string(cur_state));
  1016. }
  1017. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1018. enum pipe pipe)
  1019. {
  1020. struct drm_device *dev = dev_priv->dev;
  1021. int pp_reg;
  1022. u32 val;
  1023. enum pipe panel_pipe = PIPE_A;
  1024. bool locked = true;
  1025. if (WARN_ON(HAS_DDI(dev)))
  1026. return;
  1027. if (HAS_PCH_SPLIT(dev)) {
  1028. u32 port_sel;
  1029. pp_reg = PCH_PP_CONTROL;
  1030. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1031. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1032. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1033. panel_pipe = PIPE_B;
  1034. /* XXX: else fix for eDP */
  1035. } else if (IS_VALLEYVIEW(dev)) {
  1036. /* presumably write lock depends on pipe, not port select */
  1037. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1038. panel_pipe = pipe;
  1039. } else {
  1040. pp_reg = PP_CONTROL;
  1041. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1042. panel_pipe = PIPE_B;
  1043. }
  1044. val = I915_READ(pp_reg);
  1045. if (!(val & PANEL_POWER_ON) ||
  1046. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1047. locked = false;
  1048. I915_STATE_WARN(panel_pipe == pipe && locked,
  1049. "panel assertion failure, pipe %c regs locked\n",
  1050. pipe_name(pipe));
  1051. }
  1052. static void assert_cursor(struct drm_i915_private *dev_priv,
  1053. enum pipe pipe, bool state)
  1054. {
  1055. struct drm_device *dev = dev_priv->dev;
  1056. bool cur_state;
  1057. if (IS_845G(dev) || IS_I865G(dev))
  1058. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1059. else
  1060. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1061. I915_STATE_WARN(cur_state != state,
  1062. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1063. pipe_name(pipe), state_string(state), state_string(cur_state));
  1064. }
  1065. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1066. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1067. void assert_pipe(struct drm_i915_private *dev_priv,
  1068. enum pipe pipe, bool state)
  1069. {
  1070. int reg;
  1071. u32 val;
  1072. bool cur_state;
  1073. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1074. pipe);
  1075. /* if we need the pipe quirk it must be always on */
  1076. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1077. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1078. state = true;
  1079. if (!intel_display_power_is_enabled(dev_priv,
  1080. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1081. cur_state = false;
  1082. } else {
  1083. reg = PIPECONF(cpu_transcoder);
  1084. val = I915_READ(reg);
  1085. cur_state = !!(val & PIPECONF_ENABLE);
  1086. }
  1087. I915_STATE_WARN(cur_state != state,
  1088. "pipe %c assertion failure (expected %s, current %s)\n",
  1089. pipe_name(pipe), state_string(state), state_string(cur_state));
  1090. }
  1091. static void assert_plane(struct drm_i915_private *dev_priv,
  1092. enum plane plane, bool state)
  1093. {
  1094. int reg;
  1095. u32 val;
  1096. bool cur_state;
  1097. reg = DSPCNTR(plane);
  1098. val = I915_READ(reg);
  1099. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1100. I915_STATE_WARN(cur_state != state,
  1101. "plane %c assertion failure (expected %s, current %s)\n",
  1102. plane_name(plane), state_string(state), state_string(cur_state));
  1103. }
  1104. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1105. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1106. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1107. enum pipe pipe)
  1108. {
  1109. struct drm_device *dev = dev_priv->dev;
  1110. int reg, i;
  1111. u32 val;
  1112. int cur_pipe;
  1113. /* Primary planes are fixed to pipes on gen4+ */
  1114. if (INTEL_INFO(dev)->gen >= 4) {
  1115. reg = DSPCNTR(pipe);
  1116. val = I915_READ(reg);
  1117. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1118. "plane %c assertion failure, should be disabled but not\n",
  1119. plane_name(pipe));
  1120. return;
  1121. }
  1122. /* Need to check both planes against the pipe */
  1123. for_each_pipe(dev_priv, i) {
  1124. reg = DSPCNTR(i);
  1125. val = I915_READ(reg);
  1126. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1127. DISPPLANE_SEL_PIPE_SHIFT;
  1128. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1129. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1130. plane_name(i), pipe_name(pipe));
  1131. }
  1132. }
  1133. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1134. enum pipe pipe)
  1135. {
  1136. struct drm_device *dev = dev_priv->dev;
  1137. int reg, sprite;
  1138. u32 val;
  1139. if (INTEL_INFO(dev)->gen >= 9) {
  1140. for_each_sprite(pipe, sprite) {
  1141. val = I915_READ(PLANE_CTL(pipe, sprite));
  1142. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1143. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1144. sprite, pipe_name(pipe));
  1145. }
  1146. } else if (IS_VALLEYVIEW(dev)) {
  1147. for_each_sprite(pipe, sprite) {
  1148. reg = SPCNTR(pipe, sprite);
  1149. val = I915_READ(reg);
  1150. I915_STATE_WARN(val & SP_ENABLE,
  1151. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1152. sprite_name(pipe, sprite), pipe_name(pipe));
  1153. }
  1154. } else if (INTEL_INFO(dev)->gen >= 7) {
  1155. reg = SPRCTL(pipe);
  1156. val = I915_READ(reg);
  1157. I915_STATE_WARN(val & SPRITE_ENABLE,
  1158. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1159. plane_name(pipe), pipe_name(pipe));
  1160. } else if (INTEL_INFO(dev)->gen >= 5) {
  1161. reg = DVSCNTR(pipe);
  1162. val = I915_READ(reg);
  1163. I915_STATE_WARN(val & DVS_ENABLE,
  1164. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1165. plane_name(pipe), pipe_name(pipe));
  1166. }
  1167. }
  1168. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1169. {
  1170. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1171. drm_crtc_vblank_put(crtc);
  1172. }
  1173. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1174. {
  1175. u32 val;
  1176. bool enabled;
  1177. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1178. val = I915_READ(PCH_DREF_CONTROL);
  1179. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1180. DREF_SUPERSPREAD_SOURCE_MASK));
  1181. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1182. }
  1183. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1184. enum pipe pipe)
  1185. {
  1186. int reg;
  1187. u32 val;
  1188. bool enabled;
  1189. reg = PCH_TRANSCONF(pipe);
  1190. val = I915_READ(reg);
  1191. enabled = !!(val & TRANS_ENABLE);
  1192. I915_STATE_WARN(enabled,
  1193. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1194. pipe_name(pipe));
  1195. }
  1196. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe, u32 port_sel, u32 val)
  1198. {
  1199. if ((val & DP_PORT_EN) == 0)
  1200. return false;
  1201. if (HAS_PCH_CPT(dev_priv->dev)) {
  1202. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1203. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1204. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1205. return false;
  1206. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1207. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1208. return false;
  1209. } else {
  1210. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1211. return false;
  1212. }
  1213. return true;
  1214. }
  1215. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1216. enum pipe pipe, u32 val)
  1217. {
  1218. if ((val & SDVO_ENABLE) == 0)
  1219. return false;
  1220. if (HAS_PCH_CPT(dev_priv->dev)) {
  1221. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1222. return false;
  1223. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1225. return false;
  1226. } else {
  1227. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1228. return false;
  1229. }
  1230. return true;
  1231. }
  1232. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1233. enum pipe pipe, u32 val)
  1234. {
  1235. if ((val & LVDS_PORT_EN) == 0)
  1236. return false;
  1237. if (HAS_PCH_CPT(dev_priv->dev)) {
  1238. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1239. return false;
  1240. } else {
  1241. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1242. return false;
  1243. }
  1244. return true;
  1245. }
  1246. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1247. enum pipe pipe, u32 val)
  1248. {
  1249. if ((val & ADPA_DAC_ENABLE) == 0)
  1250. return false;
  1251. if (HAS_PCH_CPT(dev_priv->dev)) {
  1252. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1253. return false;
  1254. } else {
  1255. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1256. return false;
  1257. }
  1258. return true;
  1259. }
  1260. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1261. enum pipe pipe, int reg, u32 port_sel)
  1262. {
  1263. u32 val = I915_READ(reg);
  1264. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1265. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1266. reg, pipe_name(pipe));
  1267. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1268. && (val & DP_PIPEB_SELECT),
  1269. "IBX PCH dp port still using transcoder B\n");
  1270. }
  1271. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1272. enum pipe pipe, int reg)
  1273. {
  1274. u32 val = I915_READ(reg);
  1275. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1276. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1277. reg, pipe_name(pipe));
  1278. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1279. && (val & SDVO_PIPE_B_SELECT),
  1280. "IBX PCH hdmi port still using transcoder B\n");
  1281. }
  1282. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1283. enum pipe pipe)
  1284. {
  1285. int reg;
  1286. u32 val;
  1287. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1288. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1289. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1290. reg = PCH_ADPA;
  1291. val = I915_READ(reg);
  1292. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1293. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1294. pipe_name(pipe));
  1295. reg = PCH_LVDS;
  1296. val = I915_READ(reg);
  1297. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1298. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1299. pipe_name(pipe));
  1300. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1301. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1303. }
  1304. static void intel_init_dpio(struct drm_device *dev)
  1305. {
  1306. struct drm_i915_private *dev_priv = dev->dev_private;
  1307. if (!IS_VALLEYVIEW(dev))
  1308. return;
  1309. /*
  1310. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1311. * CHV x1 PHY (DP/HDMI D)
  1312. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1313. */
  1314. if (IS_CHERRYVIEW(dev)) {
  1315. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1316. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1317. } else {
  1318. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1319. }
  1320. }
  1321. static void vlv_enable_pll(struct intel_crtc *crtc,
  1322. const struct intel_crtc_config *pipe_config)
  1323. {
  1324. struct drm_device *dev = crtc->base.dev;
  1325. struct drm_i915_private *dev_priv = dev->dev_private;
  1326. int reg = DPLL(crtc->pipe);
  1327. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1328. assert_pipe_disabled(dev_priv, crtc->pipe);
  1329. /* No really, not for ILK+ */
  1330. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1331. /* PLL is protected by panel, make sure we can write it */
  1332. if (IS_MOBILE(dev_priv->dev))
  1333. assert_panel_unlocked(dev_priv, crtc->pipe);
  1334. I915_WRITE(reg, dpll);
  1335. POSTING_READ(reg);
  1336. udelay(150);
  1337. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1338. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1339. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1340. POSTING_READ(DPLL_MD(crtc->pipe));
  1341. /* We do this three times for luck */
  1342. I915_WRITE(reg, dpll);
  1343. POSTING_READ(reg);
  1344. udelay(150); /* wait for warmup */
  1345. I915_WRITE(reg, dpll);
  1346. POSTING_READ(reg);
  1347. udelay(150); /* wait for warmup */
  1348. I915_WRITE(reg, dpll);
  1349. POSTING_READ(reg);
  1350. udelay(150); /* wait for warmup */
  1351. }
  1352. static void chv_enable_pll(struct intel_crtc *crtc,
  1353. const struct intel_crtc_config *pipe_config)
  1354. {
  1355. struct drm_device *dev = crtc->base.dev;
  1356. struct drm_i915_private *dev_priv = dev->dev_private;
  1357. int pipe = crtc->pipe;
  1358. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1359. u32 tmp;
  1360. assert_pipe_disabled(dev_priv, crtc->pipe);
  1361. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1362. mutex_lock(&dev_priv->dpio_lock);
  1363. /* Enable back the 10bit clock to display controller */
  1364. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1365. tmp |= DPIO_DCLKP_EN;
  1366. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1367. /*
  1368. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1369. */
  1370. udelay(1);
  1371. /* Enable PLL */
  1372. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1373. /* Check PLL is locked */
  1374. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1375. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1376. /* not sure when this should be written */
  1377. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1378. POSTING_READ(DPLL_MD(pipe));
  1379. mutex_unlock(&dev_priv->dpio_lock);
  1380. }
  1381. static int intel_num_dvo_pipes(struct drm_device *dev)
  1382. {
  1383. struct intel_crtc *crtc;
  1384. int count = 0;
  1385. for_each_intel_crtc(dev, crtc)
  1386. count += crtc->active &&
  1387. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1388. return count;
  1389. }
  1390. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1391. {
  1392. struct drm_device *dev = crtc->base.dev;
  1393. struct drm_i915_private *dev_priv = dev->dev_private;
  1394. int reg = DPLL(crtc->pipe);
  1395. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1396. assert_pipe_disabled(dev_priv, crtc->pipe);
  1397. /* No really, not for ILK+ */
  1398. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1399. /* PLL is protected by panel, make sure we can write it */
  1400. if (IS_MOBILE(dev) && !IS_I830(dev))
  1401. assert_panel_unlocked(dev_priv, crtc->pipe);
  1402. /* Enable DVO 2x clock on both PLLs if necessary */
  1403. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1404. /*
  1405. * It appears to be important that we don't enable this
  1406. * for the current pipe before otherwise configuring the
  1407. * PLL. No idea how this should be handled if multiple
  1408. * DVO outputs are enabled simultaneosly.
  1409. */
  1410. dpll |= DPLL_DVO_2X_MODE;
  1411. I915_WRITE(DPLL(!crtc->pipe),
  1412. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1413. }
  1414. /* Wait for the clocks to stabilize. */
  1415. POSTING_READ(reg);
  1416. udelay(150);
  1417. if (INTEL_INFO(dev)->gen >= 4) {
  1418. I915_WRITE(DPLL_MD(crtc->pipe),
  1419. crtc->config.dpll_hw_state.dpll_md);
  1420. } else {
  1421. /* The pixel multiplier can only be updated once the
  1422. * DPLL is enabled and the clocks are stable.
  1423. *
  1424. * So write it again.
  1425. */
  1426. I915_WRITE(reg, dpll);
  1427. }
  1428. /* We do this three times for luck */
  1429. I915_WRITE(reg, dpll);
  1430. POSTING_READ(reg);
  1431. udelay(150); /* wait for warmup */
  1432. I915_WRITE(reg, dpll);
  1433. POSTING_READ(reg);
  1434. udelay(150); /* wait for warmup */
  1435. I915_WRITE(reg, dpll);
  1436. POSTING_READ(reg);
  1437. udelay(150); /* wait for warmup */
  1438. }
  1439. /**
  1440. * i9xx_disable_pll - disable a PLL
  1441. * @dev_priv: i915 private structure
  1442. * @pipe: pipe PLL to disable
  1443. *
  1444. * Disable the PLL for @pipe, making sure the pipe is off first.
  1445. *
  1446. * Note! This is for pre-ILK only.
  1447. */
  1448. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1449. {
  1450. struct drm_device *dev = crtc->base.dev;
  1451. struct drm_i915_private *dev_priv = dev->dev_private;
  1452. enum pipe pipe = crtc->pipe;
  1453. /* Disable DVO 2x clock on both PLLs if necessary */
  1454. if (IS_I830(dev) &&
  1455. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1456. intel_num_dvo_pipes(dev) == 1) {
  1457. I915_WRITE(DPLL(PIPE_B),
  1458. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1459. I915_WRITE(DPLL(PIPE_A),
  1460. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1461. }
  1462. /* Don't disable pipe or pipe PLLs if needed */
  1463. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1464. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1465. return;
  1466. /* Make sure the pipe isn't still relying on us */
  1467. assert_pipe_disabled(dev_priv, pipe);
  1468. I915_WRITE(DPLL(pipe), 0);
  1469. POSTING_READ(DPLL(pipe));
  1470. }
  1471. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1472. {
  1473. u32 val = 0;
  1474. /* Make sure the pipe isn't still relying on us */
  1475. assert_pipe_disabled(dev_priv, pipe);
  1476. /*
  1477. * Leave integrated clock source and reference clock enabled for pipe B.
  1478. * The latter is needed for VGA hotplug / manual detection.
  1479. */
  1480. if (pipe == PIPE_B)
  1481. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1482. I915_WRITE(DPLL(pipe), val);
  1483. POSTING_READ(DPLL(pipe));
  1484. }
  1485. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1486. {
  1487. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1488. u32 val;
  1489. /* Make sure the pipe isn't still relying on us */
  1490. assert_pipe_disabled(dev_priv, pipe);
  1491. /* Set PLL en = 0 */
  1492. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1493. if (pipe != PIPE_A)
  1494. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1495. I915_WRITE(DPLL(pipe), val);
  1496. POSTING_READ(DPLL(pipe));
  1497. mutex_lock(&dev_priv->dpio_lock);
  1498. /* Disable 10bit clock to display controller */
  1499. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1500. val &= ~DPIO_DCLKP_EN;
  1501. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1502. /* disable left/right clock distribution */
  1503. if (pipe != PIPE_B) {
  1504. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1505. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1506. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1507. } else {
  1508. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1509. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1510. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1511. }
  1512. mutex_unlock(&dev_priv->dpio_lock);
  1513. }
  1514. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1515. struct intel_digital_port *dport)
  1516. {
  1517. u32 port_mask;
  1518. int dpll_reg;
  1519. switch (dport->port) {
  1520. case PORT_B:
  1521. port_mask = DPLL_PORTB_READY_MASK;
  1522. dpll_reg = DPLL(0);
  1523. break;
  1524. case PORT_C:
  1525. port_mask = DPLL_PORTC_READY_MASK;
  1526. dpll_reg = DPLL(0);
  1527. break;
  1528. case PORT_D:
  1529. port_mask = DPLL_PORTD_READY_MASK;
  1530. dpll_reg = DPIO_PHY_STATUS;
  1531. break;
  1532. default:
  1533. BUG();
  1534. }
  1535. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1536. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1537. port_name(dport->port), I915_READ(dpll_reg));
  1538. }
  1539. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1540. {
  1541. struct drm_device *dev = crtc->base.dev;
  1542. struct drm_i915_private *dev_priv = dev->dev_private;
  1543. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1544. if (WARN_ON(pll == NULL))
  1545. return;
  1546. WARN_ON(!pll->config.crtc_mask);
  1547. if (pll->active == 0) {
  1548. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1549. WARN_ON(pll->on);
  1550. assert_shared_dpll_disabled(dev_priv, pll);
  1551. pll->mode_set(dev_priv, pll);
  1552. }
  1553. }
  1554. /**
  1555. * intel_enable_shared_dpll - enable PCH PLL
  1556. * @dev_priv: i915 private structure
  1557. * @pipe: pipe PLL to enable
  1558. *
  1559. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1560. * drives the transcoder clock.
  1561. */
  1562. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1563. {
  1564. struct drm_device *dev = crtc->base.dev;
  1565. struct drm_i915_private *dev_priv = dev->dev_private;
  1566. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1567. if (WARN_ON(pll == NULL))
  1568. return;
  1569. if (WARN_ON(pll->config.crtc_mask == 0))
  1570. return;
  1571. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1572. pll->name, pll->active, pll->on,
  1573. crtc->base.base.id);
  1574. if (pll->active++) {
  1575. WARN_ON(!pll->on);
  1576. assert_shared_dpll_enabled(dev_priv, pll);
  1577. return;
  1578. }
  1579. WARN_ON(pll->on);
  1580. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1581. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1582. pll->enable(dev_priv, pll);
  1583. pll->on = true;
  1584. }
  1585. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1586. {
  1587. struct drm_device *dev = crtc->base.dev;
  1588. struct drm_i915_private *dev_priv = dev->dev_private;
  1589. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1590. /* PCH only available on ILK+ */
  1591. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1592. if (WARN_ON(pll == NULL))
  1593. return;
  1594. if (WARN_ON(pll->config.crtc_mask == 0))
  1595. return;
  1596. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1597. pll->name, pll->active, pll->on,
  1598. crtc->base.base.id);
  1599. if (WARN_ON(pll->active == 0)) {
  1600. assert_shared_dpll_disabled(dev_priv, pll);
  1601. return;
  1602. }
  1603. assert_shared_dpll_enabled(dev_priv, pll);
  1604. WARN_ON(!pll->on);
  1605. if (--pll->active)
  1606. return;
  1607. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1608. pll->disable(dev_priv, pll);
  1609. pll->on = false;
  1610. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1611. }
  1612. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1613. enum pipe pipe)
  1614. {
  1615. struct drm_device *dev = dev_priv->dev;
  1616. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1618. uint32_t reg, val, pipeconf_val;
  1619. /* PCH only available on ILK+ */
  1620. BUG_ON(!HAS_PCH_SPLIT(dev));
  1621. /* Make sure PCH DPLL is enabled */
  1622. assert_shared_dpll_enabled(dev_priv,
  1623. intel_crtc_to_shared_dpll(intel_crtc));
  1624. /* FDI must be feeding us bits for PCH ports */
  1625. assert_fdi_tx_enabled(dev_priv, pipe);
  1626. assert_fdi_rx_enabled(dev_priv, pipe);
  1627. if (HAS_PCH_CPT(dev)) {
  1628. /* Workaround: Set the timing override bit before enabling the
  1629. * pch transcoder. */
  1630. reg = TRANS_CHICKEN2(pipe);
  1631. val = I915_READ(reg);
  1632. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1633. I915_WRITE(reg, val);
  1634. }
  1635. reg = PCH_TRANSCONF(pipe);
  1636. val = I915_READ(reg);
  1637. pipeconf_val = I915_READ(PIPECONF(pipe));
  1638. if (HAS_PCH_IBX(dev_priv->dev)) {
  1639. /*
  1640. * make the BPC in transcoder be consistent with
  1641. * that in pipeconf reg.
  1642. */
  1643. val &= ~PIPECONF_BPC_MASK;
  1644. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1645. }
  1646. val &= ~TRANS_INTERLACE_MASK;
  1647. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1648. if (HAS_PCH_IBX(dev_priv->dev) &&
  1649. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1650. val |= TRANS_LEGACY_INTERLACED_ILK;
  1651. else
  1652. val |= TRANS_INTERLACED;
  1653. else
  1654. val |= TRANS_PROGRESSIVE;
  1655. I915_WRITE(reg, val | TRANS_ENABLE);
  1656. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1657. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1658. }
  1659. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1660. enum transcoder cpu_transcoder)
  1661. {
  1662. u32 val, pipeconf_val;
  1663. /* PCH only available on ILK+ */
  1664. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1665. /* FDI must be feeding us bits for PCH ports */
  1666. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1667. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1668. /* Workaround: set timing override bit. */
  1669. val = I915_READ(_TRANSA_CHICKEN2);
  1670. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1671. I915_WRITE(_TRANSA_CHICKEN2, val);
  1672. val = TRANS_ENABLE;
  1673. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1674. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1675. PIPECONF_INTERLACED_ILK)
  1676. val |= TRANS_INTERLACED;
  1677. else
  1678. val |= TRANS_PROGRESSIVE;
  1679. I915_WRITE(LPT_TRANSCONF, val);
  1680. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1681. DRM_ERROR("Failed to enable PCH transcoder\n");
  1682. }
  1683. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1684. enum pipe pipe)
  1685. {
  1686. struct drm_device *dev = dev_priv->dev;
  1687. uint32_t reg, val;
  1688. /* FDI relies on the transcoder */
  1689. assert_fdi_tx_disabled(dev_priv, pipe);
  1690. assert_fdi_rx_disabled(dev_priv, pipe);
  1691. /* Ports must be off as well */
  1692. assert_pch_ports_disabled(dev_priv, pipe);
  1693. reg = PCH_TRANSCONF(pipe);
  1694. val = I915_READ(reg);
  1695. val &= ~TRANS_ENABLE;
  1696. I915_WRITE(reg, val);
  1697. /* wait for PCH transcoder off, transcoder state */
  1698. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1699. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1700. if (!HAS_PCH_IBX(dev)) {
  1701. /* Workaround: Clear the timing override chicken bit again. */
  1702. reg = TRANS_CHICKEN2(pipe);
  1703. val = I915_READ(reg);
  1704. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1705. I915_WRITE(reg, val);
  1706. }
  1707. }
  1708. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1709. {
  1710. u32 val;
  1711. val = I915_READ(LPT_TRANSCONF);
  1712. val &= ~TRANS_ENABLE;
  1713. I915_WRITE(LPT_TRANSCONF, val);
  1714. /* wait for PCH transcoder off, transcoder state */
  1715. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1716. DRM_ERROR("Failed to disable PCH transcoder\n");
  1717. /* Workaround: clear timing override bit. */
  1718. val = I915_READ(_TRANSA_CHICKEN2);
  1719. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1720. I915_WRITE(_TRANSA_CHICKEN2, val);
  1721. }
  1722. /**
  1723. * intel_enable_pipe - enable a pipe, asserting requirements
  1724. * @crtc: crtc responsible for the pipe
  1725. *
  1726. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1727. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1728. */
  1729. static void intel_enable_pipe(struct intel_crtc *crtc)
  1730. {
  1731. struct drm_device *dev = crtc->base.dev;
  1732. struct drm_i915_private *dev_priv = dev->dev_private;
  1733. enum pipe pipe = crtc->pipe;
  1734. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1735. pipe);
  1736. enum pipe pch_transcoder;
  1737. int reg;
  1738. u32 val;
  1739. assert_planes_disabled(dev_priv, pipe);
  1740. assert_cursor_disabled(dev_priv, pipe);
  1741. assert_sprites_disabled(dev_priv, pipe);
  1742. if (HAS_PCH_LPT(dev_priv->dev))
  1743. pch_transcoder = TRANSCODER_A;
  1744. else
  1745. pch_transcoder = pipe;
  1746. /*
  1747. * A pipe without a PLL won't actually be able to drive bits from
  1748. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1749. * need the check.
  1750. */
  1751. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1752. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1753. assert_dsi_pll_enabled(dev_priv);
  1754. else
  1755. assert_pll_enabled(dev_priv, pipe);
  1756. else {
  1757. if (crtc->config.has_pch_encoder) {
  1758. /* if driving the PCH, we need FDI enabled */
  1759. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1760. assert_fdi_tx_pll_enabled(dev_priv,
  1761. (enum pipe) cpu_transcoder);
  1762. }
  1763. /* FIXME: assert CPU port conditions for SNB+ */
  1764. }
  1765. reg = PIPECONF(cpu_transcoder);
  1766. val = I915_READ(reg);
  1767. if (val & PIPECONF_ENABLE) {
  1768. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1769. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1770. return;
  1771. }
  1772. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1773. POSTING_READ(reg);
  1774. }
  1775. /**
  1776. * intel_disable_pipe - disable a pipe, asserting requirements
  1777. * @crtc: crtc whose pipes is to be disabled
  1778. *
  1779. * Disable the pipe of @crtc, making sure that various hardware
  1780. * specific requirements are met, if applicable, e.g. plane
  1781. * disabled, panel fitter off, etc.
  1782. *
  1783. * Will wait until the pipe has shut down before returning.
  1784. */
  1785. static void intel_disable_pipe(struct intel_crtc *crtc)
  1786. {
  1787. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1788. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  1789. enum pipe pipe = crtc->pipe;
  1790. int reg;
  1791. u32 val;
  1792. /*
  1793. * Make sure planes won't keep trying to pump pixels to us,
  1794. * or we might hang the display.
  1795. */
  1796. assert_planes_disabled(dev_priv, pipe);
  1797. assert_cursor_disabled(dev_priv, pipe);
  1798. assert_sprites_disabled(dev_priv, pipe);
  1799. reg = PIPECONF(cpu_transcoder);
  1800. val = I915_READ(reg);
  1801. if ((val & PIPECONF_ENABLE) == 0)
  1802. return;
  1803. /*
  1804. * Double wide has implications for planes
  1805. * so best keep it disabled when not needed.
  1806. */
  1807. if (crtc->config.double_wide)
  1808. val &= ~PIPECONF_DOUBLE_WIDE;
  1809. /* Don't disable pipe or pipe PLLs if needed */
  1810. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1811. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1812. val &= ~PIPECONF_ENABLE;
  1813. I915_WRITE(reg, val);
  1814. if ((val & PIPECONF_ENABLE) == 0)
  1815. intel_wait_for_pipe_off(crtc);
  1816. }
  1817. /*
  1818. * Plane regs are double buffered, going from enabled->disabled needs a
  1819. * trigger in order to latch. The display address reg provides this.
  1820. */
  1821. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1822. enum plane plane)
  1823. {
  1824. struct drm_device *dev = dev_priv->dev;
  1825. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1826. I915_WRITE(reg, I915_READ(reg));
  1827. POSTING_READ(reg);
  1828. }
  1829. /**
  1830. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1831. * @plane: plane to be enabled
  1832. * @crtc: crtc for the plane
  1833. *
  1834. * Enable @plane on @crtc, making sure that the pipe is running first.
  1835. */
  1836. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1837. struct drm_crtc *crtc)
  1838. {
  1839. struct drm_device *dev = plane->dev;
  1840. struct drm_i915_private *dev_priv = dev->dev_private;
  1841. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1842. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1843. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1844. if (intel_crtc->primary_enabled)
  1845. return;
  1846. intel_crtc->primary_enabled = true;
  1847. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1848. crtc->x, crtc->y);
  1849. /*
  1850. * BDW signals flip done immediately if the plane
  1851. * is disabled, even if the plane enable is already
  1852. * armed to occur at the next vblank :(
  1853. */
  1854. if (IS_BROADWELL(dev))
  1855. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1856. }
  1857. /**
  1858. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1859. * @plane: plane to be disabled
  1860. * @crtc: crtc for the plane
  1861. *
  1862. * Disable @plane on @crtc, making sure that the pipe is running first.
  1863. */
  1864. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1865. struct drm_crtc *crtc)
  1866. {
  1867. struct drm_device *dev = plane->dev;
  1868. struct drm_i915_private *dev_priv = dev->dev_private;
  1869. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1870. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1871. if (!intel_crtc->primary_enabled)
  1872. return;
  1873. intel_crtc->primary_enabled = false;
  1874. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1875. crtc->x, crtc->y);
  1876. }
  1877. static bool need_vtd_wa(struct drm_device *dev)
  1878. {
  1879. #ifdef CONFIG_INTEL_IOMMU
  1880. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1881. return true;
  1882. #endif
  1883. return false;
  1884. }
  1885. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1886. {
  1887. int tile_height;
  1888. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1889. return ALIGN(height, tile_height);
  1890. }
  1891. int
  1892. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1893. struct drm_framebuffer *fb,
  1894. struct intel_engine_cs *pipelined)
  1895. {
  1896. struct drm_device *dev = fb->dev;
  1897. struct drm_i915_private *dev_priv = dev->dev_private;
  1898. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1899. u32 alignment;
  1900. int ret;
  1901. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1902. switch (obj->tiling_mode) {
  1903. case I915_TILING_NONE:
  1904. if (INTEL_INFO(dev)->gen >= 9)
  1905. alignment = 256 * 1024;
  1906. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1907. alignment = 128 * 1024;
  1908. else if (INTEL_INFO(dev)->gen >= 4)
  1909. alignment = 4 * 1024;
  1910. else
  1911. alignment = 64 * 1024;
  1912. break;
  1913. case I915_TILING_X:
  1914. if (INTEL_INFO(dev)->gen >= 9)
  1915. alignment = 256 * 1024;
  1916. else {
  1917. /* pin() will align the object as required by fence */
  1918. alignment = 0;
  1919. }
  1920. break;
  1921. case I915_TILING_Y:
  1922. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1923. return -EINVAL;
  1924. default:
  1925. BUG();
  1926. }
  1927. /* Note that the w/a also requires 64 PTE of padding following the
  1928. * bo. We currently fill all unused PTE with the shadow page and so
  1929. * we should always have valid PTE following the scanout preventing
  1930. * the VT-d warning.
  1931. */
  1932. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1933. alignment = 256 * 1024;
  1934. /*
  1935. * Global gtt pte registers are special registers which actually forward
  1936. * writes to a chunk of system memory. Which means that there is no risk
  1937. * that the register values disappear as soon as we call
  1938. * intel_runtime_pm_put(), so it is correct to wrap only the
  1939. * pin/unpin/fence and not more.
  1940. */
  1941. intel_runtime_pm_get(dev_priv);
  1942. dev_priv->mm.interruptible = false;
  1943. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1944. if (ret)
  1945. goto err_interruptible;
  1946. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1947. * fence, whereas 965+ only requires a fence if using
  1948. * framebuffer compression. For simplicity, we always install
  1949. * a fence as the cost is not that onerous.
  1950. */
  1951. ret = i915_gem_object_get_fence(obj);
  1952. if (ret)
  1953. goto err_unpin;
  1954. i915_gem_object_pin_fence(obj);
  1955. dev_priv->mm.interruptible = true;
  1956. intel_runtime_pm_put(dev_priv);
  1957. return 0;
  1958. err_unpin:
  1959. i915_gem_object_unpin_from_display_plane(obj);
  1960. err_interruptible:
  1961. dev_priv->mm.interruptible = true;
  1962. intel_runtime_pm_put(dev_priv);
  1963. return ret;
  1964. }
  1965. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1966. {
  1967. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1968. i915_gem_object_unpin_fence(obj);
  1969. i915_gem_object_unpin_from_display_plane(obj);
  1970. }
  1971. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1972. * is assumed to be a power-of-two. */
  1973. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1974. unsigned int tiling_mode,
  1975. unsigned int cpp,
  1976. unsigned int pitch)
  1977. {
  1978. if (tiling_mode != I915_TILING_NONE) {
  1979. unsigned int tile_rows, tiles;
  1980. tile_rows = *y / 8;
  1981. *y %= 8;
  1982. tiles = *x / (512/cpp);
  1983. *x %= 512/cpp;
  1984. return tile_rows * pitch * 8 + tiles * 4096;
  1985. } else {
  1986. unsigned int offset;
  1987. offset = *y * pitch + *x * cpp;
  1988. *y = 0;
  1989. *x = (offset & 4095) / cpp;
  1990. return offset & -4096;
  1991. }
  1992. }
  1993. int intel_format_to_fourcc(int format)
  1994. {
  1995. switch (format) {
  1996. case DISPPLANE_8BPP:
  1997. return DRM_FORMAT_C8;
  1998. case DISPPLANE_BGRX555:
  1999. return DRM_FORMAT_XRGB1555;
  2000. case DISPPLANE_BGRX565:
  2001. return DRM_FORMAT_RGB565;
  2002. default:
  2003. case DISPPLANE_BGRX888:
  2004. return DRM_FORMAT_XRGB8888;
  2005. case DISPPLANE_RGBX888:
  2006. return DRM_FORMAT_XBGR8888;
  2007. case DISPPLANE_BGRX101010:
  2008. return DRM_FORMAT_XRGB2101010;
  2009. case DISPPLANE_RGBX101010:
  2010. return DRM_FORMAT_XBGR2101010;
  2011. }
  2012. }
  2013. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  2014. struct intel_plane_config *plane_config)
  2015. {
  2016. struct drm_device *dev = crtc->base.dev;
  2017. struct drm_i915_gem_object *obj = NULL;
  2018. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2019. u32 base = plane_config->base;
  2020. if (plane_config->size == 0)
  2021. return false;
  2022. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2023. plane_config->size);
  2024. if (!obj)
  2025. return false;
  2026. if (plane_config->tiled) {
  2027. obj->tiling_mode = I915_TILING_X;
  2028. obj->stride = crtc->base.primary->fb->pitches[0];
  2029. }
  2030. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2031. mode_cmd.width = crtc->base.primary->fb->width;
  2032. mode_cmd.height = crtc->base.primary->fb->height;
  2033. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2034. mutex_lock(&dev->struct_mutex);
  2035. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2036. &mode_cmd, obj)) {
  2037. DRM_DEBUG_KMS("intel fb init failed\n");
  2038. goto out_unref_obj;
  2039. }
  2040. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2041. mutex_unlock(&dev->struct_mutex);
  2042. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2043. return true;
  2044. out_unref_obj:
  2045. drm_gem_object_unreference(&obj->base);
  2046. mutex_unlock(&dev->struct_mutex);
  2047. return false;
  2048. }
  2049. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2050. struct intel_plane_config *plane_config)
  2051. {
  2052. struct drm_device *dev = intel_crtc->base.dev;
  2053. struct drm_i915_private *dev_priv = dev->dev_private;
  2054. struct drm_crtc *c;
  2055. struct intel_crtc *i;
  2056. struct drm_i915_gem_object *obj;
  2057. if (!intel_crtc->base.primary->fb)
  2058. return;
  2059. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2060. return;
  2061. kfree(intel_crtc->base.primary->fb);
  2062. intel_crtc->base.primary->fb = NULL;
  2063. /*
  2064. * Failed to alloc the obj, check to see if we should share
  2065. * an fb with another CRTC instead
  2066. */
  2067. for_each_crtc(dev, c) {
  2068. i = to_intel_crtc(c);
  2069. if (c == &intel_crtc->base)
  2070. continue;
  2071. if (!i->active)
  2072. continue;
  2073. obj = intel_fb_obj(c->primary->fb);
  2074. if (obj == NULL)
  2075. continue;
  2076. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2077. if (obj->tiling_mode != I915_TILING_NONE)
  2078. dev_priv->preserve_bios_swizzle = true;
  2079. drm_framebuffer_reference(c->primary->fb);
  2080. intel_crtc->base.primary->fb = c->primary->fb;
  2081. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2082. break;
  2083. }
  2084. }
  2085. }
  2086. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2087. struct drm_framebuffer *fb,
  2088. int x, int y)
  2089. {
  2090. struct drm_device *dev = crtc->dev;
  2091. struct drm_i915_private *dev_priv = dev->dev_private;
  2092. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2093. struct drm_i915_gem_object *obj;
  2094. int plane = intel_crtc->plane;
  2095. unsigned long linear_offset;
  2096. u32 dspcntr;
  2097. u32 reg = DSPCNTR(plane);
  2098. int pixel_size;
  2099. if (!intel_crtc->primary_enabled) {
  2100. I915_WRITE(reg, 0);
  2101. if (INTEL_INFO(dev)->gen >= 4)
  2102. I915_WRITE(DSPSURF(plane), 0);
  2103. else
  2104. I915_WRITE(DSPADDR(plane), 0);
  2105. POSTING_READ(reg);
  2106. return;
  2107. }
  2108. obj = intel_fb_obj(fb);
  2109. if (WARN_ON(obj == NULL))
  2110. return;
  2111. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2112. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2113. dspcntr |= DISPLAY_PLANE_ENABLE;
  2114. if (INTEL_INFO(dev)->gen < 4) {
  2115. if (intel_crtc->pipe == PIPE_B)
  2116. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2117. /* pipesrc and dspsize control the size that is scaled from,
  2118. * which should always be the user's requested size.
  2119. */
  2120. I915_WRITE(DSPSIZE(plane),
  2121. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2122. (intel_crtc->config.pipe_src_w - 1));
  2123. I915_WRITE(DSPPOS(plane), 0);
  2124. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2125. I915_WRITE(PRIMSIZE(plane),
  2126. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2127. (intel_crtc->config.pipe_src_w - 1));
  2128. I915_WRITE(PRIMPOS(plane), 0);
  2129. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2130. }
  2131. switch (fb->pixel_format) {
  2132. case DRM_FORMAT_C8:
  2133. dspcntr |= DISPPLANE_8BPP;
  2134. break;
  2135. case DRM_FORMAT_XRGB1555:
  2136. case DRM_FORMAT_ARGB1555:
  2137. dspcntr |= DISPPLANE_BGRX555;
  2138. break;
  2139. case DRM_FORMAT_RGB565:
  2140. dspcntr |= DISPPLANE_BGRX565;
  2141. break;
  2142. case DRM_FORMAT_XRGB8888:
  2143. case DRM_FORMAT_ARGB8888:
  2144. dspcntr |= DISPPLANE_BGRX888;
  2145. break;
  2146. case DRM_FORMAT_XBGR8888:
  2147. case DRM_FORMAT_ABGR8888:
  2148. dspcntr |= DISPPLANE_RGBX888;
  2149. break;
  2150. case DRM_FORMAT_XRGB2101010:
  2151. case DRM_FORMAT_ARGB2101010:
  2152. dspcntr |= DISPPLANE_BGRX101010;
  2153. break;
  2154. case DRM_FORMAT_XBGR2101010:
  2155. case DRM_FORMAT_ABGR2101010:
  2156. dspcntr |= DISPPLANE_RGBX101010;
  2157. break;
  2158. default:
  2159. BUG();
  2160. }
  2161. if (INTEL_INFO(dev)->gen >= 4 &&
  2162. obj->tiling_mode != I915_TILING_NONE)
  2163. dspcntr |= DISPPLANE_TILED;
  2164. if (IS_G4X(dev))
  2165. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2166. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2167. if (INTEL_INFO(dev)->gen >= 4) {
  2168. intel_crtc->dspaddr_offset =
  2169. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2170. pixel_size,
  2171. fb->pitches[0]);
  2172. linear_offset -= intel_crtc->dspaddr_offset;
  2173. } else {
  2174. intel_crtc->dspaddr_offset = linear_offset;
  2175. }
  2176. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2177. dspcntr |= DISPPLANE_ROTATE_180;
  2178. x += (intel_crtc->config.pipe_src_w - 1);
  2179. y += (intel_crtc->config.pipe_src_h - 1);
  2180. /* Finding the last pixel of the last line of the display
  2181. data and adding to linear_offset*/
  2182. linear_offset +=
  2183. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2184. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2185. }
  2186. I915_WRITE(reg, dspcntr);
  2187. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2188. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2189. fb->pitches[0]);
  2190. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2191. if (INTEL_INFO(dev)->gen >= 4) {
  2192. I915_WRITE(DSPSURF(plane),
  2193. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2194. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2195. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2196. } else
  2197. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2198. POSTING_READ(reg);
  2199. }
  2200. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2201. struct drm_framebuffer *fb,
  2202. int x, int y)
  2203. {
  2204. struct drm_device *dev = crtc->dev;
  2205. struct drm_i915_private *dev_priv = dev->dev_private;
  2206. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2207. struct drm_i915_gem_object *obj;
  2208. int plane = intel_crtc->plane;
  2209. unsigned long linear_offset;
  2210. u32 dspcntr;
  2211. u32 reg = DSPCNTR(plane);
  2212. int pixel_size;
  2213. if (!intel_crtc->primary_enabled) {
  2214. I915_WRITE(reg, 0);
  2215. I915_WRITE(DSPSURF(plane), 0);
  2216. POSTING_READ(reg);
  2217. return;
  2218. }
  2219. obj = intel_fb_obj(fb);
  2220. if (WARN_ON(obj == NULL))
  2221. return;
  2222. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2223. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2224. dspcntr |= DISPLAY_PLANE_ENABLE;
  2225. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2226. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2227. switch (fb->pixel_format) {
  2228. case DRM_FORMAT_C8:
  2229. dspcntr |= DISPPLANE_8BPP;
  2230. break;
  2231. case DRM_FORMAT_RGB565:
  2232. dspcntr |= DISPPLANE_BGRX565;
  2233. break;
  2234. case DRM_FORMAT_XRGB8888:
  2235. case DRM_FORMAT_ARGB8888:
  2236. dspcntr |= DISPPLANE_BGRX888;
  2237. break;
  2238. case DRM_FORMAT_XBGR8888:
  2239. case DRM_FORMAT_ABGR8888:
  2240. dspcntr |= DISPPLANE_RGBX888;
  2241. break;
  2242. case DRM_FORMAT_XRGB2101010:
  2243. case DRM_FORMAT_ARGB2101010:
  2244. dspcntr |= DISPPLANE_BGRX101010;
  2245. break;
  2246. case DRM_FORMAT_XBGR2101010:
  2247. case DRM_FORMAT_ABGR2101010:
  2248. dspcntr |= DISPPLANE_RGBX101010;
  2249. break;
  2250. default:
  2251. BUG();
  2252. }
  2253. if (obj->tiling_mode != I915_TILING_NONE)
  2254. dspcntr |= DISPPLANE_TILED;
  2255. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2256. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2257. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2258. intel_crtc->dspaddr_offset =
  2259. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2260. pixel_size,
  2261. fb->pitches[0]);
  2262. linear_offset -= intel_crtc->dspaddr_offset;
  2263. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2264. dspcntr |= DISPPLANE_ROTATE_180;
  2265. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2266. x += (intel_crtc->config.pipe_src_w - 1);
  2267. y += (intel_crtc->config.pipe_src_h - 1);
  2268. /* Finding the last pixel of the last line of the display
  2269. data and adding to linear_offset*/
  2270. linear_offset +=
  2271. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2272. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2273. }
  2274. }
  2275. I915_WRITE(reg, dspcntr);
  2276. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2277. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2278. fb->pitches[0]);
  2279. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2280. I915_WRITE(DSPSURF(plane),
  2281. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2282. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2283. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2284. } else {
  2285. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2286. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2287. }
  2288. POSTING_READ(reg);
  2289. }
  2290. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2291. struct drm_framebuffer *fb,
  2292. int x, int y)
  2293. {
  2294. struct drm_device *dev = crtc->dev;
  2295. struct drm_i915_private *dev_priv = dev->dev_private;
  2296. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2297. struct intel_framebuffer *intel_fb;
  2298. struct drm_i915_gem_object *obj;
  2299. int pipe = intel_crtc->pipe;
  2300. u32 plane_ctl, stride;
  2301. if (!intel_crtc->primary_enabled) {
  2302. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2303. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2304. POSTING_READ(PLANE_CTL(pipe, 0));
  2305. return;
  2306. }
  2307. plane_ctl = PLANE_CTL_ENABLE |
  2308. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2309. PLANE_CTL_PIPE_CSC_ENABLE;
  2310. switch (fb->pixel_format) {
  2311. case DRM_FORMAT_RGB565:
  2312. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2313. break;
  2314. case DRM_FORMAT_XRGB8888:
  2315. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2316. break;
  2317. case DRM_FORMAT_XBGR8888:
  2318. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2319. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2320. break;
  2321. case DRM_FORMAT_XRGB2101010:
  2322. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2323. break;
  2324. case DRM_FORMAT_XBGR2101010:
  2325. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2326. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2327. break;
  2328. default:
  2329. BUG();
  2330. }
  2331. intel_fb = to_intel_framebuffer(fb);
  2332. obj = intel_fb->obj;
  2333. /*
  2334. * The stride is either expressed as a multiple of 64 bytes chunks for
  2335. * linear buffers or in number of tiles for tiled buffers.
  2336. */
  2337. switch (obj->tiling_mode) {
  2338. case I915_TILING_NONE:
  2339. stride = fb->pitches[0] >> 6;
  2340. break;
  2341. case I915_TILING_X:
  2342. plane_ctl |= PLANE_CTL_TILED_X;
  2343. stride = fb->pitches[0] >> 9;
  2344. break;
  2345. default:
  2346. BUG();
  2347. }
  2348. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2349. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180))
  2350. plane_ctl |= PLANE_CTL_ROTATE_180;
  2351. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2352. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2353. i915_gem_obj_ggtt_offset(obj),
  2354. x, y, fb->width, fb->height,
  2355. fb->pitches[0]);
  2356. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2357. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2358. I915_WRITE(PLANE_SIZE(pipe, 0),
  2359. (intel_crtc->config.pipe_src_h - 1) << 16 |
  2360. (intel_crtc->config.pipe_src_w - 1));
  2361. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2362. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2363. POSTING_READ(PLANE_SURF(pipe, 0));
  2364. }
  2365. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2366. static int
  2367. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2368. int x, int y, enum mode_set_atomic state)
  2369. {
  2370. struct drm_device *dev = crtc->dev;
  2371. struct drm_i915_private *dev_priv = dev->dev_private;
  2372. if (dev_priv->display.disable_fbc)
  2373. dev_priv->display.disable_fbc(dev);
  2374. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2375. return 0;
  2376. }
  2377. static void intel_complete_page_flips(struct drm_device *dev)
  2378. {
  2379. struct drm_crtc *crtc;
  2380. for_each_crtc(dev, crtc) {
  2381. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2382. enum plane plane = intel_crtc->plane;
  2383. intel_prepare_page_flip(dev, plane);
  2384. intel_finish_page_flip_plane(dev, plane);
  2385. }
  2386. }
  2387. static void intel_update_primary_planes(struct drm_device *dev)
  2388. {
  2389. struct drm_i915_private *dev_priv = dev->dev_private;
  2390. struct drm_crtc *crtc;
  2391. for_each_crtc(dev, crtc) {
  2392. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2393. drm_modeset_lock(&crtc->mutex, NULL);
  2394. /*
  2395. * FIXME: Once we have proper support for primary planes (and
  2396. * disabling them without disabling the entire crtc) allow again
  2397. * a NULL crtc->primary->fb.
  2398. */
  2399. if (intel_crtc->active && crtc->primary->fb)
  2400. dev_priv->display.update_primary_plane(crtc,
  2401. crtc->primary->fb,
  2402. crtc->x,
  2403. crtc->y);
  2404. drm_modeset_unlock(&crtc->mutex);
  2405. }
  2406. }
  2407. void intel_prepare_reset(struct drm_device *dev)
  2408. {
  2409. struct drm_i915_private *dev_priv = to_i915(dev);
  2410. struct intel_crtc *crtc;
  2411. /* no reset support for gen2 */
  2412. if (IS_GEN2(dev))
  2413. return;
  2414. /* reset doesn't touch the display */
  2415. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2416. return;
  2417. drm_modeset_lock_all(dev);
  2418. /*
  2419. * Disabling the crtcs gracefully seems nicer. Also the
  2420. * g33 docs say we should at least disable all the planes.
  2421. */
  2422. for_each_intel_crtc(dev, crtc) {
  2423. if (crtc->active)
  2424. dev_priv->display.crtc_disable(&crtc->base);
  2425. }
  2426. }
  2427. void intel_finish_reset(struct drm_device *dev)
  2428. {
  2429. struct drm_i915_private *dev_priv = to_i915(dev);
  2430. /*
  2431. * Flips in the rings will be nuked by the reset,
  2432. * so complete all pending flips so that user space
  2433. * will get its events and not get stuck.
  2434. */
  2435. intel_complete_page_flips(dev);
  2436. /* no reset support for gen2 */
  2437. if (IS_GEN2(dev))
  2438. return;
  2439. /* reset doesn't touch the display */
  2440. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2441. /*
  2442. * Flips in the rings have been nuked by the reset,
  2443. * so update the base address of all primary
  2444. * planes to the the last fb to make sure we're
  2445. * showing the correct fb after a reset.
  2446. */
  2447. intel_update_primary_planes(dev);
  2448. return;
  2449. }
  2450. /*
  2451. * The display has been reset as well,
  2452. * so need a full re-initialization.
  2453. */
  2454. intel_runtime_pm_disable_interrupts(dev_priv);
  2455. intel_runtime_pm_enable_interrupts(dev_priv);
  2456. intel_modeset_init_hw(dev);
  2457. spin_lock_irq(&dev_priv->irq_lock);
  2458. if (dev_priv->display.hpd_irq_setup)
  2459. dev_priv->display.hpd_irq_setup(dev);
  2460. spin_unlock_irq(&dev_priv->irq_lock);
  2461. intel_modeset_setup_hw_state(dev, true);
  2462. intel_hpd_init(dev_priv);
  2463. drm_modeset_unlock_all(dev);
  2464. }
  2465. static int
  2466. intel_finish_fb(struct drm_framebuffer *old_fb)
  2467. {
  2468. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2469. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2470. bool was_interruptible = dev_priv->mm.interruptible;
  2471. int ret;
  2472. /* Big Hammer, we also need to ensure that any pending
  2473. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2474. * current scanout is retired before unpinning the old
  2475. * framebuffer.
  2476. *
  2477. * This should only fail upon a hung GPU, in which case we
  2478. * can safely continue.
  2479. */
  2480. dev_priv->mm.interruptible = false;
  2481. ret = i915_gem_object_finish_gpu(obj);
  2482. dev_priv->mm.interruptible = was_interruptible;
  2483. return ret;
  2484. }
  2485. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2486. {
  2487. struct drm_device *dev = crtc->dev;
  2488. struct drm_i915_private *dev_priv = dev->dev_private;
  2489. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2490. bool pending;
  2491. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2492. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2493. return false;
  2494. spin_lock_irq(&dev->event_lock);
  2495. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2496. spin_unlock_irq(&dev->event_lock);
  2497. return pending;
  2498. }
  2499. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2500. {
  2501. struct drm_device *dev = crtc->base.dev;
  2502. struct drm_i915_private *dev_priv = dev->dev_private;
  2503. const struct drm_display_mode *adjusted_mode;
  2504. if (!i915.fastboot)
  2505. return;
  2506. /*
  2507. * Update pipe size and adjust fitter if needed: the reason for this is
  2508. * that in compute_mode_changes we check the native mode (not the pfit
  2509. * mode) to see if we can flip rather than do a full mode set. In the
  2510. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2511. * pfit state, we'll end up with a big fb scanned out into the wrong
  2512. * sized surface.
  2513. *
  2514. * To fix this properly, we need to hoist the checks up into
  2515. * compute_mode_changes (or above), check the actual pfit state and
  2516. * whether the platform allows pfit disable with pipe active, and only
  2517. * then update the pipesrc and pfit state, even on the flip path.
  2518. */
  2519. adjusted_mode = &crtc->config.adjusted_mode;
  2520. I915_WRITE(PIPESRC(crtc->pipe),
  2521. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2522. (adjusted_mode->crtc_vdisplay - 1));
  2523. if (!crtc->config.pch_pfit.enabled &&
  2524. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2525. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2526. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2527. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2528. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2529. }
  2530. crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2531. crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2532. }
  2533. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2534. {
  2535. struct drm_device *dev = crtc->dev;
  2536. struct drm_i915_private *dev_priv = dev->dev_private;
  2537. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2538. int pipe = intel_crtc->pipe;
  2539. u32 reg, temp;
  2540. /* enable normal train */
  2541. reg = FDI_TX_CTL(pipe);
  2542. temp = I915_READ(reg);
  2543. if (IS_IVYBRIDGE(dev)) {
  2544. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2545. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2546. } else {
  2547. temp &= ~FDI_LINK_TRAIN_NONE;
  2548. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2549. }
  2550. I915_WRITE(reg, temp);
  2551. reg = FDI_RX_CTL(pipe);
  2552. temp = I915_READ(reg);
  2553. if (HAS_PCH_CPT(dev)) {
  2554. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2555. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2556. } else {
  2557. temp &= ~FDI_LINK_TRAIN_NONE;
  2558. temp |= FDI_LINK_TRAIN_NONE;
  2559. }
  2560. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2561. /* wait one idle pattern time */
  2562. POSTING_READ(reg);
  2563. udelay(1000);
  2564. /* IVB wants error correction enabled */
  2565. if (IS_IVYBRIDGE(dev))
  2566. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2567. FDI_FE_ERRC_ENABLE);
  2568. }
  2569. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2570. {
  2571. return crtc->base.enabled && crtc->active &&
  2572. crtc->config.has_pch_encoder;
  2573. }
  2574. static void ivb_modeset_global_resources(struct drm_device *dev)
  2575. {
  2576. struct drm_i915_private *dev_priv = dev->dev_private;
  2577. struct intel_crtc *pipe_B_crtc =
  2578. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2579. struct intel_crtc *pipe_C_crtc =
  2580. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2581. uint32_t temp;
  2582. /*
  2583. * When everything is off disable fdi C so that we could enable fdi B
  2584. * with all lanes. Note that we don't care about enabled pipes without
  2585. * an enabled pch encoder.
  2586. */
  2587. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2588. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2589. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2590. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2591. temp = I915_READ(SOUTH_CHICKEN1);
  2592. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2593. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2594. I915_WRITE(SOUTH_CHICKEN1, temp);
  2595. }
  2596. }
  2597. /* The FDI link training functions for ILK/Ibexpeak. */
  2598. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2599. {
  2600. struct drm_device *dev = crtc->dev;
  2601. struct drm_i915_private *dev_priv = dev->dev_private;
  2602. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2603. int pipe = intel_crtc->pipe;
  2604. u32 reg, temp, tries;
  2605. /* FDI needs bits from pipe first */
  2606. assert_pipe_enabled(dev_priv, pipe);
  2607. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2608. for train result */
  2609. reg = FDI_RX_IMR(pipe);
  2610. temp = I915_READ(reg);
  2611. temp &= ~FDI_RX_SYMBOL_LOCK;
  2612. temp &= ~FDI_RX_BIT_LOCK;
  2613. I915_WRITE(reg, temp);
  2614. I915_READ(reg);
  2615. udelay(150);
  2616. /* enable CPU FDI TX and PCH FDI RX */
  2617. reg = FDI_TX_CTL(pipe);
  2618. temp = I915_READ(reg);
  2619. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2620. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2621. temp &= ~FDI_LINK_TRAIN_NONE;
  2622. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2623. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2624. reg = FDI_RX_CTL(pipe);
  2625. temp = I915_READ(reg);
  2626. temp &= ~FDI_LINK_TRAIN_NONE;
  2627. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2628. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2629. POSTING_READ(reg);
  2630. udelay(150);
  2631. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2632. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2633. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2634. FDI_RX_PHASE_SYNC_POINTER_EN);
  2635. reg = FDI_RX_IIR(pipe);
  2636. for (tries = 0; tries < 5; tries++) {
  2637. temp = I915_READ(reg);
  2638. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2639. if ((temp & FDI_RX_BIT_LOCK)) {
  2640. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2641. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2642. break;
  2643. }
  2644. }
  2645. if (tries == 5)
  2646. DRM_ERROR("FDI train 1 fail!\n");
  2647. /* Train 2 */
  2648. reg = FDI_TX_CTL(pipe);
  2649. temp = I915_READ(reg);
  2650. temp &= ~FDI_LINK_TRAIN_NONE;
  2651. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2652. I915_WRITE(reg, temp);
  2653. reg = FDI_RX_CTL(pipe);
  2654. temp = I915_READ(reg);
  2655. temp &= ~FDI_LINK_TRAIN_NONE;
  2656. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2657. I915_WRITE(reg, temp);
  2658. POSTING_READ(reg);
  2659. udelay(150);
  2660. reg = FDI_RX_IIR(pipe);
  2661. for (tries = 0; tries < 5; tries++) {
  2662. temp = I915_READ(reg);
  2663. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2664. if (temp & FDI_RX_SYMBOL_LOCK) {
  2665. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2666. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2667. break;
  2668. }
  2669. }
  2670. if (tries == 5)
  2671. DRM_ERROR("FDI train 2 fail!\n");
  2672. DRM_DEBUG_KMS("FDI train done\n");
  2673. }
  2674. static const int snb_b_fdi_train_param[] = {
  2675. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2676. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2677. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2678. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2679. };
  2680. /* The FDI link training functions for SNB/Cougarpoint. */
  2681. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2682. {
  2683. struct drm_device *dev = crtc->dev;
  2684. struct drm_i915_private *dev_priv = dev->dev_private;
  2685. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2686. int pipe = intel_crtc->pipe;
  2687. u32 reg, temp, i, retry;
  2688. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2689. for train result */
  2690. reg = FDI_RX_IMR(pipe);
  2691. temp = I915_READ(reg);
  2692. temp &= ~FDI_RX_SYMBOL_LOCK;
  2693. temp &= ~FDI_RX_BIT_LOCK;
  2694. I915_WRITE(reg, temp);
  2695. POSTING_READ(reg);
  2696. udelay(150);
  2697. /* enable CPU FDI TX and PCH FDI RX */
  2698. reg = FDI_TX_CTL(pipe);
  2699. temp = I915_READ(reg);
  2700. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2701. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2702. temp &= ~FDI_LINK_TRAIN_NONE;
  2703. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2704. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2705. /* SNB-B */
  2706. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2707. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2708. I915_WRITE(FDI_RX_MISC(pipe),
  2709. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2710. reg = FDI_RX_CTL(pipe);
  2711. temp = I915_READ(reg);
  2712. if (HAS_PCH_CPT(dev)) {
  2713. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2714. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2715. } else {
  2716. temp &= ~FDI_LINK_TRAIN_NONE;
  2717. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2718. }
  2719. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2720. POSTING_READ(reg);
  2721. udelay(150);
  2722. for (i = 0; i < 4; i++) {
  2723. reg = FDI_TX_CTL(pipe);
  2724. temp = I915_READ(reg);
  2725. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2726. temp |= snb_b_fdi_train_param[i];
  2727. I915_WRITE(reg, temp);
  2728. POSTING_READ(reg);
  2729. udelay(500);
  2730. for (retry = 0; retry < 5; retry++) {
  2731. reg = FDI_RX_IIR(pipe);
  2732. temp = I915_READ(reg);
  2733. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2734. if (temp & FDI_RX_BIT_LOCK) {
  2735. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2736. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2737. break;
  2738. }
  2739. udelay(50);
  2740. }
  2741. if (retry < 5)
  2742. break;
  2743. }
  2744. if (i == 4)
  2745. DRM_ERROR("FDI train 1 fail!\n");
  2746. /* Train 2 */
  2747. reg = FDI_TX_CTL(pipe);
  2748. temp = I915_READ(reg);
  2749. temp &= ~FDI_LINK_TRAIN_NONE;
  2750. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2751. if (IS_GEN6(dev)) {
  2752. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2753. /* SNB-B */
  2754. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2755. }
  2756. I915_WRITE(reg, temp);
  2757. reg = FDI_RX_CTL(pipe);
  2758. temp = I915_READ(reg);
  2759. if (HAS_PCH_CPT(dev)) {
  2760. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2761. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2762. } else {
  2763. temp &= ~FDI_LINK_TRAIN_NONE;
  2764. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2765. }
  2766. I915_WRITE(reg, temp);
  2767. POSTING_READ(reg);
  2768. udelay(150);
  2769. for (i = 0; i < 4; i++) {
  2770. reg = FDI_TX_CTL(pipe);
  2771. temp = I915_READ(reg);
  2772. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2773. temp |= snb_b_fdi_train_param[i];
  2774. I915_WRITE(reg, temp);
  2775. POSTING_READ(reg);
  2776. udelay(500);
  2777. for (retry = 0; retry < 5; retry++) {
  2778. reg = FDI_RX_IIR(pipe);
  2779. temp = I915_READ(reg);
  2780. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2781. if (temp & FDI_RX_SYMBOL_LOCK) {
  2782. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2783. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2784. break;
  2785. }
  2786. udelay(50);
  2787. }
  2788. if (retry < 5)
  2789. break;
  2790. }
  2791. if (i == 4)
  2792. DRM_ERROR("FDI train 2 fail!\n");
  2793. DRM_DEBUG_KMS("FDI train done.\n");
  2794. }
  2795. /* Manual link training for Ivy Bridge A0 parts */
  2796. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2797. {
  2798. struct drm_device *dev = crtc->dev;
  2799. struct drm_i915_private *dev_priv = dev->dev_private;
  2800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2801. int pipe = intel_crtc->pipe;
  2802. u32 reg, temp, i, j;
  2803. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2804. for train result */
  2805. reg = FDI_RX_IMR(pipe);
  2806. temp = I915_READ(reg);
  2807. temp &= ~FDI_RX_SYMBOL_LOCK;
  2808. temp &= ~FDI_RX_BIT_LOCK;
  2809. I915_WRITE(reg, temp);
  2810. POSTING_READ(reg);
  2811. udelay(150);
  2812. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2813. I915_READ(FDI_RX_IIR(pipe)));
  2814. /* Try each vswing and preemphasis setting twice before moving on */
  2815. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2816. /* disable first in case we need to retry */
  2817. reg = FDI_TX_CTL(pipe);
  2818. temp = I915_READ(reg);
  2819. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2820. temp &= ~FDI_TX_ENABLE;
  2821. I915_WRITE(reg, temp);
  2822. reg = FDI_RX_CTL(pipe);
  2823. temp = I915_READ(reg);
  2824. temp &= ~FDI_LINK_TRAIN_AUTO;
  2825. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2826. temp &= ~FDI_RX_ENABLE;
  2827. I915_WRITE(reg, temp);
  2828. /* enable CPU FDI TX and PCH FDI RX */
  2829. reg = FDI_TX_CTL(pipe);
  2830. temp = I915_READ(reg);
  2831. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2832. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2833. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2834. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2835. temp |= snb_b_fdi_train_param[j/2];
  2836. temp |= FDI_COMPOSITE_SYNC;
  2837. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2838. I915_WRITE(FDI_RX_MISC(pipe),
  2839. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2840. reg = FDI_RX_CTL(pipe);
  2841. temp = I915_READ(reg);
  2842. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2843. temp |= FDI_COMPOSITE_SYNC;
  2844. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2845. POSTING_READ(reg);
  2846. udelay(1); /* should be 0.5us */
  2847. for (i = 0; i < 4; i++) {
  2848. reg = FDI_RX_IIR(pipe);
  2849. temp = I915_READ(reg);
  2850. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2851. if (temp & FDI_RX_BIT_LOCK ||
  2852. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2853. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2854. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2855. i);
  2856. break;
  2857. }
  2858. udelay(1); /* should be 0.5us */
  2859. }
  2860. if (i == 4) {
  2861. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2862. continue;
  2863. }
  2864. /* Train 2 */
  2865. reg = FDI_TX_CTL(pipe);
  2866. temp = I915_READ(reg);
  2867. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2868. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2869. I915_WRITE(reg, temp);
  2870. reg = FDI_RX_CTL(pipe);
  2871. temp = I915_READ(reg);
  2872. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2873. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2874. I915_WRITE(reg, temp);
  2875. POSTING_READ(reg);
  2876. udelay(2); /* should be 1.5us */
  2877. for (i = 0; i < 4; i++) {
  2878. reg = FDI_RX_IIR(pipe);
  2879. temp = I915_READ(reg);
  2880. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2881. if (temp & FDI_RX_SYMBOL_LOCK ||
  2882. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2883. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2884. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2885. i);
  2886. goto train_done;
  2887. }
  2888. udelay(2); /* should be 1.5us */
  2889. }
  2890. if (i == 4)
  2891. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2892. }
  2893. train_done:
  2894. DRM_DEBUG_KMS("FDI train done.\n");
  2895. }
  2896. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2897. {
  2898. struct drm_device *dev = intel_crtc->base.dev;
  2899. struct drm_i915_private *dev_priv = dev->dev_private;
  2900. int pipe = intel_crtc->pipe;
  2901. u32 reg, temp;
  2902. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2903. reg = FDI_RX_CTL(pipe);
  2904. temp = I915_READ(reg);
  2905. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2906. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2907. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2908. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2909. POSTING_READ(reg);
  2910. udelay(200);
  2911. /* Switch from Rawclk to PCDclk */
  2912. temp = I915_READ(reg);
  2913. I915_WRITE(reg, temp | FDI_PCDCLK);
  2914. POSTING_READ(reg);
  2915. udelay(200);
  2916. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2917. reg = FDI_TX_CTL(pipe);
  2918. temp = I915_READ(reg);
  2919. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2920. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2921. POSTING_READ(reg);
  2922. udelay(100);
  2923. }
  2924. }
  2925. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2926. {
  2927. struct drm_device *dev = intel_crtc->base.dev;
  2928. struct drm_i915_private *dev_priv = dev->dev_private;
  2929. int pipe = intel_crtc->pipe;
  2930. u32 reg, temp;
  2931. /* Switch from PCDclk to Rawclk */
  2932. reg = FDI_RX_CTL(pipe);
  2933. temp = I915_READ(reg);
  2934. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2935. /* Disable CPU FDI TX PLL */
  2936. reg = FDI_TX_CTL(pipe);
  2937. temp = I915_READ(reg);
  2938. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2939. POSTING_READ(reg);
  2940. udelay(100);
  2941. reg = FDI_RX_CTL(pipe);
  2942. temp = I915_READ(reg);
  2943. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2944. /* Wait for the clocks to turn off. */
  2945. POSTING_READ(reg);
  2946. udelay(100);
  2947. }
  2948. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2949. {
  2950. struct drm_device *dev = crtc->dev;
  2951. struct drm_i915_private *dev_priv = dev->dev_private;
  2952. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2953. int pipe = intel_crtc->pipe;
  2954. u32 reg, temp;
  2955. /* disable CPU FDI tx and PCH FDI rx */
  2956. reg = FDI_TX_CTL(pipe);
  2957. temp = I915_READ(reg);
  2958. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2959. POSTING_READ(reg);
  2960. reg = FDI_RX_CTL(pipe);
  2961. temp = I915_READ(reg);
  2962. temp &= ~(0x7 << 16);
  2963. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2964. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2965. POSTING_READ(reg);
  2966. udelay(100);
  2967. /* Ironlake workaround, disable clock pointer after downing FDI */
  2968. if (HAS_PCH_IBX(dev))
  2969. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2970. /* still set train pattern 1 */
  2971. reg = FDI_TX_CTL(pipe);
  2972. temp = I915_READ(reg);
  2973. temp &= ~FDI_LINK_TRAIN_NONE;
  2974. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2975. I915_WRITE(reg, temp);
  2976. reg = FDI_RX_CTL(pipe);
  2977. temp = I915_READ(reg);
  2978. if (HAS_PCH_CPT(dev)) {
  2979. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2980. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2981. } else {
  2982. temp &= ~FDI_LINK_TRAIN_NONE;
  2983. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2984. }
  2985. /* BPC in FDI rx is consistent with that in PIPECONF */
  2986. temp &= ~(0x07 << 16);
  2987. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2988. I915_WRITE(reg, temp);
  2989. POSTING_READ(reg);
  2990. udelay(100);
  2991. }
  2992. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2993. {
  2994. struct intel_crtc *crtc;
  2995. /* Note that we don't need to be called with mode_config.lock here
  2996. * as our list of CRTC objects is static for the lifetime of the
  2997. * device and so cannot disappear as we iterate. Similarly, we can
  2998. * happily treat the predicates as racy, atomic checks as userspace
  2999. * cannot claim and pin a new fb without at least acquring the
  3000. * struct_mutex and so serialising with us.
  3001. */
  3002. for_each_intel_crtc(dev, crtc) {
  3003. if (atomic_read(&crtc->unpin_work_count) == 0)
  3004. continue;
  3005. if (crtc->unpin_work)
  3006. intel_wait_for_vblank(dev, crtc->pipe);
  3007. return true;
  3008. }
  3009. return false;
  3010. }
  3011. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3012. {
  3013. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3014. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3015. /* ensure that the unpin work is consistent wrt ->pending. */
  3016. smp_rmb();
  3017. intel_crtc->unpin_work = NULL;
  3018. if (work->event)
  3019. drm_send_vblank_event(intel_crtc->base.dev,
  3020. intel_crtc->pipe,
  3021. work->event);
  3022. drm_crtc_vblank_put(&intel_crtc->base);
  3023. wake_up_all(&dev_priv->pending_flip_queue);
  3024. queue_work(dev_priv->wq, &work->work);
  3025. trace_i915_flip_complete(intel_crtc->plane,
  3026. work->pending_flip_obj);
  3027. }
  3028. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3029. {
  3030. struct drm_device *dev = crtc->dev;
  3031. struct drm_i915_private *dev_priv = dev->dev_private;
  3032. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3033. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3034. !intel_crtc_has_pending_flip(crtc),
  3035. 60*HZ) == 0)) {
  3036. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3037. spin_lock_irq(&dev->event_lock);
  3038. if (intel_crtc->unpin_work) {
  3039. WARN_ONCE(1, "Removing stuck page flip\n");
  3040. page_flip_completed(intel_crtc);
  3041. }
  3042. spin_unlock_irq(&dev->event_lock);
  3043. }
  3044. if (crtc->primary->fb) {
  3045. mutex_lock(&dev->struct_mutex);
  3046. intel_finish_fb(crtc->primary->fb);
  3047. mutex_unlock(&dev->struct_mutex);
  3048. }
  3049. }
  3050. /* Program iCLKIP clock to the desired frequency */
  3051. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3052. {
  3053. struct drm_device *dev = crtc->dev;
  3054. struct drm_i915_private *dev_priv = dev->dev_private;
  3055. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  3056. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3057. u32 temp;
  3058. mutex_lock(&dev_priv->dpio_lock);
  3059. /* It is necessary to ungate the pixclk gate prior to programming
  3060. * the divisors, and gate it back when it is done.
  3061. */
  3062. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3063. /* Disable SSCCTL */
  3064. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3065. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3066. SBI_SSCCTL_DISABLE,
  3067. SBI_ICLK);
  3068. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3069. if (clock == 20000) {
  3070. auxdiv = 1;
  3071. divsel = 0x41;
  3072. phaseinc = 0x20;
  3073. } else {
  3074. /* The iCLK virtual clock root frequency is in MHz,
  3075. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3076. * divisors, it is necessary to divide one by another, so we
  3077. * convert the virtual clock precision to KHz here for higher
  3078. * precision.
  3079. */
  3080. u32 iclk_virtual_root_freq = 172800 * 1000;
  3081. u32 iclk_pi_range = 64;
  3082. u32 desired_divisor, msb_divisor_value, pi_value;
  3083. desired_divisor = (iclk_virtual_root_freq / clock);
  3084. msb_divisor_value = desired_divisor / iclk_pi_range;
  3085. pi_value = desired_divisor % iclk_pi_range;
  3086. auxdiv = 0;
  3087. divsel = msb_divisor_value - 2;
  3088. phaseinc = pi_value;
  3089. }
  3090. /* This should not happen with any sane values */
  3091. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3092. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3093. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3094. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3095. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3096. clock,
  3097. auxdiv,
  3098. divsel,
  3099. phasedir,
  3100. phaseinc);
  3101. /* Program SSCDIVINTPHASE6 */
  3102. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3103. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3104. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3105. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3106. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3107. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3108. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3109. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3110. /* Program SSCAUXDIV */
  3111. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3112. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3113. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3114. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3115. /* Enable modulator and associated divider */
  3116. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3117. temp &= ~SBI_SSCCTL_DISABLE;
  3118. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3119. /* Wait for initialization time */
  3120. udelay(24);
  3121. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3122. mutex_unlock(&dev_priv->dpio_lock);
  3123. }
  3124. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3125. enum pipe pch_transcoder)
  3126. {
  3127. struct drm_device *dev = crtc->base.dev;
  3128. struct drm_i915_private *dev_priv = dev->dev_private;
  3129. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  3130. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3131. I915_READ(HTOTAL(cpu_transcoder)));
  3132. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3133. I915_READ(HBLANK(cpu_transcoder)));
  3134. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3135. I915_READ(HSYNC(cpu_transcoder)));
  3136. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3137. I915_READ(VTOTAL(cpu_transcoder)));
  3138. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3139. I915_READ(VBLANK(cpu_transcoder)));
  3140. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3141. I915_READ(VSYNC(cpu_transcoder)));
  3142. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3143. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3144. }
  3145. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3146. {
  3147. struct drm_i915_private *dev_priv = dev->dev_private;
  3148. uint32_t temp;
  3149. temp = I915_READ(SOUTH_CHICKEN1);
  3150. if (temp & FDI_BC_BIFURCATION_SELECT)
  3151. return;
  3152. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3153. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3154. temp |= FDI_BC_BIFURCATION_SELECT;
  3155. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3156. I915_WRITE(SOUTH_CHICKEN1, temp);
  3157. POSTING_READ(SOUTH_CHICKEN1);
  3158. }
  3159. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3160. {
  3161. struct drm_device *dev = intel_crtc->base.dev;
  3162. struct drm_i915_private *dev_priv = dev->dev_private;
  3163. switch (intel_crtc->pipe) {
  3164. case PIPE_A:
  3165. break;
  3166. case PIPE_B:
  3167. if (intel_crtc->config.fdi_lanes > 2)
  3168. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3169. else
  3170. cpt_enable_fdi_bc_bifurcation(dev);
  3171. break;
  3172. case PIPE_C:
  3173. cpt_enable_fdi_bc_bifurcation(dev);
  3174. break;
  3175. default:
  3176. BUG();
  3177. }
  3178. }
  3179. /*
  3180. * Enable PCH resources required for PCH ports:
  3181. * - PCH PLLs
  3182. * - FDI training & RX/TX
  3183. * - update transcoder timings
  3184. * - DP transcoding bits
  3185. * - transcoder
  3186. */
  3187. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3188. {
  3189. struct drm_device *dev = crtc->dev;
  3190. struct drm_i915_private *dev_priv = dev->dev_private;
  3191. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3192. int pipe = intel_crtc->pipe;
  3193. u32 reg, temp;
  3194. assert_pch_transcoder_disabled(dev_priv, pipe);
  3195. if (IS_IVYBRIDGE(dev))
  3196. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3197. /* Write the TU size bits before fdi link training, so that error
  3198. * detection works. */
  3199. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3200. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3201. /* For PCH output, training FDI link */
  3202. dev_priv->display.fdi_link_train(crtc);
  3203. /* We need to program the right clock selection before writing the pixel
  3204. * mutliplier into the DPLL. */
  3205. if (HAS_PCH_CPT(dev)) {
  3206. u32 sel;
  3207. temp = I915_READ(PCH_DPLL_SEL);
  3208. temp |= TRANS_DPLL_ENABLE(pipe);
  3209. sel = TRANS_DPLLB_SEL(pipe);
  3210. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3211. temp |= sel;
  3212. else
  3213. temp &= ~sel;
  3214. I915_WRITE(PCH_DPLL_SEL, temp);
  3215. }
  3216. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3217. * transcoder, and we actually should do this to not upset any PCH
  3218. * transcoder that already use the clock when we share it.
  3219. *
  3220. * Note that enable_shared_dpll tries to do the right thing, but
  3221. * get_shared_dpll unconditionally resets the pll - we need that to have
  3222. * the right LVDS enable sequence. */
  3223. intel_enable_shared_dpll(intel_crtc);
  3224. /* set transcoder timing, panel must allow it */
  3225. assert_panel_unlocked(dev_priv, pipe);
  3226. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3227. intel_fdi_normal_train(crtc);
  3228. /* For PCH DP, enable TRANS_DP_CTL */
  3229. if (HAS_PCH_CPT(dev) && intel_crtc->config.has_dp_encoder) {
  3230. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3231. reg = TRANS_DP_CTL(pipe);
  3232. temp = I915_READ(reg);
  3233. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3234. TRANS_DP_SYNC_MASK |
  3235. TRANS_DP_BPC_MASK);
  3236. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3237. TRANS_DP_ENH_FRAMING);
  3238. temp |= bpc << 9; /* same format but at 11:9 */
  3239. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3240. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3241. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3242. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3243. switch (intel_trans_dp_port_sel(crtc)) {
  3244. case PCH_DP_B:
  3245. temp |= TRANS_DP_PORT_SEL_B;
  3246. break;
  3247. case PCH_DP_C:
  3248. temp |= TRANS_DP_PORT_SEL_C;
  3249. break;
  3250. case PCH_DP_D:
  3251. temp |= TRANS_DP_PORT_SEL_D;
  3252. break;
  3253. default:
  3254. BUG();
  3255. }
  3256. I915_WRITE(reg, temp);
  3257. }
  3258. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3259. }
  3260. static void lpt_pch_enable(struct drm_crtc *crtc)
  3261. {
  3262. struct drm_device *dev = crtc->dev;
  3263. struct drm_i915_private *dev_priv = dev->dev_private;
  3264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3265. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3266. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3267. lpt_program_iclkip(crtc);
  3268. /* Set transcoder timing. */
  3269. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3270. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3271. }
  3272. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3273. {
  3274. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3275. if (pll == NULL)
  3276. return;
  3277. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3278. WARN(1, "bad %s crtc mask\n", pll->name);
  3279. return;
  3280. }
  3281. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3282. if (pll->config.crtc_mask == 0) {
  3283. WARN_ON(pll->on);
  3284. WARN_ON(pll->active);
  3285. }
  3286. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3287. }
  3288. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3289. {
  3290. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3291. struct intel_shared_dpll *pll;
  3292. enum intel_dpll_id i;
  3293. if (HAS_PCH_IBX(dev_priv->dev)) {
  3294. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3295. i = (enum intel_dpll_id) crtc->pipe;
  3296. pll = &dev_priv->shared_dplls[i];
  3297. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3298. crtc->base.base.id, pll->name);
  3299. WARN_ON(pll->new_config->crtc_mask);
  3300. goto found;
  3301. }
  3302. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3303. pll = &dev_priv->shared_dplls[i];
  3304. /* Only want to check enabled timings first */
  3305. if (pll->new_config->crtc_mask == 0)
  3306. continue;
  3307. if (memcmp(&crtc->new_config->dpll_hw_state,
  3308. &pll->new_config->hw_state,
  3309. sizeof(pll->new_config->hw_state)) == 0) {
  3310. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3311. crtc->base.base.id, pll->name,
  3312. pll->new_config->crtc_mask,
  3313. pll->active);
  3314. goto found;
  3315. }
  3316. }
  3317. /* Ok no matching timings, maybe there's a free one? */
  3318. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3319. pll = &dev_priv->shared_dplls[i];
  3320. if (pll->new_config->crtc_mask == 0) {
  3321. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3322. crtc->base.base.id, pll->name);
  3323. goto found;
  3324. }
  3325. }
  3326. return NULL;
  3327. found:
  3328. if (pll->new_config->crtc_mask == 0)
  3329. pll->new_config->hw_state = crtc->new_config->dpll_hw_state;
  3330. crtc->new_config->shared_dpll = i;
  3331. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3332. pipe_name(crtc->pipe));
  3333. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3334. return pll;
  3335. }
  3336. /**
  3337. * intel_shared_dpll_start_config - start a new PLL staged config
  3338. * @dev_priv: DRM device
  3339. * @clear_pipes: mask of pipes that will have their PLLs freed
  3340. *
  3341. * Starts a new PLL staged config, copying the current config but
  3342. * releasing the references of pipes specified in clear_pipes.
  3343. */
  3344. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3345. unsigned clear_pipes)
  3346. {
  3347. struct intel_shared_dpll *pll;
  3348. enum intel_dpll_id i;
  3349. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3350. pll = &dev_priv->shared_dplls[i];
  3351. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3352. GFP_KERNEL);
  3353. if (!pll->new_config)
  3354. goto cleanup;
  3355. pll->new_config->crtc_mask &= ~clear_pipes;
  3356. }
  3357. return 0;
  3358. cleanup:
  3359. while (--i >= 0) {
  3360. pll = &dev_priv->shared_dplls[i];
  3361. kfree(pll->new_config);
  3362. pll->new_config = NULL;
  3363. }
  3364. return -ENOMEM;
  3365. }
  3366. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3367. {
  3368. struct intel_shared_dpll *pll;
  3369. enum intel_dpll_id i;
  3370. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3371. pll = &dev_priv->shared_dplls[i];
  3372. WARN_ON(pll->new_config == &pll->config);
  3373. pll->config = *pll->new_config;
  3374. kfree(pll->new_config);
  3375. pll->new_config = NULL;
  3376. }
  3377. }
  3378. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3379. {
  3380. struct intel_shared_dpll *pll;
  3381. enum intel_dpll_id i;
  3382. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3383. pll = &dev_priv->shared_dplls[i];
  3384. WARN_ON(pll->new_config == &pll->config);
  3385. kfree(pll->new_config);
  3386. pll->new_config = NULL;
  3387. }
  3388. }
  3389. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3390. {
  3391. struct drm_i915_private *dev_priv = dev->dev_private;
  3392. int dslreg = PIPEDSL(pipe);
  3393. u32 temp;
  3394. temp = I915_READ(dslreg);
  3395. udelay(500);
  3396. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3397. if (wait_for(I915_READ(dslreg) != temp, 5))
  3398. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3399. }
  3400. }
  3401. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3402. {
  3403. struct drm_device *dev = crtc->base.dev;
  3404. struct drm_i915_private *dev_priv = dev->dev_private;
  3405. int pipe = crtc->pipe;
  3406. if (crtc->config.pch_pfit.enabled) {
  3407. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3408. I915_WRITE(PS_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3409. I915_WRITE(PS_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3410. }
  3411. }
  3412. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3413. {
  3414. struct drm_device *dev = crtc->base.dev;
  3415. struct drm_i915_private *dev_priv = dev->dev_private;
  3416. int pipe = crtc->pipe;
  3417. if (crtc->config.pch_pfit.enabled) {
  3418. /* Force use of hard-coded filter coefficients
  3419. * as some pre-programmed values are broken,
  3420. * e.g. x201.
  3421. */
  3422. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3423. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3424. PF_PIPE_SEL_IVB(pipe));
  3425. else
  3426. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3427. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3428. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3429. }
  3430. }
  3431. static void intel_enable_planes(struct drm_crtc *crtc)
  3432. {
  3433. struct drm_device *dev = crtc->dev;
  3434. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3435. struct drm_plane *plane;
  3436. struct intel_plane *intel_plane;
  3437. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3438. intel_plane = to_intel_plane(plane);
  3439. if (intel_plane->pipe == pipe)
  3440. intel_plane_restore(&intel_plane->base);
  3441. }
  3442. }
  3443. static void intel_disable_planes(struct drm_crtc *crtc)
  3444. {
  3445. struct drm_device *dev = crtc->dev;
  3446. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3447. struct drm_plane *plane;
  3448. struct intel_plane *intel_plane;
  3449. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3450. intel_plane = to_intel_plane(plane);
  3451. if (intel_plane->pipe == pipe)
  3452. plane->funcs->disable_plane(plane);
  3453. }
  3454. }
  3455. void hsw_enable_ips(struct intel_crtc *crtc)
  3456. {
  3457. struct drm_device *dev = crtc->base.dev;
  3458. struct drm_i915_private *dev_priv = dev->dev_private;
  3459. if (!crtc->config.ips_enabled)
  3460. return;
  3461. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3462. intel_wait_for_vblank(dev, crtc->pipe);
  3463. assert_plane_enabled(dev_priv, crtc->plane);
  3464. if (IS_BROADWELL(dev)) {
  3465. mutex_lock(&dev_priv->rps.hw_lock);
  3466. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3467. mutex_unlock(&dev_priv->rps.hw_lock);
  3468. /* Quoting Art Runyan: "its not safe to expect any particular
  3469. * value in IPS_CTL bit 31 after enabling IPS through the
  3470. * mailbox." Moreover, the mailbox may return a bogus state,
  3471. * so we need to just enable it and continue on.
  3472. */
  3473. } else {
  3474. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3475. /* The bit only becomes 1 in the next vblank, so this wait here
  3476. * is essentially intel_wait_for_vblank. If we don't have this
  3477. * and don't wait for vblanks until the end of crtc_enable, then
  3478. * the HW state readout code will complain that the expected
  3479. * IPS_CTL value is not the one we read. */
  3480. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3481. DRM_ERROR("Timed out waiting for IPS enable\n");
  3482. }
  3483. }
  3484. void hsw_disable_ips(struct intel_crtc *crtc)
  3485. {
  3486. struct drm_device *dev = crtc->base.dev;
  3487. struct drm_i915_private *dev_priv = dev->dev_private;
  3488. if (!crtc->config.ips_enabled)
  3489. return;
  3490. assert_plane_enabled(dev_priv, crtc->plane);
  3491. if (IS_BROADWELL(dev)) {
  3492. mutex_lock(&dev_priv->rps.hw_lock);
  3493. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3494. mutex_unlock(&dev_priv->rps.hw_lock);
  3495. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3496. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3497. DRM_ERROR("Timed out waiting for IPS disable\n");
  3498. } else {
  3499. I915_WRITE(IPS_CTL, 0);
  3500. POSTING_READ(IPS_CTL);
  3501. }
  3502. /* We need to wait for a vblank before we can disable the plane. */
  3503. intel_wait_for_vblank(dev, crtc->pipe);
  3504. }
  3505. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3506. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3507. {
  3508. struct drm_device *dev = crtc->dev;
  3509. struct drm_i915_private *dev_priv = dev->dev_private;
  3510. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3511. enum pipe pipe = intel_crtc->pipe;
  3512. int palreg = PALETTE(pipe);
  3513. int i;
  3514. bool reenable_ips = false;
  3515. /* The clocks have to be on to load the palette. */
  3516. if (!crtc->enabled || !intel_crtc->active)
  3517. return;
  3518. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3519. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3520. assert_dsi_pll_enabled(dev_priv);
  3521. else
  3522. assert_pll_enabled(dev_priv, pipe);
  3523. }
  3524. /* use legacy palette for Ironlake */
  3525. if (!HAS_GMCH_DISPLAY(dev))
  3526. palreg = LGC_PALETTE(pipe);
  3527. /* Workaround : Do not read or write the pipe palette/gamma data while
  3528. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3529. */
  3530. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3531. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3532. GAMMA_MODE_MODE_SPLIT)) {
  3533. hsw_disable_ips(intel_crtc);
  3534. reenable_ips = true;
  3535. }
  3536. for (i = 0; i < 256; i++) {
  3537. I915_WRITE(palreg + 4 * i,
  3538. (intel_crtc->lut_r[i] << 16) |
  3539. (intel_crtc->lut_g[i] << 8) |
  3540. intel_crtc->lut_b[i]);
  3541. }
  3542. if (reenable_ips)
  3543. hsw_enable_ips(intel_crtc);
  3544. }
  3545. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3546. {
  3547. if (!enable && intel_crtc->overlay) {
  3548. struct drm_device *dev = intel_crtc->base.dev;
  3549. struct drm_i915_private *dev_priv = dev->dev_private;
  3550. mutex_lock(&dev->struct_mutex);
  3551. dev_priv->mm.interruptible = false;
  3552. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3553. dev_priv->mm.interruptible = true;
  3554. mutex_unlock(&dev->struct_mutex);
  3555. }
  3556. /* Let userspace switch the overlay on again. In most cases userspace
  3557. * has to recompute where to put it anyway.
  3558. */
  3559. }
  3560. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3561. {
  3562. struct drm_device *dev = crtc->dev;
  3563. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3564. int pipe = intel_crtc->pipe;
  3565. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3566. intel_enable_planes(crtc);
  3567. intel_crtc_update_cursor(crtc, true);
  3568. intel_crtc_dpms_overlay(intel_crtc, true);
  3569. hsw_enable_ips(intel_crtc);
  3570. mutex_lock(&dev->struct_mutex);
  3571. intel_fbc_update(dev);
  3572. mutex_unlock(&dev->struct_mutex);
  3573. /*
  3574. * FIXME: Once we grow proper nuclear flip support out of this we need
  3575. * to compute the mask of flip planes precisely. For the time being
  3576. * consider this a flip from a NULL plane.
  3577. */
  3578. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3579. }
  3580. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3581. {
  3582. struct drm_device *dev = crtc->dev;
  3583. struct drm_i915_private *dev_priv = dev->dev_private;
  3584. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3585. int pipe = intel_crtc->pipe;
  3586. int plane = intel_crtc->plane;
  3587. intel_crtc_wait_for_pending_flips(crtc);
  3588. if (dev_priv->fbc.plane == plane)
  3589. intel_fbc_disable(dev);
  3590. hsw_disable_ips(intel_crtc);
  3591. intel_crtc_dpms_overlay(intel_crtc, false);
  3592. intel_crtc_update_cursor(crtc, false);
  3593. intel_disable_planes(crtc);
  3594. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3595. /*
  3596. * FIXME: Once we grow proper nuclear flip support out of this we need
  3597. * to compute the mask of flip planes precisely. For the time being
  3598. * consider this a flip to a NULL plane.
  3599. */
  3600. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3601. }
  3602. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3603. {
  3604. struct drm_device *dev = crtc->dev;
  3605. struct drm_i915_private *dev_priv = dev->dev_private;
  3606. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3607. struct intel_encoder *encoder;
  3608. int pipe = intel_crtc->pipe;
  3609. WARN_ON(!crtc->enabled);
  3610. if (intel_crtc->active)
  3611. return;
  3612. if (intel_crtc->config.has_pch_encoder)
  3613. intel_prepare_shared_dpll(intel_crtc);
  3614. if (intel_crtc->config.has_dp_encoder)
  3615. intel_dp_set_m_n(intel_crtc);
  3616. intel_set_pipe_timings(intel_crtc);
  3617. if (intel_crtc->config.has_pch_encoder) {
  3618. intel_cpu_transcoder_set_m_n(intel_crtc,
  3619. &intel_crtc->config.fdi_m_n, NULL);
  3620. }
  3621. ironlake_set_pipeconf(crtc);
  3622. intel_crtc->active = true;
  3623. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3624. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3625. for_each_encoder_on_crtc(dev, crtc, encoder)
  3626. if (encoder->pre_enable)
  3627. encoder->pre_enable(encoder);
  3628. if (intel_crtc->config.has_pch_encoder) {
  3629. /* Note: FDI PLL enabling _must_ be done before we enable the
  3630. * cpu pipes, hence this is separate from all the other fdi/pch
  3631. * enabling. */
  3632. ironlake_fdi_pll_enable(intel_crtc);
  3633. } else {
  3634. assert_fdi_tx_disabled(dev_priv, pipe);
  3635. assert_fdi_rx_disabled(dev_priv, pipe);
  3636. }
  3637. ironlake_pfit_enable(intel_crtc);
  3638. /*
  3639. * On ILK+ LUT must be loaded before the pipe is running but with
  3640. * clocks enabled
  3641. */
  3642. intel_crtc_load_lut(crtc);
  3643. intel_update_watermarks(crtc);
  3644. intel_enable_pipe(intel_crtc);
  3645. if (intel_crtc->config.has_pch_encoder)
  3646. ironlake_pch_enable(crtc);
  3647. for_each_encoder_on_crtc(dev, crtc, encoder)
  3648. encoder->enable(encoder);
  3649. if (HAS_PCH_CPT(dev))
  3650. cpt_verify_modeset(dev, intel_crtc->pipe);
  3651. assert_vblank_disabled(crtc);
  3652. drm_crtc_vblank_on(crtc);
  3653. intel_crtc_enable_planes(crtc);
  3654. }
  3655. /* IPS only exists on ULT machines and is tied to pipe A. */
  3656. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3657. {
  3658. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3659. }
  3660. /*
  3661. * This implements the workaround described in the "notes" section of the mode
  3662. * set sequence documentation. When going from no pipes or single pipe to
  3663. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3664. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3665. */
  3666. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3667. {
  3668. struct drm_device *dev = crtc->base.dev;
  3669. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3670. /* We want to get the other_active_crtc only if there's only 1 other
  3671. * active crtc. */
  3672. for_each_intel_crtc(dev, crtc_it) {
  3673. if (!crtc_it->active || crtc_it == crtc)
  3674. continue;
  3675. if (other_active_crtc)
  3676. return;
  3677. other_active_crtc = crtc_it;
  3678. }
  3679. if (!other_active_crtc)
  3680. return;
  3681. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3682. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3683. }
  3684. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3685. {
  3686. struct drm_device *dev = crtc->dev;
  3687. struct drm_i915_private *dev_priv = dev->dev_private;
  3688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3689. struct intel_encoder *encoder;
  3690. int pipe = intel_crtc->pipe;
  3691. WARN_ON(!crtc->enabled);
  3692. if (intel_crtc->active)
  3693. return;
  3694. if (intel_crtc_to_shared_dpll(intel_crtc))
  3695. intel_enable_shared_dpll(intel_crtc);
  3696. if (intel_crtc->config.has_dp_encoder)
  3697. intel_dp_set_m_n(intel_crtc);
  3698. intel_set_pipe_timings(intel_crtc);
  3699. if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
  3700. I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
  3701. intel_crtc->config.pixel_multiplier - 1);
  3702. }
  3703. if (intel_crtc->config.has_pch_encoder) {
  3704. intel_cpu_transcoder_set_m_n(intel_crtc,
  3705. &intel_crtc->config.fdi_m_n, NULL);
  3706. }
  3707. haswell_set_pipeconf(crtc);
  3708. intel_set_pipe_csc(crtc);
  3709. intel_crtc->active = true;
  3710. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3711. for_each_encoder_on_crtc(dev, crtc, encoder)
  3712. if (encoder->pre_enable)
  3713. encoder->pre_enable(encoder);
  3714. if (intel_crtc->config.has_pch_encoder) {
  3715. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3716. true);
  3717. dev_priv->display.fdi_link_train(crtc);
  3718. }
  3719. intel_ddi_enable_pipe_clock(intel_crtc);
  3720. if (IS_SKYLAKE(dev))
  3721. skylake_pfit_enable(intel_crtc);
  3722. else
  3723. ironlake_pfit_enable(intel_crtc);
  3724. /*
  3725. * On ILK+ LUT must be loaded before the pipe is running but with
  3726. * clocks enabled
  3727. */
  3728. intel_crtc_load_lut(crtc);
  3729. intel_ddi_set_pipe_settings(crtc);
  3730. intel_ddi_enable_transcoder_func(crtc);
  3731. intel_update_watermarks(crtc);
  3732. intel_enable_pipe(intel_crtc);
  3733. if (intel_crtc->config.has_pch_encoder)
  3734. lpt_pch_enable(crtc);
  3735. if (intel_crtc->config.dp_encoder_is_mst)
  3736. intel_ddi_set_vc_payload_alloc(crtc, true);
  3737. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3738. encoder->enable(encoder);
  3739. intel_opregion_notify_encoder(encoder, true);
  3740. }
  3741. assert_vblank_disabled(crtc);
  3742. drm_crtc_vblank_on(crtc);
  3743. /* If we change the relative order between pipe/planes enabling, we need
  3744. * to change the workaround. */
  3745. haswell_mode_set_planes_workaround(intel_crtc);
  3746. intel_crtc_enable_planes(crtc);
  3747. }
  3748. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3749. {
  3750. struct drm_device *dev = crtc->base.dev;
  3751. struct drm_i915_private *dev_priv = dev->dev_private;
  3752. int pipe = crtc->pipe;
  3753. /* To avoid upsetting the power well on haswell only disable the pfit if
  3754. * it's in use. The hw state code will make sure we get this right. */
  3755. if (crtc->config.pch_pfit.enabled) {
  3756. I915_WRITE(PS_CTL(pipe), 0);
  3757. I915_WRITE(PS_WIN_POS(pipe), 0);
  3758. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3759. }
  3760. }
  3761. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3762. {
  3763. struct drm_device *dev = crtc->base.dev;
  3764. struct drm_i915_private *dev_priv = dev->dev_private;
  3765. int pipe = crtc->pipe;
  3766. /* To avoid upsetting the power well on haswell only disable the pfit if
  3767. * it's in use. The hw state code will make sure we get this right. */
  3768. if (crtc->config.pch_pfit.enabled) {
  3769. I915_WRITE(PF_CTL(pipe), 0);
  3770. I915_WRITE(PF_WIN_POS(pipe), 0);
  3771. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3772. }
  3773. }
  3774. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3775. {
  3776. struct drm_device *dev = crtc->dev;
  3777. struct drm_i915_private *dev_priv = dev->dev_private;
  3778. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3779. struct intel_encoder *encoder;
  3780. int pipe = intel_crtc->pipe;
  3781. u32 reg, temp;
  3782. if (!intel_crtc->active)
  3783. return;
  3784. intel_crtc_disable_planes(crtc);
  3785. drm_crtc_vblank_off(crtc);
  3786. assert_vblank_disabled(crtc);
  3787. for_each_encoder_on_crtc(dev, crtc, encoder)
  3788. encoder->disable(encoder);
  3789. if (intel_crtc->config.has_pch_encoder)
  3790. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3791. intel_disable_pipe(intel_crtc);
  3792. ironlake_pfit_disable(intel_crtc);
  3793. for_each_encoder_on_crtc(dev, crtc, encoder)
  3794. if (encoder->post_disable)
  3795. encoder->post_disable(encoder);
  3796. if (intel_crtc->config.has_pch_encoder) {
  3797. ironlake_fdi_disable(crtc);
  3798. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3799. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3800. if (HAS_PCH_CPT(dev)) {
  3801. /* disable TRANS_DP_CTL */
  3802. reg = TRANS_DP_CTL(pipe);
  3803. temp = I915_READ(reg);
  3804. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3805. TRANS_DP_PORT_SEL_MASK);
  3806. temp |= TRANS_DP_PORT_SEL_NONE;
  3807. I915_WRITE(reg, temp);
  3808. /* disable DPLL_SEL */
  3809. temp = I915_READ(PCH_DPLL_SEL);
  3810. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3811. I915_WRITE(PCH_DPLL_SEL, temp);
  3812. }
  3813. /* disable PCH DPLL */
  3814. intel_disable_shared_dpll(intel_crtc);
  3815. ironlake_fdi_pll_disable(intel_crtc);
  3816. }
  3817. intel_crtc->active = false;
  3818. intel_update_watermarks(crtc);
  3819. mutex_lock(&dev->struct_mutex);
  3820. intel_fbc_update(dev);
  3821. mutex_unlock(&dev->struct_mutex);
  3822. }
  3823. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3824. {
  3825. struct drm_device *dev = crtc->dev;
  3826. struct drm_i915_private *dev_priv = dev->dev_private;
  3827. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3828. struct intel_encoder *encoder;
  3829. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3830. if (!intel_crtc->active)
  3831. return;
  3832. intel_crtc_disable_planes(crtc);
  3833. drm_crtc_vblank_off(crtc);
  3834. assert_vblank_disabled(crtc);
  3835. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3836. intel_opregion_notify_encoder(encoder, false);
  3837. encoder->disable(encoder);
  3838. }
  3839. if (intel_crtc->config.has_pch_encoder)
  3840. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3841. false);
  3842. intel_disable_pipe(intel_crtc);
  3843. if (intel_crtc->config.dp_encoder_is_mst)
  3844. intel_ddi_set_vc_payload_alloc(crtc, false);
  3845. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3846. if (IS_SKYLAKE(dev))
  3847. skylake_pfit_disable(intel_crtc);
  3848. else
  3849. ironlake_pfit_disable(intel_crtc);
  3850. intel_ddi_disable_pipe_clock(intel_crtc);
  3851. if (intel_crtc->config.has_pch_encoder) {
  3852. lpt_disable_pch_transcoder(dev_priv);
  3853. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3854. true);
  3855. intel_ddi_fdi_disable(crtc);
  3856. }
  3857. for_each_encoder_on_crtc(dev, crtc, encoder)
  3858. if (encoder->post_disable)
  3859. encoder->post_disable(encoder);
  3860. intel_crtc->active = false;
  3861. intel_update_watermarks(crtc);
  3862. mutex_lock(&dev->struct_mutex);
  3863. intel_fbc_update(dev);
  3864. mutex_unlock(&dev->struct_mutex);
  3865. if (intel_crtc_to_shared_dpll(intel_crtc))
  3866. intel_disable_shared_dpll(intel_crtc);
  3867. }
  3868. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3869. {
  3870. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3871. intel_put_shared_dpll(intel_crtc);
  3872. }
  3873. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3874. {
  3875. struct drm_device *dev = crtc->base.dev;
  3876. struct drm_i915_private *dev_priv = dev->dev_private;
  3877. struct intel_crtc_config *pipe_config = &crtc->config;
  3878. if (!crtc->config.gmch_pfit.control)
  3879. return;
  3880. /*
  3881. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3882. * according to register description and PRM.
  3883. */
  3884. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3885. assert_pipe_disabled(dev_priv, crtc->pipe);
  3886. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3887. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3888. /* Border color in case we don't scale up to the full screen. Black by
  3889. * default, change to something else for debugging. */
  3890. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3891. }
  3892. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3893. {
  3894. switch (port) {
  3895. case PORT_A:
  3896. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3897. case PORT_B:
  3898. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3899. case PORT_C:
  3900. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3901. case PORT_D:
  3902. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3903. default:
  3904. WARN_ON_ONCE(1);
  3905. return POWER_DOMAIN_PORT_OTHER;
  3906. }
  3907. }
  3908. #define for_each_power_domain(domain, mask) \
  3909. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3910. if ((1 << (domain)) & (mask))
  3911. enum intel_display_power_domain
  3912. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3913. {
  3914. struct drm_device *dev = intel_encoder->base.dev;
  3915. struct intel_digital_port *intel_dig_port;
  3916. switch (intel_encoder->type) {
  3917. case INTEL_OUTPUT_UNKNOWN:
  3918. /* Only DDI platforms should ever use this output type */
  3919. WARN_ON_ONCE(!HAS_DDI(dev));
  3920. case INTEL_OUTPUT_DISPLAYPORT:
  3921. case INTEL_OUTPUT_HDMI:
  3922. case INTEL_OUTPUT_EDP:
  3923. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3924. return port_to_power_domain(intel_dig_port->port);
  3925. case INTEL_OUTPUT_DP_MST:
  3926. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3927. return port_to_power_domain(intel_dig_port->port);
  3928. case INTEL_OUTPUT_ANALOG:
  3929. return POWER_DOMAIN_PORT_CRT;
  3930. case INTEL_OUTPUT_DSI:
  3931. return POWER_DOMAIN_PORT_DSI;
  3932. default:
  3933. return POWER_DOMAIN_PORT_OTHER;
  3934. }
  3935. }
  3936. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3937. {
  3938. struct drm_device *dev = crtc->dev;
  3939. struct intel_encoder *intel_encoder;
  3940. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3941. enum pipe pipe = intel_crtc->pipe;
  3942. unsigned long mask;
  3943. enum transcoder transcoder;
  3944. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3945. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3946. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3947. if (intel_crtc->config.pch_pfit.enabled ||
  3948. intel_crtc->config.pch_pfit.force_thru)
  3949. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3950. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3951. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3952. return mask;
  3953. }
  3954. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3955. {
  3956. struct drm_i915_private *dev_priv = dev->dev_private;
  3957. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3958. struct intel_crtc *crtc;
  3959. /*
  3960. * First get all needed power domains, then put all unneeded, to avoid
  3961. * any unnecessary toggling of the power wells.
  3962. */
  3963. for_each_intel_crtc(dev, crtc) {
  3964. enum intel_display_power_domain domain;
  3965. if (!crtc->base.enabled)
  3966. continue;
  3967. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3968. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3969. intel_display_power_get(dev_priv, domain);
  3970. }
  3971. if (dev_priv->display.modeset_global_resources)
  3972. dev_priv->display.modeset_global_resources(dev);
  3973. for_each_intel_crtc(dev, crtc) {
  3974. enum intel_display_power_domain domain;
  3975. for_each_power_domain(domain, crtc->enabled_power_domains)
  3976. intel_display_power_put(dev_priv, domain);
  3977. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3978. }
  3979. intel_display_set_init_power(dev_priv, false);
  3980. }
  3981. /* returns HPLL frequency in kHz */
  3982. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3983. {
  3984. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3985. /* Obtain SKU information */
  3986. mutex_lock(&dev_priv->dpio_lock);
  3987. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3988. CCK_FUSE_HPLL_FREQ_MASK;
  3989. mutex_unlock(&dev_priv->dpio_lock);
  3990. return vco_freq[hpll_freq] * 1000;
  3991. }
  3992. static void vlv_update_cdclk(struct drm_device *dev)
  3993. {
  3994. struct drm_i915_private *dev_priv = dev->dev_private;
  3995. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3996. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  3997. dev_priv->vlv_cdclk_freq);
  3998. /*
  3999. * Program the gmbus_freq based on the cdclk frequency.
  4000. * BSpec erroneously claims we should aim for 4MHz, but
  4001. * in fact 1MHz is the correct frequency.
  4002. */
  4003. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4004. }
  4005. /* Adjust CDclk dividers to allow high res or save power if possible */
  4006. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4007. {
  4008. struct drm_i915_private *dev_priv = dev->dev_private;
  4009. u32 val, cmd;
  4010. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4011. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4012. cmd = 2;
  4013. else if (cdclk == 266667)
  4014. cmd = 1;
  4015. else
  4016. cmd = 0;
  4017. mutex_lock(&dev_priv->rps.hw_lock);
  4018. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4019. val &= ~DSPFREQGUAR_MASK;
  4020. val |= (cmd << DSPFREQGUAR_SHIFT);
  4021. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4022. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4023. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4024. 50)) {
  4025. DRM_ERROR("timed out waiting for CDclk change\n");
  4026. }
  4027. mutex_unlock(&dev_priv->rps.hw_lock);
  4028. if (cdclk == 400000) {
  4029. u32 divider;
  4030. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4031. mutex_lock(&dev_priv->dpio_lock);
  4032. /* adjust cdclk divider */
  4033. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4034. val &= ~DISPLAY_FREQUENCY_VALUES;
  4035. val |= divider;
  4036. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4037. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4038. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4039. 50))
  4040. DRM_ERROR("timed out waiting for CDclk change\n");
  4041. mutex_unlock(&dev_priv->dpio_lock);
  4042. }
  4043. mutex_lock(&dev_priv->dpio_lock);
  4044. /* adjust self-refresh exit latency value */
  4045. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4046. val &= ~0x7f;
  4047. /*
  4048. * For high bandwidth configs, we set a higher latency in the bunit
  4049. * so that the core display fetch happens in time to avoid underruns.
  4050. */
  4051. if (cdclk == 400000)
  4052. val |= 4500 / 250; /* 4.5 usec */
  4053. else
  4054. val |= 3000 / 250; /* 3.0 usec */
  4055. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4056. mutex_unlock(&dev_priv->dpio_lock);
  4057. vlv_update_cdclk(dev);
  4058. }
  4059. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4060. {
  4061. struct drm_i915_private *dev_priv = dev->dev_private;
  4062. u32 val, cmd;
  4063. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4064. switch (cdclk) {
  4065. case 400000:
  4066. cmd = 3;
  4067. break;
  4068. case 333333:
  4069. case 320000:
  4070. cmd = 2;
  4071. break;
  4072. case 266667:
  4073. cmd = 1;
  4074. break;
  4075. case 200000:
  4076. cmd = 0;
  4077. break;
  4078. default:
  4079. MISSING_CASE(cdclk);
  4080. return;
  4081. }
  4082. mutex_lock(&dev_priv->rps.hw_lock);
  4083. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4084. val &= ~DSPFREQGUAR_MASK_CHV;
  4085. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4086. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4087. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4088. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4089. 50)) {
  4090. DRM_ERROR("timed out waiting for CDclk change\n");
  4091. }
  4092. mutex_unlock(&dev_priv->rps.hw_lock);
  4093. vlv_update_cdclk(dev);
  4094. }
  4095. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4096. int max_pixclk)
  4097. {
  4098. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4099. /* FIXME: Punit isn't quite ready yet */
  4100. if (IS_CHERRYVIEW(dev_priv->dev))
  4101. return 400000;
  4102. /*
  4103. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4104. * 200MHz
  4105. * 267MHz
  4106. * 320/333MHz (depends on HPLL freq)
  4107. * 400MHz
  4108. * So we check to see whether we're above 90% of the lower bin and
  4109. * adjust if needed.
  4110. *
  4111. * We seem to get an unstable or solid color picture at 200MHz.
  4112. * Not sure what's wrong. For now use 200MHz only when all pipes
  4113. * are off.
  4114. */
  4115. if (max_pixclk > freq_320*9/10)
  4116. return 400000;
  4117. else if (max_pixclk > 266667*9/10)
  4118. return freq_320;
  4119. else if (max_pixclk > 0)
  4120. return 266667;
  4121. else
  4122. return 200000;
  4123. }
  4124. /* compute the max pixel clock for new configuration */
  4125. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4126. {
  4127. struct drm_device *dev = dev_priv->dev;
  4128. struct intel_crtc *intel_crtc;
  4129. int max_pixclk = 0;
  4130. for_each_intel_crtc(dev, intel_crtc) {
  4131. if (intel_crtc->new_enabled)
  4132. max_pixclk = max(max_pixclk,
  4133. intel_crtc->new_config->adjusted_mode.crtc_clock);
  4134. }
  4135. return max_pixclk;
  4136. }
  4137. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4138. unsigned *prepare_pipes)
  4139. {
  4140. struct drm_i915_private *dev_priv = dev->dev_private;
  4141. struct intel_crtc *intel_crtc;
  4142. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4143. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4144. dev_priv->vlv_cdclk_freq)
  4145. return;
  4146. /* disable/enable all currently active pipes while we change cdclk */
  4147. for_each_intel_crtc(dev, intel_crtc)
  4148. if (intel_crtc->base.enabled)
  4149. *prepare_pipes |= (1 << intel_crtc->pipe);
  4150. }
  4151. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4152. {
  4153. struct drm_i915_private *dev_priv = dev->dev_private;
  4154. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4155. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4156. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4157. /*
  4158. * FIXME: We can end up here with all power domains off, yet
  4159. * with a CDCLK frequency other than the minimum. To account
  4160. * for this take the PIPE-A power domain, which covers the HW
  4161. * blocks needed for the following programming. This can be
  4162. * removed once it's guaranteed that we get here either with
  4163. * the minimum CDCLK set, or the required power domains
  4164. * enabled.
  4165. */
  4166. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4167. if (IS_CHERRYVIEW(dev))
  4168. cherryview_set_cdclk(dev, req_cdclk);
  4169. else
  4170. valleyview_set_cdclk(dev, req_cdclk);
  4171. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4172. }
  4173. }
  4174. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4175. {
  4176. struct drm_device *dev = crtc->dev;
  4177. struct drm_i915_private *dev_priv = to_i915(dev);
  4178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4179. struct intel_encoder *encoder;
  4180. int pipe = intel_crtc->pipe;
  4181. bool is_dsi;
  4182. WARN_ON(!crtc->enabled);
  4183. if (intel_crtc->active)
  4184. return;
  4185. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4186. if (!is_dsi) {
  4187. if (IS_CHERRYVIEW(dev))
  4188. chv_prepare_pll(intel_crtc, &intel_crtc->config);
  4189. else
  4190. vlv_prepare_pll(intel_crtc, &intel_crtc->config);
  4191. }
  4192. if (intel_crtc->config.has_dp_encoder)
  4193. intel_dp_set_m_n(intel_crtc);
  4194. intel_set_pipe_timings(intel_crtc);
  4195. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4196. struct drm_i915_private *dev_priv = dev->dev_private;
  4197. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4198. I915_WRITE(CHV_CANVAS(pipe), 0);
  4199. }
  4200. i9xx_set_pipeconf(intel_crtc);
  4201. intel_crtc->active = true;
  4202. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4203. for_each_encoder_on_crtc(dev, crtc, encoder)
  4204. if (encoder->pre_pll_enable)
  4205. encoder->pre_pll_enable(encoder);
  4206. if (!is_dsi) {
  4207. if (IS_CHERRYVIEW(dev))
  4208. chv_enable_pll(intel_crtc, &intel_crtc->config);
  4209. else
  4210. vlv_enable_pll(intel_crtc, &intel_crtc->config);
  4211. }
  4212. for_each_encoder_on_crtc(dev, crtc, encoder)
  4213. if (encoder->pre_enable)
  4214. encoder->pre_enable(encoder);
  4215. i9xx_pfit_enable(intel_crtc);
  4216. intel_crtc_load_lut(crtc);
  4217. intel_update_watermarks(crtc);
  4218. intel_enable_pipe(intel_crtc);
  4219. for_each_encoder_on_crtc(dev, crtc, encoder)
  4220. encoder->enable(encoder);
  4221. assert_vblank_disabled(crtc);
  4222. drm_crtc_vblank_on(crtc);
  4223. intel_crtc_enable_planes(crtc);
  4224. /* Underruns don't raise interrupts, so check manually. */
  4225. i9xx_check_fifo_underruns(dev_priv);
  4226. }
  4227. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4228. {
  4229. struct drm_device *dev = crtc->base.dev;
  4230. struct drm_i915_private *dev_priv = dev->dev_private;
  4231. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  4232. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  4233. }
  4234. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4235. {
  4236. struct drm_device *dev = crtc->dev;
  4237. struct drm_i915_private *dev_priv = to_i915(dev);
  4238. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4239. struct intel_encoder *encoder;
  4240. int pipe = intel_crtc->pipe;
  4241. WARN_ON(!crtc->enabled);
  4242. if (intel_crtc->active)
  4243. return;
  4244. i9xx_set_pll_dividers(intel_crtc);
  4245. if (intel_crtc->config.has_dp_encoder)
  4246. intel_dp_set_m_n(intel_crtc);
  4247. intel_set_pipe_timings(intel_crtc);
  4248. i9xx_set_pipeconf(intel_crtc);
  4249. intel_crtc->active = true;
  4250. if (!IS_GEN2(dev))
  4251. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4252. for_each_encoder_on_crtc(dev, crtc, encoder)
  4253. if (encoder->pre_enable)
  4254. encoder->pre_enable(encoder);
  4255. i9xx_enable_pll(intel_crtc);
  4256. i9xx_pfit_enable(intel_crtc);
  4257. intel_crtc_load_lut(crtc);
  4258. intel_update_watermarks(crtc);
  4259. intel_enable_pipe(intel_crtc);
  4260. for_each_encoder_on_crtc(dev, crtc, encoder)
  4261. encoder->enable(encoder);
  4262. assert_vblank_disabled(crtc);
  4263. drm_crtc_vblank_on(crtc);
  4264. intel_crtc_enable_planes(crtc);
  4265. /*
  4266. * Gen2 reports pipe underruns whenever all planes are disabled.
  4267. * So don't enable underrun reporting before at least some planes
  4268. * are enabled.
  4269. * FIXME: Need to fix the logic to work when we turn off all planes
  4270. * but leave the pipe running.
  4271. */
  4272. if (IS_GEN2(dev))
  4273. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4274. /* Underruns don't raise interrupts, so check manually. */
  4275. i9xx_check_fifo_underruns(dev_priv);
  4276. }
  4277. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4278. {
  4279. struct drm_device *dev = crtc->base.dev;
  4280. struct drm_i915_private *dev_priv = dev->dev_private;
  4281. if (!crtc->config.gmch_pfit.control)
  4282. return;
  4283. assert_pipe_disabled(dev_priv, crtc->pipe);
  4284. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4285. I915_READ(PFIT_CONTROL));
  4286. I915_WRITE(PFIT_CONTROL, 0);
  4287. }
  4288. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4289. {
  4290. struct drm_device *dev = crtc->dev;
  4291. struct drm_i915_private *dev_priv = dev->dev_private;
  4292. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4293. struct intel_encoder *encoder;
  4294. int pipe = intel_crtc->pipe;
  4295. if (!intel_crtc->active)
  4296. return;
  4297. /*
  4298. * Gen2 reports pipe underruns whenever all planes are disabled.
  4299. * So diasble underrun reporting before all the planes get disabled.
  4300. * FIXME: Need to fix the logic to work when we turn off all planes
  4301. * but leave the pipe running.
  4302. */
  4303. if (IS_GEN2(dev))
  4304. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4305. /*
  4306. * Vblank time updates from the shadow to live plane control register
  4307. * are blocked if the memory self-refresh mode is active at that
  4308. * moment. So to make sure the plane gets truly disabled, disable
  4309. * first the self-refresh mode. The self-refresh enable bit in turn
  4310. * will be checked/applied by the HW only at the next frame start
  4311. * event which is after the vblank start event, so we need to have a
  4312. * wait-for-vblank between disabling the plane and the pipe.
  4313. */
  4314. intel_set_memory_cxsr(dev_priv, false);
  4315. intel_crtc_disable_planes(crtc);
  4316. /*
  4317. * On gen2 planes are double buffered but the pipe isn't, so we must
  4318. * wait for planes to fully turn off before disabling the pipe.
  4319. * We also need to wait on all gmch platforms because of the
  4320. * self-refresh mode constraint explained above.
  4321. */
  4322. intel_wait_for_vblank(dev, pipe);
  4323. drm_crtc_vblank_off(crtc);
  4324. assert_vblank_disabled(crtc);
  4325. for_each_encoder_on_crtc(dev, crtc, encoder)
  4326. encoder->disable(encoder);
  4327. intel_disable_pipe(intel_crtc);
  4328. i9xx_pfit_disable(intel_crtc);
  4329. for_each_encoder_on_crtc(dev, crtc, encoder)
  4330. if (encoder->post_disable)
  4331. encoder->post_disable(encoder);
  4332. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4333. if (IS_CHERRYVIEW(dev))
  4334. chv_disable_pll(dev_priv, pipe);
  4335. else if (IS_VALLEYVIEW(dev))
  4336. vlv_disable_pll(dev_priv, pipe);
  4337. else
  4338. i9xx_disable_pll(intel_crtc);
  4339. }
  4340. if (!IS_GEN2(dev))
  4341. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4342. intel_crtc->active = false;
  4343. intel_update_watermarks(crtc);
  4344. mutex_lock(&dev->struct_mutex);
  4345. intel_fbc_update(dev);
  4346. mutex_unlock(&dev->struct_mutex);
  4347. }
  4348. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4349. {
  4350. }
  4351. /* Master function to enable/disable CRTC and corresponding power wells */
  4352. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4353. {
  4354. struct drm_device *dev = crtc->dev;
  4355. struct drm_i915_private *dev_priv = dev->dev_private;
  4356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4357. enum intel_display_power_domain domain;
  4358. unsigned long domains;
  4359. if (enable) {
  4360. if (!intel_crtc->active) {
  4361. domains = get_crtc_power_domains(crtc);
  4362. for_each_power_domain(domain, domains)
  4363. intel_display_power_get(dev_priv, domain);
  4364. intel_crtc->enabled_power_domains = domains;
  4365. dev_priv->display.crtc_enable(crtc);
  4366. }
  4367. } else {
  4368. if (intel_crtc->active) {
  4369. dev_priv->display.crtc_disable(crtc);
  4370. domains = intel_crtc->enabled_power_domains;
  4371. for_each_power_domain(domain, domains)
  4372. intel_display_power_put(dev_priv, domain);
  4373. intel_crtc->enabled_power_domains = 0;
  4374. }
  4375. }
  4376. }
  4377. /**
  4378. * Sets the power management mode of the pipe and plane.
  4379. */
  4380. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4381. {
  4382. struct drm_device *dev = crtc->dev;
  4383. struct intel_encoder *intel_encoder;
  4384. bool enable = false;
  4385. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4386. enable |= intel_encoder->connectors_active;
  4387. intel_crtc_control(crtc, enable);
  4388. }
  4389. static void intel_crtc_disable(struct drm_crtc *crtc)
  4390. {
  4391. struct drm_device *dev = crtc->dev;
  4392. struct drm_connector *connector;
  4393. struct drm_i915_private *dev_priv = dev->dev_private;
  4394. /* crtc should still be enabled when we disable it. */
  4395. WARN_ON(!crtc->enabled);
  4396. dev_priv->display.crtc_disable(crtc);
  4397. dev_priv->display.off(crtc);
  4398. crtc->primary->funcs->disable_plane(crtc->primary);
  4399. /* Update computed state. */
  4400. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4401. if (!connector->encoder || !connector->encoder->crtc)
  4402. continue;
  4403. if (connector->encoder->crtc != crtc)
  4404. continue;
  4405. connector->dpms = DRM_MODE_DPMS_OFF;
  4406. to_intel_encoder(connector->encoder)->connectors_active = false;
  4407. }
  4408. }
  4409. void intel_encoder_destroy(struct drm_encoder *encoder)
  4410. {
  4411. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4412. drm_encoder_cleanup(encoder);
  4413. kfree(intel_encoder);
  4414. }
  4415. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4416. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4417. * state of the entire output pipe. */
  4418. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4419. {
  4420. if (mode == DRM_MODE_DPMS_ON) {
  4421. encoder->connectors_active = true;
  4422. intel_crtc_update_dpms(encoder->base.crtc);
  4423. } else {
  4424. encoder->connectors_active = false;
  4425. intel_crtc_update_dpms(encoder->base.crtc);
  4426. }
  4427. }
  4428. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4429. * internal consistency). */
  4430. static void intel_connector_check_state(struct intel_connector *connector)
  4431. {
  4432. if (connector->get_hw_state(connector)) {
  4433. struct intel_encoder *encoder = connector->encoder;
  4434. struct drm_crtc *crtc;
  4435. bool encoder_enabled;
  4436. enum pipe pipe;
  4437. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4438. connector->base.base.id,
  4439. connector->base.name);
  4440. /* there is no real hw state for MST connectors */
  4441. if (connector->mst_port)
  4442. return;
  4443. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4444. "wrong connector dpms state\n");
  4445. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4446. "active connector not linked to encoder\n");
  4447. if (encoder) {
  4448. I915_STATE_WARN(!encoder->connectors_active,
  4449. "encoder->connectors_active not set\n");
  4450. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4451. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4452. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4453. return;
  4454. crtc = encoder->base.crtc;
  4455. I915_STATE_WARN(!crtc->enabled, "crtc not enabled\n");
  4456. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4457. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4458. "encoder active on the wrong pipe\n");
  4459. }
  4460. }
  4461. }
  4462. /* Even simpler default implementation, if there's really no special case to
  4463. * consider. */
  4464. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4465. {
  4466. /* All the simple cases only support two dpms states. */
  4467. if (mode != DRM_MODE_DPMS_ON)
  4468. mode = DRM_MODE_DPMS_OFF;
  4469. if (mode == connector->dpms)
  4470. return;
  4471. connector->dpms = mode;
  4472. /* Only need to change hw state when actually enabled */
  4473. if (connector->encoder)
  4474. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4475. intel_modeset_check_state(connector->dev);
  4476. }
  4477. /* Simple connector->get_hw_state implementation for encoders that support only
  4478. * one connector and no cloning and hence the encoder state determines the state
  4479. * of the connector. */
  4480. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4481. {
  4482. enum pipe pipe = 0;
  4483. struct intel_encoder *encoder = connector->encoder;
  4484. return encoder->get_hw_state(encoder, &pipe);
  4485. }
  4486. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4487. struct intel_crtc_config *pipe_config)
  4488. {
  4489. struct drm_i915_private *dev_priv = dev->dev_private;
  4490. struct intel_crtc *pipe_B_crtc =
  4491. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4492. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4493. pipe_name(pipe), pipe_config->fdi_lanes);
  4494. if (pipe_config->fdi_lanes > 4) {
  4495. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4496. pipe_name(pipe), pipe_config->fdi_lanes);
  4497. return false;
  4498. }
  4499. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4500. if (pipe_config->fdi_lanes > 2) {
  4501. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4502. pipe_config->fdi_lanes);
  4503. return false;
  4504. } else {
  4505. return true;
  4506. }
  4507. }
  4508. if (INTEL_INFO(dev)->num_pipes == 2)
  4509. return true;
  4510. /* Ivybridge 3 pipe is really complicated */
  4511. switch (pipe) {
  4512. case PIPE_A:
  4513. return true;
  4514. case PIPE_B:
  4515. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4516. pipe_config->fdi_lanes > 2) {
  4517. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4518. pipe_name(pipe), pipe_config->fdi_lanes);
  4519. return false;
  4520. }
  4521. return true;
  4522. case PIPE_C:
  4523. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4524. pipe_B_crtc->config.fdi_lanes <= 2) {
  4525. if (pipe_config->fdi_lanes > 2) {
  4526. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4527. pipe_name(pipe), pipe_config->fdi_lanes);
  4528. return false;
  4529. }
  4530. } else {
  4531. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4532. return false;
  4533. }
  4534. return true;
  4535. default:
  4536. BUG();
  4537. }
  4538. }
  4539. #define RETRY 1
  4540. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4541. struct intel_crtc_config *pipe_config)
  4542. {
  4543. struct drm_device *dev = intel_crtc->base.dev;
  4544. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4545. int lane, link_bw, fdi_dotclock;
  4546. bool setup_ok, needs_recompute = false;
  4547. retry:
  4548. /* FDI is a binary signal running at ~2.7GHz, encoding
  4549. * each output octet as 10 bits. The actual frequency
  4550. * is stored as a divider into a 100MHz clock, and the
  4551. * mode pixel clock is stored in units of 1KHz.
  4552. * Hence the bw of each lane in terms of the mode signal
  4553. * is:
  4554. */
  4555. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4556. fdi_dotclock = adjusted_mode->crtc_clock;
  4557. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4558. pipe_config->pipe_bpp);
  4559. pipe_config->fdi_lanes = lane;
  4560. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4561. link_bw, &pipe_config->fdi_m_n);
  4562. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4563. intel_crtc->pipe, pipe_config);
  4564. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4565. pipe_config->pipe_bpp -= 2*3;
  4566. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4567. pipe_config->pipe_bpp);
  4568. needs_recompute = true;
  4569. pipe_config->bw_constrained = true;
  4570. goto retry;
  4571. }
  4572. if (needs_recompute)
  4573. return RETRY;
  4574. return setup_ok ? 0 : -EINVAL;
  4575. }
  4576. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4577. struct intel_crtc_config *pipe_config)
  4578. {
  4579. pipe_config->ips_enabled = i915.enable_ips &&
  4580. hsw_crtc_supports_ips(crtc) &&
  4581. pipe_config->pipe_bpp <= 24;
  4582. }
  4583. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4584. struct intel_crtc_config *pipe_config)
  4585. {
  4586. struct drm_device *dev = crtc->base.dev;
  4587. struct drm_i915_private *dev_priv = dev->dev_private;
  4588. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4589. /* FIXME should check pixel clock limits on all platforms */
  4590. if (INTEL_INFO(dev)->gen < 4) {
  4591. int clock_limit =
  4592. dev_priv->display.get_display_clock_speed(dev);
  4593. /*
  4594. * Enable pixel doubling when the dot clock
  4595. * is > 90% of the (display) core speed.
  4596. *
  4597. * GDG double wide on either pipe,
  4598. * otherwise pipe A only.
  4599. */
  4600. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4601. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4602. clock_limit *= 2;
  4603. pipe_config->double_wide = true;
  4604. }
  4605. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4606. return -EINVAL;
  4607. }
  4608. /*
  4609. * Pipe horizontal size must be even in:
  4610. * - DVO ganged mode
  4611. * - LVDS dual channel mode
  4612. * - Double wide pipe
  4613. */
  4614. if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4615. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4616. pipe_config->pipe_src_w &= ~1;
  4617. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4618. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4619. */
  4620. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4621. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4622. return -EINVAL;
  4623. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4624. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4625. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4626. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4627. * for lvds. */
  4628. pipe_config->pipe_bpp = 8*3;
  4629. }
  4630. if (HAS_IPS(dev))
  4631. hsw_compute_ips_config(crtc, pipe_config);
  4632. if (pipe_config->has_pch_encoder)
  4633. return ironlake_fdi_compute_config(crtc, pipe_config);
  4634. return 0;
  4635. }
  4636. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4637. {
  4638. struct drm_i915_private *dev_priv = dev->dev_private;
  4639. u32 val;
  4640. int divider;
  4641. /* FIXME: Punit isn't quite ready yet */
  4642. if (IS_CHERRYVIEW(dev))
  4643. return 400000;
  4644. if (dev_priv->hpll_freq == 0)
  4645. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4646. mutex_lock(&dev_priv->dpio_lock);
  4647. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4648. mutex_unlock(&dev_priv->dpio_lock);
  4649. divider = val & DISPLAY_FREQUENCY_VALUES;
  4650. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4651. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4652. "cdclk change in progress\n");
  4653. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4654. }
  4655. static int i945_get_display_clock_speed(struct drm_device *dev)
  4656. {
  4657. return 400000;
  4658. }
  4659. static int i915_get_display_clock_speed(struct drm_device *dev)
  4660. {
  4661. return 333000;
  4662. }
  4663. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4664. {
  4665. return 200000;
  4666. }
  4667. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4668. {
  4669. u16 gcfgc = 0;
  4670. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4671. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4672. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4673. return 267000;
  4674. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4675. return 333000;
  4676. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4677. return 444000;
  4678. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4679. return 200000;
  4680. default:
  4681. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4682. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4683. return 133000;
  4684. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4685. return 167000;
  4686. }
  4687. }
  4688. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4689. {
  4690. u16 gcfgc = 0;
  4691. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4692. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4693. return 133000;
  4694. else {
  4695. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4696. case GC_DISPLAY_CLOCK_333_MHZ:
  4697. return 333000;
  4698. default:
  4699. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4700. return 190000;
  4701. }
  4702. }
  4703. }
  4704. static int i865_get_display_clock_speed(struct drm_device *dev)
  4705. {
  4706. return 266000;
  4707. }
  4708. static int i855_get_display_clock_speed(struct drm_device *dev)
  4709. {
  4710. u16 hpllcc = 0;
  4711. /* Assume that the hardware is in the high speed state. This
  4712. * should be the default.
  4713. */
  4714. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4715. case GC_CLOCK_133_200:
  4716. case GC_CLOCK_100_200:
  4717. return 200000;
  4718. case GC_CLOCK_166_250:
  4719. return 250000;
  4720. case GC_CLOCK_100_133:
  4721. return 133000;
  4722. }
  4723. /* Shouldn't happen */
  4724. return 0;
  4725. }
  4726. static int i830_get_display_clock_speed(struct drm_device *dev)
  4727. {
  4728. return 133000;
  4729. }
  4730. static void
  4731. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4732. {
  4733. while (*num > DATA_LINK_M_N_MASK ||
  4734. *den > DATA_LINK_M_N_MASK) {
  4735. *num >>= 1;
  4736. *den >>= 1;
  4737. }
  4738. }
  4739. static void compute_m_n(unsigned int m, unsigned int n,
  4740. uint32_t *ret_m, uint32_t *ret_n)
  4741. {
  4742. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4743. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4744. intel_reduce_m_n_ratio(ret_m, ret_n);
  4745. }
  4746. void
  4747. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4748. int pixel_clock, int link_clock,
  4749. struct intel_link_m_n *m_n)
  4750. {
  4751. m_n->tu = 64;
  4752. compute_m_n(bits_per_pixel * pixel_clock,
  4753. link_clock * nlanes * 8,
  4754. &m_n->gmch_m, &m_n->gmch_n);
  4755. compute_m_n(pixel_clock, link_clock,
  4756. &m_n->link_m, &m_n->link_n);
  4757. }
  4758. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4759. {
  4760. if (i915.panel_use_ssc >= 0)
  4761. return i915.panel_use_ssc != 0;
  4762. return dev_priv->vbt.lvds_use_ssc
  4763. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4764. }
  4765. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4766. {
  4767. struct drm_device *dev = crtc->base.dev;
  4768. struct drm_i915_private *dev_priv = dev->dev_private;
  4769. int refclk;
  4770. if (IS_VALLEYVIEW(dev)) {
  4771. refclk = 100000;
  4772. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4773. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4774. refclk = dev_priv->vbt.lvds_ssc_freq;
  4775. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4776. } else if (!IS_GEN2(dev)) {
  4777. refclk = 96000;
  4778. } else {
  4779. refclk = 48000;
  4780. }
  4781. return refclk;
  4782. }
  4783. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4784. {
  4785. return (1 << dpll->n) << 16 | dpll->m2;
  4786. }
  4787. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4788. {
  4789. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4790. }
  4791. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4792. intel_clock_t *reduced_clock)
  4793. {
  4794. struct drm_device *dev = crtc->base.dev;
  4795. u32 fp, fp2 = 0;
  4796. if (IS_PINEVIEW(dev)) {
  4797. fp = pnv_dpll_compute_fp(&crtc->new_config->dpll);
  4798. if (reduced_clock)
  4799. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4800. } else {
  4801. fp = i9xx_dpll_compute_fp(&crtc->new_config->dpll);
  4802. if (reduced_clock)
  4803. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4804. }
  4805. crtc->new_config->dpll_hw_state.fp0 = fp;
  4806. crtc->lowfreq_avail = false;
  4807. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4808. reduced_clock && i915.powersave) {
  4809. crtc->new_config->dpll_hw_state.fp1 = fp2;
  4810. crtc->lowfreq_avail = true;
  4811. } else {
  4812. crtc->new_config->dpll_hw_state.fp1 = fp;
  4813. }
  4814. }
  4815. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4816. pipe)
  4817. {
  4818. u32 reg_val;
  4819. /*
  4820. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4821. * and set it to a reasonable value instead.
  4822. */
  4823. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4824. reg_val &= 0xffffff00;
  4825. reg_val |= 0x00000030;
  4826. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4827. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4828. reg_val &= 0x8cffffff;
  4829. reg_val = 0x8c000000;
  4830. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4831. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4832. reg_val &= 0xffffff00;
  4833. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4834. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4835. reg_val &= 0x00ffffff;
  4836. reg_val |= 0xb0000000;
  4837. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4838. }
  4839. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4840. struct intel_link_m_n *m_n)
  4841. {
  4842. struct drm_device *dev = crtc->base.dev;
  4843. struct drm_i915_private *dev_priv = dev->dev_private;
  4844. int pipe = crtc->pipe;
  4845. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4846. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4847. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4848. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4849. }
  4850. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4851. struct intel_link_m_n *m_n,
  4852. struct intel_link_m_n *m2_n2)
  4853. {
  4854. struct drm_device *dev = crtc->base.dev;
  4855. struct drm_i915_private *dev_priv = dev->dev_private;
  4856. int pipe = crtc->pipe;
  4857. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4858. if (INTEL_INFO(dev)->gen >= 5) {
  4859. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4860. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4861. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4862. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4863. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4864. * for gen < 8) and if DRRS is supported (to make sure the
  4865. * registers are not unnecessarily accessed).
  4866. */
  4867. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4868. crtc->config.has_drrs) {
  4869. I915_WRITE(PIPE_DATA_M2(transcoder),
  4870. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4871. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4872. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4873. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4874. }
  4875. } else {
  4876. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4877. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4878. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4879. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4880. }
  4881. }
  4882. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4883. {
  4884. if (crtc->config.has_pch_encoder)
  4885. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4886. else
  4887. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4888. &crtc->config.dp_m2_n2);
  4889. }
  4890. static void vlv_update_pll(struct intel_crtc *crtc,
  4891. struct intel_crtc_config *pipe_config)
  4892. {
  4893. u32 dpll, dpll_md;
  4894. /*
  4895. * Enable DPIO clock input. We should never disable the reference
  4896. * clock for pipe B, since VGA hotplug / manual detection depends
  4897. * on it.
  4898. */
  4899. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4900. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4901. /* We should never disable this, set it here for state tracking */
  4902. if (crtc->pipe == PIPE_B)
  4903. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4904. dpll |= DPLL_VCO_ENABLE;
  4905. pipe_config->dpll_hw_state.dpll = dpll;
  4906. dpll_md = (pipe_config->pixel_multiplier - 1)
  4907. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4908. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  4909. }
  4910. static void vlv_prepare_pll(struct intel_crtc *crtc,
  4911. const struct intel_crtc_config *pipe_config)
  4912. {
  4913. struct drm_device *dev = crtc->base.dev;
  4914. struct drm_i915_private *dev_priv = dev->dev_private;
  4915. int pipe = crtc->pipe;
  4916. u32 mdiv;
  4917. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4918. u32 coreclk, reg_val;
  4919. mutex_lock(&dev_priv->dpio_lock);
  4920. bestn = pipe_config->dpll.n;
  4921. bestm1 = pipe_config->dpll.m1;
  4922. bestm2 = pipe_config->dpll.m2;
  4923. bestp1 = pipe_config->dpll.p1;
  4924. bestp2 = pipe_config->dpll.p2;
  4925. /* See eDP HDMI DPIO driver vbios notes doc */
  4926. /* PLL B needs special handling */
  4927. if (pipe == PIPE_B)
  4928. vlv_pllb_recal_opamp(dev_priv, pipe);
  4929. /* Set up Tx target for periodic Rcomp update */
  4930. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4931. /* Disable target IRef on PLL */
  4932. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4933. reg_val &= 0x00ffffff;
  4934. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4935. /* Disable fast lock */
  4936. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4937. /* Set idtafcrecal before PLL is enabled */
  4938. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4939. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4940. mdiv |= ((bestn << DPIO_N_SHIFT));
  4941. mdiv |= (1 << DPIO_K_SHIFT);
  4942. /*
  4943. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4944. * but we don't support that).
  4945. * Note: don't use the DAC post divider as it seems unstable.
  4946. */
  4947. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4948. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4949. mdiv |= DPIO_ENABLE_CALIBRATION;
  4950. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4951. /* Set HBR and RBR LPF coefficients */
  4952. if (pipe_config->port_clock == 162000 ||
  4953. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  4954. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  4955. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4956. 0x009f0003);
  4957. else
  4958. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4959. 0x00d0000f);
  4960. if (crtc->config.has_dp_encoder) {
  4961. /* Use SSC source */
  4962. if (pipe == PIPE_A)
  4963. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4964. 0x0df40000);
  4965. else
  4966. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4967. 0x0df70000);
  4968. } else { /* HDMI or VGA */
  4969. /* Use bend source */
  4970. if (pipe == PIPE_A)
  4971. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4972. 0x0df70000);
  4973. else
  4974. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4975. 0x0df40000);
  4976. }
  4977. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4978. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4979. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  4980. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  4981. coreclk |= 0x01000000;
  4982. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4983. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4984. mutex_unlock(&dev_priv->dpio_lock);
  4985. }
  4986. static void chv_update_pll(struct intel_crtc *crtc,
  4987. struct intel_crtc_config *pipe_config)
  4988. {
  4989. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4990. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4991. DPLL_VCO_ENABLE;
  4992. if (crtc->pipe != PIPE_A)
  4993. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4994. pipe_config->dpll_hw_state.dpll_md =
  4995. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4996. }
  4997. static void chv_prepare_pll(struct intel_crtc *crtc,
  4998. const struct intel_crtc_config *pipe_config)
  4999. {
  5000. struct drm_device *dev = crtc->base.dev;
  5001. struct drm_i915_private *dev_priv = dev->dev_private;
  5002. int pipe = crtc->pipe;
  5003. int dpll_reg = DPLL(crtc->pipe);
  5004. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5005. u32 loopfilter, intcoeff;
  5006. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5007. int refclk;
  5008. bestn = pipe_config->dpll.n;
  5009. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5010. bestm1 = pipe_config->dpll.m1;
  5011. bestm2 = pipe_config->dpll.m2 >> 22;
  5012. bestp1 = pipe_config->dpll.p1;
  5013. bestp2 = pipe_config->dpll.p2;
  5014. /*
  5015. * Enable Refclk and SSC
  5016. */
  5017. I915_WRITE(dpll_reg,
  5018. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5019. mutex_lock(&dev_priv->dpio_lock);
  5020. /* p1 and p2 divider */
  5021. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5022. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5023. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5024. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5025. 1 << DPIO_CHV_K_DIV_SHIFT);
  5026. /* Feedback post-divider - m2 */
  5027. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5028. /* Feedback refclk divider - n and m1 */
  5029. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5030. DPIO_CHV_M1_DIV_BY_2 |
  5031. 1 << DPIO_CHV_N_DIV_SHIFT);
  5032. /* M2 fraction division */
  5033. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5034. /* M2 fraction division enable */
  5035. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  5036. DPIO_CHV_FRAC_DIV_EN |
  5037. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  5038. /* Loop filter */
  5039. refclk = i9xx_get_refclk(crtc, 0);
  5040. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  5041. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  5042. if (refclk == 100000)
  5043. intcoeff = 11;
  5044. else if (refclk == 38400)
  5045. intcoeff = 10;
  5046. else
  5047. intcoeff = 9;
  5048. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  5049. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5050. /* AFC Recal */
  5051. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5052. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5053. DPIO_AFC_RECAL);
  5054. mutex_unlock(&dev_priv->dpio_lock);
  5055. }
  5056. /**
  5057. * vlv_force_pll_on - forcibly enable just the PLL
  5058. * @dev_priv: i915 private structure
  5059. * @pipe: pipe PLL to enable
  5060. * @dpll: PLL configuration
  5061. *
  5062. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5063. * in cases where we need the PLL enabled even when @pipe is not going to
  5064. * be enabled.
  5065. */
  5066. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5067. const struct dpll *dpll)
  5068. {
  5069. struct intel_crtc *crtc =
  5070. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5071. struct intel_crtc_config pipe_config = {
  5072. .pixel_multiplier = 1,
  5073. .dpll = *dpll,
  5074. };
  5075. if (IS_CHERRYVIEW(dev)) {
  5076. chv_update_pll(crtc, &pipe_config);
  5077. chv_prepare_pll(crtc, &pipe_config);
  5078. chv_enable_pll(crtc, &pipe_config);
  5079. } else {
  5080. vlv_update_pll(crtc, &pipe_config);
  5081. vlv_prepare_pll(crtc, &pipe_config);
  5082. vlv_enable_pll(crtc, &pipe_config);
  5083. }
  5084. }
  5085. /**
  5086. * vlv_force_pll_off - forcibly disable just the PLL
  5087. * @dev_priv: i915 private structure
  5088. * @pipe: pipe PLL to disable
  5089. *
  5090. * Disable the PLL for @pipe. To be used in cases where we need
  5091. * the PLL enabled even when @pipe is not going to be enabled.
  5092. */
  5093. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5094. {
  5095. if (IS_CHERRYVIEW(dev))
  5096. chv_disable_pll(to_i915(dev), pipe);
  5097. else
  5098. vlv_disable_pll(to_i915(dev), pipe);
  5099. }
  5100. static void i9xx_update_pll(struct intel_crtc *crtc,
  5101. intel_clock_t *reduced_clock,
  5102. int num_connectors)
  5103. {
  5104. struct drm_device *dev = crtc->base.dev;
  5105. struct drm_i915_private *dev_priv = dev->dev_private;
  5106. u32 dpll;
  5107. bool is_sdvo;
  5108. struct dpll *clock = &crtc->new_config->dpll;
  5109. i9xx_update_pll_dividers(crtc, reduced_clock);
  5110. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5111. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5112. dpll = DPLL_VGA_MODE_DIS;
  5113. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5114. dpll |= DPLLB_MODE_LVDS;
  5115. else
  5116. dpll |= DPLLB_MODE_DAC_SERIAL;
  5117. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5118. dpll |= (crtc->new_config->pixel_multiplier - 1)
  5119. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5120. }
  5121. if (is_sdvo)
  5122. dpll |= DPLL_SDVO_HIGH_SPEED;
  5123. if (crtc->new_config->has_dp_encoder)
  5124. dpll |= DPLL_SDVO_HIGH_SPEED;
  5125. /* compute bitmask from p1 value */
  5126. if (IS_PINEVIEW(dev))
  5127. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5128. else {
  5129. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5130. if (IS_G4X(dev) && reduced_clock)
  5131. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5132. }
  5133. switch (clock->p2) {
  5134. case 5:
  5135. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5136. break;
  5137. case 7:
  5138. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5139. break;
  5140. case 10:
  5141. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5142. break;
  5143. case 14:
  5144. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5145. break;
  5146. }
  5147. if (INTEL_INFO(dev)->gen >= 4)
  5148. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5149. if (crtc->new_config->sdvo_tv_clock)
  5150. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5151. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5152. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5153. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5154. else
  5155. dpll |= PLL_REF_INPUT_DREFCLK;
  5156. dpll |= DPLL_VCO_ENABLE;
  5157. crtc->new_config->dpll_hw_state.dpll = dpll;
  5158. if (INTEL_INFO(dev)->gen >= 4) {
  5159. u32 dpll_md = (crtc->new_config->pixel_multiplier - 1)
  5160. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5161. crtc->new_config->dpll_hw_state.dpll_md = dpll_md;
  5162. }
  5163. }
  5164. static void i8xx_update_pll(struct intel_crtc *crtc,
  5165. intel_clock_t *reduced_clock,
  5166. int num_connectors)
  5167. {
  5168. struct drm_device *dev = crtc->base.dev;
  5169. struct drm_i915_private *dev_priv = dev->dev_private;
  5170. u32 dpll;
  5171. struct dpll *clock = &crtc->new_config->dpll;
  5172. i9xx_update_pll_dividers(crtc, reduced_clock);
  5173. dpll = DPLL_VGA_MODE_DIS;
  5174. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5175. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5176. } else {
  5177. if (clock->p1 == 2)
  5178. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5179. else
  5180. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5181. if (clock->p2 == 4)
  5182. dpll |= PLL_P2_DIVIDE_BY_4;
  5183. }
  5184. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5185. dpll |= DPLL_DVO_2X_MODE;
  5186. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5187. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5188. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5189. else
  5190. dpll |= PLL_REF_INPUT_DREFCLK;
  5191. dpll |= DPLL_VCO_ENABLE;
  5192. crtc->new_config->dpll_hw_state.dpll = dpll;
  5193. }
  5194. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5195. {
  5196. struct drm_device *dev = intel_crtc->base.dev;
  5197. struct drm_i915_private *dev_priv = dev->dev_private;
  5198. enum pipe pipe = intel_crtc->pipe;
  5199. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5200. struct drm_display_mode *adjusted_mode =
  5201. &intel_crtc->config.adjusted_mode;
  5202. uint32_t crtc_vtotal, crtc_vblank_end;
  5203. int vsyncshift = 0;
  5204. /* We need to be careful not to changed the adjusted mode, for otherwise
  5205. * the hw state checker will get angry at the mismatch. */
  5206. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5207. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5208. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5209. /* the chip adds 2 halflines automatically */
  5210. crtc_vtotal -= 1;
  5211. crtc_vblank_end -= 1;
  5212. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5213. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5214. else
  5215. vsyncshift = adjusted_mode->crtc_hsync_start -
  5216. adjusted_mode->crtc_htotal / 2;
  5217. if (vsyncshift < 0)
  5218. vsyncshift += adjusted_mode->crtc_htotal;
  5219. }
  5220. if (INTEL_INFO(dev)->gen > 3)
  5221. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5222. I915_WRITE(HTOTAL(cpu_transcoder),
  5223. (adjusted_mode->crtc_hdisplay - 1) |
  5224. ((adjusted_mode->crtc_htotal - 1) << 16));
  5225. I915_WRITE(HBLANK(cpu_transcoder),
  5226. (adjusted_mode->crtc_hblank_start - 1) |
  5227. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5228. I915_WRITE(HSYNC(cpu_transcoder),
  5229. (adjusted_mode->crtc_hsync_start - 1) |
  5230. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5231. I915_WRITE(VTOTAL(cpu_transcoder),
  5232. (adjusted_mode->crtc_vdisplay - 1) |
  5233. ((crtc_vtotal - 1) << 16));
  5234. I915_WRITE(VBLANK(cpu_transcoder),
  5235. (adjusted_mode->crtc_vblank_start - 1) |
  5236. ((crtc_vblank_end - 1) << 16));
  5237. I915_WRITE(VSYNC(cpu_transcoder),
  5238. (adjusted_mode->crtc_vsync_start - 1) |
  5239. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5240. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5241. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5242. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5243. * bits. */
  5244. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5245. (pipe == PIPE_B || pipe == PIPE_C))
  5246. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5247. /* pipesrc controls the size that is scaled from, which should
  5248. * always be the user's requested size.
  5249. */
  5250. I915_WRITE(PIPESRC(pipe),
  5251. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  5252. (intel_crtc->config.pipe_src_h - 1));
  5253. }
  5254. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5255. struct intel_crtc_config *pipe_config)
  5256. {
  5257. struct drm_device *dev = crtc->base.dev;
  5258. struct drm_i915_private *dev_priv = dev->dev_private;
  5259. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5260. uint32_t tmp;
  5261. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5262. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5263. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5264. tmp = I915_READ(HBLANK(cpu_transcoder));
  5265. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5266. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5267. tmp = I915_READ(HSYNC(cpu_transcoder));
  5268. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5269. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5270. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5271. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5272. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5273. tmp = I915_READ(VBLANK(cpu_transcoder));
  5274. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5275. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5276. tmp = I915_READ(VSYNC(cpu_transcoder));
  5277. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5278. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5279. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5280. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5281. pipe_config->adjusted_mode.crtc_vtotal += 1;
  5282. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  5283. }
  5284. tmp = I915_READ(PIPESRC(crtc->pipe));
  5285. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5286. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5287. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  5288. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  5289. }
  5290. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5291. struct intel_crtc_config *pipe_config)
  5292. {
  5293. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  5294. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  5295. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  5296. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  5297. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  5298. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  5299. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  5300. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  5301. mode->flags = pipe_config->adjusted_mode.flags;
  5302. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  5303. mode->flags |= pipe_config->adjusted_mode.flags;
  5304. }
  5305. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5306. {
  5307. struct drm_device *dev = intel_crtc->base.dev;
  5308. struct drm_i915_private *dev_priv = dev->dev_private;
  5309. uint32_t pipeconf;
  5310. pipeconf = 0;
  5311. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5312. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5313. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5314. if (intel_crtc->config.double_wide)
  5315. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5316. /* only g4x and later have fancy bpc/dither controls */
  5317. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5318. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5319. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5320. pipeconf |= PIPECONF_DITHER_EN |
  5321. PIPECONF_DITHER_TYPE_SP;
  5322. switch (intel_crtc->config.pipe_bpp) {
  5323. case 18:
  5324. pipeconf |= PIPECONF_6BPC;
  5325. break;
  5326. case 24:
  5327. pipeconf |= PIPECONF_8BPC;
  5328. break;
  5329. case 30:
  5330. pipeconf |= PIPECONF_10BPC;
  5331. break;
  5332. default:
  5333. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5334. BUG();
  5335. }
  5336. }
  5337. if (HAS_PIPE_CXSR(dev)) {
  5338. if (intel_crtc->lowfreq_avail) {
  5339. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5340. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5341. } else {
  5342. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5343. }
  5344. }
  5345. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5346. if (INTEL_INFO(dev)->gen < 4 ||
  5347. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5348. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5349. else
  5350. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5351. } else
  5352. pipeconf |= PIPECONF_PROGRESSIVE;
  5353. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5354. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5355. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5356. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5357. }
  5358. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc)
  5359. {
  5360. struct drm_device *dev = crtc->base.dev;
  5361. struct drm_i915_private *dev_priv = dev->dev_private;
  5362. int refclk, num_connectors = 0;
  5363. intel_clock_t clock, reduced_clock;
  5364. bool ok, has_reduced_clock = false;
  5365. bool is_lvds = false, is_dsi = false;
  5366. struct intel_encoder *encoder;
  5367. const intel_limit_t *limit;
  5368. for_each_intel_encoder(dev, encoder) {
  5369. if (encoder->new_crtc != crtc)
  5370. continue;
  5371. switch (encoder->type) {
  5372. case INTEL_OUTPUT_LVDS:
  5373. is_lvds = true;
  5374. break;
  5375. case INTEL_OUTPUT_DSI:
  5376. is_dsi = true;
  5377. break;
  5378. default:
  5379. break;
  5380. }
  5381. num_connectors++;
  5382. }
  5383. if (is_dsi)
  5384. return 0;
  5385. if (!crtc->new_config->clock_set) {
  5386. refclk = i9xx_get_refclk(crtc, num_connectors);
  5387. /*
  5388. * Returns a set of divisors for the desired target clock with
  5389. * the given refclk, or FALSE. The returned values represent
  5390. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5391. * 2) / p1 / p2.
  5392. */
  5393. limit = intel_limit(crtc, refclk);
  5394. ok = dev_priv->display.find_dpll(limit, crtc,
  5395. crtc->new_config->port_clock,
  5396. refclk, NULL, &clock);
  5397. if (!ok) {
  5398. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5399. return -EINVAL;
  5400. }
  5401. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5402. /*
  5403. * Ensure we match the reduced clock's P to the target
  5404. * clock. If the clocks don't match, we can't switch
  5405. * the display clock by using the FP0/FP1. In such case
  5406. * we will disable the LVDS downclock feature.
  5407. */
  5408. has_reduced_clock =
  5409. dev_priv->display.find_dpll(limit, crtc,
  5410. dev_priv->lvds_downclock,
  5411. refclk, &clock,
  5412. &reduced_clock);
  5413. }
  5414. /* Compat-code for transition, will disappear. */
  5415. crtc->new_config->dpll.n = clock.n;
  5416. crtc->new_config->dpll.m1 = clock.m1;
  5417. crtc->new_config->dpll.m2 = clock.m2;
  5418. crtc->new_config->dpll.p1 = clock.p1;
  5419. crtc->new_config->dpll.p2 = clock.p2;
  5420. }
  5421. if (IS_GEN2(dev)) {
  5422. i8xx_update_pll(crtc,
  5423. has_reduced_clock ? &reduced_clock : NULL,
  5424. num_connectors);
  5425. } else if (IS_CHERRYVIEW(dev)) {
  5426. chv_update_pll(crtc, crtc->new_config);
  5427. } else if (IS_VALLEYVIEW(dev)) {
  5428. vlv_update_pll(crtc, crtc->new_config);
  5429. } else {
  5430. i9xx_update_pll(crtc,
  5431. has_reduced_clock ? &reduced_clock : NULL,
  5432. num_connectors);
  5433. }
  5434. return 0;
  5435. }
  5436. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5437. struct intel_crtc_config *pipe_config)
  5438. {
  5439. struct drm_device *dev = crtc->base.dev;
  5440. struct drm_i915_private *dev_priv = dev->dev_private;
  5441. uint32_t tmp;
  5442. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5443. return;
  5444. tmp = I915_READ(PFIT_CONTROL);
  5445. if (!(tmp & PFIT_ENABLE))
  5446. return;
  5447. /* Check whether the pfit is attached to our pipe. */
  5448. if (INTEL_INFO(dev)->gen < 4) {
  5449. if (crtc->pipe != PIPE_B)
  5450. return;
  5451. } else {
  5452. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5453. return;
  5454. }
  5455. pipe_config->gmch_pfit.control = tmp;
  5456. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5457. if (INTEL_INFO(dev)->gen < 5)
  5458. pipe_config->gmch_pfit.lvds_border_bits =
  5459. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5460. }
  5461. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5462. struct intel_crtc_config *pipe_config)
  5463. {
  5464. struct drm_device *dev = crtc->base.dev;
  5465. struct drm_i915_private *dev_priv = dev->dev_private;
  5466. int pipe = pipe_config->cpu_transcoder;
  5467. intel_clock_t clock;
  5468. u32 mdiv;
  5469. int refclk = 100000;
  5470. /* In case of MIPI DPLL will not even be used */
  5471. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5472. return;
  5473. mutex_lock(&dev_priv->dpio_lock);
  5474. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5475. mutex_unlock(&dev_priv->dpio_lock);
  5476. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5477. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5478. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5479. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5480. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5481. vlv_clock(refclk, &clock);
  5482. /* clock.dot is the fast clock */
  5483. pipe_config->port_clock = clock.dot / 5;
  5484. }
  5485. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5486. struct intel_plane_config *plane_config)
  5487. {
  5488. struct drm_device *dev = crtc->base.dev;
  5489. struct drm_i915_private *dev_priv = dev->dev_private;
  5490. u32 val, base, offset;
  5491. int pipe = crtc->pipe, plane = crtc->plane;
  5492. int fourcc, pixel_format;
  5493. int aligned_height;
  5494. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5495. if (!crtc->base.primary->fb) {
  5496. DRM_DEBUG_KMS("failed to alloc fb\n");
  5497. return;
  5498. }
  5499. val = I915_READ(DSPCNTR(plane));
  5500. if (INTEL_INFO(dev)->gen >= 4)
  5501. if (val & DISPPLANE_TILED)
  5502. plane_config->tiled = true;
  5503. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5504. fourcc = intel_format_to_fourcc(pixel_format);
  5505. crtc->base.primary->fb->pixel_format = fourcc;
  5506. crtc->base.primary->fb->bits_per_pixel =
  5507. drm_format_plane_cpp(fourcc, 0) * 8;
  5508. if (INTEL_INFO(dev)->gen >= 4) {
  5509. if (plane_config->tiled)
  5510. offset = I915_READ(DSPTILEOFF(plane));
  5511. else
  5512. offset = I915_READ(DSPLINOFF(plane));
  5513. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5514. } else {
  5515. base = I915_READ(DSPADDR(plane));
  5516. }
  5517. plane_config->base = base;
  5518. val = I915_READ(PIPESRC(pipe));
  5519. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5520. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5521. val = I915_READ(DSPSTRIDE(pipe));
  5522. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5523. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5524. plane_config->tiled);
  5525. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5526. aligned_height);
  5527. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5528. pipe, plane, crtc->base.primary->fb->width,
  5529. crtc->base.primary->fb->height,
  5530. crtc->base.primary->fb->bits_per_pixel, base,
  5531. crtc->base.primary->fb->pitches[0],
  5532. plane_config->size);
  5533. }
  5534. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5535. struct intel_crtc_config *pipe_config)
  5536. {
  5537. struct drm_device *dev = crtc->base.dev;
  5538. struct drm_i915_private *dev_priv = dev->dev_private;
  5539. int pipe = pipe_config->cpu_transcoder;
  5540. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5541. intel_clock_t clock;
  5542. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5543. int refclk = 100000;
  5544. mutex_lock(&dev_priv->dpio_lock);
  5545. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5546. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5547. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5548. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5549. mutex_unlock(&dev_priv->dpio_lock);
  5550. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5551. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5552. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5553. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5554. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5555. chv_clock(refclk, &clock);
  5556. /* clock.dot is the fast clock */
  5557. pipe_config->port_clock = clock.dot / 5;
  5558. }
  5559. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5560. struct intel_crtc_config *pipe_config)
  5561. {
  5562. struct drm_device *dev = crtc->base.dev;
  5563. struct drm_i915_private *dev_priv = dev->dev_private;
  5564. uint32_t tmp;
  5565. if (!intel_display_power_is_enabled(dev_priv,
  5566. POWER_DOMAIN_PIPE(crtc->pipe)))
  5567. return false;
  5568. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5569. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5570. tmp = I915_READ(PIPECONF(crtc->pipe));
  5571. if (!(tmp & PIPECONF_ENABLE))
  5572. return false;
  5573. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5574. switch (tmp & PIPECONF_BPC_MASK) {
  5575. case PIPECONF_6BPC:
  5576. pipe_config->pipe_bpp = 18;
  5577. break;
  5578. case PIPECONF_8BPC:
  5579. pipe_config->pipe_bpp = 24;
  5580. break;
  5581. case PIPECONF_10BPC:
  5582. pipe_config->pipe_bpp = 30;
  5583. break;
  5584. default:
  5585. break;
  5586. }
  5587. }
  5588. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5589. pipe_config->limited_color_range = true;
  5590. if (INTEL_INFO(dev)->gen < 4)
  5591. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5592. intel_get_pipe_timings(crtc, pipe_config);
  5593. i9xx_get_pfit_config(crtc, pipe_config);
  5594. if (INTEL_INFO(dev)->gen >= 4) {
  5595. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5596. pipe_config->pixel_multiplier =
  5597. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5598. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5599. pipe_config->dpll_hw_state.dpll_md = tmp;
  5600. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5601. tmp = I915_READ(DPLL(crtc->pipe));
  5602. pipe_config->pixel_multiplier =
  5603. ((tmp & SDVO_MULTIPLIER_MASK)
  5604. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5605. } else {
  5606. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5607. * port and will be fixed up in the encoder->get_config
  5608. * function. */
  5609. pipe_config->pixel_multiplier = 1;
  5610. }
  5611. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5612. if (!IS_VALLEYVIEW(dev)) {
  5613. /*
  5614. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5615. * on 830. Filter it out here so that we don't
  5616. * report errors due to that.
  5617. */
  5618. if (IS_I830(dev))
  5619. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5620. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5621. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5622. } else {
  5623. /* Mask out read-only status bits. */
  5624. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5625. DPLL_PORTC_READY_MASK |
  5626. DPLL_PORTB_READY_MASK);
  5627. }
  5628. if (IS_CHERRYVIEW(dev))
  5629. chv_crtc_clock_get(crtc, pipe_config);
  5630. else if (IS_VALLEYVIEW(dev))
  5631. vlv_crtc_clock_get(crtc, pipe_config);
  5632. else
  5633. i9xx_crtc_clock_get(crtc, pipe_config);
  5634. return true;
  5635. }
  5636. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5637. {
  5638. struct drm_i915_private *dev_priv = dev->dev_private;
  5639. struct intel_encoder *encoder;
  5640. u32 val, final;
  5641. bool has_lvds = false;
  5642. bool has_cpu_edp = false;
  5643. bool has_panel = false;
  5644. bool has_ck505 = false;
  5645. bool can_ssc = false;
  5646. /* We need to take the global config into account */
  5647. for_each_intel_encoder(dev, encoder) {
  5648. switch (encoder->type) {
  5649. case INTEL_OUTPUT_LVDS:
  5650. has_panel = true;
  5651. has_lvds = true;
  5652. break;
  5653. case INTEL_OUTPUT_EDP:
  5654. has_panel = true;
  5655. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5656. has_cpu_edp = true;
  5657. break;
  5658. default:
  5659. break;
  5660. }
  5661. }
  5662. if (HAS_PCH_IBX(dev)) {
  5663. has_ck505 = dev_priv->vbt.display_clock_mode;
  5664. can_ssc = has_ck505;
  5665. } else {
  5666. has_ck505 = false;
  5667. can_ssc = true;
  5668. }
  5669. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5670. has_panel, has_lvds, has_ck505);
  5671. /* Ironlake: try to setup display ref clock before DPLL
  5672. * enabling. This is only under driver's control after
  5673. * PCH B stepping, previous chipset stepping should be
  5674. * ignoring this setting.
  5675. */
  5676. val = I915_READ(PCH_DREF_CONTROL);
  5677. /* As we must carefully and slowly disable/enable each source in turn,
  5678. * compute the final state we want first and check if we need to
  5679. * make any changes at all.
  5680. */
  5681. final = val;
  5682. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5683. if (has_ck505)
  5684. final |= DREF_NONSPREAD_CK505_ENABLE;
  5685. else
  5686. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5687. final &= ~DREF_SSC_SOURCE_MASK;
  5688. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5689. final &= ~DREF_SSC1_ENABLE;
  5690. if (has_panel) {
  5691. final |= DREF_SSC_SOURCE_ENABLE;
  5692. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5693. final |= DREF_SSC1_ENABLE;
  5694. if (has_cpu_edp) {
  5695. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5696. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5697. else
  5698. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5699. } else
  5700. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5701. } else {
  5702. final |= DREF_SSC_SOURCE_DISABLE;
  5703. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5704. }
  5705. if (final == val)
  5706. return;
  5707. /* Always enable nonspread source */
  5708. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5709. if (has_ck505)
  5710. val |= DREF_NONSPREAD_CK505_ENABLE;
  5711. else
  5712. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5713. if (has_panel) {
  5714. val &= ~DREF_SSC_SOURCE_MASK;
  5715. val |= DREF_SSC_SOURCE_ENABLE;
  5716. /* SSC must be turned on before enabling the CPU output */
  5717. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5718. DRM_DEBUG_KMS("Using SSC on panel\n");
  5719. val |= DREF_SSC1_ENABLE;
  5720. } else
  5721. val &= ~DREF_SSC1_ENABLE;
  5722. /* Get SSC going before enabling the outputs */
  5723. I915_WRITE(PCH_DREF_CONTROL, val);
  5724. POSTING_READ(PCH_DREF_CONTROL);
  5725. udelay(200);
  5726. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5727. /* Enable CPU source on CPU attached eDP */
  5728. if (has_cpu_edp) {
  5729. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5730. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5731. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5732. } else
  5733. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5734. } else
  5735. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5736. I915_WRITE(PCH_DREF_CONTROL, val);
  5737. POSTING_READ(PCH_DREF_CONTROL);
  5738. udelay(200);
  5739. } else {
  5740. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5741. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5742. /* Turn off CPU output */
  5743. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5744. I915_WRITE(PCH_DREF_CONTROL, val);
  5745. POSTING_READ(PCH_DREF_CONTROL);
  5746. udelay(200);
  5747. /* Turn off the SSC source */
  5748. val &= ~DREF_SSC_SOURCE_MASK;
  5749. val |= DREF_SSC_SOURCE_DISABLE;
  5750. /* Turn off SSC1 */
  5751. val &= ~DREF_SSC1_ENABLE;
  5752. I915_WRITE(PCH_DREF_CONTROL, val);
  5753. POSTING_READ(PCH_DREF_CONTROL);
  5754. udelay(200);
  5755. }
  5756. BUG_ON(val != final);
  5757. }
  5758. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5759. {
  5760. uint32_t tmp;
  5761. tmp = I915_READ(SOUTH_CHICKEN2);
  5762. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5763. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5764. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5765. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5766. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5767. tmp = I915_READ(SOUTH_CHICKEN2);
  5768. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5769. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5770. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5771. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5772. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5773. }
  5774. /* WaMPhyProgramming:hsw */
  5775. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5776. {
  5777. uint32_t tmp;
  5778. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5779. tmp &= ~(0xFF << 24);
  5780. tmp |= (0x12 << 24);
  5781. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5782. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5783. tmp |= (1 << 11);
  5784. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5785. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5786. tmp |= (1 << 11);
  5787. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5788. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5789. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5790. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5791. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5792. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5793. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5794. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5795. tmp &= ~(7 << 13);
  5796. tmp |= (5 << 13);
  5797. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5798. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5799. tmp &= ~(7 << 13);
  5800. tmp |= (5 << 13);
  5801. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5802. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5803. tmp &= ~0xFF;
  5804. tmp |= 0x1C;
  5805. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5806. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5807. tmp &= ~0xFF;
  5808. tmp |= 0x1C;
  5809. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5810. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5811. tmp &= ~(0xFF << 16);
  5812. tmp |= (0x1C << 16);
  5813. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5814. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5815. tmp &= ~(0xFF << 16);
  5816. tmp |= (0x1C << 16);
  5817. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5818. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5819. tmp |= (1 << 27);
  5820. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5821. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5822. tmp |= (1 << 27);
  5823. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5824. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5825. tmp &= ~(0xF << 28);
  5826. tmp |= (4 << 28);
  5827. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5828. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5829. tmp &= ~(0xF << 28);
  5830. tmp |= (4 << 28);
  5831. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5832. }
  5833. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5834. * Programming" based on the parameters passed:
  5835. * - Sequence to enable CLKOUT_DP
  5836. * - Sequence to enable CLKOUT_DP without spread
  5837. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5838. */
  5839. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5840. bool with_fdi)
  5841. {
  5842. struct drm_i915_private *dev_priv = dev->dev_private;
  5843. uint32_t reg, tmp;
  5844. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5845. with_spread = true;
  5846. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5847. with_fdi, "LP PCH doesn't have FDI\n"))
  5848. with_fdi = false;
  5849. mutex_lock(&dev_priv->dpio_lock);
  5850. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5851. tmp &= ~SBI_SSCCTL_DISABLE;
  5852. tmp |= SBI_SSCCTL_PATHALT;
  5853. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5854. udelay(24);
  5855. if (with_spread) {
  5856. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5857. tmp &= ~SBI_SSCCTL_PATHALT;
  5858. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5859. if (with_fdi) {
  5860. lpt_reset_fdi_mphy(dev_priv);
  5861. lpt_program_fdi_mphy(dev_priv);
  5862. }
  5863. }
  5864. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5865. SBI_GEN0 : SBI_DBUFF0;
  5866. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5867. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5868. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5869. mutex_unlock(&dev_priv->dpio_lock);
  5870. }
  5871. /* Sequence to disable CLKOUT_DP */
  5872. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5873. {
  5874. struct drm_i915_private *dev_priv = dev->dev_private;
  5875. uint32_t reg, tmp;
  5876. mutex_lock(&dev_priv->dpio_lock);
  5877. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5878. SBI_GEN0 : SBI_DBUFF0;
  5879. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5880. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5881. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5882. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5883. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5884. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5885. tmp |= SBI_SSCCTL_PATHALT;
  5886. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5887. udelay(32);
  5888. }
  5889. tmp |= SBI_SSCCTL_DISABLE;
  5890. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5891. }
  5892. mutex_unlock(&dev_priv->dpio_lock);
  5893. }
  5894. static void lpt_init_pch_refclk(struct drm_device *dev)
  5895. {
  5896. struct intel_encoder *encoder;
  5897. bool has_vga = false;
  5898. for_each_intel_encoder(dev, encoder) {
  5899. switch (encoder->type) {
  5900. case INTEL_OUTPUT_ANALOG:
  5901. has_vga = true;
  5902. break;
  5903. default:
  5904. break;
  5905. }
  5906. }
  5907. if (has_vga)
  5908. lpt_enable_clkout_dp(dev, true, true);
  5909. else
  5910. lpt_disable_clkout_dp(dev);
  5911. }
  5912. /*
  5913. * Initialize reference clocks when the driver loads
  5914. */
  5915. void intel_init_pch_refclk(struct drm_device *dev)
  5916. {
  5917. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5918. ironlake_init_pch_refclk(dev);
  5919. else if (HAS_PCH_LPT(dev))
  5920. lpt_init_pch_refclk(dev);
  5921. }
  5922. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5923. {
  5924. struct drm_device *dev = crtc->dev;
  5925. struct drm_i915_private *dev_priv = dev->dev_private;
  5926. struct intel_encoder *encoder;
  5927. int num_connectors = 0;
  5928. bool is_lvds = false;
  5929. for_each_intel_encoder(dev, encoder) {
  5930. if (encoder->new_crtc != to_intel_crtc(crtc))
  5931. continue;
  5932. switch (encoder->type) {
  5933. case INTEL_OUTPUT_LVDS:
  5934. is_lvds = true;
  5935. break;
  5936. default:
  5937. break;
  5938. }
  5939. num_connectors++;
  5940. }
  5941. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5942. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5943. dev_priv->vbt.lvds_ssc_freq);
  5944. return dev_priv->vbt.lvds_ssc_freq;
  5945. }
  5946. return 120000;
  5947. }
  5948. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5949. {
  5950. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5951. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5952. int pipe = intel_crtc->pipe;
  5953. uint32_t val;
  5954. val = 0;
  5955. switch (intel_crtc->config.pipe_bpp) {
  5956. case 18:
  5957. val |= PIPECONF_6BPC;
  5958. break;
  5959. case 24:
  5960. val |= PIPECONF_8BPC;
  5961. break;
  5962. case 30:
  5963. val |= PIPECONF_10BPC;
  5964. break;
  5965. case 36:
  5966. val |= PIPECONF_12BPC;
  5967. break;
  5968. default:
  5969. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5970. BUG();
  5971. }
  5972. if (intel_crtc->config.dither)
  5973. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5974. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5975. val |= PIPECONF_INTERLACED_ILK;
  5976. else
  5977. val |= PIPECONF_PROGRESSIVE;
  5978. if (intel_crtc->config.limited_color_range)
  5979. val |= PIPECONF_COLOR_RANGE_SELECT;
  5980. I915_WRITE(PIPECONF(pipe), val);
  5981. POSTING_READ(PIPECONF(pipe));
  5982. }
  5983. /*
  5984. * Set up the pipe CSC unit.
  5985. *
  5986. * Currently only full range RGB to limited range RGB conversion
  5987. * is supported, but eventually this should handle various
  5988. * RGB<->YCbCr scenarios as well.
  5989. */
  5990. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5991. {
  5992. struct drm_device *dev = crtc->dev;
  5993. struct drm_i915_private *dev_priv = dev->dev_private;
  5994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5995. int pipe = intel_crtc->pipe;
  5996. uint16_t coeff = 0x7800; /* 1.0 */
  5997. /*
  5998. * TODO: Check what kind of values actually come out of the pipe
  5999. * with these coeff/postoff values and adjust to get the best
  6000. * accuracy. Perhaps we even need to take the bpc value into
  6001. * consideration.
  6002. */
  6003. if (intel_crtc->config.limited_color_range)
  6004. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6005. /*
  6006. * GY/GU and RY/RU should be the other way around according
  6007. * to BSpec, but reality doesn't agree. Just set them up in
  6008. * a way that results in the correct picture.
  6009. */
  6010. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6011. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6012. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6013. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6014. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6015. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6016. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6017. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6018. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6019. if (INTEL_INFO(dev)->gen > 6) {
  6020. uint16_t postoff = 0;
  6021. if (intel_crtc->config.limited_color_range)
  6022. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6023. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6024. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6025. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6026. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6027. } else {
  6028. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6029. if (intel_crtc->config.limited_color_range)
  6030. mode |= CSC_BLACK_SCREEN_OFFSET;
  6031. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6032. }
  6033. }
  6034. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6035. {
  6036. struct drm_device *dev = crtc->dev;
  6037. struct drm_i915_private *dev_priv = dev->dev_private;
  6038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6039. enum pipe pipe = intel_crtc->pipe;
  6040. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  6041. uint32_t val;
  6042. val = 0;
  6043. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  6044. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6045. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6046. val |= PIPECONF_INTERLACED_ILK;
  6047. else
  6048. val |= PIPECONF_PROGRESSIVE;
  6049. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6050. POSTING_READ(PIPECONF(cpu_transcoder));
  6051. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6052. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6053. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6054. val = 0;
  6055. switch (intel_crtc->config.pipe_bpp) {
  6056. case 18:
  6057. val |= PIPEMISC_DITHER_6_BPC;
  6058. break;
  6059. case 24:
  6060. val |= PIPEMISC_DITHER_8_BPC;
  6061. break;
  6062. case 30:
  6063. val |= PIPEMISC_DITHER_10_BPC;
  6064. break;
  6065. case 36:
  6066. val |= PIPEMISC_DITHER_12_BPC;
  6067. break;
  6068. default:
  6069. /* Case prevented by pipe_config_set_bpp. */
  6070. BUG();
  6071. }
  6072. if (intel_crtc->config.dither)
  6073. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6074. I915_WRITE(PIPEMISC(pipe), val);
  6075. }
  6076. }
  6077. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6078. intel_clock_t *clock,
  6079. bool *has_reduced_clock,
  6080. intel_clock_t *reduced_clock)
  6081. {
  6082. struct drm_device *dev = crtc->dev;
  6083. struct drm_i915_private *dev_priv = dev->dev_private;
  6084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6085. int refclk;
  6086. const intel_limit_t *limit;
  6087. bool ret, is_lvds = false;
  6088. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6089. refclk = ironlake_get_refclk(crtc);
  6090. /*
  6091. * Returns a set of divisors for the desired target clock with the given
  6092. * refclk, or FALSE. The returned values represent the clock equation:
  6093. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6094. */
  6095. limit = intel_limit(intel_crtc, refclk);
  6096. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6097. intel_crtc->new_config->port_clock,
  6098. refclk, NULL, clock);
  6099. if (!ret)
  6100. return false;
  6101. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6102. /*
  6103. * Ensure we match the reduced clock's P to the target clock.
  6104. * If the clocks don't match, we can't switch the display clock
  6105. * by using the FP0/FP1. In such case we will disable the LVDS
  6106. * downclock feature.
  6107. */
  6108. *has_reduced_clock =
  6109. dev_priv->display.find_dpll(limit, intel_crtc,
  6110. dev_priv->lvds_downclock,
  6111. refclk, clock,
  6112. reduced_clock);
  6113. }
  6114. return true;
  6115. }
  6116. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6117. {
  6118. /*
  6119. * Account for spread spectrum to avoid
  6120. * oversubscribing the link. Max center spread
  6121. * is 2.5%; use 5% for safety's sake.
  6122. */
  6123. u32 bps = target_clock * bpp * 21 / 20;
  6124. return DIV_ROUND_UP(bps, link_bw * 8);
  6125. }
  6126. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6127. {
  6128. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6129. }
  6130. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6131. u32 *fp,
  6132. intel_clock_t *reduced_clock, u32 *fp2)
  6133. {
  6134. struct drm_crtc *crtc = &intel_crtc->base;
  6135. struct drm_device *dev = crtc->dev;
  6136. struct drm_i915_private *dev_priv = dev->dev_private;
  6137. struct intel_encoder *intel_encoder;
  6138. uint32_t dpll;
  6139. int factor, num_connectors = 0;
  6140. bool is_lvds = false, is_sdvo = false;
  6141. for_each_intel_encoder(dev, intel_encoder) {
  6142. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6143. continue;
  6144. switch (intel_encoder->type) {
  6145. case INTEL_OUTPUT_LVDS:
  6146. is_lvds = true;
  6147. break;
  6148. case INTEL_OUTPUT_SDVO:
  6149. case INTEL_OUTPUT_HDMI:
  6150. is_sdvo = true;
  6151. break;
  6152. default:
  6153. break;
  6154. }
  6155. num_connectors++;
  6156. }
  6157. /* Enable autotuning of the PLL clock (if permissible) */
  6158. factor = 21;
  6159. if (is_lvds) {
  6160. if ((intel_panel_use_ssc(dev_priv) &&
  6161. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6162. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6163. factor = 25;
  6164. } else if (intel_crtc->new_config->sdvo_tv_clock)
  6165. factor = 20;
  6166. if (ironlake_needs_fb_cb_tune(&intel_crtc->new_config->dpll, factor))
  6167. *fp |= FP_CB_TUNE;
  6168. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6169. *fp2 |= FP_CB_TUNE;
  6170. dpll = 0;
  6171. if (is_lvds)
  6172. dpll |= DPLLB_MODE_LVDS;
  6173. else
  6174. dpll |= DPLLB_MODE_DAC_SERIAL;
  6175. dpll |= (intel_crtc->new_config->pixel_multiplier - 1)
  6176. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6177. if (is_sdvo)
  6178. dpll |= DPLL_SDVO_HIGH_SPEED;
  6179. if (intel_crtc->new_config->has_dp_encoder)
  6180. dpll |= DPLL_SDVO_HIGH_SPEED;
  6181. /* compute bitmask from p1 value */
  6182. dpll |= (1 << (intel_crtc->new_config->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6183. /* also FPA1 */
  6184. dpll |= (1 << (intel_crtc->new_config->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6185. switch (intel_crtc->new_config->dpll.p2) {
  6186. case 5:
  6187. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6188. break;
  6189. case 7:
  6190. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6191. break;
  6192. case 10:
  6193. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6194. break;
  6195. case 14:
  6196. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6197. break;
  6198. }
  6199. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6200. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6201. else
  6202. dpll |= PLL_REF_INPUT_DREFCLK;
  6203. return dpll | DPLL_VCO_ENABLE;
  6204. }
  6205. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc)
  6206. {
  6207. struct drm_device *dev = crtc->base.dev;
  6208. intel_clock_t clock, reduced_clock;
  6209. u32 dpll = 0, fp = 0, fp2 = 0;
  6210. bool ok, has_reduced_clock = false;
  6211. bool is_lvds = false;
  6212. struct intel_shared_dpll *pll;
  6213. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6214. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6215. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6216. ok = ironlake_compute_clocks(&crtc->base, &clock,
  6217. &has_reduced_clock, &reduced_clock);
  6218. if (!ok && !crtc->new_config->clock_set) {
  6219. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6220. return -EINVAL;
  6221. }
  6222. /* Compat-code for transition, will disappear. */
  6223. if (!crtc->new_config->clock_set) {
  6224. crtc->new_config->dpll.n = clock.n;
  6225. crtc->new_config->dpll.m1 = clock.m1;
  6226. crtc->new_config->dpll.m2 = clock.m2;
  6227. crtc->new_config->dpll.p1 = clock.p1;
  6228. crtc->new_config->dpll.p2 = clock.p2;
  6229. }
  6230. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6231. if (crtc->new_config->has_pch_encoder) {
  6232. fp = i9xx_dpll_compute_fp(&crtc->new_config->dpll);
  6233. if (has_reduced_clock)
  6234. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6235. dpll = ironlake_compute_dpll(crtc,
  6236. &fp, &reduced_clock,
  6237. has_reduced_clock ? &fp2 : NULL);
  6238. crtc->new_config->dpll_hw_state.dpll = dpll;
  6239. crtc->new_config->dpll_hw_state.fp0 = fp;
  6240. if (has_reduced_clock)
  6241. crtc->new_config->dpll_hw_state.fp1 = fp2;
  6242. else
  6243. crtc->new_config->dpll_hw_state.fp1 = fp;
  6244. pll = intel_get_shared_dpll(crtc);
  6245. if (pll == NULL) {
  6246. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6247. pipe_name(crtc->pipe));
  6248. return -EINVAL;
  6249. }
  6250. }
  6251. if (is_lvds && has_reduced_clock && i915.powersave)
  6252. crtc->lowfreq_avail = true;
  6253. else
  6254. crtc->lowfreq_avail = false;
  6255. return 0;
  6256. }
  6257. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6258. struct intel_link_m_n *m_n)
  6259. {
  6260. struct drm_device *dev = crtc->base.dev;
  6261. struct drm_i915_private *dev_priv = dev->dev_private;
  6262. enum pipe pipe = crtc->pipe;
  6263. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6264. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6265. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6266. & ~TU_SIZE_MASK;
  6267. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6268. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6269. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6270. }
  6271. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6272. enum transcoder transcoder,
  6273. struct intel_link_m_n *m_n,
  6274. struct intel_link_m_n *m2_n2)
  6275. {
  6276. struct drm_device *dev = crtc->base.dev;
  6277. struct drm_i915_private *dev_priv = dev->dev_private;
  6278. enum pipe pipe = crtc->pipe;
  6279. if (INTEL_INFO(dev)->gen >= 5) {
  6280. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6281. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6282. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6283. & ~TU_SIZE_MASK;
  6284. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6285. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6286. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6287. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6288. * gen < 8) and if DRRS is supported (to make sure the
  6289. * registers are not unnecessarily read).
  6290. */
  6291. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6292. crtc->config.has_drrs) {
  6293. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6294. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6295. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6296. & ~TU_SIZE_MASK;
  6297. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6298. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6299. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6300. }
  6301. } else {
  6302. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6303. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6304. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6305. & ~TU_SIZE_MASK;
  6306. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6307. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6308. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6309. }
  6310. }
  6311. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6312. struct intel_crtc_config *pipe_config)
  6313. {
  6314. if (crtc->config.has_pch_encoder)
  6315. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6316. else
  6317. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6318. &pipe_config->dp_m_n,
  6319. &pipe_config->dp_m2_n2);
  6320. }
  6321. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6322. struct intel_crtc_config *pipe_config)
  6323. {
  6324. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6325. &pipe_config->fdi_m_n, NULL);
  6326. }
  6327. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6328. struct intel_crtc_config *pipe_config)
  6329. {
  6330. struct drm_device *dev = crtc->base.dev;
  6331. struct drm_i915_private *dev_priv = dev->dev_private;
  6332. uint32_t tmp;
  6333. tmp = I915_READ(PS_CTL(crtc->pipe));
  6334. if (tmp & PS_ENABLE) {
  6335. pipe_config->pch_pfit.enabled = true;
  6336. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6337. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6338. }
  6339. }
  6340. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6341. struct intel_crtc_config *pipe_config)
  6342. {
  6343. struct drm_device *dev = crtc->base.dev;
  6344. struct drm_i915_private *dev_priv = dev->dev_private;
  6345. uint32_t tmp;
  6346. tmp = I915_READ(PF_CTL(crtc->pipe));
  6347. if (tmp & PF_ENABLE) {
  6348. pipe_config->pch_pfit.enabled = true;
  6349. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6350. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6351. /* We currently do not free assignements of panel fitters on
  6352. * ivb/hsw (since we don't use the higher upscaling modes which
  6353. * differentiates them) so just WARN about this case for now. */
  6354. if (IS_GEN7(dev)) {
  6355. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6356. PF_PIPE_SEL_IVB(crtc->pipe));
  6357. }
  6358. }
  6359. }
  6360. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6361. struct intel_plane_config *plane_config)
  6362. {
  6363. struct drm_device *dev = crtc->base.dev;
  6364. struct drm_i915_private *dev_priv = dev->dev_private;
  6365. u32 val, base, offset;
  6366. int pipe = crtc->pipe, plane = crtc->plane;
  6367. int fourcc, pixel_format;
  6368. int aligned_height;
  6369. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6370. if (!crtc->base.primary->fb) {
  6371. DRM_DEBUG_KMS("failed to alloc fb\n");
  6372. return;
  6373. }
  6374. val = I915_READ(DSPCNTR(plane));
  6375. if (INTEL_INFO(dev)->gen >= 4)
  6376. if (val & DISPPLANE_TILED)
  6377. plane_config->tiled = true;
  6378. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6379. fourcc = intel_format_to_fourcc(pixel_format);
  6380. crtc->base.primary->fb->pixel_format = fourcc;
  6381. crtc->base.primary->fb->bits_per_pixel =
  6382. drm_format_plane_cpp(fourcc, 0) * 8;
  6383. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6384. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6385. offset = I915_READ(DSPOFFSET(plane));
  6386. } else {
  6387. if (plane_config->tiled)
  6388. offset = I915_READ(DSPTILEOFF(plane));
  6389. else
  6390. offset = I915_READ(DSPLINOFF(plane));
  6391. }
  6392. plane_config->base = base;
  6393. val = I915_READ(PIPESRC(pipe));
  6394. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6395. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6396. val = I915_READ(DSPSTRIDE(pipe));
  6397. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6398. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6399. plane_config->tiled);
  6400. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6401. aligned_height);
  6402. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6403. pipe, plane, crtc->base.primary->fb->width,
  6404. crtc->base.primary->fb->height,
  6405. crtc->base.primary->fb->bits_per_pixel, base,
  6406. crtc->base.primary->fb->pitches[0],
  6407. plane_config->size);
  6408. }
  6409. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6410. struct intel_crtc_config *pipe_config)
  6411. {
  6412. struct drm_device *dev = crtc->base.dev;
  6413. struct drm_i915_private *dev_priv = dev->dev_private;
  6414. uint32_t tmp;
  6415. if (!intel_display_power_is_enabled(dev_priv,
  6416. POWER_DOMAIN_PIPE(crtc->pipe)))
  6417. return false;
  6418. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6419. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6420. tmp = I915_READ(PIPECONF(crtc->pipe));
  6421. if (!(tmp & PIPECONF_ENABLE))
  6422. return false;
  6423. switch (tmp & PIPECONF_BPC_MASK) {
  6424. case PIPECONF_6BPC:
  6425. pipe_config->pipe_bpp = 18;
  6426. break;
  6427. case PIPECONF_8BPC:
  6428. pipe_config->pipe_bpp = 24;
  6429. break;
  6430. case PIPECONF_10BPC:
  6431. pipe_config->pipe_bpp = 30;
  6432. break;
  6433. case PIPECONF_12BPC:
  6434. pipe_config->pipe_bpp = 36;
  6435. break;
  6436. default:
  6437. break;
  6438. }
  6439. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6440. pipe_config->limited_color_range = true;
  6441. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6442. struct intel_shared_dpll *pll;
  6443. pipe_config->has_pch_encoder = true;
  6444. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6445. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6446. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6447. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6448. if (HAS_PCH_IBX(dev_priv->dev)) {
  6449. pipe_config->shared_dpll =
  6450. (enum intel_dpll_id) crtc->pipe;
  6451. } else {
  6452. tmp = I915_READ(PCH_DPLL_SEL);
  6453. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6454. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6455. else
  6456. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6457. }
  6458. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6459. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6460. &pipe_config->dpll_hw_state));
  6461. tmp = pipe_config->dpll_hw_state.dpll;
  6462. pipe_config->pixel_multiplier =
  6463. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6464. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6465. ironlake_pch_clock_get(crtc, pipe_config);
  6466. } else {
  6467. pipe_config->pixel_multiplier = 1;
  6468. }
  6469. intel_get_pipe_timings(crtc, pipe_config);
  6470. ironlake_get_pfit_config(crtc, pipe_config);
  6471. return true;
  6472. }
  6473. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6474. {
  6475. struct drm_device *dev = dev_priv->dev;
  6476. struct intel_crtc *crtc;
  6477. for_each_intel_crtc(dev, crtc)
  6478. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6479. pipe_name(crtc->pipe));
  6480. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6481. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6482. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6483. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6484. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6485. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6486. "CPU PWM1 enabled\n");
  6487. if (IS_HASWELL(dev))
  6488. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6489. "CPU PWM2 enabled\n");
  6490. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6491. "PCH PWM1 enabled\n");
  6492. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6493. "Utility pin enabled\n");
  6494. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6495. /*
  6496. * In theory we can still leave IRQs enabled, as long as only the HPD
  6497. * interrupts remain enabled. We used to check for that, but since it's
  6498. * gen-specific and since we only disable LCPLL after we fully disable
  6499. * the interrupts, the check below should be enough.
  6500. */
  6501. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6502. }
  6503. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6504. {
  6505. struct drm_device *dev = dev_priv->dev;
  6506. if (IS_HASWELL(dev))
  6507. return I915_READ(D_COMP_HSW);
  6508. else
  6509. return I915_READ(D_COMP_BDW);
  6510. }
  6511. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6512. {
  6513. struct drm_device *dev = dev_priv->dev;
  6514. if (IS_HASWELL(dev)) {
  6515. mutex_lock(&dev_priv->rps.hw_lock);
  6516. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6517. val))
  6518. DRM_ERROR("Failed to write to D_COMP\n");
  6519. mutex_unlock(&dev_priv->rps.hw_lock);
  6520. } else {
  6521. I915_WRITE(D_COMP_BDW, val);
  6522. POSTING_READ(D_COMP_BDW);
  6523. }
  6524. }
  6525. /*
  6526. * This function implements pieces of two sequences from BSpec:
  6527. * - Sequence for display software to disable LCPLL
  6528. * - Sequence for display software to allow package C8+
  6529. * The steps implemented here are just the steps that actually touch the LCPLL
  6530. * register. Callers should take care of disabling all the display engine
  6531. * functions, doing the mode unset, fixing interrupts, etc.
  6532. */
  6533. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6534. bool switch_to_fclk, bool allow_power_down)
  6535. {
  6536. uint32_t val;
  6537. assert_can_disable_lcpll(dev_priv);
  6538. val = I915_READ(LCPLL_CTL);
  6539. if (switch_to_fclk) {
  6540. val |= LCPLL_CD_SOURCE_FCLK;
  6541. I915_WRITE(LCPLL_CTL, val);
  6542. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6543. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6544. DRM_ERROR("Switching to FCLK failed\n");
  6545. val = I915_READ(LCPLL_CTL);
  6546. }
  6547. val |= LCPLL_PLL_DISABLE;
  6548. I915_WRITE(LCPLL_CTL, val);
  6549. POSTING_READ(LCPLL_CTL);
  6550. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6551. DRM_ERROR("LCPLL still locked\n");
  6552. val = hsw_read_dcomp(dev_priv);
  6553. val |= D_COMP_COMP_DISABLE;
  6554. hsw_write_dcomp(dev_priv, val);
  6555. ndelay(100);
  6556. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6557. 1))
  6558. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6559. if (allow_power_down) {
  6560. val = I915_READ(LCPLL_CTL);
  6561. val |= LCPLL_POWER_DOWN_ALLOW;
  6562. I915_WRITE(LCPLL_CTL, val);
  6563. POSTING_READ(LCPLL_CTL);
  6564. }
  6565. }
  6566. /*
  6567. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6568. * source.
  6569. */
  6570. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6571. {
  6572. uint32_t val;
  6573. val = I915_READ(LCPLL_CTL);
  6574. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6575. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6576. return;
  6577. /*
  6578. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6579. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6580. *
  6581. * The other problem is that hsw_restore_lcpll() is called as part of
  6582. * the runtime PM resume sequence, so we can't just call
  6583. * gen6_gt_force_wake_get() because that function calls
  6584. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6585. * while we are on the resume sequence. So to solve this problem we have
  6586. * to call special forcewake code that doesn't touch runtime PM and
  6587. * doesn't enable the forcewake delayed work.
  6588. */
  6589. spin_lock_irq(&dev_priv->uncore.lock);
  6590. if (dev_priv->uncore.forcewake_count++ == 0)
  6591. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6592. spin_unlock_irq(&dev_priv->uncore.lock);
  6593. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6594. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6595. I915_WRITE(LCPLL_CTL, val);
  6596. POSTING_READ(LCPLL_CTL);
  6597. }
  6598. val = hsw_read_dcomp(dev_priv);
  6599. val |= D_COMP_COMP_FORCE;
  6600. val &= ~D_COMP_COMP_DISABLE;
  6601. hsw_write_dcomp(dev_priv, val);
  6602. val = I915_READ(LCPLL_CTL);
  6603. val &= ~LCPLL_PLL_DISABLE;
  6604. I915_WRITE(LCPLL_CTL, val);
  6605. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6606. DRM_ERROR("LCPLL not locked yet\n");
  6607. if (val & LCPLL_CD_SOURCE_FCLK) {
  6608. val = I915_READ(LCPLL_CTL);
  6609. val &= ~LCPLL_CD_SOURCE_FCLK;
  6610. I915_WRITE(LCPLL_CTL, val);
  6611. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6612. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6613. DRM_ERROR("Switching back to LCPLL failed\n");
  6614. }
  6615. /* See the big comment above. */
  6616. spin_lock_irq(&dev_priv->uncore.lock);
  6617. if (--dev_priv->uncore.forcewake_count == 0)
  6618. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6619. spin_unlock_irq(&dev_priv->uncore.lock);
  6620. }
  6621. /*
  6622. * Package states C8 and deeper are really deep PC states that can only be
  6623. * reached when all the devices on the system allow it, so even if the graphics
  6624. * device allows PC8+, it doesn't mean the system will actually get to these
  6625. * states. Our driver only allows PC8+ when going into runtime PM.
  6626. *
  6627. * The requirements for PC8+ are that all the outputs are disabled, the power
  6628. * well is disabled and most interrupts are disabled, and these are also
  6629. * requirements for runtime PM. When these conditions are met, we manually do
  6630. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6631. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6632. * hang the machine.
  6633. *
  6634. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6635. * the state of some registers, so when we come back from PC8+ we need to
  6636. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6637. * need to take care of the registers kept by RC6. Notice that this happens even
  6638. * if we don't put the device in PCI D3 state (which is what currently happens
  6639. * because of the runtime PM support).
  6640. *
  6641. * For more, read "Display Sequences for Package C8" on the hardware
  6642. * documentation.
  6643. */
  6644. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6645. {
  6646. struct drm_device *dev = dev_priv->dev;
  6647. uint32_t val;
  6648. DRM_DEBUG_KMS("Enabling package C8+\n");
  6649. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6650. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6651. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6652. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6653. }
  6654. lpt_disable_clkout_dp(dev);
  6655. hsw_disable_lcpll(dev_priv, true, true);
  6656. }
  6657. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6658. {
  6659. struct drm_device *dev = dev_priv->dev;
  6660. uint32_t val;
  6661. DRM_DEBUG_KMS("Disabling package C8+\n");
  6662. hsw_restore_lcpll(dev_priv);
  6663. lpt_init_pch_refclk(dev);
  6664. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6665. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6666. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6667. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6668. }
  6669. intel_prepare_ddi(dev);
  6670. }
  6671. static int haswell_crtc_compute_clock(struct intel_crtc *crtc)
  6672. {
  6673. if (!intel_ddi_pll_select(crtc))
  6674. return -EINVAL;
  6675. crtc->lowfreq_avail = false;
  6676. return 0;
  6677. }
  6678. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  6679. enum port port,
  6680. struct intel_crtc_config *pipe_config)
  6681. {
  6682. u32 temp, dpll_ctl1;
  6683. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  6684. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  6685. switch (pipe_config->ddi_pll_sel) {
  6686. case SKL_DPLL0:
  6687. /*
  6688. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  6689. * of the shared DPLL framework and thus needs to be read out
  6690. * separately
  6691. */
  6692. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  6693. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  6694. break;
  6695. case SKL_DPLL1:
  6696. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  6697. break;
  6698. case SKL_DPLL2:
  6699. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  6700. break;
  6701. case SKL_DPLL3:
  6702. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  6703. break;
  6704. }
  6705. }
  6706. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6707. enum port port,
  6708. struct intel_crtc_config *pipe_config)
  6709. {
  6710. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6711. switch (pipe_config->ddi_pll_sel) {
  6712. case PORT_CLK_SEL_WRPLL1:
  6713. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6714. break;
  6715. case PORT_CLK_SEL_WRPLL2:
  6716. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6717. break;
  6718. }
  6719. }
  6720. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6721. struct intel_crtc_config *pipe_config)
  6722. {
  6723. struct drm_device *dev = crtc->base.dev;
  6724. struct drm_i915_private *dev_priv = dev->dev_private;
  6725. struct intel_shared_dpll *pll;
  6726. enum port port;
  6727. uint32_t tmp;
  6728. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6729. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6730. if (IS_SKYLAKE(dev))
  6731. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  6732. else
  6733. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6734. if (pipe_config->shared_dpll >= 0) {
  6735. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6736. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6737. &pipe_config->dpll_hw_state));
  6738. }
  6739. /*
  6740. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6741. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6742. * the PCH transcoder is on.
  6743. */
  6744. if (INTEL_INFO(dev)->gen < 9 &&
  6745. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6746. pipe_config->has_pch_encoder = true;
  6747. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6748. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6749. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6750. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6751. }
  6752. }
  6753. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6754. struct intel_crtc_config *pipe_config)
  6755. {
  6756. struct drm_device *dev = crtc->base.dev;
  6757. struct drm_i915_private *dev_priv = dev->dev_private;
  6758. enum intel_display_power_domain pfit_domain;
  6759. uint32_t tmp;
  6760. if (!intel_display_power_is_enabled(dev_priv,
  6761. POWER_DOMAIN_PIPE(crtc->pipe)))
  6762. return false;
  6763. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6764. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6765. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6766. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6767. enum pipe trans_edp_pipe;
  6768. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6769. default:
  6770. WARN(1, "unknown pipe linked to edp transcoder\n");
  6771. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6772. case TRANS_DDI_EDP_INPUT_A_ON:
  6773. trans_edp_pipe = PIPE_A;
  6774. break;
  6775. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6776. trans_edp_pipe = PIPE_B;
  6777. break;
  6778. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6779. trans_edp_pipe = PIPE_C;
  6780. break;
  6781. }
  6782. if (trans_edp_pipe == crtc->pipe)
  6783. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6784. }
  6785. if (!intel_display_power_is_enabled(dev_priv,
  6786. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6787. return false;
  6788. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6789. if (!(tmp & PIPECONF_ENABLE))
  6790. return false;
  6791. haswell_get_ddi_port_state(crtc, pipe_config);
  6792. intel_get_pipe_timings(crtc, pipe_config);
  6793. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6794. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  6795. if (IS_SKYLAKE(dev))
  6796. skylake_get_pfit_config(crtc, pipe_config);
  6797. else
  6798. ironlake_get_pfit_config(crtc, pipe_config);
  6799. }
  6800. if (IS_HASWELL(dev))
  6801. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6802. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6803. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  6804. pipe_config->pixel_multiplier =
  6805. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  6806. } else {
  6807. pipe_config->pixel_multiplier = 1;
  6808. }
  6809. return true;
  6810. }
  6811. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6812. {
  6813. struct drm_device *dev = crtc->dev;
  6814. struct drm_i915_private *dev_priv = dev->dev_private;
  6815. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6816. uint32_t cntl = 0, size = 0;
  6817. if (base) {
  6818. unsigned int width = intel_crtc->cursor_width;
  6819. unsigned int height = intel_crtc->cursor_height;
  6820. unsigned int stride = roundup_pow_of_two(width) * 4;
  6821. switch (stride) {
  6822. default:
  6823. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6824. width, stride);
  6825. stride = 256;
  6826. /* fallthrough */
  6827. case 256:
  6828. case 512:
  6829. case 1024:
  6830. case 2048:
  6831. break;
  6832. }
  6833. cntl |= CURSOR_ENABLE |
  6834. CURSOR_GAMMA_ENABLE |
  6835. CURSOR_FORMAT_ARGB |
  6836. CURSOR_STRIDE(stride);
  6837. size = (height << 12) | width;
  6838. }
  6839. if (intel_crtc->cursor_cntl != 0 &&
  6840. (intel_crtc->cursor_base != base ||
  6841. intel_crtc->cursor_size != size ||
  6842. intel_crtc->cursor_cntl != cntl)) {
  6843. /* On these chipsets we can only modify the base/size/stride
  6844. * whilst the cursor is disabled.
  6845. */
  6846. I915_WRITE(_CURACNTR, 0);
  6847. POSTING_READ(_CURACNTR);
  6848. intel_crtc->cursor_cntl = 0;
  6849. }
  6850. if (intel_crtc->cursor_base != base) {
  6851. I915_WRITE(_CURABASE, base);
  6852. intel_crtc->cursor_base = base;
  6853. }
  6854. if (intel_crtc->cursor_size != size) {
  6855. I915_WRITE(CURSIZE, size);
  6856. intel_crtc->cursor_size = size;
  6857. }
  6858. if (intel_crtc->cursor_cntl != cntl) {
  6859. I915_WRITE(_CURACNTR, cntl);
  6860. POSTING_READ(_CURACNTR);
  6861. intel_crtc->cursor_cntl = cntl;
  6862. }
  6863. }
  6864. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6865. {
  6866. struct drm_device *dev = crtc->dev;
  6867. struct drm_i915_private *dev_priv = dev->dev_private;
  6868. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6869. int pipe = intel_crtc->pipe;
  6870. uint32_t cntl;
  6871. cntl = 0;
  6872. if (base) {
  6873. cntl = MCURSOR_GAMMA_ENABLE;
  6874. switch (intel_crtc->cursor_width) {
  6875. case 64:
  6876. cntl |= CURSOR_MODE_64_ARGB_AX;
  6877. break;
  6878. case 128:
  6879. cntl |= CURSOR_MODE_128_ARGB_AX;
  6880. break;
  6881. case 256:
  6882. cntl |= CURSOR_MODE_256_ARGB_AX;
  6883. break;
  6884. default:
  6885. MISSING_CASE(intel_crtc->cursor_width);
  6886. return;
  6887. }
  6888. cntl |= pipe << 28; /* Connect to correct pipe */
  6889. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6890. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6891. }
  6892. if (to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180))
  6893. cntl |= CURSOR_ROTATE_180;
  6894. if (intel_crtc->cursor_cntl != cntl) {
  6895. I915_WRITE(CURCNTR(pipe), cntl);
  6896. POSTING_READ(CURCNTR(pipe));
  6897. intel_crtc->cursor_cntl = cntl;
  6898. }
  6899. /* and commit changes on next vblank */
  6900. I915_WRITE(CURBASE(pipe), base);
  6901. POSTING_READ(CURBASE(pipe));
  6902. intel_crtc->cursor_base = base;
  6903. }
  6904. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6905. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6906. bool on)
  6907. {
  6908. struct drm_device *dev = crtc->dev;
  6909. struct drm_i915_private *dev_priv = dev->dev_private;
  6910. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6911. int pipe = intel_crtc->pipe;
  6912. int x = crtc->cursor_x;
  6913. int y = crtc->cursor_y;
  6914. u32 base = 0, pos = 0;
  6915. if (on)
  6916. base = intel_crtc->cursor_addr;
  6917. if (x >= intel_crtc->config.pipe_src_w)
  6918. base = 0;
  6919. if (y >= intel_crtc->config.pipe_src_h)
  6920. base = 0;
  6921. if (x < 0) {
  6922. if (x + intel_crtc->cursor_width <= 0)
  6923. base = 0;
  6924. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6925. x = -x;
  6926. }
  6927. pos |= x << CURSOR_X_SHIFT;
  6928. if (y < 0) {
  6929. if (y + intel_crtc->cursor_height <= 0)
  6930. base = 0;
  6931. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6932. y = -y;
  6933. }
  6934. pos |= y << CURSOR_Y_SHIFT;
  6935. if (base == 0 && intel_crtc->cursor_base == 0)
  6936. return;
  6937. I915_WRITE(CURPOS(pipe), pos);
  6938. /* ILK+ do this automagically */
  6939. if (HAS_GMCH_DISPLAY(dev) &&
  6940. to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180)) {
  6941. base += (intel_crtc->cursor_height *
  6942. intel_crtc->cursor_width - 1) * 4;
  6943. }
  6944. if (IS_845G(dev) || IS_I865G(dev))
  6945. i845_update_cursor(crtc, base);
  6946. else
  6947. i9xx_update_cursor(crtc, base);
  6948. }
  6949. static bool cursor_size_ok(struct drm_device *dev,
  6950. uint32_t width, uint32_t height)
  6951. {
  6952. if (width == 0 || height == 0)
  6953. return false;
  6954. /*
  6955. * 845g/865g are special in that they are only limited by
  6956. * the width of their cursors, the height is arbitrary up to
  6957. * the precision of the register. Everything else requires
  6958. * square cursors, limited to a few power-of-two sizes.
  6959. */
  6960. if (IS_845G(dev) || IS_I865G(dev)) {
  6961. if ((width & 63) != 0)
  6962. return false;
  6963. if (width > (IS_845G(dev) ? 64 : 512))
  6964. return false;
  6965. if (height > 1023)
  6966. return false;
  6967. } else {
  6968. switch (width | height) {
  6969. case 256:
  6970. case 128:
  6971. if (IS_GEN2(dev))
  6972. return false;
  6973. case 64:
  6974. break;
  6975. default:
  6976. return false;
  6977. }
  6978. }
  6979. return true;
  6980. }
  6981. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6982. u16 *blue, uint32_t start, uint32_t size)
  6983. {
  6984. int end = (start + size > 256) ? 256 : start + size, i;
  6985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6986. for (i = start; i < end; i++) {
  6987. intel_crtc->lut_r[i] = red[i] >> 8;
  6988. intel_crtc->lut_g[i] = green[i] >> 8;
  6989. intel_crtc->lut_b[i] = blue[i] >> 8;
  6990. }
  6991. intel_crtc_load_lut(crtc);
  6992. }
  6993. /* VESA 640x480x72Hz mode to set on the pipe */
  6994. static struct drm_display_mode load_detect_mode = {
  6995. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6996. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6997. };
  6998. struct drm_framebuffer *
  6999. __intel_framebuffer_create(struct drm_device *dev,
  7000. struct drm_mode_fb_cmd2 *mode_cmd,
  7001. struct drm_i915_gem_object *obj)
  7002. {
  7003. struct intel_framebuffer *intel_fb;
  7004. int ret;
  7005. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7006. if (!intel_fb) {
  7007. drm_gem_object_unreference(&obj->base);
  7008. return ERR_PTR(-ENOMEM);
  7009. }
  7010. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7011. if (ret)
  7012. goto err;
  7013. return &intel_fb->base;
  7014. err:
  7015. drm_gem_object_unreference(&obj->base);
  7016. kfree(intel_fb);
  7017. return ERR_PTR(ret);
  7018. }
  7019. static struct drm_framebuffer *
  7020. intel_framebuffer_create(struct drm_device *dev,
  7021. struct drm_mode_fb_cmd2 *mode_cmd,
  7022. struct drm_i915_gem_object *obj)
  7023. {
  7024. struct drm_framebuffer *fb;
  7025. int ret;
  7026. ret = i915_mutex_lock_interruptible(dev);
  7027. if (ret)
  7028. return ERR_PTR(ret);
  7029. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7030. mutex_unlock(&dev->struct_mutex);
  7031. return fb;
  7032. }
  7033. static u32
  7034. intel_framebuffer_pitch_for_width(int width, int bpp)
  7035. {
  7036. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7037. return ALIGN(pitch, 64);
  7038. }
  7039. static u32
  7040. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7041. {
  7042. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7043. return PAGE_ALIGN(pitch * mode->vdisplay);
  7044. }
  7045. static struct drm_framebuffer *
  7046. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7047. struct drm_display_mode *mode,
  7048. int depth, int bpp)
  7049. {
  7050. struct drm_i915_gem_object *obj;
  7051. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7052. obj = i915_gem_alloc_object(dev,
  7053. intel_framebuffer_size_for_mode(mode, bpp));
  7054. if (obj == NULL)
  7055. return ERR_PTR(-ENOMEM);
  7056. mode_cmd.width = mode->hdisplay;
  7057. mode_cmd.height = mode->vdisplay;
  7058. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7059. bpp);
  7060. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7061. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7062. }
  7063. static struct drm_framebuffer *
  7064. mode_fits_in_fbdev(struct drm_device *dev,
  7065. struct drm_display_mode *mode)
  7066. {
  7067. #ifdef CONFIG_DRM_I915_FBDEV
  7068. struct drm_i915_private *dev_priv = dev->dev_private;
  7069. struct drm_i915_gem_object *obj;
  7070. struct drm_framebuffer *fb;
  7071. if (!dev_priv->fbdev)
  7072. return NULL;
  7073. if (!dev_priv->fbdev->fb)
  7074. return NULL;
  7075. obj = dev_priv->fbdev->fb->obj;
  7076. BUG_ON(!obj);
  7077. fb = &dev_priv->fbdev->fb->base;
  7078. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7079. fb->bits_per_pixel))
  7080. return NULL;
  7081. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7082. return NULL;
  7083. return fb;
  7084. #else
  7085. return NULL;
  7086. #endif
  7087. }
  7088. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7089. struct drm_display_mode *mode,
  7090. struct intel_load_detect_pipe *old,
  7091. struct drm_modeset_acquire_ctx *ctx)
  7092. {
  7093. struct intel_crtc *intel_crtc;
  7094. struct intel_encoder *intel_encoder =
  7095. intel_attached_encoder(connector);
  7096. struct drm_crtc *possible_crtc;
  7097. struct drm_encoder *encoder = &intel_encoder->base;
  7098. struct drm_crtc *crtc = NULL;
  7099. struct drm_device *dev = encoder->dev;
  7100. struct drm_framebuffer *fb;
  7101. struct drm_mode_config *config = &dev->mode_config;
  7102. int ret, i = -1;
  7103. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7104. connector->base.id, connector->name,
  7105. encoder->base.id, encoder->name);
  7106. retry:
  7107. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7108. if (ret)
  7109. goto fail_unlock;
  7110. /*
  7111. * Algorithm gets a little messy:
  7112. *
  7113. * - if the connector already has an assigned crtc, use it (but make
  7114. * sure it's on first)
  7115. *
  7116. * - try to find the first unused crtc that can drive this connector,
  7117. * and use that if we find one
  7118. */
  7119. /* See if we already have a CRTC for this connector */
  7120. if (encoder->crtc) {
  7121. crtc = encoder->crtc;
  7122. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7123. if (ret)
  7124. goto fail_unlock;
  7125. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7126. if (ret)
  7127. goto fail_unlock;
  7128. old->dpms_mode = connector->dpms;
  7129. old->load_detect_temp = false;
  7130. /* Make sure the crtc and connector are running */
  7131. if (connector->dpms != DRM_MODE_DPMS_ON)
  7132. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7133. return true;
  7134. }
  7135. /* Find an unused one (if possible) */
  7136. for_each_crtc(dev, possible_crtc) {
  7137. i++;
  7138. if (!(encoder->possible_crtcs & (1 << i)))
  7139. continue;
  7140. if (possible_crtc->enabled)
  7141. continue;
  7142. /* This can occur when applying the pipe A quirk on resume. */
  7143. if (to_intel_crtc(possible_crtc)->new_enabled)
  7144. continue;
  7145. crtc = possible_crtc;
  7146. break;
  7147. }
  7148. /*
  7149. * If we didn't find an unused CRTC, don't use any.
  7150. */
  7151. if (!crtc) {
  7152. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7153. goto fail_unlock;
  7154. }
  7155. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7156. if (ret)
  7157. goto fail_unlock;
  7158. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7159. if (ret)
  7160. goto fail_unlock;
  7161. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7162. to_intel_connector(connector)->new_encoder = intel_encoder;
  7163. intel_crtc = to_intel_crtc(crtc);
  7164. intel_crtc->new_enabled = true;
  7165. intel_crtc->new_config = &intel_crtc->config;
  7166. old->dpms_mode = connector->dpms;
  7167. old->load_detect_temp = true;
  7168. old->release_fb = NULL;
  7169. if (!mode)
  7170. mode = &load_detect_mode;
  7171. /* We need a framebuffer large enough to accommodate all accesses
  7172. * that the plane may generate whilst we perform load detection.
  7173. * We can not rely on the fbcon either being present (we get called
  7174. * during its initialisation to detect all boot displays, or it may
  7175. * not even exist) or that it is large enough to satisfy the
  7176. * requested mode.
  7177. */
  7178. fb = mode_fits_in_fbdev(dev, mode);
  7179. if (fb == NULL) {
  7180. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7181. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7182. old->release_fb = fb;
  7183. } else
  7184. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7185. if (IS_ERR(fb)) {
  7186. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7187. goto fail;
  7188. }
  7189. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7190. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7191. if (old->release_fb)
  7192. old->release_fb->funcs->destroy(old->release_fb);
  7193. goto fail;
  7194. }
  7195. /* let the connector get through one full cycle before testing */
  7196. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7197. return true;
  7198. fail:
  7199. intel_crtc->new_enabled = crtc->enabled;
  7200. if (intel_crtc->new_enabled)
  7201. intel_crtc->new_config = &intel_crtc->config;
  7202. else
  7203. intel_crtc->new_config = NULL;
  7204. fail_unlock:
  7205. if (ret == -EDEADLK) {
  7206. drm_modeset_backoff(ctx);
  7207. goto retry;
  7208. }
  7209. return false;
  7210. }
  7211. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7212. struct intel_load_detect_pipe *old)
  7213. {
  7214. struct intel_encoder *intel_encoder =
  7215. intel_attached_encoder(connector);
  7216. struct drm_encoder *encoder = &intel_encoder->base;
  7217. struct drm_crtc *crtc = encoder->crtc;
  7218. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7219. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7220. connector->base.id, connector->name,
  7221. encoder->base.id, encoder->name);
  7222. if (old->load_detect_temp) {
  7223. to_intel_connector(connector)->new_encoder = NULL;
  7224. intel_encoder->new_crtc = NULL;
  7225. intel_crtc->new_enabled = false;
  7226. intel_crtc->new_config = NULL;
  7227. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7228. if (old->release_fb) {
  7229. drm_framebuffer_unregister_private(old->release_fb);
  7230. drm_framebuffer_unreference(old->release_fb);
  7231. }
  7232. return;
  7233. }
  7234. /* Switch crtc and encoder back off if necessary */
  7235. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7236. connector->funcs->dpms(connector, old->dpms_mode);
  7237. }
  7238. static int i9xx_pll_refclk(struct drm_device *dev,
  7239. const struct intel_crtc_config *pipe_config)
  7240. {
  7241. struct drm_i915_private *dev_priv = dev->dev_private;
  7242. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7243. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7244. return dev_priv->vbt.lvds_ssc_freq;
  7245. else if (HAS_PCH_SPLIT(dev))
  7246. return 120000;
  7247. else if (!IS_GEN2(dev))
  7248. return 96000;
  7249. else
  7250. return 48000;
  7251. }
  7252. /* Returns the clock of the currently programmed mode of the given pipe. */
  7253. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7254. struct intel_crtc_config *pipe_config)
  7255. {
  7256. struct drm_device *dev = crtc->base.dev;
  7257. struct drm_i915_private *dev_priv = dev->dev_private;
  7258. int pipe = pipe_config->cpu_transcoder;
  7259. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7260. u32 fp;
  7261. intel_clock_t clock;
  7262. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7263. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7264. fp = pipe_config->dpll_hw_state.fp0;
  7265. else
  7266. fp = pipe_config->dpll_hw_state.fp1;
  7267. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7268. if (IS_PINEVIEW(dev)) {
  7269. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7270. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7271. } else {
  7272. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7273. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7274. }
  7275. if (!IS_GEN2(dev)) {
  7276. if (IS_PINEVIEW(dev))
  7277. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7278. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7279. else
  7280. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7281. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7282. switch (dpll & DPLL_MODE_MASK) {
  7283. case DPLLB_MODE_DAC_SERIAL:
  7284. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7285. 5 : 10;
  7286. break;
  7287. case DPLLB_MODE_LVDS:
  7288. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7289. 7 : 14;
  7290. break;
  7291. default:
  7292. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7293. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7294. return;
  7295. }
  7296. if (IS_PINEVIEW(dev))
  7297. pineview_clock(refclk, &clock);
  7298. else
  7299. i9xx_clock(refclk, &clock);
  7300. } else {
  7301. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7302. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7303. if (is_lvds) {
  7304. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7305. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7306. if (lvds & LVDS_CLKB_POWER_UP)
  7307. clock.p2 = 7;
  7308. else
  7309. clock.p2 = 14;
  7310. } else {
  7311. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7312. clock.p1 = 2;
  7313. else {
  7314. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7315. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7316. }
  7317. if (dpll & PLL_P2_DIVIDE_BY_4)
  7318. clock.p2 = 4;
  7319. else
  7320. clock.p2 = 2;
  7321. }
  7322. i9xx_clock(refclk, &clock);
  7323. }
  7324. /*
  7325. * This value includes pixel_multiplier. We will use
  7326. * port_clock to compute adjusted_mode.crtc_clock in the
  7327. * encoder's get_config() function.
  7328. */
  7329. pipe_config->port_clock = clock.dot;
  7330. }
  7331. int intel_dotclock_calculate(int link_freq,
  7332. const struct intel_link_m_n *m_n)
  7333. {
  7334. /*
  7335. * The calculation for the data clock is:
  7336. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7337. * But we want to avoid losing precison if possible, so:
  7338. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7339. *
  7340. * and the link clock is simpler:
  7341. * link_clock = (m * link_clock) / n
  7342. */
  7343. if (!m_n->link_n)
  7344. return 0;
  7345. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7346. }
  7347. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7348. struct intel_crtc_config *pipe_config)
  7349. {
  7350. struct drm_device *dev = crtc->base.dev;
  7351. /* read out port_clock from the DPLL */
  7352. i9xx_crtc_clock_get(crtc, pipe_config);
  7353. /*
  7354. * This value does not include pixel_multiplier.
  7355. * We will check that port_clock and adjusted_mode.crtc_clock
  7356. * agree once we know their relationship in the encoder's
  7357. * get_config() function.
  7358. */
  7359. pipe_config->adjusted_mode.crtc_clock =
  7360. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7361. &pipe_config->fdi_m_n);
  7362. }
  7363. /** Returns the currently programmed mode of the given pipe. */
  7364. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7365. struct drm_crtc *crtc)
  7366. {
  7367. struct drm_i915_private *dev_priv = dev->dev_private;
  7368. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7369. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7370. struct drm_display_mode *mode;
  7371. struct intel_crtc_config pipe_config;
  7372. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7373. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7374. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7375. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7376. enum pipe pipe = intel_crtc->pipe;
  7377. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7378. if (!mode)
  7379. return NULL;
  7380. /*
  7381. * Construct a pipe_config sufficient for getting the clock info
  7382. * back out of crtc_clock_get.
  7383. *
  7384. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7385. * to use a real value here instead.
  7386. */
  7387. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7388. pipe_config.pixel_multiplier = 1;
  7389. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7390. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7391. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7392. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7393. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7394. mode->hdisplay = (htot & 0xffff) + 1;
  7395. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7396. mode->hsync_start = (hsync & 0xffff) + 1;
  7397. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7398. mode->vdisplay = (vtot & 0xffff) + 1;
  7399. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7400. mode->vsync_start = (vsync & 0xffff) + 1;
  7401. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7402. drm_mode_set_name(mode);
  7403. return mode;
  7404. }
  7405. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7406. {
  7407. struct drm_device *dev = crtc->dev;
  7408. struct drm_i915_private *dev_priv = dev->dev_private;
  7409. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7410. if (!HAS_GMCH_DISPLAY(dev))
  7411. return;
  7412. if (!dev_priv->lvds_downclock_avail)
  7413. return;
  7414. /*
  7415. * Since this is called by a timer, we should never get here in
  7416. * the manual case.
  7417. */
  7418. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7419. int pipe = intel_crtc->pipe;
  7420. int dpll_reg = DPLL(pipe);
  7421. int dpll;
  7422. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7423. assert_panel_unlocked(dev_priv, pipe);
  7424. dpll = I915_READ(dpll_reg);
  7425. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7426. I915_WRITE(dpll_reg, dpll);
  7427. intel_wait_for_vblank(dev, pipe);
  7428. dpll = I915_READ(dpll_reg);
  7429. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7430. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7431. }
  7432. }
  7433. void intel_mark_busy(struct drm_device *dev)
  7434. {
  7435. struct drm_i915_private *dev_priv = dev->dev_private;
  7436. if (dev_priv->mm.busy)
  7437. return;
  7438. intel_runtime_pm_get(dev_priv);
  7439. i915_update_gfx_val(dev_priv);
  7440. dev_priv->mm.busy = true;
  7441. }
  7442. void intel_mark_idle(struct drm_device *dev)
  7443. {
  7444. struct drm_i915_private *dev_priv = dev->dev_private;
  7445. struct drm_crtc *crtc;
  7446. if (!dev_priv->mm.busy)
  7447. return;
  7448. dev_priv->mm.busy = false;
  7449. if (!i915.powersave)
  7450. goto out;
  7451. for_each_crtc(dev, crtc) {
  7452. if (!crtc->primary->fb)
  7453. continue;
  7454. intel_decrease_pllclock(crtc);
  7455. }
  7456. if (INTEL_INFO(dev)->gen >= 6)
  7457. gen6_rps_idle(dev->dev_private);
  7458. out:
  7459. intel_runtime_pm_put(dev_priv);
  7460. }
  7461. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7462. {
  7463. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7464. struct drm_device *dev = crtc->dev;
  7465. struct intel_unpin_work *work;
  7466. spin_lock_irq(&dev->event_lock);
  7467. work = intel_crtc->unpin_work;
  7468. intel_crtc->unpin_work = NULL;
  7469. spin_unlock_irq(&dev->event_lock);
  7470. if (work) {
  7471. cancel_work_sync(&work->work);
  7472. kfree(work);
  7473. }
  7474. drm_crtc_cleanup(crtc);
  7475. kfree(intel_crtc);
  7476. }
  7477. static void intel_unpin_work_fn(struct work_struct *__work)
  7478. {
  7479. struct intel_unpin_work *work =
  7480. container_of(__work, struct intel_unpin_work, work);
  7481. struct drm_device *dev = work->crtc->dev;
  7482. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7483. mutex_lock(&dev->struct_mutex);
  7484. intel_unpin_fb_obj(work->old_fb_obj);
  7485. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7486. drm_gem_object_unreference(&work->old_fb_obj->base);
  7487. intel_fbc_update(dev);
  7488. if (work->flip_queued_req)
  7489. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7490. mutex_unlock(&dev->struct_mutex);
  7491. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7492. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7493. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7494. kfree(work);
  7495. }
  7496. static void do_intel_finish_page_flip(struct drm_device *dev,
  7497. struct drm_crtc *crtc)
  7498. {
  7499. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7500. struct intel_unpin_work *work;
  7501. unsigned long flags;
  7502. /* Ignore early vblank irqs */
  7503. if (intel_crtc == NULL)
  7504. return;
  7505. /*
  7506. * This is called both by irq handlers and the reset code (to complete
  7507. * lost pageflips) so needs the full irqsave spinlocks.
  7508. */
  7509. spin_lock_irqsave(&dev->event_lock, flags);
  7510. work = intel_crtc->unpin_work;
  7511. /* Ensure we don't miss a work->pending update ... */
  7512. smp_rmb();
  7513. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7514. spin_unlock_irqrestore(&dev->event_lock, flags);
  7515. return;
  7516. }
  7517. page_flip_completed(intel_crtc);
  7518. spin_unlock_irqrestore(&dev->event_lock, flags);
  7519. }
  7520. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7521. {
  7522. struct drm_i915_private *dev_priv = dev->dev_private;
  7523. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7524. do_intel_finish_page_flip(dev, crtc);
  7525. }
  7526. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7527. {
  7528. struct drm_i915_private *dev_priv = dev->dev_private;
  7529. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7530. do_intel_finish_page_flip(dev, crtc);
  7531. }
  7532. /* Is 'a' after or equal to 'b'? */
  7533. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7534. {
  7535. return !((a - b) & 0x80000000);
  7536. }
  7537. static bool page_flip_finished(struct intel_crtc *crtc)
  7538. {
  7539. struct drm_device *dev = crtc->base.dev;
  7540. struct drm_i915_private *dev_priv = dev->dev_private;
  7541. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7542. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7543. return true;
  7544. /*
  7545. * The relevant registers doen't exist on pre-ctg.
  7546. * As the flip done interrupt doesn't trigger for mmio
  7547. * flips on gmch platforms, a flip count check isn't
  7548. * really needed there. But since ctg has the registers,
  7549. * include it in the check anyway.
  7550. */
  7551. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7552. return true;
  7553. /*
  7554. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7555. * used the same base address. In that case the mmio flip might
  7556. * have completed, but the CS hasn't even executed the flip yet.
  7557. *
  7558. * A flip count check isn't enough as the CS might have updated
  7559. * the base address just after start of vblank, but before we
  7560. * managed to process the interrupt. This means we'd complete the
  7561. * CS flip too soon.
  7562. *
  7563. * Combining both checks should get us a good enough result. It may
  7564. * still happen that the CS flip has been executed, but has not
  7565. * yet actually completed. But in case the base address is the same
  7566. * anyway, we don't really care.
  7567. */
  7568. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7569. crtc->unpin_work->gtt_offset &&
  7570. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7571. crtc->unpin_work->flip_count);
  7572. }
  7573. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7574. {
  7575. struct drm_i915_private *dev_priv = dev->dev_private;
  7576. struct intel_crtc *intel_crtc =
  7577. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7578. unsigned long flags;
  7579. /*
  7580. * This is called both by irq handlers and the reset code (to complete
  7581. * lost pageflips) so needs the full irqsave spinlocks.
  7582. *
  7583. * NB: An MMIO update of the plane base pointer will also
  7584. * generate a page-flip completion irq, i.e. every modeset
  7585. * is also accompanied by a spurious intel_prepare_page_flip().
  7586. */
  7587. spin_lock_irqsave(&dev->event_lock, flags);
  7588. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7589. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7590. spin_unlock_irqrestore(&dev->event_lock, flags);
  7591. }
  7592. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7593. {
  7594. /* Ensure that the work item is consistent when activating it ... */
  7595. smp_wmb();
  7596. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7597. /* and that it is marked active as soon as the irq could fire. */
  7598. smp_wmb();
  7599. }
  7600. static int intel_gen2_queue_flip(struct drm_device *dev,
  7601. struct drm_crtc *crtc,
  7602. struct drm_framebuffer *fb,
  7603. struct drm_i915_gem_object *obj,
  7604. struct intel_engine_cs *ring,
  7605. uint32_t flags)
  7606. {
  7607. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7608. u32 flip_mask;
  7609. int ret;
  7610. ret = intel_ring_begin(ring, 6);
  7611. if (ret)
  7612. return ret;
  7613. /* Can't queue multiple flips, so wait for the previous
  7614. * one to finish before executing the next.
  7615. */
  7616. if (intel_crtc->plane)
  7617. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7618. else
  7619. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7620. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7621. intel_ring_emit(ring, MI_NOOP);
  7622. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7623. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7624. intel_ring_emit(ring, fb->pitches[0]);
  7625. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7626. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7627. intel_mark_page_flip_active(intel_crtc);
  7628. __intel_ring_advance(ring);
  7629. return 0;
  7630. }
  7631. static int intel_gen3_queue_flip(struct drm_device *dev,
  7632. struct drm_crtc *crtc,
  7633. struct drm_framebuffer *fb,
  7634. struct drm_i915_gem_object *obj,
  7635. struct intel_engine_cs *ring,
  7636. uint32_t flags)
  7637. {
  7638. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7639. u32 flip_mask;
  7640. int ret;
  7641. ret = intel_ring_begin(ring, 6);
  7642. if (ret)
  7643. return ret;
  7644. if (intel_crtc->plane)
  7645. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7646. else
  7647. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7648. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7649. intel_ring_emit(ring, MI_NOOP);
  7650. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7651. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7652. intel_ring_emit(ring, fb->pitches[0]);
  7653. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7654. intel_ring_emit(ring, MI_NOOP);
  7655. intel_mark_page_flip_active(intel_crtc);
  7656. __intel_ring_advance(ring);
  7657. return 0;
  7658. }
  7659. static int intel_gen4_queue_flip(struct drm_device *dev,
  7660. struct drm_crtc *crtc,
  7661. struct drm_framebuffer *fb,
  7662. struct drm_i915_gem_object *obj,
  7663. struct intel_engine_cs *ring,
  7664. uint32_t flags)
  7665. {
  7666. struct drm_i915_private *dev_priv = dev->dev_private;
  7667. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7668. uint32_t pf, pipesrc;
  7669. int ret;
  7670. ret = intel_ring_begin(ring, 4);
  7671. if (ret)
  7672. return ret;
  7673. /* i965+ uses the linear or tiled offsets from the
  7674. * Display Registers (which do not change across a page-flip)
  7675. * so we need only reprogram the base address.
  7676. */
  7677. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7678. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7679. intel_ring_emit(ring, fb->pitches[0]);
  7680. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7681. obj->tiling_mode);
  7682. /* XXX Enabling the panel-fitter across page-flip is so far
  7683. * untested on non-native modes, so ignore it for now.
  7684. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7685. */
  7686. pf = 0;
  7687. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7688. intel_ring_emit(ring, pf | pipesrc);
  7689. intel_mark_page_flip_active(intel_crtc);
  7690. __intel_ring_advance(ring);
  7691. return 0;
  7692. }
  7693. static int intel_gen6_queue_flip(struct drm_device *dev,
  7694. struct drm_crtc *crtc,
  7695. struct drm_framebuffer *fb,
  7696. struct drm_i915_gem_object *obj,
  7697. struct intel_engine_cs *ring,
  7698. uint32_t flags)
  7699. {
  7700. struct drm_i915_private *dev_priv = dev->dev_private;
  7701. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7702. uint32_t pf, pipesrc;
  7703. int ret;
  7704. ret = intel_ring_begin(ring, 4);
  7705. if (ret)
  7706. return ret;
  7707. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7708. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7709. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7710. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7711. /* Contrary to the suggestions in the documentation,
  7712. * "Enable Panel Fitter" does not seem to be required when page
  7713. * flipping with a non-native mode, and worse causes a normal
  7714. * modeset to fail.
  7715. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7716. */
  7717. pf = 0;
  7718. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7719. intel_ring_emit(ring, pf | pipesrc);
  7720. intel_mark_page_flip_active(intel_crtc);
  7721. __intel_ring_advance(ring);
  7722. return 0;
  7723. }
  7724. static int intel_gen7_queue_flip(struct drm_device *dev,
  7725. struct drm_crtc *crtc,
  7726. struct drm_framebuffer *fb,
  7727. struct drm_i915_gem_object *obj,
  7728. struct intel_engine_cs *ring,
  7729. uint32_t flags)
  7730. {
  7731. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7732. uint32_t plane_bit = 0;
  7733. int len, ret;
  7734. switch (intel_crtc->plane) {
  7735. case PLANE_A:
  7736. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7737. break;
  7738. case PLANE_B:
  7739. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7740. break;
  7741. case PLANE_C:
  7742. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7743. break;
  7744. default:
  7745. WARN_ONCE(1, "unknown plane in flip command\n");
  7746. return -ENODEV;
  7747. }
  7748. len = 4;
  7749. if (ring->id == RCS) {
  7750. len += 6;
  7751. /*
  7752. * On Gen 8, SRM is now taking an extra dword to accommodate
  7753. * 48bits addresses, and we need a NOOP for the batch size to
  7754. * stay even.
  7755. */
  7756. if (IS_GEN8(dev))
  7757. len += 2;
  7758. }
  7759. /*
  7760. * BSpec MI_DISPLAY_FLIP for IVB:
  7761. * "The full packet must be contained within the same cache line."
  7762. *
  7763. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7764. * cacheline, if we ever start emitting more commands before
  7765. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7766. * then do the cacheline alignment, and finally emit the
  7767. * MI_DISPLAY_FLIP.
  7768. */
  7769. ret = intel_ring_cacheline_align(ring);
  7770. if (ret)
  7771. return ret;
  7772. ret = intel_ring_begin(ring, len);
  7773. if (ret)
  7774. return ret;
  7775. /* Unmask the flip-done completion message. Note that the bspec says that
  7776. * we should do this for both the BCS and RCS, and that we must not unmask
  7777. * more than one flip event at any time (or ensure that one flip message
  7778. * can be sent by waiting for flip-done prior to queueing new flips).
  7779. * Experimentation says that BCS works despite DERRMR masking all
  7780. * flip-done completion events and that unmasking all planes at once
  7781. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7782. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7783. */
  7784. if (ring->id == RCS) {
  7785. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7786. intel_ring_emit(ring, DERRMR);
  7787. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7788. DERRMR_PIPEB_PRI_FLIP_DONE |
  7789. DERRMR_PIPEC_PRI_FLIP_DONE));
  7790. if (IS_GEN8(dev))
  7791. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7792. MI_SRM_LRM_GLOBAL_GTT);
  7793. else
  7794. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7795. MI_SRM_LRM_GLOBAL_GTT);
  7796. intel_ring_emit(ring, DERRMR);
  7797. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7798. if (IS_GEN8(dev)) {
  7799. intel_ring_emit(ring, 0);
  7800. intel_ring_emit(ring, MI_NOOP);
  7801. }
  7802. }
  7803. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7804. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7805. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7806. intel_ring_emit(ring, (MI_NOOP));
  7807. intel_mark_page_flip_active(intel_crtc);
  7808. __intel_ring_advance(ring);
  7809. return 0;
  7810. }
  7811. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7812. struct drm_i915_gem_object *obj)
  7813. {
  7814. /*
  7815. * This is not being used for older platforms, because
  7816. * non-availability of flip done interrupt forces us to use
  7817. * CS flips. Older platforms derive flip done using some clever
  7818. * tricks involving the flip_pending status bits and vblank irqs.
  7819. * So using MMIO flips there would disrupt this mechanism.
  7820. */
  7821. if (ring == NULL)
  7822. return true;
  7823. if (INTEL_INFO(ring->dev)->gen < 5)
  7824. return false;
  7825. if (i915.use_mmio_flip < 0)
  7826. return false;
  7827. else if (i915.use_mmio_flip > 0)
  7828. return true;
  7829. else if (i915.enable_execlists)
  7830. return true;
  7831. else
  7832. return ring != i915_gem_request_get_ring(obj->last_read_req);
  7833. }
  7834. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  7835. {
  7836. struct drm_device *dev = intel_crtc->base.dev;
  7837. struct drm_i915_private *dev_priv = dev->dev_private;
  7838. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  7839. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7840. struct drm_i915_gem_object *obj = intel_fb->obj;
  7841. const enum pipe pipe = intel_crtc->pipe;
  7842. u32 ctl, stride;
  7843. ctl = I915_READ(PLANE_CTL(pipe, 0));
  7844. ctl &= ~PLANE_CTL_TILED_MASK;
  7845. if (obj->tiling_mode == I915_TILING_X)
  7846. ctl |= PLANE_CTL_TILED_X;
  7847. /*
  7848. * The stride is either expressed as a multiple of 64 bytes chunks for
  7849. * linear buffers or in number of tiles for tiled buffers.
  7850. */
  7851. stride = fb->pitches[0] >> 6;
  7852. if (obj->tiling_mode == I915_TILING_X)
  7853. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  7854. /*
  7855. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  7856. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  7857. */
  7858. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  7859. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  7860. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  7861. POSTING_READ(PLANE_SURF(pipe, 0));
  7862. }
  7863. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  7864. {
  7865. struct drm_device *dev = intel_crtc->base.dev;
  7866. struct drm_i915_private *dev_priv = dev->dev_private;
  7867. struct intel_framebuffer *intel_fb =
  7868. to_intel_framebuffer(intel_crtc->base.primary->fb);
  7869. struct drm_i915_gem_object *obj = intel_fb->obj;
  7870. u32 dspcntr;
  7871. u32 reg;
  7872. reg = DSPCNTR(intel_crtc->plane);
  7873. dspcntr = I915_READ(reg);
  7874. if (obj->tiling_mode != I915_TILING_NONE)
  7875. dspcntr |= DISPPLANE_TILED;
  7876. else
  7877. dspcntr &= ~DISPPLANE_TILED;
  7878. I915_WRITE(reg, dspcntr);
  7879. I915_WRITE(DSPSURF(intel_crtc->plane),
  7880. intel_crtc->unpin_work->gtt_offset);
  7881. POSTING_READ(DSPSURF(intel_crtc->plane));
  7882. }
  7883. /*
  7884. * XXX: This is the temporary way to update the plane registers until we get
  7885. * around to using the usual plane update functions for MMIO flips
  7886. */
  7887. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  7888. {
  7889. struct drm_device *dev = intel_crtc->base.dev;
  7890. bool atomic_update;
  7891. u32 start_vbl_count;
  7892. intel_mark_page_flip_active(intel_crtc);
  7893. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  7894. if (INTEL_INFO(dev)->gen >= 9)
  7895. skl_do_mmio_flip(intel_crtc);
  7896. else
  7897. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  7898. ilk_do_mmio_flip(intel_crtc);
  7899. if (atomic_update)
  7900. intel_pipe_update_end(intel_crtc, start_vbl_count);
  7901. }
  7902. static void intel_mmio_flip_work_func(struct work_struct *work)
  7903. {
  7904. struct intel_crtc *crtc =
  7905. container_of(work, struct intel_crtc, mmio_flip.work);
  7906. struct intel_mmio_flip *mmio_flip;
  7907. mmio_flip = &crtc->mmio_flip;
  7908. if (mmio_flip->req)
  7909. WARN_ON(__i915_wait_request(mmio_flip->req,
  7910. crtc->reset_counter,
  7911. false, NULL, NULL) != 0);
  7912. intel_do_mmio_flip(crtc);
  7913. if (mmio_flip->req) {
  7914. mutex_lock(&crtc->base.dev->struct_mutex);
  7915. i915_gem_request_assign(&mmio_flip->req, NULL);
  7916. mutex_unlock(&crtc->base.dev->struct_mutex);
  7917. }
  7918. }
  7919. static int intel_queue_mmio_flip(struct drm_device *dev,
  7920. struct drm_crtc *crtc,
  7921. struct drm_framebuffer *fb,
  7922. struct drm_i915_gem_object *obj,
  7923. struct intel_engine_cs *ring,
  7924. uint32_t flags)
  7925. {
  7926. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7927. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  7928. obj->last_write_req);
  7929. schedule_work(&intel_crtc->mmio_flip.work);
  7930. return 0;
  7931. }
  7932. static int intel_gen9_queue_flip(struct drm_device *dev,
  7933. struct drm_crtc *crtc,
  7934. struct drm_framebuffer *fb,
  7935. struct drm_i915_gem_object *obj,
  7936. struct intel_engine_cs *ring,
  7937. uint32_t flags)
  7938. {
  7939. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7940. uint32_t plane = 0, stride;
  7941. int ret;
  7942. switch(intel_crtc->pipe) {
  7943. case PIPE_A:
  7944. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
  7945. break;
  7946. case PIPE_B:
  7947. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
  7948. break;
  7949. case PIPE_C:
  7950. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
  7951. break;
  7952. default:
  7953. WARN_ONCE(1, "unknown plane in flip command\n");
  7954. return -ENODEV;
  7955. }
  7956. switch (obj->tiling_mode) {
  7957. case I915_TILING_NONE:
  7958. stride = fb->pitches[0] >> 6;
  7959. break;
  7960. case I915_TILING_X:
  7961. stride = fb->pitches[0] >> 9;
  7962. break;
  7963. default:
  7964. WARN_ONCE(1, "unknown tiling in flip command\n");
  7965. return -ENODEV;
  7966. }
  7967. ret = intel_ring_begin(ring, 10);
  7968. if (ret)
  7969. return ret;
  7970. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7971. intel_ring_emit(ring, DERRMR);
  7972. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7973. DERRMR_PIPEB_PRI_FLIP_DONE |
  7974. DERRMR_PIPEC_PRI_FLIP_DONE));
  7975. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7976. MI_SRM_LRM_GLOBAL_GTT);
  7977. intel_ring_emit(ring, DERRMR);
  7978. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7979. intel_ring_emit(ring, 0);
  7980. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
  7981. intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
  7982. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7983. intel_mark_page_flip_active(intel_crtc);
  7984. __intel_ring_advance(ring);
  7985. return 0;
  7986. }
  7987. static int intel_default_queue_flip(struct drm_device *dev,
  7988. struct drm_crtc *crtc,
  7989. struct drm_framebuffer *fb,
  7990. struct drm_i915_gem_object *obj,
  7991. struct intel_engine_cs *ring,
  7992. uint32_t flags)
  7993. {
  7994. return -ENODEV;
  7995. }
  7996. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  7997. struct drm_crtc *crtc)
  7998. {
  7999. struct drm_i915_private *dev_priv = dev->dev_private;
  8000. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8001. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8002. u32 addr;
  8003. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8004. return true;
  8005. if (!work->enable_stall_check)
  8006. return false;
  8007. if (work->flip_ready_vblank == 0) {
  8008. if (work->flip_queued_req &&
  8009. !i915_gem_request_completed(work->flip_queued_req, true))
  8010. return false;
  8011. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8012. }
  8013. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  8014. return false;
  8015. /* Potential stall - if we see that the flip has happened,
  8016. * assume a missed interrupt. */
  8017. if (INTEL_INFO(dev)->gen >= 4)
  8018. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8019. else
  8020. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8021. /* There is a potential issue here with a false positive after a flip
  8022. * to the same address. We could address this by checking for a
  8023. * non-incrementing frame counter.
  8024. */
  8025. return addr == work->gtt_offset;
  8026. }
  8027. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8028. {
  8029. struct drm_i915_private *dev_priv = dev->dev_private;
  8030. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8031. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8032. WARN_ON(!in_irq());
  8033. if (crtc == NULL)
  8034. return;
  8035. spin_lock(&dev->event_lock);
  8036. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8037. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8038. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  8039. page_flip_completed(intel_crtc);
  8040. }
  8041. spin_unlock(&dev->event_lock);
  8042. }
  8043. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8044. struct drm_framebuffer *fb,
  8045. struct drm_pending_vblank_event *event,
  8046. uint32_t page_flip_flags)
  8047. {
  8048. struct drm_device *dev = crtc->dev;
  8049. struct drm_i915_private *dev_priv = dev->dev_private;
  8050. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8051. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8052. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8053. struct drm_plane *primary = crtc->primary;
  8054. struct intel_plane *intel_plane = to_intel_plane(primary);
  8055. enum pipe pipe = intel_crtc->pipe;
  8056. struct intel_unpin_work *work;
  8057. struct intel_engine_cs *ring;
  8058. int ret;
  8059. /*
  8060. * drm_mode_page_flip_ioctl() should already catch this, but double
  8061. * check to be safe. In the future we may enable pageflipping from
  8062. * a disabled primary plane.
  8063. */
  8064. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8065. return -EBUSY;
  8066. /* Can't change pixel format via MI display flips. */
  8067. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8068. return -EINVAL;
  8069. /*
  8070. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8071. * Note that pitch changes could also affect these register.
  8072. */
  8073. if (INTEL_INFO(dev)->gen > 3 &&
  8074. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8075. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8076. return -EINVAL;
  8077. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8078. goto out_hang;
  8079. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8080. if (work == NULL)
  8081. return -ENOMEM;
  8082. work->event = event;
  8083. work->crtc = crtc;
  8084. work->old_fb_obj = intel_fb_obj(old_fb);
  8085. INIT_WORK(&work->work, intel_unpin_work_fn);
  8086. ret = drm_crtc_vblank_get(crtc);
  8087. if (ret)
  8088. goto free_work;
  8089. /* We borrow the event spin lock for protecting unpin_work */
  8090. spin_lock_irq(&dev->event_lock);
  8091. if (intel_crtc->unpin_work) {
  8092. /* Before declaring the flip queue wedged, check if
  8093. * the hardware completed the operation behind our backs.
  8094. */
  8095. if (__intel_pageflip_stall_check(dev, crtc)) {
  8096. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8097. page_flip_completed(intel_crtc);
  8098. } else {
  8099. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8100. spin_unlock_irq(&dev->event_lock);
  8101. drm_crtc_vblank_put(crtc);
  8102. kfree(work);
  8103. return -EBUSY;
  8104. }
  8105. }
  8106. intel_crtc->unpin_work = work;
  8107. spin_unlock_irq(&dev->event_lock);
  8108. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8109. flush_workqueue(dev_priv->wq);
  8110. ret = i915_mutex_lock_interruptible(dev);
  8111. if (ret)
  8112. goto cleanup;
  8113. /* Reference the objects for the scheduled work. */
  8114. drm_gem_object_reference(&work->old_fb_obj->base);
  8115. drm_gem_object_reference(&obj->base);
  8116. crtc->primary->fb = fb;
  8117. work->pending_flip_obj = obj;
  8118. atomic_inc(&intel_crtc->unpin_work_count);
  8119. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8120. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8121. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8122. if (IS_VALLEYVIEW(dev)) {
  8123. ring = &dev_priv->ring[BCS];
  8124. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8125. /* vlv: DISPLAY_FLIP fails to change tiling */
  8126. ring = NULL;
  8127. } else if (IS_IVYBRIDGE(dev)) {
  8128. ring = &dev_priv->ring[BCS];
  8129. } else if (INTEL_INFO(dev)->gen >= 7) {
  8130. ring = i915_gem_request_get_ring(obj->last_read_req);
  8131. if (ring == NULL || ring->id != RCS)
  8132. ring = &dev_priv->ring[BCS];
  8133. } else {
  8134. ring = &dev_priv->ring[RCS];
  8135. }
  8136. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
  8137. if (ret)
  8138. goto cleanup_pending;
  8139. work->gtt_offset =
  8140. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8141. if (use_mmio_flip(ring, obj)) {
  8142. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8143. page_flip_flags);
  8144. if (ret)
  8145. goto cleanup_unpin;
  8146. i915_gem_request_assign(&work->flip_queued_req,
  8147. obj->last_write_req);
  8148. } else {
  8149. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8150. page_flip_flags);
  8151. if (ret)
  8152. goto cleanup_unpin;
  8153. i915_gem_request_assign(&work->flip_queued_req,
  8154. intel_ring_get_request(ring));
  8155. }
  8156. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8157. work->enable_stall_check = true;
  8158. i915_gem_track_fb(work->old_fb_obj, obj,
  8159. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8160. intel_fbc_disable(dev);
  8161. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8162. mutex_unlock(&dev->struct_mutex);
  8163. trace_i915_flip_request(intel_crtc->plane, obj);
  8164. return 0;
  8165. cleanup_unpin:
  8166. intel_unpin_fb_obj(obj);
  8167. cleanup_pending:
  8168. atomic_dec(&intel_crtc->unpin_work_count);
  8169. crtc->primary->fb = old_fb;
  8170. drm_gem_object_unreference(&work->old_fb_obj->base);
  8171. drm_gem_object_unreference(&obj->base);
  8172. mutex_unlock(&dev->struct_mutex);
  8173. cleanup:
  8174. spin_lock_irq(&dev->event_lock);
  8175. intel_crtc->unpin_work = NULL;
  8176. spin_unlock_irq(&dev->event_lock);
  8177. drm_crtc_vblank_put(crtc);
  8178. free_work:
  8179. kfree(work);
  8180. if (ret == -EIO) {
  8181. out_hang:
  8182. ret = primary->funcs->update_plane(primary, crtc, fb,
  8183. intel_plane->crtc_x,
  8184. intel_plane->crtc_y,
  8185. intel_plane->crtc_h,
  8186. intel_plane->crtc_w,
  8187. intel_plane->src_x,
  8188. intel_plane->src_y,
  8189. intel_plane->src_h,
  8190. intel_plane->src_w);
  8191. if (ret == 0 && event) {
  8192. spin_lock_irq(&dev->event_lock);
  8193. drm_send_vblank_event(dev, pipe, event);
  8194. spin_unlock_irq(&dev->event_lock);
  8195. }
  8196. }
  8197. return ret;
  8198. }
  8199. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8200. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8201. .load_lut = intel_crtc_load_lut,
  8202. };
  8203. /**
  8204. * intel_modeset_update_staged_output_state
  8205. *
  8206. * Updates the staged output configuration state, e.g. after we've read out the
  8207. * current hw state.
  8208. */
  8209. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8210. {
  8211. struct intel_crtc *crtc;
  8212. struct intel_encoder *encoder;
  8213. struct intel_connector *connector;
  8214. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8215. base.head) {
  8216. connector->new_encoder =
  8217. to_intel_encoder(connector->base.encoder);
  8218. }
  8219. for_each_intel_encoder(dev, encoder) {
  8220. encoder->new_crtc =
  8221. to_intel_crtc(encoder->base.crtc);
  8222. }
  8223. for_each_intel_crtc(dev, crtc) {
  8224. crtc->new_enabled = crtc->base.enabled;
  8225. if (crtc->new_enabled)
  8226. crtc->new_config = &crtc->config;
  8227. else
  8228. crtc->new_config = NULL;
  8229. }
  8230. }
  8231. /**
  8232. * intel_modeset_commit_output_state
  8233. *
  8234. * This function copies the stage display pipe configuration to the real one.
  8235. */
  8236. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8237. {
  8238. struct intel_crtc *crtc;
  8239. struct intel_encoder *encoder;
  8240. struct intel_connector *connector;
  8241. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8242. base.head) {
  8243. connector->base.encoder = &connector->new_encoder->base;
  8244. }
  8245. for_each_intel_encoder(dev, encoder) {
  8246. encoder->base.crtc = &encoder->new_crtc->base;
  8247. }
  8248. for_each_intel_crtc(dev, crtc) {
  8249. crtc->base.enabled = crtc->new_enabled;
  8250. }
  8251. }
  8252. static void
  8253. connected_sink_compute_bpp(struct intel_connector *connector,
  8254. struct intel_crtc_config *pipe_config)
  8255. {
  8256. int bpp = pipe_config->pipe_bpp;
  8257. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8258. connector->base.base.id,
  8259. connector->base.name);
  8260. /* Don't use an invalid EDID bpc value */
  8261. if (connector->base.display_info.bpc &&
  8262. connector->base.display_info.bpc * 3 < bpp) {
  8263. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8264. bpp, connector->base.display_info.bpc*3);
  8265. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8266. }
  8267. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8268. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8269. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8270. bpp);
  8271. pipe_config->pipe_bpp = 24;
  8272. }
  8273. }
  8274. static int
  8275. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8276. struct drm_framebuffer *fb,
  8277. struct intel_crtc_config *pipe_config)
  8278. {
  8279. struct drm_device *dev = crtc->base.dev;
  8280. struct intel_connector *connector;
  8281. int bpp;
  8282. switch (fb->pixel_format) {
  8283. case DRM_FORMAT_C8:
  8284. bpp = 8*3; /* since we go through a colormap */
  8285. break;
  8286. case DRM_FORMAT_XRGB1555:
  8287. case DRM_FORMAT_ARGB1555:
  8288. /* checked in intel_framebuffer_init already */
  8289. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8290. return -EINVAL;
  8291. case DRM_FORMAT_RGB565:
  8292. bpp = 6*3; /* min is 18bpp */
  8293. break;
  8294. case DRM_FORMAT_XBGR8888:
  8295. case DRM_FORMAT_ABGR8888:
  8296. /* checked in intel_framebuffer_init already */
  8297. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8298. return -EINVAL;
  8299. case DRM_FORMAT_XRGB8888:
  8300. case DRM_FORMAT_ARGB8888:
  8301. bpp = 8*3;
  8302. break;
  8303. case DRM_FORMAT_XRGB2101010:
  8304. case DRM_FORMAT_ARGB2101010:
  8305. case DRM_FORMAT_XBGR2101010:
  8306. case DRM_FORMAT_ABGR2101010:
  8307. /* checked in intel_framebuffer_init already */
  8308. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8309. return -EINVAL;
  8310. bpp = 10*3;
  8311. break;
  8312. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8313. default:
  8314. DRM_DEBUG_KMS("unsupported depth\n");
  8315. return -EINVAL;
  8316. }
  8317. pipe_config->pipe_bpp = bpp;
  8318. /* Clamp display bpp to EDID value */
  8319. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8320. base.head) {
  8321. if (!connector->new_encoder ||
  8322. connector->new_encoder->new_crtc != crtc)
  8323. continue;
  8324. connected_sink_compute_bpp(connector, pipe_config);
  8325. }
  8326. return bpp;
  8327. }
  8328. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8329. {
  8330. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8331. "type: 0x%x flags: 0x%x\n",
  8332. mode->crtc_clock,
  8333. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8334. mode->crtc_hsync_end, mode->crtc_htotal,
  8335. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8336. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8337. }
  8338. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8339. struct intel_crtc_config *pipe_config,
  8340. const char *context)
  8341. {
  8342. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8343. context, pipe_name(crtc->pipe));
  8344. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8345. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8346. pipe_config->pipe_bpp, pipe_config->dither);
  8347. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8348. pipe_config->has_pch_encoder,
  8349. pipe_config->fdi_lanes,
  8350. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8351. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8352. pipe_config->fdi_m_n.tu);
  8353. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8354. pipe_config->has_dp_encoder,
  8355. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8356. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8357. pipe_config->dp_m_n.tu);
  8358. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8359. pipe_config->has_dp_encoder,
  8360. pipe_config->dp_m2_n2.gmch_m,
  8361. pipe_config->dp_m2_n2.gmch_n,
  8362. pipe_config->dp_m2_n2.link_m,
  8363. pipe_config->dp_m2_n2.link_n,
  8364. pipe_config->dp_m2_n2.tu);
  8365. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8366. pipe_config->has_audio,
  8367. pipe_config->has_infoframe);
  8368. DRM_DEBUG_KMS("requested mode:\n");
  8369. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8370. DRM_DEBUG_KMS("adjusted mode:\n");
  8371. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8372. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8373. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8374. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8375. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8376. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8377. pipe_config->gmch_pfit.control,
  8378. pipe_config->gmch_pfit.pgm_ratios,
  8379. pipe_config->gmch_pfit.lvds_border_bits);
  8380. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8381. pipe_config->pch_pfit.pos,
  8382. pipe_config->pch_pfit.size,
  8383. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8384. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8385. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8386. }
  8387. static bool encoders_cloneable(const struct intel_encoder *a,
  8388. const struct intel_encoder *b)
  8389. {
  8390. /* masks could be asymmetric, so check both ways */
  8391. return a == b || (a->cloneable & (1 << b->type) &&
  8392. b->cloneable & (1 << a->type));
  8393. }
  8394. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8395. struct intel_encoder *encoder)
  8396. {
  8397. struct drm_device *dev = crtc->base.dev;
  8398. struct intel_encoder *source_encoder;
  8399. for_each_intel_encoder(dev, source_encoder) {
  8400. if (source_encoder->new_crtc != crtc)
  8401. continue;
  8402. if (!encoders_cloneable(encoder, source_encoder))
  8403. return false;
  8404. }
  8405. return true;
  8406. }
  8407. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8408. {
  8409. struct drm_device *dev = crtc->base.dev;
  8410. struct intel_encoder *encoder;
  8411. for_each_intel_encoder(dev, encoder) {
  8412. if (encoder->new_crtc != crtc)
  8413. continue;
  8414. if (!check_single_encoder_cloning(crtc, encoder))
  8415. return false;
  8416. }
  8417. return true;
  8418. }
  8419. static bool check_digital_port_conflicts(struct drm_device *dev)
  8420. {
  8421. struct intel_connector *connector;
  8422. unsigned int used_ports = 0;
  8423. /*
  8424. * Walk the connector list instead of the encoder
  8425. * list to detect the problem on ddi platforms
  8426. * where there's just one encoder per digital port.
  8427. */
  8428. list_for_each_entry(connector,
  8429. &dev->mode_config.connector_list, base.head) {
  8430. struct intel_encoder *encoder = connector->new_encoder;
  8431. if (!encoder)
  8432. continue;
  8433. WARN_ON(!encoder->new_crtc);
  8434. switch (encoder->type) {
  8435. unsigned int port_mask;
  8436. case INTEL_OUTPUT_UNKNOWN:
  8437. if (WARN_ON(!HAS_DDI(dev)))
  8438. break;
  8439. case INTEL_OUTPUT_DISPLAYPORT:
  8440. case INTEL_OUTPUT_HDMI:
  8441. case INTEL_OUTPUT_EDP:
  8442. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8443. /* the same port mustn't appear more than once */
  8444. if (used_ports & port_mask)
  8445. return false;
  8446. used_ports |= port_mask;
  8447. default:
  8448. break;
  8449. }
  8450. }
  8451. return true;
  8452. }
  8453. static struct intel_crtc_config *
  8454. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8455. struct drm_framebuffer *fb,
  8456. struct drm_display_mode *mode)
  8457. {
  8458. struct drm_device *dev = crtc->dev;
  8459. struct intel_encoder *encoder;
  8460. struct intel_crtc_config *pipe_config;
  8461. int plane_bpp, ret = -EINVAL;
  8462. bool retry = true;
  8463. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8464. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8465. return ERR_PTR(-EINVAL);
  8466. }
  8467. if (!check_digital_port_conflicts(dev)) {
  8468. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8469. return ERR_PTR(-EINVAL);
  8470. }
  8471. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8472. if (!pipe_config)
  8473. return ERR_PTR(-ENOMEM);
  8474. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8475. drm_mode_copy(&pipe_config->requested_mode, mode);
  8476. pipe_config->cpu_transcoder =
  8477. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8478. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8479. /*
  8480. * Sanitize sync polarity flags based on requested ones. If neither
  8481. * positive or negative polarity is requested, treat this as meaning
  8482. * negative polarity.
  8483. */
  8484. if (!(pipe_config->adjusted_mode.flags &
  8485. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8486. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8487. if (!(pipe_config->adjusted_mode.flags &
  8488. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8489. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8490. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8491. * plane pixel format and any sink constraints into account. Returns the
  8492. * source plane bpp so that dithering can be selected on mismatches
  8493. * after encoders and crtc also have had their say. */
  8494. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8495. fb, pipe_config);
  8496. if (plane_bpp < 0)
  8497. goto fail;
  8498. /*
  8499. * Determine the real pipe dimensions. Note that stereo modes can
  8500. * increase the actual pipe size due to the frame doubling and
  8501. * insertion of additional space for blanks between the frame. This
  8502. * is stored in the crtc timings. We use the requested mode to do this
  8503. * computation to clearly distinguish it from the adjusted mode, which
  8504. * can be changed by the connectors in the below retry loop.
  8505. */
  8506. drm_crtc_get_hv_timing(&pipe_config->requested_mode,
  8507. &pipe_config->pipe_src_w,
  8508. &pipe_config->pipe_src_h);
  8509. encoder_retry:
  8510. /* Ensure the port clock defaults are reset when retrying. */
  8511. pipe_config->port_clock = 0;
  8512. pipe_config->pixel_multiplier = 1;
  8513. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8514. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8515. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8516. * adjust it according to limitations or connector properties, and also
  8517. * a chance to reject the mode entirely.
  8518. */
  8519. for_each_intel_encoder(dev, encoder) {
  8520. if (&encoder->new_crtc->base != crtc)
  8521. continue;
  8522. if (!(encoder->compute_config(encoder, pipe_config))) {
  8523. DRM_DEBUG_KMS("Encoder config failure\n");
  8524. goto fail;
  8525. }
  8526. }
  8527. /* Set default port clock if not overwritten by the encoder. Needs to be
  8528. * done afterwards in case the encoder adjusts the mode. */
  8529. if (!pipe_config->port_clock)
  8530. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8531. * pipe_config->pixel_multiplier;
  8532. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8533. if (ret < 0) {
  8534. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8535. goto fail;
  8536. }
  8537. if (ret == RETRY) {
  8538. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8539. ret = -EINVAL;
  8540. goto fail;
  8541. }
  8542. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8543. retry = false;
  8544. goto encoder_retry;
  8545. }
  8546. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8547. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8548. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8549. return pipe_config;
  8550. fail:
  8551. kfree(pipe_config);
  8552. return ERR_PTR(ret);
  8553. }
  8554. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8555. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8556. static void
  8557. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8558. unsigned *prepare_pipes, unsigned *disable_pipes)
  8559. {
  8560. struct intel_crtc *intel_crtc;
  8561. struct drm_device *dev = crtc->dev;
  8562. struct intel_encoder *encoder;
  8563. struct intel_connector *connector;
  8564. struct drm_crtc *tmp_crtc;
  8565. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8566. /* Check which crtcs have changed outputs connected to them, these need
  8567. * to be part of the prepare_pipes mask. We don't (yet) support global
  8568. * modeset across multiple crtcs, so modeset_pipes will only have one
  8569. * bit set at most. */
  8570. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8571. base.head) {
  8572. if (connector->base.encoder == &connector->new_encoder->base)
  8573. continue;
  8574. if (connector->base.encoder) {
  8575. tmp_crtc = connector->base.encoder->crtc;
  8576. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8577. }
  8578. if (connector->new_encoder)
  8579. *prepare_pipes |=
  8580. 1 << connector->new_encoder->new_crtc->pipe;
  8581. }
  8582. for_each_intel_encoder(dev, encoder) {
  8583. if (encoder->base.crtc == &encoder->new_crtc->base)
  8584. continue;
  8585. if (encoder->base.crtc) {
  8586. tmp_crtc = encoder->base.crtc;
  8587. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8588. }
  8589. if (encoder->new_crtc)
  8590. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8591. }
  8592. /* Check for pipes that will be enabled/disabled ... */
  8593. for_each_intel_crtc(dev, intel_crtc) {
  8594. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8595. continue;
  8596. if (!intel_crtc->new_enabled)
  8597. *disable_pipes |= 1 << intel_crtc->pipe;
  8598. else
  8599. *prepare_pipes |= 1 << intel_crtc->pipe;
  8600. }
  8601. /* set_mode is also used to update properties on life display pipes. */
  8602. intel_crtc = to_intel_crtc(crtc);
  8603. if (intel_crtc->new_enabled)
  8604. *prepare_pipes |= 1 << intel_crtc->pipe;
  8605. /*
  8606. * For simplicity do a full modeset on any pipe where the output routing
  8607. * changed. We could be more clever, but that would require us to be
  8608. * more careful with calling the relevant encoder->mode_set functions.
  8609. */
  8610. if (*prepare_pipes)
  8611. *modeset_pipes = *prepare_pipes;
  8612. /* ... and mask these out. */
  8613. *modeset_pipes &= ~(*disable_pipes);
  8614. *prepare_pipes &= ~(*disable_pipes);
  8615. /*
  8616. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8617. * obies this rule, but the modeset restore mode of
  8618. * intel_modeset_setup_hw_state does not.
  8619. */
  8620. *modeset_pipes &= 1 << intel_crtc->pipe;
  8621. *prepare_pipes &= 1 << intel_crtc->pipe;
  8622. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8623. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8624. }
  8625. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8626. {
  8627. struct drm_encoder *encoder;
  8628. struct drm_device *dev = crtc->dev;
  8629. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8630. if (encoder->crtc == crtc)
  8631. return true;
  8632. return false;
  8633. }
  8634. static void
  8635. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8636. {
  8637. struct drm_i915_private *dev_priv = dev->dev_private;
  8638. struct intel_encoder *intel_encoder;
  8639. struct intel_crtc *intel_crtc;
  8640. struct drm_connector *connector;
  8641. intel_shared_dpll_commit(dev_priv);
  8642. for_each_intel_encoder(dev, intel_encoder) {
  8643. if (!intel_encoder->base.crtc)
  8644. continue;
  8645. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8646. if (prepare_pipes & (1 << intel_crtc->pipe))
  8647. intel_encoder->connectors_active = false;
  8648. }
  8649. intel_modeset_commit_output_state(dev);
  8650. /* Double check state. */
  8651. for_each_intel_crtc(dev, intel_crtc) {
  8652. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8653. WARN_ON(intel_crtc->new_config &&
  8654. intel_crtc->new_config != &intel_crtc->config);
  8655. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8656. }
  8657. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8658. if (!connector->encoder || !connector->encoder->crtc)
  8659. continue;
  8660. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8661. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8662. struct drm_property *dpms_property =
  8663. dev->mode_config.dpms_property;
  8664. connector->dpms = DRM_MODE_DPMS_ON;
  8665. drm_object_property_set_value(&connector->base,
  8666. dpms_property,
  8667. DRM_MODE_DPMS_ON);
  8668. intel_encoder = to_intel_encoder(connector->encoder);
  8669. intel_encoder->connectors_active = true;
  8670. }
  8671. }
  8672. }
  8673. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8674. {
  8675. int diff;
  8676. if (clock1 == clock2)
  8677. return true;
  8678. if (!clock1 || !clock2)
  8679. return false;
  8680. diff = abs(clock1 - clock2);
  8681. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8682. return true;
  8683. return false;
  8684. }
  8685. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8686. list_for_each_entry((intel_crtc), \
  8687. &(dev)->mode_config.crtc_list, \
  8688. base.head) \
  8689. if (mask & (1 <<(intel_crtc)->pipe))
  8690. static bool
  8691. intel_pipe_config_compare(struct drm_device *dev,
  8692. struct intel_crtc_config *current_config,
  8693. struct intel_crtc_config *pipe_config)
  8694. {
  8695. #define PIPE_CONF_CHECK_X(name) \
  8696. if (current_config->name != pipe_config->name) { \
  8697. DRM_ERROR("mismatch in " #name " " \
  8698. "(expected 0x%08x, found 0x%08x)\n", \
  8699. current_config->name, \
  8700. pipe_config->name); \
  8701. return false; \
  8702. }
  8703. #define PIPE_CONF_CHECK_I(name) \
  8704. if (current_config->name != pipe_config->name) { \
  8705. DRM_ERROR("mismatch in " #name " " \
  8706. "(expected %i, found %i)\n", \
  8707. current_config->name, \
  8708. pipe_config->name); \
  8709. return false; \
  8710. }
  8711. /* This is required for BDW+ where there is only one set of registers for
  8712. * switching between high and low RR.
  8713. * This macro can be used whenever a comparison has to be made between one
  8714. * hw state and multiple sw state variables.
  8715. */
  8716. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8717. if ((current_config->name != pipe_config->name) && \
  8718. (current_config->alt_name != pipe_config->name)) { \
  8719. DRM_ERROR("mismatch in " #name " " \
  8720. "(expected %i or %i, found %i)\n", \
  8721. current_config->name, \
  8722. current_config->alt_name, \
  8723. pipe_config->name); \
  8724. return false; \
  8725. }
  8726. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8727. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8728. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8729. "(expected %i, found %i)\n", \
  8730. current_config->name & (mask), \
  8731. pipe_config->name & (mask)); \
  8732. return false; \
  8733. }
  8734. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8735. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8736. DRM_ERROR("mismatch in " #name " " \
  8737. "(expected %i, found %i)\n", \
  8738. current_config->name, \
  8739. pipe_config->name); \
  8740. return false; \
  8741. }
  8742. #define PIPE_CONF_QUIRK(quirk) \
  8743. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8744. PIPE_CONF_CHECK_I(cpu_transcoder);
  8745. PIPE_CONF_CHECK_I(has_pch_encoder);
  8746. PIPE_CONF_CHECK_I(fdi_lanes);
  8747. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8748. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8749. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8750. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8751. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8752. PIPE_CONF_CHECK_I(has_dp_encoder);
  8753. if (INTEL_INFO(dev)->gen < 8) {
  8754. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8755. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8756. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8757. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8758. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8759. if (current_config->has_drrs) {
  8760. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8761. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8762. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8763. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8764. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8765. }
  8766. } else {
  8767. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8768. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8769. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8770. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8771. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8772. }
  8773. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8774. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8775. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8776. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8777. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8778. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8779. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8780. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8781. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8782. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8783. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8784. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8785. PIPE_CONF_CHECK_I(pixel_multiplier);
  8786. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8787. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8788. IS_VALLEYVIEW(dev))
  8789. PIPE_CONF_CHECK_I(limited_color_range);
  8790. PIPE_CONF_CHECK_I(has_infoframe);
  8791. PIPE_CONF_CHECK_I(has_audio);
  8792. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8793. DRM_MODE_FLAG_INTERLACE);
  8794. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8795. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8796. DRM_MODE_FLAG_PHSYNC);
  8797. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8798. DRM_MODE_FLAG_NHSYNC);
  8799. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8800. DRM_MODE_FLAG_PVSYNC);
  8801. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8802. DRM_MODE_FLAG_NVSYNC);
  8803. }
  8804. PIPE_CONF_CHECK_I(pipe_src_w);
  8805. PIPE_CONF_CHECK_I(pipe_src_h);
  8806. /*
  8807. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8808. * screen. Since we don't yet re-compute the pipe config when moving
  8809. * just the lvds port away to another pipe the sw tracking won't match.
  8810. *
  8811. * Proper atomic modesets with recomputed global state will fix this.
  8812. * Until then just don't check gmch state for inherited modes.
  8813. */
  8814. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8815. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8816. /* pfit ratios are autocomputed by the hw on gen4+ */
  8817. if (INTEL_INFO(dev)->gen < 4)
  8818. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8819. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8820. }
  8821. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8822. if (current_config->pch_pfit.enabled) {
  8823. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8824. PIPE_CONF_CHECK_I(pch_pfit.size);
  8825. }
  8826. /* BDW+ don't expose a synchronous way to read the state */
  8827. if (IS_HASWELL(dev))
  8828. PIPE_CONF_CHECK_I(ips_enabled);
  8829. PIPE_CONF_CHECK_I(double_wide);
  8830. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8831. PIPE_CONF_CHECK_I(shared_dpll);
  8832. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8833. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8834. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8835. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8836. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8837. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  8838. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  8839. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  8840. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8841. PIPE_CONF_CHECK_I(pipe_bpp);
  8842. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8843. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8844. #undef PIPE_CONF_CHECK_X
  8845. #undef PIPE_CONF_CHECK_I
  8846. #undef PIPE_CONF_CHECK_I_ALT
  8847. #undef PIPE_CONF_CHECK_FLAGS
  8848. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8849. #undef PIPE_CONF_QUIRK
  8850. return true;
  8851. }
  8852. static void check_wm_state(struct drm_device *dev)
  8853. {
  8854. struct drm_i915_private *dev_priv = dev->dev_private;
  8855. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  8856. struct intel_crtc *intel_crtc;
  8857. int plane;
  8858. if (INTEL_INFO(dev)->gen < 9)
  8859. return;
  8860. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  8861. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  8862. for_each_intel_crtc(dev, intel_crtc) {
  8863. struct skl_ddb_entry *hw_entry, *sw_entry;
  8864. const enum pipe pipe = intel_crtc->pipe;
  8865. if (!intel_crtc->active)
  8866. continue;
  8867. /* planes */
  8868. for_each_plane(pipe, plane) {
  8869. hw_entry = &hw_ddb.plane[pipe][plane];
  8870. sw_entry = &sw_ddb->plane[pipe][plane];
  8871. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8872. continue;
  8873. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  8874. "(expected (%u,%u), found (%u,%u))\n",
  8875. pipe_name(pipe), plane + 1,
  8876. sw_entry->start, sw_entry->end,
  8877. hw_entry->start, hw_entry->end);
  8878. }
  8879. /* cursor */
  8880. hw_entry = &hw_ddb.cursor[pipe];
  8881. sw_entry = &sw_ddb->cursor[pipe];
  8882. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8883. continue;
  8884. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  8885. "(expected (%u,%u), found (%u,%u))\n",
  8886. pipe_name(pipe),
  8887. sw_entry->start, sw_entry->end,
  8888. hw_entry->start, hw_entry->end);
  8889. }
  8890. }
  8891. static void
  8892. check_connector_state(struct drm_device *dev)
  8893. {
  8894. struct intel_connector *connector;
  8895. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8896. base.head) {
  8897. /* This also checks the encoder/connector hw state with the
  8898. * ->get_hw_state callbacks. */
  8899. intel_connector_check_state(connector);
  8900. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  8901. "connector's staged encoder doesn't match current encoder\n");
  8902. }
  8903. }
  8904. static void
  8905. check_encoder_state(struct drm_device *dev)
  8906. {
  8907. struct intel_encoder *encoder;
  8908. struct intel_connector *connector;
  8909. for_each_intel_encoder(dev, encoder) {
  8910. bool enabled = false;
  8911. bool active = false;
  8912. enum pipe pipe, tracked_pipe;
  8913. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8914. encoder->base.base.id,
  8915. encoder->base.name);
  8916. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8917. "encoder's stage crtc doesn't match current crtc\n");
  8918. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  8919. "encoder's active_connectors set, but no crtc\n");
  8920. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8921. base.head) {
  8922. if (connector->base.encoder != &encoder->base)
  8923. continue;
  8924. enabled = true;
  8925. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8926. active = true;
  8927. }
  8928. /*
  8929. * for MST connectors if we unplug the connector is gone
  8930. * away but the encoder is still connected to a crtc
  8931. * until a modeset happens in response to the hotplug.
  8932. */
  8933. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  8934. continue;
  8935. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  8936. "encoder's enabled state mismatch "
  8937. "(expected %i, found %i)\n",
  8938. !!encoder->base.crtc, enabled);
  8939. I915_STATE_WARN(active && !encoder->base.crtc,
  8940. "active encoder with no crtc\n");
  8941. I915_STATE_WARN(encoder->connectors_active != active,
  8942. "encoder's computed active state doesn't match tracked active state "
  8943. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8944. active = encoder->get_hw_state(encoder, &pipe);
  8945. I915_STATE_WARN(active != encoder->connectors_active,
  8946. "encoder's hw state doesn't match sw tracking "
  8947. "(expected %i, found %i)\n",
  8948. encoder->connectors_active, active);
  8949. if (!encoder->base.crtc)
  8950. continue;
  8951. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8952. I915_STATE_WARN(active && pipe != tracked_pipe,
  8953. "active encoder's pipe doesn't match"
  8954. "(expected %i, found %i)\n",
  8955. tracked_pipe, pipe);
  8956. }
  8957. }
  8958. static void
  8959. check_crtc_state(struct drm_device *dev)
  8960. {
  8961. struct drm_i915_private *dev_priv = dev->dev_private;
  8962. struct intel_crtc *crtc;
  8963. struct intel_encoder *encoder;
  8964. struct intel_crtc_config pipe_config;
  8965. for_each_intel_crtc(dev, crtc) {
  8966. bool enabled = false;
  8967. bool active = false;
  8968. memset(&pipe_config, 0, sizeof(pipe_config));
  8969. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8970. crtc->base.base.id);
  8971. I915_STATE_WARN(crtc->active && !crtc->base.enabled,
  8972. "active crtc, but not enabled in sw tracking\n");
  8973. for_each_intel_encoder(dev, encoder) {
  8974. if (encoder->base.crtc != &crtc->base)
  8975. continue;
  8976. enabled = true;
  8977. if (encoder->connectors_active)
  8978. active = true;
  8979. }
  8980. I915_STATE_WARN(active != crtc->active,
  8981. "crtc's computed active state doesn't match tracked active state "
  8982. "(expected %i, found %i)\n", active, crtc->active);
  8983. I915_STATE_WARN(enabled != crtc->base.enabled,
  8984. "crtc's computed enabled state doesn't match tracked enabled state "
  8985. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8986. active = dev_priv->display.get_pipe_config(crtc,
  8987. &pipe_config);
  8988. /* hw state is inconsistent with the pipe quirk */
  8989. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  8990. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  8991. active = crtc->active;
  8992. for_each_intel_encoder(dev, encoder) {
  8993. enum pipe pipe;
  8994. if (encoder->base.crtc != &crtc->base)
  8995. continue;
  8996. if (encoder->get_hw_state(encoder, &pipe))
  8997. encoder->get_config(encoder, &pipe_config);
  8998. }
  8999. I915_STATE_WARN(crtc->active != active,
  9000. "crtc active state doesn't match with hw state "
  9001. "(expected %i, found %i)\n", crtc->active, active);
  9002. if (active &&
  9003. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  9004. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9005. intel_dump_pipe_config(crtc, &pipe_config,
  9006. "[hw state]");
  9007. intel_dump_pipe_config(crtc, &crtc->config,
  9008. "[sw state]");
  9009. }
  9010. }
  9011. }
  9012. static void
  9013. check_shared_dpll_state(struct drm_device *dev)
  9014. {
  9015. struct drm_i915_private *dev_priv = dev->dev_private;
  9016. struct intel_crtc *crtc;
  9017. struct intel_dpll_hw_state dpll_hw_state;
  9018. int i;
  9019. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9020. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9021. int enabled_crtcs = 0, active_crtcs = 0;
  9022. bool active;
  9023. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9024. DRM_DEBUG_KMS("%s\n", pll->name);
  9025. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9026. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9027. "more active pll users than references: %i vs %i\n",
  9028. pll->active, hweight32(pll->config.crtc_mask));
  9029. I915_STATE_WARN(pll->active && !pll->on,
  9030. "pll in active use but not on in sw tracking\n");
  9031. I915_STATE_WARN(pll->on && !pll->active,
  9032. "pll in on but not on in use in sw tracking\n");
  9033. I915_STATE_WARN(pll->on != active,
  9034. "pll on state mismatch (expected %i, found %i)\n",
  9035. pll->on, active);
  9036. for_each_intel_crtc(dev, crtc) {
  9037. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9038. enabled_crtcs++;
  9039. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9040. active_crtcs++;
  9041. }
  9042. I915_STATE_WARN(pll->active != active_crtcs,
  9043. "pll active crtcs mismatch (expected %i, found %i)\n",
  9044. pll->active, active_crtcs);
  9045. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9046. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9047. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9048. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9049. sizeof(dpll_hw_state)),
  9050. "pll hw state mismatch\n");
  9051. }
  9052. }
  9053. void
  9054. intel_modeset_check_state(struct drm_device *dev)
  9055. {
  9056. check_wm_state(dev);
  9057. check_connector_state(dev);
  9058. check_encoder_state(dev);
  9059. check_crtc_state(dev);
  9060. check_shared_dpll_state(dev);
  9061. }
  9062. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  9063. int dotclock)
  9064. {
  9065. /*
  9066. * FDI already provided one idea for the dotclock.
  9067. * Yell if the encoder disagrees.
  9068. */
  9069. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  9070. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9071. pipe_config->adjusted_mode.crtc_clock, dotclock);
  9072. }
  9073. static void update_scanline_offset(struct intel_crtc *crtc)
  9074. {
  9075. struct drm_device *dev = crtc->base.dev;
  9076. /*
  9077. * The scanline counter increments at the leading edge of hsync.
  9078. *
  9079. * On most platforms it starts counting from vtotal-1 on the
  9080. * first active line. That means the scanline counter value is
  9081. * always one less than what we would expect. Ie. just after
  9082. * start of vblank, which also occurs at start of hsync (on the
  9083. * last active line), the scanline counter will read vblank_start-1.
  9084. *
  9085. * On gen2 the scanline counter starts counting from 1 instead
  9086. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9087. * to keep the value positive), instead of adding one.
  9088. *
  9089. * On HSW+ the behaviour of the scanline counter depends on the output
  9090. * type. For DP ports it behaves like most other platforms, but on HDMI
  9091. * there's an extra 1 line difference. So we need to add two instead of
  9092. * one to the value.
  9093. */
  9094. if (IS_GEN2(dev)) {
  9095. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  9096. int vtotal;
  9097. vtotal = mode->crtc_vtotal;
  9098. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9099. vtotal /= 2;
  9100. crtc->scanline_offset = vtotal - 1;
  9101. } else if (HAS_DDI(dev) &&
  9102. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9103. crtc->scanline_offset = 2;
  9104. } else
  9105. crtc->scanline_offset = 1;
  9106. }
  9107. static struct intel_crtc_config *
  9108. intel_modeset_compute_config(struct drm_crtc *crtc,
  9109. struct drm_display_mode *mode,
  9110. struct drm_framebuffer *fb,
  9111. unsigned *modeset_pipes,
  9112. unsigned *prepare_pipes,
  9113. unsigned *disable_pipes)
  9114. {
  9115. struct intel_crtc_config *pipe_config = NULL;
  9116. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9117. prepare_pipes, disable_pipes);
  9118. if ((*modeset_pipes) == 0)
  9119. goto out;
  9120. /*
  9121. * Note this needs changes when we start tracking multiple modes
  9122. * and crtcs. At that point we'll need to compute the whole config
  9123. * (i.e. one pipe_config for each crtc) rather than just the one
  9124. * for this crtc.
  9125. */
  9126. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9127. if (IS_ERR(pipe_config)) {
  9128. goto out;
  9129. }
  9130. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9131. "[modeset]");
  9132. out:
  9133. return pipe_config;
  9134. }
  9135. static int __intel_set_mode(struct drm_crtc *crtc,
  9136. struct drm_display_mode *mode,
  9137. int x, int y, struct drm_framebuffer *fb,
  9138. struct intel_crtc_config *pipe_config,
  9139. unsigned modeset_pipes,
  9140. unsigned prepare_pipes,
  9141. unsigned disable_pipes)
  9142. {
  9143. struct drm_device *dev = crtc->dev;
  9144. struct drm_i915_private *dev_priv = dev->dev_private;
  9145. struct drm_display_mode *saved_mode;
  9146. struct intel_crtc *intel_crtc;
  9147. int ret = 0;
  9148. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9149. if (!saved_mode)
  9150. return -ENOMEM;
  9151. *saved_mode = crtc->mode;
  9152. if (modeset_pipes)
  9153. to_intel_crtc(crtc)->new_config = pipe_config;
  9154. /*
  9155. * See if the config requires any additional preparation, e.g.
  9156. * to adjust global state with pipes off. We need to do this
  9157. * here so we can get the modeset_pipe updated config for the new
  9158. * mode set on this crtc. For other crtcs we need to use the
  9159. * adjusted_mode bits in the crtc directly.
  9160. */
  9161. if (IS_VALLEYVIEW(dev)) {
  9162. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9163. /* may have added more to prepare_pipes than we should */
  9164. prepare_pipes &= ~disable_pipes;
  9165. }
  9166. if (dev_priv->display.crtc_compute_clock) {
  9167. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9168. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9169. if (ret)
  9170. goto done;
  9171. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9172. ret = dev_priv->display.crtc_compute_clock(intel_crtc);
  9173. if (ret) {
  9174. intel_shared_dpll_abort_config(dev_priv);
  9175. goto done;
  9176. }
  9177. }
  9178. }
  9179. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9180. intel_crtc_disable(&intel_crtc->base);
  9181. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9182. if (intel_crtc->base.enabled)
  9183. dev_priv->display.crtc_disable(&intel_crtc->base);
  9184. }
  9185. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9186. * to set it here already despite that we pass it down the callchain.
  9187. *
  9188. * Note we'll need to fix this up when we start tracking multiple
  9189. * pipes; here we assume a single modeset_pipe and only track the
  9190. * single crtc and mode.
  9191. */
  9192. if (modeset_pipes) {
  9193. crtc->mode = *mode;
  9194. /* mode_set/enable/disable functions rely on a correct pipe
  9195. * config. */
  9196. to_intel_crtc(crtc)->config = *pipe_config;
  9197. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9198. /*
  9199. * Calculate and store various constants which
  9200. * are later needed by vblank and swap-completion
  9201. * timestamping. They are derived from true hwmode.
  9202. */
  9203. drm_calc_timestamping_constants(crtc,
  9204. &pipe_config->adjusted_mode);
  9205. }
  9206. /* Only after disabling all output pipelines that will be changed can we
  9207. * update the the output configuration. */
  9208. intel_modeset_update_state(dev, prepare_pipes);
  9209. modeset_update_crtc_power_domains(dev);
  9210. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9211. * on the DPLL.
  9212. */
  9213. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9214. struct drm_plane *primary = intel_crtc->base.primary;
  9215. int vdisplay, hdisplay;
  9216. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9217. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9218. fb, 0, 0,
  9219. hdisplay, vdisplay,
  9220. x << 16, y << 16,
  9221. hdisplay << 16, vdisplay << 16);
  9222. }
  9223. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9224. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9225. update_scanline_offset(intel_crtc);
  9226. dev_priv->display.crtc_enable(&intel_crtc->base);
  9227. }
  9228. /* FIXME: add subpixel order */
  9229. done:
  9230. if (ret && crtc->enabled)
  9231. crtc->mode = *saved_mode;
  9232. kfree(pipe_config);
  9233. kfree(saved_mode);
  9234. return ret;
  9235. }
  9236. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9237. struct drm_display_mode *mode,
  9238. int x, int y, struct drm_framebuffer *fb,
  9239. struct intel_crtc_config *pipe_config,
  9240. unsigned modeset_pipes,
  9241. unsigned prepare_pipes,
  9242. unsigned disable_pipes)
  9243. {
  9244. int ret;
  9245. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9246. prepare_pipes, disable_pipes);
  9247. if (ret == 0)
  9248. intel_modeset_check_state(crtc->dev);
  9249. return ret;
  9250. }
  9251. static int intel_set_mode(struct drm_crtc *crtc,
  9252. struct drm_display_mode *mode,
  9253. int x, int y, struct drm_framebuffer *fb)
  9254. {
  9255. struct intel_crtc_config *pipe_config;
  9256. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9257. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9258. &modeset_pipes,
  9259. &prepare_pipes,
  9260. &disable_pipes);
  9261. if (IS_ERR(pipe_config))
  9262. return PTR_ERR(pipe_config);
  9263. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9264. modeset_pipes, prepare_pipes,
  9265. disable_pipes);
  9266. }
  9267. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9268. {
  9269. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9270. }
  9271. #undef for_each_intel_crtc_masked
  9272. static void intel_set_config_free(struct intel_set_config *config)
  9273. {
  9274. if (!config)
  9275. return;
  9276. kfree(config->save_connector_encoders);
  9277. kfree(config->save_encoder_crtcs);
  9278. kfree(config->save_crtc_enabled);
  9279. kfree(config);
  9280. }
  9281. static int intel_set_config_save_state(struct drm_device *dev,
  9282. struct intel_set_config *config)
  9283. {
  9284. struct drm_crtc *crtc;
  9285. struct drm_encoder *encoder;
  9286. struct drm_connector *connector;
  9287. int count;
  9288. config->save_crtc_enabled =
  9289. kcalloc(dev->mode_config.num_crtc,
  9290. sizeof(bool), GFP_KERNEL);
  9291. if (!config->save_crtc_enabled)
  9292. return -ENOMEM;
  9293. config->save_encoder_crtcs =
  9294. kcalloc(dev->mode_config.num_encoder,
  9295. sizeof(struct drm_crtc *), GFP_KERNEL);
  9296. if (!config->save_encoder_crtcs)
  9297. return -ENOMEM;
  9298. config->save_connector_encoders =
  9299. kcalloc(dev->mode_config.num_connector,
  9300. sizeof(struct drm_encoder *), GFP_KERNEL);
  9301. if (!config->save_connector_encoders)
  9302. return -ENOMEM;
  9303. /* Copy data. Note that driver private data is not affected.
  9304. * Should anything bad happen only the expected state is
  9305. * restored, not the drivers personal bookkeeping.
  9306. */
  9307. count = 0;
  9308. for_each_crtc(dev, crtc) {
  9309. config->save_crtc_enabled[count++] = crtc->enabled;
  9310. }
  9311. count = 0;
  9312. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9313. config->save_encoder_crtcs[count++] = encoder->crtc;
  9314. }
  9315. count = 0;
  9316. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9317. config->save_connector_encoders[count++] = connector->encoder;
  9318. }
  9319. return 0;
  9320. }
  9321. static void intel_set_config_restore_state(struct drm_device *dev,
  9322. struct intel_set_config *config)
  9323. {
  9324. struct intel_crtc *crtc;
  9325. struct intel_encoder *encoder;
  9326. struct intel_connector *connector;
  9327. int count;
  9328. count = 0;
  9329. for_each_intel_crtc(dev, crtc) {
  9330. crtc->new_enabled = config->save_crtc_enabled[count++];
  9331. if (crtc->new_enabled)
  9332. crtc->new_config = &crtc->config;
  9333. else
  9334. crtc->new_config = NULL;
  9335. }
  9336. count = 0;
  9337. for_each_intel_encoder(dev, encoder) {
  9338. encoder->new_crtc =
  9339. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9340. }
  9341. count = 0;
  9342. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9343. connector->new_encoder =
  9344. to_intel_encoder(config->save_connector_encoders[count++]);
  9345. }
  9346. }
  9347. static bool
  9348. is_crtc_connector_off(struct drm_mode_set *set)
  9349. {
  9350. int i;
  9351. if (set->num_connectors == 0)
  9352. return false;
  9353. if (WARN_ON(set->connectors == NULL))
  9354. return false;
  9355. for (i = 0; i < set->num_connectors; i++)
  9356. if (set->connectors[i]->encoder &&
  9357. set->connectors[i]->encoder->crtc == set->crtc &&
  9358. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9359. return true;
  9360. return false;
  9361. }
  9362. static void
  9363. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9364. struct intel_set_config *config)
  9365. {
  9366. /* We should be able to check here if the fb has the same properties
  9367. * and then just flip_or_move it */
  9368. if (is_crtc_connector_off(set)) {
  9369. config->mode_changed = true;
  9370. } else if (set->crtc->primary->fb != set->fb) {
  9371. /*
  9372. * If we have no fb, we can only flip as long as the crtc is
  9373. * active, otherwise we need a full mode set. The crtc may
  9374. * be active if we've only disabled the primary plane, or
  9375. * in fastboot situations.
  9376. */
  9377. if (set->crtc->primary->fb == NULL) {
  9378. struct intel_crtc *intel_crtc =
  9379. to_intel_crtc(set->crtc);
  9380. if (intel_crtc->active) {
  9381. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9382. config->fb_changed = true;
  9383. } else {
  9384. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9385. config->mode_changed = true;
  9386. }
  9387. } else if (set->fb == NULL) {
  9388. config->mode_changed = true;
  9389. } else if (set->fb->pixel_format !=
  9390. set->crtc->primary->fb->pixel_format) {
  9391. config->mode_changed = true;
  9392. } else {
  9393. config->fb_changed = true;
  9394. }
  9395. }
  9396. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9397. config->fb_changed = true;
  9398. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9399. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9400. drm_mode_debug_printmodeline(&set->crtc->mode);
  9401. drm_mode_debug_printmodeline(set->mode);
  9402. config->mode_changed = true;
  9403. }
  9404. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9405. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9406. }
  9407. static int
  9408. intel_modeset_stage_output_state(struct drm_device *dev,
  9409. struct drm_mode_set *set,
  9410. struct intel_set_config *config)
  9411. {
  9412. struct intel_connector *connector;
  9413. struct intel_encoder *encoder;
  9414. struct intel_crtc *crtc;
  9415. int ro;
  9416. /* The upper layers ensure that we either disable a crtc or have a list
  9417. * of connectors. For paranoia, double-check this. */
  9418. WARN_ON(!set->fb && (set->num_connectors != 0));
  9419. WARN_ON(set->fb && (set->num_connectors == 0));
  9420. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9421. base.head) {
  9422. /* Otherwise traverse passed in connector list and get encoders
  9423. * for them. */
  9424. for (ro = 0; ro < set->num_connectors; ro++) {
  9425. if (set->connectors[ro] == &connector->base) {
  9426. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9427. break;
  9428. }
  9429. }
  9430. /* If we disable the crtc, disable all its connectors. Also, if
  9431. * the connector is on the changing crtc but not on the new
  9432. * connector list, disable it. */
  9433. if ((!set->fb || ro == set->num_connectors) &&
  9434. connector->base.encoder &&
  9435. connector->base.encoder->crtc == set->crtc) {
  9436. connector->new_encoder = NULL;
  9437. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9438. connector->base.base.id,
  9439. connector->base.name);
  9440. }
  9441. if (&connector->new_encoder->base != connector->base.encoder) {
  9442. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9443. config->mode_changed = true;
  9444. }
  9445. }
  9446. /* connector->new_encoder is now updated for all connectors. */
  9447. /* Update crtc of enabled connectors. */
  9448. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9449. base.head) {
  9450. struct drm_crtc *new_crtc;
  9451. if (!connector->new_encoder)
  9452. continue;
  9453. new_crtc = connector->new_encoder->base.crtc;
  9454. for (ro = 0; ro < set->num_connectors; ro++) {
  9455. if (set->connectors[ro] == &connector->base)
  9456. new_crtc = set->crtc;
  9457. }
  9458. /* Make sure the new CRTC will work with the encoder */
  9459. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9460. new_crtc)) {
  9461. return -EINVAL;
  9462. }
  9463. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9464. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9465. connector->base.base.id,
  9466. connector->base.name,
  9467. new_crtc->base.id);
  9468. }
  9469. /* Check for any encoders that needs to be disabled. */
  9470. for_each_intel_encoder(dev, encoder) {
  9471. int num_connectors = 0;
  9472. list_for_each_entry(connector,
  9473. &dev->mode_config.connector_list,
  9474. base.head) {
  9475. if (connector->new_encoder == encoder) {
  9476. WARN_ON(!connector->new_encoder->new_crtc);
  9477. num_connectors++;
  9478. }
  9479. }
  9480. if (num_connectors == 0)
  9481. encoder->new_crtc = NULL;
  9482. else if (num_connectors > 1)
  9483. return -EINVAL;
  9484. /* Only now check for crtc changes so we don't miss encoders
  9485. * that will be disabled. */
  9486. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9487. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9488. config->mode_changed = true;
  9489. }
  9490. }
  9491. /* Now we've also updated encoder->new_crtc for all encoders. */
  9492. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9493. base.head) {
  9494. if (connector->new_encoder)
  9495. if (connector->new_encoder != connector->encoder)
  9496. connector->encoder = connector->new_encoder;
  9497. }
  9498. for_each_intel_crtc(dev, crtc) {
  9499. crtc->new_enabled = false;
  9500. for_each_intel_encoder(dev, encoder) {
  9501. if (encoder->new_crtc == crtc) {
  9502. crtc->new_enabled = true;
  9503. break;
  9504. }
  9505. }
  9506. if (crtc->new_enabled != crtc->base.enabled) {
  9507. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9508. crtc->new_enabled ? "en" : "dis");
  9509. config->mode_changed = true;
  9510. }
  9511. if (crtc->new_enabled)
  9512. crtc->new_config = &crtc->config;
  9513. else
  9514. crtc->new_config = NULL;
  9515. }
  9516. return 0;
  9517. }
  9518. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9519. {
  9520. struct drm_device *dev = crtc->base.dev;
  9521. struct intel_encoder *encoder;
  9522. struct intel_connector *connector;
  9523. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9524. pipe_name(crtc->pipe));
  9525. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9526. if (connector->new_encoder &&
  9527. connector->new_encoder->new_crtc == crtc)
  9528. connector->new_encoder = NULL;
  9529. }
  9530. for_each_intel_encoder(dev, encoder) {
  9531. if (encoder->new_crtc == crtc)
  9532. encoder->new_crtc = NULL;
  9533. }
  9534. crtc->new_enabled = false;
  9535. crtc->new_config = NULL;
  9536. }
  9537. static int intel_crtc_set_config(struct drm_mode_set *set)
  9538. {
  9539. struct drm_device *dev;
  9540. struct drm_mode_set save_set;
  9541. struct intel_set_config *config;
  9542. struct intel_crtc_config *pipe_config;
  9543. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9544. int ret;
  9545. BUG_ON(!set);
  9546. BUG_ON(!set->crtc);
  9547. BUG_ON(!set->crtc->helper_private);
  9548. /* Enforce sane interface api - has been abused by the fb helper. */
  9549. BUG_ON(!set->mode && set->fb);
  9550. BUG_ON(set->fb && set->num_connectors == 0);
  9551. if (set->fb) {
  9552. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9553. set->crtc->base.id, set->fb->base.id,
  9554. (int)set->num_connectors, set->x, set->y);
  9555. } else {
  9556. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9557. }
  9558. dev = set->crtc->dev;
  9559. ret = -ENOMEM;
  9560. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9561. if (!config)
  9562. goto out_config;
  9563. ret = intel_set_config_save_state(dev, config);
  9564. if (ret)
  9565. goto out_config;
  9566. save_set.crtc = set->crtc;
  9567. save_set.mode = &set->crtc->mode;
  9568. save_set.x = set->crtc->x;
  9569. save_set.y = set->crtc->y;
  9570. save_set.fb = set->crtc->primary->fb;
  9571. /* Compute whether we need a full modeset, only an fb base update or no
  9572. * change at all. In the future we might also check whether only the
  9573. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9574. * such cases. */
  9575. intel_set_config_compute_mode_changes(set, config);
  9576. ret = intel_modeset_stage_output_state(dev, set, config);
  9577. if (ret)
  9578. goto fail;
  9579. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9580. set->fb,
  9581. &modeset_pipes,
  9582. &prepare_pipes,
  9583. &disable_pipes);
  9584. if (IS_ERR(pipe_config)) {
  9585. ret = PTR_ERR(pipe_config);
  9586. goto fail;
  9587. } else if (pipe_config) {
  9588. if (pipe_config->has_audio !=
  9589. to_intel_crtc(set->crtc)->config.has_audio)
  9590. config->mode_changed = true;
  9591. /* Force mode sets for any infoframe stuff */
  9592. if (pipe_config->has_infoframe ||
  9593. to_intel_crtc(set->crtc)->config.has_infoframe)
  9594. config->mode_changed = true;
  9595. }
  9596. /* set_mode will free it in the mode_changed case */
  9597. if (!config->mode_changed)
  9598. kfree(pipe_config);
  9599. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9600. if (config->mode_changed) {
  9601. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9602. set->x, set->y, set->fb, pipe_config,
  9603. modeset_pipes, prepare_pipes,
  9604. disable_pipes);
  9605. } else if (config->fb_changed) {
  9606. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9607. struct drm_plane *primary = set->crtc->primary;
  9608. int vdisplay, hdisplay;
  9609. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9610. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9611. 0, 0, hdisplay, vdisplay,
  9612. set->x << 16, set->y << 16,
  9613. hdisplay << 16, vdisplay << 16);
  9614. /*
  9615. * We need to make sure the primary plane is re-enabled if it
  9616. * has previously been turned off.
  9617. */
  9618. if (!intel_crtc->primary_enabled && ret == 0) {
  9619. WARN_ON(!intel_crtc->active);
  9620. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9621. }
  9622. /*
  9623. * In the fastboot case this may be our only check of the
  9624. * state after boot. It would be better to only do it on
  9625. * the first update, but we don't have a nice way of doing that
  9626. * (and really, set_config isn't used much for high freq page
  9627. * flipping, so increasing its cost here shouldn't be a big
  9628. * deal).
  9629. */
  9630. if (i915.fastboot && ret == 0)
  9631. intel_modeset_check_state(set->crtc->dev);
  9632. }
  9633. if (ret) {
  9634. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9635. set->crtc->base.id, ret);
  9636. fail:
  9637. intel_set_config_restore_state(dev, config);
  9638. /*
  9639. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9640. * force the pipe off to avoid oopsing in the modeset code
  9641. * due to fb==NULL. This should only happen during boot since
  9642. * we don't yet reconstruct the FB from the hardware state.
  9643. */
  9644. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9645. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9646. /* Try to restore the config */
  9647. if (config->mode_changed &&
  9648. intel_set_mode(save_set.crtc, save_set.mode,
  9649. save_set.x, save_set.y, save_set.fb))
  9650. DRM_ERROR("failed to restore config after modeset failure\n");
  9651. }
  9652. out_config:
  9653. intel_set_config_free(config);
  9654. return ret;
  9655. }
  9656. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9657. .gamma_set = intel_crtc_gamma_set,
  9658. .set_config = intel_crtc_set_config,
  9659. .destroy = intel_crtc_destroy,
  9660. .page_flip = intel_crtc_page_flip,
  9661. };
  9662. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9663. struct intel_shared_dpll *pll,
  9664. struct intel_dpll_hw_state *hw_state)
  9665. {
  9666. uint32_t val;
  9667. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9668. return false;
  9669. val = I915_READ(PCH_DPLL(pll->id));
  9670. hw_state->dpll = val;
  9671. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9672. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9673. return val & DPLL_VCO_ENABLE;
  9674. }
  9675. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9676. struct intel_shared_dpll *pll)
  9677. {
  9678. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  9679. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  9680. }
  9681. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9682. struct intel_shared_dpll *pll)
  9683. {
  9684. /* PCH refclock must be enabled first */
  9685. ibx_assert_pch_refclk_enabled(dev_priv);
  9686. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9687. /* Wait for the clocks to stabilize. */
  9688. POSTING_READ(PCH_DPLL(pll->id));
  9689. udelay(150);
  9690. /* The pixel multiplier can only be updated once the
  9691. * DPLL is enabled and the clocks are stable.
  9692. *
  9693. * So write it again.
  9694. */
  9695. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9696. POSTING_READ(PCH_DPLL(pll->id));
  9697. udelay(200);
  9698. }
  9699. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9700. struct intel_shared_dpll *pll)
  9701. {
  9702. struct drm_device *dev = dev_priv->dev;
  9703. struct intel_crtc *crtc;
  9704. /* Make sure no transcoder isn't still depending on us. */
  9705. for_each_intel_crtc(dev, crtc) {
  9706. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9707. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9708. }
  9709. I915_WRITE(PCH_DPLL(pll->id), 0);
  9710. POSTING_READ(PCH_DPLL(pll->id));
  9711. udelay(200);
  9712. }
  9713. static char *ibx_pch_dpll_names[] = {
  9714. "PCH DPLL A",
  9715. "PCH DPLL B",
  9716. };
  9717. static void ibx_pch_dpll_init(struct drm_device *dev)
  9718. {
  9719. struct drm_i915_private *dev_priv = dev->dev_private;
  9720. int i;
  9721. dev_priv->num_shared_dpll = 2;
  9722. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9723. dev_priv->shared_dplls[i].id = i;
  9724. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9725. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9726. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9727. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9728. dev_priv->shared_dplls[i].get_hw_state =
  9729. ibx_pch_dpll_get_hw_state;
  9730. }
  9731. }
  9732. static void intel_shared_dpll_init(struct drm_device *dev)
  9733. {
  9734. struct drm_i915_private *dev_priv = dev->dev_private;
  9735. if (HAS_DDI(dev))
  9736. intel_ddi_pll_init(dev);
  9737. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9738. ibx_pch_dpll_init(dev);
  9739. else
  9740. dev_priv->num_shared_dpll = 0;
  9741. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9742. }
  9743. /**
  9744. * intel_prepare_plane_fb - Prepare fb for usage on plane
  9745. * @plane: drm plane to prepare for
  9746. * @fb: framebuffer to prepare for presentation
  9747. *
  9748. * Prepares a framebuffer for usage on a display plane. Generally this
  9749. * involves pinning the underlying object and updating the frontbuffer tracking
  9750. * bits. Some older platforms need special physical address handling for
  9751. * cursor planes.
  9752. *
  9753. * Returns 0 on success, negative error code on failure.
  9754. */
  9755. int
  9756. intel_prepare_plane_fb(struct drm_plane *plane,
  9757. struct drm_framebuffer *fb)
  9758. {
  9759. struct drm_device *dev = plane->dev;
  9760. struct intel_plane *intel_plane = to_intel_plane(plane);
  9761. enum pipe pipe = intel_plane->pipe;
  9762. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9763. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9764. unsigned frontbuffer_bits = 0;
  9765. int ret = 0;
  9766. if (WARN_ON(fb == plane->fb || !obj))
  9767. return 0;
  9768. switch (plane->type) {
  9769. case DRM_PLANE_TYPE_PRIMARY:
  9770. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  9771. break;
  9772. case DRM_PLANE_TYPE_CURSOR:
  9773. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  9774. break;
  9775. case DRM_PLANE_TYPE_OVERLAY:
  9776. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  9777. break;
  9778. }
  9779. mutex_lock(&dev->struct_mutex);
  9780. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  9781. INTEL_INFO(dev)->cursor_needs_physical) {
  9782. int align = IS_I830(dev) ? 16 * 1024 : 256;
  9783. ret = i915_gem_object_attach_phys(obj, align);
  9784. if (ret)
  9785. DRM_DEBUG_KMS("failed to attach phys object\n");
  9786. } else {
  9787. ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
  9788. }
  9789. if (ret == 0)
  9790. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  9791. mutex_unlock(&dev->struct_mutex);
  9792. return ret;
  9793. }
  9794. /**
  9795. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  9796. * @plane: drm plane to clean up for
  9797. * @fb: old framebuffer that was on plane
  9798. *
  9799. * Cleans up a framebuffer that has just been removed from a plane.
  9800. */
  9801. void
  9802. intel_cleanup_plane_fb(struct drm_plane *plane,
  9803. struct drm_framebuffer *fb)
  9804. {
  9805. struct drm_device *dev = plane->dev;
  9806. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9807. if (WARN_ON(!obj))
  9808. return;
  9809. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  9810. !INTEL_INFO(dev)->cursor_needs_physical) {
  9811. mutex_lock(&dev->struct_mutex);
  9812. intel_unpin_fb_obj(obj);
  9813. mutex_unlock(&dev->struct_mutex);
  9814. }
  9815. }
  9816. static int
  9817. intel_check_primary_plane(struct drm_plane *plane,
  9818. struct intel_plane_state *state)
  9819. {
  9820. struct drm_crtc *crtc = state->base.crtc;
  9821. struct drm_framebuffer *fb = state->base.fb;
  9822. struct drm_rect *dest = &state->dst;
  9823. struct drm_rect *src = &state->src;
  9824. const struct drm_rect *clip = &state->clip;
  9825. int ret;
  9826. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9827. src, dest, clip,
  9828. DRM_PLANE_HELPER_NO_SCALING,
  9829. DRM_PLANE_HELPER_NO_SCALING,
  9830. false, true, &state->visible);
  9831. if (ret)
  9832. return ret;
  9833. intel_crtc_wait_for_pending_flips(crtc);
  9834. if (intel_crtc_has_pending_flip(crtc)) {
  9835. DRM_ERROR("pipe is still busy with an old pageflip\n");
  9836. return -EBUSY;
  9837. }
  9838. return 0;
  9839. }
  9840. static void
  9841. intel_commit_primary_plane(struct drm_plane *plane,
  9842. struct intel_plane_state *state)
  9843. {
  9844. struct drm_crtc *crtc = state->base.crtc;
  9845. struct drm_framebuffer *fb = state->base.fb;
  9846. struct drm_device *dev = plane->dev;
  9847. struct drm_i915_private *dev_priv = dev->dev_private;
  9848. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9849. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9850. struct intel_plane *intel_plane = to_intel_plane(plane);
  9851. struct drm_rect *src = &state->src;
  9852. enum pipe pipe = intel_plane->pipe;
  9853. if (!fb) {
  9854. /*
  9855. * 'prepare' is never called when plane is being disabled, so
  9856. * we need to handle frontbuffer tracking here
  9857. */
  9858. mutex_lock(&dev->struct_mutex);
  9859. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
  9860. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9861. mutex_unlock(&dev->struct_mutex);
  9862. }
  9863. plane->fb = fb;
  9864. crtc->x = src->x1 >> 16;
  9865. crtc->y = src->y1 >> 16;
  9866. intel_plane->crtc_x = state->orig_dst.x1;
  9867. intel_plane->crtc_y = state->orig_dst.y1;
  9868. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  9869. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  9870. intel_plane->src_x = state->orig_src.x1;
  9871. intel_plane->src_y = state->orig_src.y1;
  9872. intel_plane->src_w = drm_rect_width(&state->orig_src);
  9873. intel_plane->src_h = drm_rect_height(&state->orig_src);
  9874. intel_plane->obj = obj;
  9875. if (intel_crtc->active) {
  9876. /*
  9877. * FBC does not work on some platforms for rotated
  9878. * planes, so disable it when rotation is not 0 and
  9879. * update it when rotation is set back to 0.
  9880. *
  9881. * FIXME: This is redundant with the fbc update done in
  9882. * the primary plane enable function except that that
  9883. * one is done too late. We eventually need to unify
  9884. * this.
  9885. */
  9886. if (intel_crtc->primary_enabled &&
  9887. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9888. dev_priv->fbc.plane == intel_crtc->plane &&
  9889. intel_plane->rotation != BIT(DRM_ROTATE_0)) {
  9890. intel_fbc_disable(dev);
  9891. }
  9892. if (state->visible) {
  9893. bool was_enabled = intel_crtc->primary_enabled;
  9894. /* FIXME: kill this fastboot hack */
  9895. intel_update_pipe_size(intel_crtc);
  9896. intel_crtc->primary_enabled = true;
  9897. dev_priv->display.update_primary_plane(crtc, plane->fb,
  9898. crtc->x, crtc->y);
  9899. /*
  9900. * BDW signals flip done immediately if the plane
  9901. * is disabled, even if the plane enable is already
  9902. * armed to occur at the next vblank :(
  9903. */
  9904. if (IS_BROADWELL(dev) && !was_enabled)
  9905. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9906. } else {
  9907. /*
  9908. * If clipping results in a non-visible primary plane,
  9909. * we'll disable the primary plane. Note that this is
  9910. * a bit different than what happens if userspace
  9911. * explicitly disables the plane by passing fb=0
  9912. * because plane->fb still gets set and pinned.
  9913. */
  9914. intel_disable_primary_hw_plane(plane, crtc);
  9915. }
  9916. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9917. mutex_lock(&dev->struct_mutex);
  9918. intel_fbc_update(dev);
  9919. mutex_unlock(&dev->struct_mutex);
  9920. }
  9921. }
  9922. int
  9923. intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  9924. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9925. unsigned int crtc_w, unsigned int crtc_h,
  9926. uint32_t src_x, uint32_t src_y,
  9927. uint32_t src_w, uint32_t src_h)
  9928. {
  9929. struct drm_device *dev = plane->dev;
  9930. struct drm_i915_private *dev_priv = dev->dev_private;
  9931. struct drm_framebuffer *old_fb = plane->fb;
  9932. struct intel_plane_state state;
  9933. struct intel_plane *intel_plane = to_intel_plane(plane);
  9934. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9935. int ret;
  9936. state.base.crtc = crtc ? crtc : plane->crtc;
  9937. state.base.fb = fb;
  9938. /* sample coordinates in 16.16 fixed point */
  9939. state.src.x1 = src_x;
  9940. state.src.x2 = src_x + src_w;
  9941. state.src.y1 = src_y;
  9942. state.src.y2 = src_y + src_h;
  9943. /* integer pixels */
  9944. state.dst.x1 = crtc_x;
  9945. state.dst.x2 = crtc_x + crtc_w;
  9946. state.dst.y1 = crtc_y;
  9947. state.dst.y2 = crtc_y + crtc_h;
  9948. state.clip.x1 = 0;
  9949. state.clip.y1 = 0;
  9950. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  9951. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  9952. state.orig_src = state.src;
  9953. state.orig_dst = state.dst;
  9954. ret = intel_plane->check_plane(plane, &state);
  9955. if (ret)
  9956. return ret;
  9957. if (fb != old_fb && fb) {
  9958. ret = intel_prepare_plane_fb(plane, fb);
  9959. if (ret)
  9960. return ret;
  9961. }
  9962. intel_runtime_pm_get(dev_priv);
  9963. intel_plane->commit_plane(plane, &state);
  9964. intel_runtime_pm_put(dev_priv);
  9965. if (fb != old_fb && old_fb) {
  9966. if (intel_crtc->active)
  9967. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9968. intel_cleanup_plane_fb(plane, old_fb);
  9969. }
  9970. plane->fb = fb;
  9971. return 0;
  9972. }
  9973. /**
  9974. * intel_disable_plane - disable a plane
  9975. * @plane: plane to disable
  9976. *
  9977. * General disable handler for all plane types.
  9978. */
  9979. int
  9980. intel_disable_plane(struct drm_plane *plane)
  9981. {
  9982. if (!plane->fb)
  9983. return 0;
  9984. if (WARN_ON(!plane->crtc))
  9985. return -EINVAL;
  9986. return plane->funcs->update_plane(plane, plane->crtc, NULL,
  9987. 0, 0, 0, 0, 0, 0, 0, 0);
  9988. }
  9989. /* Common destruction function for both primary and cursor planes */
  9990. static void intel_plane_destroy(struct drm_plane *plane)
  9991. {
  9992. struct intel_plane *intel_plane = to_intel_plane(plane);
  9993. drm_plane_cleanup(plane);
  9994. kfree(intel_plane);
  9995. }
  9996. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  9997. .update_plane = intel_update_plane,
  9998. .disable_plane = intel_disable_plane,
  9999. .destroy = intel_plane_destroy,
  10000. .set_property = intel_plane_set_property
  10001. };
  10002. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10003. int pipe)
  10004. {
  10005. struct intel_plane *primary;
  10006. const uint32_t *intel_primary_formats;
  10007. int num_formats;
  10008. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10009. if (primary == NULL)
  10010. return NULL;
  10011. primary->can_scale = false;
  10012. primary->max_downscale = 1;
  10013. primary->pipe = pipe;
  10014. primary->plane = pipe;
  10015. primary->rotation = BIT(DRM_ROTATE_0);
  10016. primary->check_plane = intel_check_primary_plane;
  10017. primary->commit_plane = intel_commit_primary_plane;
  10018. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10019. primary->plane = !pipe;
  10020. if (INTEL_INFO(dev)->gen <= 3) {
  10021. intel_primary_formats = intel_primary_formats_gen2;
  10022. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10023. } else {
  10024. intel_primary_formats = intel_primary_formats_gen4;
  10025. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10026. }
  10027. drm_universal_plane_init(dev, &primary->base, 0,
  10028. &intel_primary_plane_funcs,
  10029. intel_primary_formats, num_formats,
  10030. DRM_PLANE_TYPE_PRIMARY);
  10031. if (INTEL_INFO(dev)->gen >= 4) {
  10032. if (!dev->mode_config.rotation_property)
  10033. dev->mode_config.rotation_property =
  10034. drm_mode_create_rotation_property(dev,
  10035. BIT(DRM_ROTATE_0) |
  10036. BIT(DRM_ROTATE_180));
  10037. if (dev->mode_config.rotation_property)
  10038. drm_object_attach_property(&primary->base.base,
  10039. dev->mode_config.rotation_property,
  10040. primary->rotation);
  10041. }
  10042. return &primary->base;
  10043. }
  10044. static int
  10045. intel_check_cursor_plane(struct drm_plane *plane,
  10046. struct intel_plane_state *state)
  10047. {
  10048. struct drm_crtc *crtc = state->base.crtc;
  10049. struct drm_device *dev = crtc->dev;
  10050. struct drm_framebuffer *fb = state->base.fb;
  10051. struct drm_rect *dest = &state->dst;
  10052. struct drm_rect *src = &state->src;
  10053. const struct drm_rect *clip = &state->clip;
  10054. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10055. int crtc_w, crtc_h;
  10056. unsigned stride;
  10057. int ret;
  10058. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10059. src, dest, clip,
  10060. DRM_PLANE_HELPER_NO_SCALING,
  10061. DRM_PLANE_HELPER_NO_SCALING,
  10062. true, true, &state->visible);
  10063. if (ret)
  10064. return ret;
  10065. /* if we want to turn off the cursor ignore width and height */
  10066. if (!obj)
  10067. return 0;
  10068. /* Check for which cursor types we support */
  10069. crtc_w = drm_rect_width(&state->orig_dst);
  10070. crtc_h = drm_rect_height(&state->orig_dst);
  10071. if (!cursor_size_ok(dev, crtc_w, crtc_h)) {
  10072. DRM_DEBUG("Cursor dimension not supported\n");
  10073. return -EINVAL;
  10074. }
  10075. stride = roundup_pow_of_two(crtc_w) * 4;
  10076. if (obj->base.size < stride * crtc_h) {
  10077. DRM_DEBUG_KMS("buffer is too small\n");
  10078. return -ENOMEM;
  10079. }
  10080. if (fb == crtc->cursor->fb)
  10081. return 0;
  10082. /* we only need to pin inside GTT if cursor is non-phy */
  10083. mutex_lock(&dev->struct_mutex);
  10084. if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
  10085. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10086. ret = -EINVAL;
  10087. }
  10088. mutex_unlock(&dev->struct_mutex);
  10089. return ret;
  10090. }
  10091. static void
  10092. intel_commit_cursor_plane(struct drm_plane *plane,
  10093. struct intel_plane_state *state)
  10094. {
  10095. struct drm_crtc *crtc = state->base.crtc;
  10096. struct drm_device *dev = crtc->dev;
  10097. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10098. struct intel_plane *intel_plane = to_intel_plane(plane);
  10099. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10100. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  10101. enum pipe pipe = intel_crtc->pipe;
  10102. unsigned old_width;
  10103. uint32_t addr;
  10104. plane->fb = state->base.fb;
  10105. crtc->cursor_x = state->orig_dst.x1;
  10106. crtc->cursor_y = state->orig_dst.y1;
  10107. intel_plane->crtc_x = state->orig_dst.x1;
  10108. intel_plane->crtc_y = state->orig_dst.y1;
  10109. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  10110. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  10111. intel_plane->src_x = state->orig_src.x1;
  10112. intel_plane->src_y = state->orig_src.y1;
  10113. intel_plane->src_w = drm_rect_width(&state->orig_src);
  10114. intel_plane->src_h = drm_rect_height(&state->orig_src);
  10115. intel_plane->obj = obj;
  10116. if (intel_crtc->cursor_bo == obj)
  10117. goto update;
  10118. /*
  10119. * 'prepare' is only called when fb != NULL; we still need to update
  10120. * frontbuffer tracking for the 'disable' case here.
  10121. */
  10122. if (!obj) {
  10123. mutex_lock(&dev->struct_mutex);
  10124. i915_gem_track_fb(old_obj, NULL,
  10125. INTEL_FRONTBUFFER_CURSOR(pipe));
  10126. mutex_unlock(&dev->struct_mutex);
  10127. }
  10128. if (!obj)
  10129. addr = 0;
  10130. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10131. addr = i915_gem_obj_ggtt_offset(obj);
  10132. else
  10133. addr = obj->phys_handle->busaddr;
  10134. intel_crtc->cursor_addr = addr;
  10135. intel_crtc->cursor_bo = obj;
  10136. update:
  10137. old_width = intel_crtc->cursor_width;
  10138. intel_crtc->cursor_width = drm_rect_width(&state->orig_dst);
  10139. intel_crtc->cursor_height = drm_rect_height(&state->orig_dst);
  10140. if (intel_crtc->active) {
  10141. if (old_width != intel_crtc->cursor_width)
  10142. intel_update_watermarks(crtc);
  10143. intel_crtc_update_cursor(crtc, state->visible);
  10144. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
  10145. }
  10146. }
  10147. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10148. .update_plane = intel_update_plane,
  10149. .disable_plane = intel_disable_plane,
  10150. .destroy = intel_plane_destroy,
  10151. .set_property = intel_plane_set_property,
  10152. };
  10153. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10154. int pipe)
  10155. {
  10156. struct intel_plane *cursor;
  10157. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10158. if (cursor == NULL)
  10159. return NULL;
  10160. cursor->can_scale = false;
  10161. cursor->max_downscale = 1;
  10162. cursor->pipe = pipe;
  10163. cursor->plane = pipe;
  10164. cursor->rotation = BIT(DRM_ROTATE_0);
  10165. cursor->check_plane = intel_check_cursor_plane;
  10166. cursor->commit_plane = intel_commit_cursor_plane;
  10167. drm_universal_plane_init(dev, &cursor->base, 0,
  10168. &intel_cursor_plane_funcs,
  10169. intel_cursor_formats,
  10170. ARRAY_SIZE(intel_cursor_formats),
  10171. DRM_PLANE_TYPE_CURSOR);
  10172. if (INTEL_INFO(dev)->gen >= 4) {
  10173. if (!dev->mode_config.rotation_property)
  10174. dev->mode_config.rotation_property =
  10175. drm_mode_create_rotation_property(dev,
  10176. BIT(DRM_ROTATE_0) |
  10177. BIT(DRM_ROTATE_180));
  10178. if (dev->mode_config.rotation_property)
  10179. drm_object_attach_property(&cursor->base.base,
  10180. dev->mode_config.rotation_property,
  10181. cursor->rotation);
  10182. }
  10183. return &cursor->base;
  10184. }
  10185. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10186. {
  10187. struct drm_i915_private *dev_priv = dev->dev_private;
  10188. struct intel_crtc *intel_crtc;
  10189. struct drm_plane *primary = NULL;
  10190. struct drm_plane *cursor = NULL;
  10191. int i, ret;
  10192. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10193. if (intel_crtc == NULL)
  10194. return;
  10195. primary = intel_primary_plane_create(dev, pipe);
  10196. if (!primary)
  10197. goto fail;
  10198. cursor = intel_cursor_plane_create(dev, pipe);
  10199. if (!cursor)
  10200. goto fail;
  10201. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10202. cursor, &intel_crtc_funcs);
  10203. if (ret)
  10204. goto fail;
  10205. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10206. for (i = 0; i < 256; i++) {
  10207. intel_crtc->lut_r[i] = i;
  10208. intel_crtc->lut_g[i] = i;
  10209. intel_crtc->lut_b[i] = i;
  10210. }
  10211. /*
  10212. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10213. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10214. */
  10215. intel_crtc->pipe = pipe;
  10216. intel_crtc->plane = pipe;
  10217. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10218. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10219. intel_crtc->plane = !pipe;
  10220. }
  10221. intel_crtc->cursor_base = ~0;
  10222. intel_crtc->cursor_cntl = ~0;
  10223. intel_crtc->cursor_size = ~0;
  10224. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10225. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10226. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10227. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10228. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10229. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10230. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10231. return;
  10232. fail:
  10233. if (primary)
  10234. drm_plane_cleanup(primary);
  10235. if (cursor)
  10236. drm_plane_cleanup(cursor);
  10237. kfree(intel_crtc);
  10238. }
  10239. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10240. {
  10241. struct drm_encoder *encoder = connector->base.encoder;
  10242. struct drm_device *dev = connector->base.dev;
  10243. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10244. if (!encoder || WARN_ON(!encoder->crtc))
  10245. return INVALID_PIPE;
  10246. return to_intel_crtc(encoder->crtc)->pipe;
  10247. }
  10248. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10249. struct drm_file *file)
  10250. {
  10251. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10252. struct drm_crtc *drmmode_crtc;
  10253. struct intel_crtc *crtc;
  10254. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10255. return -ENODEV;
  10256. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10257. if (!drmmode_crtc) {
  10258. DRM_ERROR("no such CRTC id\n");
  10259. return -ENOENT;
  10260. }
  10261. crtc = to_intel_crtc(drmmode_crtc);
  10262. pipe_from_crtc_id->pipe = crtc->pipe;
  10263. return 0;
  10264. }
  10265. static int intel_encoder_clones(struct intel_encoder *encoder)
  10266. {
  10267. struct drm_device *dev = encoder->base.dev;
  10268. struct intel_encoder *source_encoder;
  10269. int index_mask = 0;
  10270. int entry = 0;
  10271. for_each_intel_encoder(dev, source_encoder) {
  10272. if (encoders_cloneable(encoder, source_encoder))
  10273. index_mask |= (1 << entry);
  10274. entry++;
  10275. }
  10276. return index_mask;
  10277. }
  10278. static bool has_edp_a(struct drm_device *dev)
  10279. {
  10280. struct drm_i915_private *dev_priv = dev->dev_private;
  10281. if (!IS_MOBILE(dev))
  10282. return false;
  10283. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10284. return false;
  10285. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10286. return false;
  10287. return true;
  10288. }
  10289. static bool intel_crt_present(struct drm_device *dev)
  10290. {
  10291. struct drm_i915_private *dev_priv = dev->dev_private;
  10292. if (INTEL_INFO(dev)->gen >= 9)
  10293. return false;
  10294. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10295. return false;
  10296. if (IS_CHERRYVIEW(dev))
  10297. return false;
  10298. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10299. return false;
  10300. return true;
  10301. }
  10302. static void intel_setup_outputs(struct drm_device *dev)
  10303. {
  10304. struct drm_i915_private *dev_priv = dev->dev_private;
  10305. struct intel_encoder *encoder;
  10306. bool dpd_is_edp = false;
  10307. intel_lvds_init(dev);
  10308. if (intel_crt_present(dev))
  10309. intel_crt_init(dev);
  10310. if (HAS_DDI(dev)) {
  10311. int found;
  10312. /* Haswell uses DDI functions to detect digital outputs */
  10313. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10314. /* DDI A only supports eDP */
  10315. if (found)
  10316. intel_ddi_init(dev, PORT_A);
  10317. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10318. * register */
  10319. found = I915_READ(SFUSE_STRAP);
  10320. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10321. intel_ddi_init(dev, PORT_B);
  10322. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10323. intel_ddi_init(dev, PORT_C);
  10324. if (found & SFUSE_STRAP_DDID_DETECTED)
  10325. intel_ddi_init(dev, PORT_D);
  10326. } else if (HAS_PCH_SPLIT(dev)) {
  10327. int found;
  10328. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10329. if (has_edp_a(dev))
  10330. intel_dp_init(dev, DP_A, PORT_A);
  10331. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10332. /* PCH SDVOB multiplex with HDMIB */
  10333. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10334. if (!found)
  10335. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10336. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10337. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10338. }
  10339. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10340. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10341. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10342. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10343. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10344. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10345. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10346. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10347. } else if (IS_VALLEYVIEW(dev)) {
  10348. /*
  10349. * The DP_DETECTED bit is the latched state of the DDC
  10350. * SDA pin at boot. However since eDP doesn't require DDC
  10351. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10352. * eDP ports may have been muxed to an alternate function.
  10353. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10354. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10355. * detect eDP ports.
  10356. */
  10357. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED)
  10358. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10359. PORT_B);
  10360. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10361. intel_dp_is_edp(dev, PORT_B))
  10362. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10363. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED)
  10364. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10365. PORT_C);
  10366. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10367. intel_dp_is_edp(dev, PORT_C))
  10368. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10369. if (IS_CHERRYVIEW(dev)) {
  10370. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10371. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10372. PORT_D);
  10373. /* eDP not supported on port D, so don't check VBT */
  10374. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10375. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10376. }
  10377. intel_dsi_init(dev);
  10378. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10379. bool found = false;
  10380. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10381. DRM_DEBUG_KMS("probing SDVOB\n");
  10382. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10383. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10384. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10385. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10386. }
  10387. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10388. intel_dp_init(dev, DP_B, PORT_B);
  10389. }
  10390. /* Before G4X SDVOC doesn't have its own detect register */
  10391. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10392. DRM_DEBUG_KMS("probing SDVOC\n");
  10393. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10394. }
  10395. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10396. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10397. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10398. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10399. }
  10400. if (SUPPORTS_INTEGRATED_DP(dev))
  10401. intel_dp_init(dev, DP_C, PORT_C);
  10402. }
  10403. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10404. (I915_READ(DP_D) & DP_DETECTED))
  10405. intel_dp_init(dev, DP_D, PORT_D);
  10406. } else if (IS_GEN2(dev))
  10407. intel_dvo_init(dev);
  10408. if (SUPPORTS_TV(dev))
  10409. intel_tv_init(dev);
  10410. intel_psr_init(dev);
  10411. for_each_intel_encoder(dev, encoder) {
  10412. encoder->base.possible_crtcs = encoder->crtc_mask;
  10413. encoder->base.possible_clones =
  10414. intel_encoder_clones(encoder);
  10415. }
  10416. intel_init_pch_refclk(dev);
  10417. drm_helper_move_panel_connectors_to_head(dev);
  10418. }
  10419. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10420. {
  10421. struct drm_device *dev = fb->dev;
  10422. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10423. drm_framebuffer_cleanup(fb);
  10424. mutex_lock(&dev->struct_mutex);
  10425. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10426. drm_gem_object_unreference(&intel_fb->obj->base);
  10427. mutex_unlock(&dev->struct_mutex);
  10428. kfree(intel_fb);
  10429. }
  10430. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10431. struct drm_file *file,
  10432. unsigned int *handle)
  10433. {
  10434. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10435. struct drm_i915_gem_object *obj = intel_fb->obj;
  10436. return drm_gem_handle_create(file, &obj->base, handle);
  10437. }
  10438. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10439. .destroy = intel_user_framebuffer_destroy,
  10440. .create_handle = intel_user_framebuffer_create_handle,
  10441. };
  10442. static int intel_framebuffer_init(struct drm_device *dev,
  10443. struct intel_framebuffer *intel_fb,
  10444. struct drm_mode_fb_cmd2 *mode_cmd,
  10445. struct drm_i915_gem_object *obj)
  10446. {
  10447. int aligned_height;
  10448. int pitch_limit;
  10449. int ret;
  10450. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10451. if (obj->tiling_mode == I915_TILING_Y) {
  10452. DRM_DEBUG("hardware does not support tiling Y\n");
  10453. return -EINVAL;
  10454. }
  10455. if (mode_cmd->pitches[0] & 63) {
  10456. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10457. mode_cmd->pitches[0]);
  10458. return -EINVAL;
  10459. }
  10460. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10461. pitch_limit = 32*1024;
  10462. } else if (INTEL_INFO(dev)->gen >= 4) {
  10463. if (obj->tiling_mode)
  10464. pitch_limit = 16*1024;
  10465. else
  10466. pitch_limit = 32*1024;
  10467. } else if (INTEL_INFO(dev)->gen >= 3) {
  10468. if (obj->tiling_mode)
  10469. pitch_limit = 8*1024;
  10470. else
  10471. pitch_limit = 16*1024;
  10472. } else
  10473. /* XXX DSPC is limited to 4k tiled */
  10474. pitch_limit = 8*1024;
  10475. if (mode_cmd->pitches[0] > pitch_limit) {
  10476. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10477. obj->tiling_mode ? "tiled" : "linear",
  10478. mode_cmd->pitches[0], pitch_limit);
  10479. return -EINVAL;
  10480. }
  10481. if (obj->tiling_mode != I915_TILING_NONE &&
  10482. mode_cmd->pitches[0] != obj->stride) {
  10483. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10484. mode_cmd->pitches[0], obj->stride);
  10485. return -EINVAL;
  10486. }
  10487. /* Reject formats not supported by any plane early. */
  10488. switch (mode_cmd->pixel_format) {
  10489. case DRM_FORMAT_C8:
  10490. case DRM_FORMAT_RGB565:
  10491. case DRM_FORMAT_XRGB8888:
  10492. case DRM_FORMAT_ARGB8888:
  10493. break;
  10494. case DRM_FORMAT_XRGB1555:
  10495. case DRM_FORMAT_ARGB1555:
  10496. if (INTEL_INFO(dev)->gen > 3) {
  10497. DRM_DEBUG("unsupported pixel format: %s\n",
  10498. drm_get_format_name(mode_cmd->pixel_format));
  10499. return -EINVAL;
  10500. }
  10501. break;
  10502. case DRM_FORMAT_XBGR8888:
  10503. case DRM_FORMAT_ABGR8888:
  10504. case DRM_FORMAT_XRGB2101010:
  10505. case DRM_FORMAT_ARGB2101010:
  10506. case DRM_FORMAT_XBGR2101010:
  10507. case DRM_FORMAT_ABGR2101010:
  10508. if (INTEL_INFO(dev)->gen < 4) {
  10509. DRM_DEBUG("unsupported pixel format: %s\n",
  10510. drm_get_format_name(mode_cmd->pixel_format));
  10511. return -EINVAL;
  10512. }
  10513. break;
  10514. case DRM_FORMAT_YUYV:
  10515. case DRM_FORMAT_UYVY:
  10516. case DRM_FORMAT_YVYU:
  10517. case DRM_FORMAT_VYUY:
  10518. if (INTEL_INFO(dev)->gen < 5) {
  10519. DRM_DEBUG("unsupported pixel format: %s\n",
  10520. drm_get_format_name(mode_cmd->pixel_format));
  10521. return -EINVAL;
  10522. }
  10523. break;
  10524. default:
  10525. DRM_DEBUG("unsupported pixel format: %s\n",
  10526. drm_get_format_name(mode_cmd->pixel_format));
  10527. return -EINVAL;
  10528. }
  10529. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10530. if (mode_cmd->offsets[0] != 0)
  10531. return -EINVAL;
  10532. aligned_height = intel_align_height(dev, mode_cmd->height,
  10533. obj->tiling_mode);
  10534. /* FIXME drm helper for size checks (especially planar formats)? */
  10535. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10536. return -EINVAL;
  10537. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10538. intel_fb->obj = obj;
  10539. intel_fb->obj->framebuffer_references++;
  10540. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10541. if (ret) {
  10542. DRM_ERROR("framebuffer init failed %d\n", ret);
  10543. return ret;
  10544. }
  10545. return 0;
  10546. }
  10547. static struct drm_framebuffer *
  10548. intel_user_framebuffer_create(struct drm_device *dev,
  10549. struct drm_file *filp,
  10550. struct drm_mode_fb_cmd2 *mode_cmd)
  10551. {
  10552. struct drm_i915_gem_object *obj;
  10553. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10554. mode_cmd->handles[0]));
  10555. if (&obj->base == NULL)
  10556. return ERR_PTR(-ENOENT);
  10557. return intel_framebuffer_create(dev, mode_cmd, obj);
  10558. }
  10559. #ifndef CONFIG_DRM_I915_FBDEV
  10560. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10561. {
  10562. }
  10563. #endif
  10564. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10565. .fb_create = intel_user_framebuffer_create,
  10566. .output_poll_changed = intel_fbdev_output_poll_changed,
  10567. };
  10568. /* Set up chip specific display functions */
  10569. static void intel_init_display(struct drm_device *dev)
  10570. {
  10571. struct drm_i915_private *dev_priv = dev->dev_private;
  10572. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10573. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10574. else if (IS_CHERRYVIEW(dev))
  10575. dev_priv->display.find_dpll = chv_find_best_dpll;
  10576. else if (IS_VALLEYVIEW(dev))
  10577. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10578. else if (IS_PINEVIEW(dev))
  10579. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10580. else
  10581. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10582. if (HAS_DDI(dev)) {
  10583. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10584. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10585. dev_priv->display.crtc_compute_clock =
  10586. haswell_crtc_compute_clock;
  10587. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10588. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10589. dev_priv->display.off = ironlake_crtc_off;
  10590. if (INTEL_INFO(dev)->gen >= 9)
  10591. dev_priv->display.update_primary_plane =
  10592. skylake_update_primary_plane;
  10593. else
  10594. dev_priv->display.update_primary_plane =
  10595. ironlake_update_primary_plane;
  10596. } else if (HAS_PCH_SPLIT(dev)) {
  10597. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10598. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10599. dev_priv->display.crtc_compute_clock =
  10600. ironlake_crtc_compute_clock;
  10601. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10602. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10603. dev_priv->display.off = ironlake_crtc_off;
  10604. dev_priv->display.update_primary_plane =
  10605. ironlake_update_primary_plane;
  10606. } else if (IS_VALLEYVIEW(dev)) {
  10607. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10608. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10609. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10610. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10611. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10612. dev_priv->display.off = i9xx_crtc_off;
  10613. dev_priv->display.update_primary_plane =
  10614. i9xx_update_primary_plane;
  10615. } else {
  10616. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10617. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10618. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10619. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10620. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10621. dev_priv->display.off = i9xx_crtc_off;
  10622. dev_priv->display.update_primary_plane =
  10623. i9xx_update_primary_plane;
  10624. }
  10625. /* Returns the core display clock speed */
  10626. if (IS_VALLEYVIEW(dev))
  10627. dev_priv->display.get_display_clock_speed =
  10628. valleyview_get_display_clock_speed;
  10629. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10630. dev_priv->display.get_display_clock_speed =
  10631. i945_get_display_clock_speed;
  10632. else if (IS_I915G(dev))
  10633. dev_priv->display.get_display_clock_speed =
  10634. i915_get_display_clock_speed;
  10635. else if (IS_I945GM(dev) || IS_845G(dev))
  10636. dev_priv->display.get_display_clock_speed =
  10637. i9xx_misc_get_display_clock_speed;
  10638. else if (IS_PINEVIEW(dev))
  10639. dev_priv->display.get_display_clock_speed =
  10640. pnv_get_display_clock_speed;
  10641. else if (IS_I915GM(dev))
  10642. dev_priv->display.get_display_clock_speed =
  10643. i915gm_get_display_clock_speed;
  10644. else if (IS_I865G(dev))
  10645. dev_priv->display.get_display_clock_speed =
  10646. i865_get_display_clock_speed;
  10647. else if (IS_I85X(dev))
  10648. dev_priv->display.get_display_clock_speed =
  10649. i855_get_display_clock_speed;
  10650. else /* 852, 830 */
  10651. dev_priv->display.get_display_clock_speed =
  10652. i830_get_display_clock_speed;
  10653. if (IS_GEN5(dev)) {
  10654. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10655. } else if (IS_GEN6(dev)) {
  10656. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10657. } else if (IS_IVYBRIDGE(dev)) {
  10658. /* FIXME: detect B0+ stepping and use auto training */
  10659. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10660. dev_priv->display.modeset_global_resources =
  10661. ivb_modeset_global_resources;
  10662. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10663. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10664. } else if (IS_VALLEYVIEW(dev)) {
  10665. dev_priv->display.modeset_global_resources =
  10666. valleyview_modeset_global_resources;
  10667. }
  10668. /* Default just returns -ENODEV to indicate unsupported */
  10669. dev_priv->display.queue_flip = intel_default_queue_flip;
  10670. switch (INTEL_INFO(dev)->gen) {
  10671. case 2:
  10672. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10673. break;
  10674. case 3:
  10675. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10676. break;
  10677. case 4:
  10678. case 5:
  10679. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10680. break;
  10681. case 6:
  10682. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10683. break;
  10684. case 7:
  10685. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10686. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10687. break;
  10688. case 9:
  10689. dev_priv->display.queue_flip = intel_gen9_queue_flip;
  10690. break;
  10691. }
  10692. intel_panel_init_backlight_funcs(dev);
  10693. mutex_init(&dev_priv->pps_mutex);
  10694. }
  10695. /*
  10696. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10697. * resume, or other times. This quirk makes sure that's the case for
  10698. * affected systems.
  10699. */
  10700. static void quirk_pipea_force(struct drm_device *dev)
  10701. {
  10702. struct drm_i915_private *dev_priv = dev->dev_private;
  10703. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10704. DRM_INFO("applying pipe a force quirk\n");
  10705. }
  10706. static void quirk_pipeb_force(struct drm_device *dev)
  10707. {
  10708. struct drm_i915_private *dev_priv = dev->dev_private;
  10709. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10710. DRM_INFO("applying pipe b force quirk\n");
  10711. }
  10712. /*
  10713. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10714. */
  10715. static void quirk_ssc_force_disable(struct drm_device *dev)
  10716. {
  10717. struct drm_i915_private *dev_priv = dev->dev_private;
  10718. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10719. DRM_INFO("applying lvds SSC disable quirk\n");
  10720. }
  10721. /*
  10722. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10723. * brightness value
  10724. */
  10725. static void quirk_invert_brightness(struct drm_device *dev)
  10726. {
  10727. struct drm_i915_private *dev_priv = dev->dev_private;
  10728. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10729. DRM_INFO("applying inverted panel brightness quirk\n");
  10730. }
  10731. /* Some VBT's incorrectly indicate no backlight is present */
  10732. static void quirk_backlight_present(struct drm_device *dev)
  10733. {
  10734. struct drm_i915_private *dev_priv = dev->dev_private;
  10735. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10736. DRM_INFO("applying backlight present quirk\n");
  10737. }
  10738. struct intel_quirk {
  10739. int device;
  10740. int subsystem_vendor;
  10741. int subsystem_device;
  10742. void (*hook)(struct drm_device *dev);
  10743. };
  10744. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10745. struct intel_dmi_quirk {
  10746. void (*hook)(struct drm_device *dev);
  10747. const struct dmi_system_id (*dmi_id_list)[];
  10748. };
  10749. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10750. {
  10751. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10752. return 1;
  10753. }
  10754. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10755. {
  10756. .dmi_id_list = &(const struct dmi_system_id[]) {
  10757. {
  10758. .callback = intel_dmi_reverse_brightness,
  10759. .ident = "NCR Corporation",
  10760. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10761. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10762. },
  10763. },
  10764. { } /* terminating entry */
  10765. },
  10766. .hook = quirk_invert_brightness,
  10767. },
  10768. };
  10769. static struct intel_quirk intel_quirks[] = {
  10770. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10771. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10772. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10773. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10774. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10775. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10776. /* 830 needs to leave pipe A & dpll A up */
  10777. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10778. /* 830 needs to leave pipe B & dpll B up */
  10779. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10780. /* Lenovo U160 cannot use SSC on LVDS */
  10781. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10782. /* Sony Vaio Y cannot use SSC on LVDS */
  10783. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10784. /* Acer Aspire 5734Z must invert backlight brightness */
  10785. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10786. /* Acer/eMachines G725 */
  10787. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10788. /* Acer/eMachines e725 */
  10789. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10790. /* Acer/Packard Bell NCL20 */
  10791. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10792. /* Acer Aspire 4736Z */
  10793. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10794. /* Acer Aspire 5336 */
  10795. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10796. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10797. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10798. /* Acer C720 Chromebook (Core i3 4005U) */
  10799. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10800. /* Apple Macbook 2,1 (Core 2 T7400) */
  10801. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  10802. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10803. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10804. /* HP Chromebook 14 (Celeron 2955U) */
  10805. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10806. };
  10807. static void intel_init_quirks(struct drm_device *dev)
  10808. {
  10809. struct pci_dev *d = dev->pdev;
  10810. int i;
  10811. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10812. struct intel_quirk *q = &intel_quirks[i];
  10813. if (d->device == q->device &&
  10814. (d->subsystem_vendor == q->subsystem_vendor ||
  10815. q->subsystem_vendor == PCI_ANY_ID) &&
  10816. (d->subsystem_device == q->subsystem_device ||
  10817. q->subsystem_device == PCI_ANY_ID))
  10818. q->hook(dev);
  10819. }
  10820. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10821. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10822. intel_dmi_quirks[i].hook(dev);
  10823. }
  10824. }
  10825. /* Disable the VGA plane that we never use */
  10826. static void i915_disable_vga(struct drm_device *dev)
  10827. {
  10828. struct drm_i915_private *dev_priv = dev->dev_private;
  10829. u8 sr1;
  10830. u32 vga_reg = i915_vgacntrl_reg(dev);
  10831. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10832. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10833. outb(SR01, VGA_SR_INDEX);
  10834. sr1 = inb(VGA_SR_DATA);
  10835. outb(sr1 | 1<<5, VGA_SR_DATA);
  10836. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10837. udelay(300);
  10838. /*
  10839. * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
  10840. * from S3 without preserving (some of?) the other bits.
  10841. */
  10842. I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
  10843. POSTING_READ(vga_reg);
  10844. }
  10845. void intel_modeset_init_hw(struct drm_device *dev)
  10846. {
  10847. intel_prepare_ddi(dev);
  10848. if (IS_VALLEYVIEW(dev))
  10849. vlv_update_cdclk(dev);
  10850. intel_init_clock_gating(dev);
  10851. intel_enable_gt_powersave(dev);
  10852. }
  10853. void intel_modeset_init(struct drm_device *dev)
  10854. {
  10855. struct drm_i915_private *dev_priv = dev->dev_private;
  10856. int sprite, ret;
  10857. enum pipe pipe;
  10858. struct intel_crtc *crtc;
  10859. drm_mode_config_init(dev);
  10860. dev->mode_config.min_width = 0;
  10861. dev->mode_config.min_height = 0;
  10862. dev->mode_config.preferred_depth = 24;
  10863. dev->mode_config.prefer_shadow = 1;
  10864. dev->mode_config.funcs = &intel_mode_funcs;
  10865. intel_init_quirks(dev);
  10866. intel_init_pm(dev);
  10867. if (INTEL_INFO(dev)->num_pipes == 0)
  10868. return;
  10869. intel_init_display(dev);
  10870. intel_init_audio(dev);
  10871. if (IS_GEN2(dev)) {
  10872. dev->mode_config.max_width = 2048;
  10873. dev->mode_config.max_height = 2048;
  10874. } else if (IS_GEN3(dev)) {
  10875. dev->mode_config.max_width = 4096;
  10876. dev->mode_config.max_height = 4096;
  10877. } else {
  10878. dev->mode_config.max_width = 8192;
  10879. dev->mode_config.max_height = 8192;
  10880. }
  10881. if (IS_845G(dev) || IS_I865G(dev)) {
  10882. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  10883. dev->mode_config.cursor_height = 1023;
  10884. } else if (IS_GEN2(dev)) {
  10885. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10886. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10887. } else {
  10888. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10889. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10890. }
  10891. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10892. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10893. INTEL_INFO(dev)->num_pipes,
  10894. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10895. for_each_pipe(dev_priv, pipe) {
  10896. intel_crtc_init(dev, pipe);
  10897. for_each_sprite(pipe, sprite) {
  10898. ret = intel_plane_init(dev, pipe, sprite);
  10899. if (ret)
  10900. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10901. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10902. }
  10903. }
  10904. intel_init_dpio(dev);
  10905. intel_shared_dpll_init(dev);
  10906. /* save the BIOS value before clobbering it */
  10907. dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
  10908. /* Just disable it once at startup */
  10909. i915_disable_vga(dev);
  10910. intel_setup_outputs(dev);
  10911. /* Just in case the BIOS is doing something questionable. */
  10912. intel_fbc_disable(dev);
  10913. drm_modeset_lock_all(dev);
  10914. intel_modeset_setup_hw_state(dev, false);
  10915. drm_modeset_unlock_all(dev);
  10916. for_each_intel_crtc(dev, crtc) {
  10917. if (!crtc->active)
  10918. continue;
  10919. /*
  10920. * Note that reserving the BIOS fb up front prevents us
  10921. * from stuffing other stolen allocations like the ring
  10922. * on top. This prevents some ugliness at boot time, and
  10923. * can even allow for smooth boot transitions if the BIOS
  10924. * fb is large enough for the active pipe configuration.
  10925. */
  10926. if (dev_priv->display.get_plane_config) {
  10927. dev_priv->display.get_plane_config(crtc,
  10928. &crtc->plane_config);
  10929. /*
  10930. * If the fb is shared between multiple heads, we'll
  10931. * just get the first one.
  10932. */
  10933. intel_find_plane_obj(crtc, &crtc->plane_config);
  10934. }
  10935. }
  10936. }
  10937. static void intel_enable_pipe_a(struct drm_device *dev)
  10938. {
  10939. struct intel_connector *connector;
  10940. struct drm_connector *crt = NULL;
  10941. struct intel_load_detect_pipe load_detect_temp;
  10942. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  10943. /* We can't just switch on the pipe A, we need to set things up with a
  10944. * proper mode and output configuration. As a gross hack, enable pipe A
  10945. * by enabling the load detect pipe once. */
  10946. list_for_each_entry(connector,
  10947. &dev->mode_config.connector_list,
  10948. base.head) {
  10949. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10950. crt = &connector->base;
  10951. break;
  10952. }
  10953. }
  10954. if (!crt)
  10955. return;
  10956. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  10957. intel_release_load_detect_pipe(crt, &load_detect_temp);
  10958. }
  10959. static bool
  10960. intel_check_plane_mapping(struct intel_crtc *crtc)
  10961. {
  10962. struct drm_device *dev = crtc->base.dev;
  10963. struct drm_i915_private *dev_priv = dev->dev_private;
  10964. u32 reg, val;
  10965. if (INTEL_INFO(dev)->num_pipes == 1)
  10966. return true;
  10967. reg = DSPCNTR(!crtc->plane);
  10968. val = I915_READ(reg);
  10969. if ((val & DISPLAY_PLANE_ENABLE) &&
  10970. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  10971. return false;
  10972. return true;
  10973. }
  10974. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  10975. {
  10976. struct drm_device *dev = crtc->base.dev;
  10977. struct drm_i915_private *dev_priv = dev->dev_private;
  10978. u32 reg;
  10979. /* Clear any frame start delays used for debugging left by the BIOS */
  10980. reg = PIPECONF(crtc->config.cpu_transcoder);
  10981. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  10982. /* restore vblank interrupts to correct state */
  10983. if (crtc->active) {
  10984. update_scanline_offset(crtc);
  10985. drm_vblank_on(dev, crtc->pipe);
  10986. } else
  10987. drm_vblank_off(dev, crtc->pipe);
  10988. /* We need to sanitize the plane -> pipe mapping first because this will
  10989. * disable the crtc (and hence change the state) if it is wrong. Note
  10990. * that gen4+ has a fixed plane -> pipe mapping. */
  10991. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  10992. struct intel_connector *connector;
  10993. bool plane;
  10994. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  10995. crtc->base.base.id);
  10996. /* Pipe has the wrong plane attached and the plane is active.
  10997. * Temporarily change the plane mapping and disable everything
  10998. * ... */
  10999. plane = crtc->plane;
  11000. crtc->plane = !plane;
  11001. crtc->primary_enabled = true;
  11002. dev_priv->display.crtc_disable(&crtc->base);
  11003. crtc->plane = plane;
  11004. /* ... and break all links. */
  11005. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11006. base.head) {
  11007. if (connector->encoder->base.crtc != &crtc->base)
  11008. continue;
  11009. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11010. connector->base.encoder = NULL;
  11011. }
  11012. /* multiple connectors may have the same encoder:
  11013. * handle them and break crtc link separately */
  11014. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11015. base.head)
  11016. if (connector->encoder->base.crtc == &crtc->base) {
  11017. connector->encoder->base.crtc = NULL;
  11018. connector->encoder->connectors_active = false;
  11019. }
  11020. WARN_ON(crtc->active);
  11021. crtc->base.enabled = false;
  11022. }
  11023. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11024. crtc->pipe == PIPE_A && !crtc->active) {
  11025. /* BIOS forgot to enable pipe A, this mostly happens after
  11026. * resume. Force-enable the pipe to fix this, the update_dpms
  11027. * call below we restore the pipe to the right state, but leave
  11028. * the required bits on. */
  11029. intel_enable_pipe_a(dev);
  11030. }
  11031. /* Adjust the state of the output pipe according to whether we
  11032. * have active connectors/encoders. */
  11033. intel_crtc_update_dpms(&crtc->base);
  11034. if (crtc->active != crtc->base.enabled) {
  11035. struct intel_encoder *encoder;
  11036. /* This can happen either due to bugs in the get_hw_state
  11037. * functions or because the pipe is force-enabled due to the
  11038. * pipe A quirk. */
  11039. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11040. crtc->base.base.id,
  11041. crtc->base.enabled ? "enabled" : "disabled",
  11042. crtc->active ? "enabled" : "disabled");
  11043. crtc->base.enabled = crtc->active;
  11044. /* Because we only establish the connector -> encoder ->
  11045. * crtc links if something is active, this means the
  11046. * crtc is now deactivated. Break the links. connector
  11047. * -> encoder links are only establish when things are
  11048. * actually up, hence no need to break them. */
  11049. WARN_ON(crtc->active);
  11050. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11051. WARN_ON(encoder->connectors_active);
  11052. encoder->base.crtc = NULL;
  11053. }
  11054. }
  11055. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11056. /*
  11057. * We start out with underrun reporting disabled to avoid races.
  11058. * For correct bookkeeping mark this on active crtcs.
  11059. *
  11060. * Also on gmch platforms we dont have any hardware bits to
  11061. * disable the underrun reporting. Which means we need to start
  11062. * out with underrun reporting disabled also on inactive pipes,
  11063. * since otherwise we'll complain about the garbage we read when
  11064. * e.g. coming up after runtime pm.
  11065. *
  11066. * No protection against concurrent access is required - at
  11067. * worst a fifo underrun happens which also sets this to false.
  11068. */
  11069. crtc->cpu_fifo_underrun_disabled = true;
  11070. crtc->pch_fifo_underrun_disabled = true;
  11071. }
  11072. }
  11073. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11074. {
  11075. struct intel_connector *connector;
  11076. struct drm_device *dev = encoder->base.dev;
  11077. /* We need to check both for a crtc link (meaning that the
  11078. * encoder is active and trying to read from a pipe) and the
  11079. * pipe itself being active. */
  11080. bool has_active_crtc = encoder->base.crtc &&
  11081. to_intel_crtc(encoder->base.crtc)->active;
  11082. if (encoder->connectors_active && !has_active_crtc) {
  11083. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11084. encoder->base.base.id,
  11085. encoder->base.name);
  11086. /* Connector is active, but has no active pipe. This is
  11087. * fallout from our resume register restoring. Disable
  11088. * the encoder manually again. */
  11089. if (encoder->base.crtc) {
  11090. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11091. encoder->base.base.id,
  11092. encoder->base.name);
  11093. encoder->disable(encoder);
  11094. if (encoder->post_disable)
  11095. encoder->post_disable(encoder);
  11096. }
  11097. encoder->base.crtc = NULL;
  11098. encoder->connectors_active = false;
  11099. /* Inconsistent output/port/pipe state happens presumably due to
  11100. * a bug in one of the get_hw_state functions. Or someplace else
  11101. * in our code, like the register restore mess on resume. Clamp
  11102. * things to off as a safer default. */
  11103. list_for_each_entry(connector,
  11104. &dev->mode_config.connector_list,
  11105. base.head) {
  11106. if (connector->encoder != encoder)
  11107. continue;
  11108. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11109. connector->base.encoder = NULL;
  11110. }
  11111. }
  11112. /* Enabled encoders without active connectors will be fixed in
  11113. * the crtc fixup. */
  11114. }
  11115. void i915_redisable_vga_power_on(struct drm_device *dev)
  11116. {
  11117. struct drm_i915_private *dev_priv = dev->dev_private;
  11118. u32 vga_reg = i915_vgacntrl_reg(dev);
  11119. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11120. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11121. i915_disable_vga(dev);
  11122. }
  11123. }
  11124. void i915_redisable_vga(struct drm_device *dev)
  11125. {
  11126. struct drm_i915_private *dev_priv = dev->dev_private;
  11127. /* This function can be called both from intel_modeset_setup_hw_state or
  11128. * at a very early point in our resume sequence, where the power well
  11129. * structures are not yet restored. Since this function is at a very
  11130. * paranoid "someone might have enabled VGA while we were not looking"
  11131. * level, just check if the power well is enabled instead of trying to
  11132. * follow the "don't touch the power well if we don't need it" policy
  11133. * the rest of the driver uses. */
  11134. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11135. return;
  11136. i915_redisable_vga_power_on(dev);
  11137. }
  11138. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11139. {
  11140. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11141. if (!crtc->active)
  11142. return false;
  11143. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11144. }
  11145. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11146. {
  11147. struct drm_i915_private *dev_priv = dev->dev_private;
  11148. enum pipe pipe;
  11149. struct intel_crtc *crtc;
  11150. struct intel_encoder *encoder;
  11151. struct intel_connector *connector;
  11152. int i;
  11153. for_each_intel_crtc(dev, crtc) {
  11154. memset(&crtc->config, 0, sizeof(crtc->config));
  11155. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11156. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11157. &crtc->config);
  11158. crtc->base.enabled = crtc->active;
  11159. crtc->primary_enabled = primary_get_hw_state(crtc);
  11160. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11161. crtc->base.base.id,
  11162. crtc->active ? "enabled" : "disabled");
  11163. }
  11164. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11165. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11166. pll->on = pll->get_hw_state(dev_priv, pll,
  11167. &pll->config.hw_state);
  11168. pll->active = 0;
  11169. pll->config.crtc_mask = 0;
  11170. for_each_intel_crtc(dev, crtc) {
  11171. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11172. pll->active++;
  11173. pll->config.crtc_mask |= 1 << crtc->pipe;
  11174. }
  11175. }
  11176. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11177. pll->name, pll->config.crtc_mask, pll->on);
  11178. if (pll->config.crtc_mask)
  11179. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11180. }
  11181. for_each_intel_encoder(dev, encoder) {
  11182. pipe = 0;
  11183. if (encoder->get_hw_state(encoder, &pipe)) {
  11184. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11185. encoder->base.crtc = &crtc->base;
  11186. encoder->get_config(encoder, &crtc->config);
  11187. } else {
  11188. encoder->base.crtc = NULL;
  11189. }
  11190. encoder->connectors_active = false;
  11191. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11192. encoder->base.base.id,
  11193. encoder->base.name,
  11194. encoder->base.crtc ? "enabled" : "disabled",
  11195. pipe_name(pipe));
  11196. }
  11197. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11198. base.head) {
  11199. if (connector->get_hw_state(connector)) {
  11200. connector->base.dpms = DRM_MODE_DPMS_ON;
  11201. connector->encoder->connectors_active = true;
  11202. connector->base.encoder = &connector->encoder->base;
  11203. } else {
  11204. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11205. connector->base.encoder = NULL;
  11206. }
  11207. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11208. connector->base.base.id,
  11209. connector->base.name,
  11210. connector->base.encoder ? "enabled" : "disabled");
  11211. }
  11212. }
  11213. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11214. * and i915 state tracking structures. */
  11215. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11216. bool force_restore)
  11217. {
  11218. struct drm_i915_private *dev_priv = dev->dev_private;
  11219. enum pipe pipe;
  11220. struct intel_crtc *crtc;
  11221. struct intel_encoder *encoder;
  11222. int i;
  11223. intel_modeset_readout_hw_state(dev);
  11224. /*
  11225. * Now that we have the config, copy it to each CRTC struct
  11226. * Note that this could go away if we move to using crtc_config
  11227. * checking everywhere.
  11228. */
  11229. for_each_intel_crtc(dev, crtc) {
  11230. if (crtc->active && i915.fastboot) {
  11231. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  11232. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11233. crtc->base.base.id);
  11234. drm_mode_debug_printmodeline(&crtc->base.mode);
  11235. }
  11236. }
  11237. /* HW state is read out, now we need to sanitize this mess. */
  11238. for_each_intel_encoder(dev, encoder) {
  11239. intel_sanitize_encoder(encoder);
  11240. }
  11241. for_each_pipe(dev_priv, pipe) {
  11242. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11243. intel_sanitize_crtc(crtc);
  11244. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  11245. }
  11246. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11247. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11248. if (!pll->on || pll->active)
  11249. continue;
  11250. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11251. pll->disable(dev_priv, pll);
  11252. pll->on = false;
  11253. }
  11254. if (IS_GEN9(dev))
  11255. skl_wm_get_hw_state(dev);
  11256. else if (HAS_PCH_SPLIT(dev))
  11257. ilk_wm_get_hw_state(dev);
  11258. if (force_restore) {
  11259. i915_redisable_vga(dev);
  11260. /*
  11261. * We need to use raw interfaces for restoring state to avoid
  11262. * checking (bogus) intermediate states.
  11263. */
  11264. for_each_pipe(dev_priv, pipe) {
  11265. struct drm_crtc *crtc =
  11266. dev_priv->pipe_to_crtc_mapping[pipe];
  11267. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11268. crtc->primary->fb);
  11269. }
  11270. } else {
  11271. intel_modeset_update_staged_output_state(dev);
  11272. }
  11273. intel_modeset_check_state(dev);
  11274. }
  11275. void intel_modeset_gem_init(struct drm_device *dev)
  11276. {
  11277. struct drm_i915_private *dev_priv = dev->dev_private;
  11278. struct drm_crtc *c;
  11279. struct drm_i915_gem_object *obj;
  11280. mutex_lock(&dev->struct_mutex);
  11281. intel_init_gt_powersave(dev);
  11282. mutex_unlock(&dev->struct_mutex);
  11283. /*
  11284. * There may be no VBT; and if the BIOS enabled SSC we can
  11285. * just keep using it to avoid unnecessary flicker. Whereas if the
  11286. * BIOS isn't using it, don't assume it will work even if the VBT
  11287. * indicates as much.
  11288. */
  11289. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11290. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11291. DREF_SSC1_ENABLE);
  11292. intel_modeset_init_hw(dev);
  11293. intel_setup_overlay(dev);
  11294. /*
  11295. * Make sure any fbs we allocated at startup are properly
  11296. * pinned & fenced. When we do the allocation it's too early
  11297. * for this.
  11298. */
  11299. mutex_lock(&dev->struct_mutex);
  11300. for_each_crtc(dev, c) {
  11301. obj = intel_fb_obj(c->primary->fb);
  11302. if (obj == NULL)
  11303. continue;
  11304. if (intel_pin_and_fence_fb_obj(c->primary,
  11305. c->primary->fb,
  11306. NULL)) {
  11307. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11308. to_intel_crtc(c)->pipe);
  11309. drm_framebuffer_unreference(c->primary->fb);
  11310. c->primary->fb = NULL;
  11311. }
  11312. }
  11313. mutex_unlock(&dev->struct_mutex);
  11314. intel_backlight_register(dev);
  11315. }
  11316. void intel_connector_unregister(struct intel_connector *intel_connector)
  11317. {
  11318. struct drm_connector *connector = &intel_connector->base;
  11319. intel_panel_destroy_backlight(connector);
  11320. drm_connector_unregister(connector);
  11321. }
  11322. void intel_modeset_cleanup(struct drm_device *dev)
  11323. {
  11324. struct drm_i915_private *dev_priv = dev->dev_private;
  11325. struct drm_connector *connector;
  11326. intel_disable_gt_powersave(dev);
  11327. intel_backlight_unregister(dev);
  11328. /*
  11329. * Interrupts and polling as the first thing to avoid creating havoc.
  11330. * Too much stuff here (turning of connectors, ...) would
  11331. * experience fancy races otherwise.
  11332. */
  11333. intel_irq_uninstall(dev_priv);
  11334. /*
  11335. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11336. * poll handlers. Hence disable polling after hpd handling is shut down.
  11337. */
  11338. drm_kms_helper_poll_fini(dev);
  11339. mutex_lock(&dev->struct_mutex);
  11340. intel_unregister_dsm_handler();
  11341. intel_fbc_disable(dev);
  11342. ironlake_teardown_rc6(dev);
  11343. mutex_unlock(&dev->struct_mutex);
  11344. /* flush any delayed tasks or pending work */
  11345. flush_scheduled_work();
  11346. /* destroy the backlight and sysfs files before encoders/connectors */
  11347. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11348. struct intel_connector *intel_connector;
  11349. intel_connector = to_intel_connector(connector);
  11350. intel_connector->unregister(intel_connector);
  11351. }
  11352. drm_mode_config_cleanup(dev);
  11353. intel_cleanup_overlay(dev);
  11354. mutex_lock(&dev->struct_mutex);
  11355. intel_cleanup_gt_powersave(dev);
  11356. mutex_unlock(&dev->struct_mutex);
  11357. }
  11358. /*
  11359. * Return which encoder is currently attached for connector.
  11360. */
  11361. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11362. {
  11363. return &intel_attached_encoder(connector)->base;
  11364. }
  11365. void intel_connector_attach_encoder(struct intel_connector *connector,
  11366. struct intel_encoder *encoder)
  11367. {
  11368. connector->encoder = encoder;
  11369. drm_mode_connector_attach_encoder(&connector->base,
  11370. &encoder->base);
  11371. }
  11372. /*
  11373. * set vga decode state - true == enable VGA decode
  11374. */
  11375. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11376. {
  11377. struct drm_i915_private *dev_priv = dev->dev_private;
  11378. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11379. u16 gmch_ctrl;
  11380. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11381. DRM_ERROR("failed to read control word\n");
  11382. return -EIO;
  11383. }
  11384. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11385. return 0;
  11386. if (state)
  11387. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11388. else
  11389. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11390. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11391. DRM_ERROR("failed to write control word\n");
  11392. return -EIO;
  11393. }
  11394. return 0;
  11395. }
  11396. struct intel_display_error_state {
  11397. u32 power_well_driver;
  11398. int num_transcoders;
  11399. struct intel_cursor_error_state {
  11400. u32 control;
  11401. u32 position;
  11402. u32 base;
  11403. u32 size;
  11404. } cursor[I915_MAX_PIPES];
  11405. struct intel_pipe_error_state {
  11406. bool power_domain_on;
  11407. u32 source;
  11408. u32 stat;
  11409. } pipe[I915_MAX_PIPES];
  11410. struct intel_plane_error_state {
  11411. u32 control;
  11412. u32 stride;
  11413. u32 size;
  11414. u32 pos;
  11415. u32 addr;
  11416. u32 surface;
  11417. u32 tile_offset;
  11418. } plane[I915_MAX_PIPES];
  11419. struct intel_transcoder_error_state {
  11420. bool power_domain_on;
  11421. enum transcoder cpu_transcoder;
  11422. u32 conf;
  11423. u32 htotal;
  11424. u32 hblank;
  11425. u32 hsync;
  11426. u32 vtotal;
  11427. u32 vblank;
  11428. u32 vsync;
  11429. } transcoder[4];
  11430. };
  11431. struct intel_display_error_state *
  11432. intel_display_capture_error_state(struct drm_device *dev)
  11433. {
  11434. struct drm_i915_private *dev_priv = dev->dev_private;
  11435. struct intel_display_error_state *error;
  11436. int transcoders[] = {
  11437. TRANSCODER_A,
  11438. TRANSCODER_B,
  11439. TRANSCODER_C,
  11440. TRANSCODER_EDP,
  11441. };
  11442. int i;
  11443. if (INTEL_INFO(dev)->num_pipes == 0)
  11444. return NULL;
  11445. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11446. if (error == NULL)
  11447. return NULL;
  11448. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11449. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11450. for_each_pipe(dev_priv, i) {
  11451. error->pipe[i].power_domain_on =
  11452. __intel_display_power_is_enabled(dev_priv,
  11453. POWER_DOMAIN_PIPE(i));
  11454. if (!error->pipe[i].power_domain_on)
  11455. continue;
  11456. error->cursor[i].control = I915_READ(CURCNTR(i));
  11457. error->cursor[i].position = I915_READ(CURPOS(i));
  11458. error->cursor[i].base = I915_READ(CURBASE(i));
  11459. error->plane[i].control = I915_READ(DSPCNTR(i));
  11460. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11461. if (INTEL_INFO(dev)->gen <= 3) {
  11462. error->plane[i].size = I915_READ(DSPSIZE(i));
  11463. error->plane[i].pos = I915_READ(DSPPOS(i));
  11464. }
  11465. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11466. error->plane[i].addr = I915_READ(DSPADDR(i));
  11467. if (INTEL_INFO(dev)->gen >= 4) {
  11468. error->plane[i].surface = I915_READ(DSPSURF(i));
  11469. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11470. }
  11471. error->pipe[i].source = I915_READ(PIPESRC(i));
  11472. if (HAS_GMCH_DISPLAY(dev))
  11473. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11474. }
  11475. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11476. if (HAS_DDI(dev_priv->dev))
  11477. error->num_transcoders++; /* Account for eDP. */
  11478. for (i = 0; i < error->num_transcoders; i++) {
  11479. enum transcoder cpu_transcoder = transcoders[i];
  11480. error->transcoder[i].power_domain_on =
  11481. __intel_display_power_is_enabled(dev_priv,
  11482. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11483. if (!error->transcoder[i].power_domain_on)
  11484. continue;
  11485. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11486. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11487. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11488. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11489. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11490. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11491. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11492. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11493. }
  11494. return error;
  11495. }
  11496. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11497. void
  11498. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11499. struct drm_device *dev,
  11500. struct intel_display_error_state *error)
  11501. {
  11502. struct drm_i915_private *dev_priv = dev->dev_private;
  11503. int i;
  11504. if (!error)
  11505. return;
  11506. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11507. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11508. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11509. error->power_well_driver);
  11510. for_each_pipe(dev_priv, i) {
  11511. err_printf(m, "Pipe [%d]:\n", i);
  11512. err_printf(m, " Power: %s\n",
  11513. error->pipe[i].power_domain_on ? "on" : "off");
  11514. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11515. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11516. err_printf(m, "Plane [%d]:\n", i);
  11517. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11518. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11519. if (INTEL_INFO(dev)->gen <= 3) {
  11520. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11521. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11522. }
  11523. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11524. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11525. if (INTEL_INFO(dev)->gen >= 4) {
  11526. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11527. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11528. }
  11529. err_printf(m, "Cursor [%d]:\n", i);
  11530. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11531. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11532. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11533. }
  11534. for (i = 0; i < error->num_transcoders; i++) {
  11535. err_printf(m, "CPU transcoder: %c\n",
  11536. transcoder_name(error->transcoder[i].cpu_transcoder));
  11537. err_printf(m, " Power: %s\n",
  11538. error->transcoder[i].power_domain_on ? "on" : "off");
  11539. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11540. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11541. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11542. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11543. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11544. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11545. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11546. }
  11547. }
  11548. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11549. {
  11550. struct intel_crtc *crtc;
  11551. for_each_intel_crtc(dev, crtc) {
  11552. struct intel_unpin_work *work;
  11553. spin_lock_irq(&dev->event_lock);
  11554. work = crtc->unpin_work;
  11555. if (work && work->event &&
  11556. work->event->base.file_priv == file) {
  11557. kfree(work->event);
  11558. work->event = NULL;
  11559. }
  11560. spin_unlock_irq(&dev->event_lock);
  11561. }
  11562. }