intel_display.c 386 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003
  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_state *pipe_config);
  73. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_state *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_state *pipe_config);
  91. static void chv_prepare_pll(struct intel_crtc *crtc,
  92. const struct intel_crtc_state *pipe_config);
  93. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  94. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  95. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  96. {
  97. if (!connector->mst_port)
  98. return connector->encoder;
  99. else
  100. return &connector->mst_port->mst_encoders[pipe]->base;
  101. }
  102. typedef struct {
  103. int min, max;
  104. } intel_range_t;
  105. typedef struct {
  106. int dot_limit;
  107. int p2_slow, p2_fast;
  108. } intel_p2_t;
  109. typedef struct intel_limit intel_limit_t;
  110. struct intel_limit {
  111. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  112. intel_p2_t p2;
  113. };
  114. int
  115. intel_pch_rawclk(struct drm_device *dev)
  116. {
  117. struct drm_i915_private *dev_priv = dev->dev_private;
  118. WARN_ON(!HAS_PCH_SPLIT(dev));
  119. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  120. }
  121. static inline u32 /* units of 100MHz */
  122. intel_fdi_link_freq(struct drm_device *dev)
  123. {
  124. if (IS_GEN5(dev)) {
  125. struct drm_i915_private *dev_priv = dev->dev_private;
  126. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  127. } else
  128. return 27;
  129. }
  130. static const intel_limit_t intel_limits_i8xx_dac = {
  131. .dot = { .min = 25000, .max = 350000 },
  132. .vco = { .min = 908000, .max = 1512000 },
  133. .n = { .min = 2, .max = 16 },
  134. .m = { .min = 96, .max = 140 },
  135. .m1 = { .min = 18, .max = 26 },
  136. .m2 = { .min = 6, .max = 16 },
  137. .p = { .min = 4, .max = 128 },
  138. .p1 = { .min = 2, .max = 33 },
  139. .p2 = { .dot_limit = 165000,
  140. .p2_slow = 4, .p2_fast = 2 },
  141. };
  142. static const intel_limit_t intel_limits_i8xx_dvo = {
  143. .dot = { .min = 25000, .max = 350000 },
  144. .vco = { .min = 908000, .max = 1512000 },
  145. .n = { .min = 2, .max = 16 },
  146. .m = { .min = 96, .max = 140 },
  147. .m1 = { .min = 18, .max = 26 },
  148. .m2 = { .min = 6, .max = 16 },
  149. .p = { .min = 4, .max = 128 },
  150. .p1 = { .min = 2, .max = 33 },
  151. .p2 = { .dot_limit = 165000,
  152. .p2_slow = 4, .p2_fast = 4 },
  153. };
  154. static const intel_limit_t intel_limits_i8xx_lvds = {
  155. .dot = { .min = 25000, .max = 350000 },
  156. .vco = { .min = 908000, .max = 1512000 },
  157. .n = { .min = 2, .max = 16 },
  158. .m = { .min = 96, .max = 140 },
  159. .m1 = { .min = 18, .max = 26 },
  160. .m2 = { .min = 6, .max = 16 },
  161. .p = { .min = 4, .max = 128 },
  162. .p1 = { .min = 1, .max = 6 },
  163. .p2 = { .dot_limit = 165000,
  164. .p2_slow = 14, .p2_fast = 7 },
  165. };
  166. static const intel_limit_t intel_limits_i9xx_sdvo = {
  167. .dot = { .min = 20000, .max = 400000 },
  168. .vco = { .min = 1400000, .max = 2800000 },
  169. .n = { .min = 1, .max = 6 },
  170. .m = { .min = 70, .max = 120 },
  171. .m1 = { .min = 8, .max = 18 },
  172. .m2 = { .min = 3, .max = 7 },
  173. .p = { .min = 5, .max = 80 },
  174. .p1 = { .min = 1, .max = 8 },
  175. .p2 = { .dot_limit = 200000,
  176. .p2_slow = 10, .p2_fast = 5 },
  177. };
  178. static const intel_limit_t intel_limits_i9xx_lvds = {
  179. .dot = { .min = 20000, .max = 400000 },
  180. .vco = { .min = 1400000, .max = 2800000 },
  181. .n = { .min = 1, .max = 6 },
  182. .m = { .min = 70, .max = 120 },
  183. .m1 = { .min = 8, .max = 18 },
  184. .m2 = { .min = 3, .max = 7 },
  185. .p = { .min = 7, .max = 98 },
  186. .p1 = { .min = 1, .max = 8 },
  187. .p2 = { .dot_limit = 112000,
  188. .p2_slow = 14, .p2_fast = 7 },
  189. };
  190. static const intel_limit_t intel_limits_g4x_sdvo = {
  191. .dot = { .min = 25000, .max = 270000 },
  192. .vco = { .min = 1750000, .max = 3500000},
  193. .n = { .min = 1, .max = 4 },
  194. .m = { .min = 104, .max = 138 },
  195. .m1 = { .min = 17, .max = 23 },
  196. .m2 = { .min = 5, .max = 11 },
  197. .p = { .min = 10, .max = 30 },
  198. .p1 = { .min = 1, .max = 3},
  199. .p2 = { .dot_limit = 270000,
  200. .p2_slow = 10,
  201. .p2_fast = 10
  202. },
  203. };
  204. static const intel_limit_t intel_limits_g4x_hdmi = {
  205. .dot = { .min = 22000, .max = 400000 },
  206. .vco = { .min = 1750000, .max = 3500000},
  207. .n = { .min = 1, .max = 4 },
  208. .m = { .min = 104, .max = 138 },
  209. .m1 = { .min = 16, .max = 23 },
  210. .m2 = { .min = 5, .max = 11 },
  211. .p = { .min = 5, .max = 80 },
  212. .p1 = { .min = 1, .max = 8},
  213. .p2 = { .dot_limit = 165000,
  214. .p2_slow = 10, .p2_fast = 5 },
  215. };
  216. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  217. .dot = { .min = 20000, .max = 115000 },
  218. .vco = { .min = 1750000, .max = 3500000 },
  219. .n = { .min = 1, .max = 3 },
  220. .m = { .min = 104, .max = 138 },
  221. .m1 = { .min = 17, .max = 23 },
  222. .m2 = { .min = 5, .max = 11 },
  223. .p = { .min = 28, .max = 112 },
  224. .p1 = { .min = 2, .max = 8 },
  225. .p2 = { .dot_limit = 0,
  226. .p2_slow = 14, .p2_fast = 14
  227. },
  228. };
  229. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  230. .dot = { .min = 80000, .max = 224000 },
  231. .vco = { .min = 1750000, .max = 3500000 },
  232. .n = { .min = 1, .max = 3 },
  233. .m = { .min = 104, .max = 138 },
  234. .m1 = { .min = 17, .max = 23 },
  235. .m2 = { .min = 5, .max = 11 },
  236. .p = { .min = 14, .max = 42 },
  237. .p1 = { .min = 2, .max = 6 },
  238. .p2 = { .dot_limit = 0,
  239. .p2_slow = 7, .p2_fast = 7
  240. },
  241. };
  242. static const intel_limit_t intel_limits_pineview_sdvo = {
  243. .dot = { .min = 20000, .max = 400000},
  244. .vco = { .min = 1700000, .max = 3500000 },
  245. /* Pineview's Ncounter is a ring counter */
  246. .n = { .min = 3, .max = 6 },
  247. .m = { .min = 2, .max = 256 },
  248. /* Pineview only has one combined m divider, which we treat as m2. */
  249. .m1 = { .min = 0, .max = 0 },
  250. .m2 = { .min = 0, .max = 254 },
  251. .p = { .min = 5, .max = 80 },
  252. .p1 = { .min = 1, .max = 8 },
  253. .p2 = { .dot_limit = 200000,
  254. .p2_slow = 10, .p2_fast = 5 },
  255. };
  256. static const intel_limit_t intel_limits_pineview_lvds = {
  257. .dot = { .min = 20000, .max = 400000 },
  258. .vco = { .min = 1700000, .max = 3500000 },
  259. .n = { .min = 3, .max = 6 },
  260. .m = { .min = 2, .max = 256 },
  261. .m1 = { .min = 0, .max = 0 },
  262. .m2 = { .min = 0, .max = 254 },
  263. .p = { .min = 7, .max = 112 },
  264. .p1 = { .min = 1, .max = 8 },
  265. .p2 = { .dot_limit = 112000,
  266. .p2_slow = 14, .p2_fast = 14 },
  267. };
  268. /* Ironlake / Sandybridge
  269. *
  270. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  271. * the range value for them is (actual_value - 2).
  272. */
  273. static const intel_limit_t intel_limits_ironlake_dac = {
  274. .dot = { .min = 25000, .max = 350000 },
  275. .vco = { .min = 1760000, .max = 3510000 },
  276. .n = { .min = 1, .max = 5 },
  277. .m = { .min = 79, .max = 127 },
  278. .m1 = { .min = 12, .max = 22 },
  279. .m2 = { .min = 5, .max = 9 },
  280. .p = { .min = 5, .max = 80 },
  281. .p1 = { .min = 1, .max = 8 },
  282. .p2 = { .dot_limit = 225000,
  283. .p2_slow = 10, .p2_fast = 5 },
  284. };
  285. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  286. .dot = { .min = 25000, .max = 350000 },
  287. .vco = { .min = 1760000, .max = 3510000 },
  288. .n = { .min = 1, .max = 3 },
  289. .m = { .min = 79, .max = 118 },
  290. .m1 = { .min = 12, .max = 22 },
  291. .m2 = { .min = 5, .max = 9 },
  292. .p = { .min = 28, .max = 112 },
  293. .p1 = { .min = 2, .max = 8 },
  294. .p2 = { .dot_limit = 225000,
  295. .p2_slow = 14, .p2_fast = 14 },
  296. };
  297. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  298. .dot = { .min = 25000, .max = 350000 },
  299. .vco = { .min = 1760000, .max = 3510000 },
  300. .n = { .min = 1, .max = 3 },
  301. .m = { .min = 79, .max = 127 },
  302. .m1 = { .min = 12, .max = 22 },
  303. .m2 = { .min = 5, .max = 9 },
  304. .p = { .min = 14, .max = 56 },
  305. .p1 = { .min = 2, .max = 8 },
  306. .p2 = { .dot_limit = 225000,
  307. .p2_slow = 7, .p2_fast = 7 },
  308. };
  309. /* LVDS 100mhz refclk limits. */
  310. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  311. .dot = { .min = 25000, .max = 350000 },
  312. .vco = { .min = 1760000, .max = 3510000 },
  313. .n = { .min = 1, .max = 2 },
  314. .m = { .min = 79, .max = 126 },
  315. .m1 = { .min = 12, .max = 22 },
  316. .m2 = { .min = 5, .max = 9 },
  317. .p = { .min = 28, .max = 112 },
  318. .p1 = { .min = 2, .max = 8 },
  319. .p2 = { .dot_limit = 225000,
  320. .p2_slow = 14, .p2_fast = 14 },
  321. };
  322. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  323. .dot = { .min = 25000, .max = 350000 },
  324. .vco = { .min = 1760000, .max = 3510000 },
  325. .n = { .min = 1, .max = 3 },
  326. .m = { .min = 79, .max = 126 },
  327. .m1 = { .min = 12, .max = 22 },
  328. .m2 = { .min = 5, .max = 9 },
  329. .p = { .min = 14, .max = 42 },
  330. .p1 = { .min = 2, .max = 6 },
  331. .p2 = { .dot_limit = 225000,
  332. .p2_slow = 7, .p2_fast = 7 },
  333. };
  334. static const intel_limit_t intel_limits_vlv = {
  335. /*
  336. * These are the data rate limits (measured in fast clocks)
  337. * since those are the strictest limits we have. The fast
  338. * clock and actual rate limits are more relaxed, so checking
  339. * them would make no difference.
  340. */
  341. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  342. .vco = { .min = 4000000, .max = 6000000 },
  343. .n = { .min = 1, .max = 7 },
  344. .m1 = { .min = 2, .max = 3 },
  345. .m2 = { .min = 11, .max = 156 },
  346. .p1 = { .min = 2, .max = 3 },
  347. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  348. };
  349. static const intel_limit_t intel_limits_chv = {
  350. /*
  351. * These are the data rate limits (measured in fast clocks)
  352. * since those are the strictest limits we have. The fast
  353. * clock and actual rate limits are more relaxed, so checking
  354. * them would make no difference.
  355. */
  356. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  357. .vco = { .min = 4860000, .max = 6700000 },
  358. .n = { .min = 1, .max = 1 },
  359. .m1 = { .min = 2, .max = 2 },
  360. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  361. .p1 = { .min = 2, .max = 4 },
  362. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  363. };
  364. static void vlv_clock(int refclk, intel_clock_t *clock)
  365. {
  366. clock->m = clock->m1 * clock->m2;
  367. clock->p = clock->p1 * clock->p2;
  368. if (WARN_ON(clock->n == 0 || clock->p == 0))
  369. return;
  370. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  371. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  372. }
  373. /**
  374. * Returns whether any output on the specified pipe is of the specified type
  375. */
  376. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  377. {
  378. struct drm_device *dev = crtc->base.dev;
  379. struct intel_encoder *encoder;
  380. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  381. if (encoder->type == type)
  382. return true;
  383. return false;
  384. }
  385. /**
  386. * Returns whether any output on the specified pipe will have the specified
  387. * type after a staged modeset is complete, i.e., the same as
  388. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  389. * encoder->crtc.
  390. */
  391. static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
  392. {
  393. struct drm_device *dev = crtc->base.dev;
  394. struct intel_encoder *encoder;
  395. for_each_intel_encoder(dev, encoder)
  396. if (encoder->new_crtc == crtc && encoder->type == type)
  397. return true;
  398. return false;
  399. }
  400. static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
  401. int refclk)
  402. {
  403. struct drm_device *dev = crtc->base.dev;
  404. const intel_limit_t *limit;
  405. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  406. if (intel_is_dual_link_lvds(dev)) {
  407. if (refclk == 100000)
  408. limit = &intel_limits_ironlake_dual_lvds_100m;
  409. else
  410. limit = &intel_limits_ironlake_dual_lvds;
  411. } else {
  412. if (refclk == 100000)
  413. limit = &intel_limits_ironlake_single_lvds_100m;
  414. else
  415. limit = &intel_limits_ironlake_single_lvds;
  416. }
  417. } else
  418. limit = &intel_limits_ironlake_dac;
  419. return limit;
  420. }
  421. static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
  422. {
  423. struct drm_device *dev = crtc->base.dev;
  424. const intel_limit_t *limit;
  425. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  426. if (intel_is_dual_link_lvds(dev))
  427. limit = &intel_limits_g4x_dual_channel_lvds;
  428. else
  429. limit = &intel_limits_g4x_single_channel_lvds;
  430. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
  431. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
  432. limit = &intel_limits_g4x_hdmi;
  433. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
  434. limit = &intel_limits_g4x_sdvo;
  435. } else /* The option is for other outputs */
  436. limit = &intel_limits_i9xx_sdvo;
  437. return limit;
  438. }
  439. static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
  440. {
  441. struct drm_device *dev = crtc->base.dev;
  442. const intel_limit_t *limit;
  443. if (HAS_PCH_SPLIT(dev))
  444. limit = intel_ironlake_limit(crtc, refclk);
  445. else if (IS_G4X(dev)) {
  446. limit = intel_g4x_limit(crtc);
  447. } else if (IS_PINEVIEW(dev)) {
  448. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  449. limit = &intel_limits_pineview_lvds;
  450. else
  451. limit = &intel_limits_pineview_sdvo;
  452. } else if (IS_CHERRYVIEW(dev)) {
  453. limit = &intel_limits_chv;
  454. } else if (IS_VALLEYVIEW(dev)) {
  455. limit = &intel_limits_vlv;
  456. } else if (!IS_GEN2(dev)) {
  457. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  458. limit = &intel_limits_i9xx_lvds;
  459. else
  460. limit = &intel_limits_i9xx_sdvo;
  461. } else {
  462. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  463. limit = &intel_limits_i8xx_lvds;
  464. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  465. limit = &intel_limits_i8xx_dvo;
  466. else
  467. limit = &intel_limits_i8xx_dac;
  468. }
  469. return limit;
  470. }
  471. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  472. static void pineview_clock(int refclk, intel_clock_t *clock)
  473. {
  474. clock->m = clock->m2 + 2;
  475. clock->p = clock->p1 * clock->p2;
  476. if (WARN_ON(clock->n == 0 || clock->p == 0))
  477. return;
  478. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  479. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  480. }
  481. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  482. {
  483. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  484. }
  485. static void i9xx_clock(int refclk, intel_clock_t *clock)
  486. {
  487. clock->m = i9xx_dpll_compute_m(clock);
  488. clock->p = clock->p1 * clock->p2;
  489. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  490. return;
  491. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  492. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  493. }
  494. static void chv_clock(int refclk, intel_clock_t *clock)
  495. {
  496. clock->m = clock->m1 * clock->m2;
  497. clock->p = clock->p1 * clock->p2;
  498. if (WARN_ON(clock->n == 0 || clock->p == 0))
  499. return;
  500. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  501. clock->n << 22);
  502. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  503. }
  504. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  505. /**
  506. * Returns whether the given set of divisors are valid for a given refclk with
  507. * the given connectors.
  508. */
  509. static bool intel_PLL_is_valid(struct drm_device *dev,
  510. const intel_limit_t *limit,
  511. const intel_clock_t *clock)
  512. {
  513. if (clock->n < limit->n.min || limit->n.max < clock->n)
  514. INTELPllInvalid("n out of range\n");
  515. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  516. INTELPllInvalid("p1 out of range\n");
  517. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  518. INTELPllInvalid("m2 out of range\n");
  519. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  520. INTELPllInvalid("m1 out of range\n");
  521. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  522. if (clock->m1 <= clock->m2)
  523. INTELPllInvalid("m1 <= m2\n");
  524. if (!IS_VALLEYVIEW(dev)) {
  525. if (clock->p < limit->p.min || limit->p.max < clock->p)
  526. INTELPllInvalid("p out of range\n");
  527. if (clock->m < limit->m.min || limit->m.max < clock->m)
  528. INTELPllInvalid("m out of range\n");
  529. }
  530. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  531. INTELPllInvalid("vco out of range\n");
  532. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  533. * connector, etc., rather than just a single range.
  534. */
  535. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  536. INTELPllInvalid("dot out of range\n");
  537. return true;
  538. }
  539. static bool
  540. i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  541. int target, int refclk, intel_clock_t *match_clock,
  542. intel_clock_t *best_clock)
  543. {
  544. struct drm_device *dev = crtc->base.dev;
  545. intel_clock_t clock;
  546. int err = target;
  547. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  548. /*
  549. * For LVDS just rely on its current settings for dual-channel.
  550. * We haven't figured out how to reliably set up different
  551. * single/dual channel state, if we even can.
  552. */
  553. if (intel_is_dual_link_lvds(dev))
  554. clock.p2 = limit->p2.p2_fast;
  555. else
  556. clock.p2 = limit->p2.p2_slow;
  557. } else {
  558. if (target < limit->p2.dot_limit)
  559. clock.p2 = limit->p2.p2_slow;
  560. else
  561. clock.p2 = limit->p2.p2_fast;
  562. }
  563. memset(best_clock, 0, sizeof(*best_clock));
  564. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  565. clock.m1++) {
  566. for (clock.m2 = limit->m2.min;
  567. clock.m2 <= limit->m2.max; clock.m2++) {
  568. if (clock.m2 >= clock.m1)
  569. break;
  570. for (clock.n = limit->n.min;
  571. clock.n <= limit->n.max; clock.n++) {
  572. for (clock.p1 = limit->p1.min;
  573. clock.p1 <= limit->p1.max; clock.p1++) {
  574. int this_err;
  575. i9xx_clock(refclk, &clock);
  576. if (!intel_PLL_is_valid(dev, limit,
  577. &clock))
  578. continue;
  579. if (match_clock &&
  580. clock.p != match_clock->p)
  581. continue;
  582. this_err = abs(clock.dot - target);
  583. if (this_err < err) {
  584. *best_clock = clock;
  585. err = this_err;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. return (err != target);
  592. }
  593. static bool
  594. pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  595. int target, int refclk, intel_clock_t *match_clock,
  596. intel_clock_t *best_clock)
  597. {
  598. struct drm_device *dev = crtc->base.dev;
  599. intel_clock_t clock;
  600. int err = target;
  601. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  602. /*
  603. * For LVDS just rely on its current settings for dual-channel.
  604. * We haven't figured out how to reliably set up different
  605. * single/dual channel state, if we even can.
  606. */
  607. if (intel_is_dual_link_lvds(dev))
  608. clock.p2 = limit->p2.p2_fast;
  609. else
  610. clock.p2 = limit->p2.p2_slow;
  611. } else {
  612. if (target < limit->p2.dot_limit)
  613. clock.p2 = limit->p2.p2_slow;
  614. else
  615. clock.p2 = limit->p2.p2_fast;
  616. }
  617. memset(best_clock, 0, sizeof(*best_clock));
  618. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  619. clock.m1++) {
  620. for (clock.m2 = limit->m2.min;
  621. clock.m2 <= limit->m2.max; clock.m2++) {
  622. for (clock.n = limit->n.min;
  623. clock.n <= limit->n.max; clock.n++) {
  624. for (clock.p1 = limit->p1.min;
  625. clock.p1 <= limit->p1.max; clock.p1++) {
  626. int this_err;
  627. pineview_clock(refclk, &clock);
  628. if (!intel_PLL_is_valid(dev, limit,
  629. &clock))
  630. continue;
  631. if (match_clock &&
  632. clock.p != match_clock->p)
  633. continue;
  634. this_err = abs(clock.dot - target);
  635. if (this_err < err) {
  636. *best_clock = clock;
  637. err = this_err;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. return (err != target);
  644. }
  645. static bool
  646. g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  647. int target, int refclk, intel_clock_t *match_clock,
  648. intel_clock_t *best_clock)
  649. {
  650. struct drm_device *dev = crtc->base.dev;
  651. intel_clock_t clock;
  652. int max_n;
  653. bool found;
  654. /* approximately equals target * 0.00585 */
  655. int err_most = (target >> 8) + (target >> 9);
  656. found = false;
  657. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  658. if (intel_is_dual_link_lvds(dev))
  659. clock.p2 = limit->p2.p2_fast;
  660. else
  661. clock.p2 = limit->p2.p2_slow;
  662. } else {
  663. if (target < limit->p2.dot_limit)
  664. clock.p2 = limit->p2.p2_slow;
  665. else
  666. clock.p2 = limit->p2.p2_fast;
  667. }
  668. memset(best_clock, 0, sizeof(*best_clock));
  669. max_n = limit->n.max;
  670. /* based on hardware requirement, prefer smaller n to precision */
  671. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  672. /* based on hardware requirement, prefere larger m1,m2 */
  673. for (clock.m1 = limit->m1.max;
  674. clock.m1 >= limit->m1.min; clock.m1--) {
  675. for (clock.m2 = limit->m2.max;
  676. clock.m2 >= limit->m2.min; clock.m2--) {
  677. for (clock.p1 = limit->p1.max;
  678. clock.p1 >= limit->p1.min; clock.p1--) {
  679. int this_err;
  680. i9xx_clock(refclk, &clock);
  681. if (!intel_PLL_is_valid(dev, limit,
  682. &clock))
  683. continue;
  684. this_err = abs(clock.dot - target);
  685. if (this_err < err_most) {
  686. *best_clock = clock;
  687. err_most = this_err;
  688. max_n = clock.n;
  689. found = true;
  690. }
  691. }
  692. }
  693. }
  694. }
  695. return found;
  696. }
  697. static bool
  698. vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  699. int target, int refclk, intel_clock_t *match_clock,
  700. intel_clock_t *best_clock)
  701. {
  702. struct drm_device *dev = crtc->base.dev;
  703. intel_clock_t clock;
  704. unsigned int bestppm = 1000000;
  705. /* min update 19.2 MHz */
  706. int max_n = min(limit->n.max, refclk / 19200);
  707. bool found = false;
  708. target *= 5; /* fast clock */
  709. memset(best_clock, 0, sizeof(*best_clock));
  710. /* based on hardware requirement, prefer smaller n to precision */
  711. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  712. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  713. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  714. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  715. clock.p = clock.p1 * clock.p2;
  716. /* based on hardware requirement, prefer bigger m1,m2 values */
  717. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  718. unsigned int ppm, diff;
  719. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  720. refclk * clock.m1);
  721. vlv_clock(refclk, &clock);
  722. if (!intel_PLL_is_valid(dev, limit,
  723. &clock))
  724. continue;
  725. diff = abs(clock.dot - target);
  726. ppm = div_u64(1000000ULL * diff, target);
  727. if (ppm < 100 && clock.p > best_clock->p) {
  728. bestppm = 0;
  729. *best_clock = clock;
  730. found = true;
  731. }
  732. if (bestppm >= 10 && ppm < bestppm - 10) {
  733. bestppm = ppm;
  734. *best_clock = clock;
  735. found = true;
  736. }
  737. }
  738. }
  739. }
  740. }
  741. return found;
  742. }
  743. static bool
  744. chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  745. int target, int refclk, intel_clock_t *match_clock,
  746. intel_clock_t *best_clock)
  747. {
  748. struct drm_device *dev = crtc->base.dev;
  749. intel_clock_t clock;
  750. uint64_t m2;
  751. int found = false;
  752. memset(best_clock, 0, sizeof(*best_clock));
  753. /*
  754. * Based on hardware doc, the n always set to 1, and m1 always
  755. * set to 2. If requires to support 200Mhz refclk, we need to
  756. * revisit this because n may not 1 anymore.
  757. */
  758. clock.n = 1, clock.m1 = 2;
  759. target *= 5; /* fast clock */
  760. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  761. for (clock.p2 = limit->p2.p2_fast;
  762. clock.p2 >= limit->p2.p2_slow;
  763. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  764. clock.p = clock.p1 * clock.p2;
  765. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  766. clock.n) << 22, refclk * clock.m1);
  767. if (m2 > INT_MAX/clock.m1)
  768. continue;
  769. clock.m2 = m2;
  770. chv_clock(refclk, &clock);
  771. if (!intel_PLL_is_valid(dev, limit, &clock))
  772. continue;
  773. /* based on hardware requirement, prefer bigger p
  774. */
  775. if (clock.p > best_clock->p) {
  776. *best_clock = clock;
  777. found = true;
  778. }
  779. }
  780. }
  781. return found;
  782. }
  783. bool intel_crtc_active(struct drm_crtc *crtc)
  784. {
  785. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  786. /* Be paranoid as we can arrive here with only partial
  787. * state retrieved from the hardware during setup.
  788. *
  789. * We can ditch the adjusted_mode.crtc_clock check as soon
  790. * as Haswell has gained clock readout/fastboot support.
  791. *
  792. * We can ditch the crtc->primary->fb check as soon as we can
  793. * properly reconstruct framebuffers.
  794. */
  795. return intel_crtc->active && crtc->primary->fb &&
  796. intel_crtc->config->base.adjusted_mode.crtc_clock;
  797. }
  798. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  799. enum pipe pipe)
  800. {
  801. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  802. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  803. return intel_crtc->config->cpu_transcoder;
  804. }
  805. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  806. {
  807. struct drm_i915_private *dev_priv = dev->dev_private;
  808. u32 reg = PIPEDSL(pipe);
  809. u32 line1, line2;
  810. u32 line_mask;
  811. if (IS_GEN2(dev))
  812. line_mask = DSL_LINEMASK_GEN2;
  813. else
  814. line_mask = DSL_LINEMASK_GEN3;
  815. line1 = I915_READ(reg) & line_mask;
  816. mdelay(5);
  817. line2 = I915_READ(reg) & line_mask;
  818. return line1 == line2;
  819. }
  820. /*
  821. * intel_wait_for_pipe_off - wait for pipe to turn off
  822. * @crtc: crtc whose pipe to wait for
  823. *
  824. * After disabling a pipe, we can't wait for vblank in the usual way,
  825. * spinning on the vblank interrupt status bit, since we won't actually
  826. * see an interrupt when the pipe is disabled.
  827. *
  828. * On Gen4 and above:
  829. * wait for the pipe register state bit to turn off
  830. *
  831. * Otherwise:
  832. * wait for the display line value to settle (it usually
  833. * ends up stopping at the start of the next frame).
  834. *
  835. */
  836. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  837. {
  838. struct drm_device *dev = crtc->base.dev;
  839. struct drm_i915_private *dev_priv = dev->dev_private;
  840. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  841. enum pipe pipe = crtc->pipe;
  842. if (INTEL_INFO(dev)->gen >= 4) {
  843. int reg = PIPECONF(cpu_transcoder);
  844. /* Wait for the Pipe State to go off */
  845. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  846. 100))
  847. WARN(1, "pipe_off wait timed out\n");
  848. } else {
  849. /* Wait for the display line to settle */
  850. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  851. WARN(1, "pipe_off wait timed out\n");
  852. }
  853. }
  854. /*
  855. * ibx_digital_port_connected - is the specified port connected?
  856. * @dev_priv: i915 private structure
  857. * @port: the port to test
  858. *
  859. * Returns true if @port is connected, false otherwise.
  860. */
  861. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  862. struct intel_digital_port *port)
  863. {
  864. u32 bit;
  865. if (HAS_PCH_IBX(dev_priv->dev)) {
  866. switch (port->port) {
  867. case PORT_B:
  868. bit = SDE_PORTB_HOTPLUG;
  869. break;
  870. case PORT_C:
  871. bit = SDE_PORTC_HOTPLUG;
  872. break;
  873. case PORT_D:
  874. bit = SDE_PORTD_HOTPLUG;
  875. break;
  876. default:
  877. return true;
  878. }
  879. } else {
  880. switch (port->port) {
  881. case PORT_B:
  882. bit = SDE_PORTB_HOTPLUG_CPT;
  883. break;
  884. case PORT_C:
  885. bit = SDE_PORTC_HOTPLUG_CPT;
  886. break;
  887. case PORT_D:
  888. bit = SDE_PORTD_HOTPLUG_CPT;
  889. break;
  890. default:
  891. return true;
  892. }
  893. }
  894. return I915_READ(SDEISR) & bit;
  895. }
  896. static const char *state_string(bool enabled)
  897. {
  898. return enabled ? "on" : "off";
  899. }
  900. /* Only for pre-ILK configs */
  901. void assert_pll(struct drm_i915_private *dev_priv,
  902. enum pipe pipe, bool state)
  903. {
  904. int reg;
  905. u32 val;
  906. bool cur_state;
  907. reg = DPLL(pipe);
  908. val = I915_READ(reg);
  909. cur_state = !!(val & DPLL_VCO_ENABLE);
  910. I915_STATE_WARN(cur_state != state,
  911. "PLL state assertion failure (expected %s, current %s)\n",
  912. state_string(state), state_string(cur_state));
  913. }
  914. /* XXX: the dsi pll is shared between MIPI DSI ports */
  915. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  916. {
  917. u32 val;
  918. bool cur_state;
  919. mutex_lock(&dev_priv->dpio_lock);
  920. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  921. mutex_unlock(&dev_priv->dpio_lock);
  922. cur_state = val & DSI_PLL_VCO_EN;
  923. I915_STATE_WARN(cur_state != state,
  924. "DSI PLL state assertion failure (expected %s, current %s)\n",
  925. state_string(state), state_string(cur_state));
  926. }
  927. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  928. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  929. struct intel_shared_dpll *
  930. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  931. {
  932. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  933. if (crtc->config->shared_dpll < 0)
  934. return NULL;
  935. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  936. }
  937. /* For ILK+ */
  938. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  939. struct intel_shared_dpll *pll,
  940. bool state)
  941. {
  942. bool cur_state;
  943. struct intel_dpll_hw_state hw_state;
  944. if (WARN (!pll,
  945. "asserting DPLL %s with no DPLL\n", state_string(state)))
  946. return;
  947. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  948. I915_STATE_WARN(cur_state != state,
  949. "%s assertion failure (expected %s, current %s)\n",
  950. pll->name, state_string(state), state_string(cur_state));
  951. }
  952. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  953. enum pipe pipe, bool state)
  954. {
  955. int reg;
  956. u32 val;
  957. bool cur_state;
  958. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  959. pipe);
  960. if (HAS_DDI(dev_priv->dev)) {
  961. /* DDI does not have a specific FDI_TX register */
  962. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  963. val = I915_READ(reg);
  964. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  965. } else {
  966. reg = FDI_TX_CTL(pipe);
  967. val = I915_READ(reg);
  968. cur_state = !!(val & FDI_TX_ENABLE);
  969. }
  970. I915_STATE_WARN(cur_state != state,
  971. "FDI TX state assertion failure (expected %s, current %s)\n",
  972. state_string(state), state_string(cur_state));
  973. }
  974. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  975. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  976. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  977. enum pipe pipe, bool state)
  978. {
  979. int reg;
  980. u32 val;
  981. bool cur_state;
  982. reg = FDI_RX_CTL(pipe);
  983. val = I915_READ(reg);
  984. cur_state = !!(val & FDI_RX_ENABLE);
  985. I915_STATE_WARN(cur_state != state,
  986. "FDI RX state assertion failure (expected %s, current %s)\n",
  987. state_string(state), state_string(cur_state));
  988. }
  989. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  990. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  991. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  992. enum pipe pipe)
  993. {
  994. int reg;
  995. u32 val;
  996. /* ILK FDI PLL is always enabled */
  997. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  998. return;
  999. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1000. if (HAS_DDI(dev_priv->dev))
  1001. return;
  1002. reg = FDI_TX_CTL(pipe);
  1003. val = I915_READ(reg);
  1004. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1005. }
  1006. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1007. enum pipe pipe, bool state)
  1008. {
  1009. int reg;
  1010. u32 val;
  1011. bool cur_state;
  1012. reg = FDI_RX_CTL(pipe);
  1013. val = I915_READ(reg);
  1014. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1015. I915_STATE_WARN(cur_state != state,
  1016. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1017. state_string(state), state_string(cur_state));
  1018. }
  1019. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1020. enum pipe pipe)
  1021. {
  1022. struct drm_device *dev = dev_priv->dev;
  1023. int pp_reg;
  1024. u32 val;
  1025. enum pipe panel_pipe = PIPE_A;
  1026. bool locked = true;
  1027. if (WARN_ON(HAS_DDI(dev)))
  1028. return;
  1029. if (HAS_PCH_SPLIT(dev)) {
  1030. u32 port_sel;
  1031. pp_reg = PCH_PP_CONTROL;
  1032. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1033. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1034. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1035. panel_pipe = PIPE_B;
  1036. /* XXX: else fix for eDP */
  1037. } else if (IS_VALLEYVIEW(dev)) {
  1038. /* presumably write lock depends on pipe, not port select */
  1039. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1040. panel_pipe = pipe;
  1041. } else {
  1042. pp_reg = PP_CONTROL;
  1043. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1044. panel_pipe = PIPE_B;
  1045. }
  1046. val = I915_READ(pp_reg);
  1047. if (!(val & PANEL_POWER_ON) ||
  1048. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1049. locked = false;
  1050. I915_STATE_WARN(panel_pipe == pipe && locked,
  1051. "panel assertion failure, pipe %c regs locked\n",
  1052. pipe_name(pipe));
  1053. }
  1054. static void assert_cursor(struct drm_i915_private *dev_priv,
  1055. enum pipe pipe, bool state)
  1056. {
  1057. struct drm_device *dev = dev_priv->dev;
  1058. bool cur_state;
  1059. if (IS_845G(dev) || IS_I865G(dev))
  1060. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1061. else
  1062. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1063. I915_STATE_WARN(cur_state != state,
  1064. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1065. pipe_name(pipe), state_string(state), state_string(cur_state));
  1066. }
  1067. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1068. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1069. void assert_pipe(struct drm_i915_private *dev_priv,
  1070. enum pipe pipe, bool state)
  1071. {
  1072. int reg;
  1073. u32 val;
  1074. bool cur_state;
  1075. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1076. pipe);
  1077. /* if we need the pipe quirk it must be always on */
  1078. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1079. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1080. state = true;
  1081. if (!intel_display_power_is_enabled(dev_priv,
  1082. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1083. cur_state = false;
  1084. } else {
  1085. reg = PIPECONF(cpu_transcoder);
  1086. val = I915_READ(reg);
  1087. cur_state = !!(val & PIPECONF_ENABLE);
  1088. }
  1089. I915_STATE_WARN(cur_state != state,
  1090. "pipe %c assertion failure (expected %s, current %s)\n",
  1091. pipe_name(pipe), state_string(state), state_string(cur_state));
  1092. }
  1093. static void assert_plane(struct drm_i915_private *dev_priv,
  1094. enum plane plane, bool state)
  1095. {
  1096. int reg;
  1097. u32 val;
  1098. bool cur_state;
  1099. reg = DSPCNTR(plane);
  1100. val = I915_READ(reg);
  1101. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1102. I915_STATE_WARN(cur_state != state,
  1103. "plane %c assertion failure (expected %s, current %s)\n",
  1104. plane_name(plane), state_string(state), state_string(cur_state));
  1105. }
  1106. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1107. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1108. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1109. enum pipe pipe)
  1110. {
  1111. struct drm_device *dev = dev_priv->dev;
  1112. int reg, i;
  1113. u32 val;
  1114. int cur_pipe;
  1115. /* Primary planes are fixed to pipes on gen4+ */
  1116. if (INTEL_INFO(dev)->gen >= 4) {
  1117. reg = DSPCNTR(pipe);
  1118. val = I915_READ(reg);
  1119. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1120. "plane %c assertion failure, should be disabled but not\n",
  1121. plane_name(pipe));
  1122. return;
  1123. }
  1124. /* Need to check both planes against the pipe */
  1125. for_each_pipe(dev_priv, i) {
  1126. reg = DSPCNTR(i);
  1127. val = I915_READ(reg);
  1128. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1129. DISPPLANE_SEL_PIPE_SHIFT;
  1130. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1131. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1132. plane_name(i), pipe_name(pipe));
  1133. }
  1134. }
  1135. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1136. enum pipe pipe)
  1137. {
  1138. struct drm_device *dev = dev_priv->dev;
  1139. int reg, sprite;
  1140. u32 val;
  1141. if (INTEL_INFO(dev)->gen >= 9) {
  1142. for_each_sprite(pipe, sprite) {
  1143. val = I915_READ(PLANE_CTL(pipe, sprite));
  1144. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1145. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1146. sprite, pipe_name(pipe));
  1147. }
  1148. } else if (IS_VALLEYVIEW(dev)) {
  1149. for_each_sprite(pipe, sprite) {
  1150. reg = SPCNTR(pipe, sprite);
  1151. val = I915_READ(reg);
  1152. I915_STATE_WARN(val & SP_ENABLE,
  1153. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1154. sprite_name(pipe, sprite), pipe_name(pipe));
  1155. }
  1156. } else if (INTEL_INFO(dev)->gen >= 7) {
  1157. reg = SPRCTL(pipe);
  1158. val = I915_READ(reg);
  1159. I915_STATE_WARN(val & SPRITE_ENABLE,
  1160. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1161. plane_name(pipe), pipe_name(pipe));
  1162. } else if (INTEL_INFO(dev)->gen >= 5) {
  1163. reg = DVSCNTR(pipe);
  1164. val = I915_READ(reg);
  1165. I915_STATE_WARN(val & DVS_ENABLE,
  1166. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1167. plane_name(pipe), pipe_name(pipe));
  1168. }
  1169. }
  1170. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1171. {
  1172. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1173. drm_crtc_vblank_put(crtc);
  1174. }
  1175. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1176. {
  1177. u32 val;
  1178. bool enabled;
  1179. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1180. val = I915_READ(PCH_DREF_CONTROL);
  1181. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1182. DREF_SUPERSPREAD_SOURCE_MASK));
  1183. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1184. }
  1185. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. int reg;
  1189. u32 val;
  1190. bool enabled;
  1191. reg = PCH_TRANSCONF(pipe);
  1192. val = I915_READ(reg);
  1193. enabled = !!(val & TRANS_ENABLE);
  1194. I915_STATE_WARN(enabled,
  1195. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1196. pipe_name(pipe));
  1197. }
  1198. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe, u32 port_sel, u32 val)
  1200. {
  1201. if ((val & DP_PORT_EN) == 0)
  1202. return false;
  1203. if (HAS_PCH_CPT(dev_priv->dev)) {
  1204. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1205. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1206. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1207. return false;
  1208. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1209. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1210. return false;
  1211. } else {
  1212. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1213. return false;
  1214. }
  1215. return true;
  1216. }
  1217. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, u32 val)
  1219. {
  1220. if ((val & SDVO_ENABLE) == 0)
  1221. return false;
  1222. if (HAS_PCH_CPT(dev_priv->dev)) {
  1223. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1224. return false;
  1225. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1226. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1227. return false;
  1228. } else {
  1229. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1230. return false;
  1231. }
  1232. return true;
  1233. }
  1234. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1235. enum pipe pipe, u32 val)
  1236. {
  1237. if ((val & LVDS_PORT_EN) == 0)
  1238. return false;
  1239. if (HAS_PCH_CPT(dev_priv->dev)) {
  1240. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1241. return false;
  1242. } else {
  1243. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1244. return false;
  1245. }
  1246. return true;
  1247. }
  1248. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1249. enum pipe pipe, u32 val)
  1250. {
  1251. if ((val & ADPA_DAC_ENABLE) == 0)
  1252. return false;
  1253. if (HAS_PCH_CPT(dev_priv->dev)) {
  1254. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1255. return false;
  1256. } else {
  1257. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1258. return false;
  1259. }
  1260. return true;
  1261. }
  1262. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1263. enum pipe pipe, int reg, u32 port_sel)
  1264. {
  1265. u32 val = I915_READ(reg);
  1266. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1267. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1268. reg, pipe_name(pipe));
  1269. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1270. && (val & DP_PIPEB_SELECT),
  1271. "IBX PCH dp port still using transcoder B\n");
  1272. }
  1273. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1274. enum pipe pipe, int reg)
  1275. {
  1276. u32 val = I915_READ(reg);
  1277. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1278. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1279. reg, pipe_name(pipe));
  1280. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1281. && (val & SDVO_PIPE_B_SELECT),
  1282. "IBX PCH hdmi port still using transcoder B\n");
  1283. }
  1284. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1285. enum pipe pipe)
  1286. {
  1287. int reg;
  1288. u32 val;
  1289. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1292. reg = PCH_ADPA;
  1293. val = I915_READ(reg);
  1294. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. reg = PCH_LVDS;
  1298. val = I915_READ(reg);
  1299. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1300. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1301. pipe_name(pipe));
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1303. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1304. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1305. }
  1306. static void intel_init_dpio(struct drm_device *dev)
  1307. {
  1308. struct drm_i915_private *dev_priv = dev->dev_private;
  1309. if (!IS_VALLEYVIEW(dev))
  1310. return;
  1311. /*
  1312. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1313. * CHV x1 PHY (DP/HDMI D)
  1314. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1315. */
  1316. if (IS_CHERRYVIEW(dev)) {
  1317. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1318. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1319. } else {
  1320. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1321. }
  1322. }
  1323. static void vlv_enable_pll(struct intel_crtc *crtc,
  1324. const struct intel_crtc_state *pipe_config)
  1325. {
  1326. struct drm_device *dev = crtc->base.dev;
  1327. struct drm_i915_private *dev_priv = dev->dev_private;
  1328. int reg = DPLL(crtc->pipe);
  1329. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1330. assert_pipe_disabled(dev_priv, crtc->pipe);
  1331. /* No really, not for ILK+ */
  1332. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1333. /* PLL is protected by panel, make sure we can write it */
  1334. if (IS_MOBILE(dev_priv->dev))
  1335. assert_panel_unlocked(dev_priv, crtc->pipe);
  1336. I915_WRITE(reg, dpll);
  1337. POSTING_READ(reg);
  1338. udelay(150);
  1339. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1340. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1341. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1342. POSTING_READ(DPLL_MD(crtc->pipe));
  1343. /* We do this three times for luck */
  1344. I915_WRITE(reg, dpll);
  1345. POSTING_READ(reg);
  1346. udelay(150); /* wait for warmup */
  1347. I915_WRITE(reg, dpll);
  1348. POSTING_READ(reg);
  1349. udelay(150); /* wait for warmup */
  1350. I915_WRITE(reg, dpll);
  1351. POSTING_READ(reg);
  1352. udelay(150); /* wait for warmup */
  1353. }
  1354. static void chv_enable_pll(struct intel_crtc *crtc,
  1355. const struct intel_crtc_state *pipe_config)
  1356. {
  1357. struct drm_device *dev = crtc->base.dev;
  1358. struct drm_i915_private *dev_priv = dev->dev_private;
  1359. int pipe = crtc->pipe;
  1360. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1361. u32 tmp;
  1362. assert_pipe_disabled(dev_priv, crtc->pipe);
  1363. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1364. mutex_lock(&dev_priv->dpio_lock);
  1365. /* Enable back the 10bit clock to display controller */
  1366. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1367. tmp |= DPIO_DCLKP_EN;
  1368. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1369. /*
  1370. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1371. */
  1372. udelay(1);
  1373. /* Enable PLL */
  1374. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1375. /* Check PLL is locked */
  1376. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1377. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1378. /* not sure when this should be written */
  1379. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1380. POSTING_READ(DPLL_MD(pipe));
  1381. mutex_unlock(&dev_priv->dpio_lock);
  1382. }
  1383. static int intel_num_dvo_pipes(struct drm_device *dev)
  1384. {
  1385. struct intel_crtc *crtc;
  1386. int count = 0;
  1387. for_each_intel_crtc(dev, crtc)
  1388. count += crtc->active &&
  1389. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1390. return count;
  1391. }
  1392. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1393. {
  1394. struct drm_device *dev = crtc->base.dev;
  1395. struct drm_i915_private *dev_priv = dev->dev_private;
  1396. int reg = DPLL(crtc->pipe);
  1397. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1398. assert_pipe_disabled(dev_priv, crtc->pipe);
  1399. /* No really, not for ILK+ */
  1400. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1401. /* PLL is protected by panel, make sure we can write it */
  1402. if (IS_MOBILE(dev) && !IS_I830(dev))
  1403. assert_panel_unlocked(dev_priv, crtc->pipe);
  1404. /* Enable DVO 2x clock on both PLLs if necessary */
  1405. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1406. /*
  1407. * It appears to be important that we don't enable this
  1408. * for the current pipe before otherwise configuring the
  1409. * PLL. No idea how this should be handled if multiple
  1410. * DVO outputs are enabled simultaneosly.
  1411. */
  1412. dpll |= DPLL_DVO_2X_MODE;
  1413. I915_WRITE(DPLL(!crtc->pipe),
  1414. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1415. }
  1416. /* Wait for the clocks to stabilize. */
  1417. POSTING_READ(reg);
  1418. udelay(150);
  1419. if (INTEL_INFO(dev)->gen >= 4) {
  1420. I915_WRITE(DPLL_MD(crtc->pipe),
  1421. crtc->config->dpll_hw_state.dpll_md);
  1422. } else {
  1423. /* The pixel multiplier can only be updated once the
  1424. * DPLL is enabled and the clocks are stable.
  1425. *
  1426. * So write it again.
  1427. */
  1428. I915_WRITE(reg, dpll);
  1429. }
  1430. /* We do this three times for luck */
  1431. I915_WRITE(reg, dpll);
  1432. POSTING_READ(reg);
  1433. udelay(150); /* wait for warmup */
  1434. I915_WRITE(reg, dpll);
  1435. POSTING_READ(reg);
  1436. udelay(150); /* wait for warmup */
  1437. I915_WRITE(reg, dpll);
  1438. POSTING_READ(reg);
  1439. udelay(150); /* wait for warmup */
  1440. }
  1441. /**
  1442. * i9xx_disable_pll - disable a PLL
  1443. * @dev_priv: i915 private structure
  1444. * @pipe: pipe PLL to disable
  1445. *
  1446. * Disable the PLL for @pipe, making sure the pipe is off first.
  1447. *
  1448. * Note! This is for pre-ILK only.
  1449. */
  1450. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1451. {
  1452. struct drm_device *dev = crtc->base.dev;
  1453. struct drm_i915_private *dev_priv = dev->dev_private;
  1454. enum pipe pipe = crtc->pipe;
  1455. /* Disable DVO 2x clock on both PLLs if necessary */
  1456. if (IS_I830(dev) &&
  1457. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1458. intel_num_dvo_pipes(dev) == 1) {
  1459. I915_WRITE(DPLL(PIPE_B),
  1460. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1461. I915_WRITE(DPLL(PIPE_A),
  1462. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1463. }
  1464. /* Don't disable pipe or pipe PLLs if needed */
  1465. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1466. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1467. return;
  1468. /* Make sure the pipe isn't still relying on us */
  1469. assert_pipe_disabled(dev_priv, pipe);
  1470. I915_WRITE(DPLL(pipe), 0);
  1471. POSTING_READ(DPLL(pipe));
  1472. }
  1473. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1474. {
  1475. u32 val = 0;
  1476. /* Make sure the pipe isn't still relying on us */
  1477. assert_pipe_disabled(dev_priv, pipe);
  1478. /*
  1479. * Leave integrated clock source and reference clock enabled for pipe B.
  1480. * The latter is needed for VGA hotplug / manual detection.
  1481. */
  1482. if (pipe == PIPE_B)
  1483. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1484. I915_WRITE(DPLL(pipe), val);
  1485. POSTING_READ(DPLL(pipe));
  1486. }
  1487. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1488. {
  1489. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1490. u32 val;
  1491. /* Make sure the pipe isn't still relying on us */
  1492. assert_pipe_disabled(dev_priv, pipe);
  1493. /* Set PLL en = 0 */
  1494. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1495. if (pipe != PIPE_A)
  1496. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1497. I915_WRITE(DPLL(pipe), val);
  1498. POSTING_READ(DPLL(pipe));
  1499. mutex_lock(&dev_priv->dpio_lock);
  1500. /* Disable 10bit clock to display controller */
  1501. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1502. val &= ~DPIO_DCLKP_EN;
  1503. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1504. /* disable left/right clock distribution */
  1505. if (pipe != PIPE_B) {
  1506. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1507. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1508. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1509. } else {
  1510. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1511. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1512. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1513. }
  1514. mutex_unlock(&dev_priv->dpio_lock);
  1515. }
  1516. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1517. struct intel_digital_port *dport)
  1518. {
  1519. u32 port_mask;
  1520. int dpll_reg;
  1521. switch (dport->port) {
  1522. case PORT_B:
  1523. port_mask = DPLL_PORTB_READY_MASK;
  1524. dpll_reg = DPLL(0);
  1525. break;
  1526. case PORT_C:
  1527. port_mask = DPLL_PORTC_READY_MASK;
  1528. dpll_reg = DPLL(0);
  1529. break;
  1530. case PORT_D:
  1531. port_mask = DPLL_PORTD_READY_MASK;
  1532. dpll_reg = DPIO_PHY_STATUS;
  1533. break;
  1534. default:
  1535. BUG();
  1536. }
  1537. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1538. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1539. port_name(dport->port), I915_READ(dpll_reg));
  1540. }
  1541. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1542. {
  1543. struct drm_device *dev = crtc->base.dev;
  1544. struct drm_i915_private *dev_priv = dev->dev_private;
  1545. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1546. if (WARN_ON(pll == NULL))
  1547. return;
  1548. WARN_ON(!pll->config.crtc_mask);
  1549. if (pll->active == 0) {
  1550. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1551. WARN_ON(pll->on);
  1552. assert_shared_dpll_disabled(dev_priv, pll);
  1553. pll->mode_set(dev_priv, pll);
  1554. }
  1555. }
  1556. /**
  1557. * intel_enable_shared_dpll - enable PCH PLL
  1558. * @dev_priv: i915 private structure
  1559. * @pipe: pipe PLL to enable
  1560. *
  1561. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1562. * drives the transcoder clock.
  1563. */
  1564. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1565. {
  1566. struct drm_device *dev = crtc->base.dev;
  1567. struct drm_i915_private *dev_priv = dev->dev_private;
  1568. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1569. if (WARN_ON(pll == NULL))
  1570. return;
  1571. if (WARN_ON(pll->config.crtc_mask == 0))
  1572. return;
  1573. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1574. pll->name, pll->active, pll->on,
  1575. crtc->base.base.id);
  1576. if (pll->active++) {
  1577. WARN_ON(!pll->on);
  1578. assert_shared_dpll_enabled(dev_priv, pll);
  1579. return;
  1580. }
  1581. WARN_ON(pll->on);
  1582. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1583. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1584. pll->enable(dev_priv, pll);
  1585. pll->on = true;
  1586. }
  1587. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1588. {
  1589. struct drm_device *dev = crtc->base.dev;
  1590. struct drm_i915_private *dev_priv = dev->dev_private;
  1591. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1592. /* PCH only available on ILK+ */
  1593. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1594. if (WARN_ON(pll == NULL))
  1595. return;
  1596. if (WARN_ON(pll->config.crtc_mask == 0))
  1597. return;
  1598. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1599. pll->name, pll->active, pll->on,
  1600. crtc->base.base.id);
  1601. if (WARN_ON(pll->active == 0)) {
  1602. assert_shared_dpll_disabled(dev_priv, pll);
  1603. return;
  1604. }
  1605. assert_shared_dpll_enabled(dev_priv, pll);
  1606. WARN_ON(!pll->on);
  1607. if (--pll->active)
  1608. return;
  1609. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1610. pll->disable(dev_priv, pll);
  1611. pll->on = false;
  1612. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1613. }
  1614. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1615. enum pipe pipe)
  1616. {
  1617. struct drm_device *dev = dev_priv->dev;
  1618. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1619. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1620. uint32_t reg, val, pipeconf_val;
  1621. /* PCH only available on ILK+ */
  1622. BUG_ON(!HAS_PCH_SPLIT(dev));
  1623. /* Make sure PCH DPLL is enabled */
  1624. assert_shared_dpll_enabled(dev_priv,
  1625. intel_crtc_to_shared_dpll(intel_crtc));
  1626. /* FDI must be feeding us bits for PCH ports */
  1627. assert_fdi_tx_enabled(dev_priv, pipe);
  1628. assert_fdi_rx_enabled(dev_priv, pipe);
  1629. if (HAS_PCH_CPT(dev)) {
  1630. /* Workaround: Set the timing override bit before enabling the
  1631. * pch transcoder. */
  1632. reg = TRANS_CHICKEN2(pipe);
  1633. val = I915_READ(reg);
  1634. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1635. I915_WRITE(reg, val);
  1636. }
  1637. reg = PCH_TRANSCONF(pipe);
  1638. val = I915_READ(reg);
  1639. pipeconf_val = I915_READ(PIPECONF(pipe));
  1640. if (HAS_PCH_IBX(dev_priv->dev)) {
  1641. /*
  1642. * make the BPC in transcoder be consistent with
  1643. * that in pipeconf reg.
  1644. */
  1645. val &= ~PIPECONF_BPC_MASK;
  1646. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1647. }
  1648. val &= ~TRANS_INTERLACE_MASK;
  1649. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1650. if (HAS_PCH_IBX(dev_priv->dev) &&
  1651. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1652. val |= TRANS_LEGACY_INTERLACED_ILK;
  1653. else
  1654. val |= TRANS_INTERLACED;
  1655. else
  1656. val |= TRANS_PROGRESSIVE;
  1657. I915_WRITE(reg, val | TRANS_ENABLE);
  1658. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1659. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1660. }
  1661. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1662. enum transcoder cpu_transcoder)
  1663. {
  1664. u32 val, pipeconf_val;
  1665. /* PCH only available on ILK+ */
  1666. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1667. /* FDI must be feeding us bits for PCH ports */
  1668. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1669. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1670. /* Workaround: set timing override bit. */
  1671. val = I915_READ(_TRANSA_CHICKEN2);
  1672. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1673. I915_WRITE(_TRANSA_CHICKEN2, val);
  1674. val = TRANS_ENABLE;
  1675. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1676. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1677. PIPECONF_INTERLACED_ILK)
  1678. val |= TRANS_INTERLACED;
  1679. else
  1680. val |= TRANS_PROGRESSIVE;
  1681. I915_WRITE(LPT_TRANSCONF, val);
  1682. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1683. DRM_ERROR("Failed to enable PCH transcoder\n");
  1684. }
  1685. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1686. enum pipe pipe)
  1687. {
  1688. struct drm_device *dev = dev_priv->dev;
  1689. uint32_t reg, val;
  1690. /* FDI relies on the transcoder */
  1691. assert_fdi_tx_disabled(dev_priv, pipe);
  1692. assert_fdi_rx_disabled(dev_priv, pipe);
  1693. /* Ports must be off as well */
  1694. assert_pch_ports_disabled(dev_priv, pipe);
  1695. reg = PCH_TRANSCONF(pipe);
  1696. val = I915_READ(reg);
  1697. val &= ~TRANS_ENABLE;
  1698. I915_WRITE(reg, val);
  1699. /* wait for PCH transcoder off, transcoder state */
  1700. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1701. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1702. if (!HAS_PCH_IBX(dev)) {
  1703. /* Workaround: Clear the timing override chicken bit again. */
  1704. reg = TRANS_CHICKEN2(pipe);
  1705. val = I915_READ(reg);
  1706. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1707. I915_WRITE(reg, val);
  1708. }
  1709. }
  1710. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1711. {
  1712. u32 val;
  1713. val = I915_READ(LPT_TRANSCONF);
  1714. val &= ~TRANS_ENABLE;
  1715. I915_WRITE(LPT_TRANSCONF, val);
  1716. /* wait for PCH transcoder off, transcoder state */
  1717. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1718. DRM_ERROR("Failed to disable PCH transcoder\n");
  1719. /* Workaround: clear timing override bit. */
  1720. val = I915_READ(_TRANSA_CHICKEN2);
  1721. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1722. I915_WRITE(_TRANSA_CHICKEN2, val);
  1723. }
  1724. /**
  1725. * intel_enable_pipe - enable a pipe, asserting requirements
  1726. * @crtc: crtc responsible for the pipe
  1727. *
  1728. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1729. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1730. */
  1731. static void intel_enable_pipe(struct intel_crtc *crtc)
  1732. {
  1733. struct drm_device *dev = crtc->base.dev;
  1734. struct drm_i915_private *dev_priv = dev->dev_private;
  1735. enum pipe pipe = crtc->pipe;
  1736. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1737. pipe);
  1738. enum pipe pch_transcoder;
  1739. int reg;
  1740. u32 val;
  1741. assert_planes_disabled(dev_priv, pipe);
  1742. assert_cursor_disabled(dev_priv, pipe);
  1743. assert_sprites_disabled(dev_priv, pipe);
  1744. if (HAS_PCH_LPT(dev_priv->dev))
  1745. pch_transcoder = TRANSCODER_A;
  1746. else
  1747. pch_transcoder = pipe;
  1748. /*
  1749. * A pipe without a PLL won't actually be able to drive bits from
  1750. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1751. * need the check.
  1752. */
  1753. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1754. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1755. assert_dsi_pll_enabled(dev_priv);
  1756. else
  1757. assert_pll_enabled(dev_priv, pipe);
  1758. else {
  1759. if (crtc->config->has_pch_encoder) {
  1760. /* if driving the PCH, we need FDI enabled */
  1761. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1762. assert_fdi_tx_pll_enabled(dev_priv,
  1763. (enum pipe) cpu_transcoder);
  1764. }
  1765. /* FIXME: assert CPU port conditions for SNB+ */
  1766. }
  1767. reg = PIPECONF(cpu_transcoder);
  1768. val = I915_READ(reg);
  1769. if (val & PIPECONF_ENABLE) {
  1770. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1771. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1772. return;
  1773. }
  1774. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1775. POSTING_READ(reg);
  1776. }
  1777. /**
  1778. * intel_disable_pipe - disable a pipe, asserting requirements
  1779. * @crtc: crtc whose pipes is to be disabled
  1780. *
  1781. * Disable the pipe of @crtc, making sure that various hardware
  1782. * specific requirements are met, if applicable, e.g. plane
  1783. * disabled, panel fitter off, etc.
  1784. *
  1785. * Will wait until the pipe has shut down before returning.
  1786. */
  1787. static void intel_disable_pipe(struct intel_crtc *crtc)
  1788. {
  1789. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1790. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1791. enum pipe pipe = crtc->pipe;
  1792. int reg;
  1793. u32 val;
  1794. /*
  1795. * Make sure planes won't keep trying to pump pixels to us,
  1796. * or we might hang the display.
  1797. */
  1798. assert_planes_disabled(dev_priv, pipe);
  1799. assert_cursor_disabled(dev_priv, pipe);
  1800. assert_sprites_disabled(dev_priv, pipe);
  1801. reg = PIPECONF(cpu_transcoder);
  1802. val = I915_READ(reg);
  1803. if ((val & PIPECONF_ENABLE) == 0)
  1804. return;
  1805. /*
  1806. * Double wide has implications for planes
  1807. * so best keep it disabled when not needed.
  1808. */
  1809. if (crtc->config->double_wide)
  1810. val &= ~PIPECONF_DOUBLE_WIDE;
  1811. /* Don't disable pipe or pipe PLLs if needed */
  1812. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1813. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1814. val &= ~PIPECONF_ENABLE;
  1815. I915_WRITE(reg, val);
  1816. if ((val & PIPECONF_ENABLE) == 0)
  1817. intel_wait_for_pipe_off(crtc);
  1818. }
  1819. /*
  1820. * Plane regs are double buffered, going from enabled->disabled needs a
  1821. * trigger in order to latch. The display address reg provides this.
  1822. */
  1823. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1824. enum plane plane)
  1825. {
  1826. struct drm_device *dev = dev_priv->dev;
  1827. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1828. I915_WRITE(reg, I915_READ(reg));
  1829. POSTING_READ(reg);
  1830. }
  1831. /**
  1832. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1833. * @plane: plane to be enabled
  1834. * @crtc: crtc for the plane
  1835. *
  1836. * Enable @plane on @crtc, making sure that the pipe is running first.
  1837. */
  1838. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1839. struct drm_crtc *crtc)
  1840. {
  1841. struct drm_device *dev = plane->dev;
  1842. struct drm_i915_private *dev_priv = dev->dev_private;
  1843. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1844. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1845. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1846. if (intel_crtc->primary_enabled)
  1847. return;
  1848. intel_crtc->primary_enabled = true;
  1849. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1850. crtc->x, crtc->y);
  1851. /*
  1852. * BDW signals flip done immediately if the plane
  1853. * is disabled, even if the plane enable is already
  1854. * armed to occur at the next vblank :(
  1855. */
  1856. if (IS_BROADWELL(dev))
  1857. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1858. }
  1859. /**
  1860. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1861. * @plane: plane to be disabled
  1862. * @crtc: crtc for the plane
  1863. *
  1864. * Disable @plane on @crtc, making sure that the pipe is running first.
  1865. */
  1866. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1867. struct drm_crtc *crtc)
  1868. {
  1869. struct drm_device *dev = plane->dev;
  1870. struct drm_i915_private *dev_priv = dev->dev_private;
  1871. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1872. if (WARN_ON(!intel_crtc->active))
  1873. return;
  1874. if (!intel_crtc->primary_enabled)
  1875. return;
  1876. intel_crtc->primary_enabled = false;
  1877. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1878. crtc->x, crtc->y);
  1879. }
  1880. static bool need_vtd_wa(struct drm_device *dev)
  1881. {
  1882. #ifdef CONFIG_INTEL_IOMMU
  1883. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1884. return true;
  1885. #endif
  1886. return false;
  1887. }
  1888. int
  1889. intel_fb_align_height(struct drm_device *dev, int height, unsigned int tiling)
  1890. {
  1891. int tile_height;
  1892. tile_height = tiling ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1893. return ALIGN(height, tile_height);
  1894. }
  1895. int
  1896. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1897. struct drm_framebuffer *fb,
  1898. struct intel_engine_cs *pipelined)
  1899. {
  1900. struct drm_device *dev = fb->dev;
  1901. struct drm_i915_private *dev_priv = dev->dev_private;
  1902. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1903. u32 alignment;
  1904. int ret;
  1905. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1906. switch (obj->tiling_mode) {
  1907. case I915_TILING_NONE:
  1908. if (INTEL_INFO(dev)->gen >= 9)
  1909. alignment = 256 * 1024;
  1910. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1911. alignment = 128 * 1024;
  1912. else if (INTEL_INFO(dev)->gen >= 4)
  1913. alignment = 4 * 1024;
  1914. else
  1915. alignment = 64 * 1024;
  1916. break;
  1917. case I915_TILING_X:
  1918. if (INTEL_INFO(dev)->gen >= 9)
  1919. alignment = 256 * 1024;
  1920. else {
  1921. /* pin() will align the object as required by fence */
  1922. alignment = 0;
  1923. }
  1924. break;
  1925. case I915_TILING_Y:
  1926. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1927. return -EINVAL;
  1928. default:
  1929. BUG();
  1930. }
  1931. /* Note that the w/a also requires 64 PTE of padding following the
  1932. * bo. We currently fill all unused PTE with the shadow page and so
  1933. * we should always have valid PTE following the scanout preventing
  1934. * the VT-d warning.
  1935. */
  1936. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1937. alignment = 256 * 1024;
  1938. /*
  1939. * Global gtt pte registers are special registers which actually forward
  1940. * writes to a chunk of system memory. Which means that there is no risk
  1941. * that the register values disappear as soon as we call
  1942. * intel_runtime_pm_put(), so it is correct to wrap only the
  1943. * pin/unpin/fence and not more.
  1944. */
  1945. intel_runtime_pm_get(dev_priv);
  1946. dev_priv->mm.interruptible = false;
  1947. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1948. if (ret)
  1949. goto err_interruptible;
  1950. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1951. * fence, whereas 965+ only requires a fence if using
  1952. * framebuffer compression. For simplicity, we always install
  1953. * a fence as the cost is not that onerous.
  1954. */
  1955. ret = i915_gem_object_get_fence(obj);
  1956. if (ret)
  1957. goto err_unpin;
  1958. i915_gem_object_pin_fence(obj);
  1959. dev_priv->mm.interruptible = true;
  1960. intel_runtime_pm_put(dev_priv);
  1961. return 0;
  1962. err_unpin:
  1963. i915_gem_object_unpin_from_display_plane(obj);
  1964. err_interruptible:
  1965. dev_priv->mm.interruptible = true;
  1966. intel_runtime_pm_put(dev_priv);
  1967. return ret;
  1968. }
  1969. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1970. {
  1971. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1972. i915_gem_object_unpin_fence(obj);
  1973. i915_gem_object_unpin_from_display_plane(obj);
  1974. }
  1975. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1976. * is assumed to be a power-of-two. */
  1977. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1978. unsigned int tiling_mode,
  1979. unsigned int cpp,
  1980. unsigned int pitch)
  1981. {
  1982. if (tiling_mode != I915_TILING_NONE) {
  1983. unsigned int tile_rows, tiles;
  1984. tile_rows = *y / 8;
  1985. *y %= 8;
  1986. tiles = *x / (512/cpp);
  1987. *x %= 512/cpp;
  1988. return tile_rows * pitch * 8 + tiles * 4096;
  1989. } else {
  1990. unsigned int offset;
  1991. offset = *y * pitch + *x * cpp;
  1992. *y = 0;
  1993. *x = (offset & 4095) / cpp;
  1994. return offset & -4096;
  1995. }
  1996. }
  1997. static int i9xx_format_to_fourcc(int format)
  1998. {
  1999. switch (format) {
  2000. case DISPPLANE_8BPP:
  2001. return DRM_FORMAT_C8;
  2002. case DISPPLANE_BGRX555:
  2003. return DRM_FORMAT_XRGB1555;
  2004. case DISPPLANE_BGRX565:
  2005. return DRM_FORMAT_RGB565;
  2006. default:
  2007. case DISPPLANE_BGRX888:
  2008. return DRM_FORMAT_XRGB8888;
  2009. case DISPPLANE_RGBX888:
  2010. return DRM_FORMAT_XBGR8888;
  2011. case DISPPLANE_BGRX101010:
  2012. return DRM_FORMAT_XRGB2101010;
  2013. case DISPPLANE_RGBX101010:
  2014. return DRM_FORMAT_XBGR2101010;
  2015. }
  2016. }
  2017. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2018. {
  2019. switch (format) {
  2020. case PLANE_CTL_FORMAT_RGB_565:
  2021. return DRM_FORMAT_RGB565;
  2022. default:
  2023. case PLANE_CTL_FORMAT_XRGB_8888:
  2024. if (rgb_order) {
  2025. if (alpha)
  2026. return DRM_FORMAT_ABGR8888;
  2027. else
  2028. return DRM_FORMAT_XBGR8888;
  2029. } else {
  2030. if (alpha)
  2031. return DRM_FORMAT_ARGB8888;
  2032. else
  2033. return DRM_FORMAT_XRGB8888;
  2034. }
  2035. case PLANE_CTL_FORMAT_XRGB_2101010:
  2036. if (rgb_order)
  2037. return DRM_FORMAT_XBGR2101010;
  2038. else
  2039. return DRM_FORMAT_XRGB2101010;
  2040. }
  2041. }
  2042. static bool
  2043. intel_alloc_plane_obj(struct intel_crtc *crtc,
  2044. struct intel_initial_plane_config *plane_config)
  2045. {
  2046. struct drm_device *dev = crtc->base.dev;
  2047. struct drm_i915_gem_object *obj = NULL;
  2048. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2049. u32 base = plane_config->base;
  2050. if (plane_config->size == 0)
  2051. return false;
  2052. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2053. plane_config->size);
  2054. if (!obj)
  2055. return false;
  2056. obj->tiling_mode = plane_config->tiling;
  2057. if (obj->tiling_mode == I915_TILING_X)
  2058. obj->stride = crtc->base.primary->fb->pitches[0];
  2059. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2060. mode_cmd.width = crtc->base.primary->fb->width;
  2061. mode_cmd.height = crtc->base.primary->fb->height;
  2062. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2063. mutex_lock(&dev->struct_mutex);
  2064. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2065. &mode_cmd, obj)) {
  2066. DRM_DEBUG_KMS("intel fb init failed\n");
  2067. goto out_unref_obj;
  2068. }
  2069. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2070. mutex_unlock(&dev->struct_mutex);
  2071. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2072. return true;
  2073. out_unref_obj:
  2074. drm_gem_object_unreference(&obj->base);
  2075. mutex_unlock(&dev->struct_mutex);
  2076. return false;
  2077. }
  2078. static void
  2079. intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2080. struct intel_initial_plane_config *plane_config)
  2081. {
  2082. struct drm_device *dev = intel_crtc->base.dev;
  2083. struct drm_i915_private *dev_priv = dev->dev_private;
  2084. struct drm_crtc *c;
  2085. struct intel_crtc *i;
  2086. struct drm_i915_gem_object *obj;
  2087. if (!intel_crtc->base.primary->fb)
  2088. return;
  2089. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2090. return;
  2091. kfree(intel_crtc->base.primary->fb);
  2092. intel_crtc->base.primary->fb = NULL;
  2093. /*
  2094. * Failed to alloc the obj, check to see if we should share
  2095. * an fb with another CRTC instead
  2096. */
  2097. for_each_crtc(dev, c) {
  2098. i = to_intel_crtc(c);
  2099. if (c == &intel_crtc->base)
  2100. continue;
  2101. if (!i->active)
  2102. continue;
  2103. obj = intel_fb_obj(c->primary->fb);
  2104. if (obj == NULL)
  2105. continue;
  2106. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2107. if (obj->tiling_mode != I915_TILING_NONE)
  2108. dev_priv->preserve_bios_swizzle = true;
  2109. drm_framebuffer_reference(c->primary->fb);
  2110. intel_crtc->base.primary->fb = c->primary->fb;
  2111. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2112. break;
  2113. }
  2114. }
  2115. }
  2116. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2117. struct drm_framebuffer *fb,
  2118. int x, int y)
  2119. {
  2120. struct drm_device *dev = crtc->dev;
  2121. struct drm_i915_private *dev_priv = dev->dev_private;
  2122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2123. struct drm_i915_gem_object *obj;
  2124. int plane = intel_crtc->plane;
  2125. unsigned long linear_offset;
  2126. u32 dspcntr;
  2127. u32 reg = DSPCNTR(plane);
  2128. int pixel_size;
  2129. if (!intel_crtc->primary_enabled) {
  2130. I915_WRITE(reg, 0);
  2131. if (INTEL_INFO(dev)->gen >= 4)
  2132. I915_WRITE(DSPSURF(plane), 0);
  2133. else
  2134. I915_WRITE(DSPADDR(plane), 0);
  2135. POSTING_READ(reg);
  2136. return;
  2137. }
  2138. obj = intel_fb_obj(fb);
  2139. if (WARN_ON(obj == NULL))
  2140. return;
  2141. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2142. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2143. dspcntr |= DISPLAY_PLANE_ENABLE;
  2144. if (INTEL_INFO(dev)->gen < 4) {
  2145. if (intel_crtc->pipe == PIPE_B)
  2146. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2147. /* pipesrc and dspsize control the size that is scaled from,
  2148. * which should always be the user's requested size.
  2149. */
  2150. I915_WRITE(DSPSIZE(plane),
  2151. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2152. (intel_crtc->config->pipe_src_w - 1));
  2153. I915_WRITE(DSPPOS(plane), 0);
  2154. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2155. I915_WRITE(PRIMSIZE(plane),
  2156. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2157. (intel_crtc->config->pipe_src_w - 1));
  2158. I915_WRITE(PRIMPOS(plane), 0);
  2159. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2160. }
  2161. switch (fb->pixel_format) {
  2162. case DRM_FORMAT_C8:
  2163. dspcntr |= DISPPLANE_8BPP;
  2164. break;
  2165. case DRM_FORMAT_XRGB1555:
  2166. case DRM_FORMAT_ARGB1555:
  2167. dspcntr |= DISPPLANE_BGRX555;
  2168. break;
  2169. case DRM_FORMAT_RGB565:
  2170. dspcntr |= DISPPLANE_BGRX565;
  2171. break;
  2172. case DRM_FORMAT_XRGB8888:
  2173. case DRM_FORMAT_ARGB8888:
  2174. dspcntr |= DISPPLANE_BGRX888;
  2175. break;
  2176. case DRM_FORMAT_XBGR8888:
  2177. case DRM_FORMAT_ABGR8888:
  2178. dspcntr |= DISPPLANE_RGBX888;
  2179. break;
  2180. case DRM_FORMAT_XRGB2101010:
  2181. case DRM_FORMAT_ARGB2101010:
  2182. dspcntr |= DISPPLANE_BGRX101010;
  2183. break;
  2184. case DRM_FORMAT_XBGR2101010:
  2185. case DRM_FORMAT_ABGR2101010:
  2186. dspcntr |= DISPPLANE_RGBX101010;
  2187. break;
  2188. default:
  2189. BUG();
  2190. }
  2191. if (INTEL_INFO(dev)->gen >= 4 &&
  2192. obj->tiling_mode != I915_TILING_NONE)
  2193. dspcntr |= DISPPLANE_TILED;
  2194. if (IS_G4X(dev))
  2195. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2196. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2197. if (INTEL_INFO(dev)->gen >= 4) {
  2198. intel_crtc->dspaddr_offset =
  2199. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2200. pixel_size,
  2201. fb->pitches[0]);
  2202. linear_offset -= intel_crtc->dspaddr_offset;
  2203. } else {
  2204. intel_crtc->dspaddr_offset = linear_offset;
  2205. }
  2206. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2207. dspcntr |= DISPPLANE_ROTATE_180;
  2208. x += (intel_crtc->config->pipe_src_w - 1);
  2209. y += (intel_crtc->config->pipe_src_h - 1);
  2210. /* Finding the last pixel of the last line of the display
  2211. data and adding to linear_offset*/
  2212. linear_offset +=
  2213. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2214. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2215. }
  2216. I915_WRITE(reg, dspcntr);
  2217. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2218. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2219. fb->pitches[0]);
  2220. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2221. if (INTEL_INFO(dev)->gen >= 4) {
  2222. I915_WRITE(DSPSURF(plane),
  2223. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2224. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2225. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2226. } else
  2227. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2228. POSTING_READ(reg);
  2229. }
  2230. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2231. struct drm_framebuffer *fb,
  2232. int x, int y)
  2233. {
  2234. struct drm_device *dev = crtc->dev;
  2235. struct drm_i915_private *dev_priv = dev->dev_private;
  2236. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2237. struct drm_i915_gem_object *obj;
  2238. int plane = intel_crtc->plane;
  2239. unsigned long linear_offset;
  2240. u32 dspcntr;
  2241. u32 reg = DSPCNTR(plane);
  2242. int pixel_size;
  2243. if (!intel_crtc->primary_enabled) {
  2244. I915_WRITE(reg, 0);
  2245. I915_WRITE(DSPSURF(plane), 0);
  2246. POSTING_READ(reg);
  2247. return;
  2248. }
  2249. obj = intel_fb_obj(fb);
  2250. if (WARN_ON(obj == NULL))
  2251. return;
  2252. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2253. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2254. dspcntr |= DISPLAY_PLANE_ENABLE;
  2255. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2256. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2257. switch (fb->pixel_format) {
  2258. case DRM_FORMAT_C8:
  2259. dspcntr |= DISPPLANE_8BPP;
  2260. break;
  2261. case DRM_FORMAT_RGB565:
  2262. dspcntr |= DISPPLANE_BGRX565;
  2263. break;
  2264. case DRM_FORMAT_XRGB8888:
  2265. case DRM_FORMAT_ARGB8888:
  2266. dspcntr |= DISPPLANE_BGRX888;
  2267. break;
  2268. case DRM_FORMAT_XBGR8888:
  2269. case DRM_FORMAT_ABGR8888:
  2270. dspcntr |= DISPPLANE_RGBX888;
  2271. break;
  2272. case DRM_FORMAT_XRGB2101010:
  2273. case DRM_FORMAT_ARGB2101010:
  2274. dspcntr |= DISPPLANE_BGRX101010;
  2275. break;
  2276. case DRM_FORMAT_XBGR2101010:
  2277. case DRM_FORMAT_ABGR2101010:
  2278. dspcntr |= DISPPLANE_RGBX101010;
  2279. break;
  2280. default:
  2281. BUG();
  2282. }
  2283. if (obj->tiling_mode != I915_TILING_NONE)
  2284. dspcntr |= DISPPLANE_TILED;
  2285. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2286. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2287. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2288. intel_crtc->dspaddr_offset =
  2289. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2290. pixel_size,
  2291. fb->pitches[0]);
  2292. linear_offset -= intel_crtc->dspaddr_offset;
  2293. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2294. dspcntr |= DISPPLANE_ROTATE_180;
  2295. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2296. x += (intel_crtc->config->pipe_src_w - 1);
  2297. y += (intel_crtc->config->pipe_src_h - 1);
  2298. /* Finding the last pixel of the last line of the display
  2299. data and adding to linear_offset*/
  2300. linear_offset +=
  2301. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2302. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2303. }
  2304. }
  2305. I915_WRITE(reg, dspcntr);
  2306. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2307. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2308. fb->pitches[0]);
  2309. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2310. I915_WRITE(DSPSURF(plane),
  2311. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2312. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2313. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2314. } else {
  2315. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2316. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2317. }
  2318. POSTING_READ(reg);
  2319. }
  2320. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2321. struct drm_framebuffer *fb,
  2322. int x, int y)
  2323. {
  2324. struct drm_device *dev = crtc->dev;
  2325. struct drm_i915_private *dev_priv = dev->dev_private;
  2326. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2327. struct intel_framebuffer *intel_fb;
  2328. struct drm_i915_gem_object *obj;
  2329. int pipe = intel_crtc->pipe;
  2330. u32 plane_ctl, stride;
  2331. if (!intel_crtc->primary_enabled) {
  2332. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2333. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2334. POSTING_READ(PLANE_CTL(pipe, 0));
  2335. return;
  2336. }
  2337. plane_ctl = PLANE_CTL_ENABLE |
  2338. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2339. PLANE_CTL_PIPE_CSC_ENABLE;
  2340. switch (fb->pixel_format) {
  2341. case DRM_FORMAT_RGB565:
  2342. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2343. break;
  2344. case DRM_FORMAT_XRGB8888:
  2345. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2346. break;
  2347. case DRM_FORMAT_XBGR8888:
  2348. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2349. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2350. break;
  2351. case DRM_FORMAT_XRGB2101010:
  2352. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2353. break;
  2354. case DRM_FORMAT_XBGR2101010:
  2355. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2356. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2357. break;
  2358. default:
  2359. BUG();
  2360. }
  2361. intel_fb = to_intel_framebuffer(fb);
  2362. obj = intel_fb->obj;
  2363. /*
  2364. * The stride is either expressed as a multiple of 64 bytes chunks for
  2365. * linear buffers or in number of tiles for tiled buffers.
  2366. */
  2367. switch (obj->tiling_mode) {
  2368. case I915_TILING_NONE:
  2369. stride = fb->pitches[0] >> 6;
  2370. break;
  2371. case I915_TILING_X:
  2372. plane_ctl |= PLANE_CTL_TILED_X;
  2373. stride = fb->pitches[0] >> 9;
  2374. break;
  2375. default:
  2376. BUG();
  2377. }
  2378. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2379. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
  2380. plane_ctl |= PLANE_CTL_ROTATE_180;
  2381. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2382. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2383. i915_gem_obj_ggtt_offset(obj),
  2384. x, y, fb->width, fb->height,
  2385. fb->pitches[0]);
  2386. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2387. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2388. I915_WRITE(PLANE_SIZE(pipe, 0),
  2389. (intel_crtc->config->pipe_src_h - 1) << 16 |
  2390. (intel_crtc->config->pipe_src_w - 1));
  2391. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2392. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2393. POSTING_READ(PLANE_SURF(pipe, 0));
  2394. }
  2395. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2396. static int
  2397. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2398. int x, int y, enum mode_set_atomic state)
  2399. {
  2400. struct drm_device *dev = crtc->dev;
  2401. struct drm_i915_private *dev_priv = dev->dev_private;
  2402. if (dev_priv->display.disable_fbc)
  2403. dev_priv->display.disable_fbc(dev);
  2404. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2405. return 0;
  2406. }
  2407. static void intel_complete_page_flips(struct drm_device *dev)
  2408. {
  2409. struct drm_crtc *crtc;
  2410. for_each_crtc(dev, crtc) {
  2411. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2412. enum plane plane = intel_crtc->plane;
  2413. intel_prepare_page_flip(dev, plane);
  2414. intel_finish_page_flip_plane(dev, plane);
  2415. }
  2416. }
  2417. static void intel_update_primary_planes(struct drm_device *dev)
  2418. {
  2419. struct drm_i915_private *dev_priv = dev->dev_private;
  2420. struct drm_crtc *crtc;
  2421. for_each_crtc(dev, crtc) {
  2422. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2423. drm_modeset_lock(&crtc->mutex, NULL);
  2424. /*
  2425. * FIXME: Once we have proper support for primary planes (and
  2426. * disabling them without disabling the entire crtc) allow again
  2427. * a NULL crtc->primary->fb.
  2428. */
  2429. if (intel_crtc->active && crtc->primary->fb)
  2430. dev_priv->display.update_primary_plane(crtc,
  2431. crtc->primary->fb,
  2432. crtc->x,
  2433. crtc->y);
  2434. drm_modeset_unlock(&crtc->mutex);
  2435. }
  2436. }
  2437. void intel_prepare_reset(struct drm_device *dev)
  2438. {
  2439. struct drm_i915_private *dev_priv = to_i915(dev);
  2440. struct intel_crtc *crtc;
  2441. /* no reset support for gen2 */
  2442. if (IS_GEN2(dev))
  2443. return;
  2444. /* reset doesn't touch the display */
  2445. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2446. return;
  2447. drm_modeset_lock_all(dev);
  2448. /*
  2449. * Disabling the crtcs gracefully seems nicer. Also the
  2450. * g33 docs say we should at least disable all the planes.
  2451. */
  2452. for_each_intel_crtc(dev, crtc) {
  2453. if (crtc->active)
  2454. dev_priv->display.crtc_disable(&crtc->base);
  2455. }
  2456. }
  2457. void intel_finish_reset(struct drm_device *dev)
  2458. {
  2459. struct drm_i915_private *dev_priv = to_i915(dev);
  2460. /*
  2461. * Flips in the rings will be nuked by the reset,
  2462. * so complete all pending flips so that user space
  2463. * will get its events and not get stuck.
  2464. */
  2465. intel_complete_page_flips(dev);
  2466. /* no reset support for gen2 */
  2467. if (IS_GEN2(dev))
  2468. return;
  2469. /* reset doesn't touch the display */
  2470. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2471. /*
  2472. * Flips in the rings have been nuked by the reset,
  2473. * so update the base address of all primary
  2474. * planes to the the last fb to make sure we're
  2475. * showing the correct fb after a reset.
  2476. */
  2477. intel_update_primary_planes(dev);
  2478. return;
  2479. }
  2480. /*
  2481. * The display has been reset as well,
  2482. * so need a full re-initialization.
  2483. */
  2484. intel_runtime_pm_disable_interrupts(dev_priv);
  2485. intel_runtime_pm_enable_interrupts(dev_priv);
  2486. intel_modeset_init_hw(dev);
  2487. spin_lock_irq(&dev_priv->irq_lock);
  2488. if (dev_priv->display.hpd_irq_setup)
  2489. dev_priv->display.hpd_irq_setup(dev);
  2490. spin_unlock_irq(&dev_priv->irq_lock);
  2491. intel_modeset_setup_hw_state(dev, true);
  2492. intel_hpd_init(dev_priv);
  2493. drm_modeset_unlock_all(dev);
  2494. }
  2495. static int
  2496. intel_finish_fb(struct drm_framebuffer *old_fb)
  2497. {
  2498. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2499. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2500. bool was_interruptible = dev_priv->mm.interruptible;
  2501. int ret;
  2502. /* Big Hammer, we also need to ensure that any pending
  2503. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2504. * current scanout is retired before unpinning the old
  2505. * framebuffer.
  2506. *
  2507. * This should only fail upon a hung GPU, in which case we
  2508. * can safely continue.
  2509. */
  2510. dev_priv->mm.interruptible = false;
  2511. ret = i915_gem_object_finish_gpu(obj);
  2512. dev_priv->mm.interruptible = was_interruptible;
  2513. return ret;
  2514. }
  2515. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2516. {
  2517. struct drm_device *dev = crtc->dev;
  2518. struct drm_i915_private *dev_priv = dev->dev_private;
  2519. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2520. bool pending;
  2521. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2522. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2523. return false;
  2524. spin_lock_irq(&dev->event_lock);
  2525. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2526. spin_unlock_irq(&dev->event_lock);
  2527. return pending;
  2528. }
  2529. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2530. {
  2531. struct drm_device *dev = crtc->base.dev;
  2532. struct drm_i915_private *dev_priv = dev->dev_private;
  2533. const struct drm_display_mode *adjusted_mode;
  2534. if (!i915.fastboot)
  2535. return;
  2536. /*
  2537. * Update pipe size and adjust fitter if needed: the reason for this is
  2538. * that in compute_mode_changes we check the native mode (not the pfit
  2539. * mode) to see if we can flip rather than do a full mode set. In the
  2540. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2541. * pfit state, we'll end up with a big fb scanned out into the wrong
  2542. * sized surface.
  2543. *
  2544. * To fix this properly, we need to hoist the checks up into
  2545. * compute_mode_changes (or above), check the actual pfit state and
  2546. * whether the platform allows pfit disable with pipe active, and only
  2547. * then update the pipesrc and pfit state, even on the flip path.
  2548. */
  2549. adjusted_mode = &crtc->config->base.adjusted_mode;
  2550. I915_WRITE(PIPESRC(crtc->pipe),
  2551. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2552. (adjusted_mode->crtc_vdisplay - 1));
  2553. if (!crtc->config->pch_pfit.enabled &&
  2554. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2555. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2556. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2557. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2558. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2559. }
  2560. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2561. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2562. }
  2563. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2564. {
  2565. struct drm_device *dev = crtc->dev;
  2566. struct drm_i915_private *dev_priv = dev->dev_private;
  2567. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2568. int pipe = intel_crtc->pipe;
  2569. u32 reg, temp;
  2570. /* enable normal train */
  2571. reg = FDI_TX_CTL(pipe);
  2572. temp = I915_READ(reg);
  2573. if (IS_IVYBRIDGE(dev)) {
  2574. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2575. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2576. } else {
  2577. temp &= ~FDI_LINK_TRAIN_NONE;
  2578. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2579. }
  2580. I915_WRITE(reg, temp);
  2581. reg = FDI_RX_CTL(pipe);
  2582. temp = I915_READ(reg);
  2583. if (HAS_PCH_CPT(dev)) {
  2584. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2585. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2586. } else {
  2587. temp &= ~FDI_LINK_TRAIN_NONE;
  2588. temp |= FDI_LINK_TRAIN_NONE;
  2589. }
  2590. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2591. /* wait one idle pattern time */
  2592. POSTING_READ(reg);
  2593. udelay(1000);
  2594. /* IVB wants error correction enabled */
  2595. if (IS_IVYBRIDGE(dev))
  2596. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2597. FDI_FE_ERRC_ENABLE);
  2598. }
  2599. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2600. {
  2601. return crtc->base.enabled && crtc->active &&
  2602. crtc->config->has_pch_encoder;
  2603. }
  2604. static void ivb_modeset_global_resources(struct drm_device *dev)
  2605. {
  2606. struct drm_i915_private *dev_priv = dev->dev_private;
  2607. struct intel_crtc *pipe_B_crtc =
  2608. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2609. struct intel_crtc *pipe_C_crtc =
  2610. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2611. uint32_t temp;
  2612. /*
  2613. * When everything is off disable fdi C so that we could enable fdi B
  2614. * with all lanes. Note that we don't care about enabled pipes without
  2615. * an enabled pch encoder.
  2616. */
  2617. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2618. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2619. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2620. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2621. temp = I915_READ(SOUTH_CHICKEN1);
  2622. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2623. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2624. I915_WRITE(SOUTH_CHICKEN1, temp);
  2625. }
  2626. }
  2627. /* The FDI link training functions for ILK/Ibexpeak. */
  2628. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2629. {
  2630. struct drm_device *dev = crtc->dev;
  2631. struct drm_i915_private *dev_priv = dev->dev_private;
  2632. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2633. int pipe = intel_crtc->pipe;
  2634. u32 reg, temp, tries;
  2635. /* FDI needs bits from pipe first */
  2636. assert_pipe_enabled(dev_priv, pipe);
  2637. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2638. for train result */
  2639. reg = FDI_RX_IMR(pipe);
  2640. temp = I915_READ(reg);
  2641. temp &= ~FDI_RX_SYMBOL_LOCK;
  2642. temp &= ~FDI_RX_BIT_LOCK;
  2643. I915_WRITE(reg, temp);
  2644. I915_READ(reg);
  2645. udelay(150);
  2646. /* enable CPU FDI TX and PCH FDI RX */
  2647. reg = FDI_TX_CTL(pipe);
  2648. temp = I915_READ(reg);
  2649. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2650. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2651. temp &= ~FDI_LINK_TRAIN_NONE;
  2652. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2653. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2654. reg = FDI_RX_CTL(pipe);
  2655. temp = I915_READ(reg);
  2656. temp &= ~FDI_LINK_TRAIN_NONE;
  2657. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2658. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2659. POSTING_READ(reg);
  2660. udelay(150);
  2661. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2662. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2663. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2664. FDI_RX_PHASE_SYNC_POINTER_EN);
  2665. reg = FDI_RX_IIR(pipe);
  2666. for (tries = 0; tries < 5; tries++) {
  2667. temp = I915_READ(reg);
  2668. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2669. if ((temp & FDI_RX_BIT_LOCK)) {
  2670. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2671. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2672. break;
  2673. }
  2674. }
  2675. if (tries == 5)
  2676. DRM_ERROR("FDI train 1 fail!\n");
  2677. /* Train 2 */
  2678. reg = FDI_TX_CTL(pipe);
  2679. temp = I915_READ(reg);
  2680. temp &= ~FDI_LINK_TRAIN_NONE;
  2681. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2682. I915_WRITE(reg, temp);
  2683. reg = FDI_RX_CTL(pipe);
  2684. temp = I915_READ(reg);
  2685. temp &= ~FDI_LINK_TRAIN_NONE;
  2686. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2687. I915_WRITE(reg, temp);
  2688. POSTING_READ(reg);
  2689. udelay(150);
  2690. reg = FDI_RX_IIR(pipe);
  2691. for (tries = 0; tries < 5; tries++) {
  2692. temp = I915_READ(reg);
  2693. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2694. if (temp & FDI_RX_SYMBOL_LOCK) {
  2695. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2696. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2697. break;
  2698. }
  2699. }
  2700. if (tries == 5)
  2701. DRM_ERROR("FDI train 2 fail!\n");
  2702. DRM_DEBUG_KMS("FDI train done\n");
  2703. }
  2704. static const int snb_b_fdi_train_param[] = {
  2705. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2706. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2707. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2708. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2709. };
  2710. /* The FDI link training functions for SNB/Cougarpoint. */
  2711. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2712. {
  2713. struct drm_device *dev = crtc->dev;
  2714. struct drm_i915_private *dev_priv = dev->dev_private;
  2715. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2716. int pipe = intel_crtc->pipe;
  2717. u32 reg, temp, i, retry;
  2718. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2719. for train result */
  2720. reg = FDI_RX_IMR(pipe);
  2721. temp = I915_READ(reg);
  2722. temp &= ~FDI_RX_SYMBOL_LOCK;
  2723. temp &= ~FDI_RX_BIT_LOCK;
  2724. I915_WRITE(reg, temp);
  2725. POSTING_READ(reg);
  2726. udelay(150);
  2727. /* enable CPU FDI TX and PCH FDI RX */
  2728. reg = FDI_TX_CTL(pipe);
  2729. temp = I915_READ(reg);
  2730. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2731. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2732. temp &= ~FDI_LINK_TRAIN_NONE;
  2733. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2734. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2735. /* SNB-B */
  2736. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2737. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2738. I915_WRITE(FDI_RX_MISC(pipe),
  2739. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2740. reg = FDI_RX_CTL(pipe);
  2741. temp = I915_READ(reg);
  2742. if (HAS_PCH_CPT(dev)) {
  2743. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2744. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2745. } else {
  2746. temp &= ~FDI_LINK_TRAIN_NONE;
  2747. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2748. }
  2749. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2750. POSTING_READ(reg);
  2751. udelay(150);
  2752. for (i = 0; i < 4; i++) {
  2753. reg = FDI_TX_CTL(pipe);
  2754. temp = I915_READ(reg);
  2755. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2756. temp |= snb_b_fdi_train_param[i];
  2757. I915_WRITE(reg, temp);
  2758. POSTING_READ(reg);
  2759. udelay(500);
  2760. for (retry = 0; retry < 5; retry++) {
  2761. reg = FDI_RX_IIR(pipe);
  2762. temp = I915_READ(reg);
  2763. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2764. if (temp & FDI_RX_BIT_LOCK) {
  2765. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2766. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2767. break;
  2768. }
  2769. udelay(50);
  2770. }
  2771. if (retry < 5)
  2772. break;
  2773. }
  2774. if (i == 4)
  2775. DRM_ERROR("FDI train 1 fail!\n");
  2776. /* Train 2 */
  2777. reg = FDI_TX_CTL(pipe);
  2778. temp = I915_READ(reg);
  2779. temp &= ~FDI_LINK_TRAIN_NONE;
  2780. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2781. if (IS_GEN6(dev)) {
  2782. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2783. /* SNB-B */
  2784. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2785. }
  2786. I915_WRITE(reg, temp);
  2787. reg = FDI_RX_CTL(pipe);
  2788. temp = I915_READ(reg);
  2789. if (HAS_PCH_CPT(dev)) {
  2790. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2791. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2792. } else {
  2793. temp &= ~FDI_LINK_TRAIN_NONE;
  2794. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2795. }
  2796. I915_WRITE(reg, temp);
  2797. POSTING_READ(reg);
  2798. udelay(150);
  2799. for (i = 0; i < 4; i++) {
  2800. reg = FDI_TX_CTL(pipe);
  2801. temp = I915_READ(reg);
  2802. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2803. temp |= snb_b_fdi_train_param[i];
  2804. I915_WRITE(reg, temp);
  2805. POSTING_READ(reg);
  2806. udelay(500);
  2807. for (retry = 0; retry < 5; retry++) {
  2808. reg = FDI_RX_IIR(pipe);
  2809. temp = I915_READ(reg);
  2810. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2811. if (temp & FDI_RX_SYMBOL_LOCK) {
  2812. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2813. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2814. break;
  2815. }
  2816. udelay(50);
  2817. }
  2818. if (retry < 5)
  2819. break;
  2820. }
  2821. if (i == 4)
  2822. DRM_ERROR("FDI train 2 fail!\n");
  2823. DRM_DEBUG_KMS("FDI train done.\n");
  2824. }
  2825. /* Manual link training for Ivy Bridge A0 parts */
  2826. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2827. {
  2828. struct drm_device *dev = crtc->dev;
  2829. struct drm_i915_private *dev_priv = dev->dev_private;
  2830. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2831. int pipe = intel_crtc->pipe;
  2832. u32 reg, temp, i, j;
  2833. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2834. for train result */
  2835. reg = FDI_RX_IMR(pipe);
  2836. temp = I915_READ(reg);
  2837. temp &= ~FDI_RX_SYMBOL_LOCK;
  2838. temp &= ~FDI_RX_BIT_LOCK;
  2839. I915_WRITE(reg, temp);
  2840. POSTING_READ(reg);
  2841. udelay(150);
  2842. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2843. I915_READ(FDI_RX_IIR(pipe)));
  2844. /* Try each vswing and preemphasis setting twice before moving on */
  2845. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2846. /* disable first in case we need to retry */
  2847. reg = FDI_TX_CTL(pipe);
  2848. temp = I915_READ(reg);
  2849. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2850. temp &= ~FDI_TX_ENABLE;
  2851. I915_WRITE(reg, temp);
  2852. reg = FDI_RX_CTL(pipe);
  2853. temp = I915_READ(reg);
  2854. temp &= ~FDI_LINK_TRAIN_AUTO;
  2855. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2856. temp &= ~FDI_RX_ENABLE;
  2857. I915_WRITE(reg, temp);
  2858. /* enable CPU FDI TX and PCH FDI RX */
  2859. reg = FDI_TX_CTL(pipe);
  2860. temp = I915_READ(reg);
  2861. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2862. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2863. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2864. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2865. temp |= snb_b_fdi_train_param[j/2];
  2866. temp |= FDI_COMPOSITE_SYNC;
  2867. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2868. I915_WRITE(FDI_RX_MISC(pipe),
  2869. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2870. reg = FDI_RX_CTL(pipe);
  2871. temp = I915_READ(reg);
  2872. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2873. temp |= FDI_COMPOSITE_SYNC;
  2874. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2875. POSTING_READ(reg);
  2876. udelay(1); /* should be 0.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_BIT_LOCK ||
  2882. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2883. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2884. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2885. i);
  2886. break;
  2887. }
  2888. udelay(1); /* should be 0.5us */
  2889. }
  2890. if (i == 4) {
  2891. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2892. continue;
  2893. }
  2894. /* Train 2 */
  2895. reg = FDI_TX_CTL(pipe);
  2896. temp = I915_READ(reg);
  2897. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2898. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2899. I915_WRITE(reg, temp);
  2900. reg = FDI_RX_CTL(pipe);
  2901. temp = I915_READ(reg);
  2902. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2903. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2904. I915_WRITE(reg, temp);
  2905. POSTING_READ(reg);
  2906. udelay(2); /* should be 1.5us */
  2907. for (i = 0; i < 4; i++) {
  2908. reg = FDI_RX_IIR(pipe);
  2909. temp = I915_READ(reg);
  2910. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2911. if (temp & FDI_RX_SYMBOL_LOCK ||
  2912. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2913. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2914. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2915. i);
  2916. goto train_done;
  2917. }
  2918. udelay(2); /* should be 1.5us */
  2919. }
  2920. if (i == 4)
  2921. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2922. }
  2923. train_done:
  2924. DRM_DEBUG_KMS("FDI train done.\n");
  2925. }
  2926. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2927. {
  2928. struct drm_device *dev = intel_crtc->base.dev;
  2929. struct drm_i915_private *dev_priv = dev->dev_private;
  2930. int pipe = intel_crtc->pipe;
  2931. u32 reg, temp;
  2932. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2933. reg = FDI_RX_CTL(pipe);
  2934. temp = I915_READ(reg);
  2935. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2936. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2937. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2938. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2939. POSTING_READ(reg);
  2940. udelay(200);
  2941. /* Switch from Rawclk to PCDclk */
  2942. temp = I915_READ(reg);
  2943. I915_WRITE(reg, temp | FDI_PCDCLK);
  2944. POSTING_READ(reg);
  2945. udelay(200);
  2946. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2947. reg = FDI_TX_CTL(pipe);
  2948. temp = I915_READ(reg);
  2949. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2950. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2951. POSTING_READ(reg);
  2952. udelay(100);
  2953. }
  2954. }
  2955. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2956. {
  2957. struct drm_device *dev = intel_crtc->base.dev;
  2958. struct drm_i915_private *dev_priv = dev->dev_private;
  2959. int pipe = intel_crtc->pipe;
  2960. u32 reg, temp;
  2961. /* Switch from PCDclk to Rawclk */
  2962. reg = FDI_RX_CTL(pipe);
  2963. temp = I915_READ(reg);
  2964. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2965. /* Disable CPU FDI TX PLL */
  2966. reg = FDI_TX_CTL(pipe);
  2967. temp = I915_READ(reg);
  2968. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2969. POSTING_READ(reg);
  2970. udelay(100);
  2971. reg = FDI_RX_CTL(pipe);
  2972. temp = I915_READ(reg);
  2973. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2974. /* Wait for the clocks to turn off. */
  2975. POSTING_READ(reg);
  2976. udelay(100);
  2977. }
  2978. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2979. {
  2980. struct drm_device *dev = crtc->dev;
  2981. struct drm_i915_private *dev_priv = dev->dev_private;
  2982. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2983. int pipe = intel_crtc->pipe;
  2984. u32 reg, temp;
  2985. /* disable CPU FDI tx and PCH FDI rx */
  2986. reg = FDI_TX_CTL(pipe);
  2987. temp = I915_READ(reg);
  2988. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2989. POSTING_READ(reg);
  2990. reg = FDI_RX_CTL(pipe);
  2991. temp = I915_READ(reg);
  2992. temp &= ~(0x7 << 16);
  2993. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2994. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2995. POSTING_READ(reg);
  2996. udelay(100);
  2997. /* Ironlake workaround, disable clock pointer after downing FDI */
  2998. if (HAS_PCH_IBX(dev))
  2999. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3000. /* still set train pattern 1 */
  3001. reg = FDI_TX_CTL(pipe);
  3002. temp = I915_READ(reg);
  3003. temp &= ~FDI_LINK_TRAIN_NONE;
  3004. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3005. I915_WRITE(reg, temp);
  3006. reg = FDI_RX_CTL(pipe);
  3007. temp = I915_READ(reg);
  3008. if (HAS_PCH_CPT(dev)) {
  3009. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3010. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3011. } else {
  3012. temp &= ~FDI_LINK_TRAIN_NONE;
  3013. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3014. }
  3015. /* BPC in FDI rx is consistent with that in PIPECONF */
  3016. temp &= ~(0x07 << 16);
  3017. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3018. I915_WRITE(reg, temp);
  3019. POSTING_READ(reg);
  3020. udelay(100);
  3021. }
  3022. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3023. {
  3024. struct intel_crtc *crtc;
  3025. /* Note that we don't need to be called with mode_config.lock here
  3026. * as our list of CRTC objects is static for the lifetime of the
  3027. * device and so cannot disappear as we iterate. Similarly, we can
  3028. * happily treat the predicates as racy, atomic checks as userspace
  3029. * cannot claim and pin a new fb without at least acquring the
  3030. * struct_mutex and so serialising with us.
  3031. */
  3032. for_each_intel_crtc(dev, crtc) {
  3033. if (atomic_read(&crtc->unpin_work_count) == 0)
  3034. continue;
  3035. if (crtc->unpin_work)
  3036. intel_wait_for_vblank(dev, crtc->pipe);
  3037. return true;
  3038. }
  3039. return false;
  3040. }
  3041. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3042. {
  3043. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3044. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3045. /* ensure that the unpin work is consistent wrt ->pending. */
  3046. smp_rmb();
  3047. intel_crtc->unpin_work = NULL;
  3048. if (work->event)
  3049. drm_send_vblank_event(intel_crtc->base.dev,
  3050. intel_crtc->pipe,
  3051. work->event);
  3052. drm_crtc_vblank_put(&intel_crtc->base);
  3053. wake_up_all(&dev_priv->pending_flip_queue);
  3054. queue_work(dev_priv->wq, &work->work);
  3055. trace_i915_flip_complete(intel_crtc->plane,
  3056. work->pending_flip_obj);
  3057. }
  3058. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3059. {
  3060. struct drm_device *dev = crtc->dev;
  3061. struct drm_i915_private *dev_priv = dev->dev_private;
  3062. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3063. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3064. !intel_crtc_has_pending_flip(crtc),
  3065. 60*HZ) == 0)) {
  3066. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3067. spin_lock_irq(&dev->event_lock);
  3068. if (intel_crtc->unpin_work) {
  3069. WARN_ONCE(1, "Removing stuck page flip\n");
  3070. page_flip_completed(intel_crtc);
  3071. }
  3072. spin_unlock_irq(&dev->event_lock);
  3073. }
  3074. if (crtc->primary->fb) {
  3075. mutex_lock(&dev->struct_mutex);
  3076. intel_finish_fb(crtc->primary->fb);
  3077. mutex_unlock(&dev->struct_mutex);
  3078. }
  3079. }
  3080. /* Program iCLKIP clock to the desired frequency */
  3081. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3082. {
  3083. struct drm_device *dev = crtc->dev;
  3084. struct drm_i915_private *dev_priv = dev->dev_private;
  3085. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3086. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3087. u32 temp;
  3088. mutex_lock(&dev_priv->dpio_lock);
  3089. /* It is necessary to ungate the pixclk gate prior to programming
  3090. * the divisors, and gate it back when it is done.
  3091. */
  3092. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3093. /* Disable SSCCTL */
  3094. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3095. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3096. SBI_SSCCTL_DISABLE,
  3097. SBI_ICLK);
  3098. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3099. if (clock == 20000) {
  3100. auxdiv = 1;
  3101. divsel = 0x41;
  3102. phaseinc = 0x20;
  3103. } else {
  3104. /* The iCLK virtual clock root frequency is in MHz,
  3105. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3106. * divisors, it is necessary to divide one by another, so we
  3107. * convert the virtual clock precision to KHz here for higher
  3108. * precision.
  3109. */
  3110. u32 iclk_virtual_root_freq = 172800 * 1000;
  3111. u32 iclk_pi_range = 64;
  3112. u32 desired_divisor, msb_divisor_value, pi_value;
  3113. desired_divisor = (iclk_virtual_root_freq / clock);
  3114. msb_divisor_value = desired_divisor / iclk_pi_range;
  3115. pi_value = desired_divisor % iclk_pi_range;
  3116. auxdiv = 0;
  3117. divsel = msb_divisor_value - 2;
  3118. phaseinc = pi_value;
  3119. }
  3120. /* This should not happen with any sane values */
  3121. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3122. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3123. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3124. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3125. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3126. clock,
  3127. auxdiv,
  3128. divsel,
  3129. phasedir,
  3130. phaseinc);
  3131. /* Program SSCDIVINTPHASE6 */
  3132. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3133. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3134. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3135. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3136. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3137. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3138. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3139. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3140. /* Program SSCAUXDIV */
  3141. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3142. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3143. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3144. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3145. /* Enable modulator and associated divider */
  3146. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3147. temp &= ~SBI_SSCCTL_DISABLE;
  3148. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3149. /* Wait for initialization time */
  3150. udelay(24);
  3151. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3152. mutex_unlock(&dev_priv->dpio_lock);
  3153. }
  3154. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3155. enum pipe pch_transcoder)
  3156. {
  3157. struct drm_device *dev = crtc->base.dev;
  3158. struct drm_i915_private *dev_priv = dev->dev_private;
  3159. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3160. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3161. I915_READ(HTOTAL(cpu_transcoder)));
  3162. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3163. I915_READ(HBLANK(cpu_transcoder)));
  3164. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3165. I915_READ(HSYNC(cpu_transcoder)));
  3166. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3167. I915_READ(VTOTAL(cpu_transcoder)));
  3168. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3169. I915_READ(VBLANK(cpu_transcoder)));
  3170. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3171. I915_READ(VSYNC(cpu_transcoder)));
  3172. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3173. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3174. }
  3175. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3176. {
  3177. struct drm_i915_private *dev_priv = dev->dev_private;
  3178. uint32_t temp;
  3179. temp = I915_READ(SOUTH_CHICKEN1);
  3180. if (temp & FDI_BC_BIFURCATION_SELECT)
  3181. return;
  3182. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3183. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3184. temp |= FDI_BC_BIFURCATION_SELECT;
  3185. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3186. I915_WRITE(SOUTH_CHICKEN1, temp);
  3187. POSTING_READ(SOUTH_CHICKEN1);
  3188. }
  3189. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3190. {
  3191. struct drm_device *dev = intel_crtc->base.dev;
  3192. struct drm_i915_private *dev_priv = dev->dev_private;
  3193. switch (intel_crtc->pipe) {
  3194. case PIPE_A:
  3195. break;
  3196. case PIPE_B:
  3197. if (intel_crtc->config->fdi_lanes > 2)
  3198. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3199. else
  3200. cpt_enable_fdi_bc_bifurcation(dev);
  3201. break;
  3202. case PIPE_C:
  3203. cpt_enable_fdi_bc_bifurcation(dev);
  3204. break;
  3205. default:
  3206. BUG();
  3207. }
  3208. }
  3209. /*
  3210. * Enable PCH resources required for PCH ports:
  3211. * - PCH PLLs
  3212. * - FDI training & RX/TX
  3213. * - update transcoder timings
  3214. * - DP transcoding bits
  3215. * - transcoder
  3216. */
  3217. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3218. {
  3219. struct drm_device *dev = crtc->dev;
  3220. struct drm_i915_private *dev_priv = dev->dev_private;
  3221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3222. int pipe = intel_crtc->pipe;
  3223. u32 reg, temp;
  3224. assert_pch_transcoder_disabled(dev_priv, pipe);
  3225. if (IS_IVYBRIDGE(dev))
  3226. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3227. /* Write the TU size bits before fdi link training, so that error
  3228. * detection works. */
  3229. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3230. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3231. /* For PCH output, training FDI link */
  3232. dev_priv->display.fdi_link_train(crtc);
  3233. /* We need to program the right clock selection before writing the pixel
  3234. * mutliplier into the DPLL. */
  3235. if (HAS_PCH_CPT(dev)) {
  3236. u32 sel;
  3237. temp = I915_READ(PCH_DPLL_SEL);
  3238. temp |= TRANS_DPLL_ENABLE(pipe);
  3239. sel = TRANS_DPLLB_SEL(pipe);
  3240. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3241. temp |= sel;
  3242. else
  3243. temp &= ~sel;
  3244. I915_WRITE(PCH_DPLL_SEL, temp);
  3245. }
  3246. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3247. * transcoder, and we actually should do this to not upset any PCH
  3248. * transcoder that already use the clock when we share it.
  3249. *
  3250. * Note that enable_shared_dpll tries to do the right thing, but
  3251. * get_shared_dpll unconditionally resets the pll - we need that to have
  3252. * the right LVDS enable sequence. */
  3253. intel_enable_shared_dpll(intel_crtc);
  3254. /* set transcoder timing, panel must allow it */
  3255. assert_panel_unlocked(dev_priv, pipe);
  3256. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3257. intel_fdi_normal_train(crtc);
  3258. /* For PCH DP, enable TRANS_DP_CTL */
  3259. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3260. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3261. reg = TRANS_DP_CTL(pipe);
  3262. temp = I915_READ(reg);
  3263. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3264. TRANS_DP_SYNC_MASK |
  3265. TRANS_DP_BPC_MASK);
  3266. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3267. TRANS_DP_ENH_FRAMING);
  3268. temp |= bpc << 9; /* same format but at 11:9 */
  3269. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3270. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3271. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3272. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3273. switch (intel_trans_dp_port_sel(crtc)) {
  3274. case PCH_DP_B:
  3275. temp |= TRANS_DP_PORT_SEL_B;
  3276. break;
  3277. case PCH_DP_C:
  3278. temp |= TRANS_DP_PORT_SEL_C;
  3279. break;
  3280. case PCH_DP_D:
  3281. temp |= TRANS_DP_PORT_SEL_D;
  3282. break;
  3283. default:
  3284. BUG();
  3285. }
  3286. I915_WRITE(reg, temp);
  3287. }
  3288. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3289. }
  3290. static void lpt_pch_enable(struct drm_crtc *crtc)
  3291. {
  3292. struct drm_device *dev = crtc->dev;
  3293. struct drm_i915_private *dev_priv = dev->dev_private;
  3294. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3295. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3296. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3297. lpt_program_iclkip(crtc);
  3298. /* Set transcoder timing. */
  3299. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3300. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3301. }
  3302. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3303. {
  3304. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3305. if (pll == NULL)
  3306. return;
  3307. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3308. WARN(1, "bad %s crtc mask\n", pll->name);
  3309. return;
  3310. }
  3311. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3312. if (pll->config.crtc_mask == 0) {
  3313. WARN_ON(pll->on);
  3314. WARN_ON(pll->active);
  3315. }
  3316. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3317. }
  3318. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3319. struct intel_crtc_state *crtc_state)
  3320. {
  3321. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3322. struct intel_shared_dpll *pll;
  3323. enum intel_dpll_id i;
  3324. if (HAS_PCH_IBX(dev_priv->dev)) {
  3325. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3326. i = (enum intel_dpll_id) crtc->pipe;
  3327. pll = &dev_priv->shared_dplls[i];
  3328. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3329. crtc->base.base.id, pll->name);
  3330. WARN_ON(pll->new_config->crtc_mask);
  3331. goto found;
  3332. }
  3333. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3334. pll = &dev_priv->shared_dplls[i];
  3335. /* Only want to check enabled timings first */
  3336. if (pll->new_config->crtc_mask == 0)
  3337. continue;
  3338. if (memcmp(&crtc_state->dpll_hw_state,
  3339. &pll->new_config->hw_state,
  3340. sizeof(pll->new_config->hw_state)) == 0) {
  3341. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3342. crtc->base.base.id, pll->name,
  3343. pll->new_config->crtc_mask,
  3344. pll->active);
  3345. goto found;
  3346. }
  3347. }
  3348. /* Ok no matching timings, maybe there's a free one? */
  3349. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3350. pll = &dev_priv->shared_dplls[i];
  3351. if (pll->new_config->crtc_mask == 0) {
  3352. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3353. crtc->base.base.id, pll->name);
  3354. goto found;
  3355. }
  3356. }
  3357. return NULL;
  3358. found:
  3359. if (pll->new_config->crtc_mask == 0)
  3360. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3361. crtc_state->shared_dpll = i;
  3362. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3363. pipe_name(crtc->pipe));
  3364. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3365. return pll;
  3366. }
  3367. /**
  3368. * intel_shared_dpll_start_config - start a new PLL staged config
  3369. * @dev_priv: DRM device
  3370. * @clear_pipes: mask of pipes that will have their PLLs freed
  3371. *
  3372. * Starts a new PLL staged config, copying the current config but
  3373. * releasing the references of pipes specified in clear_pipes.
  3374. */
  3375. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3376. unsigned clear_pipes)
  3377. {
  3378. struct intel_shared_dpll *pll;
  3379. enum intel_dpll_id i;
  3380. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3381. pll = &dev_priv->shared_dplls[i];
  3382. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3383. GFP_KERNEL);
  3384. if (!pll->new_config)
  3385. goto cleanup;
  3386. pll->new_config->crtc_mask &= ~clear_pipes;
  3387. }
  3388. return 0;
  3389. cleanup:
  3390. while (--i >= 0) {
  3391. pll = &dev_priv->shared_dplls[i];
  3392. kfree(pll->new_config);
  3393. pll->new_config = NULL;
  3394. }
  3395. return -ENOMEM;
  3396. }
  3397. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3398. {
  3399. struct intel_shared_dpll *pll;
  3400. enum intel_dpll_id i;
  3401. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3402. pll = &dev_priv->shared_dplls[i];
  3403. WARN_ON(pll->new_config == &pll->config);
  3404. pll->config = *pll->new_config;
  3405. kfree(pll->new_config);
  3406. pll->new_config = NULL;
  3407. }
  3408. }
  3409. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3410. {
  3411. struct intel_shared_dpll *pll;
  3412. enum intel_dpll_id i;
  3413. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3414. pll = &dev_priv->shared_dplls[i];
  3415. WARN_ON(pll->new_config == &pll->config);
  3416. kfree(pll->new_config);
  3417. pll->new_config = NULL;
  3418. }
  3419. }
  3420. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3421. {
  3422. struct drm_i915_private *dev_priv = dev->dev_private;
  3423. int dslreg = PIPEDSL(pipe);
  3424. u32 temp;
  3425. temp = I915_READ(dslreg);
  3426. udelay(500);
  3427. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3428. if (wait_for(I915_READ(dslreg) != temp, 5))
  3429. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3430. }
  3431. }
  3432. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3433. {
  3434. struct drm_device *dev = crtc->base.dev;
  3435. struct drm_i915_private *dev_priv = dev->dev_private;
  3436. int pipe = crtc->pipe;
  3437. if (crtc->config->pch_pfit.enabled) {
  3438. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3439. I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3440. I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3441. }
  3442. }
  3443. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3444. {
  3445. struct drm_device *dev = crtc->base.dev;
  3446. struct drm_i915_private *dev_priv = dev->dev_private;
  3447. int pipe = crtc->pipe;
  3448. if (crtc->config->pch_pfit.enabled) {
  3449. /* Force use of hard-coded filter coefficients
  3450. * as some pre-programmed values are broken,
  3451. * e.g. x201.
  3452. */
  3453. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3454. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3455. PF_PIPE_SEL_IVB(pipe));
  3456. else
  3457. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3458. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3459. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3460. }
  3461. }
  3462. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3463. {
  3464. struct drm_device *dev = crtc->dev;
  3465. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3466. struct drm_plane *plane;
  3467. struct intel_plane *intel_plane;
  3468. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3469. intel_plane = to_intel_plane(plane);
  3470. if (intel_plane->pipe == pipe)
  3471. intel_plane_restore(&intel_plane->base);
  3472. }
  3473. }
  3474. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3475. {
  3476. struct drm_device *dev = crtc->dev;
  3477. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3478. struct drm_plane *plane;
  3479. struct intel_plane *intel_plane;
  3480. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3481. intel_plane = to_intel_plane(plane);
  3482. if (intel_plane->pipe == pipe)
  3483. plane->funcs->disable_plane(plane);
  3484. }
  3485. }
  3486. void hsw_enable_ips(struct intel_crtc *crtc)
  3487. {
  3488. struct drm_device *dev = crtc->base.dev;
  3489. struct drm_i915_private *dev_priv = dev->dev_private;
  3490. if (!crtc->config->ips_enabled)
  3491. return;
  3492. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3493. intel_wait_for_vblank(dev, crtc->pipe);
  3494. assert_plane_enabled(dev_priv, crtc->plane);
  3495. if (IS_BROADWELL(dev)) {
  3496. mutex_lock(&dev_priv->rps.hw_lock);
  3497. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3498. mutex_unlock(&dev_priv->rps.hw_lock);
  3499. /* Quoting Art Runyan: "its not safe to expect any particular
  3500. * value in IPS_CTL bit 31 after enabling IPS through the
  3501. * mailbox." Moreover, the mailbox may return a bogus state,
  3502. * so we need to just enable it and continue on.
  3503. */
  3504. } else {
  3505. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3506. /* The bit only becomes 1 in the next vblank, so this wait here
  3507. * is essentially intel_wait_for_vblank. If we don't have this
  3508. * and don't wait for vblanks until the end of crtc_enable, then
  3509. * the HW state readout code will complain that the expected
  3510. * IPS_CTL value is not the one we read. */
  3511. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3512. DRM_ERROR("Timed out waiting for IPS enable\n");
  3513. }
  3514. }
  3515. void hsw_disable_ips(struct intel_crtc *crtc)
  3516. {
  3517. struct drm_device *dev = crtc->base.dev;
  3518. struct drm_i915_private *dev_priv = dev->dev_private;
  3519. if (!crtc->config->ips_enabled)
  3520. return;
  3521. assert_plane_enabled(dev_priv, crtc->plane);
  3522. if (IS_BROADWELL(dev)) {
  3523. mutex_lock(&dev_priv->rps.hw_lock);
  3524. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3525. mutex_unlock(&dev_priv->rps.hw_lock);
  3526. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3527. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3528. DRM_ERROR("Timed out waiting for IPS disable\n");
  3529. } else {
  3530. I915_WRITE(IPS_CTL, 0);
  3531. POSTING_READ(IPS_CTL);
  3532. }
  3533. /* We need to wait for a vblank before we can disable the plane. */
  3534. intel_wait_for_vblank(dev, crtc->pipe);
  3535. }
  3536. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3537. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3538. {
  3539. struct drm_device *dev = crtc->dev;
  3540. struct drm_i915_private *dev_priv = dev->dev_private;
  3541. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3542. enum pipe pipe = intel_crtc->pipe;
  3543. int palreg = PALETTE(pipe);
  3544. int i;
  3545. bool reenable_ips = false;
  3546. /* The clocks have to be on to load the palette. */
  3547. if (!crtc->enabled || !intel_crtc->active)
  3548. return;
  3549. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3550. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3551. assert_dsi_pll_enabled(dev_priv);
  3552. else
  3553. assert_pll_enabled(dev_priv, pipe);
  3554. }
  3555. /* use legacy palette for Ironlake */
  3556. if (!HAS_GMCH_DISPLAY(dev))
  3557. palreg = LGC_PALETTE(pipe);
  3558. /* Workaround : Do not read or write the pipe palette/gamma data while
  3559. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3560. */
  3561. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3562. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3563. GAMMA_MODE_MODE_SPLIT)) {
  3564. hsw_disable_ips(intel_crtc);
  3565. reenable_ips = true;
  3566. }
  3567. for (i = 0; i < 256; i++) {
  3568. I915_WRITE(palreg + 4 * i,
  3569. (intel_crtc->lut_r[i] << 16) |
  3570. (intel_crtc->lut_g[i] << 8) |
  3571. intel_crtc->lut_b[i]);
  3572. }
  3573. if (reenable_ips)
  3574. hsw_enable_ips(intel_crtc);
  3575. }
  3576. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3577. {
  3578. if (!enable && intel_crtc->overlay) {
  3579. struct drm_device *dev = intel_crtc->base.dev;
  3580. struct drm_i915_private *dev_priv = dev->dev_private;
  3581. mutex_lock(&dev->struct_mutex);
  3582. dev_priv->mm.interruptible = false;
  3583. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3584. dev_priv->mm.interruptible = true;
  3585. mutex_unlock(&dev->struct_mutex);
  3586. }
  3587. /* Let userspace switch the overlay on again. In most cases userspace
  3588. * has to recompute where to put it anyway.
  3589. */
  3590. }
  3591. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3592. {
  3593. struct drm_device *dev = crtc->dev;
  3594. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3595. int pipe = intel_crtc->pipe;
  3596. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3597. intel_enable_sprite_planes(crtc);
  3598. intel_crtc_update_cursor(crtc, true);
  3599. intel_crtc_dpms_overlay(intel_crtc, true);
  3600. hsw_enable_ips(intel_crtc);
  3601. mutex_lock(&dev->struct_mutex);
  3602. intel_fbc_update(dev);
  3603. mutex_unlock(&dev->struct_mutex);
  3604. /*
  3605. * FIXME: Once we grow proper nuclear flip support out of this we need
  3606. * to compute the mask of flip planes precisely. For the time being
  3607. * consider this a flip from a NULL plane.
  3608. */
  3609. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3610. }
  3611. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3612. {
  3613. struct drm_device *dev = crtc->dev;
  3614. struct drm_i915_private *dev_priv = dev->dev_private;
  3615. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3616. int pipe = intel_crtc->pipe;
  3617. int plane = intel_crtc->plane;
  3618. intel_crtc_wait_for_pending_flips(crtc);
  3619. if (dev_priv->fbc.plane == plane)
  3620. intel_fbc_disable(dev);
  3621. hsw_disable_ips(intel_crtc);
  3622. intel_crtc_dpms_overlay(intel_crtc, false);
  3623. intel_crtc_update_cursor(crtc, false);
  3624. intel_disable_sprite_planes(crtc);
  3625. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3626. /*
  3627. * FIXME: Once we grow proper nuclear flip support out of this we need
  3628. * to compute the mask of flip planes precisely. For the time being
  3629. * consider this a flip to a NULL plane.
  3630. */
  3631. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3632. }
  3633. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3634. {
  3635. struct drm_device *dev = crtc->dev;
  3636. struct drm_i915_private *dev_priv = dev->dev_private;
  3637. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3638. struct intel_encoder *encoder;
  3639. int pipe = intel_crtc->pipe;
  3640. WARN_ON(!crtc->enabled);
  3641. if (intel_crtc->active)
  3642. return;
  3643. if (intel_crtc->config->has_pch_encoder)
  3644. intel_prepare_shared_dpll(intel_crtc);
  3645. if (intel_crtc->config->has_dp_encoder)
  3646. intel_dp_set_m_n(intel_crtc);
  3647. intel_set_pipe_timings(intel_crtc);
  3648. if (intel_crtc->config->has_pch_encoder) {
  3649. intel_cpu_transcoder_set_m_n(intel_crtc,
  3650. &intel_crtc->config->fdi_m_n, NULL);
  3651. }
  3652. ironlake_set_pipeconf(crtc);
  3653. intel_crtc->active = true;
  3654. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3655. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3656. for_each_encoder_on_crtc(dev, crtc, encoder)
  3657. if (encoder->pre_enable)
  3658. encoder->pre_enable(encoder);
  3659. if (intel_crtc->config->has_pch_encoder) {
  3660. /* Note: FDI PLL enabling _must_ be done before we enable the
  3661. * cpu pipes, hence this is separate from all the other fdi/pch
  3662. * enabling. */
  3663. ironlake_fdi_pll_enable(intel_crtc);
  3664. } else {
  3665. assert_fdi_tx_disabled(dev_priv, pipe);
  3666. assert_fdi_rx_disabled(dev_priv, pipe);
  3667. }
  3668. ironlake_pfit_enable(intel_crtc);
  3669. /*
  3670. * On ILK+ LUT must be loaded before the pipe is running but with
  3671. * clocks enabled
  3672. */
  3673. intel_crtc_load_lut(crtc);
  3674. intel_update_watermarks(crtc);
  3675. intel_enable_pipe(intel_crtc);
  3676. if (intel_crtc->config->has_pch_encoder)
  3677. ironlake_pch_enable(crtc);
  3678. assert_vblank_disabled(crtc);
  3679. drm_crtc_vblank_on(crtc);
  3680. for_each_encoder_on_crtc(dev, crtc, encoder)
  3681. encoder->enable(encoder);
  3682. if (HAS_PCH_CPT(dev))
  3683. cpt_verify_modeset(dev, intel_crtc->pipe);
  3684. intel_crtc_enable_planes(crtc);
  3685. }
  3686. /* IPS only exists on ULT machines and is tied to pipe A. */
  3687. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3688. {
  3689. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3690. }
  3691. /*
  3692. * This implements the workaround described in the "notes" section of the mode
  3693. * set sequence documentation. When going from no pipes or single pipe to
  3694. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3695. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3696. */
  3697. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3698. {
  3699. struct drm_device *dev = crtc->base.dev;
  3700. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3701. /* We want to get the other_active_crtc only if there's only 1 other
  3702. * active crtc. */
  3703. for_each_intel_crtc(dev, crtc_it) {
  3704. if (!crtc_it->active || crtc_it == crtc)
  3705. continue;
  3706. if (other_active_crtc)
  3707. return;
  3708. other_active_crtc = crtc_it;
  3709. }
  3710. if (!other_active_crtc)
  3711. return;
  3712. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3713. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3714. }
  3715. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3716. {
  3717. struct drm_device *dev = crtc->dev;
  3718. struct drm_i915_private *dev_priv = dev->dev_private;
  3719. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3720. struct intel_encoder *encoder;
  3721. int pipe = intel_crtc->pipe;
  3722. WARN_ON(!crtc->enabled);
  3723. if (intel_crtc->active)
  3724. return;
  3725. if (intel_crtc_to_shared_dpll(intel_crtc))
  3726. intel_enable_shared_dpll(intel_crtc);
  3727. if (intel_crtc->config->has_dp_encoder)
  3728. intel_dp_set_m_n(intel_crtc);
  3729. intel_set_pipe_timings(intel_crtc);
  3730. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  3731. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  3732. intel_crtc->config->pixel_multiplier - 1);
  3733. }
  3734. if (intel_crtc->config->has_pch_encoder) {
  3735. intel_cpu_transcoder_set_m_n(intel_crtc,
  3736. &intel_crtc->config->fdi_m_n, NULL);
  3737. }
  3738. haswell_set_pipeconf(crtc);
  3739. intel_set_pipe_csc(crtc);
  3740. intel_crtc->active = true;
  3741. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3742. for_each_encoder_on_crtc(dev, crtc, encoder)
  3743. if (encoder->pre_enable)
  3744. encoder->pre_enable(encoder);
  3745. if (intel_crtc->config->has_pch_encoder) {
  3746. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3747. true);
  3748. dev_priv->display.fdi_link_train(crtc);
  3749. }
  3750. intel_ddi_enable_pipe_clock(intel_crtc);
  3751. if (IS_SKYLAKE(dev))
  3752. skylake_pfit_enable(intel_crtc);
  3753. else
  3754. ironlake_pfit_enable(intel_crtc);
  3755. /*
  3756. * On ILK+ LUT must be loaded before the pipe is running but with
  3757. * clocks enabled
  3758. */
  3759. intel_crtc_load_lut(crtc);
  3760. intel_ddi_set_pipe_settings(crtc);
  3761. intel_ddi_enable_transcoder_func(crtc);
  3762. intel_update_watermarks(crtc);
  3763. intel_enable_pipe(intel_crtc);
  3764. if (intel_crtc->config->has_pch_encoder)
  3765. lpt_pch_enable(crtc);
  3766. if (intel_crtc->config->dp_encoder_is_mst)
  3767. intel_ddi_set_vc_payload_alloc(crtc, true);
  3768. assert_vblank_disabled(crtc);
  3769. drm_crtc_vblank_on(crtc);
  3770. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3771. encoder->enable(encoder);
  3772. intel_opregion_notify_encoder(encoder, true);
  3773. }
  3774. /* If we change the relative order between pipe/planes enabling, we need
  3775. * to change the workaround. */
  3776. haswell_mode_set_planes_workaround(intel_crtc);
  3777. intel_crtc_enable_planes(crtc);
  3778. }
  3779. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3780. {
  3781. struct drm_device *dev = crtc->base.dev;
  3782. struct drm_i915_private *dev_priv = dev->dev_private;
  3783. int pipe = crtc->pipe;
  3784. /* To avoid upsetting the power well on haswell only disable the pfit if
  3785. * it's in use. The hw state code will make sure we get this right. */
  3786. if (crtc->config->pch_pfit.enabled) {
  3787. I915_WRITE(PS_CTL(pipe), 0);
  3788. I915_WRITE(PS_WIN_POS(pipe), 0);
  3789. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3790. }
  3791. }
  3792. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3793. {
  3794. struct drm_device *dev = crtc->base.dev;
  3795. struct drm_i915_private *dev_priv = dev->dev_private;
  3796. int pipe = crtc->pipe;
  3797. /* To avoid upsetting the power well on haswell only disable the pfit if
  3798. * it's in use. The hw state code will make sure we get this right. */
  3799. if (crtc->config->pch_pfit.enabled) {
  3800. I915_WRITE(PF_CTL(pipe), 0);
  3801. I915_WRITE(PF_WIN_POS(pipe), 0);
  3802. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3803. }
  3804. }
  3805. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3806. {
  3807. struct drm_device *dev = crtc->dev;
  3808. struct drm_i915_private *dev_priv = dev->dev_private;
  3809. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3810. struct intel_encoder *encoder;
  3811. int pipe = intel_crtc->pipe;
  3812. u32 reg, temp;
  3813. if (!intel_crtc->active)
  3814. return;
  3815. intel_crtc_disable_planes(crtc);
  3816. for_each_encoder_on_crtc(dev, crtc, encoder)
  3817. encoder->disable(encoder);
  3818. drm_crtc_vblank_off(crtc);
  3819. assert_vblank_disabled(crtc);
  3820. if (intel_crtc->config->has_pch_encoder)
  3821. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3822. intel_disable_pipe(intel_crtc);
  3823. ironlake_pfit_disable(intel_crtc);
  3824. for_each_encoder_on_crtc(dev, crtc, encoder)
  3825. if (encoder->post_disable)
  3826. encoder->post_disable(encoder);
  3827. if (intel_crtc->config->has_pch_encoder) {
  3828. ironlake_fdi_disable(crtc);
  3829. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3830. if (HAS_PCH_CPT(dev)) {
  3831. /* disable TRANS_DP_CTL */
  3832. reg = TRANS_DP_CTL(pipe);
  3833. temp = I915_READ(reg);
  3834. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3835. TRANS_DP_PORT_SEL_MASK);
  3836. temp |= TRANS_DP_PORT_SEL_NONE;
  3837. I915_WRITE(reg, temp);
  3838. /* disable DPLL_SEL */
  3839. temp = I915_READ(PCH_DPLL_SEL);
  3840. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3841. I915_WRITE(PCH_DPLL_SEL, temp);
  3842. }
  3843. /* disable PCH DPLL */
  3844. intel_disable_shared_dpll(intel_crtc);
  3845. ironlake_fdi_pll_disable(intel_crtc);
  3846. }
  3847. intel_crtc->active = false;
  3848. intel_update_watermarks(crtc);
  3849. mutex_lock(&dev->struct_mutex);
  3850. intel_fbc_update(dev);
  3851. mutex_unlock(&dev->struct_mutex);
  3852. }
  3853. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3854. {
  3855. struct drm_device *dev = crtc->dev;
  3856. struct drm_i915_private *dev_priv = dev->dev_private;
  3857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3858. struct intel_encoder *encoder;
  3859. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3860. if (!intel_crtc->active)
  3861. return;
  3862. intel_crtc_disable_planes(crtc);
  3863. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3864. intel_opregion_notify_encoder(encoder, false);
  3865. encoder->disable(encoder);
  3866. }
  3867. drm_crtc_vblank_off(crtc);
  3868. assert_vblank_disabled(crtc);
  3869. if (intel_crtc->config->has_pch_encoder)
  3870. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3871. false);
  3872. intel_disable_pipe(intel_crtc);
  3873. if (intel_crtc->config->dp_encoder_is_mst)
  3874. intel_ddi_set_vc_payload_alloc(crtc, false);
  3875. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3876. if (IS_SKYLAKE(dev))
  3877. skylake_pfit_disable(intel_crtc);
  3878. else
  3879. ironlake_pfit_disable(intel_crtc);
  3880. intel_ddi_disable_pipe_clock(intel_crtc);
  3881. if (intel_crtc->config->has_pch_encoder) {
  3882. lpt_disable_pch_transcoder(dev_priv);
  3883. intel_ddi_fdi_disable(crtc);
  3884. }
  3885. for_each_encoder_on_crtc(dev, crtc, encoder)
  3886. if (encoder->post_disable)
  3887. encoder->post_disable(encoder);
  3888. intel_crtc->active = false;
  3889. intel_update_watermarks(crtc);
  3890. mutex_lock(&dev->struct_mutex);
  3891. intel_fbc_update(dev);
  3892. mutex_unlock(&dev->struct_mutex);
  3893. if (intel_crtc_to_shared_dpll(intel_crtc))
  3894. intel_disable_shared_dpll(intel_crtc);
  3895. }
  3896. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3897. {
  3898. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3899. intel_put_shared_dpll(intel_crtc);
  3900. }
  3901. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3902. {
  3903. struct drm_device *dev = crtc->base.dev;
  3904. struct drm_i915_private *dev_priv = dev->dev_private;
  3905. struct intel_crtc_state *pipe_config = crtc->config;
  3906. if (!pipe_config->gmch_pfit.control)
  3907. return;
  3908. /*
  3909. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3910. * according to register description and PRM.
  3911. */
  3912. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3913. assert_pipe_disabled(dev_priv, crtc->pipe);
  3914. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3915. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3916. /* Border color in case we don't scale up to the full screen. Black by
  3917. * default, change to something else for debugging. */
  3918. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3919. }
  3920. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3921. {
  3922. switch (port) {
  3923. case PORT_A:
  3924. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3925. case PORT_B:
  3926. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3927. case PORT_C:
  3928. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3929. case PORT_D:
  3930. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3931. default:
  3932. WARN_ON_ONCE(1);
  3933. return POWER_DOMAIN_PORT_OTHER;
  3934. }
  3935. }
  3936. #define for_each_power_domain(domain, mask) \
  3937. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3938. if ((1 << (domain)) & (mask))
  3939. enum intel_display_power_domain
  3940. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3941. {
  3942. struct drm_device *dev = intel_encoder->base.dev;
  3943. struct intel_digital_port *intel_dig_port;
  3944. switch (intel_encoder->type) {
  3945. case INTEL_OUTPUT_UNKNOWN:
  3946. /* Only DDI platforms should ever use this output type */
  3947. WARN_ON_ONCE(!HAS_DDI(dev));
  3948. case INTEL_OUTPUT_DISPLAYPORT:
  3949. case INTEL_OUTPUT_HDMI:
  3950. case INTEL_OUTPUT_EDP:
  3951. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3952. return port_to_power_domain(intel_dig_port->port);
  3953. case INTEL_OUTPUT_DP_MST:
  3954. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3955. return port_to_power_domain(intel_dig_port->port);
  3956. case INTEL_OUTPUT_ANALOG:
  3957. return POWER_DOMAIN_PORT_CRT;
  3958. case INTEL_OUTPUT_DSI:
  3959. return POWER_DOMAIN_PORT_DSI;
  3960. default:
  3961. return POWER_DOMAIN_PORT_OTHER;
  3962. }
  3963. }
  3964. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3965. {
  3966. struct drm_device *dev = crtc->dev;
  3967. struct intel_encoder *intel_encoder;
  3968. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3969. enum pipe pipe = intel_crtc->pipe;
  3970. unsigned long mask;
  3971. enum transcoder transcoder;
  3972. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3973. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3974. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3975. if (intel_crtc->config->pch_pfit.enabled ||
  3976. intel_crtc->config->pch_pfit.force_thru)
  3977. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3978. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3979. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3980. return mask;
  3981. }
  3982. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3983. {
  3984. struct drm_i915_private *dev_priv = dev->dev_private;
  3985. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3986. struct intel_crtc *crtc;
  3987. /*
  3988. * First get all needed power domains, then put all unneeded, to avoid
  3989. * any unnecessary toggling of the power wells.
  3990. */
  3991. for_each_intel_crtc(dev, crtc) {
  3992. enum intel_display_power_domain domain;
  3993. if (!crtc->base.enabled)
  3994. continue;
  3995. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3996. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3997. intel_display_power_get(dev_priv, domain);
  3998. }
  3999. if (dev_priv->display.modeset_global_resources)
  4000. dev_priv->display.modeset_global_resources(dev);
  4001. for_each_intel_crtc(dev, crtc) {
  4002. enum intel_display_power_domain domain;
  4003. for_each_power_domain(domain, crtc->enabled_power_domains)
  4004. intel_display_power_put(dev_priv, domain);
  4005. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4006. }
  4007. intel_display_set_init_power(dev_priv, false);
  4008. }
  4009. /* returns HPLL frequency in kHz */
  4010. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4011. {
  4012. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4013. /* Obtain SKU information */
  4014. mutex_lock(&dev_priv->dpio_lock);
  4015. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4016. CCK_FUSE_HPLL_FREQ_MASK;
  4017. mutex_unlock(&dev_priv->dpio_lock);
  4018. return vco_freq[hpll_freq] * 1000;
  4019. }
  4020. static void vlv_update_cdclk(struct drm_device *dev)
  4021. {
  4022. struct drm_i915_private *dev_priv = dev->dev_private;
  4023. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4024. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4025. dev_priv->vlv_cdclk_freq);
  4026. /*
  4027. * Program the gmbus_freq based on the cdclk frequency.
  4028. * BSpec erroneously claims we should aim for 4MHz, but
  4029. * in fact 1MHz is the correct frequency.
  4030. */
  4031. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4032. }
  4033. /* Adjust CDclk dividers to allow high res or save power if possible */
  4034. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4035. {
  4036. struct drm_i915_private *dev_priv = dev->dev_private;
  4037. u32 val, cmd;
  4038. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4039. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4040. cmd = 2;
  4041. else if (cdclk == 266667)
  4042. cmd = 1;
  4043. else
  4044. cmd = 0;
  4045. mutex_lock(&dev_priv->rps.hw_lock);
  4046. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4047. val &= ~DSPFREQGUAR_MASK;
  4048. val |= (cmd << DSPFREQGUAR_SHIFT);
  4049. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4050. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4051. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4052. 50)) {
  4053. DRM_ERROR("timed out waiting for CDclk change\n");
  4054. }
  4055. mutex_unlock(&dev_priv->rps.hw_lock);
  4056. if (cdclk == 400000) {
  4057. u32 divider;
  4058. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4059. mutex_lock(&dev_priv->dpio_lock);
  4060. /* adjust cdclk divider */
  4061. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4062. val &= ~DISPLAY_FREQUENCY_VALUES;
  4063. val |= divider;
  4064. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4065. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4066. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4067. 50))
  4068. DRM_ERROR("timed out waiting for CDclk change\n");
  4069. mutex_unlock(&dev_priv->dpio_lock);
  4070. }
  4071. mutex_lock(&dev_priv->dpio_lock);
  4072. /* adjust self-refresh exit latency value */
  4073. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4074. val &= ~0x7f;
  4075. /*
  4076. * For high bandwidth configs, we set a higher latency in the bunit
  4077. * so that the core display fetch happens in time to avoid underruns.
  4078. */
  4079. if (cdclk == 400000)
  4080. val |= 4500 / 250; /* 4.5 usec */
  4081. else
  4082. val |= 3000 / 250; /* 3.0 usec */
  4083. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4084. mutex_unlock(&dev_priv->dpio_lock);
  4085. vlv_update_cdclk(dev);
  4086. }
  4087. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4088. {
  4089. struct drm_i915_private *dev_priv = dev->dev_private;
  4090. u32 val, cmd;
  4091. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4092. switch (cdclk) {
  4093. case 400000:
  4094. cmd = 3;
  4095. break;
  4096. case 333333:
  4097. case 320000:
  4098. cmd = 2;
  4099. break;
  4100. case 266667:
  4101. cmd = 1;
  4102. break;
  4103. case 200000:
  4104. cmd = 0;
  4105. break;
  4106. default:
  4107. MISSING_CASE(cdclk);
  4108. return;
  4109. }
  4110. mutex_lock(&dev_priv->rps.hw_lock);
  4111. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4112. val &= ~DSPFREQGUAR_MASK_CHV;
  4113. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4114. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4115. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4116. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4117. 50)) {
  4118. DRM_ERROR("timed out waiting for CDclk change\n");
  4119. }
  4120. mutex_unlock(&dev_priv->rps.hw_lock);
  4121. vlv_update_cdclk(dev);
  4122. }
  4123. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4124. int max_pixclk)
  4125. {
  4126. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4127. /* FIXME: Punit isn't quite ready yet */
  4128. if (IS_CHERRYVIEW(dev_priv->dev))
  4129. return 400000;
  4130. /*
  4131. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4132. * 200MHz
  4133. * 267MHz
  4134. * 320/333MHz (depends on HPLL freq)
  4135. * 400MHz
  4136. * So we check to see whether we're above 90% of the lower bin and
  4137. * adjust if needed.
  4138. *
  4139. * We seem to get an unstable or solid color picture at 200MHz.
  4140. * Not sure what's wrong. For now use 200MHz only when all pipes
  4141. * are off.
  4142. */
  4143. if (max_pixclk > freq_320*9/10)
  4144. return 400000;
  4145. else if (max_pixclk > 266667*9/10)
  4146. return freq_320;
  4147. else if (max_pixclk > 0)
  4148. return 266667;
  4149. else
  4150. return 200000;
  4151. }
  4152. /* compute the max pixel clock for new configuration */
  4153. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4154. {
  4155. struct drm_device *dev = dev_priv->dev;
  4156. struct intel_crtc *intel_crtc;
  4157. int max_pixclk = 0;
  4158. for_each_intel_crtc(dev, intel_crtc) {
  4159. if (intel_crtc->new_enabled)
  4160. max_pixclk = max(max_pixclk,
  4161. intel_crtc->new_config->base.adjusted_mode.crtc_clock);
  4162. }
  4163. return max_pixclk;
  4164. }
  4165. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4166. unsigned *prepare_pipes)
  4167. {
  4168. struct drm_i915_private *dev_priv = dev->dev_private;
  4169. struct intel_crtc *intel_crtc;
  4170. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4171. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4172. dev_priv->vlv_cdclk_freq)
  4173. return;
  4174. /* disable/enable all currently active pipes while we change cdclk */
  4175. for_each_intel_crtc(dev, intel_crtc)
  4176. if (intel_crtc->base.enabled)
  4177. *prepare_pipes |= (1 << intel_crtc->pipe);
  4178. }
  4179. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4180. {
  4181. struct drm_i915_private *dev_priv = dev->dev_private;
  4182. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4183. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4184. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4185. /*
  4186. * FIXME: We can end up here with all power domains off, yet
  4187. * with a CDCLK frequency other than the minimum. To account
  4188. * for this take the PIPE-A power domain, which covers the HW
  4189. * blocks needed for the following programming. This can be
  4190. * removed once it's guaranteed that we get here either with
  4191. * the minimum CDCLK set, or the required power domains
  4192. * enabled.
  4193. */
  4194. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4195. if (IS_CHERRYVIEW(dev))
  4196. cherryview_set_cdclk(dev, req_cdclk);
  4197. else
  4198. valleyview_set_cdclk(dev, req_cdclk);
  4199. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4200. }
  4201. }
  4202. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4203. {
  4204. struct drm_device *dev = crtc->dev;
  4205. struct drm_i915_private *dev_priv = to_i915(dev);
  4206. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4207. struct intel_encoder *encoder;
  4208. int pipe = intel_crtc->pipe;
  4209. bool is_dsi;
  4210. WARN_ON(!crtc->enabled);
  4211. if (intel_crtc->active)
  4212. return;
  4213. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4214. if (!is_dsi) {
  4215. if (IS_CHERRYVIEW(dev))
  4216. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4217. else
  4218. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4219. }
  4220. if (intel_crtc->config->has_dp_encoder)
  4221. intel_dp_set_m_n(intel_crtc);
  4222. intel_set_pipe_timings(intel_crtc);
  4223. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4224. struct drm_i915_private *dev_priv = dev->dev_private;
  4225. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4226. I915_WRITE(CHV_CANVAS(pipe), 0);
  4227. }
  4228. i9xx_set_pipeconf(intel_crtc);
  4229. intel_crtc->active = true;
  4230. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4231. for_each_encoder_on_crtc(dev, crtc, encoder)
  4232. if (encoder->pre_pll_enable)
  4233. encoder->pre_pll_enable(encoder);
  4234. if (!is_dsi) {
  4235. if (IS_CHERRYVIEW(dev))
  4236. chv_enable_pll(intel_crtc, intel_crtc->config);
  4237. else
  4238. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4239. }
  4240. for_each_encoder_on_crtc(dev, crtc, encoder)
  4241. if (encoder->pre_enable)
  4242. encoder->pre_enable(encoder);
  4243. i9xx_pfit_enable(intel_crtc);
  4244. intel_crtc_load_lut(crtc);
  4245. intel_update_watermarks(crtc);
  4246. intel_enable_pipe(intel_crtc);
  4247. assert_vblank_disabled(crtc);
  4248. drm_crtc_vblank_on(crtc);
  4249. for_each_encoder_on_crtc(dev, crtc, encoder)
  4250. encoder->enable(encoder);
  4251. intel_crtc_enable_planes(crtc);
  4252. /* Underruns don't raise interrupts, so check manually. */
  4253. i9xx_check_fifo_underruns(dev_priv);
  4254. }
  4255. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4256. {
  4257. struct drm_device *dev = crtc->base.dev;
  4258. struct drm_i915_private *dev_priv = dev->dev_private;
  4259. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4260. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4261. }
  4262. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4263. {
  4264. struct drm_device *dev = crtc->dev;
  4265. struct drm_i915_private *dev_priv = to_i915(dev);
  4266. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4267. struct intel_encoder *encoder;
  4268. int pipe = intel_crtc->pipe;
  4269. WARN_ON(!crtc->enabled);
  4270. if (intel_crtc->active)
  4271. return;
  4272. i9xx_set_pll_dividers(intel_crtc);
  4273. if (intel_crtc->config->has_dp_encoder)
  4274. intel_dp_set_m_n(intel_crtc);
  4275. intel_set_pipe_timings(intel_crtc);
  4276. i9xx_set_pipeconf(intel_crtc);
  4277. intel_crtc->active = true;
  4278. if (!IS_GEN2(dev))
  4279. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4280. for_each_encoder_on_crtc(dev, crtc, encoder)
  4281. if (encoder->pre_enable)
  4282. encoder->pre_enable(encoder);
  4283. i9xx_enable_pll(intel_crtc);
  4284. i9xx_pfit_enable(intel_crtc);
  4285. intel_crtc_load_lut(crtc);
  4286. intel_update_watermarks(crtc);
  4287. intel_enable_pipe(intel_crtc);
  4288. assert_vblank_disabled(crtc);
  4289. drm_crtc_vblank_on(crtc);
  4290. for_each_encoder_on_crtc(dev, crtc, encoder)
  4291. encoder->enable(encoder);
  4292. intel_crtc_enable_planes(crtc);
  4293. /*
  4294. * Gen2 reports pipe underruns whenever all planes are disabled.
  4295. * So don't enable underrun reporting before at least some planes
  4296. * are enabled.
  4297. * FIXME: Need to fix the logic to work when we turn off all planes
  4298. * but leave the pipe running.
  4299. */
  4300. if (IS_GEN2(dev))
  4301. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4302. /* Underruns don't raise interrupts, so check manually. */
  4303. i9xx_check_fifo_underruns(dev_priv);
  4304. }
  4305. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4306. {
  4307. struct drm_device *dev = crtc->base.dev;
  4308. struct drm_i915_private *dev_priv = dev->dev_private;
  4309. if (!crtc->config->gmch_pfit.control)
  4310. return;
  4311. assert_pipe_disabled(dev_priv, crtc->pipe);
  4312. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4313. I915_READ(PFIT_CONTROL));
  4314. I915_WRITE(PFIT_CONTROL, 0);
  4315. }
  4316. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4317. {
  4318. struct drm_device *dev = crtc->dev;
  4319. struct drm_i915_private *dev_priv = dev->dev_private;
  4320. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4321. struct intel_encoder *encoder;
  4322. int pipe = intel_crtc->pipe;
  4323. if (!intel_crtc->active)
  4324. return;
  4325. /*
  4326. * Gen2 reports pipe underruns whenever all planes are disabled.
  4327. * So diasble underrun reporting before all the planes get disabled.
  4328. * FIXME: Need to fix the logic to work when we turn off all planes
  4329. * but leave the pipe running.
  4330. */
  4331. if (IS_GEN2(dev))
  4332. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4333. /*
  4334. * Vblank time updates from the shadow to live plane control register
  4335. * are blocked if the memory self-refresh mode is active at that
  4336. * moment. So to make sure the plane gets truly disabled, disable
  4337. * first the self-refresh mode. The self-refresh enable bit in turn
  4338. * will be checked/applied by the HW only at the next frame start
  4339. * event which is after the vblank start event, so we need to have a
  4340. * wait-for-vblank between disabling the plane and the pipe.
  4341. */
  4342. intel_set_memory_cxsr(dev_priv, false);
  4343. intel_crtc_disable_planes(crtc);
  4344. /*
  4345. * On gen2 planes are double buffered but the pipe isn't, so we must
  4346. * wait for planes to fully turn off before disabling the pipe.
  4347. * We also need to wait on all gmch platforms because of the
  4348. * self-refresh mode constraint explained above.
  4349. */
  4350. intel_wait_for_vblank(dev, pipe);
  4351. for_each_encoder_on_crtc(dev, crtc, encoder)
  4352. encoder->disable(encoder);
  4353. drm_crtc_vblank_off(crtc);
  4354. assert_vblank_disabled(crtc);
  4355. intel_disable_pipe(intel_crtc);
  4356. i9xx_pfit_disable(intel_crtc);
  4357. for_each_encoder_on_crtc(dev, crtc, encoder)
  4358. if (encoder->post_disable)
  4359. encoder->post_disable(encoder);
  4360. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4361. if (IS_CHERRYVIEW(dev))
  4362. chv_disable_pll(dev_priv, pipe);
  4363. else if (IS_VALLEYVIEW(dev))
  4364. vlv_disable_pll(dev_priv, pipe);
  4365. else
  4366. i9xx_disable_pll(intel_crtc);
  4367. }
  4368. if (!IS_GEN2(dev))
  4369. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4370. intel_crtc->active = false;
  4371. intel_update_watermarks(crtc);
  4372. mutex_lock(&dev->struct_mutex);
  4373. intel_fbc_update(dev);
  4374. mutex_unlock(&dev->struct_mutex);
  4375. }
  4376. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4377. {
  4378. }
  4379. /* Master function to enable/disable CRTC and corresponding power wells */
  4380. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4381. {
  4382. struct drm_device *dev = crtc->dev;
  4383. struct drm_i915_private *dev_priv = dev->dev_private;
  4384. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4385. enum intel_display_power_domain domain;
  4386. unsigned long domains;
  4387. if (enable) {
  4388. if (!intel_crtc->active) {
  4389. domains = get_crtc_power_domains(crtc);
  4390. for_each_power_domain(domain, domains)
  4391. intel_display_power_get(dev_priv, domain);
  4392. intel_crtc->enabled_power_domains = domains;
  4393. dev_priv->display.crtc_enable(crtc);
  4394. }
  4395. } else {
  4396. if (intel_crtc->active) {
  4397. dev_priv->display.crtc_disable(crtc);
  4398. domains = intel_crtc->enabled_power_domains;
  4399. for_each_power_domain(domain, domains)
  4400. intel_display_power_put(dev_priv, domain);
  4401. intel_crtc->enabled_power_domains = 0;
  4402. }
  4403. }
  4404. }
  4405. /**
  4406. * Sets the power management mode of the pipe and plane.
  4407. */
  4408. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4409. {
  4410. struct drm_device *dev = crtc->dev;
  4411. struct intel_encoder *intel_encoder;
  4412. bool enable = false;
  4413. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4414. enable |= intel_encoder->connectors_active;
  4415. intel_crtc_control(crtc, enable);
  4416. }
  4417. static void intel_crtc_disable(struct drm_crtc *crtc)
  4418. {
  4419. struct drm_device *dev = crtc->dev;
  4420. struct drm_connector *connector;
  4421. struct drm_i915_private *dev_priv = dev->dev_private;
  4422. /* crtc should still be enabled when we disable it. */
  4423. WARN_ON(!crtc->enabled);
  4424. dev_priv->display.crtc_disable(crtc);
  4425. dev_priv->display.off(crtc);
  4426. crtc->primary->funcs->disable_plane(crtc->primary);
  4427. /* Update computed state. */
  4428. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4429. if (!connector->encoder || !connector->encoder->crtc)
  4430. continue;
  4431. if (connector->encoder->crtc != crtc)
  4432. continue;
  4433. connector->dpms = DRM_MODE_DPMS_OFF;
  4434. to_intel_encoder(connector->encoder)->connectors_active = false;
  4435. }
  4436. }
  4437. void intel_encoder_destroy(struct drm_encoder *encoder)
  4438. {
  4439. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4440. drm_encoder_cleanup(encoder);
  4441. kfree(intel_encoder);
  4442. }
  4443. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4444. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4445. * state of the entire output pipe. */
  4446. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4447. {
  4448. if (mode == DRM_MODE_DPMS_ON) {
  4449. encoder->connectors_active = true;
  4450. intel_crtc_update_dpms(encoder->base.crtc);
  4451. } else {
  4452. encoder->connectors_active = false;
  4453. intel_crtc_update_dpms(encoder->base.crtc);
  4454. }
  4455. }
  4456. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4457. * internal consistency). */
  4458. static void intel_connector_check_state(struct intel_connector *connector)
  4459. {
  4460. if (connector->get_hw_state(connector)) {
  4461. struct intel_encoder *encoder = connector->encoder;
  4462. struct drm_crtc *crtc;
  4463. bool encoder_enabled;
  4464. enum pipe pipe;
  4465. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4466. connector->base.base.id,
  4467. connector->base.name);
  4468. /* there is no real hw state for MST connectors */
  4469. if (connector->mst_port)
  4470. return;
  4471. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4472. "wrong connector dpms state\n");
  4473. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4474. "active connector not linked to encoder\n");
  4475. if (encoder) {
  4476. I915_STATE_WARN(!encoder->connectors_active,
  4477. "encoder->connectors_active not set\n");
  4478. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4479. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4480. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4481. return;
  4482. crtc = encoder->base.crtc;
  4483. I915_STATE_WARN(!crtc->enabled, "crtc not enabled\n");
  4484. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4485. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4486. "encoder active on the wrong pipe\n");
  4487. }
  4488. }
  4489. }
  4490. /* Even simpler default implementation, if there's really no special case to
  4491. * consider. */
  4492. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4493. {
  4494. /* All the simple cases only support two dpms states. */
  4495. if (mode != DRM_MODE_DPMS_ON)
  4496. mode = DRM_MODE_DPMS_OFF;
  4497. if (mode == connector->dpms)
  4498. return;
  4499. connector->dpms = mode;
  4500. /* Only need to change hw state when actually enabled */
  4501. if (connector->encoder)
  4502. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4503. intel_modeset_check_state(connector->dev);
  4504. }
  4505. /* Simple connector->get_hw_state implementation for encoders that support only
  4506. * one connector and no cloning and hence the encoder state determines the state
  4507. * of the connector. */
  4508. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4509. {
  4510. enum pipe pipe = 0;
  4511. struct intel_encoder *encoder = connector->encoder;
  4512. return encoder->get_hw_state(encoder, &pipe);
  4513. }
  4514. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4515. struct intel_crtc_state *pipe_config)
  4516. {
  4517. struct drm_i915_private *dev_priv = dev->dev_private;
  4518. struct intel_crtc *pipe_B_crtc =
  4519. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4520. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4521. pipe_name(pipe), pipe_config->fdi_lanes);
  4522. if (pipe_config->fdi_lanes > 4) {
  4523. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4524. pipe_name(pipe), pipe_config->fdi_lanes);
  4525. return false;
  4526. }
  4527. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4528. if (pipe_config->fdi_lanes > 2) {
  4529. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4530. pipe_config->fdi_lanes);
  4531. return false;
  4532. } else {
  4533. return true;
  4534. }
  4535. }
  4536. if (INTEL_INFO(dev)->num_pipes == 2)
  4537. return true;
  4538. /* Ivybridge 3 pipe is really complicated */
  4539. switch (pipe) {
  4540. case PIPE_A:
  4541. return true;
  4542. case PIPE_B:
  4543. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4544. pipe_config->fdi_lanes > 2) {
  4545. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4546. pipe_name(pipe), pipe_config->fdi_lanes);
  4547. return false;
  4548. }
  4549. return true;
  4550. case PIPE_C:
  4551. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4552. pipe_B_crtc->config->fdi_lanes <= 2) {
  4553. if (pipe_config->fdi_lanes > 2) {
  4554. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4555. pipe_name(pipe), pipe_config->fdi_lanes);
  4556. return false;
  4557. }
  4558. } else {
  4559. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4560. return false;
  4561. }
  4562. return true;
  4563. default:
  4564. BUG();
  4565. }
  4566. }
  4567. #define RETRY 1
  4568. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4569. struct intel_crtc_state *pipe_config)
  4570. {
  4571. struct drm_device *dev = intel_crtc->base.dev;
  4572. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4573. int lane, link_bw, fdi_dotclock;
  4574. bool setup_ok, needs_recompute = false;
  4575. retry:
  4576. /* FDI is a binary signal running at ~2.7GHz, encoding
  4577. * each output octet as 10 bits. The actual frequency
  4578. * is stored as a divider into a 100MHz clock, and the
  4579. * mode pixel clock is stored in units of 1KHz.
  4580. * Hence the bw of each lane in terms of the mode signal
  4581. * is:
  4582. */
  4583. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4584. fdi_dotclock = adjusted_mode->crtc_clock;
  4585. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4586. pipe_config->pipe_bpp);
  4587. pipe_config->fdi_lanes = lane;
  4588. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4589. link_bw, &pipe_config->fdi_m_n);
  4590. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4591. intel_crtc->pipe, pipe_config);
  4592. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4593. pipe_config->pipe_bpp -= 2*3;
  4594. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4595. pipe_config->pipe_bpp);
  4596. needs_recompute = true;
  4597. pipe_config->bw_constrained = true;
  4598. goto retry;
  4599. }
  4600. if (needs_recompute)
  4601. return RETRY;
  4602. return setup_ok ? 0 : -EINVAL;
  4603. }
  4604. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4605. struct intel_crtc_state *pipe_config)
  4606. {
  4607. pipe_config->ips_enabled = i915.enable_ips &&
  4608. hsw_crtc_supports_ips(crtc) &&
  4609. pipe_config->pipe_bpp <= 24;
  4610. }
  4611. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4612. struct intel_crtc_state *pipe_config)
  4613. {
  4614. struct drm_device *dev = crtc->base.dev;
  4615. struct drm_i915_private *dev_priv = dev->dev_private;
  4616. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4617. /* FIXME should check pixel clock limits on all platforms */
  4618. if (INTEL_INFO(dev)->gen < 4) {
  4619. int clock_limit =
  4620. dev_priv->display.get_display_clock_speed(dev);
  4621. /*
  4622. * Enable pixel doubling when the dot clock
  4623. * is > 90% of the (display) core speed.
  4624. *
  4625. * GDG double wide on either pipe,
  4626. * otherwise pipe A only.
  4627. */
  4628. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4629. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4630. clock_limit *= 2;
  4631. pipe_config->double_wide = true;
  4632. }
  4633. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4634. return -EINVAL;
  4635. }
  4636. /*
  4637. * Pipe horizontal size must be even in:
  4638. * - DVO ganged mode
  4639. * - LVDS dual channel mode
  4640. * - Double wide pipe
  4641. */
  4642. if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4643. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4644. pipe_config->pipe_src_w &= ~1;
  4645. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4646. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4647. */
  4648. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4649. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4650. return -EINVAL;
  4651. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4652. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4653. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4654. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4655. * for lvds. */
  4656. pipe_config->pipe_bpp = 8*3;
  4657. }
  4658. if (HAS_IPS(dev))
  4659. hsw_compute_ips_config(crtc, pipe_config);
  4660. if (pipe_config->has_pch_encoder)
  4661. return ironlake_fdi_compute_config(crtc, pipe_config);
  4662. return 0;
  4663. }
  4664. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4665. {
  4666. struct drm_i915_private *dev_priv = dev->dev_private;
  4667. u32 val;
  4668. int divider;
  4669. /* FIXME: Punit isn't quite ready yet */
  4670. if (IS_CHERRYVIEW(dev))
  4671. return 400000;
  4672. if (dev_priv->hpll_freq == 0)
  4673. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4674. mutex_lock(&dev_priv->dpio_lock);
  4675. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4676. mutex_unlock(&dev_priv->dpio_lock);
  4677. divider = val & DISPLAY_FREQUENCY_VALUES;
  4678. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4679. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4680. "cdclk change in progress\n");
  4681. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4682. }
  4683. static int i945_get_display_clock_speed(struct drm_device *dev)
  4684. {
  4685. return 400000;
  4686. }
  4687. static int i915_get_display_clock_speed(struct drm_device *dev)
  4688. {
  4689. return 333000;
  4690. }
  4691. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4692. {
  4693. return 200000;
  4694. }
  4695. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4696. {
  4697. u16 gcfgc = 0;
  4698. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4699. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4700. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4701. return 267000;
  4702. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4703. return 333000;
  4704. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4705. return 444000;
  4706. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4707. return 200000;
  4708. default:
  4709. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4710. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4711. return 133000;
  4712. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4713. return 167000;
  4714. }
  4715. }
  4716. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4717. {
  4718. u16 gcfgc = 0;
  4719. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4720. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4721. return 133000;
  4722. else {
  4723. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4724. case GC_DISPLAY_CLOCK_333_MHZ:
  4725. return 333000;
  4726. default:
  4727. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4728. return 190000;
  4729. }
  4730. }
  4731. }
  4732. static int i865_get_display_clock_speed(struct drm_device *dev)
  4733. {
  4734. return 266000;
  4735. }
  4736. static int i855_get_display_clock_speed(struct drm_device *dev)
  4737. {
  4738. u16 hpllcc = 0;
  4739. /* Assume that the hardware is in the high speed state. This
  4740. * should be the default.
  4741. */
  4742. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4743. case GC_CLOCK_133_200:
  4744. case GC_CLOCK_100_200:
  4745. return 200000;
  4746. case GC_CLOCK_166_250:
  4747. return 250000;
  4748. case GC_CLOCK_100_133:
  4749. return 133000;
  4750. }
  4751. /* Shouldn't happen */
  4752. return 0;
  4753. }
  4754. static int i830_get_display_clock_speed(struct drm_device *dev)
  4755. {
  4756. return 133000;
  4757. }
  4758. static void
  4759. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4760. {
  4761. while (*num > DATA_LINK_M_N_MASK ||
  4762. *den > DATA_LINK_M_N_MASK) {
  4763. *num >>= 1;
  4764. *den >>= 1;
  4765. }
  4766. }
  4767. static void compute_m_n(unsigned int m, unsigned int n,
  4768. uint32_t *ret_m, uint32_t *ret_n)
  4769. {
  4770. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4771. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4772. intel_reduce_m_n_ratio(ret_m, ret_n);
  4773. }
  4774. void
  4775. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4776. int pixel_clock, int link_clock,
  4777. struct intel_link_m_n *m_n)
  4778. {
  4779. m_n->tu = 64;
  4780. compute_m_n(bits_per_pixel * pixel_clock,
  4781. link_clock * nlanes * 8,
  4782. &m_n->gmch_m, &m_n->gmch_n);
  4783. compute_m_n(pixel_clock, link_clock,
  4784. &m_n->link_m, &m_n->link_n);
  4785. }
  4786. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4787. {
  4788. if (i915.panel_use_ssc >= 0)
  4789. return i915.panel_use_ssc != 0;
  4790. return dev_priv->vbt.lvds_use_ssc
  4791. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4792. }
  4793. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4794. {
  4795. struct drm_device *dev = crtc->base.dev;
  4796. struct drm_i915_private *dev_priv = dev->dev_private;
  4797. int refclk;
  4798. if (IS_VALLEYVIEW(dev)) {
  4799. refclk = 100000;
  4800. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4801. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4802. refclk = dev_priv->vbt.lvds_ssc_freq;
  4803. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4804. } else if (!IS_GEN2(dev)) {
  4805. refclk = 96000;
  4806. } else {
  4807. refclk = 48000;
  4808. }
  4809. return refclk;
  4810. }
  4811. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4812. {
  4813. return (1 << dpll->n) << 16 | dpll->m2;
  4814. }
  4815. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4816. {
  4817. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4818. }
  4819. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4820. struct intel_crtc_state *crtc_state,
  4821. intel_clock_t *reduced_clock)
  4822. {
  4823. struct drm_device *dev = crtc->base.dev;
  4824. u32 fp, fp2 = 0;
  4825. if (IS_PINEVIEW(dev)) {
  4826. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  4827. if (reduced_clock)
  4828. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4829. } else {
  4830. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  4831. if (reduced_clock)
  4832. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4833. }
  4834. crtc_state->dpll_hw_state.fp0 = fp;
  4835. crtc->lowfreq_avail = false;
  4836. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4837. reduced_clock && i915.powersave) {
  4838. crtc_state->dpll_hw_state.fp1 = fp2;
  4839. crtc->lowfreq_avail = true;
  4840. } else {
  4841. crtc_state->dpll_hw_state.fp1 = fp;
  4842. }
  4843. }
  4844. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4845. pipe)
  4846. {
  4847. u32 reg_val;
  4848. /*
  4849. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4850. * and set it to a reasonable value instead.
  4851. */
  4852. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4853. reg_val &= 0xffffff00;
  4854. reg_val |= 0x00000030;
  4855. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4856. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4857. reg_val &= 0x8cffffff;
  4858. reg_val = 0x8c000000;
  4859. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4860. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4861. reg_val &= 0xffffff00;
  4862. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4863. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4864. reg_val &= 0x00ffffff;
  4865. reg_val |= 0xb0000000;
  4866. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4867. }
  4868. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4869. struct intel_link_m_n *m_n)
  4870. {
  4871. struct drm_device *dev = crtc->base.dev;
  4872. struct drm_i915_private *dev_priv = dev->dev_private;
  4873. int pipe = crtc->pipe;
  4874. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4875. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4876. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4877. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4878. }
  4879. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4880. struct intel_link_m_n *m_n,
  4881. struct intel_link_m_n *m2_n2)
  4882. {
  4883. struct drm_device *dev = crtc->base.dev;
  4884. struct drm_i915_private *dev_priv = dev->dev_private;
  4885. int pipe = crtc->pipe;
  4886. enum transcoder transcoder = crtc->config->cpu_transcoder;
  4887. if (INTEL_INFO(dev)->gen >= 5) {
  4888. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4889. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4890. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4891. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4892. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4893. * for gen < 8) and if DRRS is supported (to make sure the
  4894. * registers are not unnecessarily accessed).
  4895. */
  4896. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4897. crtc->config->has_drrs) {
  4898. I915_WRITE(PIPE_DATA_M2(transcoder),
  4899. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4900. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4901. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4902. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4903. }
  4904. } else {
  4905. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4906. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4907. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4908. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4909. }
  4910. }
  4911. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4912. {
  4913. if (crtc->config->has_pch_encoder)
  4914. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  4915. else
  4916. intel_cpu_transcoder_set_m_n(crtc, &crtc->config->dp_m_n,
  4917. &crtc->config->dp_m2_n2);
  4918. }
  4919. static void vlv_update_pll(struct intel_crtc *crtc,
  4920. struct intel_crtc_state *pipe_config)
  4921. {
  4922. u32 dpll, dpll_md;
  4923. /*
  4924. * Enable DPIO clock input. We should never disable the reference
  4925. * clock for pipe B, since VGA hotplug / manual detection depends
  4926. * on it.
  4927. */
  4928. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4929. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4930. /* We should never disable this, set it here for state tracking */
  4931. if (crtc->pipe == PIPE_B)
  4932. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4933. dpll |= DPLL_VCO_ENABLE;
  4934. pipe_config->dpll_hw_state.dpll = dpll;
  4935. dpll_md = (pipe_config->pixel_multiplier - 1)
  4936. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4937. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  4938. }
  4939. static void vlv_prepare_pll(struct intel_crtc *crtc,
  4940. const struct intel_crtc_state *pipe_config)
  4941. {
  4942. struct drm_device *dev = crtc->base.dev;
  4943. struct drm_i915_private *dev_priv = dev->dev_private;
  4944. int pipe = crtc->pipe;
  4945. u32 mdiv;
  4946. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4947. u32 coreclk, reg_val;
  4948. mutex_lock(&dev_priv->dpio_lock);
  4949. bestn = pipe_config->dpll.n;
  4950. bestm1 = pipe_config->dpll.m1;
  4951. bestm2 = pipe_config->dpll.m2;
  4952. bestp1 = pipe_config->dpll.p1;
  4953. bestp2 = pipe_config->dpll.p2;
  4954. /* See eDP HDMI DPIO driver vbios notes doc */
  4955. /* PLL B needs special handling */
  4956. if (pipe == PIPE_B)
  4957. vlv_pllb_recal_opamp(dev_priv, pipe);
  4958. /* Set up Tx target for periodic Rcomp update */
  4959. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4960. /* Disable target IRef on PLL */
  4961. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4962. reg_val &= 0x00ffffff;
  4963. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4964. /* Disable fast lock */
  4965. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4966. /* Set idtafcrecal before PLL is enabled */
  4967. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4968. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4969. mdiv |= ((bestn << DPIO_N_SHIFT));
  4970. mdiv |= (1 << DPIO_K_SHIFT);
  4971. /*
  4972. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4973. * but we don't support that).
  4974. * Note: don't use the DAC post divider as it seems unstable.
  4975. */
  4976. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4977. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4978. mdiv |= DPIO_ENABLE_CALIBRATION;
  4979. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4980. /* Set HBR and RBR LPF coefficients */
  4981. if (pipe_config->port_clock == 162000 ||
  4982. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  4983. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  4984. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4985. 0x009f0003);
  4986. else
  4987. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4988. 0x00d0000f);
  4989. if (pipe_config->has_dp_encoder) {
  4990. /* Use SSC source */
  4991. if (pipe == PIPE_A)
  4992. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4993. 0x0df40000);
  4994. else
  4995. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4996. 0x0df70000);
  4997. } else { /* HDMI or VGA */
  4998. /* Use bend source */
  4999. if (pipe == PIPE_A)
  5000. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5001. 0x0df70000);
  5002. else
  5003. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5004. 0x0df40000);
  5005. }
  5006. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5007. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5008. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5009. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5010. coreclk |= 0x01000000;
  5011. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5012. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5013. mutex_unlock(&dev_priv->dpio_lock);
  5014. }
  5015. static void chv_update_pll(struct intel_crtc *crtc,
  5016. struct intel_crtc_state *pipe_config)
  5017. {
  5018. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5019. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5020. DPLL_VCO_ENABLE;
  5021. if (crtc->pipe != PIPE_A)
  5022. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5023. pipe_config->dpll_hw_state.dpll_md =
  5024. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5025. }
  5026. static void chv_prepare_pll(struct intel_crtc *crtc,
  5027. const struct intel_crtc_state *pipe_config)
  5028. {
  5029. struct drm_device *dev = crtc->base.dev;
  5030. struct drm_i915_private *dev_priv = dev->dev_private;
  5031. int pipe = crtc->pipe;
  5032. int dpll_reg = DPLL(crtc->pipe);
  5033. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5034. u32 loopfilter, intcoeff;
  5035. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5036. int refclk;
  5037. bestn = pipe_config->dpll.n;
  5038. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5039. bestm1 = pipe_config->dpll.m1;
  5040. bestm2 = pipe_config->dpll.m2 >> 22;
  5041. bestp1 = pipe_config->dpll.p1;
  5042. bestp2 = pipe_config->dpll.p2;
  5043. /*
  5044. * Enable Refclk and SSC
  5045. */
  5046. I915_WRITE(dpll_reg,
  5047. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5048. mutex_lock(&dev_priv->dpio_lock);
  5049. /* p1 and p2 divider */
  5050. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5051. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5052. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5053. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5054. 1 << DPIO_CHV_K_DIV_SHIFT);
  5055. /* Feedback post-divider - m2 */
  5056. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5057. /* Feedback refclk divider - n and m1 */
  5058. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5059. DPIO_CHV_M1_DIV_BY_2 |
  5060. 1 << DPIO_CHV_N_DIV_SHIFT);
  5061. /* M2 fraction division */
  5062. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5063. /* M2 fraction division enable */
  5064. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  5065. DPIO_CHV_FRAC_DIV_EN |
  5066. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  5067. /* Loop filter */
  5068. refclk = i9xx_get_refclk(crtc, 0);
  5069. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  5070. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  5071. if (refclk == 100000)
  5072. intcoeff = 11;
  5073. else if (refclk == 38400)
  5074. intcoeff = 10;
  5075. else
  5076. intcoeff = 9;
  5077. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  5078. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5079. /* AFC Recal */
  5080. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5081. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5082. DPIO_AFC_RECAL);
  5083. mutex_unlock(&dev_priv->dpio_lock);
  5084. }
  5085. /**
  5086. * vlv_force_pll_on - forcibly enable just the PLL
  5087. * @dev_priv: i915 private structure
  5088. * @pipe: pipe PLL to enable
  5089. * @dpll: PLL configuration
  5090. *
  5091. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5092. * in cases where we need the PLL enabled even when @pipe is not going to
  5093. * be enabled.
  5094. */
  5095. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5096. const struct dpll *dpll)
  5097. {
  5098. struct intel_crtc *crtc =
  5099. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5100. struct intel_crtc_state pipe_config = {
  5101. .pixel_multiplier = 1,
  5102. .dpll = *dpll,
  5103. };
  5104. if (IS_CHERRYVIEW(dev)) {
  5105. chv_update_pll(crtc, &pipe_config);
  5106. chv_prepare_pll(crtc, &pipe_config);
  5107. chv_enable_pll(crtc, &pipe_config);
  5108. } else {
  5109. vlv_update_pll(crtc, &pipe_config);
  5110. vlv_prepare_pll(crtc, &pipe_config);
  5111. vlv_enable_pll(crtc, &pipe_config);
  5112. }
  5113. }
  5114. /**
  5115. * vlv_force_pll_off - forcibly disable just the PLL
  5116. * @dev_priv: i915 private structure
  5117. * @pipe: pipe PLL to disable
  5118. *
  5119. * Disable the PLL for @pipe. To be used in cases where we need
  5120. * the PLL enabled even when @pipe is not going to be enabled.
  5121. */
  5122. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5123. {
  5124. if (IS_CHERRYVIEW(dev))
  5125. chv_disable_pll(to_i915(dev), pipe);
  5126. else
  5127. vlv_disable_pll(to_i915(dev), pipe);
  5128. }
  5129. static void i9xx_update_pll(struct intel_crtc *crtc,
  5130. struct intel_crtc_state *crtc_state,
  5131. intel_clock_t *reduced_clock,
  5132. int num_connectors)
  5133. {
  5134. struct drm_device *dev = crtc->base.dev;
  5135. struct drm_i915_private *dev_priv = dev->dev_private;
  5136. u32 dpll;
  5137. bool is_sdvo;
  5138. struct dpll *clock = &crtc_state->dpll;
  5139. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5140. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5141. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5142. dpll = DPLL_VGA_MODE_DIS;
  5143. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5144. dpll |= DPLLB_MODE_LVDS;
  5145. else
  5146. dpll |= DPLLB_MODE_DAC_SERIAL;
  5147. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5148. dpll |= (crtc_state->pixel_multiplier - 1)
  5149. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5150. }
  5151. if (is_sdvo)
  5152. dpll |= DPLL_SDVO_HIGH_SPEED;
  5153. if (crtc_state->has_dp_encoder)
  5154. dpll |= DPLL_SDVO_HIGH_SPEED;
  5155. /* compute bitmask from p1 value */
  5156. if (IS_PINEVIEW(dev))
  5157. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5158. else {
  5159. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5160. if (IS_G4X(dev) && reduced_clock)
  5161. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5162. }
  5163. switch (clock->p2) {
  5164. case 5:
  5165. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5166. break;
  5167. case 7:
  5168. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5169. break;
  5170. case 10:
  5171. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5172. break;
  5173. case 14:
  5174. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5175. break;
  5176. }
  5177. if (INTEL_INFO(dev)->gen >= 4)
  5178. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5179. if (crtc_state->sdvo_tv_clock)
  5180. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5181. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5182. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5183. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5184. else
  5185. dpll |= PLL_REF_INPUT_DREFCLK;
  5186. dpll |= DPLL_VCO_ENABLE;
  5187. crtc_state->dpll_hw_state.dpll = dpll;
  5188. if (INTEL_INFO(dev)->gen >= 4) {
  5189. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5190. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5191. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5192. }
  5193. }
  5194. static void i8xx_update_pll(struct intel_crtc *crtc,
  5195. struct intel_crtc_state *crtc_state,
  5196. intel_clock_t *reduced_clock,
  5197. int num_connectors)
  5198. {
  5199. struct drm_device *dev = crtc->base.dev;
  5200. struct drm_i915_private *dev_priv = dev->dev_private;
  5201. u32 dpll;
  5202. struct dpll *clock = &crtc_state->dpll;
  5203. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5204. dpll = DPLL_VGA_MODE_DIS;
  5205. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5206. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5207. } else {
  5208. if (clock->p1 == 2)
  5209. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5210. else
  5211. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5212. if (clock->p2 == 4)
  5213. dpll |= PLL_P2_DIVIDE_BY_4;
  5214. }
  5215. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5216. dpll |= DPLL_DVO_2X_MODE;
  5217. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5218. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5219. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5220. else
  5221. dpll |= PLL_REF_INPUT_DREFCLK;
  5222. dpll |= DPLL_VCO_ENABLE;
  5223. crtc_state->dpll_hw_state.dpll = dpll;
  5224. }
  5225. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5226. {
  5227. struct drm_device *dev = intel_crtc->base.dev;
  5228. struct drm_i915_private *dev_priv = dev->dev_private;
  5229. enum pipe pipe = intel_crtc->pipe;
  5230. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5231. struct drm_display_mode *adjusted_mode =
  5232. &intel_crtc->config->base.adjusted_mode;
  5233. uint32_t crtc_vtotal, crtc_vblank_end;
  5234. int vsyncshift = 0;
  5235. /* We need to be careful not to changed the adjusted mode, for otherwise
  5236. * the hw state checker will get angry at the mismatch. */
  5237. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5238. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5239. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5240. /* the chip adds 2 halflines automatically */
  5241. crtc_vtotal -= 1;
  5242. crtc_vblank_end -= 1;
  5243. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5244. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5245. else
  5246. vsyncshift = adjusted_mode->crtc_hsync_start -
  5247. adjusted_mode->crtc_htotal / 2;
  5248. if (vsyncshift < 0)
  5249. vsyncshift += adjusted_mode->crtc_htotal;
  5250. }
  5251. if (INTEL_INFO(dev)->gen > 3)
  5252. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5253. I915_WRITE(HTOTAL(cpu_transcoder),
  5254. (adjusted_mode->crtc_hdisplay - 1) |
  5255. ((adjusted_mode->crtc_htotal - 1) << 16));
  5256. I915_WRITE(HBLANK(cpu_transcoder),
  5257. (adjusted_mode->crtc_hblank_start - 1) |
  5258. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5259. I915_WRITE(HSYNC(cpu_transcoder),
  5260. (adjusted_mode->crtc_hsync_start - 1) |
  5261. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5262. I915_WRITE(VTOTAL(cpu_transcoder),
  5263. (adjusted_mode->crtc_vdisplay - 1) |
  5264. ((crtc_vtotal - 1) << 16));
  5265. I915_WRITE(VBLANK(cpu_transcoder),
  5266. (adjusted_mode->crtc_vblank_start - 1) |
  5267. ((crtc_vblank_end - 1) << 16));
  5268. I915_WRITE(VSYNC(cpu_transcoder),
  5269. (adjusted_mode->crtc_vsync_start - 1) |
  5270. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5271. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5272. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5273. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5274. * bits. */
  5275. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5276. (pipe == PIPE_B || pipe == PIPE_C))
  5277. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5278. /* pipesrc controls the size that is scaled from, which should
  5279. * always be the user's requested size.
  5280. */
  5281. I915_WRITE(PIPESRC(pipe),
  5282. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5283. (intel_crtc->config->pipe_src_h - 1));
  5284. }
  5285. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5286. struct intel_crtc_state *pipe_config)
  5287. {
  5288. struct drm_device *dev = crtc->base.dev;
  5289. struct drm_i915_private *dev_priv = dev->dev_private;
  5290. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5291. uint32_t tmp;
  5292. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5293. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5294. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5295. tmp = I915_READ(HBLANK(cpu_transcoder));
  5296. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5297. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5298. tmp = I915_READ(HSYNC(cpu_transcoder));
  5299. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5300. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5301. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5302. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5303. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5304. tmp = I915_READ(VBLANK(cpu_transcoder));
  5305. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5306. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5307. tmp = I915_READ(VSYNC(cpu_transcoder));
  5308. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5309. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5310. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5311. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5312. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5313. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5314. }
  5315. tmp = I915_READ(PIPESRC(crtc->pipe));
  5316. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5317. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5318. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5319. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5320. }
  5321. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5322. struct intel_crtc_state *pipe_config)
  5323. {
  5324. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5325. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5326. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5327. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5328. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5329. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5330. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5331. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5332. mode->flags = pipe_config->base.adjusted_mode.flags;
  5333. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5334. mode->flags |= pipe_config->base.adjusted_mode.flags;
  5335. }
  5336. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5337. {
  5338. struct drm_device *dev = intel_crtc->base.dev;
  5339. struct drm_i915_private *dev_priv = dev->dev_private;
  5340. uint32_t pipeconf;
  5341. pipeconf = 0;
  5342. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5343. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5344. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5345. if (intel_crtc->config->double_wide)
  5346. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5347. /* only g4x and later have fancy bpc/dither controls */
  5348. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5349. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5350. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5351. pipeconf |= PIPECONF_DITHER_EN |
  5352. PIPECONF_DITHER_TYPE_SP;
  5353. switch (intel_crtc->config->pipe_bpp) {
  5354. case 18:
  5355. pipeconf |= PIPECONF_6BPC;
  5356. break;
  5357. case 24:
  5358. pipeconf |= PIPECONF_8BPC;
  5359. break;
  5360. case 30:
  5361. pipeconf |= PIPECONF_10BPC;
  5362. break;
  5363. default:
  5364. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5365. BUG();
  5366. }
  5367. }
  5368. if (HAS_PIPE_CXSR(dev)) {
  5369. if (intel_crtc->lowfreq_avail) {
  5370. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5371. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5372. } else {
  5373. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5374. }
  5375. }
  5376. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5377. if (INTEL_INFO(dev)->gen < 4 ||
  5378. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5379. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5380. else
  5381. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5382. } else
  5383. pipeconf |= PIPECONF_PROGRESSIVE;
  5384. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  5385. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5386. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5387. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5388. }
  5389. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  5390. struct intel_crtc_state *crtc_state)
  5391. {
  5392. struct drm_device *dev = crtc->base.dev;
  5393. struct drm_i915_private *dev_priv = dev->dev_private;
  5394. int refclk, num_connectors = 0;
  5395. intel_clock_t clock, reduced_clock;
  5396. bool ok, has_reduced_clock = false;
  5397. bool is_lvds = false, is_dsi = false;
  5398. struct intel_encoder *encoder;
  5399. const intel_limit_t *limit;
  5400. for_each_intel_encoder(dev, encoder) {
  5401. if (encoder->new_crtc != crtc)
  5402. continue;
  5403. switch (encoder->type) {
  5404. case INTEL_OUTPUT_LVDS:
  5405. is_lvds = true;
  5406. break;
  5407. case INTEL_OUTPUT_DSI:
  5408. is_dsi = true;
  5409. break;
  5410. default:
  5411. break;
  5412. }
  5413. num_connectors++;
  5414. }
  5415. if (is_dsi)
  5416. return 0;
  5417. if (!crtc_state->clock_set) {
  5418. refclk = i9xx_get_refclk(crtc, num_connectors);
  5419. /*
  5420. * Returns a set of divisors for the desired target clock with
  5421. * the given refclk, or FALSE. The returned values represent
  5422. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5423. * 2) / p1 / p2.
  5424. */
  5425. limit = intel_limit(crtc, refclk);
  5426. ok = dev_priv->display.find_dpll(limit, crtc,
  5427. crtc_state->port_clock,
  5428. refclk, NULL, &clock);
  5429. if (!ok) {
  5430. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5431. return -EINVAL;
  5432. }
  5433. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5434. /*
  5435. * Ensure we match the reduced clock's P to the target
  5436. * clock. If the clocks don't match, we can't switch
  5437. * the display clock by using the FP0/FP1. In such case
  5438. * we will disable the LVDS downclock feature.
  5439. */
  5440. has_reduced_clock =
  5441. dev_priv->display.find_dpll(limit, crtc,
  5442. dev_priv->lvds_downclock,
  5443. refclk, &clock,
  5444. &reduced_clock);
  5445. }
  5446. /* Compat-code for transition, will disappear. */
  5447. crtc_state->dpll.n = clock.n;
  5448. crtc_state->dpll.m1 = clock.m1;
  5449. crtc_state->dpll.m2 = clock.m2;
  5450. crtc_state->dpll.p1 = clock.p1;
  5451. crtc_state->dpll.p2 = clock.p2;
  5452. }
  5453. if (IS_GEN2(dev)) {
  5454. i8xx_update_pll(crtc, crtc_state,
  5455. has_reduced_clock ? &reduced_clock : NULL,
  5456. num_connectors);
  5457. } else if (IS_CHERRYVIEW(dev)) {
  5458. chv_update_pll(crtc, crtc_state);
  5459. } else if (IS_VALLEYVIEW(dev)) {
  5460. vlv_update_pll(crtc, crtc_state);
  5461. } else {
  5462. i9xx_update_pll(crtc, crtc_state,
  5463. has_reduced_clock ? &reduced_clock : NULL,
  5464. num_connectors);
  5465. }
  5466. return 0;
  5467. }
  5468. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5469. struct intel_crtc_state *pipe_config)
  5470. {
  5471. struct drm_device *dev = crtc->base.dev;
  5472. struct drm_i915_private *dev_priv = dev->dev_private;
  5473. uint32_t tmp;
  5474. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5475. return;
  5476. tmp = I915_READ(PFIT_CONTROL);
  5477. if (!(tmp & PFIT_ENABLE))
  5478. return;
  5479. /* Check whether the pfit is attached to our pipe. */
  5480. if (INTEL_INFO(dev)->gen < 4) {
  5481. if (crtc->pipe != PIPE_B)
  5482. return;
  5483. } else {
  5484. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5485. return;
  5486. }
  5487. pipe_config->gmch_pfit.control = tmp;
  5488. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5489. if (INTEL_INFO(dev)->gen < 5)
  5490. pipe_config->gmch_pfit.lvds_border_bits =
  5491. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5492. }
  5493. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5494. struct intel_crtc_state *pipe_config)
  5495. {
  5496. struct drm_device *dev = crtc->base.dev;
  5497. struct drm_i915_private *dev_priv = dev->dev_private;
  5498. int pipe = pipe_config->cpu_transcoder;
  5499. intel_clock_t clock;
  5500. u32 mdiv;
  5501. int refclk = 100000;
  5502. /* In case of MIPI DPLL will not even be used */
  5503. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5504. return;
  5505. mutex_lock(&dev_priv->dpio_lock);
  5506. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5507. mutex_unlock(&dev_priv->dpio_lock);
  5508. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5509. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5510. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5511. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5512. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5513. vlv_clock(refclk, &clock);
  5514. /* clock.dot is the fast clock */
  5515. pipe_config->port_clock = clock.dot / 5;
  5516. }
  5517. static void
  5518. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  5519. struct intel_initial_plane_config *plane_config)
  5520. {
  5521. struct drm_device *dev = crtc->base.dev;
  5522. struct drm_i915_private *dev_priv = dev->dev_private;
  5523. u32 val, base, offset;
  5524. int pipe = crtc->pipe, plane = crtc->plane;
  5525. int fourcc, pixel_format;
  5526. int aligned_height;
  5527. struct drm_framebuffer *fb;
  5528. struct intel_framebuffer *intel_fb;
  5529. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5530. if (!intel_fb) {
  5531. DRM_DEBUG_KMS("failed to alloc fb\n");
  5532. return;
  5533. }
  5534. fb = &intel_fb->base;
  5535. val = I915_READ(DSPCNTR(plane));
  5536. if (INTEL_INFO(dev)->gen >= 4)
  5537. if (val & DISPPLANE_TILED)
  5538. plane_config->tiling = I915_TILING_X;
  5539. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5540. fourcc = i9xx_format_to_fourcc(pixel_format);
  5541. fb->pixel_format = fourcc;
  5542. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  5543. if (INTEL_INFO(dev)->gen >= 4) {
  5544. if (plane_config->tiling)
  5545. offset = I915_READ(DSPTILEOFF(plane));
  5546. else
  5547. offset = I915_READ(DSPLINOFF(plane));
  5548. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5549. } else {
  5550. base = I915_READ(DSPADDR(plane));
  5551. }
  5552. plane_config->base = base;
  5553. val = I915_READ(PIPESRC(pipe));
  5554. fb->width = ((val >> 16) & 0xfff) + 1;
  5555. fb->height = ((val >> 0) & 0xfff) + 1;
  5556. val = I915_READ(DSPSTRIDE(pipe));
  5557. fb->pitches[0] = val & 0xffffffc0;
  5558. aligned_height = intel_fb_align_height(dev, fb->height,
  5559. plane_config->tiling);
  5560. plane_config->size = PAGE_ALIGN(fb->pitches[0] * aligned_height);
  5561. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5562. pipe_name(pipe), plane, fb->width, fb->height,
  5563. fb->bits_per_pixel, base, fb->pitches[0],
  5564. plane_config->size);
  5565. crtc->base.primary->fb = fb;
  5566. }
  5567. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5568. struct intel_crtc_state *pipe_config)
  5569. {
  5570. struct drm_device *dev = crtc->base.dev;
  5571. struct drm_i915_private *dev_priv = dev->dev_private;
  5572. int pipe = pipe_config->cpu_transcoder;
  5573. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5574. intel_clock_t clock;
  5575. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5576. int refclk = 100000;
  5577. mutex_lock(&dev_priv->dpio_lock);
  5578. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5579. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5580. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5581. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5582. mutex_unlock(&dev_priv->dpio_lock);
  5583. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5584. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5585. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5586. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5587. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5588. chv_clock(refclk, &clock);
  5589. /* clock.dot is the fast clock */
  5590. pipe_config->port_clock = clock.dot / 5;
  5591. }
  5592. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5593. struct intel_crtc_state *pipe_config)
  5594. {
  5595. struct drm_device *dev = crtc->base.dev;
  5596. struct drm_i915_private *dev_priv = dev->dev_private;
  5597. uint32_t tmp;
  5598. if (!intel_display_power_is_enabled(dev_priv,
  5599. POWER_DOMAIN_PIPE(crtc->pipe)))
  5600. return false;
  5601. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5602. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5603. tmp = I915_READ(PIPECONF(crtc->pipe));
  5604. if (!(tmp & PIPECONF_ENABLE))
  5605. return false;
  5606. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5607. switch (tmp & PIPECONF_BPC_MASK) {
  5608. case PIPECONF_6BPC:
  5609. pipe_config->pipe_bpp = 18;
  5610. break;
  5611. case PIPECONF_8BPC:
  5612. pipe_config->pipe_bpp = 24;
  5613. break;
  5614. case PIPECONF_10BPC:
  5615. pipe_config->pipe_bpp = 30;
  5616. break;
  5617. default:
  5618. break;
  5619. }
  5620. }
  5621. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5622. pipe_config->limited_color_range = true;
  5623. if (INTEL_INFO(dev)->gen < 4)
  5624. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5625. intel_get_pipe_timings(crtc, pipe_config);
  5626. i9xx_get_pfit_config(crtc, pipe_config);
  5627. if (INTEL_INFO(dev)->gen >= 4) {
  5628. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5629. pipe_config->pixel_multiplier =
  5630. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5631. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5632. pipe_config->dpll_hw_state.dpll_md = tmp;
  5633. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5634. tmp = I915_READ(DPLL(crtc->pipe));
  5635. pipe_config->pixel_multiplier =
  5636. ((tmp & SDVO_MULTIPLIER_MASK)
  5637. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5638. } else {
  5639. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5640. * port and will be fixed up in the encoder->get_config
  5641. * function. */
  5642. pipe_config->pixel_multiplier = 1;
  5643. }
  5644. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5645. if (!IS_VALLEYVIEW(dev)) {
  5646. /*
  5647. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5648. * on 830. Filter it out here so that we don't
  5649. * report errors due to that.
  5650. */
  5651. if (IS_I830(dev))
  5652. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5653. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5654. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5655. } else {
  5656. /* Mask out read-only status bits. */
  5657. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5658. DPLL_PORTC_READY_MASK |
  5659. DPLL_PORTB_READY_MASK);
  5660. }
  5661. if (IS_CHERRYVIEW(dev))
  5662. chv_crtc_clock_get(crtc, pipe_config);
  5663. else if (IS_VALLEYVIEW(dev))
  5664. vlv_crtc_clock_get(crtc, pipe_config);
  5665. else
  5666. i9xx_crtc_clock_get(crtc, pipe_config);
  5667. return true;
  5668. }
  5669. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5670. {
  5671. struct drm_i915_private *dev_priv = dev->dev_private;
  5672. struct intel_encoder *encoder;
  5673. u32 val, final;
  5674. bool has_lvds = false;
  5675. bool has_cpu_edp = false;
  5676. bool has_panel = false;
  5677. bool has_ck505 = false;
  5678. bool can_ssc = false;
  5679. /* We need to take the global config into account */
  5680. for_each_intel_encoder(dev, encoder) {
  5681. switch (encoder->type) {
  5682. case INTEL_OUTPUT_LVDS:
  5683. has_panel = true;
  5684. has_lvds = true;
  5685. break;
  5686. case INTEL_OUTPUT_EDP:
  5687. has_panel = true;
  5688. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5689. has_cpu_edp = true;
  5690. break;
  5691. default:
  5692. break;
  5693. }
  5694. }
  5695. if (HAS_PCH_IBX(dev)) {
  5696. has_ck505 = dev_priv->vbt.display_clock_mode;
  5697. can_ssc = has_ck505;
  5698. } else {
  5699. has_ck505 = false;
  5700. can_ssc = true;
  5701. }
  5702. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5703. has_panel, has_lvds, has_ck505);
  5704. /* Ironlake: try to setup display ref clock before DPLL
  5705. * enabling. This is only under driver's control after
  5706. * PCH B stepping, previous chipset stepping should be
  5707. * ignoring this setting.
  5708. */
  5709. val = I915_READ(PCH_DREF_CONTROL);
  5710. /* As we must carefully and slowly disable/enable each source in turn,
  5711. * compute the final state we want first and check if we need to
  5712. * make any changes at all.
  5713. */
  5714. final = val;
  5715. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5716. if (has_ck505)
  5717. final |= DREF_NONSPREAD_CK505_ENABLE;
  5718. else
  5719. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5720. final &= ~DREF_SSC_SOURCE_MASK;
  5721. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5722. final &= ~DREF_SSC1_ENABLE;
  5723. if (has_panel) {
  5724. final |= DREF_SSC_SOURCE_ENABLE;
  5725. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5726. final |= DREF_SSC1_ENABLE;
  5727. if (has_cpu_edp) {
  5728. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5729. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5730. else
  5731. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5732. } else
  5733. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5734. } else {
  5735. final |= DREF_SSC_SOURCE_DISABLE;
  5736. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5737. }
  5738. if (final == val)
  5739. return;
  5740. /* Always enable nonspread source */
  5741. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5742. if (has_ck505)
  5743. val |= DREF_NONSPREAD_CK505_ENABLE;
  5744. else
  5745. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5746. if (has_panel) {
  5747. val &= ~DREF_SSC_SOURCE_MASK;
  5748. val |= DREF_SSC_SOURCE_ENABLE;
  5749. /* SSC must be turned on before enabling the CPU output */
  5750. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5751. DRM_DEBUG_KMS("Using SSC on panel\n");
  5752. val |= DREF_SSC1_ENABLE;
  5753. } else
  5754. val &= ~DREF_SSC1_ENABLE;
  5755. /* Get SSC going before enabling the outputs */
  5756. I915_WRITE(PCH_DREF_CONTROL, val);
  5757. POSTING_READ(PCH_DREF_CONTROL);
  5758. udelay(200);
  5759. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5760. /* Enable CPU source on CPU attached eDP */
  5761. if (has_cpu_edp) {
  5762. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5763. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5764. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5765. } else
  5766. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5767. } else
  5768. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5769. I915_WRITE(PCH_DREF_CONTROL, val);
  5770. POSTING_READ(PCH_DREF_CONTROL);
  5771. udelay(200);
  5772. } else {
  5773. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5774. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5775. /* Turn off CPU output */
  5776. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5777. I915_WRITE(PCH_DREF_CONTROL, val);
  5778. POSTING_READ(PCH_DREF_CONTROL);
  5779. udelay(200);
  5780. /* Turn off the SSC source */
  5781. val &= ~DREF_SSC_SOURCE_MASK;
  5782. val |= DREF_SSC_SOURCE_DISABLE;
  5783. /* Turn off SSC1 */
  5784. val &= ~DREF_SSC1_ENABLE;
  5785. I915_WRITE(PCH_DREF_CONTROL, val);
  5786. POSTING_READ(PCH_DREF_CONTROL);
  5787. udelay(200);
  5788. }
  5789. BUG_ON(val != final);
  5790. }
  5791. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5792. {
  5793. uint32_t tmp;
  5794. tmp = I915_READ(SOUTH_CHICKEN2);
  5795. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5796. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5797. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5798. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5799. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5800. tmp = I915_READ(SOUTH_CHICKEN2);
  5801. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5802. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5803. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5804. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5805. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5806. }
  5807. /* WaMPhyProgramming:hsw */
  5808. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5809. {
  5810. uint32_t tmp;
  5811. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5812. tmp &= ~(0xFF << 24);
  5813. tmp |= (0x12 << 24);
  5814. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5815. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5816. tmp |= (1 << 11);
  5817. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5818. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5819. tmp |= (1 << 11);
  5820. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5821. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5822. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5823. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5824. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5825. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5826. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5827. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5828. tmp &= ~(7 << 13);
  5829. tmp |= (5 << 13);
  5830. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5831. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5832. tmp &= ~(7 << 13);
  5833. tmp |= (5 << 13);
  5834. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5835. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5836. tmp &= ~0xFF;
  5837. tmp |= 0x1C;
  5838. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5839. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5840. tmp &= ~0xFF;
  5841. tmp |= 0x1C;
  5842. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5843. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5844. tmp &= ~(0xFF << 16);
  5845. tmp |= (0x1C << 16);
  5846. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5847. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5848. tmp &= ~(0xFF << 16);
  5849. tmp |= (0x1C << 16);
  5850. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5851. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5852. tmp |= (1 << 27);
  5853. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5854. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5855. tmp |= (1 << 27);
  5856. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5857. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5858. tmp &= ~(0xF << 28);
  5859. tmp |= (4 << 28);
  5860. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5861. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5862. tmp &= ~(0xF << 28);
  5863. tmp |= (4 << 28);
  5864. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5865. }
  5866. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5867. * Programming" based on the parameters passed:
  5868. * - Sequence to enable CLKOUT_DP
  5869. * - Sequence to enable CLKOUT_DP without spread
  5870. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5871. */
  5872. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5873. bool with_fdi)
  5874. {
  5875. struct drm_i915_private *dev_priv = dev->dev_private;
  5876. uint32_t reg, tmp;
  5877. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5878. with_spread = true;
  5879. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5880. with_fdi, "LP PCH doesn't have FDI\n"))
  5881. with_fdi = false;
  5882. mutex_lock(&dev_priv->dpio_lock);
  5883. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5884. tmp &= ~SBI_SSCCTL_DISABLE;
  5885. tmp |= SBI_SSCCTL_PATHALT;
  5886. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5887. udelay(24);
  5888. if (with_spread) {
  5889. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5890. tmp &= ~SBI_SSCCTL_PATHALT;
  5891. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5892. if (with_fdi) {
  5893. lpt_reset_fdi_mphy(dev_priv);
  5894. lpt_program_fdi_mphy(dev_priv);
  5895. }
  5896. }
  5897. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5898. SBI_GEN0 : SBI_DBUFF0;
  5899. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5900. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5901. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5902. mutex_unlock(&dev_priv->dpio_lock);
  5903. }
  5904. /* Sequence to disable CLKOUT_DP */
  5905. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5906. {
  5907. struct drm_i915_private *dev_priv = dev->dev_private;
  5908. uint32_t reg, tmp;
  5909. mutex_lock(&dev_priv->dpio_lock);
  5910. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5911. SBI_GEN0 : SBI_DBUFF0;
  5912. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5913. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5914. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5915. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5916. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5917. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5918. tmp |= SBI_SSCCTL_PATHALT;
  5919. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5920. udelay(32);
  5921. }
  5922. tmp |= SBI_SSCCTL_DISABLE;
  5923. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5924. }
  5925. mutex_unlock(&dev_priv->dpio_lock);
  5926. }
  5927. static void lpt_init_pch_refclk(struct drm_device *dev)
  5928. {
  5929. struct intel_encoder *encoder;
  5930. bool has_vga = false;
  5931. for_each_intel_encoder(dev, encoder) {
  5932. switch (encoder->type) {
  5933. case INTEL_OUTPUT_ANALOG:
  5934. has_vga = true;
  5935. break;
  5936. default:
  5937. break;
  5938. }
  5939. }
  5940. if (has_vga)
  5941. lpt_enable_clkout_dp(dev, true, true);
  5942. else
  5943. lpt_disable_clkout_dp(dev);
  5944. }
  5945. /*
  5946. * Initialize reference clocks when the driver loads
  5947. */
  5948. void intel_init_pch_refclk(struct drm_device *dev)
  5949. {
  5950. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5951. ironlake_init_pch_refclk(dev);
  5952. else if (HAS_PCH_LPT(dev))
  5953. lpt_init_pch_refclk(dev);
  5954. }
  5955. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5956. {
  5957. struct drm_device *dev = crtc->dev;
  5958. struct drm_i915_private *dev_priv = dev->dev_private;
  5959. struct intel_encoder *encoder;
  5960. int num_connectors = 0;
  5961. bool is_lvds = false;
  5962. for_each_intel_encoder(dev, encoder) {
  5963. if (encoder->new_crtc != to_intel_crtc(crtc))
  5964. continue;
  5965. switch (encoder->type) {
  5966. case INTEL_OUTPUT_LVDS:
  5967. is_lvds = true;
  5968. break;
  5969. default:
  5970. break;
  5971. }
  5972. num_connectors++;
  5973. }
  5974. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5975. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5976. dev_priv->vbt.lvds_ssc_freq);
  5977. return dev_priv->vbt.lvds_ssc_freq;
  5978. }
  5979. return 120000;
  5980. }
  5981. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5982. {
  5983. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5984. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5985. int pipe = intel_crtc->pipe;
  5986. uint32_t val;
  5987. val = 0;
  5988. switch (intel_crtc->config->pipe_bpp) {
  5989. case 18:
  5990. val |= PIPECONF_6BPC;
  5991. break;
  5992. case 24:
  5993. val |= PIPECONF_8BPC;
  5994. break;
  5995. case 30:
  5996. val |= PIPECONF_10BPC;
  5997. break;
  5998. case 36:
  5999. val |= PIPECONF_12BPC;
  6000. break;
  6001. default:
  6002. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6003. BUG();
  6004. }
  6005. if (intel_crtc->config->dither)
  6006. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6007. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6008. val |= PIPECONF_INTERLACED_ILK;
  6009. else
  6010. val |= PIPECONF_PROGRESSIVE;
  6011. if (intel_crtc->config->limited_color_range)
  6012. val |= PIPECONF_COLOR_RANGE_SELECT;
  6013. I915_WRITE(PIPECONF(pipe), val);
  6014. POSTING_READ(PIPECONF(pipe));
  6015. }
  6016. /*
  6017. * Set up the pipe CSC unit.
  6018. *
  6019. * Currently only full range RGB to limited range RGB conversion
  6020. * is supported, but eventually this should handle various
  6021. * RGB<->YCbCr scenarios as well.
  6022. */
  6023. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6024. {
  6025. struct drm_device *dev = crtc->dev;
  6026. struct drm_i915_private *dev_priv = dev->dev_private;
  6027. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6028. int pipe = intel_crtc->pipe;
  6029. uint16_t coeff = 0x7800; /* 1.0 */
  6030. /*
  6031. * TODO: Check what kind of values actually come out of the pipe
  6032. * with these coeff/postoff values and adjust to get the best
  6033. * accuracy. Perhaps we even need to take the bpc value into
  6034. * consideration.
  6035. */
  6036. if (intel_crtc->config->limited_color_range)
  6037. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6038. /*
  6039. * GY/GU and RY/RU should be the other way around according
  6040. * to BSpec, but reality doesn't agree. Just set them up in
  6041. * a way that results in the correct picture.
  6042. */
  6043. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6044. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6045. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6046. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6047. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6048. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6049. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6050. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6051. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6052. if (INTEL_INFO(dev)->gen > 6) {
  6053. uint16_t postoff = 0;
  6054. if (intel_crtc->config->limited_color_range)
  6055. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6056. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6057. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6058. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6059. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6060. } else {
  6061. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6062. if (intel_crtc->config->limited_color_range)
  6063. mode |= CSC_BLACK_SCREEN_OFFSET;
  6064. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6065. }
  6066. }
  6067. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6068. {
  6069. struct drm_device *dev = crtc->dev;
  6070. struct drm_i915_private *dev_priv = dev->dev_private;
  6071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6072. enum pipe pipe = intel_crtc->pipe;
  6073. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6074. uint32_t val;
  6075. val = 0;
  6076. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6077. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6078. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6079. val |= PIPECONF_INTERLACED_ILK;
  6080. else
  6081. val |= PIPECONF_PROGRESSIVE;
  6082. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6083. POSTING_READ(PIPECONF(cpu_transcoder));
  6084. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6085. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6086. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6087. val = 0;
  6088. switch (intel_crtc->config->pipe_bpp) {
  6089. case 18:
  6090. val |= PIPEMISC_DITHER_6_BPC;
  6091. break;
  6092. case 24:
  6093. val |= PIPEMISC_DITHER_8_BPC;
  6094. break;
  6095. case 30:
  6096. val |= PIPEMISC_DITHER_10_BPC;
  6097. break;
  6098. case 36:
  6099. val |= PIPEMISC_DITHER_12_BPC;
  6100. break;
  6101. default:
  6102. /* Case prevented by pipe_config_set_bpp. */
  6103. BUG();
  6104. }
  6105. if (intel_crtc->config->dither)
  6106. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6107. I915_WRITE(PIPEMISC(pipe), val);
  6108. }
  6109. }
  6110. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6111. struct intel_crtc_state *crtc_state,
  6112. intel_clock_t *clock,
  6113. bool *has_reduced_clock,
  6114. intel_clock_t *reduced_clock)
  6115. {
  6116. struct drm_device *dev = crtc->dev;
  6117. struct drm_i915_private *dev_priv = dev->dev_private;
  6118. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6119. int refclk;
  6120. const intel_limit_t *limit;
  6121. bool ret, is_lvds = false;
  6122. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6123. refclk = ironlake_get_refclk(crtc);
  6124. /*
  6125. * Returns a set of divisors for the desired target clock with the given
  6126. * refclk, or FALSE. The returned values represent the clock equation:
  6127. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6128. */
  6129. limit = intel_limit(intel_crtc, refclk);
  6130. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6131. crtc_state->port_clock,
  6132. refclk, NULL, clock);
  6133. if (!ret)
  6134. return false;
  6135. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6136. /*
  6137. * Ensure we match the reduced clock's P to the target clock.
  6138. * If the clocks don't match, we can't switch the display clock
  6139. * by using the FP0/FP1. In such case we will disable the LVDS
  6140. * downclock feature.
  6141. */
  6142. *has_reduced_clock =
  6143. dev_priv->display.find_dpll(limit, intel_crtc,
  6144. dev_priv->lvds_downclock,
  6145. refclk, clock,
  6146. reduced_clock);
  6147. }
  6148. return true;
  6149. }
  6150. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6151. {
  6152. /*
  6153. * Account for spread spectrum to avoid
  6154. * oversubscribing the link. Max center spread
  6155. * is 2.5%; use 5% for safety's sake.
  6156. */
  6157. u32 bps = target_clock * bpp * 21 / 20;
  6158. return DIV_ROUND_UP(bps, link_bw * 8);
  6159. }
  6160. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6161. {
  6162. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6163. }
  6164. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6165. struct intel_crtc_state *crtc_state,
  6166. u32 *fp,
  6167. intel_clock_t *reduced_clock, u32 *fp2)
  6168. {
  6169. struct drm_crtc *crtc = &intel_crtc->base;
  6170. struct drm_device *dev = crtc->dev;
  6171. struct drm_i915_private *dev_priv = dev->dev_private;
  6172. struct intel_encoder *intel_encoder;
  6173. uint32_t dpll;
  6174. int factor, num_connectors = 0;
  6175. bool is_lvds = false, is_sdvo = false;
  6176. for_each_intel_encoder(dev, intel_encoder) {
  6177. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6178. continue;
  6179. switch (intel_encoder->type) {
  6180. case INTEL_OUTPUT_LVDS:
  6181. is_lvds = true;
  6182. break;
  6183. case INTEL_OUTPUT_SDVO:
  6184. case INTEL_OUTPUT_HDMI:
  6185. is_sdvo = true;
  6186. break;
  6187. default:
  6188. break;
  6189. }
  6190. num_connectors++;
  6191. }
  6192. /* Enable autotuning of the PLL clock (if permissible) */
  6193. factor = 21;
  6194. if (is_lvds) {
  6195. if ((intel_panel_use_ssc(dev_priv) &&
  6196. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6197. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6198. factor = 25;
  6199. } else if (crtc_state->sdvo_tv_clock)
  6200. factor = 20;
  6201. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6202. *fp |= FP_CB_TUNE;
  6203. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6204. *fp2 |= FP_CB_TUNE;
  6205. dpll = 0;
  6206. if (is_lvds)
  6207. dpll |= DPLLB_MODE_LVDS;
  6208. else
  6209. dpll |= DPLLB_MODE_DAC_SERIAL;
  6210. dpll |= (crtc_state->pixel_multiplier - 1)
  6211. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6212. if (is_sdvo)
  6213. dpll |= DPLL_SDVO_HIGH_SPEED;
  6214. if (crtc_state->has_dp_encoder)
  6215. dpll |= DPLL_SDVO_HIGH_SPEED;
  6216. /* compute bitmask from p1 value */
  6217. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6218. /* also FPA1 */
  6219. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6220. switch (crtc_state->dpll.p2) {
  6221. case 5:
  6222. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6223. break;
  6224. case 7:
  6225. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6226. break;
  6227. case 10:
  6228. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6229. break;
  6230. case 14:
  6231. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6232. break;
  6233. }
  6234. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6235. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6236. else
  6237. dpll |= PLL_REF_INPUT_DREFCLK;
  6238. return dpll | DPLL_VCO_ENABLE;
  6239. }
  6240. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6241. struct intel_crtc_state *crtc_state)
  6242. {
  6243. struct drm_device *dev = crtc->base.dev;
  6244. intel_clock_t clock, reduced_clock;
  6245. u32 dpll = 0, fp = 0, fp2 = 0;
  6246. bool ok, has_reduced_clock = false;
  6247. bool is_lvds = false;
  6248. struct intel_shared_dpll *pll;
  6249. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6250. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6251. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6252. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  6253. &has_reduced_clock, &reduced_clock);
  6254. if (!ok && !crtc_state->clock_set) {
  6255. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6256. return -EINVAL;
  6257. }
  6258. /* Compat-code for transition, will disappear. */
  6259. if (!crtc_state->clock_set) {
  6260. crtc_state->dpll.n = clock.n;
  6261. crtc_state->dpll.m1 = clock.m1;
  6262. crtc_state->dpll.m2 = clock.m2;
  6263. crtc_state->dpll.p1 = clock.p1;
  6264. crtc_state->dpll.p2 = clock.p2;
  6265. }
  6266. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6267. if (crtc_state->has_pch_encoder) {
  6268. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6269. if (has_reduced_clock)
  6270. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6271. dpll = ironlake_compute_dpll(crtc, crtc_state,
  6272. &fp, &reduced_clock,
  6273. has_reduced_clock ? &fp2 : NULL);
  6274. crtc_state->dpll_hw_state.dpll = dpll;
  6275. crtc_state->dpll_hw_state.fp0 = fp;
  6276. if (has_reduced_clock)
  6277. crtc_state->dpll_hw_state.fp1 = fp2;
  6278. else
  6279. crtc_state->dpll_hw_state.fp1 = fp;
  6280. pll = intel_get_shared_dpll(crtc, crtc_state);
  6281. if (pll == NULL) {
  6282. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6283. pipe_name(crtc->pipe));
  6284. return -EINVAL;
  6285. }
  6286. }
  6287. if (is_lvds && has_reduced_clock && i915.powersave)
  6288. crtc->lowfreq_avail = true;
  6289. else
  6290. crtc->lowfreq_avail = false;
  6291. return 0;
  6292. }
  6293. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6294. struct intel_link_m_n *m_n)
  6295. {
  6296. struct drm_device *dev = crtc->base.dev;
  6297. struct drm_i915_private *dev_priv = dev->dev_private;
  6298. enum pipe pipe = crtc->pipe;
  6299. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6300. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6301. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6302. & ~TU_SIZE_MASK;
  6303. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6304. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6305. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6306. }
  6307. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6308. enum transcoder transcoder,
  6309. struct intel_link_m_n *m_n,
  6310. struct intel_link_m_n *m2_n2)
  6311. {
  6312. struct drm_device *dev = crtc->base.dev;
  6313. struct drm_i915_private *dev_priv = dev->dev_private;
  6314. enum pipe pipe = crtc->pipe;
  6315. if (INTEL_INFO(dev)->gen >= 5) {
  6316. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6317. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6318. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6319. & ~TU_SIZE_MASK;
  6320. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6321. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6322. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6323. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6324. * gen < 8) and if DRRS is supported (to make sure the
  6325. * registers are not unnecessarily read).
  6326. */
  6327. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6328. crtc->config->has_drrs) {
  6329. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6330. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6331. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6332. & ~TU_SIZE_MASK;
  6333. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6334. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6335. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6336. }
  6337. } else {
  6338. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6339. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6340. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6341. & ~TU_SIZE_MASK;
  6342. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6343. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6344. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6345. }
  6346. }
  6347. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6348. struct intel_crtc_state *pipe_config)
  6349. {
  6350. if (pipe_config->has_pch_encoder)
  6351. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6352. else
  6353. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6354. &pipe_config->dp_m_n,
  6355. &pipe_config->dp_m2_n2);
  6356. }
  6357. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6358. struct intel_crtc_state *pipe_config)
  6359. {
  6360. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6361. &pipe_config->fdi_m_n, NULL);
  6362. }
  6363. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6364. struct intel_crtc_state *pipe_config)
  6365. {
  6366. struct drm_device *dev = crtc->base.dev;
  6367. struct drm_i915_private *dev_priv = dev->dev_private;
  6368. uint32_t tmp;
  6369. tmp = I915_READ(PS_CTL(crtc->pipe));
  6370. if (tmp & PS_ENABLE) {
  6371. pipe_config->pch_pfit.enabled = true;
  6372. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6373. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6374. }
  6375. }
  6376. static void
  6377. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6378. struct intel_initial_plane_config *plane_config)
  6379. {
  6380. struct drm_device *dev = crtc->base.dev;
  6381. struct drm_i915_private *dev_priv = dev->dev_private;
  6382. u32 val, base, offset, stride_mult;
  6383. int pipe = crtc->pipe;
  6384. int fourcc, pixel_format;
  6385. int aligned_height;
  6386. struct drm_framebuffer *fb;
  6387. struct intel_framebuffer *intel_fb;
  6388. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6389. if (!intel_fb) {
  6390. DRM_DEBUG_KMS("failed to alloc fb\n");
  6391. return;
  6392. }
  6393. fb = &intel_fb->base;
  6394. val = I915_READ(PLANE_CTL(pipe, 0));
  6395. if (val & PLANE_CTL_TILED_MASK)
  6396. plane_config->tiling = I915_TILING_X;
  6397. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  6398. fourcc = skl_format_to_fourcc(pixel_format,
  6399. val & PLANE_CTL_ORDER_RGBX,
  6400. val & PLANE_CTL_ALPHA_MASK);
  6401. fb->pixel_format = fourcc;
  6402. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6403. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  6404. plane_config->base = base;
  6405. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  6406. val = I915_READ(PLANE_SIZE(pipe, 0));
  6407. fb->height = ((val >> 16) & 0xfff) + 1;
  6408. fb->width = ((val >> 0) & 0x1fff) + 1;
  6409. val = I915_READ(PLANE_STRIDE(pipe, 0));
  6410. switch (plane_config->tiling) {
  6411. case I915_TILING_NONE:
  6412. stride_mult = 64;
  6413. break;
  6414. case I915_TILING_X:
  6415. stride_mult = 512;
  6416. break;
  6417. default:
  6418. MISSING_CASE(plane_config->tiling);
  6419. goto error;
  6420. }
  6421. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  6422. aligned_height = intel_fb_align_height(dev, fb->height,
  6423. plane_config->tiling);
  6424. plane_config->size = ALIGN(fb->pitches[0] * aligned_height, PAGE_SIZE);
  6425. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6426. pipe_name(pipe), fb->width, fb->height,
  6427. fb->bits_per_pixel, base, fb->pitches[0],
  6428. plane_config->size);
  6429. crtc->base.primary->fb = fb;
  6430. return;
  6431. error:
  6432. kfree(fb);
  6433. }
  6434. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6435. struct intel_crtc_state *pipe_config)
  6436. {
  6437. struct drm_device *dev = crtc->base.dev;
  6438. struct drm_i915_private *dev_priv = dev->dev_private;
  6439. uint32_t tmp;
  6440. tmp = I915_READ(PF_CTL(crtc->pipe));
  6441. if (tmp & PF_ENABLE) {
  6442. pipe_config->pch_pfit.enabled = true;
  6443. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6444. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6445. /* We currently do not free assignements of panel fitters on
  6446. * ivb/hsw (since we don't use the higher upscaling modes which
  6447. * differentiates them) so just WARN about this case for now. */
  6448. if (IS_GEN7(dev)) {
  6449. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6450. PF_PIPE_SEL_IVB(crtc->pipe));
  6451. }
  6452. }
  6453. }
  6454. static void
  6455. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  6456. struct intel_initial_plane_config *plane_config)
  6457. {
  6458. struct drm_device *dev = crtc->base.dev;
  6459. struct drm_i915_private *dev_priv = dev->dev_private;
  6460. u32 val, base, offset;
  6461. int pipe = crtc->pipe;
  6462. int fourcc, pixel_format;
  6463. int aligned_height;
  6464. struct drm_framebuffer *fb;
  6465. struct intel_framebuffer *intel_fb;
  6466. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6467. if (!intel_fb) {
  6468. DRM_DEBUG_KMS("failed to alloc fb\n");
  6469. return;
  6470. }
  6471. fb = &intel_fb->base;
  6472. val = I915_READ(DSPCNTR(pipe));
  6473. if (INTEL_INFO(dev)->gen >= 4)
  6474. if (val & DISPPLANE_TILED)
  6475. plane_config->tiling = I915_TILING_X;
  6476. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6477. fourcc = i9xx_format_to_fourcc(pixel_format);
  6478. fb->pixel_format = fourcc;
  6479. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6480. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  6481. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6482. offset = I915_READ(DSPOFFSET(pipe));
  6483. } else {
  6484. if (plane_config->tiling)
  6485. offset = I915_READ(DSPTILEOFF(pipe));
  6486. else
  6487. offset = I915_READ(DSPLINOFF(pipe));
  6488. }
  6489. plane_config->base = base;
  6490. val = I915_READ(PIPESRC(pipe));
  6491. fb->width = ((val >> 16) & 0xfff) + 1;
  6492. fb->height = ((val >> 0) & 0xfff) + 1;
  6493. val = I915_READ(DSPSTRIDE(pipe));
  6494. fb->pitches[0] = val & 0xffffffc0;
  6495. aligned_height = intel_fb_align_height(dev, fb->height,
  6496. plane_config->tiling);
  6497. plane_config->size = PAGE_ALIGN(fb->pitches[0] * aligned_height);
  6498. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6499. pipe_name(pipe), fb->width, fb->height,
  6500. fb->bits_per_pixel, base, fb->pitches[0],
  6501. plane_config->size);
  6502. crtc->base.primary->fb = fb;
  6503. }
  6504. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6505. struct intel_crtc_state *pipe_config)
  6506. {
  6507. struct drm_device *dev = crtc->base.dev;
  6508. struct drm_i915_private *dev_priv = dev->dev_private;
  6509. uint32_t tmp;
  6510. if (!intel_display_power_is_enabled(dev_priv,
  6511. POWER_DOMAIN_PIPE(crtc->pipe)))
  6512. return false;
  6513. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6514. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6515. tmp = I915_READ(PIPECONF(crtc->pipe));
  6516. if (!(tmp & PIPECONF_ENABLE))
  6517. return false;
  6518. switch (tmp & PIPECONF_BPC_MASK) {
  6519. case PIPECONF_6BPC:
  6520. pipe_config->pipe_bpp = 18;
  6521. break;
  6522. case PIPECONF_8BPC:
  6523. pipe_config->pipe_bpp = 24;
  6524. break;
  6525. case PIPECONF_10BPC:
  6526. pipe_config->pipe_bpp = 30;
  6527. break;
  6528. case PIPECONF_12BPC:
  6529. pipe_config->pipe_bpp = 36;
  6530. break;
  6531. default:
  6532. break;
  6533. }
  6534. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6535. pipe_config->limited_color_range = true;
  6536. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6537. struct intel_shared_dpll *pll;
  6538. pipe_config->has_pch_encoder = true;
  6539. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6540. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6541. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6542. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6543. if (HAS_PCH_IBX(dev_priv->dev)) {
  6544. pipe_config->shared_dpll =
  6545. (enum intel_dpll_id) crtc->pipe;
  6546. } else {
  6547. tmp = I915_READ(PCH_DPLL_SEL);
  6548. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6549. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6550. else
  6551. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6552. }
  6553. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6554. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6555. &pipe_config->dpll_hw_state));
  6556. tmp = pipe_config->dpll_hw_state.dpll;
  6557. pipe_config->pixel_multiplier =
  6558. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6559. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6560. ironlake_pch_clock_get(crtc, pipe_config);
  6561. } else {
  6562. pipe_config->pixel_multiplier = 1;
  6563. }
  6564. intel_get_pipe_timings(crtc, pipe_config);
  6565. ironlake_get_pfit_config(crtc, pipe_config);
  6566. return true;
  6567. }
  6568. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6569. {
  6570. struct drm_device *dev = dev_priv->dev;
  6571. struct intel_crtc *crtc;
  6572. for_each_intel_crtc(dev, crtc)
  6573. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6574. pipe_name(crtc->pipe));
  6575. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6576. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6577. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6578. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6579. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6580. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6581. "CPU PWM1 enabled\n");
  6582. if (IS_HASWELL(dev))
  6583. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6584. "CPU PWM2 enabled\n");
  6585. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6586. "PCH PWM1 enabled\n");
  6587. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6588. "Utility pin enabled\n");
  6589. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6590. /*
  6591. * In theory we can still leave IRQs enabled, as long as only the HPD
  6592. * interrupts remain enabled. We used to check for that, but since it's
  6593. * gen-specific and since we only disable LCPLL after we fully disable
  6594. * the interrupts, the check below should be enough.
  6595. */
  6596. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6597. }
  6598. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6599. {
  6600. struct drm_device *dev = dev_priv->dev;
  6601. if (IS_HASWELL(dev))
  6602. return I915_READ(D_COMP_HSW);
  6603. else
  6604. return I915_READ(D_COMP_BDW);
  6605. }
  6606. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6607. {
  6608. struct drm_device *dev = dev_priv->dev;
  6609. if (IS_HASWELL(dev)) {
  6610. mutex_lock(&dev_priv->rps.hw_lock);
  6611. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6612. val))
  6613. DRM_ERROR("Failed to write to D_COMP\n");
  6614. mutex_unlock(&dev_priv->rps.hw_lock);
  6615. } else {
  6616. I915_WRITE(D_COMP_BDW, val);
  6617. POSTING_READ(D_COMP_BDW);
  6618. }
  6619. }
  6620. /*
  6621. * This function implements pieces of two sequences from BSpec:
  6622. * - Sequence for display software to disable LCPLL
  6623. * - Sequence for display software to allow package C8+
  6624. * The steps implemented here are just the steps that actually touch the LCPLL
  6625. * register. Callers should take care of disabling all the display engine
  6626. * functions, doing the mode unset, fixing interrupts, etc.
  6627. */
  6628. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6629. bool switch_to_fclk, bool allow_power_down)
  6630. {
  6631. uint32_t val;
  6632. assert_can_disable_lcpll(dev_priv);
  6633. val = I915_READ(LCPLL_CTL);
  6634. if (switch_to_fclk) {
  6635. val |= LCPLL_CD_SOURCE_FCLK;
  6636. I915_WRITE(LCPLL_CTL, val);
  6637. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6638. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6639. DRM_ERROR("Switching to FCLK failed\n");
  6640. val = I915_READ(LCPLL_CTL);
  6641. }
  6642. val |= LCPLL_PLL_DISABLE;
  6643. I915_WRITE(LCPLL_CTL, val);
  6644. POSTING_READ(LCPLL_CTL);
  6645. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6646. DRM_ERROR("LCPLL still locked\n");
  6647. val = hsw_read_dcomp(dev_priv);
  6648. val |= D_COMP_COMP_DISABLE;
  6649. hsw_write_dcomp(dev_priv, val);
  6650. ndelay(100);
  6651. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6652. 1))
  6653. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6654. if (allow_power_down) {
  6655. val = I915_READ(LCPLL_CTL);
  6656. val |= LCPLL_POWER_DOWN_ALLOW;
  6657. I915_WRITE(LCPLL_CTL, val);
  6658. POSTING_READ(LCPLL_CTL);
  6659. }
  6660. }
  6661. /*
  6662. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6663. * source.
  6664. */
  6665. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6666. {
  6667. uint32_t val;
  6668. val = I915_READ(LCPLL_CTL);
  6669. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6670. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6671. return;
  6672. /*
  6673. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6674. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6675. */
  6676. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6677. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6678. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6679. I915_WRITE(LCPLL_CTL, val);
  6680. POSTING_READ(LCPLL_CTL);
  6681. }
  6682. val = hsw_read_dcomp(dev_priv);
  6683. val |= D_COMP_COMP_FORCE;
  6684. val &= ~D_COMP_COMP_DISABLE;
  6685. hsw_write_dcomp(dev_priv, val);
  6686. val = I915_READ(LCPLL_CTL);
  6687. val &= ~LCPLL_PLL_DISABLE;
  6688. I915_WRITE(LCPLL_CTL, val);
  6689. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6690. DRM_ERROR("LCPLL not locked yet\n");
  6691. if (val & LCPLL_CD_SOURCE_FCLK) {
  6692. val = I915_READ(LCPLL_CTL);
  6693. val &= ~LCPLL_CD_SOURCE_FCLK;
  6694. I915_WRITE(LCPLL_CTL, val);
  6695. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6696. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6697. DRM_ERROR("Switching back to LCPLL failed\n");
  6698. }
  6699. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6700. }
  6701. /*
  6702. * Package states C8 and deeper are really deep PC states that can only be
  6703. * reached when all the devices on the system allow it, so even if the graphics
  6704. * device allows PC8+, it doesn't mean the system will actually get to these
  6705. * states. Our driver only allows PC8+ when going into runtime PM.
  6706. *
  6707. * The requirements for PC8+ are that all the outputs are disabled, the power
  6708. * well is disabled and most interrupts are disabled, and these are also
  6709. * requirements for runtime PM. When these conditions are met, we manually do
  6710. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6711. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6712. * hang the machine.
  6713. *
  6714. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6715. * the state of some registers, so when we come back from PC8+ we need to
  6716. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6717. * need to take care of the registers kept by RC6. Notice that this happens even
  6718. * if we don't put the device in PCI D3 state (which is what currently happens
  6719. * because of the runtime PM support).
  6720. *
  6721. * For more, read "Display Sequences for Package C8" on the hardware
  6722. * documentation.
  6723. */
  6724. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6725. {
  6726. struct drm_device *dev = dev_priv->dev;
  6727. uint32_t val;
  6728. DRM_DEBUG_KMS("Enabling package C8+\n");
  6729. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6730. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6731. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6732. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6733. }
  6734. lpt_disable_clkout_dp(dev);
  6735. hsw_disable_lcpll(dev_priv, true, true);
  6736. }
  6737. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6738. {
  6739. struct drm_device *dev = dev_priv->dev;
  6740. uint32_t val;
  6741. DRM_DEBUG_KMS("Disabling package C8+\n");
  6742. hsw_restore_lcpll(dev_priv);
  6743. lpt_init_pch_refclk(dev);
  6744. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6745. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6746. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6747. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6748. }
  6749. intel_prepare_ddi(dev);
  6750. }
  6751. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  6752. struct intel_crtc_state *crtc_state)
  6753. {
  6754. if (!intel_ddi_pll_select(crtc, crtc_state))
  6755. return -EINVAL;
  6756. crtc->lowfreq_avail = false;
  6757. return 0;
  6758. }
  6759. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  6760. enum port port,
  6761. struct intel_crtc_state *pipe_config)
  6762. {
  6763. u32 temp, dpll_ctl1;
  6764. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  6765. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  6766. switch (pipe_config->ddi_pll_sel) {
  6767. case SKL_DPLL0:
  6768. /*
  6769. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  6770. * of the shared DPLL framework and thus needs to be read out
  6771. * separately
  6772. */
  6773. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  6774. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  6775. break;
  6776. case SKL_DPLL1:
  6777. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  6778. break;
  6779. case SKL_DPLL2:
  6780. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  6781. break;
  6782. case SKL_DPLL3:
  6783. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  6784. break;
  6785. }
  6786. }
  6787. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6788. enum port port,
  6789. struct intel_crtc_state *pipe_config)
  6790. {
  6791. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6792. switch (pipe_config->ddi_pll_sel) {
  6793. case PORT_CLK_SEL_WRPLL1:
  6794. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6795. break;
  6796. case PORT_CLK_SEL_WRPLL2:
  6797. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6798. break;
  6799. }
  6800. }
  6801. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6802. struct intel_crtc_state *pipe_config)
  6803. {
  6804. struct drm_device *dev = crtc->base.dev;
  6805. struct drm_i915_private *dev_priv = dev->dev_private;
  6806. struct intel_shared_dpll *pll;
  6807. enum port port;
  6808. uint32_t tmp;
  6809. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6810. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6811. if (IS_SKYLAKE(dev))
  6812. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  6813. else
  6814. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6815. if (pipe_config->shared_dpll >= 0) {
  6816. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6817. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6818. &pipe_config->dpll_hw_state));
  6819. }
  6820. /*
  6821. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6822. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6823. * the PCH transcoder is on.
  6824. */
  6825. if (INTEL_INFO(dev)->gen < 9 &&
  6826. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6827. pipe_config->has_pch_encoder = true;
  6828. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6829. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6830. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6831. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6832. }
  6833. }
  6834. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6835. struct intel_crtc_state *pipe_config)
  6836. {
  6837. struct drm_device *dev = crtc->base.dev;
  6838. struct drm_i915_private *dev_priv = dev->dev_private;
  6839. enum intel_display_power_domain pfit_domain;
  6840. uint32_t tmp;
  6841. if (!intel_display_power_is_enabled(dev_priv,
  6842. POWER_DOMAIN_PIPE(crtc->pipe)))
  6843. return false;
  6844. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6845. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6846. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6847. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6848. enum pipe trans_edp_pipe;
  6849. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6850. default:
  6851. WARN(1, "unknown pipe linked to edp transcoder\n");
  6852. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6853. case TRANS_DDI_EDP_INPUT_A_ON:
  6854. trans_edp_pipe = PIPE_A;
  6855. break;
  6856. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6857. trans_edp_pipe = PIPE_B;
  6858. break;
  6859. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6860. trans_edp_pipe = PIPE_C;
  6861. break;
  6862. }
  6863. if (trans_edp_pipe == crtc->pipe)
  6864. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6865. }
  6866. if (!intel_display_power_is_enabled(dev_priv,
  6867. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6868. return false;
  6869. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6870. if (!(tmp & PIPECONF_ENABLE))
  6871. return false;
  6872. haswell_get_ddi_port_state(crtc, pipe_config);
  6873. intel_get_pipe_timings(crtc, pipe_config);
  6874. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6875. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  6876. if (IS_SKYLAKE(dev))
  6877. skylake_get_pfit_config(crtc, pipe_config);
  6878. else
  6879. ironlake_get_pfit_config(crtc, pipe_config);
  6880. }
  6881. if (IS_HASWELL(dev))
  6882. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6883. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6884. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  6885. pipe_config->pixel_multiplier =
  6886. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  6887. } else {
  6888. pipe_config->pixel_multiplier = 1;
  6889. }
  6890. return true;
  6891. }
  6892. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6893. {
  6894. struct drm_device *dev = crtc->dev;
  6895. struct drm_i915_private *dev_priv = dev->dev_private;
  6896. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6897. uint32_t cntl = 0, size = 0;
  6898. if (base) {
  6899. unsigned int width = intel_crtc->cursor_width;
  6900. unsigned int height = intel_crtc->cursor_height;
  6901. unsigned int stride = roundup_pow_of_two(width) * 4;
  6902. switch (stride) {
  6903. default:
  6904. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6905. width, stride);
  6906. stride = 256;
  6907. /* fallthrough */
  6908. case 256:
  6909. case 512:
  6910. case 1024:
  6911. case 2048:
  6912. break;
  6913. }
  6914. cntl |= CURSOR_ENABLE |
  6915. CURSOR_GAMMA_ENABLE |
  6916. CURSOR_FORMAT_ARGB |
  6917. CURSOR_STRIDE(stride);
  6918. size = (height << 12) | width;
  6919. }
  6920. if (intel_crtc->cursor_cntl != 0 &&
  6921. (intel_crtc->cursor_base != base ||
  6922. intel_crtc->cursor_size != size ||
  6923. intel_crtc->cursor_cntl != cntl)) {
  6924. /* On these chipsets we can only modify the base/size/stride
  6925. * whilst the cursor is disabled.
  6926. */
  6927. I915_WRITE(_CURACNTR, 0);
  6928. POSTING_READ(_CURACNTR);
  6929. intel_crtc->cursor_cntl = 0;
  6930. }
  6931. if (intel_crtc->cursor_base != base) {
  6932. I915_WRITE(_CURABASE, base);
  6933. intel_crtc->cursor_base = base;
  6934. }
  6935. if (intel_crtc->cursor_size != size) {
  6936. I915_WRITE(CURSIZE, size);
  6937. intel_crtc->cursor_size = size;
  6938. }
  6939. if (intel_crtc->cursor_cntl != cntl) {
  6940. I915_WRITE(_CURACNTR, cntl);
  6941. POSTING_READ(_CURACNTR);
  6942. intel_crtc->cursor_cntl = cntl;
  6943. }
  6944. }
  6945. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6946. {
  6947. struct drm_device *dev = crtc->dev;
  6948. struct drm_i915_private *dev_priv = dev->dev_private;
  6949. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6950. int pipe = intel_crtc->pipe;
  6951. uint32_t cntl;
  6952. cntl = 0;
  6953. if (base) {
  6954. cntl = MCURSOR_GAMMA_ENABLE;
  6955. switch (intel_crtc->cursor_width) {
  6956. case 64:
  6957. cntl |= CURSOR_MODE_64_ARGB_AX;
  6958. break;
  6959. case 128:
  6960. cntl |= CURSOR_MODE_128_ARGB_AX;
  6961. break;
  6962. case 256:
  6963. cntl |= CURSOR_MODE_256_ARGB_AX;
  6964. break;
  6965. default:
  6966. MISSING_CASE(intel_crtc->cursor_width);
  6967. return;
  6968. }
  6969. cntl |= pipe << 28; /* Connect to correct pipe */
  6970. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6971. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6972. }
  6973. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  6974. cntl |= CURSOR_ROTATE_180;
  6975. if (intel_crtc->cursor_cntl != cntl) {
  6976. I915_WRITE(CURCNTR(pipe), cntl);
  6977. POSTING_READ(CURCNTR(pipe));
  6978. intel_crtc->cursor_cntl = cntl;
  6979. }
  6980. /* and commit changes on next vblank */
  6981. I915_WRITE(CURBASE(pipe), base);
  6982. POSTING_READ(CURBASE(pipe));
  6983. intel_crtc->cursor_base = base;
  6984. }
  6985. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6986. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6987. bool on)
  6988. {
  6989. struct drm_device *dev = crtc->dev;
  6990. struct drm_i915_private *dev_priv = dev->dev_private;
  6991. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6992. int pipe = intel_crtc->pipe;
  6993. int x = crtc->cursor_x;
  6994. int y = crtc->cursor_y;
  6995. u32 base = 0, pos = 0;
  6996. if (on)
  6997. base = intel_crtc->cursor_addr;
  6998. if (x >= intel_crtc->config->pipe_src_w)
  6999. base = 0;
  7000. if (y >= intel_crtc->config->pipe_src_h)
  7001. base = 0;
  7002. if (x < 0) {
  7003. if (x + intel_crtc->cursor_width <= 0)
  7004. base = 0;
  7005. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7006. x = -x;
  7007. }
  7008. pos |= x << CURSOR_X_SHIFT;
  7009. if (y < 0) {
  7010. if (y + intel_crtc->cursor_height <= 0)
  7011. base = 0;
  7012. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7013. y = -y;
  7014. }
  7015. pos |= y << CURSOR_Y_SHIFT;
  7016. if (base == 0 && intel_crtc->cursor_base == 0)
  7017. return;
  7018. I915_WRITE(CURPOS(pipe), pos);
  7019. /* ILK+ do this automagically */
  7020. if (HAS_GMCH_DISPLAY(dev) &&
  7021. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  7022. base += (intel_crtc->cursor_height *
  7023. intel_crtc->cursor_width - 1) * 4;
  7024. }
  7025. if (IS_845G(dev) || IS_I865G(dev))
  7026. i845_update_cursor(crtc, base);
  7027. else
  7028. i9xx_update_cursor(crtc, base);
  7029. }
  7030. static bool cursor_size_ok(struct drm_device *dev,
  7031. uint32_t width, uint32_t height)
  7032. {
  7033. if (width == 0 || height == 0)
  7034. return false;
  7035. /*
  7036. * 845g/865g are special in that they are only limited by
  7037. * the width of their cursors, the height is arbitrary up to
  7038. * the precision of the register. Everything else requires
  7039. * square cursors, limited to a few power-of-two sizes.
  7040. */
  7041. if (IS_845G(dev) || IS_I865G(dev)) {
  7042. if ((width & 63) != 0)
  7043. return false;
  7044. if (width > (IS_845G(dev) ? 64 : 512))
  7045. return false;
  7046. if (height > 1023)
  7047. return false;
  7048. } else {
  7049. switch (width | height) {
  7050. case 256:
  7051. case 128:
  7052. if (IS_GEN2(dev))
  7053. return false;
  7054. case 64:
  7055. break;
  7056. default:
  7057. return false;
  7058. }
  7059. }
  7060. return true;
  7061. }
  7062. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7063. u16 *blue, uint32_t start, uint32_t size)
  7064. {
  7065. int end = (start + size > 256) ? 256 : start + size, i;
  7066. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7067. for (i = start; i < end; i++) {
  7068. intel_crtc->lut_r[i] = red[i] >> 8;
  7069. intel_crtc->lut_g[i] = green[i] >> 8;
  7070. intel_crtc->lut_b[i] = blue[i] >> 8;
  7071. }
  7072. intel_crtc_load_lut(crtc);
  7073. }
  7074. /* VESA 640x480x72Hz mode to set on the pipe */
  7075. static struct drm_display_mode load_detect_mode = {
  7076. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7077. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7078. };
  7079. struct drm_framebuffer *
  7080. __intel_framebuffer_create(struct drm_device *dev,
  7081. struct drm_mode_fb_cmd2 *mode_cmd,
  7082. struct drm_i915_gem_object *obj)
  7083. {
  7084. struct intel_framebuffer *intel_fb;
  7085. int ret;
  7086. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7087. if (!intel_fb) {
  7088. drm_gem_object_unreference(&obj->base);
  7089. return ERR_PTR(-ENOMEM);
  7090. }
  7091. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7092. if (ret)
  7093. goto err;
  7094. return &intel_fb->base;
  7095. err:
  7096. drm_gem_object_unreference(&obj->base);
  7097. kfree(intel_fb);
  7098. return ERR_PTR(ret);
  7099. }
  7100. static struct drm_framebuffer *
  7101. intel_framebuffer_create(struct drm_device *dev,
  7102. struct drm_mode_fb_cmd2 *mode_cmd,
  7103. struct drm_i915_gem_object *obj)
  7104. {
  7105. struct drm_framebuffer *fb;
  7106. int ret;
  7107. ret = i915_mutex_lock_interruptible(dev);
  7108. if (ret)
  7109. return ERR_PTR(ret);
  7110. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7111. mutex_unlock(&dev->struct_mutex);
  7112. return fb;
  7113. }
  7114. static u32
  7115. intel_framebuffer_pitch_for_width(int width, int bpp)
  7116. {
  7117. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7118. return ALIGN(pitch, 64);
  7119. }
  7120. static u32
  7121. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7122. {
  7123. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7124. return PAGE_ALIGN(pitch * mode->vdisplay);
  7125. }
  7126. static struct drm_framebuffer *
  7127. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7128. struct drm_display_mode *mode,
  7129. int depth, int bpp)
  7130. {
  7131. struct drm_i915_gem_object *obj;
  7132. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7133. obj = i915_gem_alloc_object(dev,
  7134. intel_framebuffer_size_for_mode(mode, bpp));
  7135. if (obj == NULL)
  7136. return ERR_PTR(-ENOMEM);
  7137. mode_cmd.width = mode->hdisplay;
  7138. mode_cmd.height = mode->vdisplay;
  7139. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7140. bpp);
  7141. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7142. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7143. }
  7144. static struct drm_framebuffer *
  7145. mode_fits_in_fbdev(struct drm_device *dev,
  7146. struct drm_display_mode *mode)
  7147. {
  7148. #ifdef CONFIG_DRM_I915_FBDEV
  7149. struct drm_i915_private *dev_priv = dev->dev_private;
  7150. struct drm_i915_gem_object *obj;
  7151. struct drm_framebuffer *fb;
  7152. if (!dev_priv->fbdev)
  7153. return NULL;
  7154. if (!dev_priv->fbdev->fb)
  7155. return NULL;
  7156. obj = dev_priv->fbdev->fb->obj;
  7157. BUG_ON(!obj);
  7158. fb = &dev_priv->fbdev->fb->base;
  7159. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7160. fb->bits_per_pixel))
  7161. return NULL;
  7162. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7163. return NULL;
  7164. return fb;
  7165. #else
  7166. return NULL;
  7167. #endif
  7168. }
  7169. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7170. struct drm_display_mode *mode,
  7171. struct intel_load_detect_pipe *old,
  7172. struct drm_modeset_acquire_ctx *ctx)
  7173. {
  7174. struct intel_crtc *intel_crtc;
  7175. struct intel_encoder *intel_encoder =
  7176. intel_attached_encoder(connector);
  7177. struct drm_crtc *possible_crtc;
  7178. struct drm_encoder *encoder = &intel_encoder->base;
  7179. struct drm_crtc *crtc = NULL;
  7180. struct drm_device *dev = encoder->dev;
  7181. struct drm_framebuffer *fb;
  7182. struct drm_mode_config *config = &dev->mode_config;
  7183. int ret, i = -1;
  7184. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7185. connector->base.id, connector->name,
  7186. encoder->base.id, encoder->name);
  7187. retry:
  7188. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7189. if (ret)
  7190. goto fail_unlock;
  7191. /*
  7192. * Algorithm gets a little messy:
  7193. *
  7194. * - if the connector already has an assigned crtc, use it (but make
  7195. * sure it's on first)
  7196. *
  7197. * - try to find the first unused crtc that can drive this connector,
  7198. * and use that if we find one
  7199. */
  7200. /* See if we already have a CRTC for this connector */
  7201. if (encoder->crtc) {
  7202. crtc = encoder->crtc;
  7203. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7204. if (ret)
  7205. goto fail_unlock;
  7206. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7207. if (ret)
  7208. goto fail_unlock;
  7209. old->dpms_mode = connector->dpms;
  7210. old->load_detect_temp = false;
  7211. /* Make sure the crtc and connector are running */
  7212. if (connector->dpms != DRM_MODE_DPMS_ON)
  7213. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7214. return true;
  7215. }
  7216. /* Find an unused one (if possible) */
  7217. for_each_crtc(dev, possible_crtc) {
  7218. i++;
  7219. if (!(encoder->possible_crtcs & (1 << i)))
  7220. continue;
  7221. if (possible_crtc->enabled)
  7222. continue;
  7223. /* This can occur when applying the pipe A quirk on resume. */
  7224. if (to_intel_crtc(possible_crtc)->new_enabled)
  7225. continue;
  7226. crtc = possible_crtc;
  7227. break;
  7228. }
  7229. /*
  7230. * If we didn't find an unused CRTC, don't use any.
  7231. */
  7232. if (!crtc) {
  7233. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7234. goto fail_unlock;
  7235. }
  7236. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7237. if (ret)
  7238. goto fail_unlock;
  7239. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7240. if (ret)
  7241. goto fail_unlock;
  7242. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7243. to_intel_connector(connector)->new_encoder = intel_encoder;
  7244. intel_crtc = to_intel_crtc(crtc);
  7245. intel_crtc->new_enabled = true;
  7246. intel_crtc->new_config = intel_crtc->config;
  7247. old->dpms_mode = connector->dpms;
  7248. old->load_detect_temp = true;
  7249. old->release_fb = NULL;
  7250. if (!mode)
  7251. mode = &load_detect_mode;
  7252. /* We need a framebuffer large enough to accommodate all accesses
  7253. * that the plane may generate whilst we perform load detection.
  7254. * We can not rely on the fbcon either being present (we get called
  7255. * during its initialisation to detect all boot displays, or it may
  7256. * not even exist) or that it is large enough to satisfy the
  7257. * requested mode.
  7258. */
  7259. fb = mode_fits_in_fbdev(dev, mode);
  7260. if (fb == NULL) {
  7261. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7262. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7263. old->release_fb = fb;
  7264. } else
  7265. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7266. if (IS_ERR(fb)) {
  7267. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7268. goto fail;
  7269. }
  7270. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7271. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7272. if (old->release_fb)
  7273. old->release_fb->funcs->destroy(old->release_fb);
  7274. goto fail;
  7275. }
  7276. /* let the connector get through one full cycle before testing */
  7277. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7278. return true;
  7279. fail:
  7280. intel_crtc->new_enabled = crtc->enabled;
  7281. if (intel_crtc->new_enabled)
  7282. intel_crtc->new_config = intel_crtc->config;
  7283. else
  7284. intel_crtc->new_config = NULL;
  7285. fail_unlock:
  7286. if (ret == -EDEADLK) {
  7287. drm_modeset_backoff(ctx);
  7288. goto retry;
  7289. }
  7290. return false;
  7291. }
  7292. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7293. struct intel_load_detect_pipe *old)
  7294. {
  7295. struct intel_encoder *intel_encoder =
  7296. intel_attached_encoder(connector);
  7297. struct drm_encoder *encoder = &intel_encoder->base;
  7298. struct drm_crtc *crtc = encoder->crtc;
  7299. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7300. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7301. connector->base.id, connector->name,
  7302. encoder->base.id, encoder->name);
  7303. if (old->load_detect_temp) {
  7304. to_intel_connector(connector)->new_encoder = NULL;
  7305. intel_encoder->new_crtc = NULL;
  7306. intel_crtc->new_enabled = false;
  7307. intel_crtc->new_config = NULL;
  7308. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7309. if (old->release_fb) {
  7310. drm_framebuffer_unregister_private(old->release_fb);
  7311. drm_framebuffer_unreference(old->release_fb);
  7312. }
  7313. return;
  7314. }
  7315. /* Switch crtc and encoder back off if necessary */
  7316. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7317. connector->funcs->dpms(connector, old->dpms_mode);
  7318. }
  7319. static int i9xx_pll_refclk(struct drm_device *dev,
  7320. const struct intel_crtc_state *pipe_config)
  7321. {
  7322. struct drm_i915_private *dev_priv = dev->dev_private;
  7323. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7324. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7325. return dev_priv->vbt.lvds_ssc_freq;
  7326. else if (HAS_PCH_SPLIT(dev))
  7327. return 120000;
  7328. else if (!IS_GEN2(dev))
  7329. return 96000;
  7330. else
  7331. return 48000;
  7332. }
  7333. /* Returns the clock of the currently programmed mode of the given pipe. */
  7334. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7335. struct intel_crtc_state *pipe_config)
  7336. {
  7337. struct drm_device *dev = crtc->base.dev;
  7338. struct drm_i915_private *dev_priv = dev->dev_private;
  7339. int pipe = pipe_config->cpu_transcoder;
  7340. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7341. u32 fp;
  7342. intel_clock_t clock;
  7343. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7344. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7345. fp = pipe_config->dpll_hw_state.fp0;
  7346. else
  7347. fp = pipe_config->dpll_hw_state.fp1;
  7348. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7349. if (IS_PINEVIEW(dev)) {
  7350. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7351. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7352. } else {
  7353. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7354. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7355. }
  7356. if (!IS_GEN2(dev)) {
  7357. if (IS_PINEVIEW(dev))
  7358. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7359. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7360. else
  7361. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7362. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7363. switch (dpll & DPLL_MODE_MASK) {
  7364. case DPLLB_MODE_DAC_SERIAL:
  7365. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7366. 5 : 10;
  7367. break;
  7368. case DPLLB_MODE_LVDS:
  7369. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7370. 7 : 14;
  7371. break;
  7372. default:
  7373. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7374. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7375. return;
  7376. }
  7377. if (IS_PINEVIEW(dev))
  7378. pineview_clock(refclk, &clock);
  7379. else
  7380. i9xx_clock(refclk, &clock);
  7381. } else {
  7382. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7383. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7384. if (is_lvds) {
  7385. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7386. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7387. if (lvds & LVDS_CLKB_POWER_UP)
  7388. clock.p2 = 7;
  7389. else
  7390. clock.p2 = 14;
  7391. } else {
  7392. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7393. clock.p1 = 2;
  7394. else {
  7395. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7396. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7397. }
  7398. if (dpll & PLL_P2_DIVIDE_BY_4)
  7399. clock.p2 = 4;
  7400. else
  7401. clock.p2 = 2;
  7402. }
  7403. i9xx_clock(refclk, &clock);
  7404. }
  7405. /*
  7406. * This value includes pixel_multiplier. We will use
  7407. * port_clock to compute adjusted_mode.crtc_clock in the
  7408. * encoder's get_config() function.
  7409. */
  7410. pipe_config->port_clock = clock.dot;
  7411. }
  7412. int intel_dotclock_calculate(int link_freq,
  7413. const struct intel_link_m_n *m_n)
  7414. {
  7415. /*
  7416. * The calculation for the data clock is:
  7417. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7418. * But we want to avoid losing precison if possible, so:
  7419. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7420. *
  7421. * and the link clock is simpler:
  7422. * link_clock = (m * link_clock) / n
  7423. */
  7424. if (!m_n->link_n)
  7425. return 0;
  7426. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7427. }
  7428. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7429. struct intel_crtc_state *pipe_config)
  7430. {
  7431. struct drm_device *dev = crtc->base.dev;
  7432. /* read out port_clock from the DPLL */
  7433. i9xx_crtc_clock_get(crtc, pipe_config);
  7434. /*
  7435. * This value does not include pixel_multiplier.
  7436. * We will check that port_clock and adjusted_mode.crtc_clock
  7437. * agree once we know their relationship in the encoder's
  7438. * get_config() function.
  7439. */
  7440. pipe_config->base.adjusted_mode.crtc_clock =
  7441. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7442. &pipe_config->fdi_m_n);
  7443. }
  7444. /** Returns the currently programmed mode of the given pipe. */
  7445. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7446. struct drm_crtc *crtc)
  7447. {
  7448. struct drm_i915_private *dev_priv = dev->dev_private;
  7449. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7450. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7451. struct drm_display_mode *mode;
  7452. struct intel_crtc_state pipe_config;
  7453. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7454. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7455. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7456. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7457. enum pipe pipe = intel_crtc->pipe;
  7458. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7459. if (!mode)
  7460. return NULL;
  7461. /*
  7462. * Construct a pipe_config sufficient for getting the clock info
  7463. * back out of crtc_clock_get.
  7464. *
  7465. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7466. * to use a real value here instead.
  7467. */
  7468. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7469. pipe_config.pixel_multiplier = 1;
  7470. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7471. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7472. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7473. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7474. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7475. mode->hdisplay = (htot & 0xffff) + 1;
  7476. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7477. mode->hsync_start = (hsync & 0xffff) + 1;
  7478. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7479. mode->vdisplay = (vtot & 0xffff) + 1;
  7480. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7481. mode->vsync_start = (vsync & 0xffff) + 1;
  7482. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7483. drm_mode_set_name(mode);
  7484. return mode;
  7485. }
  7486. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7487. {
  7488. struct drm_device *dev = crtc->dev;
  7489. struct drm_i915_private *dev_priv = dev->dev_private;
  7490. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7491. if (!HAS_GMCH_DISPLAY(dev))
  7492. return;
  7493. if (!dev_priv->lvds_downclock_avail)
  7494. return;
  7495. /*
  7496. * Since this is called by a timer, we should never get here in
  7497. * the manual case.
  7498. */
  7499. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7500. int pipe = intel_crtc->pipe;
  7501. int dpll_reg = DPLL(pipe);
  7502. int dpll;
  7503. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7504. assert_panel_unlocked(dev_priv, pipe);
  7505. dpll = I915_READ(dpll_reg);
  7506. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7507. I915_WRITE(dpll_reg, dpll);
  7508. intel_wait_for_vblank(dev, pipe);
  7509. dpll = I915_READ(dpll_reg);
  7510. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7511. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7512. }
  7513. }
  7514. void intel_mark_busy(struct drm_device *dev)
  7515. {
  7516. struct drm_i915_private *dev_priv = dev->dev_private;
  7517. if (dev_priv->mm.busy)
  7518. return;
  7519. intel_runtime_pm_get(dev_priv);
  7520. i915_update_gfx_val(dev_priv);
  7521. dev_priv->mm.busy = true;
  7522. }
  7523. void intel_mark_idle(struct drm_device *dev)
  7524. {
  7525. struct drm_i915_private *dev_priv = dev->dev_private;
  7526. struct drm_crtc *crtc;
  7527. if (!dev_priv->mm.busy)
  7528. return;
  7529. dev_priv->mm.busy = false;
  7530. if (!i915.powersave)
  7531. goto out;
  7532. for_each_crtc(dev, crtc) {
  7533. if (!crtc->primary->fb)
  7534. continue;
  7535. intel_decrease_pllclock(crtc);
  7536. }
  7537. if (INTEL_INFO(dev)->gen >= 6)
  7538. gen6_rps_idle(dev->dev_private);
  7539. out:
  7540. intel_runtime_pm_put(dev_priv);
  7541. }
  7542. static void intel_crtc_set_state(struct intel_crtc *crtc,
  7543. struct intel_crtc_state *crtc_state)
  7544. {
  7545. kfree(crtc->config);
  7546. crtc->config = crtc_state;
  7547. crtc->base.state = &crtc_state->base;
  7548. }
  7549. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7550. {
  7551. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7552. struct drm_device *dev = crtc->dev;
  7553. struct intel_unpin_work *work;
  7554. spin_lock_irq(&dev->event_lock);
  7555. work = intel_crtc->unpin_work;
  7556. intel_crtc->unpin_work = NULL;
  7557. spin_unlock_irq(&dev->event_lock);
  7558. if (work) {
  7559. cancel_work_sync(&work->work);
  7560. kfree(work);
  7561. }
  7562. intel_crtc_set_state(intel_crtc, NULL);
  7563. drm_crtc_cleanup(crtc);
  7564. kfree(intel_crtc);
  7565. }
  7566. static void intel_unpin_work_fn(struct work_struct *__work)
  7567. {
  7568. struct intel_unpin_work *work =
  7569. container_of(__work, struct intel_unpin_work, work);
  7570. struct drm_device *dev = work->crtc->dev;
  7571. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7572. mutex_lock(&dev->struct_mutex);
  7573. intel_unpin_fb_obj(work->old_fb_obj);
  7574. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7575. drm_gem_object_unreference(&work->old_fb_obj->base);
  7576. intel_fbc_update(dev);
  7577. if (work->flip_queued_req)
  7578. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7579. mutex_unlock(&dev->struct_mutex);
  7580. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7581. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7582. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7583. kfree(work);
  7584. }
  7585. static void do_intel_finish_page_flip(struct drm_device *dev,
  7586. struct drm_crtc *crtc)
  7587. {
  7588. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7589. struct intel_unpin_work *work;
  7590. unsigned long flags;
  7591. /* Ignore early vblank irqs */
  7592. if (intel_crtc == NULL)
  7593. return;
  7594. /*
  7595. * This is called both by irq handlers and the reset code (to complete
  7596. * lost pageflips) so needs the full irqsave spinlocks.
  7597. */
  7598. spin_lock_irqsave(&dev->event_lock, flags);
  7599. work = intel_crtc->unpin_work;
  7600. /* Ensure we don't miss a work->pending update ... */
  7601. smp_rmb();
  7602. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7603. spin_unlock_irqrestore(&dev->event_lock, flags);
  7604. return;
  7605. }
  7606. page_flip_completed(intel_crtc);
  7607. spin_unlock_irqrestore(&dev->event_lock, flags);
  7608. }
  7609. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7610. {
  7611. struct drm_i915_private *dev_priv = dev->dev_private;
  7612. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7613. do_intel_finish_page_flip(dev, crtc);
  7614. }
  7615. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7616. {
  7617. struct drm_i915_private *dev_priv = dev->dev_private;
  7618. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7619. do_intel_finish_page_flip(dev, crtc);
  7620. }
  7621. /* Is 'a' after or equal to 'b'? */
  7622. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7623. {
  7624. return !((a - b) & 0x80000000);
  7625. }
  7626. static bool page_flip_finished(struct intel_crtc *crtc)
  7627. {
  7628. struct drm_device *dev = crtc->base.dev;
  7629. struct drm_i915_private *dev_priv = dev->dev_private;
  7630. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7631. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7632. return true;
  7633. /*
  7634. * The relevant registers doen't exist on pre-ctg.
  7635. * As the flip done interrupt doesn't trigger for mmio
  7636. * flips on gmch platforms, a flip count check isn't
  7637. * really needed there. But since ctg has the registers,
  7638. * include it in the check anyway.
  7639. */
  7640. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7641. return true;
  7642. /*
  7643. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7644. * used the same base address. In that case the mmio flip might
  7645. * have completed, but the CS hasn't even executed the flip yet.
  7646. *
  7647. * A flip count check isn't enough as the CS might have updated
  7648. * the base address just after start of vblank, but before we
  7649. * managed to process the interrupt. This means we'd complete the
  7650. * CS flip too soon.
  7651. *
  7652. * Combining both checks should get us a good enough result. It may
  7653. * still happen that the CS flip has been executed, but has not
  7654. * yet actually completed. But in case the base address is the same
  7655. * anyway, we don't really care.
  7656. */
  7657. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7658. crtc->unpin_work->gtt_offset &&
  7659. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7660. crtc->unpin_work->flip_count);
  7661. }
  7662. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7663. {
  7664. struct drm_i915_private *dev_priv = dev->dev_private;
  7665. struct intel_crtc *intel_crtc =
  7666. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7667. unsigned long flags;
  7668. /*
  7669. * This is called both by irq handlers and the reset code (to complete
  7670. * lost pageflips) so needs the full irqsave spinlocks.
  7671. *
  7672. * NB: An MMIO update of the plane base pointer will also
  7673. * generate a page-flip completion irq, i.e. every modeset
  7674. * is also accompanied by a spurious intel_prepare_page_flip().
  7675. */
  7676. spin_lock_irqsave(&dev->event_lock, flags);
  7677. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7678. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7679. spin_unlock_irqrestore(&dev->event_lock, flags);
  7680. }
  7681. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7682. {
  7683. /* Ensure that the work item is consistent when activating it ... */
  7684. smp_wmb();
  7685. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7686. /* and that it is marked active as soon as the irq could fire. */
  7687. smp_wmb();
  7688. }
  7689. static int intel_gen2_queue_flip(struct drm_device *dev,
  7690. struct drm_crtc *crtc,
  7691. struct drm_framebuffer *fb,
  7692. struct drm_i915_gem_object *obj,
  7693. struct intel_engine_cs *ring,
  7694. uint32_t flags)
  7695. {
  7696. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7697. u32 flip_mask;
  7698. int ret;
  7699. ret = intel_ring_begin(ring, 6);
  7700. if (ret)
  7701. return ret;
  7702. /* Can't queue multiple flips, so wait for the previous
  7703. * one to finish before executing the next.
  7704. */
  7705. if (intel_crtc->plane)
  7706. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7707. else
  7708. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7709. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7710. intel_ring_emit(ring, MI_NOOP);
  7711. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7712. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7713. intel_ring_emit(ring, fb->pitches[0]);
  7714. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7715. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7716. intel_mark_page_flip_active(intel_crtc);
  7717. __intel_ring_advance(ring);
  7718. return 0;
  7719. }
  7720. static int intel_gen3_queue_flip(struct drm_device *dev,
  7721. struct drm_crtc *crtc,
  7722. struct drm_framebuffer *fb,
  7723. struct drm_i915_gem_object *obj,
  7724. struct intel_engine_cs *ring,
  7725. uint32_t flags)
  7726. {
  7727. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7728. u32 flip_mask;
  7729. int ret;
  7730. ret = intel_ring_begin(ring, 6);
  7731. if (ret)
  7732. return ret;
  7733. if (intel_crtc->plane)
  7734. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7735. else
  7736. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7737. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7738. intel_ring_emit(ring, MI_NOOP);
  7739. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7740. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7741. intel_ring_emit(ring, fb->pitches[0]);
  7742. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7743. intel_ring_emit(ring, MI_NOOP);
  7744. intel_mark_page_flip_active(intel_crtc);
  7745. __intel_ring_advance(ring);
  7746. return 0;
  7747. }
  7748. static int intel_gen4_queue_flip(struct drm_device *dev,
  7749. struct drm_crtc *crtc,
  7750. struct drm_framebuffer *fb,
  7751. struct drm_i915_gem_object *obj,
  7752. struct intel_engine_cs *ring,
  7753. uint32_t flags)
  7754. {
  7755. struct drm_i915_private *dev_priv = dev->dev_private;
  7756. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7757. uint32_t pf, pipesrc;
  7758. int ret;
  7759. ret = intel_ring_begin(ring, 4);
  7760. if (ret)
  7761. return ret;
  7762. /* i965+ uses the linear or tiled offsets from the
  7763. * Display Registers (which do not change across a page-flip)
  7764. * so we need only reprogram the base address.
  7765. */
  7766. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7767. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7768. intel_ring_emit(ring, fb->pitches[0]);
  7769. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7770. obj->tiling_mode);
  7771. /* XXX Enabling the panel-fitter across page-flip is so far
  7772. * untested on non-native modes, so ignore it for now.
  7773. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7774. */
  7775. pf = 0;
  7776. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7777. intel_ring_emit(ring, pf | pipesrc);
  7778. intel_mark_page_flip_active(intel_crtc);
  7779. __intel_ring_advance(ring);
  7780. return 0;
  7781. }
  7782. static int intel_gen6_queue_flip(struct drm_device *dev,
  7783. struct drm_crtc *crtc,
  7784. struct drm_framebuffer *fb,
  7785. struct drm_i915_gem_object *obj,
  7786. struct intel_engine_cs *ring,
  7787. uint32_t flags)
  7788. {
  7789. struct drm_i915_private *dev_priv = dev->dev_private;
  7790. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7791. uint32_t pf, pipesrc;
  7792. int ret;
  7793. ret = intel_ring_begin(ring, 4);
  7794. if (ret)
  7795. return ret;
  7796. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7797. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7798. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7799. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7800. /* Contrary to the suggestions in the documentation,
  7801. * "Enable Panel Fitter" does not seem to be required when page
  7802. * flipping with a non-native mode, and worse causes a normal
  7803. * modeset to fail.
  7804. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7805. */
  7806. pf = 0;
  7807. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7808. intel_ring_emit(ring, pf | pipesrc);
  7809. intel_mark_page_flip_active(intel_crtc);
  7810. __intel_ring_advance(ring);
  7811. return 0;
  7812. }
  7813. static int intel_gen7_queue_flip(struct drm_device *dev,
  7814. struct drm_crtc *crtc,
  7815. struct drm_framebuffer *fb,
  7816. struct drm_i915_gem_object *obj,
  7817. struct intel_engine_cs *ring,
  7818. uint32_t flags)
  7819. {
  7820. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7821. uint32_t plane_bit = 0;
  7822. int len, ret;
  7823. switch (intel_crtc->plane) {
  7824. case PLANE_A:
  7825. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7826. break;
  7827. case PLANE_B:
  7828. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7829. break;
  7830. case PLANE_C:
  7831. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7832. break;
  7833. default:
  7834. WARN_ONCE(1, "unknown plane in flip command\n");
  7835. return -ENODEV;
  7836. }
  7837. len = 4;
  7838. if (ring->id == RCS) {
  7839. len += 6;
  7840. /*
  7841. * On Gen 8, SRM is now taking an extra dword to accommodate
  7842. * 48bits addresses, and we need a NOOP for the batch size to
  7843. * stay even.
  7844. */
  7845. if (IS_GEN8(dev))
  7846. len += 2;
  7847. }
  7848. /*
  7849. * BSpec MI_DISPLAY_FLIP for IVB:
  7850. * "The full packet must be contained within the same cache line."
  7851. *
  7852. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7853. * cacheline, if we ever start emitting more commands before
  7854. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7855. * then do the cacheline alignment, and finally emit the
  7856. * MI_DISPLAY_FLIP.
  7857. */
  7858. ret = intel_ring_cacheline_align(ring);
  7859. if (ret)
  7860. return ret;
  7861. ret = intel_ring_begin(ring, len);
  7862. if (ret)
  7863. return ret;
  7864. /* Unmask the flip-done completion message. Note that the bspec says that
  7865. * we should do this for both the BCS and RCS, and that we must not unmask
  7866. * more than one flip event at any time (or ensure that one flip message
  7867. * can be sent by waiting for flip-done prior to queueing new flips).
  7868. * Experimentation says that BCS works despite DERRMR masking all
  7869. * flip-done completion events and that unmasking all planes at once
  7870. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7871. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7872. */
  7873. if (ring->id == RCS) {
  7874. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7875. intel_ring_emit(ring, DERRMR);
  7876. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7877. DERRMR_PIPEB_PRI_FLIP_DONE |
  7878. DERRMR_PIPEC_PRI_FLIP_DONE));
  7879. if (IS_GEN8(dev))
  7880. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7881. MI_SRM_LRM_GLOBAL_GTT);
  7882. else
  7883. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7884. MI_SRM_LRM_GLOBAL_GTT);
  7885. intel_ring_emit(ring, DERRMR);
  7886. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7887. if (IS_GEN8(dev)) {
  7888. intel_ring_emit(ring, 0);
  7889. intel_ring_emit(ring, MI_NOOP);
  7890. }
  7891. }
  7892. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7893. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7894. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7895. intel_ring_emit(ring, (MI_NOOP));
  7896. intel_mark_page_flip_active(intel_crtc);
  7897. __intel_ring_advance(ring);
  7898. return 0;
  7899. }
  7900. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7901. struct drm_i915_gem_object *obj)
  7902. {
  7903. /*
  7904. * This is not being used for older platforms, because
  7905. * non-availability of flip done interrupt forces us to use
  7906. * CS flips. Older platforms derive flip done using some clever
  7907. * tricks involving the flip_pending status bits and vblank irqs.
  7908. * So using MMIO flips there would disrupt this mechanism.
  7909. */
  7910. if (ring == NULL)
  7911. return true;
  7912. if (INTEL_INFO(ring->dev)->gen < 5)
  7913. return false;
  7914. if (i915.use_mmio_flip < 0)
  7915. return false;
  7916. else if (i915.use_mmio_flip > 0)
  7917. return true;
  7918. else if (i915.enable_execlists)
  7919. return true;
  7920. else
  7921. return ring != i915_gem_request_get_ring(obj->last_read_req);
  7922. }
  7923. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  7924. {
  7925. struct drm_device *dev = intel_crtc->base.dev;
  7926. struct drm_i915_private *dev_priv = dev->dev_private;
  7927. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  7928. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7929. struct drm_i915_gem_object *obj = intel_fb->obj;
  7930. const enum pipe pipe = intel_crtc->pipe;
  7931. u32 ctl, stride;
  7932. ctl = I915_READ(PLANE_CTL(pipe, 0));
  7933. ctl &= ~PLANE_CTL_TILED_MASK;
  7934. if (obj->tiling_mode == I915_TILING_X)
  7935. ctl |= PLANE_CTL_TILED_X;
  7936. /*
  7937. * The stride is either expressed as a multiple of 64 bytes chunks for
  7938. * linear buffers or in number of tiles for tiled buffers.
  7939. */
  7940. stride = fb->pitches[0] >> 6;
  7941. if (obj->tiling_mode == I915_TILING_X)
  7942. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  7943. /*
  7944. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  7945. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  7946. */
  7947. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  7948. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  7949. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  7950. POSTING_READ(PLANE_SURF(pipe, 0));
  7951. }
  7952. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  7953. {
  7954. struct drm_device *dev = intel_crtc->base.dev;
  7955. struct drm_i915_private *dev_priv = dev->dev_private;
  7956. struct intel_framebuffer *intel_fb =
  7957. to_intel_framebuffer(intel_crtc->base.primary->fb);
  7958. struct drm_i915_gem_object *obj = intel_fb->obj;
  7959. u32 dspcntr;
  7960. u32 reg;
  7961. reg = DSPCNTR(intel_crtc->plane);
  7962. dspcntr = I915_READ(reg);
  7963. if (obj->tiling_mode != I915_TILING_NONE)
  7964. dspcntr |= DISPPLANE_TILED;
  7965. else
  7966. dspcntr &= ~DISPPLANE_TILED;
  7967. I915_WRITE(reg, dspcntr);
  7968. I915_WRITE(DSPSURF(intel_crtc->plane),
  7969. intel_crtc->unpin_work->gtt_offset);
  7970. POSTING_READ(DSPSURF(intel_crtc->plane));
  7971. }
  7972. /*
  7973. * XXX: This is the temporary way to update the plane registers until we get
  7974. * around to using the usual plane update functions for MMIO flips
  7975. */
  7976. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  7977. {
  7978. struct drm_device *dev = intel_crtc->base.dev;
  7979. bool atomic_update;
  7980. u32 start_vbl_count;
  7981. intel_mark_page_flip_active(intel_crtc);
  7982. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  7983. if (INTEL_INFO(dev)->gen >= 9)
  7984. skl_do_mmio_flip(intel_crtc);
  7985. else
  7986. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  7987. ilk_do_mmio_flip(intel_crtc);
  7988. if (atomic_update)
  7989. intel_pipe_update_end(intel_crtc, start_vbl_count);
  7990. }
  7991. static void intel_mmio_flip_work_func(struct work_struct *work)
  7992. {
  7993. struct intel_crtc *crtc =
  7994. container_of(work, struct intel_crtc, mmio_flip.work);
  7995. struct intel_mmio_flip *mmio_flip;
  7996. mmio_flip = &crtc->mmio_flip;
  7997. if (mmio_flip->req)
  7998. WARN_ON(__i915_wait_request(mmio_flip->req,
  7999. crtc->reset_counter,
  8000. false, NULL, NULL) != 0);
  8001. intel_do_mmio_flip(crtc);
  8002. if (mmio_flip->req) {
  8003. mutex_lock(&crtc->base.dev->struct_mutex);
  8004. i915_gem_request_assign(&mmio_flip->req, NULL);
  8005. mutex_unlock(&crtc->base.dev->struct_mutex);
  8006. }
  8007. }
  8008. static int intel_queue_mmio_flip(struct drm_device *dev,
  8009. struct drm_crtc *crtc,
  8010. struct drm_framebuffer *fb,
  8011. struct drm_i915_gem_object *obj,
  8012. struct intel_engine_cs *ring,
  8013. uint32_t flags)
  8014. {
  8015. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8016. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  8017. obj->last_write_req);
  8018. schedule_work(&intel_crtc->mmio_flip.work);
  8019. return 0;
  8020. }
  8021. static int intel_gen9_queue_flip(struct drm_device *dev,
  8022. struct drm_crtc *crtc,
  8023. struct drm_framebuffer *fb,
  8024. struct drm_i915_gem_object *obj,
  8025. struct intel_engine_cs *ring,
  8026. uint32_t flags)
  8027. {
  8028. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8029. uint32_t plane = 0, stride;
  8030. int ret;
  8031. switch(intel_crtc->pipe) {
  8032. case PIPE_A:
  8033. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
  8034. break;
  8035. case PIPE_B:
  8036. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
  8037. break;
  8038. case PIPE_C:
  8039. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
  8040. break;
  8041. default:
  8042. WARN_ONCE(1, "unknown plane in flip command\n");
  8043. return -ENODEV;
  8044. }
  8045. switch (obj->tiling_mode) {
  8046. case I915_TILING_NONE:
  8047. stride = fb->pitches[0] >> 6;
  8048. break;
  8049. case I915_TILING_X:
  8050. stride = fb->pitches[0] >> 9;
  8051. break;
  8052. default:
  8053. WARN_ONCE(1, "unknown tiling in flip command\n");
  8054. return -ENODEV;
  8055. }
  8056. ret = intel_ring_begin(ring, 10);
  8057. if (ret)
  8058. return ret;
  8059. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8060. intel_ring_emit(ring, DERRMR);
  8061. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8062. DERRMR_PIPEB_PRI_FLIP_DONE |
  8063. DERRMR_PIPEC_PRI_FLIP_DONE));
  8064. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8065. MI_SRM_LRM_GLOBAL_GTT);
  8066. intel_ring_emit(ring, DERRMR);
  8067. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8068. intel_ring_emit(ring, 0);
  8069. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
  8070. intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
  8071. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8072. intel_mark_page_flip_active(intel_crtc);
  8073. __intel_ring_advance(ring);
  8074. return 0;
  8075. }
  8076. static int intel_default_queue_flip(struct drm_device *dev,
  8077. struct drm_crtc *crtc,
  8078. struct drm_framebuffer *fb,
  8079. struct drm_i915_gem_object *obj,
  8080. struct intel_engine_cs *ring,
  8081. uint32_t flags)
  8082. {
  8083. return -ENODEV;
  8084. }
  8085. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8086. struct drm_crtc *crtc)
  8087. {
  8088. struct drm_i915_private *dev_priv = dev->dev_private;
  8089. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8090. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8091. u32 addr;
  8092. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8093. return true;
  8094. if (!work->enable_stall_check)
  8095. return false;
  8096. if (work->flip_ready_vblank == 0) {
  8097. if (work->flip_queued_req &&
  8098. !i915_gem_request_completed(work->flip_queued_req, true))
  8099. return false;
  8100. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8101. }
  8102. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  8103. return false;
  8104. /* Potential stall - if we see that the flip has happened,
  8105. * assume a missed interrupt. */
  8106. if (INTEL_INFO(dev)->gen >= 4)
  8107. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8108. else
  8109. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8110. /* There is a potential issue here with a false positive after a flip
  8111. * to the same address. We could address this by checking for a
  8112. * non-incrementing frame counter.
  8113. */
  8114. return addr == work->gtt_offset;
  8115. }
  8116. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8117. {
  8118. struct drm_i915_private *dev_priv = dev->dev_private;
  8119. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8120. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8121. WARN_ON(!in_irq());
  8122. if (crtc == NULL)
  8123. return;
  8124. spin_lock(&dev->event_lock);
  8125. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8126. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8127. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  8128. page_flip_completed(intel_crtc);
  8129. }
  8130. spin_unlock(&dev->event_lock);
  8131. }
  8132. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8133. struct drm_framebuffer *fb,
  8134. struct drm_pending_vblank_event *event,
  8135. uint32_t page_flip_flags)
  8136. {
  8137. struct drm_device *dev = crtc->dev;
  8138. struct drm_i915_private *dev_priv = dev->dev_private;
  8139. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8140. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8141. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8142. struct drm_plane *primary = crtc->primary;
  8143. enum pipe pipe = intel_crtc->pipe;
  8144. struct intel_unpin_work *work;
  8145. struct intel_engine_cs *ring;
  8146. int ret;
  8147. /*
  8148. * drm_mode_page_flip_ioctl() should already catch this, but double
  8149. * check to be safe. In the future we may enable pageflipping from
  8150. * a disabled primary plane.
  8151. */
  8152. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8153. return -EBUSY;
  8154. /* Can't change pixel format via MI display flips. */
  8155. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8156. return -EINVAL;
  8157. /*
  8158. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8159. * Note that pitch changes could also affect these register.
  8160. */
  8161. if (INTEL_INFO(dev)->gen > 3 &&
  8162. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8163. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8164. return -EINVAL;
  8165. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8166. goto out_hang;
  8167. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8168. if (work == NULL)
  8169. return -ENOMEM;
  8170. work->event = event;
  8171. work->crtc = crtc;
  8172. work->old_fb_obj = intel_fb_obj(old_fb);
  8173. INIT_WORK(&work->work, intel_unpin_work_fn);
  8174. ret = drm_crtc_vblank_get(crtc);
  8175. if (ret)
  8176. goto free_work;
  8177. /* We borrow the event spin lock for protecting unpin_work */
  8178. spin_lock_irq(&dev->event_lock);
  8179. if (intel_crtc->unpin_work) {
  8180. /* Before declaring the flip queue wedged, check if
  8181. * the hardware completed the operation behind our backs.
  8182. */
  8183. if (__intel_pageflip_stall_check(dev, crtc)) {
  8184. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8185. page_flip_completed(intel_crtc);
  8186. } else {
  8187. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8188. spin_unlock_irq(&dev->event_lock);
  8189. drm_crtc_vblank_put(crtc);
  8190. kfree(work);
  8191. return -EBUSY;
  8192. }
  8193. }
  8194. intel_crtc->unpin_work = work;
  8195. spin_unlock_irq(&dev->event_lock);
  8196. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8197. flush_workqueue(dev_priv->wq);
  8198. ret = i915_mutex_lock_interruptible(dev);
  8199. if (ret)
  8200. goto cleanup;
  8201. /* Reference the objects for the scheduled work. */
  8202. drm_gem_object_reference(&work->old_fb_obj->base);
  8203. drm_gem_object_reference(&obj->base);
  8204. crtc->primary->fb = fb;
  8205. work->pending_flip_obj = obj;
  8206. atomic_inc(&intel_crtc->unpin_work_count);
  8207. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8208. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8209. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8210. if (IS_VALLEYVIEW(dev)) {
  8211. ring = &dev_priv->ring[BCS];
  8212. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8213. /* vlv: DISPLAY_FLIP fails to change tiling */
  8214. ring = NULL;
  8215. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  8216. ring = &dev_priv->ring[BCS];
  8217. } else if (INTEL_INFO(dev)->gen >= 7) {
  8218. ring = i915_gem_request_get_ring(obj->last_read_req);
  8219. if (ring == NULL || ring->id != RCS)
  8220. ring = &dev_priv->ring[BCS];
  8221. } else {
  8222. ring = &dev_priv->ring[RCS];
  8223. }
  8224. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
  8225. if (ret)
  8226. goto cleanup_pending;
  8227. work->gtt_offset =
  8228. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8229. if (use_mmio_flip(ring, obj)) {
  8230. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8231. page_flip_flags);
  8232. if (ret)
  8233. goto cleanup_unpin;
  8234. i915_gem_request_assign(&work->flip_queued_req,
  8235. obj->last_write_req);
  8236. } else {
  8237. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8238. page_flip_flags);
  8239. if (ret)
  8240. goto cleanup_unpin;
  8241. i915_gem_request_assign(&work->flip_queued_req,
  8242. intel_ring_get_request(ring));
  8243. }
  8244. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8245. work->enable_stall_check = true;
  8246. i915_gem_track_fb(work->old_fb_obj, obj,
  8247. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8248. intel_fbc_disable(dev);
  8249. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8250. mutex_unlock(&dev->struct_mutex);
  8251. trace_i915_flip_request(intel_crtc->plane, obj);
  8252. return 0;
  8253. cleanup_unpin:
  8254. intel_unpin_fb_obj(obj);
  8255. cleanup_pending:
  8256. atomic_dec(&intel_crtc->unpin_work_count);
  8257. crtc->primary->fb = old_fb;
  8258. drm_gem_object_unreference(&work->old_fb_obj->base);
  8259. drm_gem_object_unreference(&obj->base);
  8260. mutex_unlock(&dev->struct_mutex);
  8261. cleanup:
  8262. spin_lock_irq(&dev->event_lock);
  8263. intel_crtc->unpin_work = NULL;
  8264. spin_unlock_irq(&dev->event_lock);
  8265. drm_crtc_vblank_put(crtc);
  8266. free_work:
  8267. kfree(work);
  8268. if (ret == -EIO) {
  8269. out_hang:
  8270. ret = intel_plane_restore(primary);
  8271. if (ret == 0 && event) {
  8272. spin_lock_irq(&dev->event_lock);
  8273. drm_send_vblank_event(dev, pipe, event);
  8274. spin_unlock_irq(&dev->event_lock);
  8275. }
  8276. }
  8277. return ret;
  8278. }
  8279. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8280. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8281. .load_lut = intel_crtc_load_lut,
  8282. .atomic_begin = intel_begin_crtc_commit,
  8283. .atomic_flush = intel_finish_crtc_commit,
  8284. };
  8285. /**
  8286. * intel_modeset_update_staged_output_state
  8287. *
  8288. * Updates the staged output configuration state, e.g. after we've read out the
  8289. * current hw state.
  8290. */
  8291. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8292. {
  8293. struct intel_crtc *crtc;
  8294. struct intel_encoder *encoder;
  8295. struct intel_connector *connector;
  8296. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8297. base.head) {
  8298. connector->new_encoder =
  8299. to_intel_encoder(connector->base.encoder);
  8300. }
  8301. for_each_intel_encoder(dev, encoder) {
  8302. encoder->new_crtc =
  8303. to_intel_crtc(encoder->base.crtc);
  8304. }
  8305. for_each_intel_crtc(dev, crtc) {
  8306. crtc->new_enabled = crtc->base.enabled;
  8307. if (crtc->new_enabled)
  8308. crtc->new_config = crtc->config;
  8309. else
  8310. crtc->new_config = NULL;
  8311. }
  8312. }
  8313. /**
  8314. * intel_modeset_commit_output_state
  8315. *
  8316. * This function copies the stage display pipe configuration to the real one.
  8317. */
  8318. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8319. {
  8320. struct intel_crtc *crtc;
  8321. struct intel_encoder *encoder;
  8322. struct intel_connector *connector;
  8323. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8324. base.head) {
  8325. connector->base.encoder = &connector->new_encoder->base;
  8326. }
  8327. for_each_intel_encoder(dev, encoder) {
  8328. encoder->base.crtc = &encoder->new_crtc->base;
  8329. }
  8330. for_each_intel_crtc(dev, crtc) {
  8331. crtc->base.enabled = crtc->new_enabled;
  8332. }
  8333. }
  8334. static void
  8335. connected_sink_compute_bpp(struct intel_connector *connector,
  8336. struct intel_crtc_state *pipe_config)
  8337. {
  8338. int bpp = pipe_config->pipe_bpp;
  8339. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8340. connector->base.base.id,
  8341. connector->base.name);
  8342. /* Don't use an invalid EDID bpc value */
  8343. if (connector->base.display_info.bpc &&
  8344. connector->base.display_info.bpc * 3 < bpp) {
  8345. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8346. bpp, connector->base.display_info.bpc*3);
  8347. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8348. }
  8349. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8350. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8351. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8352. bpp);
  8353. pipe_config->pipe_bpp = 24;
  8354. }
  8355. }
  8356. static int
  8357. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8358. struct drm_framebuffer *fb,
  8359. struct intel_crtc_state *pipe_config)
  8360. {
  8361. struct drm_device *dev = crtc->base.dev;
  8362. struct intel_connector *connector;
  8363. int bpp;
  8364. switch (fb->pixel_format) {
  8365. case DRM_FORMAT_C8:
  8366. bpp = 8*3; /* since we go through a colormap */
  8367. break;
  8368. case DRM_FORMAT_XRGB1555:
  8369. case DRM_FORMAT_ARGB1555:
  8370. /* checked in intel_framebuffer_init already */
  8371. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8372. return -EINVAL;
  8373. case DRM_FORMAT_RGB565:
  8374. bpp = 6*3; /* min is 18bpp */
  8375. break;
  8376. case DRM_FORMAT_XBGR8888:
  8377. case DRM_FORMAT_ABGR8888:
  8378. /* checked in intel_framebuffer_init already */
  8379. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8380. return -EINVAL;
  8381. case DRM_FORMAT_XRGB8888:
  8382. case DRM_FORMAT_ARGB8888:
  8383. bpp = 8*3;
  8384. break;
  8385. case DRM_FORMAT_XRGB2101010:
  8386. case DRM_FORMAT_ARGB2101010:
  8387. case DRM_FORMAT_XBGR2101010:
  8388. case DRM_FORMAT_ABGR2101010:
  8389. /* checked in intel_framebuffer_init already */
  8390. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8391. return -EINVAL;
  8392. bpp = 10*3;
  8393. break;
  8394. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8395. default:
  8396. DRM_DEBUG_KMS("unsupported depth\n");
  8397. return -EINVAL;
  8398. }
  8399. pipe_config->pipe_bpp = bpp;
  8400. /* Clamp display bpp to EDID value */
  8401. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8402. base.head) {
  8403. if (!connector->new_encoder ||
  8404. connector->new_encoder->new_crtc != crtc)
  8405. continue;
  8406. connected_sink_compute_bpp(connector, pipe_config);
  8407. }
  8408. return bpp;
  8409. }
  8410. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8411. {
  8412. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8413. "type: 0x%x flags: 0x%x\n",
  8414. mode->crtc_clock,
  8415. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8416. mode->crtc_hsync_end, mode->crtc_htotal,
  8417. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8418. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8419. }
  8420. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8421. struct intel_crtc_state *pipe_config,
  8422. const char *context)
  8423. {
  8424. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8425. context, pipe_name(crtc->pipe));
  8426. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8427. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8428. pipe_config->pipe_bpp, pipe_config->dither);
  8429. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8430. pipe_config->has_pch_encoder,
  8431. pipe_config->fdi_lanes,
  8432. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8433. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8434. pipe_config->fdi_m_n.tu);
  8435. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8436. pipe_config->has_dp_encoder,
  8437. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8438. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8439. pipe_config->dp_m_n.tu);
  8440. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8441. pipe_config->has_dp_encoder,
  8442. pipe_config->dp_m2_n2.gmch_m,
  8443. pipe_config->dp_m2_n2.gmch_n,
  8444. pipe_config->dp_m2_n2.link_m,
  8445. pipe_config->dp_m2_n2.link_n,
  8446. pipe_config->dp_m2_n2.tu);
  8447. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8448. pipe_config->has_audio,
  8449. pipe_config->has_infoframe);
  8450. DRM_DEBUG_KMS("requested mode:\n");
  8451. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8452. DRM_DEBUG_KMS("adjusted mode:\n");
  8453. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8454. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8455. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8456. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8457. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8458. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8459. pipe_config->gmch_pfit.control,
  8460. pipe_config->gmch_pfit.pgm_ratios,
  8461. pipe_config->gmch_pfit.lvds_border_bits);
  8462. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8463. pipe_config->pch_pfit.pos,
  8464. pipe_config->pch_pfit.size,
  8465. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8466. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8467. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8468. }
  8469. static bool encoders_cloneable(const struct intel_encoder *a,
  8470. const struct intel_encoder *b)
  8471. {
  8472. /* masks could be asymmetric, so check both ways */
  8473. return a == b || (a->cloneable & (1 << b->type) &&
  8474. b->cloneable & (1 << a->type));
  8475. }
  8476. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8477. struct intel_encoder *encoder)
  8478. {
  8479. struct drm_device *dev = crtc->base.dev;
  8480. struct intel_encoder *source_encoder;
  8481. for_each_intel_encoder(dev, source_encoder) {
  8482. if (source_encoder->new_crtc != crtc)
  8483. continue;
  8484. if (!encoders_cloneable(encoder, source_encoder))
  8485. return false;
  8486. }
  8487. return true;
  8488. }
  8489. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8490. {
  8491. struct drm_device *dev = crtc->base.dev;
  8492. struct intel_encoder *encoder;
  8493. for_each_intel_encoder(dev, encoder) {
  8494. if (encoder->new_crtc != crtc)
  8495. continue;
  8496. if (!check_single_encoder_cloning(crtc, encoder))
  8497. return false;
  8498. }
  8499. return true;
  8500. }
  8501. static bool check_digital_port_conflicts(struct drm_device *dev)
  8502. {
  8503. struct intel_connector *connector;
  8504. unsigned int used_ports = 0;
  8505. /*
  8506. * Walk the connector list instead of the encoder
  8507. * list to detect the problem on ddi platforms
  8508. * where there's just one encoder per digital port.
  8509. */
  8510. list_for_each_entry(connector,
  8511. &dev->mode_config.connector_list, base.head) {
  8512. struct intel_encoder *encoder = connector->new_encoder;
  8513. if (!encoder)
  8514. continue;
  8515. WARN_ON(!encoder->new_crtc);
  8516. switch (encoder->type) {
  8517. unsigned int port_mask;
  8518. case INTEL_OUTPUT_UNKNOWN:
  8519. if (WARN_ON(!HAS_DDI(dev)))
  8520. break;
  8521. case INTEL_OUTPUT_DISPLAYPORT:
  8522. case INTEL_OUTPUT_HDMI:
  8523. case INTEL_OUTPUT_EDP:
  8524. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8525. /* the same port mustn't appear more than once */
  8526. if (used_ports & port_mask)
  8527. return false;
  8528. used_ports |= port_mask;
  8529. default:
  8530. break;
  8531. }
  8532. }
  8533. return true;
  8534. }
  8535. static struct intel_crtc_state *
  8536. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8537. struct drm_framebuffer *fb,
  8538. struct drm_display_mode *mode)
  8539. {
  8540. struct drm_device *dev = crtc->dev;
  8541. struct intel_encoder *encoder;
  8542. struct intel_crtc_state *pipe_config;
  8543. int plane_bpp, ret = -EINVAL;
  8544. bool retry = true;
  8545. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8546. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8547. return ERR_PTR(-EINVAL);
  8548. }
  8549. if (!check_digital_port_conflicts(dev)) {
  8550. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8551. return ERR_PTR(-EINVAL);
  8552. }
  8553. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8554. if (!pipe_config)
  8555. return ERR_PTR(-ENOMEM);
  8556. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  8557. drm_mode_copy(&pipe_config->base.mode, mode);
  8558. pipe_config->cpu_transcoder =
  8559. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8560. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8561. /*
  8562. * Sanitize sync polarity flags based on requested ones. If neither
  8563. * positive or negative polarity is requested, treat this as meaning
  8564. * negative polarity.
  8565. */
  8566. if (!(pipe_config->base.adjusted_mode.flags &
  8567. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8568. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8569. if (!(pipe_config->base.adjusted_mode.flags &
  8570. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8571. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8572. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8573. * plane pixel format and any sink constraints into account. Returns the
  8574. * source plane bpp so that dithering can be selected on mismatches
  8575. * after encoders and crtc also have had their say. */
  8576. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8577. fb, pipe_config);
  8578. if (plane_bpp < 0)
  8579. goto fail;
  8580. /*
  8581. * Determine the real pipe dimensions. Note that stereo modes can
  8582. * increase the actual pipe size due to the frame doubling and
  8583. * insertion of additional space for blanks between the frame. This
  8584. * is stored in the crtc timings. We use the requested mode to do this
  8585. * computation to clearly distinguish it from the adjusted mode, which
  8586. * can be changed by the connectors in the below retry loop.
  8587. */
  8588. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  8589. &pipe_config->pipe_src_w,
  8590. &pipe_config->pipe_src_h);
  8591. encoder_retry:
  8592. /* Ensure the port clock defaults are reset when retrying. */
  8593. pipe_config->port_clock = 0;
  8594. pipe_config->pixel_multiplier = 1;
  8595. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8596. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  8597. CRTC_STEREO_DOUBLE);
  8598. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8599. * adjust it according to limitations or connector properties, and also
  8600. * a chance to reject the mode entirely.
  8601. */
  8602. for_each_intel_encoder(dev, encoder) {
  8603. if (&encoder->new_crtc->base != crtc)
  8604. continue;
  8605. if (!(encoder->compute_config(encoder, pipe_config))) {
  8606. DRM_DEBUG_KMS("Encoder config failure\n");
  8607. goto fail;
  8608. }
  8609. }
  8610. /* Set default port clock if not overwritten by the encoder. Needs to be
  8611. * done afterwards in case the encoder adjusts the mode. */
  8612. if (!pipe_config->port_clock)
  8613. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  8614. * pipe_config->pixel_multiplier;
  8615. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8616. if (ret < 0) {
  8617. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8618. goto fail;
  8619. }
  8620. if (ret == RETRY) {
  8621. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8622. ret = -EINVAL;
  8623. goto fail;
  8624. }
  8625. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8626. retry = false;
  8627. goto encoder_retry;
  8628. }
  8629. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8630. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8631. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8632. return pipe_config;
  8633. fail:
  8634. kfree(pipe_config);
  8635. return ERR_PTR(ret);
  8636. }
  8637. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8638. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8639. static void
  8640. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8641. unsigned *prepare_pipes, unsigned *disable_pipes)
  8642. {
  8643. struct intel_crtc *intel_crtc;
  8644. struct drm_device *dev = crtc->dev;
  8645. struct intel_encoder *encoder;
  8646. struct intel_connector *connector;
  8647. struct drm_crtc *tmp_crtc;
  8648. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8649. /* Check which crtcs have changed outputs connected to them, these need
  8650. * to be part of the prepare_pipes mask. We don't (yet) support global
  8651. * modeset across multiple crtcs, so modeset_pipes will only have one
  8652. * bit set at most. */
  8653. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8654. base.head) {
  8655. if (connector->base.encoder == &connector->new_encoder->base)
  8656. continue;
  8657. if (connector->base.encoder) {
  8658. tmp_crtc = connector->base.encoder->crtc;
  8659. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8660. }
  8661. if (connector->new_encoder)
  8662. *prepare_pipes |=
  8663. 1 << connector->new_encoder->new_crtc->pipe;
  8664. }
  8665. for_each_intel_encoder(dev, encoder) {
  8666. if (encoder->base.crtc == &encoder->new_crtc->base)
  8667. continue;
  8668. if (encoder->base.crtc) {
  8669. tmp_crtc = encoder->base.crtc;
  8670. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8671. }
  8672. if (encoder->new_crtc)
  8673. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8674. }
  8675. /* Check for pipes that will be enabled/disabled ... */
  8676. for_each_intel_crtc(dev, intel_crtc) {
  8677. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8678. continue;
  8679. if (!intel_crtc->new_enabled)
  8680. *disable_pipes |= 1 << intel_crtc->pipe;
  8681. else
  8682. *prepare_pipes |= 1 << intel_crtc->pipe;
  8683. }
  8684. /* set_mode is also used to update properties on life display pipes. */
  8685. intel_crtc = to_intel_crtc(crtc);
  8686. if (intel_crtc->new_enabled)
  8687. *prepare_pipes |= 1 << intel_crtc->pipe;
  8688. /*
  8689. * For simplicity do a full modeset on any pipe where the output routing
  8690. * changed. We could be more clever, but that would require us to be
  8691. * more careful with calling the relevant encoder->mode_set functions.
  8692. */
  8693. if (*prepare_pipes)
  8694. *modeset_pipes = *prepare_pipes;
  8695. /* ... and mask these out. */
  8696. *modeset_pipes &= ~(*disable_pipes);
  8697. *prepare_pipes &= ~(*disable_pipes);
  8698. /*
  8699. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8700. * obies this rule, but the modeset restore mode of
  8701. * intel_modeset_setup_hw_state does not.
  8702. */
  8703. *modeset_pipes &= 1 << intel_crtc->pipe;
  8704. *prepare_pipes &= 1 << intel_crtc->pipe;
  8705. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8706. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8707. }
  8708. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8709. {
  8710. struct drm_encoder *encoder;
  8711. struct drm_device *dev = crtc->dev;
  8712. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8713. if (encoder->crtc == crtc)
  8714. return true;
  8715. return false;
  8716. }
  8717. static void
  8718. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8719. {
  8720. struct drm_i915_private *dev_priv = dev->dev_private;
  8721. struct intel_encoder *intel_encoder;
  8722. struct intel_crtc *intel_crtc;
  8723. struct drm_connector *connector;
  8724. intel_shared_dpll_commit(dev_priv);
  8725. for_each_intel_encoder(dev, intel_encoder) {
  8726. if (!intel_encoder->base.crtc)
  8727. continue;
  8728. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8729. if (prepare_pipes & (1 << intel_crtc->pipe))
  8730. intel_encoder->connectors_active = false;
  8731. }
  8732. intel_modeset_commit_output_state(dev);
  8733. /* Double check state. */
  8734. for_each_intel_crtc(dev, intel_crtc) {
  8735. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8736. WARN_ON(intel_crtc->new_config &&
  8737. intel_crtc->new_config != intel_crtc->config);
  8738. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8739. }
  8740. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8741. if (!connector->encoder || !connector->encoder->crtc)
  8742. continue;
  8743. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8744. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8745. struct drm_property *dpms_property =
  8746. dev->mode_config.dpms_property;
  8747. connector->dpms = DRM_MODE_DPMS_ON;
  8748. drm_object_property_set_value(&connector->base,
  8749. dpms_property,
  8750. DRM_MODE_DPMS_ON);
  8751. intel_encoder = to_intel_encoder(connector->encoder);
  8752. intel_encoder->connectors_active = true;
  8753. }
  8754. }
  8755. }
  8756. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8757. {
  8758. int diff;
  8759. if (clock1 == clock2)
  8760. return true;
  8761. if (!clock1 || !clock2)
  8762. return false;
  8763. diff = abs(clock1 - clock2);
  8764. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8765. return true;
  8766. return false;
  8767. }
  8768. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8769. list_for_each_entry((intel_crtc), \
  8770. &(dev)->mode_config.crtc_list, \
  8771. base.head) \
  8772. if (mask & (1 <<(intel_crtc)->pipe))
  8773. static bool
  8774. intel_pipe_config_compare(struct drm_device *dev,
  8775. struct intel_crtc_state *current_config,
  8776. struct intel_crtc_state *pipe_config)
  8777. {
  8778. #define PIPE_CONF_CHECK_X(name) \
  8779. if (current_config->name != pipe_config->name) { \
  8780. DRM_ERROR("mismatch in " #name " " \
  8781. "(expected 0x%08x, found 0x%08x)\n", \
  8782. current_config->name, \
  8783. pipe_config->name); \
  8784. return false; \
  8785. }
  8786. #define PIPE_CONF_CHECK_I(name) \
  8787. if (current_config->name != pipe_config->name) { \
  8788. DRM_ERROR("mismatch in " #name " " \
  8789. "(expected %i, found %i)\n", \
  8790. current_config->name, \
  8791. pipe_config->name); \
  8792. return false; \
  8793. }
  8794. /* This is required for BDW+ where there is only one set of registers for
  8795. * switching between high and low RR.
  8796. * This macro can be used whenever a comparison has to be made between one
  8797. * hw state and multiple sw state variables.
  8798. */
  8799. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8800. if ((current_config->name != pipe_config->name) && \
  8801. (current_config->alt_name != pipe_config->name)) { \
  8802. DRM_ERROR("mismatch in " #name " " \
  8803. "(expected %i or %i, found %i)\n", \
  8804. current_config->name, \
  8805. current_config->alt_name, \
  8806. pipe_config->name); \
  8807. return false; \
  8808. }
  8809. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8810. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8811. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8812. "(expected %i, found %i)\n", \
  8813. current_config->name & (mask), \
  8814. pipe_config->name & (mask)); \
  8815. return false; \
  8816. }
  8817. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8818. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8819. DRM_ERROR("mismatch in " #name " " \
  8820. "(expected %i, found %i)\n", \
  8821. current_config->name, \
  8822. pipe_config->name); \
  8823. return false; \
  8824. }
  8825. #define PIPE_CONF_QUIRK(quirk) \
  8826. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8827. PIPE_CONF_CHECK_I(cpu_transcoder);
  8828. PIPE_CONF_CHECK_I(has_pch_encoder);
  8829. PIPE_CONF_CHECK_I(fdi_lanes);
  8830. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8831. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8832. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8833. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8834. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8835. PIPE_CONF_CHECK_I(has_dp_encoder);
  8836. if (INTEL_INFO(dev)->gen < 8) {
  8837. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8838. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8839. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8840. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8841. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8842. if (current_config->has_drrs) {
  8843. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8844. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8845. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8846. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8847. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8848. }
  8849. } else {
  8850. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8851. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8852. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8853. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8854. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8855. }
  8856. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  8857. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  8858. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  8859. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  8860. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  8861. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  8862. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  8863. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  8864. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  8865. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  8866. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  8867. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  8868. PIPE_CONF_CHECK_I(pixel_multiplier);
  8869. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8870. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8871. IS_VALLEYVIEW(dev))
  8872. PIPE_CONF_CHECK_I(limited_color_range);
  8873. PIPE_CONF_CHECK_I(has_infoframe);
  8874. PIPE_CONF_CHECK_I(has_audio);
  8875. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8876. DRM_MODE_FLAG_INTERLACE);
  8877. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8878. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8879. DRM_MODE_FLAG_PHSYNC);
  8880. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8881. DRM_MODE_FLAG_NHSYNC);
  8882. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8883. DRM_MODE_FLAG_PVSYNC);
  8884. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8885. DRM_MODE_FLAG_NVSYNC);
  8886. }
  8887. PIPE_CONF_CHECK_I(pipe_src_w);
  8888. PIPE_CONF_CHECK_I(pipe_src_h);
  8889. /*
  8890. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8891. * screen. Since we don't yet re-compute the pipe config when moving
  8892. * just the lvds port away to another pipe the sw tracking won't match.
  8893. *
  8894. * Proper atomic modesets with recomputed global state will fix this.
  8895. * Until then just don't check gmch state for inherited modes.
  8896. */
  8897. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8898. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8899. /* pfit ratios are autocomputed by the hw on gen4+ */
  8900. if (INTEL_INFO(dev)->gen < 4)
  8901. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8902. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8903. }
  8904. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8905. if (current_config->pch_pfit.enabled) {
  8906. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8907. PIPE_CONF_CHECK_I(pch_pfit.size);
  8908. }
  8909. /* BDW+ don't expose a synchronous way to read the state */
  8910. if (IS_HASWELL(dev))
  8911. PIPE_CONF_CHECK_I(ips_enabled);
  8912. PIPE_CONF_CHECK_I(double_wide);
  8913. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8914. PIPE_CONF_CHECK_I(shared_dpll);
  8915. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8916. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8917. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8918. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8919. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8920. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  8921. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  8922. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  8923. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8924. PIPE_CONF_CHECK_I(pipe_bpp);
  8925. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  8926. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8927. #undef PIPE_CONF_CHECK_X
  8928. #undef PIPE_CONF_CHECK_I
  8929. #undef PIPE_CONF_CHECK_I_ALT
  8930. #undef PIPE_CONF_CHECK_FLAGS
  8931. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8932. #undef PIPE_CONF_QUIRK
  8933. return true;
  8934. }
  8935. static void check_wm_state(struct drm_device *dev)
  8936. {
  8937. struct drm_i915_private *dev_priv = dev->dev_private;
  8938. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  8939. struct intel_crtc *intel_crtc;
  8940. int plane;
  8941. if (INTEL_INFO(dev)->gen < 9)
  8942. return;
  8943. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  8944. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  8945. for_each_intel_crtc(dev, intel_crtc) {
  8946. struct skl_ddb_entry *hw_entry, *sw_entry;
  8947. const enum pipe pipe = intel_crtc->pipe;
  8948. if (!intel_crtc->active)
  8949. continue;
  8950. /* planes */
  8951. for_each_plane(pipe, plane) {
  8952. hw_entry = &hw_ddb.plane[pipe][plane];
  8953. sw_entry = &sw_ddb->plane[pipe][plane];
  8954. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8955. continue;
  8956. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  8957. "(expected (%u,%u), found (%u,%u))\n",
  8958. pipe_name(pipe), plane + 1,
  8959. sw_entry->start, sw_entry->end,
  8960. hw_entry->start, hw_entry->end);
  8961. }
  8962. /* cursor */
  8963. hw_entry = &hw_ddb.cursor[pipe];
  8964. sw_entry = &sw_ddb->cursor[pipe];
  8965. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8966. continue;
  8967. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  8968. "(expected (%u,%u), found (%u,%u))\n",
  8969. pipe_name(pipe),
  8970. sw_entry->start, sw_entry->end,
  8971. hw_entry->start, hw_entry->end);
  8972. }
  8973. }
  8974. static void
  8975. check_connector_state(struct drm_device *dev)
  8976. {
  8977. struct intel_connector *connector;
  8978. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8979. base.head) {
  8980. /* This also checks the encoder/connector hw state with the
  8981. * ->get_hw_state callbacks. */
  8982. intel_connector_check_state(connector);
  8983. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  8984. "connector's staged encoder doesn't match current encoder\n");
  8985. }
  8986. }
  8987. static void
  8988. check_encoder_state(struct drm_device *dev)
  8989. {
  8990. struct intel_encoder *encoder;
  8991. struct intel_connector *connector;
  8992. for_each_intel_encoder(dev, encoder) {
  8993. bool enabled = false;
  8994. bool active = false;
  8995. enum pipe pipe, tracked_pipe;
  8996. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8997. encoder->base.base.id,
  8998. encoder->base.name);
  8999. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  9000. "encoder's stage crtc doesn't match current crtc\n");
  9001. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  9002. "encoder's active_connectors set, but no crtc\n");
  9003. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9004. base.head) {
  9005. if (connector->base.encoder != &encoder->base)
  9006. continue;
  9007. enabled = true;
  9008. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  9009. active = true;
  9010. }
  9011. /*
  9012. * for MST connectors if we unplug the connector is gone
  9013. * away but the encoder is still connected to a crtc
  9014. * until a modeset happens in response to the hotplug.
  9015. */
  9016. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  9017. continue;
  9018. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9019. "encoder's enabled state mismatch "
  9020. "(expected %i, found %i)\n",
  9021. !!encoder->base.crtc, enabled);
  9022. I915_STATE_WARN(active && !encoder->base.crtc,
  9023. "active encoder with no crtc\n");
  9024. I915_STATE_WARN(encoder->connectors_active != active,
  9025. "encoder's computed active state doesn't match tracked active state "
  9026. "(expected %i, found %i)\n", active, encoder->connectors_active);
  9027. active = encoder->get_hw_state(encoder, &pipe);
  9028. I915_STATE_WARN(active != encoder->connectors_active,
  9029. "encoder's hw state doesn't match sw tracking "
  9030. "(expected %i, found %i)\n",
  9031. encoder->connectors_active, active);
  9032. if (!encoder->base.crtc)
  9033. continue;
  9034. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  9035. I915_STATE_WARN(active && pipe != tracked_pipe,
  9036. "active encoder's pipe doesn't match"
  9037. "(expected %i, found %i)\n",
  9038. tracked_pipe, pipe);
  9039. }
  9040. }
  9041. static void
  9042. check_crtc_state(struct drm_device *dev)
  9043. {
  9044. struct drm_i915_private *dev_priv = dev->dev_private;
  9045. struct intel_crtc *crtc;
  9046. struct intel_encoder *encoder;
  9047. struct intel_crtc_state pipe_config;
  9048. for_each_intel_crtc(dev, crtc) {
  9049. bool enabled = false;
  9050. bool active = false;
  9051. memset(&pipe_config, 0, sizeof(pipe_config));
  9052. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9053. crtc->base.base.id);
  9054. I915_STATE_WARN(crtc->active && !crtc->base.enabled,
  9055. "active crtc, but not enabled in sw tracking\n");
  9056. for_each_intel_encoder(dev, encoder) {
  9057. if (encoder->base.crtc != &crtc->base)
  9058. continue;
  9059. enabled = true;
  9060. if (encoder->connectors_active)
  9061. active = true;
  9062. }
  9063. I915_STATE_WARN(active != crtc->active,
  9064. "crtc's computed active state doesn't match tracked active state "
  9065. "(expected %i, found %i)\n", active, crtc->active);
  9066. I915_STATE_WARN(enabled != crtc->base.enabled,
  9067. "crtc's computed enabled state doesn't match tracked enabled state "
  9068. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  9069. active = dev_priv->display.get_pipe_config(crtc,
  9070. &pipe_config);
  9071. /* hw state is inconsistent with the pipe quirk */
  9072. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9073. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9074. active = crtc->active;
  9075. for_each_intel_encoder(dev, encoder) {
  9076. enum pipe pipe;
  9077. if (encoder->base.crtc != &crtc->base)
  9078. continue;
  9079. if (encoder->get_hw_state(encoder, &pipe))
  9080. encoder->get_config(encoder, &pipe_config);
  9081. }
  9082. I915_STATE_WARN(crtc->active != active,
  9083. "crtc active state doesn't match with hw state "
  9084. "(expected %i, found %i)\n", crtc->active, active);
  9085. if (active &&
  9086. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  9087. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9088. intel_dump_pipe_config(crtc, &pipe_config,
  9089. "[hw state]");
  9090. intel_dump_pipe_config(crtc, crtc->config,
  9091. "[sw state]");
  9092. }
  9093. }
  9094. }
  9095. static void
  9096. check_shared_dpll_state(struct drm_device *dev)
  9097. {
  9098. struct drm_i915_private *dev_priv = dev->dev_private;
  9099. struct intel_crtc *crtc;
  9100. struct intel_dpll_hw_state dpll_hw_state;
  9101. int i;
  9102. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9103. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9104. int enabled_crtcs = 0, active_crtcs = 0;
  9105. bool active;
  9106. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9107. DRM_DEBUG_KMS("%s\n", pll->name);
  9108. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9109. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9110. "more active pll users than references: %i vs %i\n",
  9111. pll->active, hweight32(pll->config.crtc_mask));
  9112. I915_STATE_WARN(pll->active && !pll->on,
  9113. "pll in active use but not on in sw tracking\n");
  9114. I915_STATE_WARN(pll->on && !pll->active,
  9115. "pll in on but not on in use in sw tracking\n");
  9116. I915_STATE_WARN(pll->on != active,
  9117. "pll on state mismatch (expected %i, found %i)\n",
  9118. pll->on, active);
  9119. for_each_intel_crtc(dev, crtc) {
  9120. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9121. enabled_crtcs++;
  9122. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9123. active_crtcs++;
  9124. }
  9125. I915_STATE_WARN(pll->active != active_crtcs,
  9126. "pll active crtcs mismatch (expected %i, found %i)\n",
  9127. pll->active, active_crtcs);
  9128. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9129. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9130. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9131. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9132. sizeof(dpll_hw_state)),
  9133. "pll hw state mismatch\n");
  9134. }
  9135. }
  9136. void
  9137. intel_modeset_check_state(struct drm_device *dev)
  9138. {
  9139. check_wm_state(dev);
  9140. check_connector_state(dev);
  9141. check_encoder_state(dev);
  9142. check_crtc_state(dev);
  9143. check_shared_dpll_state(dev);
  9144. }
  9145. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  9146. int dotclock)
  9147. {
  9148. /*
  9149. * FDI already provided one idea for the dotclock.
  9150. * Yell if the encoder disagrees.
  9151. */
  9152. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  9153. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9154. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  9155. }
  9156. static void update_scanline_offset(struct intel_crtc *crtc)
  9157. {
  9158. struct drm_device *dev = crtc->base.dev;
  9159. /*
  9160. * The scanline counter increments at the leading edge of hsync.
  9161. *
  9162. * On most platforms it starts counting from vtotal-1 on the
  9163. * first active line. That means the scanline counter value is
  9164. * always one less than what we would expect. Ie. just after
  9165. * start of vblank, which also occurs at start of hsync (on the
  9166. * last active line), the scanline counter will read vblank_start-1.
  9167. *
  9168. * On gen2 the scanline counter starts counting from 1 instead
  9169. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9170. * to keep the value positive), instead of adding one.
  9171. *
  9172. * On HSW+ the behaviour of the scanline counter depends on the output
  9173. * type. For DP ports it behaves like most other platforms, but on HDMI
  9174. * there's an extra 1 line difference. So we need to add two instead of
  9175. * one to the value.
  9176. */
  9177. if (IS_GEN2(dev)) {
  9178. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  9179. int vtotal;
  9180. vtotal = mode->crtc_vtotal;
  9181. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9182. vtotal /= 2;
  9183. crtc->scanline_offset = vtotal - 1;
  9184. } else if (HAS_DDI(dev) &&
  9185. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9186. crtc->scanline_offset = 2;
  9187. } else
  9188. crtc->scanline_offset = 1;
  9189. }
  9190. static struct intel_crtc_state *
  9191. intel_modeset_compute_config(struct drm_crtc *crtc,
  9192. struct drm_display_mode *mode,
  9193. struct drm_framebuffer *fb,
  9194. unsigned *modeset_pipes,
  9195. unsigned *prepare_pipes,
  9196. unsigned *disable_pipes)
  9197. {
  9198. struct intel_crtc_state *pipe_config = NULL;
  9199. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9200. prepare_pipes, disable_pipes);
  9201. if ((*modeset_pipes) == 0)
  9202. goto out;
  9203. /*
  9204. * Note this needs changes when we start tracking multiple modes
  9205. * and crtcs. At that point we'll need to compute the whole config
  9206. * (i.e. one pipe_config for each crtc) rather than just the one
  9207. * for this crtc.
  9208. */
  9209. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9210. if (IS_ERR(pipe_config)) {
  9211. goto out;
  9212. }
  9213. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9214. "[modeset]");
  9215. out:
  9216. return pipe_config;
  9217. }
  9218. static int __intel_set_mode(struct drm_crtc *crtc,
  9219. struct drm_display_mode *mode,
  9220. int x, int y, struct drm_framebuffer *fb,
  9221. struct intel_crtc_state *pipe_config,
  9222. unsigned modeset_pipes,
  9223. unsigned prepare_pipes,
  9224. unsigned disable_pipes)
  9225. {
  9226. struct drm_device *dev = crtc->dev;
  9227. struct drm_i915_private *dev_priv = dev->dev_private;
  9228. struct drm_display_mode *saved_mode;
  9229. struct intel_crtc *intel_crtc;
  9230. int ret = 0;
  9231. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9232. if (!saved_mode)
  9233. return -ENOMEM;
  9234. *saved_mode = crtc->mode;
  9235. if (modeset_pipes)
  9236. to_intel_crtc(crtc)->new_config = pipe_config;
  9237. /*
  9238. * See if the config requires any additional preparation, e.g.
  9239. * to adjust global state with pipes off. We need to do this
  9240. * here so we can get the modeset_pipe updated config for the new
  9241. * mode set on this crtc. For other crtcs we need to use the
  9242. * adjusted_mode bits in the crtc directly.
  9243. */
  9244. if (IS_VALLEYVIEW(dev)) {
  9245. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9246. /* may have added more to prepare_pipes than we should */
  9247. prepare_pipes &= ~disable_pipes;
  9248. }
  9249. if (dev_priv->display.crtc_compute_clock) {
  9250. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9251. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9252. if (ret)
  9253. goto done;
  9254. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9255. struct intel_crtc_state *state = intel_crtc->new_config;
  9256. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9257. state);
  9258. if (ret) {
  9259. intel_shared_dpll_abort_config(dev_priv);
  9260. goto done;
  9261. }
  9262. }
  9263. }
  9264. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9265. intel_crtc_disable(&intel_crtc->base);
  9266. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9267. if (intel_crtc->base.enabled)
  9268. dev_priv->display.crtc_disable(&intel_crtc->base);
  9269. }
  9270. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9271. * to set it here already despite that we pass it down the callchain.
  9272. *
  9273. * Note we'll need to fix this up when we start tracking multiple
  9274. * pipes; here we assume a single modeset_pipe and only track the
  9275. * single crtc and mode.
  9276. */
  9277. if (modeset_pipes) {
  9278. crtc->mode = *mode;
  9279. /* mode_set/enable/disable functions rely on a correct pipe
  9280. * config. */
  9281. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  9282. /*
  9283. * Calculate and store various constants which
  9284. * are later needed by vblank and swap-completion
  9285. * timestamping. They are derived from true hwmode.
  9286. */
  9287. drm_calc_timestamping_constants(crtc,
  9288. &pipe_config->base.adjusted_mode);
  9289. }
  9290. /* Only after disabling all output pipelines that will be changed can we
  9291. * update the the output configuration. */
  9292. intel_modeset_update_state(dev, prepare_pipes);
  9293. modeset_update_crtc_power_domains(dev);
  9294. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9295. * on the DPLL.
  9296. */
  9297. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9298. struct drm_plane *primary = intel_crtc->base.primary;
  9299. int vdisplay, hdisplay;
  9300. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9301. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9302. fb, 0, 0,
  9303. hdisplay, vdisplay,
  9304. x << 16, y << 16,
  9305. hdisplay << 16, vdisplay << 16);
  9306. }
  9307. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9308. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9309. update_scanline_offset(intel_crtc);
  9310. dev_priv->display.crtc_enable(&intel_crtc->base);
  9311. }
  9312. /* FIXME: add subpixel order */
  9313. done:
  9314. if (ret && crtc->enabled)
  9315. crtc->mode = *saved_mode;
  9316. kfree(saved_mode);
  9317. return ret;
  9318. }
  9319. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9320. struct drm_display_mode *mode,
  9321. int x, int y, struct drm_framebuffer *fb,
  9322. struct intel_crtc_state *pipe_config,
  9323. unsigned modeset_pipes,
  9324. unsigned prepare_pipes,
  9325. unsigned disable_pipes)
  9326. {
  9327. int ret;
  9328. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9329. prepare_pipes, disable_pipes);
  9330. if (ret == 0)
  9331. intel_modeset_check_state(crtc->dev);
  9332. return ret;
  9333. }
  9334. static int intel_set_mode(struct drm_crtc *crtc,
  9335. struct drm_display_mode *mode,
  9336. int x, int y, struct drm_framebuffer *fb)
  9337. {
  9338. struct intel_crtc_state *pipe_config;
  9339. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9340. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9341. &modeset_pipes,
  9342. &prepare_pipes,
  9343. &disable_pipes);
  9344. if (IS_ERR(pipe_config))
  9345. return PTR_ERR(pipe_config);
  9346. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9347. modeset_pipes, prepare_pipes,
  9348. disable_pipes);
  9349. }
  9350. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9351. {
  9352. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9353. }
  9354. #undef for_each_intel_crtc_masked
  9355. static void intel_set_config_free(struct intel_set_config *config)
  9356. {
  9357. if (!config)
  9358. return;
  9359. kfree(config->save_connector_encoders);
  9360. kfree(config->save_encoder_crtcs);
  9361. kfree(config->save_crtc_enabled);
  9362. kfree(config);
  9363. }
  9364. static int intel_set_config_save_state(struct drm_device *dev,
  9365. struct intel_set_config *config)
  9366. {
  9367. struct drm_crtc *crtc;
  9368. struct drm_encoder *encoder;
  9369. struct drm_connector *connector;
  9370. int count;
  9371. config->save_crtc_enabled =
  9372. kcalloc(dev->mode_config.num_crtc,
  9373. sizeof(bool), GFP_KERNEL);
  9374. if (!config->save_crtc_enabled)
  9375. return -ENOMEM;
  9376. config->save_encoder_crtcs =
  9377. kcalloc(dev->mode_config.num_encoder,
  9378. sizeof(struct drm_crtc *), GFP_KERNEL);
  9379. if (!config->save_encoder_crtcs)
  9380. return -ENOMEM;
  9381. config->save_connector_encoders =
  9382. kcalloc(dev->mode_config.num_connector,
  9383. sizeof(struct drm_encoder *), GFP_KERNEL);
  9384. if (!config->save_connector_encoders)
  9385. return -ENOMEM;
  9386. /* Copy data. Note that driver private data is not affected.
  9387. * Should anything bad happen only the expected state is
  9388. * restored, not the drivers personal bookkeeping.
  9389. */
  9390. count = 0;
  9391. for_each_crtc(dev, crtc) {
  9392. config->save_crtc_enabled[count++] = crtc->enabled;
  9393. }
  9394. count = 0;
  9395. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9396. config->save_encoder_crtcs[count++] = encoder->crtc;
  9397. }
  9398. count = 0;
  9399. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9400. config->save_connector_encoders[count++] = connector->encoder;
  9401. }
  9402. return 0;
  9403. }
  9404. static void intel_set_config_restore_state(struct drm_device *dev,
  9405. struct intel_set_config *config)
  9406. {
  9407. struct intel_crtc *crtc;
  9408. struct intel_encoder *encoder;
  9409. struct intel_connector *connector;
  9410. int count;
  9411. count = 0;
  9412. for_each_intel_crtc(dev, crtc) {
  9413. crtc->new_enabled = config->save_crtc_enabled[count++];
  9414. if (crtc->new_enabled)
  9415. crtc->new_config = crtc->config;
  9416. else
  9417. crtc->new_config = NULL;
  9418. }
  9419. count = 0;
  9420. for_each_intel_encoder(dev, encoder) {
  9421. encoder->new_crtc =
  9422. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9423. }
  9424. count = 0;
  9425. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9426. connector->new_encoder =
  9427. to_intel_encoder(config->save_connector_encoders[count++]);
  9428. }
  9429. }
  9430. static bool
  9431. is_crtc_connector_off(struct drm_mode_set *set)
  9432. {
  9433. int i;
  9434. if (set->num_connectors == 0)
  9435. return false;
  9436. if (WARN_ON(set->connectors == NULL))
  9437. return false;
  9438. for (i = 0; i < set->num_connectors; i++)
  9439. if (set->connectors[i]->encoder &&
  9440. set->connectors[i]->encoder->crtc == set->crtc &&
  9441. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9442. return true;
  9443. return false;
  9444. }
  9445. static void
  9446. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9447. struct intel_set_config *config)
  9448. {
  9449. /* We should be able to check here if the fb has the same properties
  9450. * and then just flip_or_move it */
  9451. if (is_crtc_connector_off(set)) {
  9452. config->mode_changed = true;
  9453. } else if (set->crtc->primary->fb != set->fb) {
  9454. /*
  9455. * If we have no fb, we can only flip as long as the crtc is
  9456. * active, otherwise we need a full mode set. The crtc may
  9457. * be active if we've only disabled the primary plane, or
  9458. * in fastboot situations.
  9459. */
  9460. if (set->crtc->primary->fb == NULL) {
  9461. struct intel_crtc *intel_crtc =
  9462. to_intel_crtc(set->crtc);
  9463. if (intel_crtc->active) {
  9464. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9465. config->fb_changed = true;
  9466. } else {
  9467. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9468. config->mode_changed = true;
  9469. }
  9470. } else if (set->fb == NULL) {
  9471. config->mode_changed = true;
  9472. } else if (set->fb->pixel_format !=
  9473. set->crtc->primary->fb->pixel_format) {
  9474. config->mode_changed = true;
  9475. } else {
  9476. config->fb_changed = true;
  9477. }
  9478. }
  9479. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9480. config->fb_changed = true;
  9481. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9482. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9483. drm_mode_debug_printmodeline(&set->crtc->mode);
  9484. drm_mode_debug_printmodeline(set->mode);
  9485. config->mode_changed = true;
  9486. }
  9487. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9488. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9489. }
  9490. static int
  9491. intel_modeset_stage_output_state(struct drm_device *dev,
  9492. struct drm_mode_set *set,
  9493. struct intel_set_config *config)
  9494. {
  9495. struct intel_connector *connector;
  9496. struct intel_encoder *encoder;
  9497. struct intel_crtc *crtc;
  9498. int ro;
  9499. /* The upper layers ensure that we either disable a crtc or have a list
  9500. * of connectors. For paranoia, double-check this. */
  9501. WARN_ON(!set->fb && (set->num_connectors != 0));
  9502. WARN_ON(set->fb && (set->num_connectors == 0));
  9503. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9504. base.head) {
  9505. /* Otherwise traverse passed in connector list and get encoders
  9506. * for them. */
  9507. for (ro = 0; ro < set->num_connectors; ro++) {
  9508. if (set->connectors[ro] == &connector->base) {
  9509. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9510. break;
  9511. }
  9512. }
  9513. /* If we disable the crtc, disable all its connectors. Also, if
  9514. * the connector is on the changing crtc but not on the new
  9515. * connector list, disable it. */
  9516. if ((!set->fb || ro == set->num_connectors) &&
  9517. connector->base.encoder &&
  9518. connector->base.encoder->crtc == set->crtc) {
  9519. connector->new_encoder = NULL;
  9520. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9521. connector->base.base.id,
  9522. connector->base.name);
  9523. }
  9524. if (&connector->new_encoder->base != connector->base.encoder) {
  9525. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9526. config->mode_changed = true;
  9527. }
  9528. }
  9529. /* connector->new_encoder is now updated for all connectors. */
  9530. /* Update crtc of enabled connectors. */
  9531. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9532. base.head) {
  9533. struct drm_crtc *new_crtc;
  9534. if (!connector->new_encoder)
  9535. continue;
  9536. new_crtc = connector->new_encoder->base.crtc;
  9537. for (ro = 0; ro < set->num_connectors; ro++) {
  9538. if (set->connectors[ro] == &connector->base)
  9539. new_crtc = set->crtc;
  9540. }
  9541. /* Make sure the new CRTC will work with the encoder */
  9542. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9543. new_crtc)) {
  9544. return -EINVAL;
  9545. }
  9546. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9547. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9548. connector->base.base.id,
  9549. connector->base.name,
  9550. new_crtc->base.id);
  9551. }
  9552. /* Check for any encoders that needs to be disabled. */
  9553. for_each_intel_encoder(dev, encoder) {
  9554. int num_connectors = 0;
  9555. list_for_each_entry(connector,
  9556. &dev->mode_config.connector_list,
  9557. base.head) {
  9558. if (connector->new_encoder == encoder) {
  9559. WARN_ON(!connector->new_encoder->new_crtc);
  9560. num_connectors++;
  9561. }
  9562. }
  9563. if (num_connectors == 0)
  9564. encoder->new_crtc = NULL;
  9565. else if (num_connectors > 1)
  9566. return -EINVAL;
  9567. /* Only now check for crtc changes so we don't miss encoders
  9568. * that will be disabled. */
  9569. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9570. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9571. config->mode_changed = true;
  9572. }
  9573. }
  9574. /* Now we've also updated encoder->new_crtc for all encoders. */
  9575. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9576. base.head) {
  9577. if (connector->new_encoder)
  9578. if (connector->new_encoder != connector->encoder)
  9579. connector->encoder = connector->new_encoder;
  9580. }
  9581. for_each_intel_crtc(dev, crtc) {
  9582. crtc->new_enabled = false;
  9583. for_each_intel_encoder(dev, encoder) {
  9584. if (encoder->new_crtc == crtc) {
  9585. crtc->new_enabled = true;
  9586. break;
  9587. }
  9588. }
  9589. if (crtc->new_enabled != crtc->base.enabled) {
  9590. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9591. crtc->new_enabled ? "en" : "dis");
  9592. config->mode_changed = true;
  9593. }
  9594. if (crtc->new_enabled)
  9595. crtc->new_config = crtc->config;
  9596. else
  9597. crtc->new_config = NULL;
  9598. }
  9599. return 0;
  9600. }
  9601. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9602. {
  9603. struct drm_device *dev = crtc->base.dev;
  9604. struct intel_encoder *encoder;
  9605. struct intel_connector *connector;
  9606. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9607. pipe_name(crtc->pipe));
  9608. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9609. if (connector->new_encoder &&
  9610. connector->new_encoder->new_crtc == crtc)
  9611. connector->new_encoder = NULL;
  9612. }
  9613. for_each_intel_encoder(dev, encoder) {
  9614. if (encoder->new_crtc == crtc)
  9615. encoder->new_crtc = NULL;
  9616. }
  9617. crtc->new_enabled = false;
  9618. crtc->new_config = NULL;
  9619. }
  9620. static int intel_crtc_set_config(struct drm_mode_set *set)
  9621. {
  9622. struct drm_device *dev;
  9623. struct drm_mode_set save_set;
  9624. struct intel_set_config *config;
  9625. struct intel_crtc_state *pipe_config;
  9626. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9627. int ret;
  9628. BUG_ON(!set);
  9629. BUG_ON(!set->crtc);
  9630. BUG_ON(!set->crtc->helper_private);
  9631. /* Enforce sane interface api - has been abused by the fb helper. */
  9632. BUG_ON(!set->mode && set->fb);
  9633. BUG_ON(set->fb && set->num_connectors == 0);
  9634. if (set->fb) {
  9635. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9636. set->crtc->base.id, set->fb->base.id,
  9637. (int)set->num_connectors, set->x, set->y);
  9638. } else {
  9639. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9640. }
  9641. dev = set->crtc->dev;
  9642. ret = -ENOMEM;
  9643. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9644. if (!config)
  9645. goto out_config;
  9646. ret = intel_set_config_save_state(dev, config);
  9647. if (ret)
  9648. goto out_config;
  9649. save_set.crtc = set->crtc;
  9650. save_set.mode = &set->crtc->mode;
  9651. save_set.x = set->crtc->x;
  9652. save_set.y = set->crtc->y;
  9653. save_set.fb = set->crtc->primary->fb;
  9654. /* Compute whether we need a full modeset, only an fb base update or no
  9655. * change at all. In the future we might also check whether only the
  9656. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9657. * such cases. */
  9658. intel_set_config_compute_mode_changes(set, config);
  9659. ret = intel_modeset_stage_output_state(dev, set, config);
  9660. if (ret)
  9661. goto fail;
  9662. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9663. set->fb,
  9664. &modeset_pipes,
  9665. &prepare_pipes,
  9666. &disable_pipes);
  9667. if (IS_ERR(pipe_config)) {
  9668. ret = PTR_ERR(pipe_config);
  9669. goto fail;
  9670. } else if (pipe_config) {
  9671. if (pipe_config->has_audio !=
  9672. to_intel_crtc(set->crtc)->config->has_audio)
  9673. config->mode_changed = true;
  9674. /*
  9675. * Note we have an issue here with infoframes: current code
  9676. * only updates them on the full mode set path per hw
  9677. * requirements. So here we should be checking for any
  9678. * required changes and forcing a mode set.
  9679. */
  9680. }
  9681. /* set_mode will free it in the mode_changed case */
  9682. if (!config->mode_changed)
  9683. kfree(pipe_config);
  9684. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9685. if (config->mode_changed) {
  9686. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9687. set->x, set->y, set->fb, pipe_config,
  9688. modeset_pipes, prepare_pipes,
  9689. disable_pipes);
  9690. } else if (config->fb_changed) {
  9691. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9692. struct drm_plane *primary = set->crtc->primary;
  9693. int vdisplay, hdisplay;
  9694. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9695. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9696. 0, 0, hdisplay, vdisplay,
  9697. set->x << 16, set->y << 16,
  9698. hdisplay << 16, vdisplay << 16);
  9699. /*
  9700. * We need to make sure the primary plane is re-enabled if it
  9701. * has previously been turned off.
  9702. */
  9703. if (!intel_crtc->primary_enabled && ret == 0) {
  9704. WARN_ON(!intel_crtc->active);
  9705. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9706. }
  9707. /*
  9708. * In the fastboot case this may be our only check of the
  9709. * state after boot. It would be better to only do it on
  9710. * the first update, but we don't have a nice way of doing that
  9711. * (and really, set_config isn't used much for high freq page
  9712. * flipping, so increasing its cost here shouldn't be a big
  9713. * deal).
  9714. */
  9715. if (i915.fastboot && ret == 0)
  9716. intel_modeset_check_state(set->crtc->dev);
  9717. }
  9718. if (ret) {
  9719. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9720. set->crtc->base.id, ret);
  9721. fail:
  9722. intel_set_config_restore_state(dev, config);
  9723. /*
  9724. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9725. * force the pipe off to avoid oopsing in the modeset code
  9726. * due to fb==NULL. This should only happen during boot since
  9727. * we don't yet reconstruct the FB from the hardware state.
  9728. */
  9729. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9730. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9731. /* Try to restore the config */
  9732. if (config->mode_changed &&
  9733. intel_set_mode(save_set.crtc, save_set.mode,
  9734. save_set.x, save_set.y, save_set.fb))
  9735. DRM_ERROR("failed to restore config after modeset failure\n");
  9736. }
  9737. out_config:
  9738. intel_set_config_free(config);
  9739. return ret;
  9740. }
  9741. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9742. .gamma_set = intel_crtc_gamma_set,
  9743. .set_config = intel_crtc_set_config,
  9744. .destroy = intel_crtc_destroy,
  9745. .page_flip = intel_crtc_page_flip,
  9746. .atomic_duplicate_state = intel_crtc_duplicate_state,
  9747. .atomic_destroy_state = intel_crtc_destroy_state,
  9748. };
  9749. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9750. struct intel_shared_dpll *pll,
  9751. struct intel_dpll_hw_state *hw_state)
  9752. {
  9753. uint32_t val;
  9754. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9755. return false;
  9756. val = I915_READ(PCH_DPLL(pll->id));
  9757. hw_state->dpll = val;
  9758. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9759. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9760. return val & DPLL_VCO_ENABLE;
  9761. }
  9762. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9763. struct intel_shared_dpll *pll)
  9764. {
  9765. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  9766. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  9767. }
  9768. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9769. struct intel_shared_dpll *pll)
  9770. {
  9771. /* PCH refclock must be enabled first */
  9772. ibx_assert_pch_refclk_enabled(dev_priv);
  9773. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9774. /* Wait for the clocks to stabilize. */
  9775. POSTING_READ(PCH_DPLL(pll->id));
  9776. udelay(150);
  9777. /* The pixel multiplier can only be updated once the
  9778. * DPLL is enabled and the clocks are stable.
  9779. *
  9780. * So write it again.
  9781. */
  9782. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9783. POSTING_READ(PCH_DPLL(pll->id));
  9784. udelay(200);
  9785. }
  9786. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9787. struct intel_shared_dpll *pll)
  9788. {
  9789. struct drm_device *dev = dev_priv->dev;
  9790. struct intel_crtc *crtc;
  9791. /* Make sure no transcoder isn't still depending on us. */
  9792. for_each_intel_crtc(dev, crtc) {
  9793. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9794. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9795. }
  9796. I915_WRITE(PCH_DPLL(pll->id), 0);
  9797. POSTING_READ(PCH_DPLL(pll->id));
  9798. udelay(200);
  9799. }
  9800. static char *ibx_pch_dpll_names[] = {
  9801. "PCH DPLL A",
  9802. "PCH DPLL B",
  9803. };
  9804. static void ibx_pch_dpll_init(struct drm_device *dev)
  9805. {
  9806. struct drm_i915_private *dev_priv = dev->dev_private;
  9807. int i;
  9808. dev_priv->num_shared_dpll = 2;
  9809. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9810. dev_priv->shared_dplls[i].id = i;
  9811. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9812. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9813. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9814. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9815. dev_priv->shared_dplls[i].get_hw_state =
  9816. ibx_pch_dpll_get_hw_state;
  9817. }
  9818. }
  9819. static void intel_shared_dpll_init(struct drm_device *dev)
  9820. {
  9821. struct drm_i915_private *dev_priv = dev->dev_private;
  9822. if (HAS_DDI(dev))
  9823. intel_ddi_pll_init(dev);
  9824. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9825. ibx_pch_dpll_init(dev);
  9826. else
  9827. dev_priv->num_shared_dpll = 0;
  9828. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9829. }
  9830. /**
  9831. * intel_prepare_plane_fb - Prepare fb for usage on plane
  9832. * @plane: drm plane to prepare for
  9833. * @fb: framebuffer to prepare for presentation
  9834. *
  9835. * Prepares a framebuffer for usage on a display plane. Generally this
  9836. * involves pinning the underlying object and updating the frontbuffer tracking
  9837. * bits. Some older platforms need special physical address handling for
  9838. * cursor planes.
  9839. *
  9840. * Returns 0 on success, negative error code on failure.
  9841. */
  9842. int
  9843. intel_prepare_plane_fb(struct drm_plane *plane,
  9844. struct drm_framebuffer *fb)
  9845. {
  9846. struct drm_device *dev = plane->dev;
  9847. struct intel_plane *intel_plane = to_intel_plane(plane);
  9848. enum pipe pipe = intel_plane->pipe;
  9849. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9850. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9851. unsigned frontbuffer_bits = 0;
  9852. int ret = 0;
  9853. if (!obj)
  9854. return 0;
  9855. switch (plane->type) {
  9856. case DRM_PLANE_TYPE_PRIMARY:
  9857. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  9858. break;
  9859. case DRM_PLANE_TYPE_CURSOR:
  9860. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  9861. break;
  9862. case DRM_PLANE_TYPE_OVERLAY:
  9863. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  9864. break;
  9865. }
  9866. mutex_lock(&dev->struct_mutex);
  9867. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  9868. INTEL_INFO(dev)->cursor_needs_physical) {
  9869. int align = IS_I830(dev) ? 16 * 1024 : 256;
  9870. ret = i915_gem_object_attach_phys(obj, align);
  9871. if (ret)
  9872. DRM_DEBUG_KMS("failed to attach phys object\n");
  9873. } else {
  9874. ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
  9875. }
  9876. if (ret == 0)
  9877. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  9878. mutex_unlock(&dev->struct_mutex);
  9879. return ret;
  9880. }
  9881. /**
  9882. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  9883. * @plane: drm plane to clean up for
  9884. * @fb: old framebuffer that was on plane
  9885. *
  9886. * Cleans up a framebuffer that has just been removed from a plane.
  9887. */
  9888. void
  9889. intel_cleanup_plane_fb(struct drm_plane *plane,
  9890. struct drm_framebuffer *fb)
  9891. {
  9892. struct drm_device *dev = plane->dev;
  9893. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9894. if (WARN_ON(!obj))
  9895. return;
  9896. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  9897. !INTEL_INFO(dev)->cursor_needs_physical) {
  9898. mutex_lock(&dev->struct_mutex);
  9899. intel_unpin_fb_obj(obj);
  9900. mutex_unlock(&dev->struct_mutex);
  9901. }
  9902. }
  9903. static int
  9904. intel_check_primary_plane(struct drm_plane *plane,
  9905. struct intel_plane_state *state)
  9906. {
  9907. struct drm_device *dev = plane->dev;
  9908. struct drm_i915_private *dev_priv = dev->dev_private;
  9909. struct drm_crtc *crtc = state->base.crtc;
  9910. struct intel_crtc *intel_crtc;
  9911. struct drm_framebuffer *fb = state->base.fb;
  9912. struct drm_rect *dest = &state->dst;
  9913. struct drm_rect *src = &state->src;
  9914. const struct drm_rect *clip = &state->clip;
  9915. int ret;
  9916. crtc = crtc ? crtc : plane->crtc;
  9917. intel_crtc = to_intel_crtc(crtc);
  9918. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9919. src, dest, clip,
  9920. DRM_PLANE_HELPER_NO_SCALING,
  9921. DRM_PLANE_HELPER_NO_SCALING,
  9922. false, true, &state->visible);
  9923. if (ret)
  9924. return ret;
  9925. if (intel_crtc->active) {
  9926. intel_crtc->atomic.wait_for_flips = true;
  9927. /*
  9928. * FBC does not work on some platforms for rotated
  9929. * planes, so disable it when rotation is not 0 and
  9930. * update it when rotation is set back to 0.
  9931. *
  9932. * FIXME: This is redundant with the fbc update done in
  9933. * the primary plane enable function except that that
  9934. * one is done too late. We eventually need to unify
  9935. * this.
  9936. */
  9937. if (intel_crtc->primary_enabled &&
  9938. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9939. dev_priv->fbc.plane == intel_crtc->plane &&
  9940. state->base.rotation != BIT(DRM_ROTATE_0)) {
  9941. intel_crtc->atomic.disable_fbc = true;
  9942. }
  9943. if (state->visible) {
  9944. /*
  9945. * BDW signals flip done immediately if the plane
  9946. * is disabled, even if the plane enable is already
  9947. * armed to occur at the next vblank :(
  9948. */
  9949. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  9950. intel_crtc->atomic.wait_vblank = true;
  9951. }
  9952. intel_crtc->atomic.fb_bits |=
  9953. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  9954. intel_crtc->atomic.update_fbc = true;
  9955. }
  9956. return 0;
  9957. }
  9958. static void
  9959. intel_commit_primary_plane(struct drm_plane *plane,
  9960. struct intel_plane_state *state)
  9961. {
  9962. struct drm_crtc *crtc = state->base.crtc;
  9963. struct drm_framebuffer *fb = state->base.fb;
  9964. struct drm_device *dev = plane->dev;
  9965. struct drm_i915_private *dev_priv = dev->dev_private;
  9966. struct intel_crtc *intel_crtc;
  9967. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9968. struct intel_plane *intel_plane = to_intel_plane(plane);
  9969. struct drm_rect *src = &state->src;
  9970. crtc = crtc ? crtc : plane->crtc;
  9971. intel_crtc = to_intel_crtc(crtc);
  9972. plane->fb = fb;
  9973. crtc->x = src->x1 >> 16;
  9974. crtc->y = src->y1 >> 16;
  9975. intel_plane->obj = obj;
  9976. if (intel_crtc->active) {
  9977. if (state->visible) {
  9978. /* FIXME: kill this fastboot hack */
  9979. intel_update_pipe_size(intel_crtc);
  9980. intel_crtc->primary_enabled = true;
  9981. dev_priv->display.update_primary_plane(crtc, plane->fb,
  9982. crtc->x, crtc->y);
  9983. } else {
  9984. /*
  9985. * If clipping results in a non-visible primary plane,
  9986. * we'll disable the primary plane. Note that this is
  9987. * a bit different than what happens if userspace
  9988. * explicitly disables the plane by passing fb=0
  9989. * because plane->fb still gets set and pinned.
  9990. */
  9991. intel_disable_primary_hw_plane(plane, crtc);
  9992. }
  9993. }
  9994. }
  9995. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  9996. {
  9997. struct drm_device *dev = crtc->dev;
  9998. struct drm_i915_private *dev_priv = dev->dev_private;
  9999. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10000. struct intel_plane *intel_plane;
  10001. struct drm_plane *p;
  10002. unsigned fb_bits = 0;
  10003. /* Track fb's for any planes being disabled */
  10004. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  10005. intel_plane = to_intel_plane(p);
  10006. if (intel_crtc->atomic.disabled_planes &
  10007. (1 << drm_plane_index(p))) {
  10008. switch (p->type) {
  10009. case DRM_PLANE_TYPE_PRIMARY:
  10010. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  10011. break;
  10012. case DRM_PLANE_TYPE_CURSOR:
  10013. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  10014. break;
  10015. case DRM_PLANE_TYPE_OVERLAY:
  10016. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  10017. break;
  10018. }
  10019. mutex_lock(&dev->struct_mutex);
  10020. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  10021. mutex_unlock(&dev->struct_mutex);
  10022. }
  10023. }
  10024. if (intel_crtc->atomic.wait_for_flips)
  10025. intel_crtc_wait_for_pending_flips(crtc);
  10026. if (intel_crtc->atomic.disable_fbc)
  10027. intel_fbc_disable(dev);
  10028. if (intel_crtc->atomic.pre_disable_primary)
  10029. intel_pre_disable_primary(crtc);
  10030. if (intel_crtc->atomic.update_wm)
  10031. intel_update_watermarks(crtc);
  10032. intel_runtime_pm_get(dev_priv);
  10033. /* Perform vblank evasion around commit operation */
  10034. if (intel_crtc->active)
  10035. intel_crtc->atomic.evade =
  10036. intel_pipe_update_start(intel_crtc,
  10037. &intel_crtc->atomic.start_vbl_count);
  10038. }
  10039. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  10040. {
  10041. struct drm_device *dev = crtc->dev;
  10042. struct drm_i915_private *dev_priv = dev->dev_private;
  10043. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10044. struct drm_plane *p;
  10045. if (intel_crtc->atomic.evade)
  10046. intel_pipe_update_end(intel_crtc,
  10047. intel_crtc->atomic.start_vbl_count);
  10048. intel_runtime_pm_put(dev_priv);
  10049. if (intel_crtc->atomic.wait_vblank)
  10050. intel_wait_for_vblank(dev, intel_crtc->pipe);
  10051. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  10052. if (intel_crtc->atomic.update_fbc) {
  10053. mutex_lock(&dev->struct_mutex);
  10054. intel_fbc_update(dev);
  10055. mutex_unlock(&dev->struct_mutex);
  10056. }
  10057. if (intel_crtc->atomic.post_enable_primary)
  10058. intel_post_enable_primary(crtc);
  10059. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  10060. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  10061. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  10062. false, false);
  10063. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  10064. }
  10065. /**
  10066. * intel_plane_destroy - destroy a plane
  10067. * @plane: plane to destroy
  10068. *
  10069. * Common destruction function for all types of planes (primary, cursor,
  10070. * sprite).
  10071. */
  10072. void intel_plane_destroy(struct drm_plane *plane)
  10073. {
  10074. struct intel_plane *intel_plane = to_intel_plane(plane);
  10075. drm_plane_cleanup(plane);
  10076. kfree(intel_plane);
  10077. }
  10078. const struct drm_plane_funcs intel_plane_funcs = {
  10079. .update_plane = drm_plane_helper_update,
  10080. .disable_plane = drm_plane_helper_disable,
  10081. .destroy = intel_plane_destroy,
  10082. .set_property = intel_plane_set_property,
  10083. .atomic_get_property = intel_plane_atomic_get_property,
  10084. .atomic_set_property = intel_plane_atomic_set_property,
  10085. .atomic_duplicate_state = intel_plane_duplicate_state,
  10086. .atomic_destroy_state = intel_plane_destroy_state,
  10087. };
  10088. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10089. int pipe)
  10090. {
  10091. struct intel_plane *primary;
  10092. struct intel_plane_state *state;
  10093. const uint32_t *intel_primary_formats;
  10094. int num_formats;
  10095. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10096. if (primary == NULL)
  10097. return NULL;
  10098. state = intel_create_plane_state(&primary->base);
  10099. if (!state) {
  10100. kfree(primary);
  10101. return NULL;
  10102. }
  10103. primary->base.state = &state->base;
  10104. primary->can_scale = false;
  10105. primary->max_downscale = 1;
  10106. primary->pipe = pipe;
  10107. primary->plane = pipe;
  10108. primary->check_plane = intel_check_primary_plane;
  10109. primary->commit_plane = intel_commit_primary_plane;
  10110. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10111. primary->plane = !pipe;
  10112. if (INTEL_INFO(dev)->gen <= 3) {
  10113. intel_primary_formats = intel_primary_formats_gen2;
  10114. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10115. } else {
  10116. intel_primary_formats = intel_primary_formats_gen4;
  10117. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10118. }
  10119. drm_universal_plane_init(dev, &primary->base, 0,
  10120. &intel_plane_funcs,
  10121. intel_primary_formats, num_formats,
  10122. DRM_PLANE_TYPE_PRIMARY);
  10123. if (INTEL_INFO(dev)->gen >= 4) {
  10124. if (!dev->mode_config.rotation_property)
  10125. dev->mode_config.rotation_property =
  10126. drm_mode_create_rotation_property(dev,
  10127. BIT(DRM_ROTATE_0) |
  10128. BIT(DRM_ROTATE_180));
  10129. if (dev->mode_config.rotation_property)
  10130. drm_object_attach_property(&primary->base.base,
  10131. dev->mode_config.rotation_property,
  10132. state->base.rotation);
  10133. }
  10134. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  10135. return &primary->base;
  10136. }
  10137. static int
  10138. intel_check_cursor_plane(struct drm_plane *plane,
  10139. struct intel_plane_state *state)
  10140. {
  10141. struct drm_crtc *crtc = state->base.crtc;
  10142. struct drm_device *dev = plane->dev;
  10143. struct drm_framebuffer *fb = state->base.fb;
  10144. struct drm_rect *dest = &state->dst;
  10145. struct drm_rect *src = &state->src;
  10146. const struct drm_rect *clip = &state->clip;
  10147. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10148. struct intel_crtc *intel_crtc;
  10149. unsigned stride;
  10150. int ret;
  10151. crtc = crtc ? crtc : plane->crtc;
  10152. intel_crtc = to_intel_crtc(crtc);
  10153. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10154. src, dest, clip,
  10155. DRM_PLANE_HELPER_NO_SCALING,
  10156. DRM_PLANE_HELPER_NO_SCALING,
  10157. true, true, &state->visible);
  10158. if (ret)
  10159. return ret;
  10160. /* if we want to turn off the cursor ignore width and height */
  10161. if (!obj)
  10162. goto finish;
  10163. /* Check for which cursor types we support */
  10164. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  10165. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  10166. state->base.crtc_w, state->base.crtc_h);
  10167. return -EINVAL;
  10168. }
  10169. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  10170. if (obj->base.size < stride * state->base.crtc_h) {
  10171. DRM_DEBUG_KMS("buffer is too small\n");
  10172. return -ENOMEM;
  10173. }
  10174. if (fb == crtc->cursor->fb)
  10175. return 0;
  10176. /* we only need to pin inside GTT if cursor is non-phy */
  10177. mutex_lock(&dev->struct_mutex);
  10178. if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
  10179. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10180. ret = -EINVAL;
  10181. }
  10182. mutex_unlock(&dev->struct_mutex);
  10183. finish:
  10184. if (intel_crtc->active) {
  10185. if (intel_crtc->cursor_width != state->base.crtc_w)
  10186. intel_crtc->atomic.update_wm = true;
  10187. intel_crtc->atomic.fb_bits |=
  10188. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  10189. }
  10190. return ret;
  10191. }
  10192. static void
  10193. intel_commit_cursor_plane(struct drm_plane *plane,
  10194. struct intel_plane_state *state)
  10195. {
  10196. struct drm_crtc *crtc = state->base.crtc;
  10197. struct drm_device *dev = plane->dev;
  10198. struct intel_crtc *intel_crtc;
  10199. struct intel_plane *intel_plane = to_intel_plane(plane);
  10200. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10201. uint32_t addr;
  10202. crtc = crtc ? crtc : plane->crtc;
  10203. intel_crtc = to_intel_crtc(crtc);
  10204. plane->fb = state->base.fb;
  10205. crtc->cursor_x = state->base.crtc_x;
  10206. crtc->cursor_y = state->base.crtc_y;
  10207. intel_plane->obj = obj;
  10208. if (intel_crtc->cursor_bo == obj)
  10209. goto update;
  10210. if (!obj)
  10211. addr = 0;
  10212. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10213. addr = i915_gem_obj_ggtt_offset(obj);
  10214. else
  10215. addr = obj->phys_handle->busaddr;
  10216. intel_crtc->cursor_addr = addr;
  10217. intel_crtc->cursor_bo = obj;
  10218. update:
  10219. intel_crtc->cursor_width = state->base.crtc_w;
  10220. intel_crtc->cursor_height = state->base.crtc_h;
  10221. if (intel_crtc->active)
  10222. intel_crtc_update_cursor(crtc, state->visible);
  10223. }
  10224. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10225. int pipe)
  10226. {
  10227. struct intel_plane *cursor;
  10228. struct intel_plane_state *state;
  10229. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10230. if (cursor == NULL)
  10231. return NULL;
  10232. state = intel_create_plane_state(&cursor->base);
  10233. if (!state) {
  10234. kfree(cursor);
  10235. return NULL;
  10236. }
  10237. cursor->base.state = &state->base;
  10238. cursor->can_scale = false;
  10239. cursor->max_downscale = 1;
  10240. cursor->pipe = pipe;
  10241. cursor->plane = pipe;
  10242. cursor->check_plane = intel_check_cursor_plane;
  10243. cursor->commit_plane = intel_commit_cursor_plane;
  10244. drm_universal_plane_init(dev, &cursor->base, 0,
  10245. &intel_plane_funcs,
  10246. intel_cursor_formats,
  10247. ARRAY_SIZE(intel_cursor_formats),
  10248. DRM_PLANE_TYPE_CURSOR);
  10249. if (INTEL_INFO(dev)->gen >= 4) {
  10250. if (!dev->mode_config.rotation_property)
  10251. dev->mode_config.rotation_property =
  10252. drm_mode_create_rotation_property(dev,
  10253. BIT(DRM_ROTATE_0) |
  10254. BIT(DRM_ROTATE_180));
  10255. if (dev->mode_config.rotation_property)
  10256. drm_object_attach_property(&cursor->base.base,
  10257. dev->mode_config.rotation_property,
  10258. state->base.rotation);
  10259. }
  10260. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  10261. return &cursor->base;
  10262. }
  10263. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10264. {
  10265. struct drm_i915_private *dev_priv = dev->dev_private;
  10266. struct intel_crtc *intel_crtc;
  10267. struct intel_crtc_state *crtc_state = NULL;
  10268. struct drm_plane *primary = NULL;
  10269. struct drm_plane *cursor = NULL;
  10270. int i, ret;
  10271. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10272. if (intel_crtc == NULL)
  10273. return;
  10274. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  10275. if (!crtc_state)
  10276. goto fail;
  10277. intel_crtc_set_state(intel_crtc, crtc_state);
  10278. primary = intel_primary_plane_create(dev, pipe);
  10279. if (!primary)
  10280. goto fail;
  10281. cursor = intel_cursor_plane_create(dev, pipe);
  10282. if (!cursor)
  10283. goto fail;
  10284. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10285. cursor, &intel_crtc_funcs);
  10286. if (ret)
  10287. goto fail;
  10288. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10289. for (i = 0; i < 256; i++) {
  10290. intel_crtc->lut_r[i] = i;
  10291. intel_crtc->lut_g[i] = i;
  10292. intel_crtc->lut_b[i] = i;
  10293. }
  10294. /*
  10295. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10296. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10297. */
  10298. intel_crtc->pipe = pipe;
  10299. intel_crtc->plane = pipe;
  10300. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10301. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10302. intel_crtc->plane = !pipe;
  10303. }
  10304. intel_crtc->cursor_base = ~0;
  10305. intel_crtc->cursor_cntl = ~0;
  10306. intel_crtc->cursor_size = ~0;
  10307. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10308. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10309. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10310. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10311. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10312. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10313. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10314. return;
  10315. fail:
  10316. if (primary)
  10317. drm_plane_cleanup(primary);
  10318. if (cursor)
  10319. drm_plane_cleanup(cursor);
  10320. kfree(crtc_state);
  10321. kfree(intel_crtc);
  10322. }
  10323. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10324. {
  10325. struct drm_encoder *encoder = connector->base.encoder;
  10326. struct drm_device *dev = connector->base.dev;
  10327. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10328. if (!encoder || WARN_ON(!encoder->crtc))
  10329. return INVALID_PIPE;
  10330. return to_intel_crtc(encoder->crtc)->pipe;
  10331. }
  10332. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10333. struct drm_file *file)
  10334. {
  10335. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10336. struct drm_crtc *drmmode_crtc;
  10337. struct intel_crtc *crtc;
  10338. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10339. return -ENODEV;
  10340. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10341. if (!drmmode_crtc) {
  10342. DRM_ERROR("no such CRTC id\n");
  10343. return -ENOENT;
  10344. }
  10345. crtc = to_intel_crtc(drmmode_crtc);
  10346. pipe_from_crtc_id->pipe = crtc->pipe;
  10347. return 0;
  10348. }
  10349. static int intel_encoder_clones(struct intel_encoder *encoder)
  10350. {
  10351. struct drm_device *dev = encoder->base.dev;
  10352. struct intel_encoder *source_encoder;
  10353. int index_mask = 0;
  10354. int entry = 0;
  10355. for_each_intel_encoder(dev, source_encoder) {
  10356. if (encoders_cloneable(encoder, source_encoder))
  10357. index_mask |= (1 << entry);
  10358. entry++;
  10359. }
  10360. return index_mask;
  10361. }
  10362. static bool has_edp_a(struct drm_device *dev)
  10363. {
  10364. struct drm_i915_private *dev_priv = dev->dev_private;
  10365. if (!IS_MOBILE(dev))
  10366. return false;
  10367. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10368. return false;
  10369. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10370. return false;
  10371. return true;
  10372. }
  10373. static bool intel_crt_present(struct drm_device *dev)
  10374. {
  10375. struct drm_i915_private *dev_priv = dev->dev_private;
  10376. if (INTEL_INFO(dev)->gen >= 9)
  10377. return false;
  10378. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10379. return false;
  10380. if (IS_CHERRYVIEW(dev))
  10381. return false;
  10382. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10383. return false;
  10384. return true;
  10385. }
  10386. static void intel_setup_outputs(struct drm_device *dev)
  10387. {
  10388. struct drm_i915_private *dev_priv = dev->dev_private;
  10389. struct intel_encoder *encoder;
  10390. struct drm_connector *connector;
  10391. bool dpd_is_edp = false;
  10392. intel_lvds_init(dev);
  10393. if (intel_crt_present(dev))
  10394. intel_crt_init(dev);
  10395. if (HAS_DDI(dev)) {
  10396. int found;
  10397. /* Haswell uses DDI functions to detect digital outputs */
  10398. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10399. /* DDI A only supports eDP */
  10400. if (found)
  10401. intel_ddi_init(dev, PORT_A);
  10402. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10403. * register */
  10404. found = I915_READ(SFUSE_STRAP);
  10405. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10406. intel_ddi_init(dev, PORT_B);
  10407. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10408. intel_ddi_init(dev, PORT_C);
  10409. if (found & SFUSE_STRAP_DDID_DETECTED)
  10410. intel_ddi_init(dev, PORT_D);
  10411. } else if (HAS_PCH_SPLIT(dev)) {
  10412. int found;
  10413. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10414. if (has_edp_a(dev))
  10415. intel_dp_init(dev, DP_A, PORT_A);
  10416. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10417. /* PCH SDVOB multiplex with HDMIB */
  10418. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10419. if (!found)
  10420. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10421. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10422. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10423. }
  10424. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10425. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10426. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10427. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10428. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10429. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10430. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10431. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10432. } else if (IS_VALLEYVIEW(dev)) {
  10433. /*
  10434. * The DP_DETECTED bit is the latched state of the DDC
  10435. * SDA pin at boot. However since eDP doesn't require DDC
  10436. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10437. * eDP ports may have been muxed to an alternate function.
  10438. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10439. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10440. * detect eDP ports.
  10441. */
  10442. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  10443. !intel_dp_is_edp(dev, PORT_B))
  10444. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10445. PORT_B);
  10446. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10447. intel_dp_is_edp(dev, PORT_B))
  10448. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10449. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  10450. !intel_dp_is_edp(dev, PORT_C))
  10451. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10452. PORT_C);
  10453. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10454. intel_dp_is_edp(dev, PORT_C))
  10455. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10456. if (IS_CHERRYVIEW(dev)) {
  10457. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10458. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10459. PORT_D);
  10460. /* eDP not supported on port D, so don't check VBT */
  10461. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10462. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10463. }
  10464. intel_dsi_init(dev);
  10465. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10466. bool found = false;
  10467. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10468. DRM_DEBUG_KMS("probing SDVOB\n");
  10469. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10470. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10471. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10472. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10473. }
  10474. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10475. intel_dp_init(dev, DP_B, PORT_B);
  10476. }
  10477. /* Before G4X SDVOC doesn't have its own detect register */
  10478. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10479. DRM_DEBUG_KMS("probing SDVOC\n");
  10480. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10481. }
  10482. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10483. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10484. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10485. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10486. }
  10487. if (SUPPORTS_INTEGRATED_DP(dev))
  10488. intel_dp_init(dev, DP_C, PORT_C);
  10489. }
  10490. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10491. (I915_READ(DP_D) & DP_DETECTED))
  10492. intel_dp_init(dev, DP_D, PORT_D);
  10493. } else if (IS_GEN2(dev))
  10494. intel_dvo_init(dev);
  10495. if (SUPPORTS_TV(dev))
  10496. intel_tv_init(dev);
  10497. /*
  10498. * FIXME: We don't have full atomic support yet, but we want to be
  10499. * able to enable/test plane updates via the atomic interface in the
  10500. * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
  10501. * will take some atomic codepaths to lookup properties during
  10502. * drmModeGetConnector() that unconditionally dereference
  10503. * connector->state.
  10504. *
  10505. * We create a dummy connector state here for each connector to ensure
  10506. * the DRM core doesn't try to dereference a NULL connector->state.
  10507. * The actual connector properties will never be updated or contain
  10508. * useful information, but since we're doing this specifically for
  10509. * testing/debug of the plane operations (and only when a specific
  10510. * kernel module option is given), that shouldn't really matter.
  10511. *
  10512. * Once atomic support for crtc's + connectors lands, this loop should
  10513. * be removed since we'll be setting up real connector state, which
  10514. * will contain Intel-specific properties.
  10515. */
  10516. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  10517. list_for_each_entry(connector,
  10518. &dev->mode_config.connector_list,
  10519. head) {
  10520. if (!WARN_ON(connector->state)) {
  10521. connector->state =
  10522. kzalloc(sizeof(*connector->state),
  10523. GFP_KERNEL);
  10524. }
  10525. }
  10526. }
  10527. intel_psr_init(dev);
  10528. for_each_intel_encoder(dev, encoder) {
  10529. encoder->base.possible_crtcs = encoder->crtc_mask;
  10530. encoder->base.possible_clones =
  10531. intel_encoder_clones(encoder);
  10532. }
  10533. intel_init_pch_refclk(dev);
  10534. drm_helper_move_panel_connectors_to_head(dev);
  10535. }
  10536. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10537. {
  10538. struct drm_device *dev = fb->dev;
  10539. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10540. drm_framebuffer_cleanup(fb);
  10541. mutex_lock(&dev->struct_mutex);
  10542. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10543. drm_gem_object_unreference(&intel_fb->obj->base);
  10544. mutex_unlock(&dev->struct_mutex);
  10545. kfree(intel_fb);
  10546. }
  10547. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10548. struct drm_file *file,
  10549. unsigned int *handle)
  10550. {
  10551. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10552. struct drm_i915_gem_object *obj = intel_fb->obj;
  10553. return drm_gem_handle_create(file, &obj->base, handle);
  10554. }
  10555. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10556. .destroy = intel_user_framebuffer_destroy,
  10557. .create_handle = intel_user_framebuffer_create_handle,
  10558. };
  10559. static int intel_framebuffer_init(struct drm_device *dev,
  10560. struct intel_framebuffer *intel_fb,
  10561. struct drm_mode_fb_cmd2 *mode_cmd,
  10562. struct drm_i915_gem_object *obj)
  10563. {
  10564. int aligned_height;
  10565. int pitch_limit;
  10566. int ret;
  10567. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10568. if (obj->tiling_mode == I915_TILING_Y) {
  10569. DRM_DEBUG("hardware does not support tiling Y\n");
  10570. return -EINVAL;
  10571. }
  10572. if (mode_cmd->pitches[0] & 63) {
  10573. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10574. mode_cmd->pitches[0]);
  10575. return -EINVAL;
  10576. }
  10577. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10578. pitch_limit = 32*1024;
  10579. } else if (INTEL_INFO(dev)->gen >= 4) {
  10580. if (obj->tiling_mode)
  10581. pitch_limit = 16*1024;
  10582. else
  10583. pitch_limit = 32*1024;
  10584. } else if (INTEL_INFO(dev)->gen >= 3) {
  10585. if (obj->tiling_mode)
  10586. pitch_limit = 8*1024;
  10587. else
  10588. pitch_limit = 16*1024;
  10589. } else
  10590. /* XXX DSPC is limited to 4k tiled */
  10591. pitch_limit = 8*1024;
  10592. if (mode_cmd->pitches[0] > pitch_limit) {
  10593. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10594. obj->tiling_mode ? "tiled" : "linear",
  10595. mode_cmd->pitches[0], pitch_limit);
  10596. return -EINVAL;
  10597. }
  10598. if (obj->tiling_mode != I915_TILING_NONE &&
  10599. mode_cmd->pitches[0] != obj->stride) {
  10600. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10601. mode_cmd->pitches[0], obj->stride);
  10602. return -EINVAL;
  10603. }
  10604. /* Reject formats not supported by any plane early. */
  10605. switch (mode_cmd->pixel_format) {
  10606. case DRM_FORMAT_C8:
  10607. case DRM_FORMAT_RGB565:
  10608. case DRM_FORMAT_XRGB8888:
  10609. case DRM_FORMAT_ARGB8888:
  10610. break;
  10611. case DRM_FORMAT_XRGB1555:
  10612. case DRM_FORMAT_ARGB1555:
  10613. if (INTEL_INFO(dev)->gen > 3) {
  10614. DRM_DEBUG("unsupported pixel format: %s\n",
  10615. drm_get_format_name(mode_cmd->pixel_format));
  10616. return -EINVAL;
  10617. }
  10618. break;
  10619. case DRM_FORMAT_XBGR8888:
  10620. case DRM_FORMAT_ABGR8888:
  10621. case DRM_FORMAT_XRGB2101010:
  10622. case DRM_FORMAT_ARGB2101010:
  10623. case DRM_FORMAT_XBGR2101010:
  10624. case DRM_FORMAT_ABGR2101010:
  10625. if (INTEL_INFO(dev)->gen < 4) {
  10626. DRM_DEBUG("unsupported pixel format: %s\n",
  10627. drm_get_format_name(mode_cmd->pixel_format));
  10628. return -EINVAL;
  10629. }
  10630. break;
  10631. case DRM_FORMAT_YUYV:
  10632. case DRM_FORMAT_UYVY:
  10633. case DRM_FORMAT_YVYU:
  10634. case DRM_FORMAT_VYUY:
  10635. if (INTEL_INFO(dev)->gen < 5) {
  10636. DRM_DEBUG("unsupported pixel format: %s\n",
  10637. drm_get_format_name(mode_cmd->pixel_format));
  10638. return -EINVAL;
  10639. }
  10640. break;
  10641. default:
  10642. DRM_DEBUG("unsupported pixel format: %s\n",
  10643. drm_get_format_name(mode_cmd->pixel_format));
  10644. return -EINVAL;
  10645. }
  10646. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10647. if (mode_cmd->offsets[0] != 0)
  10648. return -EINVAL;
  10649. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  10650. obj->tiling_mode);
  10651. /* FIXME drm helper for size checks (especially planar formats)? */
  10652. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10653. return -EINVAL;
  10654. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10655. intel_fb->obj = obj;
  10656. intel_fb->obj->framebuffer_references++;
  10657. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10658. if (ret) {
  10659. DRM_ERROR("framebuffer init failed %d\n", ret);
  10660. return ret;
  10661. }
  10662. return 0;
  10663. }
  10664. static struct drm_framebuffer *
  10665. intel_user_framebuffer_create(struct drm_device *dev,
  10666. struct drm_file *filp,
  10667. struct drm_mode_fb_cmd2 *mode_cmd)
  10668. {
  10669. struct drm_i915_gem_object *obj;
  10670. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10671. mode_cmd->handles[0]));
  10672. if (&obj->base == NULL)
  10673. return ERR_PTR(-ENOENT);
  10674. return intel_framebuffer_create(dev, mode_cmd, obj);
  10675. }
  10676. #ifndef CONFIG_DRM_I915_FBDEV
  10677. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10678. {
  10679. }
  10680. #endif
  10681. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10682. .fb_create = intel_user_framebuffer_create,
  10683. .output_poll_changed = intel_fbdev_output_poll_changed,
  10684. .atomic_check = intel_atomic_check,
  10685. .atomic_commit = intel_atomic_commit,
  10686. };
  10687. /* Set up chip specific display functions */
  10688. static void intel_init_display(struct drm_device *dev)
  10689. {
  10690. struct drm_i915_private *dev_priv = dev->dev_private;
  10691. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10692. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10693. else if (IS_CHERRYVIEW(dev))
  10694. dev_priv->display.find_dpll = chv_find_best_dpll;
  10695. else if (IS_VALLEYVIEW(dev))
  10696. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10697. else if (IS_PINEVIEW(dev))
  10698. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10699. else
  10700. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10701. if (INTEL_INFO(dev)->gen >= 9) {
  10702. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10703. dev_priv->display.get_initial_plane_config =
  10704. skylake_get_initial_plane_config;
  10705. dev_priv->display.crtc_compute_clock =
  10706. haswell_crtc_compute_clock;
  10707. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10708. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10709. dev_priv->display.off = ironlake_crtc_off;
  10710. dev_priv->display.update_primary_plane =
  10711. skylake_update_primary_plane;
  10712. } else if (HAS_DDI(dev)) {
  10713. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10714. dev_priv->display.get_initial_plane_config =
  10715. ironlake_get_initial_plane_config;
  10716. dev_priv->display.crtc_compute_clock =
  10717. haswell_crtc_compute_clock;
  10718. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10719. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10720. dev_priv->display.off = ironlake_crtc_off;
  10721. dev_priv->display.update_primary_plane =
  10722. ironlake_update_primary_plane;
  10723. } else if (HAS_PCH_SPLIT(dev)) {
  10724. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10725. dev_priv->display.get_initial_plane_config =
  10726. ironlake_get_initial_plane_config;
  10727. dev_priv->display.crtc_compute_clock =
  10728. ironlake_crtc_compute_clock;
  10729. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10730. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10731. dev_priv->display.off = ironlake_crtc_off;
  10732. dev_priv->display.update_primary_plane =
  10733. ironlake_update_primary_plane;
  10734. } else if (IS_VALLEYVIEW(dev)) {
  10735. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10736. dev_priv->display.get_initial_plane_config =
  10737. i9xx_get_initial_plane_config;
  10738. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10739. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10740. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10741. dev_priv->display.off = i9xx_crtc_off;
  10742. dev_priv->display.update_primary_plane =
  10743. i9xx_update_primary_plane;
  10744. } else {
  10745. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10746. dev_priv->display.get_initial_plane_config =
  10747. i9xx_get_initial_plane_config;
  10748. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10749. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10750. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10751. dev_priv->display.off = i9xx_crtc_off;
  10752. dev_priv->display.update_primary_plane =
  10753. i9xx_update_primary_plane;
  10754. }
  10755. /* Returns the core display clock speed */
  10756. if (IS_VALLEYVIEW(dev))
  10757. dev_priv->display.get_display_clock_speed =
  10758. valleyview_get_display_clock_speed;
  10759. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10760. dev_priv->display.get_display_clock_speed =
  10761. i945_get_display_clock_speed;
  10762. else if (IS_I915G(dev))
  10763. dev_priv->display.get_display_clock_speed =
  10764. i915_get_display_clock_speed;
  10765. else if (IS_I945GM(dev) || IS_845G(dev))
  10766. dev_priv->display.get_display_clock_speed =
  10767. i9xx_misc_get_display_clock_speed;
  10768. else if (IS_PINEVIEW(dev))
  10769. dev_priv->display.get_display_clock_speed =
  10770. pnv_get_display_clock_speed;
  10771. else if (IS_I915GM(dev))
  10772. dev_priv->display.get_display_clock_speed =
  10773. i915gm_get_display_clock_speed;
  10774. else if (IS_I865G(dev))
  10775. dev_priv->display.get_display_clock_speed =
  10776. i865_get_display_clock_speed;
  10777. else if (IS_I85X(dev))
  10778. dev_priv->display.get_display_clock_speed =
  10779. i855_get_display_clock_speed;
  10780. else /* 852, 830 */
  10781. dev_priv->display.get_display_clock_speed =
  10782. i830_get_display_clock_speed;
  10783. if (IS_GEN5(dev)) {
  10784. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10785. } else if (IS_GEN6(dev)) {
  10786. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10787. } else if (IS_IVYBRIDGE(dev)) {
  10788. /* FIXME: detect B0+ stepping and use auto training */
  10789. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10790. dev_priv->display.modeset_global_resources =
  10791. ivb_modeset_global_resources;
  10792. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10793. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10794. } else if (IS_VALLEYVIEW(dev)) {
  10795. dev_priv->display.modeset_global_resources =
  10796. valleyview_modeset_global_resources;
  10797. }
  10798. /* Default just returns -ENODEV to indicate unsupported */
  10799. dev_priv->display.queue_flip = intel_default_queue_flip;
  10800. switch (INTEL_INFO(dev)->gen) {
  10801. case 2:
  10802. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10803. break;
  10804. case 3:
  10805. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10806. break;
  10807. case 4:
  10808. case 5:
  10809. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10810. break;
  10811. case 6:
  10812. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10813. break;
  10814. case 7:
  10815. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10816. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10817. break;
  10818. case 9:
  10819. dev_priv->display.queue_flip = intel_gen9_queue_flip;
  10820. break;
  10821. }
  10822. intel_panel_init_backlight_funcs(dev);
  10823. mutex_init(&dev_priv->pps_mutex);
  10824. }
  10825. /*
  10826. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10827. * resume, or other times. This quirk makes sure that's the case for
  10828. * affected systems.
  10829. */
  10830. static void quirk_pipea_force(struct drm_device *dev)
  10831. {
  10832. struct drm_i915_private *dev_priv = dev->dev_private;
  10833. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10834. DRM_INFO("applying pipe a force quirk\n");
  10835. }
  10836. static void quirk_pipeb_force(struct drm_device *dev)
  10837. {
  10838. struct drm_i915_private *dev_priv = dev->dev_private;
  10839. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10840. DRM_INFO("applying pipe b force quirk\n");
  10841. }
  10842. /*
  10843. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10844. */
  10845. static void quirk_ssc_force_disable(struct drm_device *dev)
  10846. {
  10847. struct drm_i915_private *dev_priv = dev->dev_private;
  10848. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10849. DRM_INFO("applying lvds SSC disable quirk\n");
  10850. }
  10851. /*
  10852. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10853. * brightness value
  10854. */
  10855. static void quirk_invert_brightness(struct drm_device *dev)
  10856. {
  10857. struct drm_i915_private *dev_priv = dev->dev_private;
  10858. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10859. DRM_INFO("applying inverted panel brightness quirk\n");
  10860. }
  10861. /* Some VBT's incorrectly indicate no backlight is present */
  10862. static void quirk_backlight_present(struct drm_device *dev)
  10863. {
  10864. struct drm_i915_private *dev_priv = dev->dev_private;
  10865. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10866. DRM_INFO("applying backlight present quirk\n");
  10867. }
  10868. struct intel_quirk {
  10869. int device;
  10870. int subsystem_vendor;
  10871. int subsystem_device;
  10872. void (*hook)(struct drm_device *dev);
  10873. };
  10874. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10875. struct intel_dmi_quirk {
  10876. void (*hook)(struct drm_device *dev);
  10877. const struct dmi_system_id (*dmi_id_list)[];
  10878. };
  10879. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10880. {
  10881. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10882. return 1;
  10883. }
  10884. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10885. {
  10886. .dmi_id_list = &(const struct dmi_system_id[]) {
  10887. {
  10888. .callback = intel_dmi_reverse_brightness,
  10889. .ident = "NCR Corporation",
  10890. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10891. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10892. },
  10893. },
  10894. { } /* terminating entry */
  10895. },
  10896. .hook = quirk_invert_brightness,
  10897. },
  10898. };
  10899. static struct intel_quirk intel_quirks[] = {
  10900. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10901. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10902. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10903. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10904. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10905. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10906. /* 830 needs to leave pipe A & dpll A up */
  10907. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10908. /* 830 needs to leave pipe B & dpll B up */
  10909. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10910. /* Lenovo U160 cannot use SSC on LVDS */
  10911. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10912. /* Sony Vaio Y cannot use SSC on LVDS */
  10913. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10914. /* Acer Aspire 5734Z must invert backlight brightness */
  10915. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10916. /* Acer/eMachines G725 */
  10917. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10918. /* Acer/eMachines e725 */
  10919. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10920. /* Acer/Packard Bell NCL20 */
  10921. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10922. /* Acer Aspire 4736Z */
  10923. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10924. /* Acer Aspire 5336 */
  10925. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10926. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10927. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10928. /* Acer C720 Chromebook (Core i3 4005U) */
  10929. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10930. /* Apple Macbook 2,1 (Core 2 T7400) */
  10931. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  10932. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10933. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10934. /* HP Chromebook 14 (Celeron 2955U) */
  10935. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10936. };
  10937. static void intel_init_quirks(struct drm_device *dev)
  10938. {
  10939. struct pci_dev *d = dev->pdev;
  10940. int i;
  10941. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10942. struct intel_quirk *q = &intel_quirks[i];
  10943. if (d->device == q->device &&
  10944. (d->subsystem_vendor == q->subsystem_vendor ||
  10945. q->subsystem_vendor == PCI_ANY_ID) &&
  10946. (d->subsystem_device == q->subsystem_device ||
  10947. q->subsystem_device == PCI_ANY_ID))
  10948. q->hook(dev);
  10949. }
  10950. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10951. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10952. intel_dmi_quirks[i].hook(dev);
  10953. }
  10954. }
  10955. /* Disable the VGA plane that we never use */
  10956. static void i915_disable_vga(struct drm_device *dev)
  10957. {
  10958. struct drm_i915_private *dev_priv = dev->dev_private;
  10959. u8 sr1;
  10960. u32 vga_reg = i915_vgacntrl_reg(dev);
  10961. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10962. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10963. outb(SR01, VGA_SR_INDEX);
  10964. sr1 = inb(VGA_SR_DATA);
  10965. outb(sr1 | 1<<5, VGA_SR_DATA);
  10966. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10967. udelay(300);
  10968. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  10969. POSTING_READ(vga_reg);
  10970. }
  10971. void intel_modeset_init_hw(struct drm_device *dev)
  10972. {
  10973. intel_prepare_ddi(dev);
  10974. if (IS_VALLEYVIEW(dev))
  10975. vlv_update_cdclk(dev);
  10976. intel_init_clock_gating(dev);
  10977. intel_enable_gt_powersave(dev);
  10978. }
  10979. void intel_modeset_init(struct drm_device *dev)
  10980. {
  10981. struct drm_i915_private *dev_priv = dev->dev_private;
  10982. int sprite, ret;
  10983. enum pipe pipe;
  10984. struct intel_crtc *crtc;
  10985. drm_mode_config_init(dev);
  10986. dev->mode_config.min_width = 0;
  10987. dev->mode_config.min_height = 0;
  10988. dev->mode_config.preferred_depth = 24;
  10989. dev->mode_config.prefer_shadow = 1;
  10990. dev->mode_config.funcs = &intel_mode_funcs;
  10991. intel_init_quirks(dev);
  10992. intel_init_pm(dev);
  10993. if (INTEL_INFO(dev)->num_pipes == 0)
  10994. return;
  10995. intel_init_display(dev);
  10996. intel_init_audio(dev);
  10997. if (IS_GEN2(dev)) {
  10998. dev->mode_config.max_width = 2048;
  10999. dev->mode_config.max_height = 2048;
  11000. } else if (IS_GEN3(dev)) {
  11001. dev->mode_config.max_width = 4096;
  11002. dev->mode_config.max_height = 4096;
  11003. } else {
  11004. dev->mode_config.max_width = 8192;
  11005. dev->mode_config.max_height = 8192;
  11006. }
  11007. if (IS_845G(dev) || IS_I865G(dev)) {
  11008. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  11009. dev->mode_config.cursor_height = 1023;
  11010. } else if (IS_GEN2(dev)) {
  11011. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  11012. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  11013. } else {
  11014. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  11015. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  11016. }
  11017. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  11018. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  11019. INTEL_INFO(dev)->num_pipes,
  11020. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  11021. for_each_pipe(dev_priv, pipe) {
  11022. intel_crtc_init(dev, pipe);
  11023. for_each_sprite(pipe, sprite) {
  11024. ret = intel_plane_init(dev, pipe, sprite);
  11025. if (ret)
  11026. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  11027. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  11028. }
  11029. }
  11030. intel_init_dpio(dev);
  11031. intel_shared_dpll_init(dev);
  11032. /* Just disable it once at startup */
  11033. i915_disable_vga(dev);
  11034. intel_setup_outputs(dev);
  11035. /* Just in case the BIOS is doing something questionable. */
  11036. intel_fbc_disable(dev);
  11037. drm_modeset_lock_all(dev);
  11038. intel_modeset_setup_hw_state(dev, false);
  11039. drm_modeset_unlock_all(dev);
  11040. for_each_intel_crtc(dev, crtc) {
  11041. if (!crtc->active)
  11042. continue;
  11043. /*
  11044. * Note that reserving the BIOS fb up front prevents us
  11045. * from stuffing other stolen allocations like the ring
  11046. * on top. This prevents some ugliness at boot time, and
  11047. * can even allow for smooth boot transitions if the BIOS
  11048. * fb is large enough for the active pipe configuration.
  11049. */
  11050. if (dev_priv->display.get_initial_plane_config) {
  11051. dev_priv->display.get_initial_plane_config(crtc,
  11052. &crtc->plane_config);
  11053. /*
  11054. * If the fb is shared between multiple heads, we'll
  11055. * just get the first one.
  11056. */
  11057. intel_find_plane_obj(crtc, &crtc->plane_config);
  11058. }
  11059. }
  11060. }
  11061. static void intel_enable_pipe_a(struct drm_device *dev)
  11062. {
  11063. struct intel_connector *connector;
  11064. struct drm_connector *crt = NULL;
  11065. struct intel_load_detect_pipe load_detect_temp;
  11066. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  11067. /* We can't just switch on the pipe A, we need to set things up with a
  11068. * proper mode and output configuration. As a gross hack, enable pipe A
  11069. * by enabling the load detect pipe once. */
  11070. list_for_each_entry(connector,
  11071. &dev->mode_config.connector_list,
  11072. base.head) {
  11073. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  11074. crt = &connector->base;
  11075. break;
  11076. }
  11077. }
  11078. if (!crt)
  11079. return;
  11080. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  11081. intel_release_load_detect_pipe(crt, &load_detect_temp);
  11082. }
  11083. static bool
  11084. intel_check_plane_mapping(struct intel_crtc *crtc)
  11085. {
  11086. struct drm_device *dev = crtc->base.dev;
  11087. struct drm_i915_private *dev_priv = dev->dev_private;
  11088. u32 reg, val;
  11089. if (INTEL_INFO(dev)->num_pipes == 1)
  11090. return true;
  11091. reg = DSPCNTR(!crtc->plane);
  11092. val = I915_READ(reg);
  11093. if ((val & DISPLAY_PLANE_ENABLE) &&
  11094. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11095. return false;
  11096. return true;
  11097. }
  11098. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11099. {
  11100. struct drm_device *dev = crtc->base.dev;
  11101. struct drm_i915_private *dev_priv = dev->dev_private;
  11102. u32 reg;
  11103. /* Clear any frame start delays used for debugging left by the BIOS */
  11104. reg = PIPECONF(crtc->config->cpu_transcoder);
  11105. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11106. /* restore vblank interrupts to correct state */
  11107. if (crtc->active) {
  11108. update_scanline_offset(crtc);
  11109. drm_vblank_on(dev, crtc->pipe);
  11110. } else
  11111. drm_vblank_off(dev, crtc->pipe);
  11112. /* We need to sanitize the plane -> pipe mapping first because this will
  11113. * disable the crtc (and hence change the state) if it is wrong. Note
  11114. * that gen4+ has a fixed plane -> pipe mapping. */
  11115. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11116. struct intel_connector *connector;
  11117. bool plane;
  11118. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11119. crtc->base.base.id);
  11120. /* Pipe has the wrong plane attached and the plane is active.
  11121. * Temporarily change the plane mapping and disable everything
  11122. * ... */
  11123. plane = crtc->plane;
  11124. crtc->plane = !plane;
  11125. crtc->primary_enabled = true;
  11126. dev_priv->display.crtc_disable(&crtc->base);
  11127. crtc->plane = plane;
  11128. /* ... and break all links. */
  11129. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11130. base.head) {
  11131. if (connector->encoder->base.crtc != &crtc->base)
  11132. continue;
  11133. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11134. connector->base.encoder = NULL;
  11135. }
  11136. /* multiple connectors may have the same encoder:
  11137. * handle them and break crtc link separately */
  11138. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11139. base.head)
  11140. if (connector->encoder->base.crtc == &crtc->base) {
  11141. connector->encoder->base.crtc = NULL;
  11142. connector->encoder->connectors_active = false;
  11143. }
  11144. WARN_ON(crtc->active);
  11145. crtc->base.enabled = false;
  11146. }
  11147. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11148. crtc->pipe == PIPE_A && !crtc->active) {
  11149. /* BIOS forgot to enable pipe A, this mostly happens after
  11150. * resume. Force-enable the pipe to fix this, the update_dpms
  11151. * call below we restore the pipe to the right state, but leave
  11152. * the required bits on. */
  11153. intel_enable_pipe_a(dev);
  11154. }
  11155. /* Adjust the state of the output pipe according to whether we
  11156. * have active connectors/encoders. */
  11157. intel_crtc_update_dpms(&crtc->base);
  11158. if (crtc->active != crtc->base.enabled) {
  11159. struct intel_encoder *encoder;
  11160. /* This can happen either due to bugs in the get_hw_state
  11161. * functions or because the pipe is force-enabled due to the
  11162. * pipe A quirk. */
  11163. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11164. crtc->base.base.id,
  11165. crtc->base.enabled ? "enabled" : "disabled",
  11166. crtc->active ? "enabled" : "disabled");
  11167. crtc->base.enabled = crtc->active;
  11168. /* Because we only establish the connector -> encoder ->
  11169. * crtc links if something is active, this means the
  11170. * crtc is now deactivated. Break the links. connector
  11171. * -> encoder links are only establish when things are
  11172. * actually up, hence no need to break them. */
  11173. WARN_ON(crtc->active);
  11174. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11175. WARN_ON(encoder->connectors_active);
  11176. encoder->base.crtc = NULL;
  11177. }
  11178. }
  11179. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11180. /*
  11181. * We start out with underrun reporting disabled to avoid races.
  11182. * For correct bookkeeping mark this on active crtcs.
  11183. *
  11184. * Also on gmch platforms we dont have any hardware bits to
  11185. * disable the underrun reporting. Which means we need to start
  11186. * out with underrun reporting disabled also on inactive pipes,
  11187. * since otherwise we'll complain about the garbage we read when
  11188. * e.g. coming up after runtime pm.
  11189. *
  11190. * No protection against concurrent access is required - at
  11191. * worst a fifo underrun happens which also sets this to false.
  11192. */
  11193. crtc->cpu_fifo_underrun_disabled = true;
  11194. crtc->pch_fifo_underrun_disabled = true;
  11195. }
  11196. }
  11197. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11198. {
  11199. struct intel_connector *connector;
  11200. struct drm_device *dev = encoder->base.dev;
  11201. /* We need to check both for a crtc link (meaning that the
  11202. * encoder is active and trying to read from a pipe) and the
  11203. * pipe itself being active. */
  11204. bool has_active_crtc = encoder->base.crtc &&
  11205. to_intel_crtc(encoder->base.crtc)->active;
  11206. if (encoder->connectors_active && !has_active_crtc) {
  11207. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11208. encoder->base.base.id,
  11209. encoder->base.name);
  11210. /* Connector is active, but has no active pipe. This is
  11211. * fallout from our resume register restoring. Disable
  11212. * the encoder manually again. */
  11213. if (encoder->base.crtc) {
  11214. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11215. encoder->base.base.id,
  11216. encoder->base.name);
  11217. encoder->disable(encoder);
  11218. if (encoder->post_disable)
  11219. encoder->post_disable(encoder);
  11220. }
  11221. encoder->base.crtc = NULL;
  11222. encoder->connectors_active = false;
  11223. /* Inconsistent output/port/pipe state happens presumably due to
  11224. * a bug in one of the get_hw_state functions. Or someplace else
  11225. * in our code, like the register restore mess on resume. Clamp
  11226. * things to off as a safer default. */
  11227. list_for_each_entry(connector,
  11228. &dev->mode_config.connector_list,
  11229. base.head) {
  11230. if (connector->encoder != encoder)
  11231. continue;
  11232. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11233. connector->base.encoder = NULL;
  11234. }
  11235. }
  11236. /* Enabled encoders without active connectors will be fixed in
  11237. * the crtc fixup. */
  11238. }
  11239. void i915_redisable_vga_power_on(struct drm_device *dev)
  11240. {
  11241. struct drm_i915_private *dev_priv = dev->dev_private;
  11242. u32 vga_reg = i915_vgacntrl_reg(dev);
  11243. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11244. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11245. i915_disable_vga(dev);
  11246. }
  11247. }
  11248. void i915_redisable_vga(struct drm_device *dev)
  11249. {
  11250. struct drm_i915_private *dev_priv = dev->dev_private;
  11251. /* This function can be called both from intel_modeset_setup_hw_state or
  11252. * at a very early point in our resume sequence, where the power well
  11253. * structures are not yet restored. Since this function is at a very
  11254. * paranoid "someone might have enabled VGA while we were not looking"
  11255. * level, just check if the power well is enabled instead of trying to
  11256. * follow the "don't touch the power well if we don't need it" policy
  11257. * the rest of the driver uses. */
  11258. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11259. return;
  11260. i915_redisable_vga_power_on(dev);
  11261. }
  11262. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11263. {
  11264. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11265. if (!crtc->active)
  11266. return false;
  11267. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11268. }
  11269. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11270. {
  11271. struct drm_i915_private *dev_priv = dev->dev_private;
  11272. enum pipe pipe;
  11273. struct intel_crtc *crtc;
  11274. struct intel_encoder *encoder;
  11275. struct intel_connector *connector;
  11276. int i;
  11277. for_each_intel_crtc(dev, crtc) {
  11278. memset(crtc->config, 0, sizeof(*crtc->config));
  11279. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11280. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11281. crtc->config);
  11282. crtc->base.enabled = crtc->active;
  11283. crtc->primary_enabled = primary_get_hw_state(crtc);
  11284. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11285. crtc->base.base.id,
  11286. crtc->active ? "enabled" : "disabled");
  11287. }
  11288. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11289. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11290. pll->on = pll->get_hw_state(dev_priv, pll,
  11291. &pll->config.hw_state);
  11292. pll->active = 0;
  11293. pll->config.crtc_mask = 0;
  11294. for_each_intel_crtc(dev, crtc) {
  11295. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11296. pll->active++;
  11297. pll->config.crtc_mask |= 1 << crtc->pipe;
  11298. }
  11299. }
  11300. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11301. pll->name, pll->config.crtc_mask, pll->on);
  11302. if (pll->config.crtc_mask)
  11303. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11304. }
  11305. for_each_intel_encoder(dev, encoder) {
  11306. pipe = 0;
  11307. if (encoder->get_hw_state(encoder, &pipe)) {
  11308. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11309. encoder->base.crtc = &crtc->base;
  11310. encoder->get_config(encoder, crtc->config);
  11311. } else {
  11312. encoder->base.crtc = NULL;
  11313. }
  11314. encoder->connectors_active = false;
  11315. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11316. encoder->base.base.id,
  11317. encoder->base.name,
  11318. encoder->base.crtc ? "enabled" : "disabled",
  11319. pipe_name(pipe));
  11320. }
  11321. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11322. base.head) {
  11323. if (connector->get_hw_state(connector)) {
  11324. connector->base.dpms = DRM_MODE_DPMS_ON;
  11325. connector->encoder->connectors_active = true;
  11326. connector->base.encoder = &connector->encoder->base;
  11327. } else {
  11328. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11329. connector->base.encoder = NULL;
  11330. }
  11331. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11332. connector->base.base.id,
  11333. connector->base.name,
  11334. connector->base.encoder ? "enabled" : "disabled");
  11335. }
  11336. }
  11337. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11338. * and i915 state tracking structures. */
  11339. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11340. bool force_restore)
  11341. {
  11342. struct drm_i915_private *dev_priv = dev->dev_private;
  11343. enum pipe pipe;
  11344. struct intel_crtc *crtc;
  11345. struct intel_encoder *encoder;
  11346. int i;
  11347. intel_modeset_readout_hw_state(dev);
  11348. /*
  11349. * Now that we have the config, copy it to each CRTC struct
  11350. * Note that this could go away if we move to using crtc_config
  11351. * checking everywhere.
  11352. */
  11353. for_each_intel_crtc(dev, crtc) {
  11354. if (crtc->active && i915.fastboot) {
  11355. intel_mode_from_pipe_config(&crtc->base.mode,
  11356. crtc->config);
  11357. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11358. crtc->base.base.id);
  11359. drm_mode_debug_printmodeline(&crtc->base.mode);
  11360. }
  11361. }
  11362. /* HW state is read out, now we need to sanitize this mess. */
  11363. for_each_intel_encoder(dev, encoder) {
  11364. intel_sanitize_encoder(encoder);
  11365. }
  11366. for_each_pipe(dev_priv, pipe) {
  11367. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11368. intel_sanitize_crtc(crtc);
  11369. intel_dump_pipe_config(crtc, crtc->config,
  11370. "[setup_hw_state]");
  11371. }
  11372. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11373. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11374. if (!pll->on || pll->active)
  11375. continue;
  11376. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11377. pll->disable(dev_priv, pll);
  11378. pll->on = false;
  11379. }
  11380. if (IS_GEN9(dev))
  11381. skl_wm_get_hw_state(dev);
  11382. else if (HAS_PCH_SPLIT(dev))
  11383. ilk_wm_get_hw_state(dev);
  11384. if (force_restore) {
  11385. i915_redisable_vga(dev);
  11386. /*
  11387. * We need to use raw interfaces for restoring state to avoid
  11388. * checking (bogus) intermediate states.
  11389. */
  11390. for_each_pipe(dev_priv, pipe) {
  11391. struct drm_crtc *crtc =
  11392. dev_priv->pipe_to_crtc_mapping[pipe];
  11393. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11394. crtc->primary->fb);
  11395. }
  11396. } else {
  11397. intel_modeset_update_staged_output_state(dev);
  11398. }
  11399. intel_modeset_check_state(dev);
  11400. }
  11401. void intel_modeset_gem_init(struct drm_device *dev)
  11402. {
  11403. struct drm_i915_private *dev_priv = dev->dev_private;
  11404. struct drm_crtc *c;
  11405. struct drm_i915_gem_object *obj;
  11406. mutex_lock(&dev->struct_mutex);
  11407. intel_init_gt_powersave(dev);
  11408. mutex_unlock(&dev->struct_mutex);
  11409. /*
  11410. * There may be no VBT; and if the BIOS enabled SSC we can
  11411. * just keep using it to avoid unnecessary flicker. Whereas if the
  11412. * BIOS isn't using it, don't assume it will work even if the VBT
  11413. * indicates as much.
  11414. */
  11415. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11416. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11417. DREF_SSC1_ENABLE);
  11418. intel_modeset_init_hw(dev);
  11419. intel_setup_overlay(dev);
  11420. /*
  11421. * Make sure any fbs we allocated at startup are properly
  11422. * pinned & fenced. When we do the allocation it's too early
  11423. * for this.
  11424. */
  11425. mutex_lock(&dev->struct_mutex);
  11426. for_each_crtc(dev, c) {
  11427. obj = intel_fb_obj(c->primary->fb);
  11428. if (obj == NULL)
  11429. continue;
  11430. if (intel_pin_and_fence_fb_obj(c->primary,
  11431. c->primary->fb,
  11432. NULL)) {
  11433. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11434. to_intel_crtc(c)->pipe);
  11435. drm_framebuffer_unreference(c->primary->fb);
  11436. c->primary->fb = NULL;
  11437. }
  11438. }
  11439. mutex_unlock(&dev->struct_mutex);
  11440. intel_backlight_register(dev);
  11441. }
  11442. void intel_connector_unregister(struct intel_connector *intel_connector)
  11443. {
  11444. struct drm_connector *connector = &intel_connector->base;
  11445. intel_panel_destroy_backlight(connector);
  11446. drm_connector_unregister(connector);
  11447. }
  11448. void intel_modeset_cleanup(struct drm_device *dev)
  11449. {
  11450. struct drm_i915_private *dev_priv = dev->dev_private;
  11451. struct drm_connector *connector;
  11452. intel_disable_gt_powersave(dev);
  11453. intel_backlight_unregister(dev);
  11454. /*
  11455. * Interrupts and polling as the first thing to avoid creating havoc.
  11456. * Too much stuff here (turning of connectors, ...) would
  11457. * experience fancy races otherwise.
  11458. */
  11459. intel_irq_uninstall(dev_priv);
  11460. /*
  11461. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11462. * poll handlers. Hence disable polling after hpd handling is shut down.
  11463. */
  11464. drm_kms_helper_poll_fini(dev);
  11465. mutex_lock(&dev->struct_mutex);
  11466. intel_unregister_dsm_handler();
  11467. intel_fbc_disable(dev);
  11468. ironlake_teardown_rc6(dev);
  11469. mutex_unlock(&dev->struct_mutex);
  11470. /* flush any delayed tasks or pending work */
  11471. flush_scheduled_work();
  11472. /* destroy the backlight and sysfs files before encoders/connectors */
  11473. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11474. struct intel_connector *intel_connector;
  11475. intel_connector = to_intel_connector(connector);
  11476. intel_connector->unregister(intel_connector);
  11477. }
  11478. drm_mode_config_cleanup(dev);
  11479. intel_cleanup_overlay(dev);
  11480. mutex_lock(&dev->struct_mutex);
  11481. intel_cleanup_gt_powersave(dev);
  11482. mutex_unlock(&dev->struct_mutex);
  11483. }
  11484. /*
  11485. * Return which encoder is currently attached for connector.
  11486. */
  11487. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11488. {
  11489. return &intel_attached_encoder(connector)->base;
  11490. }
  11491. void intel_connector_attach_encoder(struct intel_connector *connector,
  11492. struct intel_encoder *encoder)
  11493. {
  11494. connector->encoder = encoder;
  11495. drm_mode_connector_attach_encoder(&connector->base,
  11496. &encoder->base);
  11497. }
  11498. /*
  11499. * set vga decode state - true == enable VGA decode
  11500. */
  11501. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11502. {
  11503. struct drm_i915_private *dev_priv = dev->dev_private;
  11504. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11505. u16 gmch_ctrl;
  11506. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11507. DRM_ERROR("failed to read control word\n");
  11508. return -EIO;
  11509. }
  11510. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11511. return 0;
  11512. if (state)
  11513. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11514. else
  11515. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11516. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11517. DRM_ERROR("failed to write control word\n");
  11518. return -EIO;
  11519. }
  11520. return 0;
  11521. }
  11522. struct intel_display_error_state {
  11523. u32 power_well_driver;
  11524. int num_transcoders;
  11525. struct intel_cursor_error_state {
  11526. u32 control;
  11527. u32 position;
  11528. u32 base;
  11529. u32 size;
  11530. } cursor[I915_MAX_PIPES];
  11531. struct intel_pipe_error_state {
  11532. bool power_domain_on;
  11533. u32 source;
  11534. u32 stat;
  11535. } pipe[I915_MAX_PIPES];
  11536. struct intel_plane_error_state {
  11537. u32 control;
  11538. u32 stride;
  11539. u32 size;
  11540. u32 pos;
  11541. u32 addr;
  11542. u32 surface;
  11543. u32 tile_offset;
  11544. } plane[I915_MAX_PIPES];
  11545. struct intel_transcoder_error_state {
  11546. bool power_domain_on;
  11547. enum transcoder cpu_transcoder;
  11548. u32 conf;
  11549. u32 htotal;
  11550. u32 hblank;
  11551. u32 hsync;
  11552. u32 vtotal;
  11553. u32 vblank;
  11554. u32 vsync;
  11555. } transcoder[4];
  11556. };
  11557. struct intel_display_error_state *
  11558. intel_display_capture_error_state(struct drm_device *dev)
  11559. {
  11560. struct drm_i915_private *dev_priv = dev->dev_private;
  11561. struct intel_display_error_state *error;
  11562. int transcoders[] = {
  11563. TRANSCODER_A,
  11564. TRANSCODER_B,
  11565. TRANSCODER_C,
  11566. TRANSCODER_EDP,
  11567. };
  11568. int i;
  11569. if (INTEL_INFO(dev)->num_pipes == 0)
  11570. return NULL;
  11571. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11572. if (error == NULL)
  11573. return NULL;
  11574. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11575. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11576. for_each_pipe(dev_priv, i) {
  11577. error->pipe[i].power_domain_on =
  11578. __intel_display_power_is_enabled(dev_priv,
  11579. POWER_DOMAIN_PIPE(i));
  11580. if (!error->pipe[i].power_domain_on)
  11581. continue;
  11582. error->cursor[i].control = I915_READ(CURCNTR(i));
  11583. error->cursor[i].position = I915_READ(CURPOS(i));
  11584. error->cursor[i].base = I915_READ(CURBASE(i));
  11585. error->plane[i].control = I915_READ(DSPCNTR(i));
  11586. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11587. if (INTEL_INFO(dev)->gen <= 3) {
  11588. error->plane[i].size = I915_READ(DSPSIZE(i));
  11589. error->plane[i].pos = I915_READ(DSPPOS(i));
  11590. }
  11591. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11592. error->plane[i].addr = I915_READ(DSPADDR(i));
  11593. if (INTEL_INFO(dev)->gen >= 4) {
  11594. error->plane[i].surface = I915_READ(DSPSURF(i));
  11595. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11596. }
  11597. error->pipe[i].source = I915_READ(PIPESRC(i));
  11598. if (HAS_GMCH_DISPLAY(dev))
  11599. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11600. }
  11601. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11602. if (HAS_DDI(dev_priv->dev))
  11603. error->num_transcoders++; /* Account for eDP. */
  11604. for (i = 0; i < error->num_transcoders; i++) {
  11605. enum transcoder cpu_transcoder = transcoders[i];
  11606. error->transcoder[i].power_domain_on =
  11607. __intel_display_power_is_enabled(dev_priv,
  11608. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11609. if (!error->transcoder[i].power_domain_on)
  11610. continue;
  11611. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11612. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11613. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11614. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11615. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11616. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11617. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11618. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11619. }
  11620. return error;
  11621. }
  11622. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11623. void
  11624. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11625. struct drm_device *dev,
  11626. struct intel_display_error_state *error)
  11627. {
  11628. struct drm_i915_private *dev_priv = dev->dev_private;
  11629. int i;
  11630. if (!error)
  11631. return;
  11632. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11633. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11634. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11635. error->power_well_driver);
  11636. for_each_pipe(dev_priv, i) {
  11637. err_printf(m, "Pipe [%d]:\n", i);
  11638. err_printf(m, " Power: %s\n",
  11639. error->pipe[i].power_domain_on ? "on" : "off");
  11640. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11641. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11642. err_printf(m, "Plane [%d]:\n", i);
  11643. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11644. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11645. if (INTEL_INFO(dev)->gen <= 3) {
  11646. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11647. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11648. }
  11649. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11650. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11651. if (INTEL_INFO(dev)->gen >= 4) {
  11652. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11653. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11654. }
  11655. err_printf(m, "Cursor [%d]:\n", i);
  11656. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11657. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11658. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11659. }
  11660. for (i = 0; i < error->num_transcoders; i++) {
  11661. err_printf(m, "CPU transcoder: %c\n",
  11662. transcoder_name(error->transcoder[i].cpu_transcoder));
  11663. err_printf(m, " Power: %s\n",
  11664. error->transcoder[i].power_domain_on ? "on" : "off");
  11665. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11666. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11667. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11668. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11669. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11670. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11671. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11672. }
  11673. }
  11674. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11675. {
  11676. struct intel_crtc *crtc;
  11677. for_each_intel_crtc(dev, crtc) {
  11678. struct intel_unpin_work *work;
  11679. spin_lock_irq(&dev->event_lock);
  11680. work = crtc->unpin_work;
  11681. if (work && work->event &&
  11682. work->event->base.file_priv == file) {
  11683. kfree(work->event);
  11684. work->event = NULL;
  11685. }
  11686. spin_unlock_irq(&dev->event_lock);
  11687. }
  11688. }