intel_display.c 346 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569
  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 <linux/dma_remapping.h>
  42. #define DIV_ROUND_CLOSEST_ULL(ll, d) \
  43. ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
  44. static void intel_increase_pllclock(struct drm_crtc *crtc);
  45. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  46. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  47. struct intel_crtc_config *pipe_config);
  48. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  49. struct intel_crtc_config *pipe_config);
  50. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  51. int x, int y, struct drm_framebuffer *old_fb);
  52. static int intel_framebuffer_init(struct drm_device *dev,
  53. struct intel_framebuffer *ifb,
  54. struct drm_mode_fb_cmd2 *mode_cmd,
  55. struct drm_i915_gem_object *obj);
  56. static void intel_dp_set_m_n(struct intel_crtc *crtc);
  57. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  58. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  59. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  60. struct intel_link_m_n *m_n);
  61. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  62. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  63. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  64. static void vlv_prepare_pll(struct intel_crtc *crtc);
  65. typedef struct {
  66. int min, max;
  67. } intel_range_t;
  68. typedef struct {
  69. int dot_limit;
  70. int p2_slow, p2_fast;
  71. } intel_p2_t;
  72. typedef struct intel_limit intel_limit_t;
  73. struct intel_limit {
  74. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  75. intel_p2_t p2;
  76. };
  77. int
  78. intel_pch_rawclk(struct drm_device *dev)
  79. {
  80. struct drm_i915_private *dev_priv = dev->dev_private;
  81. WARN_ON(!HAS_PCH_SPLIT(dev));
  82. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  83. }
  84. static inline u32 /* units of 100MHz */
  85. intel_fdi_link_freq(struct drm_device *dev)
  86. {
  87. if (IS_GEN5(dev)) {
  88. struct drm_i915_private *dev_priv = dev->dev_private;
  89. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  90. } else
  91. return 27;
  92. }
  93. static const intel_limit_t intel_limits_i8xx_dac = {
  94. .dot = { .min = 25000, .max = 350000 },
  95. .vco = { .min = 908000, .max = 1512000 },
  96. .n = { .min = 2, .max = 16 },
  97. .m = { .min = 96, .max = 140 },
  98. .m1 = { .min = 18, .max = 26 },
  99. .m2 = { .min = 6, .max = 16 },
  100. .p = { .min = 4, .max = 128 },
  101. .p1 = { .min = 2, .max = 33 },
  102. .p2 = { .dot_limit = 165000,
  103. .p2_slow = 4, .p2_fast = 2 },
  104. };
  105. static const intel_limit_t intel_limits_i8xx_dvo = {
  106. .dot = { .min = 25000, .max = 350000 },
  107. .vco = { .min = 908000, .max = 1512000 },
  108. .n = { .min = 2, .max = 16 },
  109. .m = { .min = 96, .max = 140 },
  110. .m1 = { .min = 18, .max = 26 },
  111. .m2 = { .min = 6, .max = 16 },
  112. .p = { .min = 4, .max = 128 },
  113. .p1 = { .min = 2, .max = 33 },
  114. .p2 = { .dot_limit = 165000,
  115. .p2_slow = 4, .p2_fast = 4 },
  116. };
  117. static const intel_limit_t intel_limits_i8xx_lvds = {
  118. .dot = { .min = 25000, .max = 350000 },
  119. .vco = { .min = 908000, .max = 1512000 },
  120. .n = { .min = 2, .max = 16 },
  121. .m = { .min = 96, .max = 140 },
  122. .m1 = { .min = 18, .max = 26 },
  123. .m2 = { .min = 6, .max = 16 },
  124. .p = { .min = 4, .max = 128 },
  125. .p1 = { .min = 1, .max = 6 },
  126. .p2 = { .dot_limit = 165000,
  127. .p2_slow = 14, .p2_fast = 7 },
  128. };
  129. static const intel_limit_t intel_limits_i9xx_sdvo = {
  130. .dot = { .min = 20000, .max = 400000 },
  131. .vco = { .min = 1400000, .max = 2800000 },
  132. .n = { .min = 1, .max = 6 },
  133. .m = { .min = 70, .max = 120 },
  134. .m1 = { .min = 8, .max = 18 },
  135. .m2 = { .min = 3, .max = 7 },
  136. .p = { .min = 5, .max = 80 },
  137. .p1 = { .min = 1, .max = 8 },
  138. .p2 = { .dot_limit = 200000,
  139. .p2_slow = 10, .p2_fast = 5 },
  140. };
  141. static const intel_limit_t intel_limits_i9xx_lvds = {
  142. .dot = { .min = 20000, .max = 400000 },
  143. .vco = { .min = 1400000, .max = 2800000 },
  144. .n = { .min = 1, .max = 6 },
  145. .m = { .min = 70, .max = 120 },
  146. .m1 = { .min = 8, .max = 18 },
  147. .m2 = { .min = 3, .max = 7 },
  148. .p = { .min = 7, .max = 98 },
  149. .p1 = { .min = 1, .max = 8 },
  150. .p2 = { .dot_limit = 112000,
  151. .p2_slow = 14, .p2_fast = 7 },
  152. };
  153. static const intel_limit_t intel_limits_g4x_sdvo = {
  154. .dot = { .min = 25000, .max = 270000 },
  155. .vco = { .min = 1750000, .max = 3500000},
  156. .n = { .min = 1, .max = 4 },
  157. .m = { .min = 104, .max = 138 },
  158. .m1 = { .min = 17, .max = 23 },
  159. .m2 = { .min = 5, .max = 11 },
  160. .p = { .min = 10, .max = 30 },
  161. .p1 = { .min = 1, .max = 3},
  162. .p2 = { .dot_limit = 270000,
  163. .p2_slow = 10,
  164. .p2_fast = 10
  165. },
  166. };
  167. static const intel_limit_t intel_limits_g4x_hdmi = {
  168. .dot = { .min = 22000, .max = 400000 },
  169. .vco = { .min = 1750000, .max = 3500000},
  170. .n = { .min = 1, .max = 4 },
  171. .m = { .min = 104, .max = 138 },
  172. .m1 = { .min = 16, .max = 23 },
  173. .m2 = { .min = 5, .max = 11 },
  174. .p = { .min = 5, .max = 80 },
  175. .p1 = { .min = 1, .max = 8},
  176. .p2 = { .dot_limit = 165000,
  177. .p2_slow = 10, .p2_fast = 5 },
  178. };
  179. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  180. .dot = { .min = 20000, .max = 115000 },
  181. .vco = { .min = 1750000, .max = 3500000 },
  182. .n = { .min = 1, .max = 3 },
  183. .m = { .min = 104, .max = 138 },
  184. .m1 = { .min = 17, .max = 23 },
  185. .m2 = { .min = 5, .max = 11 },
  186. .p = { .min = 28, .max = 112 },
  187. .p1 = { .min = 2, .max = 8 },
  188. .p2 = { .dot_limit = 0,
  189. .p2_slow = 14, .p2_fast = 14
  190. },
  191. };
  192. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  193. .dot = { .min = 80000, .max = 224000 },
  194. .vco = { .min = 1750000, .max = 3500000 },
  195. .n = { .min = 1, .max = 3 },
  196. .m = { .min = 104, .max = 138 },
  197. .m1 = { .min = 17, .max = 23 },
  198. .m2 = { .min = 5, .max = 11 },
  199. .p = { .min = 14, .max = 42 },
  200. .p1 = { .min = 2, .max = 6 },
  201. .p2 = { .dot_limit = 0,
  202. .p2_slow = 7, .p2_fast = 7
  203. },
  204. };
  205. static const intel_limit_t intel_limits_pineview_sdvo = {
  206. .dot = { .min = 20000, .max = 400000},
  207. .vco = { .min = 1700000, .max = 3500000 },
  208. /* Pineview's Ncounter is a ring counter */
  209. .n = { .min = 3, .max = 6 },
  210. .m = { .min = 2, .max = 256 },
  211. /* Pineview only has one combined m divider, which we treat as m2. */
  212. .m1 = { .min = 0, .max = 0 },
  213. .m2 = { .min = 0, .max = 254 },
  214. .p = { .min = 5, .max = 80 },
  215. .p1 = { .min = 1, .max = 8 },
  216. .p2 = { .dot_limit = 200000,
  217. .p2_slow = 10, .p2_fast = 5 },
  218. };
  219. static const intel_limit_t intel_limits_pineview_lvds = {
  220. .dot = { .min = 20000, .max = 400000 },
  221. .vco = { .min = 1700000, .max = 3500000 },
  222. .n = { .min = 3, .max = 6 },
  223. .m = { .min = 2, .max = 256 },
  224. .m1 = { .min = 0, .max = 0 },
  225. .m2 = { .min = 0, .max = 254 },
  226. .p = { .min = 7, .max = 112 },
  227. .p1 = { .min = 1, .max = 8 },
  228. .p2 = { .dot_limit = 112000,
  229. .p2_slow = 14, .p2_fast = 14 },
  230. };
  231. /* Ironlake / Sandybridge
  232. *
  233. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  234. * the range value for them is (actual_value - 2).
  235. */
  236. static const intel_limit_t intel_limits_ironlake_dac = {
  237. .dot = { .min = 25000, .max = 350000 },
  238. .vco = { .min = 1760000, .max = 3510000 },
  239. .n = { .min = 1, .max = 5 },
  240. .m = { .min = 79, .max = 127 },
  241. .m1 = { .min = 12, .max = 22 },
  242. .m2 = { .min = 5, .max = 9 },
  243. .p = { .min = 5, .max = 80 },
  244. .p1 = { .min = 1, .max = 8 },
  245. .p2 = { .dot_limit = 225000,
  246. .p2_slow = 10, .p2_fast = 5 },
  247. };
  248. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  249. .dot = { .min = 25000, .max = 350000 },
  250. .vco = { .min = 1760000, .max = 3510000 },
  251. .n = { .min = 1, .max = 3 },
  252. .m = { .min = 79, .max = 118 },
  253. .m1 = { .min = 12, .max = 22 },
  254. .m2 = { .min = 5, .max = 9 },
  255. .p = { .min = 28, .max = 112 },
  256. .p1 = { .min = 2, .max = 8 },
  257. .p2 = { .dot_limit = 225000,
  258. .p2_slow = 14, .p2_fast = 14 },
  259. };
  260. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  261. .dot = { .min = 25000, .max = 350000 },
  262. .vco = { .min = 1760000, .max = 3510000 },
  263. .n = { .min = 1, .max = 3 },
  264. .m = { .min = 79, .max = 127 },
  265. .m1 = { .min = 12, .max = 22 },
  266. .m2 = { .min = 5, .max = 9 },
  267. .p = { .min = 14, .max = 56 },
  268. .p1 = { .min = 2, .max = 8 },
  269. .p2 = { .dot_limit = 225000,
  270. .p2_slow = 7, .p2_fast = 7 },
  271. };
  272. /* LVDS 100mhz refclk limits. */
  273. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  274. .dot = { .min = 25000, .max = 350000 },
  275. .vco = { .min = 1760000, .max = 3510000 },
  276. .n = { .min = 1, .max = 2 },
  277. .m = { .min = 79, .max = 126 },
  278. .m1 = { .min = 12, .max = 22 },
  279. .m2 = { .min = 5, .max = 9 },
  280. .p = { .min = 28, .max = 112 },
  281. .p1 = { .min = 2, .max = 8 },
  282. .p2 = { .dot_limit = 225000,
  283. .p2_slow = 14, .p2_fast = 14 },
  284. };
  285. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  286. .dot = { .min = 25000, .max = 350000 },
  287. .vco = { .min = 1760000, .max = 3510000 },
  288. .n = { .min = 1, .max = 3 },
  289. .m = { .min = 79, .max = 126 },
  290. .m1 = { .min = 12, .max = 22 },
  291. .m2 = { .min = 5, .max = 9 },
  292. .p = { .min = 14, .max = 42 },
  293. .p1 = { .min = 2, .max = 6 },
  294. .p2 = { .dot_limit = 225000,
  295. .p2_slow = 7, .p2_fast = 7 },
  296. };
  297. static const intel_limit_t intel_limits_vlv = {
  298. /*
  299. * These are the data rate limits (measured in fast clocks)
  300. * since those are the strictest limits we have. The fast
  301. * clock and actual rate limits are more relaxed, so checking
  302. * them would make no difference.
  303. */
  304. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  305. .vco = { .min = 4000000, .max = 6000000 },
  306. .n = { .min = 1, .max = 7 },
  307. .m1 = { .min = 2, .max = 3 },
  308. .m2 = { .min = 11, .max = 156 },
  309. .p1 = { .min = 2, .max = 3 },
  310. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  311. };
  312. static const intel_limit_t intel_limits_chv = {
  313. /*
  314. * These are the data rate limits (measured in fast clocks)
  315. * since those are the strictest limits we have. The fast
  316. * clock and actual rate limits are more relaxed, so checking
  317. * them would make no difference.
  318. */
  319. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  320. .vco = { .min = 4860000, .max = 6700000 },
  321. .n = { .min = 1, .max = 1 },
  322. .m1 = { .min = 2, .max = 2 },
  323. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  324. .p1 = { .min = 2, .max = 4 },
  325. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  326. };
  327. static void vlv_clock(int refclk, intel_clock_t *clock)
  328. {
  329. clock->m = clock->m1 * clock->m2;
  330. clock->p = clock->p1 * clock->p2;
  331. if (WARN_ON(clock->n == 0 || clock->p == 0))
  332. return;
  333. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  334. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  335. }
  336. /**
  337. * Returns whether any output on the specified pipe is of the specified type
  338. */
  339. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  340. {
  341. struct drm_device *dev = crtc->dev;
  342. struct intel_encoder *encoder;
  343. for_each_encoder_on_crtc(dev, crtc, encoder)
  344. if (encoder->type == type)
  345. return true;
  346. return false;
  347. }
  348. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  349. int refclk)
  350. {
  351. struct drm_device *dev = crtc->dev;
  352. const intel_limit_t *limit;
  353. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  354. if (intel_is_dual_link_lvds(dev)) {
  355. if (refclk == 100000)
  356. limit = &intel_limits_ironlake_dual_lvds_100m;
  357. else
  358. limit = &intel_limits_ironlake_dual_lvds;
  359. } else {
  360. if (refclk == 100000)
  361. limit = &intel_limits_ironlake_single_lvds_100m;
  362. else
  363. limit = &intel_limits_ironlake_single_lvds;
  364. }
  365. } else
  366. limit = &intel_limits_ironlake_dac;
  367. return limit;
  368. }
  369. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  370. {
  371. struct drm_device *dev = crtc->dev;
  372. const intel_limit_t *limit;
  373. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  374. if (intel_is_dual_link_lvds(dev))
  375. limit = &intel_limits_g4x_dual_channel_lvds;
  376. else
  377. limit = &intel_limits_g4x_single_channel_lvds;
  378. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  379. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  380. limit = &intel_limits_g4x_hdmi;
  381. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  382. limit = &intel_limits_g4x_sdvo;
  383. } else /* The option is for other outputs */
  384. limit = &intel_limits_i9xx_sdvo;
  385. return limit;
  386. }
  387. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  388. {
  389. struct drm_device *dev = crtc->dev;
  390. const intel_limit_t *limit;
  391. if (HAS_PCH_SPLIT(dev))
  392. limit = intel_ironlake_limit(crtc, refclk);
  393. else if (IS_G4X(dev)) {
  394. limit = intel_g4x_limit(crtc);
  395. } else if (IS_PINEVIEW(dev)) {
  396. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  397. limit = &intel_limits_pineview_lvds;
  398. else
  399. limit = &intel_limits_pineview_sdvo;
  400. } else if (IS_CHERRYVIEW(dev)) {
  401. limit = &intel_limits_chv;
  402. } else if (IS_VALLEYVIEW(dev)) {
  403. limit = &intel_limits_vlv;
  404. } else if (!IS_GEN2(dev)) {
  405. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  406. limit = &intel_limits_i9xx_lvds;
  407. else
  408. limit = &intel_limits_i9xx_sdvo;
  409. } else {
  410. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  411. limit = &intel_limits_i8xx_lvds;
  412. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  413. limit = &intel_limits_i8xx_dvo;
  414. else
  415. limit = &intel_limits_i8xx_dac;
  416. }
  417. return limit;
  418. }
  419. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  420. static void pineview_clock(int refclk, intel_clock_t *clock)
  421. {
  422. clock->m = clock->m2 + 2;
  423. clock->p = clock->p1 * clock->p2;
  424. if (WARN_ON(clock->n == 0 || clock->p == 0))
  425. return;
  426. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  427. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  428. }
  429. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  430. {
  431. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  432. }
  433. static void i9xx_clock(int refclk, intel_clock_t *clock)
  434. {
  435. clock->m = i9xx_dpll_compute_m(clock);
  436. clock->p = clock->p1 * clock->p2;
  437. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  438. return;
  439. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  440. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  441. }
  442. static void chv_clock(int refclk, intel_clock_t *clock)
  443. {
  444. clock->m = clock->m1 * clock->m2;
  445. clock->p = clock->p1 * clock->p2;
  446. if (WARN_ON(clock->n == 0 || clock->p == 0))
  447. return;
  448. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  449. clock->n << 22);
  450. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  451. }
  452. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  453. /**
  454. * Returns whether the given set of divisors are valid for a given refclk with
  455. * the given connectors.
  456. */
  457. static bool intel_PLL_is_valid(struct drm_device *dev,
  458. const intel_limit_t *limit,
  459. const intel_clock_t *clock)
  460. {
  461. if (clock->n < limit->n.min || limit->n.max < clock->n)
  462. INTELPllInvalid("n out of range\n");
  463. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  464. INTELPllInvalid("p1 out of range\n");
  465. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  466. INTELPllInvalid("m2 out of range\n");
  467. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  468. INTELPllInvalid("m1 out of range\n");
  469. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  470. if (clock->m1 <= clock->m2)
  471. INTELPllInvalid("m1 <= m2\n");
  472. if (!IS_VALLEYVIEW(dev)) {
  473. if (clock->p < limit->p.min || limit->p.max < clock->p)
  474. INTELPllInvalid("p out of range\n");
  475. if (clock->m < limit->m.min || limit->m.max < clock->m)
  476. INTELPllInvalid("m out of range\n");
  477. }
  478. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  479. INTELPllInvalid("vco out of range\n");
  480. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  481. * connector, etc., rather than just a single range.
  482. */
  483. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  484. INTELPllInvalid("dot out of range\n");
  485. return true;
  486. }
  487. static bool
  488. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  489. int target, int refclk, intel_clock_t *match_clock,
  490. intel_clock_t *best_clock)
  491. {
  492. struct drm_device *dev = crtc->dev;
  493. intel_clock_t clock;
  494. int err = target;
  495. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  496. /*
  497. * For LVDS just rely on its current settings for dual-channel.
  498. * We haven't figured out how to reliably set up different
  499. * single/dual channel state, if we even can.
  500. */
  501. if (intel_is_dual_link_lvds(dev))
  502. clock.p2 = limit->p2.p2_fast;
  503. else
  504. clock.p2 = limit->p2.p2_slow;
  505. } else {
  506. if (target < limit->p2.dot_limit)
  507. clock.p2 = limit->p2.p2_slow;
  508. else
  509. clock.p2 = limit->p2.p2_fast;
  510. }
  511. memset(best_clock, 0, sizeof(*best_clock));
  512. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  513. clock.m1++) {
  514. for (clock.m2 = limit->m2.min;
  515. clock.m2 <= limit->m2.max; clock.m2++) {
  516. if (clock.m2 >= clock.m1)
  517. break;
  518. for (clock.n = limit->n.min;
  519. clock.n <= limit->n.max; clock.n++) {
  520. for (clock.p1 = limit->p1.min;
  521. clock.p1 <= limit->p1.max; clock.p1++) {
  522. int this_err;
  523. i9xx_clock(refclk, &clock);
  524. if (!intel_PLL_is_valid(dev, limit,
  525. &clock))
  526. continue;
  527. if (match_clock &&
  528. clock.p != match_clock->p)
  529. continue;
  530. this_err = abs(clock.dot - target);
  531. if (this_err < err) {
  532. *best_clock = clock;
  533. err = this_err;
  534. }
  535. }
  536. }
  537. }
  538. }
  539. return (err != target);
  540. }
  541. static bool
  542. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  543. int target, int refclk, intel_clock_t *match_clock,
  544. intel_clock_t *best_clock)
  545. {
  546. struct drm_device *dev = crtc->dev;
  547. intel_clock_t clock;
  548. int err = target;
  549. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  550. /*
  551. * For LVDS just rely on its current settings for dual-channel.
  552. * We haven't figured out how to reliably set up different
  553. * single/dual channel state, if we even can.
  554. */
  555. if (intel_is_dual_link_lvds(dev))
  556. clock.p2 = limit->p2.p2_fast;
  557. else
  558. clock.p2 = limit->p2.p2_slow;
  559. } else {
  560. if (target < limit->p2.dot_limit)
  561. clock.p2 = limit->p2.p2_slow;
  562. else
  563. clock.p2 = limit->p2.p2_fast;
  564. }
  565. memset(best_clock, 0, sizeof(*best_clock));
  566. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  567. clock.m1++) {
  568. for (clock.m2 = limit->m2.min;
  569. clock.m2 <= limit->m2.max; clock.m2++) {
  570. 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. pineview_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. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_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->dev;
  599. intel_clock_t clock;
  600. int max_n;
  601. bool found;
  602. /* approximately equals target * 0.00585 */
  603. int err_most = (target >> 8) + (target >> 9);
  604. found = false;
  605. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  606. if (intel_is_dual_link_lvds(dev))
  607. clock.p2 = limit->p2.p2_fast;
  608. else
  609. clock.p2 = limit->p2.p2_slow;
  610. } else {
  611. if (target < limit->p2.dot_limit)
  612. clock.p2 = limit->p2.p2_slow;
  613. else
  614. clock.p2 = limit->p2.p2_fast;
  615. }
  616. memset(best_clock, 0, sizeof(*best_clock));
  617. max_n = limit->n.max;
  618. /* based on hardware requirement, prefer smaller n to precision */
  619. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  620. /* based on hardware requirement, prefere larger m1,m2 */
  621. for (clock.m1 = limit->m1.max;
  622. clock.m1 >= limit->m1.min; clock.m1--) {
  623. for (clock.m2 = limit->m2.max;
  624. clock.m2 >= limit->m2.min; clock.m2--) {
  625. for (clock.p1 = limit->p1.max;
  626. clock.p1 >= limit->p1.min; clock.p1--) {
  627. int this_err;
  628. i9xx_clock(refclk, &clock);
  629. if (!intel_PLL_is_valid(dev, limit,
  630. &clock))
  631. continue;
  632. this_err = abs(clock.dot - target);
  633. if (this_err < err_most) {
  634. *best_clock = clock;
  635. err_most = this_err;
  636. max_n = clock.n;
  637. found = true;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. return found;
  644. }
  645. static bool
  646. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_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->dev;
  651. intel_clock_t clock;
  652. unsigned int bestppm = 1000000;
  653. /* min update 19.2 MHz */
  654. int max_n = min(limit->n.max, refclk / 19200);
  655. bool found = false;
  656. target *= 5; /* fast clock */
  657. memset(best_clock, 0, sizeof(*best_clock));
  658. /* based on hardware requirement, prefer smaller n to precision */
  659. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  660. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  661. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  662. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  663. clock.p = clock.p1 * clock.p2;
  664. /* based on hardware requirement, prefer bigger m1,m2 values */
  665. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  666. unsigned int ppm, diff;
  667. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  668. refclk * clock.m1);
  669. vlv_clock(refclk, &clock);
  670. if (!intel_PLL_is_valid(dev, limit,
  671. &clock))
  672. continue;
  673. diff = abs(clock.dot - target);
  674. ppm = div_u64(1000000ULL * diff, target);
  675. if (ppm < 100 && clock.p > best_clock->p) {
  676. bestppm = 0;
  677. *best_clock = clock;
  678. found = true;
  679. }
  680. if (bestppm >= 10 && ppm < bestppm - 10) {
  681. bestppm = ppm;
  682. *best_clock = clock;
  683. found = true;
  684. }
  685. }
  686. }
  687. }
  688. }
  689. return found;
  690. }
  691. static bool
  692. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  693. int target, int refclk, intel_clock_t *match_clock,
  694. intel_clock_t *best_clock)
  695. {
  696. struct drm_device *dev = crtc->dev;
  697. intel_clock_t clock;
  698. uint64_t m2;
  699. int found = false;
  700. memset(best_clock, 0, sizeof(*best_clock));
  701. /*
  702. * Based on hardware doc, the n always set to 1, and m1 always
  703. * set to 2. If requires to support 200Mhz refclk, we need to
  704. * revisit this because n may not 1 anymore.
  705. */
  706. clock.n = 1, clock.m1 = 2;
  707. target *= 5; /* fast clock */
  708. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  709. for (clock.p2 = limit->p2.p2_fast;
  710. clock.p2 >= limit->p2.p2_slow;
  711. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  712. clock.p = clock.p1 * clock.p2;
  713. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  714. clock.n) << 22, refclk * clock.m1);
  715. if (m2 > INT_MAX/clock.m1)
  716. continue;
  717. clock.m2 = m2;
  718. chv_clock(refclk, &clock);
  719. if (!intel_PLL_is_valid(dev, limit, &clock))
  720. continue;
  721. /* based on hardware requirement, prefer bigger p
  722. */
  723. if (clock.p > best_clock->p) {
  724. *best_clock = clock;
  725. found = true;
  726. }
  727. }
  728. }
  729. return found;
  730. }
  731. bool intel_crtc_active(struct drm_crtc *crtc)
  732. {
  733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  734. /* Be paranoid as we can arrive here with only partial
  735. * state retrieved from the hardware during setup.
  736. *
  737. * We can ditch the adjusted_mode.crtc_clock check as soon
  738. * as Haswell has gained clock readout/fastboot support.
  739. *
  740. * We can ditch the crtc->primary->fb check as soon as we can
  741. * properly reconstruct framebuffers.
  742. */
  743. return intel_crtc->active && crtc->primary->fb &&
  744. intel_crtc->config.adjusted_mode.crtc_clock;
  745. }
  746. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  747. enum pipe pipe)
  748. {
  749. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  750. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  751. return intel_crtc->config.cpu_transcoder;
  752. }
  753. static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
  754. {
  755. struct drm_i915_private *dev_priv = dev->dev_private;
  756. u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
  757. frame = I915_READ(frame_reg);
  758. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  759. WARN(1, "vblank wait timed out\n");
  760. }
  761. /**
  762. * intel_wait_for_vblank - wait for vblank on a given pipe
  763. * @dev: drm device
  764. * @pipe: pipe to wait for
  765. *
  766. * Wait for vblank to occur on a given pipe. Needed for various bits of
  767. * mode setting code.
  768. */
  769. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  770. {
  771. struct drm_i915_private *dev_priv = dev->dev_private;
  772. int pipestat_reg = PIPESTAT(pipe);
  773. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  774. g4x_wait_for_vblank(dev, pipe);
  775. return;
  776. }
  777. /* Clear existing vblank status. Note this will clear any other
  778. * sticky status fields as well.
  779. *
  780. * This races with i915_driver_irq_handler() with the result
  781. * that either function could miss a vblank event. Here it is not
  782. * fatal, as we will either wait upon the next vblank interrupt or
  783. * timeout. Generally speaking intel_wait_for_vblank() is only
  784. * called during modeset at which time the GPU should be idle and
  785. * should *not* be performing page flips and thus not waiting on
  786. * vblanks...
  787. * Currently, the result of us stealing a vblank from the irq
  788. * handler is that a single frame will be skipped during swapbuffers.
  789. */
  790. I915_WRITE(pipestat_reg,
  791. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  792. /* Wait for vblank interrupt bit to set */
  793. if (wait_for(I915_READ(pipestat_reg) &
  794. PIPE_VBLANK_INTERRUPT_STATUS,
  795. 50))
  796. DRM_DEBUG_KMS("vblank wait timed out\n");
  797. }
  798. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  799. {
  800. struct drm_i915_private *dev_priv = dev->dev_private;
  801. u32 reg = PIPEDSL(pipe);
  802. u32 line1, line2;
  803. u32 line_mask;
  804. if (IS_GEN2(dev))
  805. line_mask = DSL_LINEMASK_GEN2;
  806. else
  807. line_mask = DSL_LINEMASK_GEN3;
  808. line1 = I915_READ(reg) & line_mask;
  809. mdelay(5);
  810. line2 = I915_READ(reg) & line_mask;
  811. return line1 == line2;
  812. }
  813. /*
  814. * intel_wait_for_pipe_off - wait for pipe to turn off
  815. * @dev: drm device
  816. * @pipe: pipe to wait for
  817. *
  818. * After disabling a pipe, we can't wait for vblank in the usual way,
  819. * spinning on the vblank interrupt status bit, since we won't actually
  820. * see an interrupt when the pipe is disabled.
  821. *
  822. * On Gen4 and above:
  823. * wait for the pipe register state bit to turn off
  824. *
  825. * Otherwise:
  826. * wait for the display line value to settle (it usually
  827. * ends up stopping at the start of the next frame).
  828. *
  829. */
  830. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  831. {
  832. struct drm_i915_private *dev_priv = dev->dev_private;
  833. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  834. pipe);
  835. if (INTEL_INFO(dev)->gen >= 4) {
  836. int reg = PIPECONF(cpu_transcoder);
  837. /* Wait for the Pipe State to go off */
  838. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  839. 100))
  840. WARN(1, "pipe_off wait timed out\n");
  841. } else {
  842. /* Wait for the display line to settle */
  843. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  844. WARN(1, "pipe_off wait timed out\n");
  845. }
  846. }
  847. /*
  848. * ibx_digital_port_connected - is the specified port connected?
  849. * @dev_priv: i915 private structure
  850. * @port: the port to test
  851. *
  852. * Returns true if @port is connected, false otherwise.
  853. */
  854. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  855. struct intel_digital_port *port)
  856. {
  857. u32 bit;
  858. if (HAS_PCH_IBX(dev_priv->dev)) {
  859. switch (port->port) {
  860. case PORT_B:
  861. bit = SDE_PORTB_HOTPLUG;
  862. break;
  863. case PORT_C:
  864. bit = SDE_PORTC_HOTPLUG;
  865. break;
  866. case PORT_D:
  867. bit = SDE_PORTD_HOTPLUG;
  868. break;
  869. default:
  870. return true;
  871. }
  872. } else {
  873. switch (port->port) {
  874. case PORT_B:
  875. bit = SDE_PORTB_HOTPLUG_CPT;
  876. break;
  877. case PORT_C:
  878. bit = SDE_PORTC_HOTPLUG_CPT;
  879. break;
  880. case PORT_D:
  881. bit = SDE_PORTD_HOTPLUG_CPT;
  882. break;
  883. default:
  884. return true;
  885. }
  886. }
  887. return I915_READ(SDEISR) & bit;
  888. }
  889. static const char *state_string(bool enabled)
  890. {
  891. return enabled ? "on" : "off";
  892. }
  893. /* Only for pre-ILK configs */
  894. void assert_pll(struct drm_i915_private *dev_priv,
  895. enum pipe pipe, bool state)
  896. {
  897. int reg;
  898. u32 val;
  899. bool cur_state;
  900. reg = DPLL(pipe);
  901. val = I915_READ(reg);
  902. cur_state = !!(val & DPLL_VCO_ENABLE);
  903. WARN(cur_state != state,
  904. "PLL state assertion failure (expected %s, current %s)\n",
  905. state_string(state), state_string(cur_state));
  906. }
  907. /* XXX: the dsi pll is shared between MIPI DSI ports */
  908. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  909. {
  910. u32 val;
  911. bool cur_state;
  912. mutex_lock(&dev_priv->dpio_lock);
  913. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  914. mutex_unlock(&dev_priv->dpio_lock);
  915. cur_state = val & DSI_PLL_VCO_EN;
  916. WARN(cur_state != state,
  917. "DSI PLL state assertion failure (expected %s, current %s)\n",
  918. state_string(state), state_string(cur_state));
  919. }
  920. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  921. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  922. struct intel_shared_dpll *
  923. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  924. {
  925. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  926. if (crtc->config.shared_dpll < 0)
  927. return NULL;
  928. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  929. }
  930. /* For ILK+ */
  931. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  932. struct intel_shared_dpll *pll,
  933. bool state)
  934. {
  935. bool cur_state;
  936. struct intel_dpll_hw_state hw_state;
  937. if (HAS_PCH_LPT(dev_priv->dev)) {
  938. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  939. return;
  940. }
  941. if (WARN (!pll,
  942. "asserting DPLL %s with no DPLL\n", state_string(state)))
  943. return;
  944. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  945. WARN(cur_state != state,
  946. "%s assertion failure (expected %s, current %s)\n",
  947. pll->name, state_string(state), state_string(cur_state));
  948. }
  949. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  950. enum pipe pipe, bool state)
  951. {
  952. int reg;
  953. u32 val;
  954. bool cur_state;
  955. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  956. pipe);
  957. if (HAS_DDI(dev_priv->dev)) {
  958. /* DDI does not have a specific FDI_TX register */
  959. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  960. val = I915_READ(reg);
  961. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  962. } else {
  963. reg = FDI_TX_CTL(pipe);
  964. val = I915_READ(reg);
  965. cur_state = !!(val & FDI_TX_ENABLE);
  966. }
  967. WARN(cur_state != state,
  968. "FDI TX state assertion failure (expected %s, current %s)\n",
  969. state_string(state), state_string(cur_state));
  970. }
  971. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  972. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  973. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  974. enum pipe pipe, bool state)
  975. {
  976. int reg;
  977. u32 val;
  978. bool cur_state;
  979. reg = FDI_RX_CTL(pipe);
  980. val = I915_READ(reg);
  981. cur_state = !!(val & FDI_RX_ENABLE);
  982. WARN(cur_state != state,
  983. "FDI RX state assertion failure (expected %s, current %s)\n",
  984. state_string(state), state_string(cur_state));
  985. }
  986. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  987. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  988. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  989. enum pipe pipe)
  990. {
  991. int reg;
  992. u32 val;
  993. /* ILK FDI PLL is always enabled */
  994. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  995. return;
  996. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  997. if (HAS_DDI(dev_priv->dev))
  998. return;
  999. reg = FDI_TX_CTL(pipe);
  1000. val = I915_READ(reg);
  1001. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1002. }
  1003. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1004. enum pipe pipe, bool state)
  1005. {
  1006. int reg;
  1007. u32 val;
  1008. bool cur_state;
  1009. reg = FDI_RX_CTL(pipe);
  1010. val = I915_READ(reg);
  1011. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1012. WARN(cur_state != state,
  1013. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1014. state_string(state), state_string(cur_state));
  1015. }
  1016. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1017. enum pipe pipe)
  1018. {
  1019. int pp_reg, lvds_reg;
  1020. u32 val;
  1021. enum pipe panel_pipe = PIPE_A;
  1022. bool locked = true;
  1023. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1024. pp_reg = PCH_PP_CONTROL;
  1025. lvds_reg = PCH_LVDS;
  1026. } else {
  1027. pp_reg = PP_CONTROL;
  1028. lvds_reg = LVDS;
  1029. }
  1030. val = I915_READ(pp_reg);
  1031. if (!(val & PANEL_POWER_ON) ||
  1032. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  1033. locked = false;
  1034. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  1035. panel_pipe = PIPE_B;
  1036. WARN(panel_pipe == pipe && locked,
  1037. "panel assertion failure, pipe %c regs locked\n",
  1038. pipe_name(pipe));
  1039. }
  1040. static void assert_cursor(struct drm_i915_private *dev_priv,
  1041. enum pipe pipe, bool state)
  1042. {
  1043. struct drm_device *dev = dev_priv->dev;
  1044. bool cur_state;
  1045. if (IS_845G(dev) || IS_I865G(dev))
  1046. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1047. else
  1048. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1049. WARN(cur_state != state,
  1050. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1051. pipe_name(pipe), state_string(state), state_string(cur_state));
  1052. }
  1053. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1054. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1055. void assert_pipe(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, bool state)
  1057. {
  1058. int reg;
  1059. u32 val;
  1060. bool cur_state;
  1061. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1062. pipe);
  1063. /* if we need the pipe A quirk it must be always on */
  1064. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  1065. state = true;
  1066. if (!intel_display_power_enabled(dev_priv,
  1067. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1068. cur_state = false;
  1069. } else {
  1070. reg = PIPECONF(cpu_transcoder);
  1071. val = I915_READ(reg);
  1072. cur_state = !!(val & PIPECONF_ENABLE);
  1073. }
  1074. WARN(cur_state != state,
  1075. "pipe %c assertion failure (expected %s, current %s)\n",
  1076. pipe_name(pipe), state_string(state), state_string(cur_state));
  1077. }
  1078. static void assert_plane(struct drm_i915_private *dev_priv,
  1079. enum plane plane, bool state)
  1080. {
  1081. int reg;
  1082. u32 val;
  1083. bool cur_state;
  1084. reg = DSPCNTR(plane);
  1085. val = I915_READ(reg);
  1086. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1087. WARN(cur_state != state,
  1088. "plane %c assertion failure (expected %s, current %s)\n",
  1089. plane_name(plane), state_string(state), state_string(cur_state));
  1090. }
  1091. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1092. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1093. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1094. enum pipe pipe)
  1095. {
  1096. struct drm_device *dev = dev_priv->dev;
  1097. int reg, i;
  1098. u32 val;
  1099. int cur_pipe;
  1100. /* Primary planes are fixed to pipes on gen4+ */
  1101. if (INTEL_INFO(dev)->gen >= 4) {
  1102. reg = DSPCNTR(pipe);
  1103. val = I915_READ(reg);
  1104. WARN(val & DISPLAY_PLANE_ENABLE,
  1105. "plane %c assertion failure, should be disabled but not\n",
  1106. plane_name(pipe));
  1107. return;
  1108. }
  1109. /* Need to check both planes against the pipe */
  1110. for_each_pipe(i) {
  1111. reg = DSPCNTR(i);
  1112. val = I915_READ(reg);
  1113. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1114. DISPPLANE_SEL_PIPE_SHIFT;
  1115. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1116. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1117. plane_name(i), pipe_name(pipe));
  1118. }
  1119. }
  1120. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1121. enum pipe pipe)
  1122. {
  1123. struct drm_device *dev = dev_priv->dev;
  1124. int reg, sprite;
  1125. u32 val;
  1126. if (IS_VALLEYVIEW(dev)) {
  1127. for_each_sprite(pipe, sprite) {
  1128. reg = SPCNTR(pipe, sprite);
  1129. val = I915_READ(reg);
  1130. WARN(val & SP_ENABLE,
  1131. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1132. sprite_name(pipe, sprite), pipe_name(pipe));
  1133. }
  1134. } else if (INTEL_INFO(dev)->gen >= 7) {
  1135. reg = SPRCTL(pipe);
  1136. val = I915_READ(reg);
  1137. WARN(val & SPRITE_ENABLE,
  1138. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1139. plane_name(pipe), pipe_name(pipe));
  1140. } else if (INTEL_INFO(dev)->gen >= 5) {
  1141. reg = DVSCNTR(pipe);
  1142. val = I915_READ(reg);
  1143. WARN(val & DVS_ENABLE,
  1144. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1145. plane_name(pipe), pipe_name(pipe));
  1146. }
  1147. }
  1148. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1149. {
  1150. u32 val;
  1151. bool enabled;
  1152. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1153. val = I915_READ(PCH_DREF_CONTROL);
  1154. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1155. DREF_SUPERSPREAD_SOURCE_MASK));
  1156. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1157. }
  1158. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1159. enum pipe pipe)
  1160. {
  1161. int reg;
  1162. u32 val;
  1163. bool enabled;
  1164. reg = PCH_TRANSCONF(pipe);
  1165. val = I915_READ(reg);
  1166. enabled = !!(val & TRANS_ENABLE);
  1167. WARN(enabled,
  1168. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1169. pipe_name(pipe));
  1170. }
  1171. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1172. enum pipe pipe, u32 port_sel, u32 val)
  1173. {
  1174. if ((val & DP_PORT_EN) == 0)
  1175. return false;
  1176. if (HAS_PCH_CPT(dev_priv->dev)) {
  1177. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1178. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1179. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1180. return false;
  1181. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1182. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1183. return false;
  1184. } else {
  1185. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1186. return false;
  1187. }
  1188. return true;
  1189. }
  1190. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1191. enum pipe pipe, u32 val)
  1192. {
  1193. if ((val & SDVO_ENABLE) == 0)
  1194. return false;
  1195. if (HAS_PCH_CPT(dev_priv->dev)) {
  1196. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1197. return false;
  1198. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1199. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1200. return false;
  1201. } else {
  1202. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1203. return false;
  1204. }
  1205. return true;
  1206. }
  1207. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1208. enum pipe pipe, u32 val)
  1209. {
  1210. if ((val & LVDS_PORT_EN) == 0)
  1211. return false;
  1212. if (HAS_PCH_CPT(dev_priv->dev)) {
  1213. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1214. return false;
  1215. } else {
  1216. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1217. return false;
  1218. }
  1219. return true;
  1220. }
  1221. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1222. enum pipe pipe, u32 val)
  1223. {
  1224. if ((val & ADPA_DAC_ENABLE) == 0)
  1225. return false;
  1226. if (HAS_PCH_CPT(dev_priv->dev)) {
  1227. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, int reg, u32 port_sel)
  1237. {
  1238. u32 val = I915_READ(reg);
  1239. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1240. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1241. reg, pipe_name(pipe));
  1242. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1243. && (val & DP_PIPEB_SELECT),
  1244. "IBX PCH dp port still using transcoder B\n");
  1245. }
  1246. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1247. enum pipe pipe, int reg)
  1248. {
  1249. u32 val = I915_READ(reg);
  1250. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1251. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1252. reg, pipe_name(pipe));
  1253. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1254. && (val & SDVO_PIPE_B_SELECT),
  1255. "IBX PCH hdmi port still using transcoder B\n");
  1256. }
  1257. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1258. enum pipe pipe)
  1259. {
  1260. int reg;
  1261. u32 val;
  1262. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1263. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1264. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1265. reg = PCH_ADPA;
  1266. val = I915_READ(reg);
  1267. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1268. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1269. pipe_name(pipe));
  1270. reg = PCH_LVDS;
  1271. val = I915_READ(reg);
  1272. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1273. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1274. pipe_name(pipe));
  1275. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1276. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1277. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1278. }
  1279. static void intel_init_dpio(struct drm_device *dev)
  1280. {
  1281. struct drm_i915_private *dev_priv = dev->dev_private;
  1282. if (!IS_VALLEYVIEW(dev))
  1283. return;
  1284. /*
  1285. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1286. * CHV x1 PHY (DP/HDMI D)
  1287. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1288. */
  1289. if (IS_CHERRYVIEW(dev)) {
  1290. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1291. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1292. } else {
  1293. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1294. }
  1295. }
  1296. static void intel_reset_dpio(struct drm_device *dev)
  1297. {
  1298. struct drm_i915_private *dev_priv = dev->dev_private;
  1299. if (!IS_VALLEYVIEW(dev))
  1300. return;
  1301. if (IS_CHERRYVIEW(dev)) {
  1302. enum dpio_phy phy;
  1303. u32 val;
  1304. for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
  1305. /* Poll for phypwrgood signal */
  1306. if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
  1307. PHY_POWERGOOD(phy), 1))
  1308. DRM_ERROR("Display PHY %d is not power up\n", phy);
  1309. /*
  1310. * Deassert common lane reset for PHY.
  1311. *
  1312. * This should only be done on init and resume from S3
  1313. * with both PLLs disabled, or we risk losing DPIO and
  1314. * PLL synchronization.
  1315. */
  1316. val = I915_READ(DISPLAY_PHY_CONTROL);
  1317. I915_WRITE(DISPLAY_PHY_CONTROL,
  1318. PHY_COM_LANE_RESET_DEASSERT(phy, val));
  1319. }
  1320. } else {
  1321. /*
  1322. * If DPIO has already been reset, e.g. by BIOS, just skip all
  1323. * this.
  1324. */
  1325. if (I915_READ(DPIO_CTL) & DPIO_CMNRST)
  1326. return;
  1327. /*
  1328. * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
  1329. * Need to assert and de-assert PHY SB reset by gating the
  1330. * common lane power, then un-gating it.
  1331. * Simply ungating isn't enough to reset the PHY enough to get
  1332. * ports and lanes running.
  1333. */
  1334. __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
  1335. false);
  1336. __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
  1337. true);
  1338. }
  1339. }
  1340. static void vlv_enable_pll(struct intel_crtc *crtc)
  1341. {
  1342. struct drm_device *dev = crtc->base.dev;
  1343. struct drm_i915_private *dev_priv = dev->dev_private;
  1344. int reg = DPLL(crtc->pipe);
  1345. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1346. assert_pipe_disabled(dev_priv, crtc->pipe);
  1347. /* No really, not for ILK+ */
  1348. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1349. /* PLL is protected by panel, make sure we can write it */
  1350. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1351. assert_panel_unlocked(dev_priv, crtc->pipe);
  1352. I915_WRITE(reg, dpll);
  1353. POSTING_READ(reg);
  1354. udelay(150);
  1355. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1356. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1357. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1358. POSTING_READ(DPLL_MD(crtc->pipe));
  1359. /* We do this three times for luck */
  1360. I915_WRITE(reg, dpll);
  1361. POSTING_READ(reg);
  1362. udelay(150); /* wait for warmup */
  1363. I915_WRITE(reg, dpll);
  1364. POSTING_READ(reg);
  1365. udelay(150); /* wait for warmup */
  1366. I915_WRITE(reg, dpll);
  1367. POSTING_READ(reg);
  1368. udelay(150); /* wait for warmup */
  1369. }
  1370. static void chv_enable_pll(struct intel_crtc *crtc)
  1371. {
  1372. struct drm_device *dev = crtc->base.dev;
  1373. struct drm_i915_private *dev_priv = dev->dev_private;
  1374. int pipe = crtc->pipe;
  1375. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1376. u32 tmp;
  1377. assert_pipe_disabled(dev_priv, crtc->pipe);
  1378. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1379. mutex_lock(&dev_priv->dpio_lock);
  1380. /* Enable back the 10bit clock to display controller */
  1381. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1382. tmp |= DPIO_DCLKP_EN;
  1383. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1384. /*
  1385. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1386. */
  1387. udelay(1);
  1388. /* Enable PLL */
  1389. I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
  1390. /* Check PLL is locked */
  1391. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1392. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1393. /* not sure when this should be written */
  1394. I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
  1395. POSTING_READ(DPLL_MD(pipe));
  1396. mutex_unlock(&dev_priv->dpio_lock);
  1397. }
  1398. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1399. {
  1400. struct drm_device *dev = crtc->base.dev;
  1401. struct drm_i915_private *dev_priv = dev->dev_private;
  1402. int reg = DPLL(crtc->pipe);
  1403. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1404. assert_pipe_disabled(dev_priv, crtc->pipe);
  1405. /* No really, not for ILK+ */
  1406. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1407. /* PLL is protected by panel, make sure we can write it */
  1408. if (IS_MOBILE(dev) && !IS_I830(dev))
  1409. assert_panel_unlocked(dev_priv, crtc->pipe);
  1410. I915_WRITE(reg, dpll);
  1411. /* Wait for the clocks to stabilize. */
  1412. POSTING_READ(reg);
  1413. udelay(150);
  1414. if (INTEL_INFO(dev)->gen >= 4) {
  1415. I915_WRITE(DPLL_MD(crtc->pipe),
  1416. crtc->config.dpll_hw_state.dpll_md);
  1417. } else {
  1418. /* The pixel multiplier can only be updated once the
  1419. * DPLL is enabled and the clocks are stable.
  1420. *
  1421. * So write it again.
  1422. */
  1423. I915_WRITE(reg, dpll);
  1424. }
  1425. /* We do this three times for luck */
  1426. I915_WRITE(reg, dpll);
  1427. POSTING_READ(reg);
  1428. udelay(150); /* wait for warmup */
  1429. I915_WRITE(reg, dpll);
  1430. POSTING_READ(reg);
  1431. udelay(150); /* wait for warmup */
  1432. I915_WRITE(reg, dpll);
  1433. POSTING_READ(reg);
  1434. udelay(150); /* wait for warmup */
  1435. }
  1436. /**
  1437. * i9xx_disable_pll - disable a PLL
  1438. * @dev_priv: i915 private structure
  1439. * @pipe: pipe PLL to disable
  1440. *
  1441. * Disable the PLL for @pipe, making sure the pipe is off first.
  1442. *
  1443. * Note! This is for pre-ILK only.
  1444. */
  1445. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1446. {
  1447. /* Don't disable pipe A or pipe A PLLs if needed */
  1448. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1449. return;
  1450. /* Make sure the pipe isn't still relying on us */
  1451. assert_pipe_disabled(dev_priv, pipe);
  1452. I915_WRITE(DPLL(pipe), 0);
  1453. POSTING_READ(DPLL(pipe));
  1454. }
  1455. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1456. {
  1457. u32 val = 0;
  1458. /* Make sure the pipe isn't still relying on us */
  1459. assert_pipe_disabled(dev_priv, pipe);
  1460. /*
  1461. * Leave integrated clock source and reference clock enabled for pipe B.
  1462. * The latter is needed for VGA hotplug / manual detection.
  1463. */
  1464. if (pipe == PIPE_B)
  1465. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1466. I915_WRITE(DPLL(pipe), val);
  1467. POSTING_READ(DPLL(pipe));
  1468. }
  1469. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1470. {
  1471. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1472. u32 val;
  1473. /* Make sure the pipe isn't still relying on us */
  1474. assert_pipe_disabled(dev_priv, pipe);
  1475. /* Set PLL en = 0 */
  1476. val = DPLL_SSC_REF_CLOCK_CHV;
  1477. if (pipe != PIPE_A)
  1478. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1479. I915_WRITE(DPLL(pipe), val);
  1480. POSTING_READ(DPLL(pipe));
  1481. mutex_lock(&dev_priv->dpio_lock);
  1482. /* Disable 10bit clock to display controller */
  1483. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1484. val &= ~DPIO_DCLKP_EN;
  1485. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1486. mutex_unlock(&dev_priv->dpio_lock);
  1487. }
  1488. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1489. struct intel_digital_port *dport)
  1490. {
  1491. u32 port_mask;
  1492. int dpll_reg;
  1493. switch (dport->port) {
  1494. case PORT_B:
  1495. port_mask = DPLL_PORTB_READY_MASK;
  1496. dpll_reg = DPLL(0);
  1497. break;
  1498. case PORT_C:
  1499. port_mask = DPLL_PORTC_READY_MASK;
  1500. dpll_reg = DPLL(0);
  1501. break;
  1502. case PORT_D:
  1503. port_mask = DPLL_PORTD_READY_MASK;
  1504. dpll_reg = DPIO_PHY_STATUS;
  1505. break;
  1506. default:
  1507. BUG();
  1508. }
  1509. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1510. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1511. port_name(dport->port), I915_READ(dpll_reg));
  1512. }
  1513. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1514. {
  1515. struct drm_device *dev = crtc->base.dev;
  1516. struct drm_i915_private *dev_priv = dev->dev_private;
  1517. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1518. WARN_ON(!pll->refcount);
  1519. if (pll->active == 0) {
  1520. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1521. WARN_ON(pll->on);
  1522. assert_shared_dpll_disabled(dev_priv, pll);
  1523. pll->mode_set(dev_priv, pll);
  1524. }
  1525. }
  1526. /**
  1527. * intel_enable_shared_dpll - enable PCH PLL
  1528. * @dev_priv: i915 private structure
  1529. * @pipe: pipe PLL to enable
  1530. *
  1531. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1532. * drives the transcoder clock.
  1533. */
  1534. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1535. {
  1536. struct drm_device *dev = crtc->base.dev;
  1537. struct drm_i915_private *dev_priv = dev->dev_private;
  1538. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1539. if (WARN_ON(pll == NULL))
  1540. return;
  1541. if (WARN_ON(pll->refcount == 0))
  1542. return;
  1543. DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
  1544. pll->name, pll->active, pll->on,
  1545. crtc->base.base.id);
  1546. if (pll->active++) {
  1547. WARN_ON(!pll->on);
  1548. assert_shared_dpll_enabled(dev_priv, pll);
  1549. return;
  1550. }
  1551. WARN_ON(pll->on);
  1552. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1553. pll->enable(dev_priv, pll);
  1554. pll->on = true;
  1555. }
  1556. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1557. {
  1558. struct drm_device *dev = crtc->base.dev;
  1559. struct drm_i915_private *dev_priv = dev->dev_private;
  1560. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1561. /* PCH only available on ILK+ */
  1562. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1563. if (WARN_ON(pll == NULL))
  1564. return;
  1565. if (WARN_ON(pll->refcount == 0))
  1566. return;
  1567. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1568. pll->name, pll->active, pll->on,
  1569. crtc->base.base.id);
  1570. if (WARN_ON(pll->active == 0)) {
  1571. assert_shared_dpll_disabled(dev_priv, pll);
  1572. return;
  1573. }
  1574. assert_shared_dpll_enabled(dev_priv, pll);
  1575. WARN_ON(!pll->on);
  1576. if (--pll->active)
  1577. return;
  1578. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1579. pll->disable(dev_priv, pll);
  1580. pll->on = false;
  1581. }
  1582. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1583. enum pipe pipe)
  1584. {
  1585. struct drm_device *dev = dev_priv->dev;
  1586. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1587. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1588. uint32_t reg, val, pipeconf_val;
  1589. /* PCH only available on ILK+ */
  1590. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1591. /* Make sure PCH DPLL is enabled */
  1592. assert_shared_dpll_enabled(dev_priv,
  1593. intel_crtc_to_shared_dpll(intel_crtc));
  1594. /* FDI must be feeding us bits for PCH ports */
  1595. assert_fdi_tx_enabled(dev_priv, pipe);
  1596. assert_fdi_rx_enabled(dev_priv, pipe);
  1597. if (HAS_PCH_CPT(dev)) {
  1598. /* Workaround: Set the timing override bit before enabling the
  1599. * pch transcoder. */
  1600. reg = TRANS_CHICKEN2(pipe);
  1601. val = I915_READ(reg);
  1602. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1603. I915_WRITE(reg, val);
  1604. }
  1605. reg = PCH_TRANSCONF(pipe);
  1606. val = I915_READ(reg);
  1607. pipeconf_val = I915_READ(PIPECONF(pipe));
  1608. if (HAS_PCH_IBX(dev_priv->dev)) {
  1609. /*
  1610. * make the BPC in transcoder be consistent with
  1611. * that in pipeconf reg.
  1612. */
  1613. val &= ~PIPECONF_BPC_MASK;
  1614. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1615. }
  1616. val &= ~TRANS_INTERLACE_MASK;
  1617. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1618. if (HAS_PCH_IBX(dev_priv->dev) &&
  1619. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1620. val |= TRANS_LEGACY_INTERLACED_ILK;
  1621. else
  1622. val |= TRANS_INTERLACED;
  1623. else
  1624. val |= TRANS_PROGRESSIVE;
  1625. I915_WRITE(reg, val | TRANS_ENABLE);
  1626. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1627. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1628. }
  1629. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1630. enum transcoder cpu_transcoder)
  1631. {
  1632. u32 val, pipeconf_val;
  1633. /* PCH only available on ILK+ */
  1634. BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
  1635. /* FDI must be feeding us bits for PCH ports */
  1636. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1637. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1638. /* Workaround: set timing override bit. */
  1639. val = I915_READ(_TRANSA_CHICKEN2);
  1640. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1641. I915_WRITE(_TRANSA_CHICKEN2, val);
  1642. val = TRANS_ENABLE;
  1643. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1644. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1645. PIPECONF_INTERLACED_ILK)
  1646. val |= TRANS_INTERLACED;
  1647. else
  1648. val |= TRANS_PROGRESSIVE;
  1649. I915_WRITE(LPT_TRANSCONF, val);
  1650. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1651. DRM_ERROR("Failed to enable PCH transcoder\n");
  1652. }
  1653. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1654. enum pipe pipe)
  1655. {
  1656. struct drm_device *dev = dev_priv->dev;
  1657. uint32_t reg, val;
  1658. /* FDI relies on the transcoder */
  1659. assert_fdi_tx_disabled(dev_priv, pipe);
  1660. assert_fdi_rx_disabled(dev_priv, pipe);
  1661. /* Ports must be off as well */
  1662. assert_pch_ports_disabled(dev_priv, pipe);
  1663. reg = PCH_TRANSCONF(pipe);
  1664. val = I915_READ(reg);
  1665. val &= ~TRANS_ENABLE;
  1666. I915_WRITE(reg, val);
  1667. /* wait for PCH transcoder off, transcoder state */
  1668. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1669. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1670. if (!HAS_PCH_IBX(dev)) {
  1671. /* Workaround: Clear the timing override chicken bit again. */
  1672. reg = TRANS_CHICKEN2(pipe);
  1673. val = I915_READ(reg);
  1674. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1675. I915_WRITE(reg, val);
  1676. }
  1677. }
  1678. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1679. {
  1680. u32 val;
  1681. val = I915_READ(LPT_TRANSCONF);
  1682. val &= ~TRANS_ENABLE;
  1683. I915_WRITE(LPT_TRANSCONF, val);
  1684. /* wait for PCH transcoder off, transcoder state */
  1685. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1686. DRM_ERROR("Failed to disable PCH transcoder\n");
  1687. /* Workaround: clear timing override bit. */
  1688. val = I915_READ(_TRANSA_CHICKEN2);
  1689. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1690. I915_WRITE(_TRANSA_CHICKEN2, val);
  1691. }
  1692. /**
  1693. * intel_enable_pipe - enable a pipe, asserting requirements
  1694. * @crtc: crtc responsible for the pipe
  1695. *
  1696. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1697. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1698. */
  1699. static void intel_enable_pipe(struct intel_crtc *crtc)
  1700. {
  1701. struct drm_device *dev = crtc->base.dev;
  1702. struct drm_i915_private *dev_priv = dev->dev_private;
  1703. enum pipe pipe = crtc->pipe;
  1704. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1705. pipe);
  1706. enum pipe pch_transcoder;
  1707. int reg;
  1708. u32 val;
  1709. assert_planes_disabled(dev_priv, pipe);
  1710. assert_cursor_disabled(dev_priv, pipe);
  1711. assert_sprites_disabled(dev_priv, pipe);
  1712. if (HAS_PCH_LPT(dev_priv->dev))
  1713. pch_transcoder = TRANSCODER_A;
  1714. else
  1715. pch_transcoder = pipe;
  1716. /*
  1717. * A pipe without a PLL won't actually be able to drive bits from
  1718. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1719. * need the check.
  1720. */
  1721. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1722. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1723. assert_dsi_pll_enabled(dev_priv);
  1724. else
  1725. assert_pll_enabled(dev_priv, pipe);
  1726. else {
  1727. if (crtc->config.has_pch_encoder) {
  1728. /* if driving the PCH, we need FDI enabled */
  1729. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1730. assert_fdi_tx_pll_enabled(dev_priv,
  1731. (enum pipe) cpu_transcoder);
  1732. }
  1733. /* FIXME: assert CPU port conditions for SNB+ */
  1734. }
  1735. reg = PIPECONF(cpu_transcoder);
  1736. val = I915_READ(reg);
  1737. if (val & PIPECONF_ENABLE) {
  1738. WARN_ON(!(pipe == PIPE_A &&
  1739. dev_priv->quirks & QUIRK_PIPEA_FORCE));
  1740. return;
  1741. }
  1742. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1743. POSTING_READ(reg);
  1744. }
  1745. /**
  1746. * intel_disable_pipe - disable a pipe, asserting requirements
  1747. * @dev_priv: i915 private structure
  1748. * @pipe: pipe to disable
  1749. *
  1750. * Disable @pipe, making sure that various hardware specific requirements
  1751. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1752. *
  1753. * @pipe should be %PIPE_A or %PIPE_B.
  1754. *
  1755. * Will wait until the pipe has shut down before returning.
  1756. */
  1757. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1758. enum pipe pipe)
  1759. {
  1760. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1761. pipe);
  1762. int reg;
  1763. u32 val;
  1764. /*
  1765. * Make sure planes won't keep trying to pump pixels to us,
  1766. * or we might hang the display.
  1767. */
  1768. assert_planes_disabled(dev_priv, pipe);
  1769. assert_cursor_disabled(dev_priv, pipe);
  1770. assert_sprites_disabled(dev_priv, pipe);
  1771. /* Don't disable pipe A or pipe A PLLs if needed */
  1772. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1773. return;
  1774. reg = PIPECONF(cpu_transcoder);
  1775. val = I915_READ(reg);
  1776. if ((val & PIPECONF_ENABLE) == 0)
  1777. return;
  1778. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1779. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1780. }
  1781. /*
  1782. * Plane regs are double buffered, going from enabled->disabled needs a
  1783. * trigger in order to latch. The display address reg provides this.
  1784. */
  1785. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1786. enum plane plane)
  1787. {
  1788. struct drm_device *dev = dev_priv->dev;
  1789. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1790. I915_WRITE(reg, I915_READ(reg));
  1791. POSTING_READ(reg);
  1792. }
  1793. /**
  1794. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1795. * @dev_priv: i915 private structure
  1796. * @plane: plane to enable
  1797. * @pipe: pipe being fed
  1798. *
  1799. * Enable @plane on @pipe, making sure that @pipe is running first.
  1800. */
  1801. static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1802. enum plane plane, enum pipe pipe)
  1803. {
  1804. struct drm_device *dev = dev_priv->dev;
  1805. struct intel_crtc *intel_crtc =
  1806. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1807. int reg;
  1808. u32 val;
  1809. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1810. assert_pipe_enabled(dev_priv, pipe);
  1811. if (intel_crtc->primary_enabled)
  1812. return;
  1813. intel_crtc->primary_enabled = true;
  1814. reg = DSPCNTR(plane);
  1815. val = I915_READ(reg);
  1816. WARN_ON(val & DISPLAY_PLANE_ENABLE);
  1817. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1818. intel_flush_primary_plane(dev_priv, plane);
  1819. /*
  1820. * BDW signals flip done immediately if the plane
  1821. * is disabled, even if the plane enable is already
  1822. * armed to occur at the next vblank :(
  1823. */
  1824. if (IS_BROADWELL(dev))
  1825. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1826. }
  1827. /**
  1828. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1829. * @dev_priv: i915 private structure
  1830. * @plane: plane to disable
  1831. * @pipe: pipe consuming the data
  1832. *
  1833. * Disable @plane; should be an independent operation.
  1834. */
  1835. static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1836. enum plane plane, enum pipe pipe)
  1837. {
  1838. struct intel_crtc *intel_crtc =
  1839. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1840. int reg;
  1841. u32 val;
  1842. if (!intel_crtc->primary_enabled)
  1843. return;
  1844. intel_crtc->primary_enabled = false;
  1845. reg = DSPCNTR(plane);
  1846. val = I915_READ(reg);
  1847. WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
  1848. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1849. intel_flush_primary_plane(dev_priv, plane);
  1850. }
  1851. static bool need_vtd_wa(struct drm_device *dev)
  1852. {
  1853. #ifdef CONFIG_INTEL_IOMMU
  1854. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1855. return true;
  1856. #endif
  1857. return false;
  1858. }
  1859. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1860. {
  1861. int tile_height;
  1862. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1863. return ALIGN(height, tile_height);
  1864. }
  1865. int
  1866. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1867. struct drm_i915_gem_object *obj,
  1868. struct intel_engine_cs *pipelined)
  1869. {
  1870. struct drm_i915_private *dev_priv = dev->dev_private;
  1871. u32 alignment;
  1872. int ret;
  1873. switch (obj->tiling_mode) {
  1874. case I915_TILING_NONE:
  1875. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1876. alignment = 128 * 1024;
  1877. else if (INTEL_INFO(dev)->gen >= 4)
  1878. alignment = 4 * 1024;
  1879. else
  1880. alignment = 64 * 1024;
  1881. break;
  1882. case I915_TILING_X:
  1883. /* pin() will align the object as required by fence */
  1884. alignment = 0;
  1885. break;
  1886. case I915_TILING_Y:
  1887. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1888. return -EINVAL;
  1889. default:
  1890. BUG();
  1891. }
  1892. /* Note that the w/a also requires 64 PTE of padding following the
  1893. * bo. We currently fill all unused PTE with the shadow page and so
  1894. * we should always have valid PTE following the scanout preventing
  1895. * the VT-d warning.
  1896. */
  1897. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1898. alignment = 256 * 1024;
  1899. dev_priv->mm.interruptible = false;
  1900. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1901. if (ret)
  1902. goto err_interruptible;
  1903. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1904. * fence, whereas 965+ only requires a fence if using
  1905. * framebuffer compression. For simplicity, we always install
  1906. * a fence as the cost is not that onerous.
  1907. */
  1908. ret = i915_gem_object_get_fence(obj);
  1909. if (ret)
  1910. goto err_unpin;
  1911. i915_gem_object_pin_fence(obj);
  1912. dev_priv->mm.interruptible = true;
  1913. return 0;
  1914. err_unpin:
  1915. i915_gem_object_unpin_from_display_plane(obj);
  1916. err_interruptible:
  1917. dev_priv->mm.interruptible = true;
  1918. return ret;
  1919. }
  1920. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1921. {
  1922. i915_gem_object_unpin_fence(obj);
  1923. i915_gem_object_unpin_from_display_plane(obj);
  1924. }
  1925. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1926. * is assumed to be a power-of-two. */
  1927. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1928. unsigned int tiling_mode,
  1929. unsigned int cpp,
  1930. unsigned int pitch)
  1931. {
  1932. if (tiling_mode != I915_TILING_NONE) {
  1933. unsigned int tile_rows, tiles;
  1934. tile_rows = *y / 8;
  1935. *y %= 8;
  1936. tiles = *x / (512/cpp);
  1937. *x %= 512/cpp;
  1938. return tile_rows * pitch * 8 + tiles * 4096;
  1939. } else {
  1940. unsigned int offset;
  1941. offset = *y * pitch + *x * cpp;
  1942. *y = 0;
  1943. *x = (offset & 4095) / cpp;
  1944. return offset & -4096;
  1945. }
  1946. }
  1947. int intel_format_to_fourcc(int format)
  1948. {
  1949. switch (format) {
  1950. case DISPPLANE_8BPP:
  1951. return DRM_FORMAT_C8;
  1952. case DISPPLANE_BGRX555:
  1953. return DRM_FORMAT_XRGB1555;
  1954. case DISPPLANE_BGRX565:
  1955. return DRM_FORMAT_RGB565;
  1956. default:
  1957. case DISPPLANE_BGRX888:
  1958. return DRM_FORMAT_XRGB8888;
  1959. case DISPPLANE_RGBX888:
  1960. return DRM_FORMAT_XBGR8888;
  1961. case DISPPLANE_BGRX101010:
  1962. return DRM_FORMAT_XRGB2101010;
  1963. case DISPPLANE_RGBX101010:
  1964. return DRM_FORMAT_XBGR2101010;
  1965. }
  1966. }
  1967. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  1968. struct intel_plane_config *plane_config)
  1969. {
  1970. struct drm_device *dev = crtc->base.dev;
  1971. struct drm_i915_gem_object *obj = NULL;
  1972. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  1973. u32 base = plane_config->base;
  1974. if (plane_config->size == 0)
  1975. return false;
  1976. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  1977. plane_config->size);
  1978. if (!obj)
  1979. return false;
  1980. if (plane_config->tiled) {
  1981. obj->tiling_mode = I915_TILING_X;
  1982. obj->stride = crtc->base.primary->fb->pitches[0];
  1983. }
  1984. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  1985. mode_cmd.width = crtc->base.primary->fb->width;
  1986. mode_cmd.height = crtc->base.primary->fb->height;
  1987. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  1988. mutex_lock(&dev->struct_mutex);
  1989. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  1990. &mode_cmd, obj)) {
  1991. DRM_DEBUG_KMS("intel fb init failed\n");
  1992. goto out_unref_obj;
  1993. }
  1994. mutex_unlock(&dev->struct_mutex);
  1995. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  1996. return true;
  1997. out_unref_obj:
  1998. drm_gem_object_unreference(&obj->base);
  1999. mutex_unlock(&dev->struct_mutex);
  2000. return false;
  2001. }
  2002. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2003. struct intel_plane_config *plane_config)
  2004. {
  2005. struct drm_device *dev = intel_crtc->base.dev;
  2006. struct drm_crtc *c;
  2007. struct intel_crtc *i;
  2008. struct intel_framebuffer *fb;
  2009. if (!intel_crtc->base.primary->fb)
  2010. return;
  2011. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2012. return;
  2013. kfree(intel_crtc->base.primary->fb);
  2014. intel_crtc->base.primary->fb = NULL;
  2015. /*
  2016. * Failed to alloc the obj, check to see if we should share
  2017. * an fb with another CRTC instead
  2018. */
  2019. for_each_crtc(dev, c) {
  2020. i = to_intel_crtc(c);
  2021. if (c == &intel_crtc->base)
  2022. continue;
  2023. if (!i->active || !c->primary->fb)
  2024. continue;
  2025. fb = to_intel_framebuffer(c->primary->fb);
  2026. if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
  2027. drm_framebuffer_reference(c->primary->fb);
  2028. intel_crtc->base.primary->fb = c->primary->fb;
  2029. break;
  2030. }
  2031. }
  2032. }
  2033. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2034. struct drm_framebuffer *fb,
  2035. int x, int y)
  2036. {
  2037. struct drm_device *dev = crtc->dev;
  2038. struct drm_i915_private *dev_priv = dev->dev_private;
  2039. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2040. struct intel_framebuffer *intel_fb;
  2041. struct drm_i915_gem_object *obj;
  2042. int plane = intel_crtc->plane;
  2043. unsigned long linear_offset;
  2044. u32 dspcntr;
  2045. u32 reg;
  2046. intel_fb = to_intel_framebuffer(fb);
  2047. obj = intel_fb->obj;
  2048. reg = DSPCNTR(plane);
  2049. dspcntr = I915_READ(reg);
  2050. /* Mask out pixel format bits in case we change it */
  2051. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2052. switch (fb->pixel_format) {
  2053. case DRM_FORMAT_C8:
  2054. dspcntr |= DISPPLANE_8BPP;
  2055. break;
  2056. case DRM_FORMAT_XRGB1555:
  2057. case DRM_FORMAT_ARGB1555:
  2058. dspcntr |= DISPPLANE_BGRX555;
  2059. break;
  2060. case DRM_FORMAT_RGB565:
  2061. dspcntr |= DISPPLANE_BGRX565;
  2062. break;
  2063. case DRM_FORMAT_XRGB8888:
  2064. case DRM_FORMAT_ARGB8888:
  2065. dspcntr |= DISPPLANE_BGRX888;
  2066. break;
  2067. case DRM_FORMAT_XBGR8888:
  2068. case DRM_FORMAT_ABGR8888:
  2069. dspcntr |= DISPPLANE_RGBX888;
  2070. break;
  2071. case DRM_FORMAT_XRGB2101010:
  2072. case DRM_FORMAT_ARGB2101010:
  2073. dspcntr |= DISPPLANE_BGRX101010;
  2074. break;
  2075. case DRM_FORMAT_XBGR2101010:
  2076. case DRM_FORMAT_ABGR2101010:
  2077. dspcntr |= DISPPLANE_RGBX101010;
  2078. break;
  2079. default:
  2080. BUG();
  2081. }
  2082. if (INTEL_INFO(dev)->gen >= 4) {
  2083. if (obj->tiling_mode != I915_TILING_NONE)
  2084. dspcntr |= DISPPLANE_TILED;
  2085. else
  2086. dspcntr &= ~DISPPLANE_TILED;
  2087. }
  2088. if (IS_G4X(dev))
  2089. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2090. I915_WRITE(reg, dspcntr);
  2091. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2092. if (INTEL_INFO(dev)->gen >= 4) {
  2093. intel_crtc->dspaddr_offset =
  2094. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2095. fb->bits_per_pixel / 8,
  2096. fb->pitches[0]);
  2097. linear_offset -= intel_crtc->dspaddr_offset;
  2098. } else {
  2099. intel_crtc->dspaddr_offset = linear_offset;
  2100. }
  2101. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2102. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2103. fb->pitches[0]);
  2104. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2105. if (INTEL_INFO(dev)->gen >= 4) {
  2106. I915_WRITE(DSPSURF(plane),
  2107. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2108. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2109. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2110. } else
  2111. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2112. POSTING_READ(reg);
  2113. }
  2114. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2115. struct drm_framebuffer *fb,
  2116. int x, int y)
  2117. {
  2118. struct drm_device *dev = crtc->dev;
  2119. struct drm_i915_private *dev_priv = dev->dev_private;
  2120. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2121. struct intel_framebuffer *intel_fb;
  2122. struct drm_i915_gem_object *obj;
  2123. int plane = intel_crtc->plane;
  2124. unsigned long linear_offset;
  2125. u32 dspcntr;
  2126. u32 reg;
  2127. intel_fb = to_intel_framebuffer(fb);
  2128. obj = intel_fb->obj;
  2129. reg = DSPCNTR(plane);
  2130. dspcntr = I915_READ(reg);
  2131. /* Mask out pixel format bits in case we change it */
  2132. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2133. switch (fb->pixel_format) {
  2134. case DRM_FORMAT_C8:
  2135. dspcntr |= DISPPLANE_8BPP;
  2136. break;
  2137. case DRM_FORMAT_RGB565:
  2138. dspcntr |= DISPPLANE_BGRX565;
  2139. break;
  2140. case DRM_FORMAT_XRGB8888:
  2141. case DRM_FORMAT_ARGB8888:
  2142. dspcntr |= DISPPLANE_BGRX888;
  2143. break;
  2144. case DRM_FORMAT_XBGR8888:
  2145. case DRM_FORMAT_ABGR8888:
  2146. dspcntr |= DISPPLANE_RGBX888;
  2147. break;
  2148. case DRM_FORMAT_XRGB2101010:
  2149. case DRM_FORMAT_ARGB2101010:
  2150. dspcntr |= DISPPLANE_BGRX101010;
  2151. break;
  2152. case DRM_FORMAT_XBGR2101010:
  2153. case DRM_FORMAT_ABGR2101010:
  2154. dspcntr |= DISPPLANE_RGBX101010;
  2155. break;
  2156. default:
  2157. BUG();
  2158. }
  2159. if (obj->tiling_mode != I915_TILING_NONE)
  2160. dspcntr |= DISPPLANE_TILED;
  2161. else
  2162. dspcntr &= ~DISPPLANE_TILED;
  2163. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2164. dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
  2165. else
  2166. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2167. I915_WRITE(reg, dspcntr);
  2168. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2169. intel_crtc->dspaddr_offset =
  2170. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2171. fb->bits_per_pixel / 8,
  2172. fb->pitches[0]);
  2173. linear_offset -= intel_crtc->dspaddr_offset;
  2174. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2175. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2176. fb->pitches[0]);
  2177. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2178. I915_WRITE(DSPSURF(plane),
  2179. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2180. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2181. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2182. } else {
  2183. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2184. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2185. }
  2186. POSTING_READ(reg);
  2187. }
  2188. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2189. static int
  2190. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2191. int x, int y, enum mode_set_atomic state)
  2192. {
  2193. struct drm_device *dev = crtc->dev;
  2194. struct drm_i915_private *dev_priv = dev->dev_private;
  2195. if (dev_priv->display.disable_fbc)
  2196. dev_priv->display.disable_fbc(dev);
  2197. intel_increase_pllclock(crtc);
  2198. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2199. return 0;
  2200. }
  2201. void intel_display_handle_reset(struct drm_device *dev)
  2202. {
  2203. struct drm_i915_private *dev_priv = dev->dev_private;
  2204. struct drm_crtc *crtc;
  2205. /*
  2206. * Flips in the rings have been nuked by the reset,
  2207. * so complete all pending flips so that user space
  2208. * will get its events and not get stuck.
  2209. *
  2210. * Also update the base address of all primary
  2211. * planes to the the last fb to make sure we're
  2212. * showing the correct fb after a reset.
  2213. *
  2214. * Need to make two loops over the crtcs so that we
  2215. * don't try to grab a crtc mutex before the
  2216. * pending_flip_queue really got woken up.
  2217. */
  2218. for_each_crtc(dev, crtc) {
  2219. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2220. enum plane plane = intel_crtc->plane;
  2221. intel_prepare_page_flip(dev, plane);
  2222. intel_finish_page_flip_plane(dev, plane);
  2223. }
  2224. for_each_crtc(dev, crtc) {
  2225. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2226. drm_modeset_lock(&crtc->mutex, NULL);
  2227. /*
  2228. * FIXME: Once we have proper support for primary planes (and
  2229. * disabling them without disabling the entire crtc) allow again
  2230. * a NULL crtc->primary->fb.
  2231. */
  2232. if (intel_crtc->active && crtc->primary->fb)
  2233. dev_priv->display.update_primary_plane(crtc,
  2234. crtc->primary->fb,
  2235. crtc->x,
  2236. crtc->y);
  2237. drm_modeset_unlock(&crtc->mutex);
  2238. }
  2239. }
  2240. static int
  2241. intel_finish_fb(struct drm_framebuffer *old_fb)
  2242. {
  2243. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  2244. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2245. bool was_interruptible = dev_priv->mm.interruptible;
  2246. int ret;
  2247. /* Big Hammer, we also need to ensure that any pending
  2248. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2249. * current scanout is retired before unpinning the old
  2250. * framebuffer.
  2251. *
  2252. * This should only fail upon a hung GPU, in which case we
  2253. * can safely continue.
  2254. */
  2255. dev_priv->mm.interruptible = false;
  2256. ret = i915_gem_object_finish_gpu(obj);
  2257. dev_priv->mm.interruptible = was_interruptible;
  2258. return ret;
  2259. }
  2260. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2261. {
  2262. struct drm_device *dev = crtc->dev;
  2263. struct drm_i915_private *dev_priv = dev->dev_private;
  2264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2265. unsigned long flags;
  2266. bool pending;
  2267. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2268. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2269. return false;
  2270. spin_lock_irqsave(&dev->event_lock, flags);
  2271. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2272. spin_unlock_irqrestore(&dev->event_lock, flags);
  2273. return pending;
  2274. }
  2275. static int
  2276. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2277. struct drm_framebuffer *fb)
  2278. {
  2279. struct drm_device *dev = crtc->dev;
  2280. struct drm_i915_private *dev_priv = dev->dev_private;
  2281. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2282. struct drm_framebuffer *old_fb;
  2283. int ret;
  2284. if (intel_crtc_has_pending_flip(crtc)) {
  2285. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2286. return -EBUSY;
  2287. }
  2288. /* no fb bound */
  2289. if (!fb) {
  2290. DRM_ERROR("No FB bound\n");
  2291. return 0;
  2292. }
  2293. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2294. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2295. plane_name(intel_crtc->plane),
  2296. INTEL_INFO(dev)->num_pipes);
  2297. return -EINVAL;
  2298. }
  2299. mutex_lock(&dev->struct_mutex);
  2300. ret = intel_pin_and_fence_fb_obj(dev,
  2301. to_intel_framebuffer(fb)->obj,
  2302. NULL);
  2303. mutex_unlock(&dev->struct_mutex);
  2304. if (ret != 0) {
  2305. DRM_ERROR("pin & fence failed\n");
  2306. return ret;
  2307. }
  2308. /*
  2309. * Update pipe size and adjust fitter if needed: the reason for this is
  2310. * that in compute_mode_changes we check the native mode (not the pfit
  2311. * mode) to see if we can flip rather than do a full mode set. In the
  2312. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2313. * pfit state, we'll end up with a big fb scanned out into the wrong
  2314. * sized surface.
  2315. *
  2316. * To fix this properly, we need to hoist the checks up into
  2317. * compute_mode_changes (or above), check the actual pfit state and
  2318. * whether the platform allows pfit disable with pipe active, and only
  2319. * then update the pipesrc and pfit state, even on the flip path.
  2320. */
  2321. if (i915.fastboot) {
  2322. const struct drm_display_mode *adjusted_mode =
  2323. &intel_crtc->config.adjusted_mode;
  2324. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2325. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2326. (adjusted_mode->crtc_vdisplay - 1));
  2327. if (!intel_crtc->config.pch_pfit.enabled &&
  2328. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2329. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2330. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2331. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2332. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2333. }
  2334. intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2335. intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2336. }
  2337. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2338. old_fb = crtc->primary->fb;
  2339. crtc->primary->fb = fb;
  2340. crtc->x = x;
  2341. crtc->y = y;
  2342. if (old_fb) {
  2343. if (intel_crtc->active && old_fb != fb)
  2344. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2345. mutex_lock(&dev->struct_mutex);
  2346. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  2347. mutex_unlock(&dev->struct_mutex);
  2348. }
  2349. mutex_lock(&dev->struct_mutex);
  2350. intel_update_fbc(dev);
  2351. intel_edp_psr_update(dev);
  2352. mutex_unlock(&dev->struct_mutex);
  2353. return 0;
  2354. }
  2355. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2356. {
  2357. struct drm_device *dev = crtc->dev;
  2358. struct drm_i915_private *dev_priv = dev->dev_private;
  2359. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2360. int pipe = intel_crtc->pipe;
  2361. u32 reg, temp;
  2362. /* enable normal train */
  2363. reg = FDI_TX_CTL(pipe);
  2364. temp = I915_READ(reg);
  2365. if (IS_IVYBRIDGE(dev)) {
  2366. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2367. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2368. } else {
  2369. temp &= ~FDI_LINK_TRAIN_NONE;
  2370. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2371. }
  2372. I915_WRITE(reg, temp);
  2373. reg = FDI_RX_CTL(pipe);
  2374. temp = I915_READ(reg);
  2375. if (HAS_PCH_CPT(dev)) {
  2376. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2377. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2378. } else {
  2379. temp &= ~FDI_LINK_TRAIN_NONE;
  2380. temp |= FDI_LINK_TRAIN_NONE;
  2381. }
  2382. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2383. /* wait one idle pattern time */
  2384. POSTING_READ(reg);
  2385. udelay(1000);
  2386. /* IVB wants error correction enabled */
  2387. if (IS_IVYBRIDGE(dev))
  2388. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2389. FDI_FE_ERRC_ENABLE);
  2390. }
  2391. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2392. {
  2393. return crtc->base.enabled && crtc->active &&
  2394. crtc->config.has_pch_encoder;
  2395. }
  2396. static void ivb_modeset_global_resources(struct drm_device *dev)
  2397. {
  2398. struct drm_i915_private *dev_priv = dev->dev_private;
  2399. struct intel_crtc *pipe_B_crtc =
  2400. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2401. struct intel_crtc *pipe_C_crtc =
  2402. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2403. uint32_t temp;
  2404. /*
  2405. * When everything is off disable fdi C so that we could enable fdi B
  2406. * with all lanes. Note that we don't care about enabled pipes without
  2407. * an enabled pch encoder.
  2408. */
  2409. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2410. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2411. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2412. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2413. temp = I915_READ(SOUTH_CHICKEN1);
  2414. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2415. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2416. I915_WRITE(SOUTH_CHICKEN1, temp);
  2417. }
  2418. }
  2419. /* The FDI link training functions for ILK/Ibexpeak. */
  2420. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2421. {
  2422. struct drm_device *dev = crtc->dev;
  2423. struct drm_i915_private *dev_priv = dev->dev_private;
  2424. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2425. int pipe = intel_crtc->pipe;
  2426. u32 reg, temp, tries;
  2427. /* FDI needs bits from pipe first */
  2428. assert_pipe_enabled(dev_priv, pipe);
  2429. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2430. for train result */
  2431. reg = FDI_RX_IMR(pipe);
  2432. temp = I915_READ(reg);
  2433. temp &= ~FDI_RX_SYMBOL_LOCK;
  2434. temp &= ~FDI_RX_BIT_LOCK;
  2435. I915_WRITE(reg, temp);
  2436. I915_READ(reg);
  2437. udelay(150);
  2438. /* enable CPU FDI TX and PCH FDI RX */
  2439. reg = FDI_TX_CTL(pipe);
  2440. temp = I915_READ(reg);
  2441. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2442. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2443. temp &= ~FDI_LINK_TRAIN_NONE;
  2444. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2445. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2446. reg = FDI_RX_CTL(pipe);
  2447. temp = I915_READ(reg);
  2448. temp &= ~FDI_LINK_TRAIN_NONE;
  2449. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2450. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2451. POSTING_READ(reg);
  2452. udelay(150);
  2453. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2454. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2455. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2456. FDI_RX_PHASE_SYNC_POINTER_EN);
  2457. reg = FDI_RX_IIR(pipe);
  2458. for (tries = 0; tries < 5; tries++) {
  2459. temp = I915_READ(reg);
  2460. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2461. if ((temp & FDI_RX_BIT_LOCK)) {
  2462. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2463. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2464. break;
  2465. }
  2466. }
  2467. if (tries == 5)
  2468. DRM_ERROR("FDI train 1 fail!\n");
  2469. /* Train 2 */
  2470. reg = FDI_TX_CTL(pipe);
  2471. temp = I915_READ(reg);
  2472. temp &= ~FDI_LINK_TRAIN_NONE;
  2473. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2474. I915_WRITE(reg, temp);
  2475. reg = FDI_RX_CTL(pipe);
  2476. temp = I915_READ(reg);
  2477. temp &= ~FDI_LINK_TRAIN_NONE;
  2478. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2479. I915_WRITE(reg, temp);
  2480. POSTING_READ(reg);
  2481. udelay(150);
  2482. reg = FDI_RX_IIR(pipe);
  2483. for (tries = 0; tries < 5; tries++) {
  2484. temp = I915_READ(reg);
  2485. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2486. if (temp & FDI_RX_SYMBOL_LOCK) {
  2487. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2488. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2489. break;
  2490. }
  2491. }
  2492. if (tries == 5)
  2493. DRM_ERROR("FDI train 2 fail!\n");
  2494. DRM_DEBUG_KMS("FDI train done\n");
  2495. }
  2496. static const int snb_b_fdi_train_param[] = {
  2497. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2498. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2499. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2500. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2501. };
  2502. /* The FDI link training functions for SNB/Cougarpoint. */
  2503. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2504. {
  2505. struct drm_device *dev = crtc->dev;
  2506. struct drm_i915_private *dev_priv = dev->dev_private;
  2507. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2508. int pipe = intel_crtc->pipe;
  2509. u32 reg, temp, i, retry;
  2510. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2511. for train result */
  2512. reg = FDI_RX_IMR(pipe);
  2513. temp = I915_READ(reg);
  2514. temp &= ~FDI_RX_SYMBOL_LOCK;
  2515. temp &= ~FDI_RX_BIT_LOCK;
  2516. I915_WRITE(reg, temp);
  2517. POSTING_READ(reg);
  2518. udelay(150);
  2519. /* enable CPU FDI TX and PCH FDI RX */
  2520. reg = FDI_TX_CTL(pipe);
  2521. temp = I915_READ(reg);
  2522. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2523. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2524. temp &= ~FDI_LINK_TRAIN_NONE;
  2525. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2526. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2527. /* SNB-B */
  2528. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2529. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2530. I915_WRITE(FDI_RX_MISC(pipe),
  2531. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2532. reg = FDI_RX_CTL(pipe);
  2533. temp = I915_READ(reg);
  2534. if (HAS_PCH_CPT(dev)) {
  2535. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2536. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2537. } else {
  2538. temp &= ~FDI_LINK_TRAIN_NONE;
  2539. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2540. }
  2541. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2542. POSTING_READ(reg);
  2543. udelay(150);
  2544. for (i = 0; i < 4; i++) {
  2545. reg = FDI_TX_CTL(pipe);
  2546. temp = I915_READ(reg);
  2547. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2548. temp |= snb_b_fdi_train_param[i];
  2549. I915_WRITE(reg, temp);
  2550. POSTING_READ(reg);
  2551. udelay(500);
  2552. for (retry = 0; retry < 5; retry++) {
  2553. reg = FDI_RX_IIR(pipe);
  2554. temp = I915_READ(reg);
  2555. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2556. if (temp & FDI_RX_BIT_LOCK) {
  2557. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2558. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2559. break;
  2560. }
  2561. udelay(50);
  2562. }
  2563. if (retry < 5)
  2564. break;
  2565. }
  2566. if (i == 4)
  2567. DRM_ERROR("FDI train 1 fail!\n");
  2568. /* Train 2 */
  2569. reg = FDI_TX_CTL(pipe);
  2570. temp = I915_READ(reg);
  2571. temp &= ~FDI_LINK_TRAIN_NONE;
  2572. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2573. if (IS_GEN6(dev)) {
  2574. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2575. /* SNB-B */
  2576. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2577. }
  2578. I915_WRITE(reg, temp);
  2579. reg = FDI_RX_CTL(pipe);
  2580. temp = I915_READ(reg);
  2581. if (HAS_PCH_CPT(dev)) {
  2582. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2583. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2584. } else {
  2585. temp &= ~FDI_LINK_TRAIN_NONE;
  2586. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2587. }
  2588. I915_WRITE(reg, temp);
  2589. POSTING_READ(reg);
  2590. udelay(150);
  2591. for (i = 0; i < 4; i++) {
  2592. reg = FDI_TX_CTL(pipe);
  2593. temp = I915_READ(reg);
  2594. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2595. temp |= snb_b_fdi_train_param[i];
  2596. I915_WRITE(reg, temp);
  2597. POSTING_READ(reg);
  2598. udelay(500);
  2599. for (retry = 0; retry < 5; retry++) {
  2600. reg = FDI_RX_IIR(pipe);
  2601. temp = I915_READ(reg);
  2602. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2603. if (temp & FDI_RX_SYMBOL_LOCK) {
  2604. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2605. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2606. break;
  2607. }
  2608. udelay(50);
  2609. }
  2610. if (retry < 5)
  2611. break;
  2612. }
  2613. if (i == 4)
  2614. DRM_ERROR("FDI train 2 fail!\n");
  2615. DRM_DEBUG_KMS("FDI train done.\n");
  2616. }
  2617. /* Manual link training for Ivy Bridge A0 parts */
  2618. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2619. {
  2620. struct drm_device *dev = crtc->dev;
  2621. struct drm_i915_private *dev_priv = dev->dev_private;
  2622. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2623. int pipe = intel_crtc->pipe;
  2624. u32 reg, temp, i, j;
  2625. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2626. for train result */
  2627. reg = FDI_RX_IMR(pipe);
  2628. temp = I915_READ(reg);
  2629. temp &= ~FDI_RX_SYMBOL_LOCK;
  2630. temp &= ~FDI_RX_BIT_LOCK;
  2631. I915_WRITE(reg, temp);
  2632. POSTING_READ(reg);
  2633. udelay(150);
  2634. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2635. I915_READ(FDI_RX_IIR(pipe)));
  2636. /* Try each vswing and preemphasis setting twice before moving on */
  2637. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2638. /* disable first in case we need to retry */
  2639. reg = FDI_TX_CTL(pipe);
  2640. temp = I915_READ(reg);
  2641. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2642. temp &= ~FDI_TX_ENABLE;
  2643. I915_WRITE(reg, temp);
  2644. reg = FDI_RX_CTL(pipe);
  2645. temp = I915_READ(reg);
  2646. temp &= ~FDI_LINK_TRAIN_AUTO;
  2647. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2648. temp &= ~FDI_RX_ENABLE;
  2649. I915_WRITE(reg, temp);
  2650. /* enable CPU FDI TX and PCH FDI RX */
  2651. reg = FDI_TX_CTL(pipe);
  2652. temp = I915_READ(reg);
  2653. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2654. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2655. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2656. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2657. temp |= snb_b_fdi_train_param[j/2];
  2658. temp |= FDI_COMPOSITE_SYNC;
  2659. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2660. I915_WRITE(FDI_RX_MISC(pipe),
  2661. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2662. reg = FDI_RX_CTL(pipe);
  2663. temp = I915_READ(reg);
  2664. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2665. temp |= FDI_COMPOSITE_SYNC;
  2666. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2667. POSTING_READ(reg);
  2668. udelay(1); /* should be 0.5us */
  2669. for (i = 0; i < 4; i++) {
  2670. reg = FDI_RX_IIR(pipe);
  2671. temp = I915_READ(reg);
  2672. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2673. if (temp & FDI_RX_BIT_LOCK ||
  2674. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2675. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2676. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2677. i);
  2678. break;
  2679. }
  2680. udelay(1); /* should be 0.5us */
  2681. }
  2682. if (i == 4) {
  2683. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2684. continue;
  2685. }
  2686. /* Train 2 */
  2687. reg = FDI_TX_CTL(pipe);
  2688. temp = I915_READ(reg);
  2689. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2690. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2691. I915_WRITE(reg, temp);
  2692. reg = FDI_RX_CTL(pipe);
  2693. temp = I915_READ(reg);
  2694. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2695. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2696. I915_WRITE(reg, temp);
  2697. POSTING_READ(reg);
  2698. udelay(2); /* should be 1.5us */
  2699. for (i = 0; i < 4; i++) {
  2700. reg = FDI_RX_IIR(pipe);
  2701. temp = I915_READ(reg);
  2702. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2703. if (temp & FDI_RX_SYMBOL_LOCK ||
  2704. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2705. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2706. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2707. i);
  2708. goto train_done;
  2709. }
  2710. udelay(2); /* should be 1.5us */
  2711. }
  2712. if (i == 4)
  2713. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2714. }
  2715. train_done:
  2716. DRM_DEBUG_KMS("FDI train done.\n");
  2717. }
  2718. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2719. {
  2720. struct drm_device *dev = intel_crtc->base.dev;
  2721. struct drm_i915_private *dev_priv = dev->dev_private;
  2722. int pipe = intel_crtc->pipe;
  2723. u32 reg, temp;
  2724. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2725. reg = FDI_RX_CTL(pipe);
  2726. temp = I915_READ(reg);
  2727. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2728. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2729. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2730. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2731. POSTING_READ(reg);
  2732. udelay(200);
  2733. /* Switch from Rawclk to PCDclk */
  2734. temp = I915_READ(reg);
  2735. I915_WRITE(reg, temp | FDI_PCDCLK);
  2736. POSTING_READ(reg);
  2737. udelay(200);
  2738. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2739. reg = FDI_TX_CTL(pipe);
  2740. temp = I915_READ(reg);
  2741. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2742. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2743. POSTING_READ(reg);
  2744. udelay(100);
  2745. }
  2746. }
  2747. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2748. {
  2749. struct drm_device *dev = intel_crtc->base.dev;
  2750. struct drm_i915_private *dev_priv = dev->dev_private;
  2751. int pipe = intel_crtc->pipe;
  2752. u32 reg, temp;
  2753. /* Switch from PCDclk to Rawclk */
  2754. reg = FDI_RX_CTL(pipe);
  2755. temp = I915_READ(reg);
  2756. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2757. /* Disable CPU FDI TX PLL */
  2758. reg = FDI_TX_CTL(pipe);
  2759. temp = I915_READ(reg);
  2760. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2761. POSTING_READ(reg);
  2762. udelay(100);
  2763. reg = FDI_RX_CTL(pipe);
  2764. temp = I915_READ(reg);
  2765. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2766. /* Wait for the clocks to turn off. */
  2767. POSTING_READ(reg);
  2768. udelay(100);
  2769. }
  2770. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2771. {
  2772. struct drm_device *dev = crtc->dev;
  2773. struct drm_i915_private *dev_priv = dev->dev_private;
  2774. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2775. int pipe = intel_crtc->pipe;
  2776. u32 reg, temp;
  2777. /* disable CPU FDI tx and PCH FDI rx */
  2778. reg = FDI_TX_CTL(pipe);
  2779. temp = I915_READ(reg);
  2780. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2781. POSTING_READ(reg);
  2782. reg = FDI_RX_CTL(pipe);
  2783. temp = I915_READ(reg);
  2784. temp &= ~(0x7 << 16);
  2785. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2786. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2787. POSTING_READ(reg);
  2788. udelay(100);
  2789. /* Ironlake workaround, disable clock pointer after downing FDI */
  2790. if (HAS_PCH_IBX(dev))
  2791. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2792. /* still set train pattern 1 */
  2793. reg = FDI_TX_CTL(pipe);
  2794. temp = I915_READ(reg);
  2795. temp &= ~FDI_LINK_TRAIN_NONE;
  2796. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2797. I915_WRITE(reg, temp);
  2798. reg = FDI_RX_CTL(pipe);
  2799. temp = I915_READ(reg);
  2800. if (HAS_PCH_CPT(dev)) {
  2801. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2802. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2803. } else {
  2804. temp &= ~FDI_LINK_TRAIN_NONE;
  2805. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2806. }
  2807. /* BPC in FDI rx is consistent with that in PIPECONF */
  2808. temp &= ~(0x07 << 16);
  2809. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2810. I915_WRITE(reg, temp);
  2811. POSTING_READ(reg);
  2812. udelay(100);
  2813. }
  2814. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2815. {
  2816. struct intel_crtc *crtc;
  2817. /* Note that we don't need to be called with mode_config.lock here
  2818. * as our list of CRTC objects is static for the lifetime of the
  2819. * device and so cannot disappear as we iterate. Similarly, we can
  2820. * happily treat the predicates as racy, atomic checks as userspace
  2821. * cannot claim and pin a new fb without at least acquring the
  2822. * struct_mutex and so serialising with us.
  2823. */
  2824. for_each_intel_crtc(dev, crtc) {
  2825. if (atomic_read(&crtc->unpin_work_count) == 0)
  2826. continue;
  2827. if (crtc->unpin_work)
  2828. intel_wait_for_vblank(dev, crtc->pipe);
  2829. return true;
  2830. }
  2831. return false;
  2832. }
  2833. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2834. {
  2835. struct drm_device *dev = crtc->dev;
  2836. struct drm_i915_private *dev_priv = dev->dev_private;
  2837. if (crtc->primary->fb == NULL)
  2838. return;
  2839. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2840. WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  2841. !intel_crtc_has_pending_flip(crtc),
  2842. 60*HZ) == 0);
  2843. mutex_lock(&dev->struct_mutex);
  2844. intel_finish_fb(crtc->primary->fb);
  2845. mutex_unlock(&dev->struct_mutex);
  2846. }
  2847. /* Program iCLKIP clock to the desired frequency */
  2848. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2849. {
  2850. struct drm_device *dev = crtc->dev;
  2851. struct drm_i915_private *dev_priv = dev->dev_private;
  2852. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2853. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2854. u32 temp;
  2855. mutex_lock(&dev_priv->dpio_lock);
  2856. /* It is necessary to ungate the pixclk gate prior to programming
  2857. * the divisors, and gate it back when it is done.
  2858. */
  2859. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2860. /* Disable SSCCTL */
  2861. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2862. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2863. SBI_SSCCTL_DISABLE,
  2864. SBI_ICLK);
  2865. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2866. if (clock == 20000) {
  2867. auxdiv = 1;
  2868. divsel = 0x41;
  2869. phaseinc = 0x20;
  2870. } else {
  2871. /* The iCLK virtual clock root frequency is in MHz,
  2872. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2873. * divisors, it is necessary to divide one by another, so we
  2874. * convert the virtual clock precision to KHz here for higher
  2875. * precision.
  2876. */
  2877. u32 iclk_virtual_root_freq = 172800 * 1000;
  2878. u32 iclk_pi_range = 64;
  2879. u32 desired_divisor, msb_divisor_value, pi_value;
  2880. desired_divisor = (iclk_virtual_root_freq / clock);
  2881. msb_divisor_value = desired_divisor / iclk_pi_range;
  2882. pi_value = desired_divisor % iclk_pi_range;
  2883. auxdiv = 0;
  2884. divsel = msb_divisor_value - 2;
  2885. phaseinc = pi_value;
  2886. }
  2887. /* This should not happen with any sane values */
  2888. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2889. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2890. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2891. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2892. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2893. clock,
  2894. auxdiv,
  2895. divsel,
  2896. phasedir,
  2897. phaseinc);
  2898. /* Program SSCDIVINTPHASE6 */
  2899. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2900. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2901. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2902. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2903. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2904. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2905. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2906. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2907. /* Program SSCAUXDIV */
  2908. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2909. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2910. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2911. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2912. /* Enable modulator and associated divider */
  2913. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2914. temp &= ~SBI_SSCCTL_DISABLE;
  2915. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2916. /* Wait for initialization time */
  2917. udelay(24);
  2918. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2919. mutex_unlock(&dev_priv->dpio_lock);
  2920. }
  2921. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2922. enum pipe pch_transcoder)
  2923. {
  2924. struct drm_device *dev = crtc->base.dev;
  2925. struct drm_i915_private *dev_priv = dev->dev_private;
  2926. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2927. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2928. I915_READ(HTOTAL(cpu_transcoder)));
  2929. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2930. I915_READ(HBLANK(cpu_transcoder)));
  2931. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2932. I915_READ(HSYNC(cpu_transcoder)));
  2933. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2934. I915_READ(VTOTAL(cpu_transcoder)));
  2935. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2936. I915_READ(VBLANK(cpu_transcoder)));
  2937. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2938. I915_READ(VSYNC(cpu_transcoder)));
  2939. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2940. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2941. }
  2942. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  2943. {
  2944. struct drm_i915_private *dev_priv = dev->dev_private;
  2945. uint32_t temp;
  2946. temp = I915_READ(SOUTH_CHICKEN1);
  2947. if (temp & FDI_BC_BIFURCATION_SELECT)
  2948. return;
  2949. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2950. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2951. temp |= FDI_BC_BIFURCATION_SELECT;
  2952. DRM_DEBUG_KMS("enabling fdi C rx\n");
  2953. I915_WRITE(SOUTH_CHICKEN1, temp);
  2954. POSTING_READ(SOUTH_CHICKEN1);
  2955. }
  2956. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  2957. {
  2958. struct drm_device *dev = intel_crtc->base.dev;
  2959. struct drm_i915_private *dev_priv = dev->dev_private;
  2960. switch (intel_crtc->pipe) {
  2961. case PIPE_A:
  2962. break;
  2963. case PIPE_B:
  2964. if (intel_crtc->config.fdi_lanes > 2)
  2965. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  2966. else
  2967. cpt_enable_fdi_bc_bifurcation(dev);
  2968. break;
  2969. case PIPE_C:
  2970. cpt_enable_fdi_bc_bifurcation(dev);
  2971. break;
  2972. default:
  2973. BUG();
  2974. }
  2975. }
  2976. /*
  2977. * Enable PCH resources required for PCH ports:
  2978. * - PCH PLLs
  2979. * - FDI training & RX/TX
  2980. * - update transcoder timings
  2981. * - DP transcoding bits
  2982. * - transcoder
  2983. */
  2984. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2985. {
  2986. struct drm_device *dev = crtc->dev;
  2987. struct drm_i915_private *dev_priv = dev->dev_private;
  2988. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2989. int pipe = intel_crtc->pipe;
  2990. u32 reg, temp;
  2991. assert_pch_transcoder_disabled(dev_priv, pipe);
  2992. if (IS_IVYBRIDGE(dev))
  2993. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  2994. /* Write the TU size bits before fdi link training, so that error
  2995. * detection works. */
  2996. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2997. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2998. /* For PCH output, training FDI link */
  2999. dev_priv->display.fdi_link_train(crtc);
  3000. /* We need to program the right clock selection before writing the pixel
  3001. * mutliplier into the DPLL. */
  3002. if (HAS_PCH_CPT(dev)) {
  3003. u32 sel;
  3004. temp = I915_READ(PCH_DPLL_SEL);
  3005. temp |= TRANS_DPLL_ENABLE(pipe);
  3006. sel = TRANS_DPLLB_SEL(pipe);
  3007. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3008. temp |= sel;
  3009. else
  3010. temp &= ~sel;
  3011. I915_WRITE(PCH_DPLL_SEL, temp);
  3012. }
  3013. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3014. * transcoder, and we actually should do this to not upset any PCH
  3015. * transcoder that already use the clock when we share it.
  3016. *
  3017. * Note that enable_shared_dpll tries to do the right thing, but
  3018. * get_shared_dpll unconditionally resets the pll - we need that to have
  3019. * the right LVDS enable sequence. */
  3020. intel_enable_shared_dpll(intel_crtc);
  3021. /* set transcoder timing, panel must allow it */
  3022. assert_panel_unlocked(dev_priv, pipe);
  3023. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3024. intel_fdi_normal_train(crtc);
  3025. /* For PCH DP, enable TRANS_DP_CTL */
  3026. if (HAS_PCH_CPT(dev) &&
  3027. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3028. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3029. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3030. reg = TRANS_DP_CTL(pipe);
  3031. temp = I915_READ(reg);
  3032. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3033. TRANS_DP_SYNC_MASK |
  3034. TRANS_DP_BPC_MASK);
  3035. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3036. TRANS_DP_ENH_FRAMING);
  3037. temp |= bpc << 9; /* same format but at 11:9 */
  3038. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3039. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3040. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3041. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3042. switch (intel_trans_dp_port_sel(crtc)) {
  3043. case PCH_DP_B:
  3044. temp |= TRANS_DP_PORT_SEL_B;
  3045. break;
  3046. case PCH_DP_C:
  3047. temp |= TRANS_DP_PORT_SEL_C;
  3048. break;
  3049. case PCH_DP_D:
  3050. temp |= TRANS_DP_PORT_SEL_D;
  3051. break;
  3052. default:
  3053. BUG();
  3054. }
  3055. I915_WRITE(reg, temp);
  3056. }
  3057. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3058. }
  3059. static void lpt_pch_enable(struct drm_crtc *crtc)
  3060. {
  3061. struct drm_device *dev = crtc->dev;
  3062. struct drm_i915_private *dev_priv = dev->dev_private;
  3063. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3064. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3065. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3066. lpt_program_iclkip(crtc);
  3067. /* Set transcoder timing. */
  3068. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3069. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3070. }
  3071. static void intel_put_shared_dpll(struct intel_crtc *crtc)
  3072. {
  3073. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3074. if (pll == NULL)
  3075. return;
  3076. if (pll->refcount == 0) {
  3077. WARN(1, "bad %s refcount\n", pll->name);
  3078. return;
  3079. }
  3080. if (--pll->refcount == 0) {
  3081. WARN_ON(pll->on);
  3082. WARN_ON(pll->active);
  3083. }
  3084. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3085. }
  3086. static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3087. {
  3088. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3089. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3090. enum intel_dpll_id i;
  3091. if (pll) {
  3092. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3093. crtc->base.base.id, pll->name);
  3094. intel_put_shared_dpll(crtc);
  3095. }
  3096. if (HAS_PCH_IBX(dev_priv->dev)) {
  3097. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3098. i = (enum intel_dpll_id) crtc->pipe;
  3099. pll = &dev_priv->shared_dplls[i];
  3100. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3101. crtc->base.base.id, pll->name);
  3102. WARN_ON(pll->refcount);
  3103. goto found;
  3104. }
  3105. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3106. pll = &dev_priv->shared_dplls[i];
  3107. /* Only want to check enabled timings first */
  3108. if (pll->refcount == 0)
  3109. continue;
  3110. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3111. sizeof(pll->hw_state)) == 0) {
  3112. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3113. crtc->base.base.id,
  3114. pll->name, pll->refcount, pll->active);
  3115. goto found;
  3116. }
  3117. }
  3118. /* Ok no matching timings, maybe there's a free one? */
  3119. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3120. pll = &dev_priv->shared_dplls[i];
  3121. if (pll->refcount == 0) {
  3122. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3123. crtc->base.base.id, pll->name);
  3124. goto found;
  3125. }
  3126. }
  3127. return NULL;
  3128. found:
  3129. if (pll->refcount == 0)
  3130. pll->hw_state = crtc->config.dpll_hw_state;
  3131. crtc->config.shared_dpll = i;
  3132. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3133. pipe_name(crtc->pipe));
  3134. pll->refcount++;
  3135. return pll;
  3136. }
  3137. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3138. {
  3139. struct drm_i915_private *dev_priv = dev->dev_private;
  3140. int dslreg = PIPEDSL(pipe);
  3141. u32 temp;
  3142. temp = I915_READ(dslreg);
  3143. udelay(500);
  3144. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3145. if (wait_for(I915_READ(dslreg) != temp, 5))
  3146. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3147. }
  3148. }
  3149. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3150. {
  3151. struct drm_device *dev = crtc->base.dev;
  3152. struct drm_i915_private *dev_priv = dev->dev_private;
  3153. int pipe = crtc->pipe;
  3154. if (crtc->config.pch_pfit.enabled) {
  3155. /* Force use of hard-coded filter coefficients
  3156. * as some pre-programmed values are broken,
  3157. * e.g. x201.
  3158. */
  3159. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3160. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3161. PF_PIPE_SEL_IVB(pipe));
  3162. else
  3163. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3164. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3165. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3166. }
  3167. }
  3168. static void intel_enable_planes(struct drm_crtc *crtc)
  3169. {
  3170. struct drm_device *dev = crtc->dev;
  3171. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3172. struct drm_plane *plane;
  3173. struct intel_plane *intel_plane;
  3174. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3175. intel_plane = to_intel_plane(plane);
  3176. if (intel_plane->pipe == pipe)
  3177. intel_plane_restore(&intel_plane->base);
  3178. }
  3179. }
  3180. static void intel_disable_planes(struct drm_crtc *crtc)
  3181. {
  3182. struct drm_device *dev = crtc->dev;
  3183. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3184. struct drm_plane *plane;
  3185. struct intel_plane *intel_plane;
  3186. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3187. intel_plane = to_intel_plane(plane);
  3188. if (intel_plane->pipe == pipe)
  3189. intel_plane_disable(&intel_plane->base);
  3190. }
  3191. }
  3192. void hsw_enable_ips(struct intel_crtc *crtc)
  3193. {
  3194. struct drm_device *dev = crtc->base.dev;
  3195. struct drm_i915_private *dev_priv = dev->dev_private;
  3196. if (!crtc->config.ips_enabled)
  3197. return;
  3198. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3199. intel_wait_for_vblank(dev, crtc->pipe);
  3200. assert_plane_enabled(dev_priv, crtc->plane);
  3201. if (IS_BROADWELL(dev)) {
  3202. mutex_lock(&dev_priv->rps.hw_lock);
  3203. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3204. mutex_unlock(&dev_priv->rps.hw_lock);
  3205. /* Quoting Art Runyan: "its not safe to expect any particular
  3206. * value in IPS_CTL bit 31 after enabling IPS through the
  3207. * mailbox." Moreover, the mailbox may return a bogus state,
  3208. * so we need to just enable it and continue on.
  3209. */
  3210. } else {
  3211. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3212. /* The bit only becomes 1 in the next vblank, so this wait here
  3213. * is essentially intel_wait_for_vblank. If we don't have this
  3214. * and don't wait for vblanks until the end of crtc_enable, then
  3215. * the HW state readout code will complain that the expected
  3216. * IPS_CTL value is not the one we read. */
  3217. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3218. DRM_ERROR("Timed out waiting for IPS enable\n");
  3219. }
  3220. }
  3221. void hsw_disable_ips(struct intel_crtc *crtc)
  3222. {
  3223. struct drm_device *dev = crtc->base.dev;
  3224. struct drm_i915_private *dev_priv = dev->dev_private;
  3225. if (!crtc->config.ips_enabled)
  3226. return;
  3227. assert_plane_enabled(dev_priv, crtc->plane);
  3228. if (IS_BROADWELL(dev)) {
  3229. mutex_lock(&dev_priv->rps.hw_lock);
  3230. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3231. mutex_unlock(&dev_priv->rps.hw_lock);
  3232. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3233. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3234. DRM_ERROR("Timed out waiting for IPS disable\n");
  3235. } else {
  3236. I915_WRITE(IPS_CTL, 0);
  3237. POSTING_READ(IPS_CTL);
  3238. }
  3239. /* We need to wait for a vblank before we can disable the plane. */
  3240. intel_wait_for_vblank(dev, crtc->pipe);
  3241. }
  3242. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3243. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3244. {
  3245. struct drm_device *dev = crtc->dev;
  3246. struct drm_i915_private *dev_priv = dev->dev_private;
  3247. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3248. enum pipe pipe = intel_crtc->pipe;
  3249. int palreg = PALETTE(pipe);
  3250. int i;
  3251. bool reenable_ips = false;
  3252. /* The clocks have to be on to load the palette. */
  3253. if (!crtc->enabled || !intel_crtc->active)
  3254. return;
  3255. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3256. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3257. assert_dsi_pll_enabled(dev_priv);
  3258. else
  3259. assert_pll_enabled(dev_priv, pipe);
  3260. }
  3261. /* use legacy palette for Ironlake */
  3262. if (HAS_PCH_SPLIT(dev))
  3263. palreg = LGC_PALETTE(pipe);
  3264. /* Workaround : Do not read or write the pipe palette/gamma data while
  3265. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3266. */
  3267. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3268. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3269. GAMMA_MODE_MODE_SPLIT)) {
  3270. hsw_disable_ips(intel_crtc);
  3271. reenable_ips = true;
  3272. }
  3273. for (i = 0; i < 256; i++) {
  3274. I915_WRITE(palreg + 4 * i,
  3275. (intel_crtc->lut_r[i] << 16) |
  3276. (intel_crtc->lut_g[i] << 8) |
  3277. intel_crtc->lut_b[i]);
  3278. }
  3279. if (reenable_ips)
  3280. hsw_enable_ips(intel_crtc);
  3281. }
  3282. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3283. {
  3284. if (!enable && intel_crtc->overlay) {
  3285. struct drm_device *dev = intel_crtc->base.dev;
  3286. struct drm_i915_private *dev_priv = dev->dev_private;
  3287. mutex_lock(&dev->struct_mutex);
  3288. dev_priv->mm.interruptible = false;
  3289. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3290. dev_priv->mm.interruptible = true;
  3291. mutex_unlock(&dev->struct_mutex);
  3292. }
  3293. /* Let userspace switch the overlay on again. In most cases userspace
  3294. * has to recompute where to put it anyway.
  3295. */
  3296. }
  3297. /**
  3298. * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
  3299. * cursor plane briefly if not already running after enabling the display
  3300. * plane.
  3301. * This workaround avoids occasional blank screens when self refresh is
  3302. * enabled.
  3303. */
  3304. static void
  3305. g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
  3306. {
  3307. u32 cntl = I915_READ(CURCNTR(pipe));
  3308. if ((cntl & CURSOR_MODE) == 0) {
  3309. u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
  3310. I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
  3311. I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
  3312. intel_wait_for_vblank(dev_priv->dev, pipe);
  3313. I915_WRITE(CURCNTR(pipe), cntl);
  3314. I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
  3315. I915_WRITE(FW_BLC_SELF, fw_bcl_self);
  3316. }
  3317. }
  3318. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3319. {
  3320. struct drm_device *dev = crtc->dev;
  3321. struct drm_i915_private *dev_priv = dev->dev_private;
  3322. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3323. int pipe = intel_crtc->pipe;
  3324. int plane = intel_crtc->plane;
  3325. intel_enable_primary_hw_plane(dev_priv, plane, pipe);
  3326. intel_enable_planes(crtc);
  3327. /* The fixup needs to happen before cursor is enabled */
  3328. if (IS_G4X(dev))
  3329. g4x_fixup_plane(dev_priv, pipe);
  3330. intel_crtc_update_cursor(crtc, true);
  3331. intel_crtc_dpms_overlay(intel_crtc, true);
  3332. hsw_enable_ips(intel_crtc);
  3333. mutex_lock(&dev->struct_mutex);
  3334. intel_update_fbc(dev);
  3335. intel_edp_psr_update(dev);
  3336. mutex_unlock(&dev->struct_mutex);
  3337. }
  3338. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3339. {
  3340. struct drm_device *dev = crtc->dev;
  3341. struct drm_i915_private *dev_priv = dev->dev_private;
  3342. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3343. int pipe = intel_crtc->pipe;
  3344. int plane = intel_crtc->plane;
  3345. intel_crtc_wait_for_pending_flips(crtc);
  3346. drm_crtc_vblank_off(crtc);
  3347. if (dev_priv->fbc.plane == plane)
  3348. intel_disable_fbc(dev);
  3349. hsw_disable_ips(intel_crtc);
  3350. intel_crtc_dpms_overlay(intel_crtc, false);
  3351. intel_crtc_update_cursor(crtc, false);
  3352. intel_disable_planes(crtc);
  3353. intel_disable_primary_hw_plane(dev_priv, plane, pipe);
  3354. }
  3355. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3356. {
  3357. struct drm_device *dev = crtc->dev;
  3358. struct drm_i915_private *dev_priv = dev->dev_private;
  3359. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3360. struct intel_encoder *encoder;
  3361. int pipe = intel_crtc->pipe;
  3362. enum plane plane = intel_crtc->plane;
  3363. WARN_ON(!crtc->enabled);
  3364. if (intel_crtc->active)
  3365. return;
  3366. if (intel_crtc->config.has_pch_encoder)
  3367. intel_prepare_shared_dpll(intel_crtc);
  3368. if (intel_crtc->config.has_dp_encoder)
  3369. intel_dp_set_m_n(intel_crtc);
  3370. intel_set_pipe_timings(intel_crtc);
  3371. if (intel_crtc->config.has_pch_encoder) {
  3372. intel_cpu_transcoder_set_m_n(intel_crtc,
  3373. &intel_crtc->config.fdi_m_n);
  3374. }
  3375. ironlake_set_pipeconf(crtc);
  3376. /* Set up the display plane register */
  3377. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  3378. POSTING_READ(DSPCNTR(plane));
  3379. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3380. crtc->x, crtc->y);
  3381. intel_crtc->active = true;
  3382. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3383. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3384. for_each_encoder_on_crtc(dev, crtc, encoder)
  3385. if (encoder->pre_enable)
  3386. encoder->pre_enable(encoder);
  3387. if (intel_crtc->config.has_pch_encoder) {
  3388. /* Note: FDI PLL enabling _must_ be done before we enable the
  3389. * cpu pipes, hence this is separate from all the other fdi/pch
  3390. * enabling. */
  3391. ironlake_fdi_pll_enable(intel_crtc);
  3392. } else {
  3393. assert_fdi_tx_disabled(dev_priv, pipe);
  3394. assert_fdi_rx_disabled(dev_priv, pipe);
  3395. }
  3396. ironlake_pfit_enable(intel_crtc);
  3397. /*
  3398. * On ILK+ LUT must be loaded before the pipe is running but with
  3399. * clocks enabled
  3400. */
  3401. intel_crtc_load_lut(crtc);
  3402. intel_update_watermarks(crtc);
  3403. intel_enable_pipe(intel_crtc);
  3404. if (intel_crtc->config.has_pch_encoder)
  3405. ironlake_pch_enable(crtc);
  3406. for_each_encoder_on_crtc(dev, crtc, encoder)
  3407. encoder->enable(encoder);
  3408. if (HAS_PCH_CPT(dev))
  3409. cpt_verify_modeset(dev, intel_crtc->pipe);
  3410. intel_crtc_enable_planes(crtc);
  3411. drm_crtc_vblank_on(crtc);
  3412. }
  3413. /* IPS only exists on ULT machines and is tied to pipe A. */
  3414. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3415. {
  3416. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3417. }
  3418. /*
  3419. * This implements the workaround described in the "notes" section of the mode
  3420. * set sequence documentation. When going from no pipes or single pipe to
  3421. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3422. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3423. */
  3424. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3425. {
  3426. struct drm_device *dev = crtc->base.dev;
  3427. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3428. /* We want to get the other_active_crtc only if there's only 1 other
  3429. * active crtc. */
  3430. for_each_intel_crtc(dev, crtc_it) {
  3431. if (!crtc_it->active || crtc_it == crtc)
  3432. continue;
  3433. if (other_active_crtc)
  3434. return;
  3435. other_active_crtc = crtc_it;
  3436. }
  3437. if (!other_active_crtc)
  3438. return;
  3439. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3440. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3441. }
  3442. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3443. {
  3444. struct drm_device *dev = crtc->dev;
  3445. struct drm_i915_private *dev_priv = dev->dev_private;
  3446. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3447. struct intel_encoder *encoder;
  3448. int pipe = intel_crtc->pipe;
  3449. enum plane plane = intel_crtc->plane;
  3450. WARN_ON(!crtc->enabled);
  3451. if (intel_crtc->active)
  3452. return;
  3453. if (intel_crtc->config.has_dp_encoder)
  3454. intel_dp_set_m_n(intel_crtc);
  3455. intel_set_pipe_timings(intel_crtc);
  3456. if (intel_crtc->config.has_pch_encoder) {
  3457. intel_cpu_transcoder_set_m_n(intel_crtc,
  3458. &intel_crtc->config.fdi_m_n);
  3459. }
  3460. haswell_set_pipeconf(crtc);
  3461. intel_set_pipe_csc(crtc);
  3462. /* Set up the display plane register */
  3463. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  3464. POSTING_READ(DSPCNTR(plane));
  3465. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3466. crtc->x, crtc->y);
  3467. intel_crtc->active = true;
  3468. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3469. if (intel_crtc->config.has_pch_encoder)
  3470. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3471. if (intel_crtc->config.has_pch_encoder)
  3472. dev_priv->display.fdi_link_train(crtc);
  3473. for_each_encoder_on_crtc(dev, crtc, encoder)
  3474. if (encoder->pre_enable)
  3475. encoder->pre_enable(encoder);
  3476. intel_ddi_enable_pipe_clock(intel_crtc);
  3477. ironlake_pfit_enable(intel_crtc);
  3478. /*
  3479. * On ILK+ LUT must be loaded before the pipe is running but with
  3480. * clocks enabled
  3481. */
  3482. intel_crtc_load_lut(crtc);
  3483. intel_ddi_set_pipe_settings(crtc);
  3484. intel_ddi_enable_transcoder_func(crtc);
  3485. intel_update_watermarks(crtc);
  3486. intel_enable_pipe(intel_crtc);
  3487. if (intel_crtc->config.has_pch_encoder)
  3488. lpt_pch_enable(crtc);
  3489. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3490. encoder->enable(encoder);
  3491. intel_opregion_notify_encoder(encoder, true);
  3492. }
  3493. /* If we change the relative order between pipe/planes enabling, we need
  3494. * to change the workaround. */
  3495. haswell_mode_set_planes_workaround(intel_crtc);
  3496. intel_crtc_enable_planes(crtc);
  3497. drm_crtc_vblank_on(crtc);
  3498. }
  3499. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3500. {
  3501. struct drm_device *dev = crtc->base.dev;
  3502. struct drm_i915_private *dev_priv = dev->dev_private;
  3503. int pipe = crtc->pipe;
  3504. /* To avoid upsetting the power well on haswell only disable the pfit if
  3505. * it's in use. The hw state code will make sure we get this right. */
  3506. if (crtc->config.pch_pfit.enabled) {
  3507. I915_WRITE(PF_CTL(pipe), 0);
  3508. I915_WRITE(PF_WIN_POS(pipe), 0);
  3509. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3510. }
  3511. }
  3512. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3513. {
  3514. struct drm_device *dev = crtc->dev;
  3515. struct drm_i915_private *dev_priv = dev->dev_private;
  3516. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3517. struct intel_encoder *encoder;
  3518. int pipe = intel_crtc->pipe;
  3519. u32 reg, temp;
  3520. if (!intel_crtc->active)
  3521. return;
  3522. intel_crtc_disable_planes(crtc);
  3523. for_each_encoder_on_crtc(dev, crtc, encoder)
  3524. encoder->disable(encoder);
  3525. if (intel_crtc->config.has_pch_encoder)
  3526. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3527. intel_disable_pipe(dev_priv, pipe);
  3528. ironlake_pfit_disable(intel_crtc);
  3529. for_each_encoder_on_crtc(dev, crtc, encoder)
  3530. if (encoder->post_disable)
  3531. encoder->post_disable(encoder);
  3532. if (intel_crtc->config.has_pch_encoder) {
  3533. ironlake_fdi_disable(crtc);
  3534. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3535. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3536. if (HAS_PCH_CPT(dev)) {
  3537. /* disable TRANS_DP_CTL */
  3538. reg = TRANS_DP_CTL(pipe);
  3539. temp = I915_READ(reg);
  3540. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3541. TRANS_DP_PORT_SEL_MASK);
  3542. temp |= TRANS_DP_PORT_SEL_NONE;
  3543. I915_WRITE(reg, temp);
  3544. /* disable DPLL_SEL */
  3545. temp = I915_READ(PCH_DPLL_SEL);
  3546. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3547. I915_WRITE(PCH_DPLL_SEL, temp);
  3548. }
  3549. /* disable PCH DPLL */
  3550. intel_disable_shared_dpll(intel_crtc);
  3551. ironlake_fdi_pll_disable(intel_crtc);
  3552. }
  3553. intel_crtc->active = false;
  3554. intel_update_watermarks(crtc);
  3555. mutex_lock(&dev->struct_mutex);
  3556. intel_update_fbc(dev);
  3557. intel_edp_psr_update(dev);
  3558. mutex_unlock(&dev->struct_mutex);
  3559. }
  3560. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3561. {
  3562. struct drm_device *dev = crtc->dev;
  3563. struct drm_i915_private *dev_priv = dev->dev_private;
  3564. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3565. struct intel_encoder *encoder;
  3566. int pipe = intel_crtc->pipe;
  3567. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3568. if (!intel_crtc->active)
  3569. return;
  3570. intel_crtc_disable_planes(crtc);
  3571. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3572. intel_opregion_notify_encoder(encoder, false);
  3573. encoder->disable(encoder);
  3574. }
  3575. if (intel_crtc->config.has_pch_encoder)
  3576. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3577. intel_disable_pipe(dev_priv, pipe);
  3578. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3579. ironlake_pfit_disable(intel_crtc);
  3580. intel_ddi_disable_pipe_clock(intel_crtc);
  3581. for_each_encoder_on_crtc(dev, crtc, encoder)
  3582. if (encoder->post_disable)
  3583. encoder->post_disable(encoder);
  3584. if (intel_crtc->config.has_pch_encoder) {
  3585. lpt_disable_pch_transcoder(dev_priv);
  3586. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3587. intel_ddi_fdi_disable(crtc);
  3588. }
  3589. intel_crtc->active = false;
  3590. intel_update_watermarks(crtc);
  3591. mutex_lock(&dev->struct_mutex);
  3592. intel_update_fbc(dev);
  3593. intel_edp_psr_update(dev);
  3594. mutex_unlock(&dev->struct_mutex);
  3595. }
  3596. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3597. {
  3598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3599. intel_put_shared_dpll(intel_crtc);
  3600. }
  3601. static void haswell_crtc_off(struct drm_crtc *crtc)
  3602. {
  3603. intel_ddi_put_crtc_pll(crtc);
  3604. }
  3605. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3606. {
  3607. struct drm_device *dev = crtc->base.dev;
  3608. struct drm_i915_private *dev_priv = dev->dev_private;
  3609. struct intel_crtc_config *pipe_config = &crtc->config;
  3610. if (!crtc->config.gmch_pfit.control)
  3611. return;
  3612. /*
  3613. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3614. * according to register description and PRM.
  3615. */
  3616. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3617. assert_pipe_disabled(dev_priv, crtc->pipe);
  3618. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3619. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3620. /* Border color in case we don't scale up to the full screen. Black by
  3621. * default, change to something else for debugging. */
  3622. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3623. }
  3624. #define for_each_power_domain(domain, mask) \
  3625. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3626. if ((1 << (domain)) & (mask))
  3627. enum intel_display_power_domain
  3628. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3629. {
  3630. struct drm_device *dev = intel_encoder->base.dev;
  3631. struct intel_digital_port *intel_dig_port;
  3632. switch (intel_encoder->type) {
  3633. case INTEL_OUTPUT_UNKNOWN:
  3634. /* Only DDI platforms should ever use this output type */
  3635. WARN_ON_ONCE(!HAS_DDI(dev));
  3636. case INTEL_OUTPUT_DISPLAYPORT:
  3637. case INTEL_OUTPUT_HDMI:
  3638. case INTEL_OUTPUT_EDP:
  3639. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3640. switch (intel_dig_port->port) {
  3641. case PORT_A:
  3642. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3643. case PORT_B:
  3644. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3645. case PORT_C:
  3646. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3647. case PORT_D:
  3648. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3649. default:
  3650. WARN_ON_ONCE(1);
  3651. return POWER_DOMAIN_PORT_OTHER;
  3652. }
  3653. case INTEL_OUTPUT_ANALOG:
  3654. return POWER_DOMAIN_PORT_CRT;
  3655. case INTEL_OUTPUT_DSI:
  3656. return POWER_DOMAIN_PORT_DSI;
  3657. default:
  3658. return POWER_DOMAIN_PORT_OTHER;
  3659. }
  3660. }
  3661. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3662. {
  3663. struct drm_device *dev = crtc->dev;
  3664. struct intel_encoder *intel_encoder;
  3665. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3666. enum pipe pipe = intel_crtc->pipe;
  3667. bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
  3668. unsigned long mask;
  3669. enum transcoder transcoder;
  3670. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3671. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3672. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3673. if (pfit_enabled)
  3674. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3675. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3676. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3677. return mask;
  3678. }
  3679. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  3680. bool enable)
  3681. {
  3682. if (dev_priv->power_domains.init_power_on == enable)
  3683. return;
  3684. if (enable)
  3685. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  3686. else
  3687. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  3688. dev_priv->power_domains.init_power_on = enable;
  3689. }
  3690. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3691. {
  3692. struct drm_i915_private *dev_priv = dev->dev_private;
  3693. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3694. struct intel_crtc *crtc;
  3695. /*
  3696. * First get all needed power domains, then put all unneeded, to avoid
  3697. * any unnecessary toggling of the power wells.
  3698. */
  3699. for_each_intel_crtc(dev, crtc) {
  3700. enum intel_display_power_domain domain;
  3701. if (!crtc->base.enabled)
  3702. continue;
  3703. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3704. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3705. intel_display_power_get(dev_priv, domain);
  3706. }
  3707. for_each_intel_crtc(dev, crtc) {
  3708. enum intel_display_power_domain domain;
  3709. for_each_power_domain(domain, crtc->enabled_power_domains)
  3710. intel_display_power_put(dev_priv, domain);
  3711. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3712. }
  3713. intel_display_set_init_power(dev_priv, false);
  3714. }
  3715. int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3716. {
  3717. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3718. /* Obtain SKU information */
  3719. mutex_lock(&dev_priv->dpio_lock);
  3720. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3721. CCK_FUSE_HPLL_FREQ_MASK;
  3722. mutex_unlock(&dev_priv->dpio_lock);
  3723. return vco_freq[hpll_freq];
  3724. }
  3725. /* Adjust CDclk dividers to allow high res or save power if possible */
  3726. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3727. {
  3728. struct drm_i915_private *dev_priv = dev->dev_private;
  3729. u32 val, cmd;
  3730. WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
  3731. dev_priv->vlv_cdclk_freq = cdclk;
  3732. if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
  3733. cmd = 2;
  3734. else if (cdclk == 266)
  3735. cmd = 1;
  3736. else
  3737. cmd = 0;
  3738. mutex_lock(&dev_priv->rps.hw_lock);
  3739. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3740. val &= ~DSPFREQGUAR_MASK;
  3741. val |= (cmd << DSPFREQGUAR_SHIFT);
  3742. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3743. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3744. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3745. 50)) {
  3746. DRM_ERROR("timed out waiting for CDclk change\n");
  3747. }
  3748. mutex_unlock(&dev_priv->rps.hw_lock);
  3749. if (cdclk == 400) {
  3750. u32 divider, vco;
  3751. vco = valleyview_get_vco(dev_priv);
  3752. divider = ((vco << 1) / cdclk) - 1;
  3753. mutex_lock(&dev_priv->dpio_lock);
  3754. /* adjust cdclk divider */
  3755. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3756. val &= ~0xf;
  3757. val |= divider;
  3758. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3759. mutex_unlock(&dev_priv->dpio_lock);
  3760. }
  3761. mutex_lock(&dev_priv->dpio_lock);
  3762. /* adjust self-refresh exit latency value */
  3763. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3764. val &= ~0x7f;
  3765. /*
  3766. * For high bandwidth configs, we set a higher latency in the bunit
  3767. * so that the core display fetch happens in time to avoid underruns.
  3768. */
  3769. if (cdclk == 400)
  3770. val |= 4500 / 250; /* 4.5 usec */
  3771. else
  3772. val |= 3000 / 250; /* 3.0 usec */
  3773. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3774. mutex_unlock(&dev_priv->dpio_lock);
  3775. /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
  3776. intel_i2c_reset(dev);
  3777. }
  3778. int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
  3779. {
  3780. int cur_cdclk, vco;
  3781. int divider;
  3782. vco = valleyview_get_vco(dev_priv);
  3783. mutex_lock(&dev_priv->dpio_lock);
  3784. divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3785. mutex_unlock(&dev_priv->dpio_lock);
  3786. divider &= 0xf;
  3787. cur_cdclk = (vco << 1) / (divider + 1);
  3788. return cur_cdclk;
  3789. }
  3790. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  3791. int max_pixclk)
  3792. {
  3793. /*
  3794. * Really only a few cases to deal with, as only 4 CDclks are supported:
  3795. * 200MHz
  3796. * 267MHz
  3797. * 320MHz
  3798. * 400MHz
  3799. * So we check to see whether we're above 90% of the lower bin and
  3800. * adjust if needed.
  3801. */
  3802. if (max_pixclk > 288000) {
  3803. return 400;
  3804. } else if (max_pixclk > 240000) {
  3805. return 320;
  3806. } else
  3807. return 266;
  3808. /* Looks like the 200MHz CDclk freq doesn't work on some configs */
  3809. }
  3810. /* compute the max pixel clock for new configuration */
  3811. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  3812. {
  3813. struct drm_device *dev = dev_priv->dev;
  3814. struct intel_crtc *intel_crtc;
  3815. int max_pixclk = 0;
  3816. for_each_intel_crtc(dev, intel_crtc) {
  3817. if (intel_crtc->new_enabled)
  3818. max_pixclk = max(max_pixclk,
  3819. intel_crtc->new_config->adjusted_mode.crtc_clock);
  3820. }
  3821. return max_pixclk;
  3822. }
  3823. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  3824. unsigned *prepare_pipes)
  3825. {
  3826. struct drm_i915_private *dev_priv = dev->dev_private;
  3827. struct intel_crtc *intel_crtc;
  3828. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3829. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  3830. dev_priv->vlv_cdclk_freq)
  3831. return;
  3832. /* disable/enable all currently active pipes while we change cdclk */
  3833. for_each_intel_crtc(dev, intel_crtc)
  3834. if (intel_crtc->base.enabled)
  3835. *prepare_pipes |= (1 << intel_crtc->pipe);
  3836. }
  3837. static void valleyview_modeset_global_resources(struct drm_device *dev)
  3838. {
  3839. struct drm_i915_private *dev_priv = dev->dev_private;
  3840. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3841. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  3842. if (req_cdclk != dev_priv->vlv_cdclk_freq)
  3843. valleyview_set_cdclk(dev, req_cdclk);
  3844. modeset_update_crtc_power_domains(dev);
  3845. }
  3846. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3847. {
  3848. struct drm_device *dev = crtc->dev;
  3849. struct drm_i915_private *dev_priv = dev->dev_private;
  3850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3851. struct intel_encoder *encoder;
  3852. int pipe = intel_crtc->pipe;
  3853. int plane = intel_crtc->plane;
  3854. bool is_dsi;
  3855. u32 dspcntr;
  3856. WARN_ON(!crtc->enabled);
  3857. if (intel_crtc->active)
  3858. return;
  3859. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3860. if (!is_dsi && !IS_CHERRYVIEW(dev))
  3861. vlv_prepare_pll(intel_crtc);
  3862. /* Set up the display plane register */
  3863. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3864. if (intel_crtc->config.has_dp_encoder)
  3865. intel_dp_set_m_n(intel_crtc);
  3866. intel_set_pipe_timings(intel_crtc);
  3867. /* pipesrc and dspsize control the size that is scaled from,
  3868. * which should always be the user's requested size.
  3869. */
  3870. I915_WRITE(DSPSIZE(plane),
  3871. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3872. (intel_crtc->config.pipe_src_w - 1));
  3873. I915_WRITE(DSPPOS(plane), 0);
  3874. i9xx_set_pipeconf(intel_crtc);
  3875. I915_WRITE(DSPCNTR(plane), dspcntr);
  3876. POSTING_READ(DSPCNTR(plane));
  3877. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3878. crtc->x, crtc->y);
  3879. intel_crtc->active = true;
  3880. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3881. for_each_encoder_on_crtc(dev, crtc, encoder)
  3882. if (encoder->pre_pll_enable)
  3883. encoder->pre_pll_enable(encoder);
  3884. if (!is_dsi) {
  3885. if (IS_CHERRYVIEW(dev))
  3886. chv_enable_pll(intel_crtc);
  3887. else
  3888. vlv_enable_pll(intel_crtc);
  3889. }
  3890. for_each_encoder_on_crtc(dev, crtc, encoder)
  3891. if (encoder->pre_enable)
  3892. encoder->pre_enable(encoder);
  3893. i9xx_pfit_enable(intel_crtc);
  3894. intel_crtc_load_lut(crtc);
  3895. intel_update_watermarks(crtc);
  3896. intel_enable_pipe(intel_crtc);
  3897. for_each_encoder_on_crtc(dev, crtc, encoder)
  3898. encoder->enable(encoder);
  3899. intel_crtc_enable_planes(crtc);
  3900. drm_crtc_vblank_on(crtc);
  3901. /* Underruns don't raise interrupts, so check manually. */
  3902. i9xx_check_fifo_underruns(dev);
  3903. }
  3904. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  3905. {
  3906. struct drm_device *dev = crtc->base.dev;
  3907. struct drm_i915_private *dev_priv = dev->dev_private;
  3908. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  3909. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  3910. }
  3911. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3912. {
  3913. struct drm_device *dev = crtc->dev;
  3914. struct drm_i915_private *dev_priv = dev->dev_private;
  3915. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3916. struct intel_encoder *encoder;
  3917. int pipe = intel_crtc->pipe;
  3918. int plane = intel_crtc->plane;
  3919. u32 dspcntr;
  3920. WARN_ON(!crtc->enabled);
  3921. if (intel_crtc->active)
  3922. return;
  3923. i9xx_set_pll_dividers(intel_crtc);
  3924. /* Set up the display plane register */
  3925. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3926. if (pipe == 0)
  3927. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3928. else
  3929. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3930. if (intel_crtc->config.has_dp_encoder)
  3931. intel_dp_set_m_n(intel_crtc);
  3932. intel_set_pipe_timings(intel_crtc);
  3933. /* pipesrc and dspsize control the size that is scaled from,
  3934. * which should always be the user's requested size.
  3935. */
  3936. I915_WRITE(DSPSIZE(plane),
  3937. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3938. (intel_crtc->config.pipe_src_w - 1));
  3939. I915_WRITE(DSPPOS(plane), 0);
  3940. i9xx_set_pipeconf(intel_crtc);
  3941. I915_WRITE(DSPCNTR(plane), dspcntr);
  3942. POSTING_READ(DSPCNTR(plane));
  3943. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3944. crtc->x, crtc->y);
  3945. intel_crtc->active = true;
  3946. if (!IS_GEN2(dev))
  3947. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3948. for_each_encoder_on_crtc(dev, crtc, encoder)
  3949. if (encoder->pre_enable)
  3950. encoder->pre_enable(encoder);
  3951. i9xx_enable_pll(intel_crtc);
  3952. i9xx_pfit_enable(intel_crtc);
  3953. intel_crtc_load_lut(crtc);
  3954. intel_update_watermarks(crtc);
  3955. intel_enable_pipe(intel_crtc);
  3956. for_each_encoder_on_crtc(dev, crtc, encoder)
  3957. encoder->enable(encoder);
  3958. intel_crtc_enable_planes(crtc);
  3959. /*
  3960. * Gen2 reports pipe underruns whenever all planes are disabled.
  3961. * So don't enable underrun reporting before at least some planes
  3962. * are enabled.
  3963. * FIXME: Need to fix the logic to work when we turn off all planes
  3964. * but leave the pipe running.
  3965. */
  3966. if (IS_GEN2(dev))
  3967. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3968. drm_crtc_vblank_on(crtc);
  3969. /* Underruns don't raise interrupts, so check manually. */
  3970. i9xx_check_fifo_underruns(dev);
  3971. }
  3972. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  3973. {
  3974. struct drm_device *dev = crtc->base.dev;
  3975. struct drm_i915_private *dev_priv = dev->dev_private;
  3976. if (!crtc->config.gmch_pfit.control)
  3977. return;
  3978. assert_pipe_disabled(dev_priv, crtc->pipe);
  3979. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  3980. I915_READ(PFIT_CONTROL));
  3981. I915_WRITE(PFIT_CONTROL, 0);
  3982. }
  3983. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  3984. {
  3985. struct drm_device *dev = crtc->dev;
  3986. struct drm_i915_private *dev_priv = dev->dev_private;
  3987. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3988. struct intel_encoder *encoder;
  3989. int pipe = intel_crtc->pipe;
  3990. if (!intel_crtc->active)
  3991. return;
  3992. /*
  3993. * Gen2 reports pipe underruns whenever all planes are disabled.
  3994. * So diasble underrun reporting before all the planes get disabled.
  3995. * FIXME: Need to fix the logic to work when we turn off all planes
  3996. * but leave the pipe running.
  3997. */
  3998. if (IS_GEN2(dev))
  3999. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4000. intel_crtc_disable_planes(crtc);
  4001. for_each_encoder_on_crtc(dev, crtc, encoder)
  4002. encoder->disable(encoder);
  4003. /*
  4004. * On gen2 planes are double buffered but the pipe isn't, so we must
  4005. * wait for planes to fully turn off before disabling the pipe.
  4006. */
  4007. if (IS_GEN2(dev))
  4008. intel_wait_for_vblank(dev, pipe);
  4009. intel_disable_pipe(dev_priv, pipe);
  4010. i9xx_pfit_disable(intel_crtc);
  4011. for_each_encoder_on_crtc(dev, crtc, encoder)
  4012. if (encoder->post_disable)
  4013. encoder->post_disable(encoder);
  4014. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  4015. if (IS_CHERRYVIEW(dev))
  4016. chv_disable_pll(dev_priv, pipe);
  4017. else if (IS_VALLEYVIEW(dev))
  4018. vlv_disable_pll(dev_priv, pipe);
  4019. else
  4020. i9xx_disable_pll(dev_priv, pipe);
  4021. }
  4022. if (!IS_GEN2(dev))
  4023. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4024. intel_crtc->active = false;
  4025. intel_update_watermarks(crtc);
  4026. mutex_lock(&dev->struct_mutex);
  4027. intel_update_fbc(dev);
  4028. intel_edp_psr_update(dev);
  4029. mutex_unlock(&dev->struct_mutex);
  4030. }
  4031. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4032. {
  4033. }
  4034. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4035. bool enabled)
  4036. {
  4037. struct drm_device *dev = crtc->dev;
  4038. struct drm_i915_master_private *master_priv;
  4039. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4040. int pipe = intel_crtc->pipe;
  4041. if (!dev->primary->master)
  4042. return;
  4043. master_priv = dev->primary->master->driver_priv;
  4044. if (!master_priv->sarea_priv)
  4045. return;
  4046. switch (pipe) {
  4047. case 0:
  4048. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4049. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4050. break;
  4051. case 1:
  4052. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4053. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4054. break;
  4055. default:
  4056. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4057. break;
  4058. }
  4059. }
  4060. /**
  4061. * Sets the power management mode of the pipe and plane.
  4062. */
  4063. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4064. {
  4065. struct drm_device *dev = crtc->dev;
  4066. struct drm_i915_private *dev_priv = dev->dev_private;
  4067. struct intel_encoder *intel_encoder;
  4068. bool enable = false;
  4069. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4070. enable |= intel_encoder->connectors_active;
  4071. if (enable)
  4072. dev_priv->display.crtc_enable(crtc);
  4073. else
  4074. dev_priv->display.crtc_disable(crtc);
  4075. intel_crtc_update_sarea(crtc, enable);
  4076. }
  4077. static void intel_crtc_disable(struct drm_crtc *crtc)
  4078. {
  4079. struct drm_device *dev = crtc->dev;
  4080. struct drm_connector *connector;
  4081. struct drm_i915_private *dev_priv = dev->dev_private;
  4082. /* crtc should still be enabled when we disable it. */
  4083. WARN_ON(!crtc->enabled);
  4084. dev_priv->display.crtc_disable(crtc);
  4085. intel_crtc_update_sarea(crtc, false);
  4086. dev_priv->display.off(crtc);
  4087. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  4088. assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  4089. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  4090. if (crtc->primary->fb) {
  4091. mutex_lock(&dev->struct_mutex);
  4092. intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
  4093. mutex_unlock(&dev->struct_mutex);
  4094. crtc->primary->fb = NULL;
  4095. }
  4096. /* Update computed state. */
  4097. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4098. if (!connector->encoder || !connector->encoder->crtc)
  4099. continue;
  4100. if (connector->encoder->crtc != crtc)
  4101. continue;
  4102. connector->dpms = DRM_MODE_DPMS_OFF;
  4103. to_intel_encoder(connector->encoder)->connectors_active = false;
  4104. }
  4105. }
  4106. void intel_encoder_destroy(struct drm_encoder *encoder)
  4107. {
  4108. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4109. drm_encoder_cleanup(encoder);
  4110. kfree(intel_encoder);
  4111. }
  4112. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4113. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4114. * state of the entire output pipe. */
  4115. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4116. {
  4117. if (mode == DRM_MODE_DPMS_ON) {
  4118. encoder->connectors_active = true;
  4119. intel_crtc_update_dpms(encoder->base.crtc);
  4120. } else {
  4121. encoder->connectors_active = false;
  4122. intel_crtc_update_dpms(encoder->base.crtc);
  4123. }
  4124. }
  4125. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4126. * internal consistency). */
  4127. static void intel_connector_check_state(struct intel_connector *connector)
  4128. {
  4129. if (connector->get_hw_state(connector)) {
  4130. struct intel_encoder *encoder = connector->encoder;
  4131. struct drm_crtc *crtc;
  4132. bool encoder_enabled;
  4133. enum pipe pipe;
  4134. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4135. connector->base.base.id,
  4136. connector->base.name);
  4137. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4138. "wrong connector dpms state\n");
  4139. WARN(connector->base.encoder != &encoder->base,
  4140. "active connector not linked to encoder\n");
  4141. WARN(!encoder->connectors_active,
  4142. "encoder->connectors_active not set\n");
  4143. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4144. WARN(!encoder_enabled, "encoder not enabled\n");
  4145. if (WARN_ON(!encoder->base.crtc))
  4146. return;
  4147. crtc = encoder->base.crtc;
  4148. WARN(!crtc->enabled, "crtc not enabled\n");
  4149. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4150. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4151. "encoder active on the wrong pipe\n");
  4152. }
  4153. }
  4154. /* Even simpler default implementation, if there's really no special case to
  4155. * consider. */
  4156. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4157. {
  4158. /* All the simple cases only support two dpms states. */
  4159. if (mode != DRM_MODE_DPMS_ON)
  4160. mode = DRM_MODE_DPMS_OFF;
  4161. if (mode == connector->dpms)
  4162. return;
  4163. connector->dpms = mode;
  4164. /* Only need to change hw state when actually enabled */
  4165. if (connector->encoder)
  4166. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4167. intel_modeset_check_state(connector->dev);
  4168. }
  4169. /* Simple connector->get_hw_state implementation for encoders that support only
  4170. * one connector and no cloning and hence the encoder state determines the state
  4171. * of the connector. */
  4172. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4173. {
  4174. enum pipe pipe = 0;
  4175. struct intel_encoder *encoder = connector->encoder;
  4176. return encoder->get_hw_state(encoder, &pipe);
  4177. }
  4178. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4179. struct intel_crtc_config *pipe_config)
  4180. {
  4181. struct drm_i915_private *dev_priv = dev->dev_private;
  4182. struct intel_crtc *pipe_B_crtc =
  4183. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4184. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4185. pipe_name(pipe), pipe_config->fdi_lanes);
  4186. if (pipe_config->fdi_lanes > 4) {
  4187. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4188. pipe_name(pipe), pipe_config->fdi_lanes);
  4189. return false;
  4190. }
  4191. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4192. if (pipe_config->fdi_lanes > 2) {
  4193. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4194. pipe_config->fdi_lanes);
  4195. return false;
  4196. } else {
  4197. return true;
  4198. }
  4199. }
  4200. if (INTEL_INFO(dev)->num_pipes == 2)
  4201. return true;
  4202. /* Ivybridge 3 pipe is really complicated */
  4203. switch (pipe) {
  4204. case PIPE_A:
  4205. return true;
  4206. case PIPE_B:
  4207. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4208. pipe_config->fdi_lanes > 2) {
  4209. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4210. pipe_name(pipe), pipe_config->fdi_lanes);
  4211. return false;
  4212. }
  4213. return true;
  4214. case PIPE_C:
  4215. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4216. pipe_B_crtc->config.fdi_lanes <= 2) {
  4217. if (pipe_config->fdi_lanes > 2) {
  4218. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4219. pipe_name(pipe), pipe_config->fdi_lanes);
  4220. return false;
  4221. }
  4222. } else {
  4223. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4224. return false;
  4225. }
  4226. return true;
  4227. default:
  4228. BUG();
  4229. }
  4230. }
  4231. #define RETRY 1
  4232. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4233. struct intel_crtc_config *pipe_config)
  4234. {
  4235. struct drm_device *dev = intel_crtc->base.dev;
  4236. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4237. int lane, link_bw, fdi_dotclock;
  4238. bool setup_ok, needs_recompute = false;
  4239. retry:
  4240. /* FDI is a binary signal running at ~2.7GHz, encoding
  4241. * each output octet as 10 bits. The actual frequency
  4242. * is stored as a divider into a 100MHz clock, and the
  4243. * mode pixel clock is stored in units of 1KHz.
  4244. * Hence the bw of each lane in terms of the mode signal
  4245. * is:
  4246. */
  4247. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4248. fdi_dotclock = adjusted_mode->crtc_clock;
  4249. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4250. pipe_config->pipe_bpp);
  4251. pipe_config->fdi_lanes = lane;
  4252. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4253. link_bw, &pipe_config->fdi_m_n);
  4254. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4255. intel_crtc->pipe, pipe_config);
  4256. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4257. pipe_config->pipe_bpp -= 2*3;
  4258. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4259. pipe_config->pipe_bpp);
  4260. needs_recompute = true;
  4261. pipe_config->bw_constrained = true;
  4262. goto retry;
  4263. }
  4264. if (needs_recompute)
  4265. return RETRY;
  4266. return setup_ok ? 0 : -EINVAL;
  4267. }
  4268. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4269. struct intel_crtc_config *pipe_config)
  4270. {
  4271. pipe_config->ips_enabled = i915.enable_ips &&
  4272. hsw_crtc_supports_ips(crtc) &&
  4273. pipe_config->pipe_bpp <= 24;
  4274. }
  4275. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4276. struct intel_crtc_config *pipe_config)
  4277. {
  4278. struct drm_device *dev = crtc->base.dev;
  4279. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4280. /* FIXME should check pixel clock limits on all platforms */
  4281. if (INTEL_INFO(dev)->gen < 4) {
  4282. struct drm_i915_private *dev_priv = dev->dev_private;
  4283. int clock_limit =
  4284. dev_priv->display.get_display_clock_speed(dev);
  4285. /*
  4286. * Enable pixel doubling when the dot clock
  4287. * is > 90% of the (display) core speed.
  4288. *
  4289. * GDG double wide on either pipe,
  4290. * otherwise pipe A only.
  4291. */
  4292. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4293. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4294. clock_limit *= 2;
  4295. pipe_config->double_wide = true;
  4296. }
  4297. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4298. return -EINVAL;
  4299. }
  4300. /*
  4301. * Pipe horizontal size must be even in:
  4302. * - DVO ganged mode
  4303. * - LVDS dual channel mode
  4304. * - Double wide pipe
  4305. */
  4306. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4307. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4308. pipe_config->pipe_src_w &= ~1;
  4309. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4310. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4311. */
  4312. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4313. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4314. return -EINVAL;
  4315. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4316. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4317. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4318. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4319. * for lvds. */
  4320. pipe_config->pipe_bpp = 8*3;
  4321. }
  4322. if (HAS_IPS(dev))
  4323. hsw_compute_ips_config(crtc, pipe_config);
  4324. /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
  4325. * clock survives for now. */
  4326. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  4327. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4328. if (pipe_config->has_pch_encoder)
  4329. return ironlake_fdi_compute_config(crtc, pipe_config);
  4330. return 0;
  4331. }
  4332. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4333. {
  4334. return 400000; /* FIXME */
  4335. }
  4336. static int i945_get_display_clock_speed(struct drm_device *dev)
  4337. {
  4338. return 400000;
  4339. }
  4340. static int i915_get_display_clock_speed(struct drm_device *dev)
  4341. {
  4342. return 333000;
  4343. }
  4344. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4345. {
  4346. return 200000;
  4347. }
  4348. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4349. {
  4350. u16 gcfgc = 0;
  4351. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4352. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4353. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4354. return 267000;
  4355. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4356. return 333000;
  4357. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4358. return 444000;
  4359. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4360. return 200000;
  4361. default:
  4362. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4363. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4364. return 133000;
  4365. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4366. return 167000;
  4367. }
  4368. }
  4369. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4370. {
  4371. u16 gcfgc = 0;
  4372. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4373. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4374. return 133000;
  4375. else {
  4376. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4377. case GC_DISPLAY_CLOCK_333_MHZ:
  4378. return 333000;
  4379. default:
  4380. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4381. return 190000;
  4382. }
  4383. }
  4384. }
  4385. static int i865_get_display_clock_speed(struct drm_device *dev)
  4386. {
  4387. return 266000;
  4388. }
  4389. static int i855_get_display_clock_speed(struct drm_device *dev)
  4390. {
  4391. u16 hpllcc = 0;
  4392. /* Assume that the hardware is in the high speed state. This
  4393. * should be the default.
  4394. */
  4395. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4396. case GC_CLOCK_133_200:
  4397. case GC_CLOCK_100_200:
  4398. return 200000;
  4399. case GC_CLOCK_166_250:
  4400. return 250000;
  4401. case GC_CLOCK_100_133:
  4402. return 133000;
  4403. }
  4404. /* Shouldn't happen */
  4405. return 0;
  4406. }
  4407. static int i830_get_display_clock_speed(struct drm_device *dev)
  4408. {
  4409. return 133000;
  4410. }
  4411. static void
  4412. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4413. {
  4414. while (*num > DATA_LINK_M_N_MASK ||
  4415. *den > DATA_LINK_M_N_MASK) {
  4416. *num >>= 1;
  4417. *den >>= 1;
  4418. }
  4419. }
  4420. static void compute_m_n(unsigned int m, unsigned int n,
  4421. uint32_t *ret_m, uint32_t *ret_n)
  4422. {
  4423. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4424. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4425. intel_reduce_m_n_ratio(ret_m, ret_n);
  4426. }
  4427. void
  4428. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4429. int pixel_clock, int link_clock,
  4430. struct intel_link_m_n *m_n)
  4431. {
  4432. m_n->tu = 64;
  4433. compute_m_n(bits_per_pixel * pixel_clock,
  4434. link_clock * nlanes * 8,
  4435. &m_n->gmch_m, &m_n->gmch_n);
  4436. compute_m_n(pixel_clock, link_clock,
  4437. &m_n->link_m, &m_n->link_n);
  4438. }
  4439. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4440. {
  4441. if (i915.panel_use_ssc >= 0)
  4442. return i915.panel_use_ssc != 0;
  4443. return dev_priv->vbt.lvds_use_ssc
  4444. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4445. }
  4446. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4447. {
  4448. struct drm_device *dev = crtc->dev;
  4449. struct drm_i915_private *dev_priv = dev->dev_private;
  4450. int refclk;
  4451. if (IS_VALLEYVIEW(dev)) {
  4452. refclk = 100000;
  4453. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4454. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4455. refclk = dev_priv->vbt.lvds_ssc_freq;
  4456. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4457. } else if (!IS_GEN2(dev)) {
  4458. refclk = 96000;
  4459. } else {
  4460. refclk = 48000;
  4461. }
  4462. return refclk;
  4463. }
  4464. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4465. {
  4466. return (1 << dpll->n) << 16 | dpll->m2;
  4467. }
  4468. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4469. {
  4470. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4471. }
  4472. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4473. intel_clock_t *reduced_clock)
  4474. {
  4475. struct drm_device *dev = crtc->base.dev;
  4476. u32 fp, fp2 = 0;
  4477. if (IS_PINEVIEW(dev)) {
  4478. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4479. if (reduced_clock)
  4480. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4481. } else {
  4482. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4483. if (reduced_clock)
  4484. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4485. }
  4486. crtc->config.dpll_hw_state.fp0 = fp;
  4487. crtc->lowfreq_avail = false;
  4488. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4489. reduced_clock && i915.powersave) {
  4490. crtc->config.dpll_hw_state.fp1 = fp2;
  4491. crtc->lowfreq_avail = true;
  4492. } else {
  4493. crtc->config.dpll_hw_state.fp1 = fp;
  4494. }
  4495. }
  4496. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4497. pipe)
  4498. {
  4499. u32 reg_val;
  4500. /*
  4501. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4502. * and set it to a reasonable value instead.
  4503. */
  4504. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4505. reg_val &= 0xffffff00;
  4506. reg_val |= 0x00000030;
  4507. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4508. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4509. reg_val &= 0x8cffffff;
  4510. reg_val = 0x8c000000;
  4511. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4512. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4513. reg_val &= 0xffffff00;
  4514. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4515. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4516. reg_val &= 0x00ffffff;
  4517. reg_val |= 0xb0000000;
  4518. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4519. }
  4520. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4521. struct intel_link_m_n *m_n)
  4522. {
  4523. struct drm_device *dev = crtc->base.dev;
  4524. struct drm_i915_private *dev_priv = dev->dev_private;
  4525. int pipe = crtc->pipe;
  4526. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4527. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4528. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4529. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4530. }
  4531. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4532. struct intel_link_m_n *m_n)
  4533. {
  4534. struct drm_device *dev = crtc->base.dev;
  4535. struct drm_i915_private *dev_priv = dev->dev_private;
  4536. int pipe = crtc->pipe;
  4537. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4538. if (INTEL_INFO(dev)->gen >= 5) {
  4539. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4540. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4541. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4542. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4543. } else {
  4544. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4545. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4546. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4547. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4548. }
  4549. }
  4550. static void intel_dp_set_m_n(struct intel_crtc *crtc)
  4551. {
  4552. if (crtc->config.has_pch_encoder)
  4553. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4554. else
  4555. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4556. }
  4557. static void vlv_update_pll(struct intel_crtc *crtc)
  4558. {
  4559. u32 dpll, dpll_md;
  4560. /*
  4561. * Enable DPIO clock input. We should never disable the reference
  4562. * clock for pipe B, since VGA hotplug / manual detection depends
  4563. * on it.
  4564. */
  4565. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4566. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4567. /* We should never disable this, set it here for state tracking */
  4568. if (crtc->pipe == PIPE_B)
  4569. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4570. dpll |= DPLL_VCO_ENABLE;
  4571. crtc->config.dpll_hw_state.dpll = dpll;
  4572. dpll_md = (crtc->config.pixel_multiplier - 1)
  4573. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4574. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4575. }
  4576. static void vlv_prepare_pll(struct intel_crtc *crtc)
  4577. {
  4578. struct drm_device *dev = crtc->base.dev;
  4579. struct drm_i915_private *dev_priv = dev->dev_private;
  4580. int pipe = crtc->pipe;
  4581. u32 mdiv;
  4582. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4583. u32 coreclk, reg_val;
  4584. mutex_lock(&dev_priv->dpio_lock);
  4585. bestn = crtc->config.dpll.n;
  4586. bestm1 = crtc->config.dpll.m1;
  4587. bestm2 = crtc->config.dpll.m2;
  4588. bestp1 = crtc->config.dpll.p1;
  4589. bestp2 = crtc->config.dpll.p2;
  4590. /* See eDP HDMI DPIO driver vbios notes doc */
  4591. /* PLL B needs special handling */
  4592. if (pipe == PIPE_B)
  4593. vlv_pllb_recal_opamp(dev_priv, pipe);
  4594. /* Set up Tx target for periodic Rcomp update */
  4595. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4596. /* Disable target IRef on PLL */
  4597. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4598. reg_val &= 0x00ffffff;
  4599. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4600. /* Disable fast lock */
  4601. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4602. /* Set idtafcrecal before PLL is enabled */
  4603. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4604. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4605. mdiv |= ((bestn << DPIO_N_SHIFT));
  4606. mdiv |= (1 << DPIO_K_SHIFT);
  4607. /*
  4608. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4609. * but we don't support that).
  4610. * Note: don't use the DAC post divider as it seems unstable.
  4611. */
  4612. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4613. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4614. mdiv |= DPIO_ENABLE_CALIBRATION;
  4615. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4616. /* Set HBR and RBR LPF coefficients */
  4617. if (crtc->config.port_clock == 162000 ||
  4618. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4619. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4620. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4621. 0x009f0003);
  4622. else
  4623. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4624. 0x00d0000f);
  4625. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4626. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4627. /* Use SSC source */
  4628. if (pipe == PIPE_A)
  4629. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4630. 0x0df40000);
  4631. else
  4632. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4633. 0x0df70000);
  4634. } else { /* HDMI or VGA */
  4635. /* Use bend source */
  4636. if (pipe == PIPE_A)
  4637. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4638. 0x0df70000);
  4639. else
  4640. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4641. 0x0df40000);
  4642. }
  4643. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4644. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4645. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4646. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4647. coreclk |= 0x01000000;
  4648. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4649. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4650. mutex_unlock(&dev_priv->dpio_lock);
  4651. }
  4652. static void chv_update_pll(struct intel_crtc *crtc)
  4653. {
  4654. struct drm_device *dev = crtc->base.dev;
  4655. struct drm_i915_private *dev_priv = dev->dev_private;
  4656. int pipe = crtc->pipe;
  4657. int dpll_reg = DPLL(crtc->pipe);
  4658. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4659. u32 loopfilter, intcoeff;
  4660. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4661. int refclk;
  4662. crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4663. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4664. DPLL_VCO_ENABLE;
  4665. if (pipe != PIPE_A)
  4666. crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4667. crtc->config.dpll_hw_state.dpll_md =
  4668. (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4669. bestn = crtc->config.dpll.n;
  4670. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4671. bestm1 = crtc->config.dpll.m1;
  4672. bestm2 = crtc->config.dpll.m2 >> 22;
  4673. bestp1 = crtc->config.dpll.p1;
  4674. bestp2 = crtc->config.dpll.p2;
  4675. /*
  4676. * Enable Refclk and SSC
  4677. */
  4678. I915_WRITE(dpll_reg,
  4679. crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4680. mutex_lock(&dev_priv->dpio_lock);
  4681. /* p1 and p2 divider */
  4682. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4683. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4684. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4685. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4686. 1 << DPIO_CHV_K_DIV_SHIFT);
  4687. /* Feedback post-divider - m2 */
  4688. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4689. /* Feedback refclk divider - n and m1 */
  4690. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4691. DPIO_CHV_M1_DIV_BY_2 |
  4692. 1 << DPIO_CHV_N_DIV_SHIFT);
  4693. /* M2 fraction division */
  4694. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4695. /* M2 fraction division enable */
  4696. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4697. DPIO_CHV_FRAC_DIV_EN |
  4698. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4699. /* Loop filter */
  4700. refclk = i9xx_get_refclk(&crtc->base, 0);
  4701. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4702. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4703. if (refclk == 100000)
  4704. intcoeff = 11;
  4705. else if (refclk == 38400)
  4706. intcoeff = 10;
  4707. else
  4708. intcoeff = 9;
  4709. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4710. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4711. /* AFC Recal */
  4712. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4713. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4714. DPIO_AFC_RECAL);
  4715. mutex_unlock(&dev_priv->dpio_lock);
  4716. }
  4717. static void i9xx_update_pll(struct intel_crtc *crtc,
  4718. intel_clock_t *reduced_clock,
  4719. int num_connectors)
  4720. {
  4721. struct drm_device *dev = crtc->base.dev;
  4722. struct drm_i915_private *dev_priv = dev->dev_private;
  4723. u32 dpll;
  4724. bool is_sdvo;
  4725. struct dpll *clock = &crtc->config.dpll;
  4726. i9xx_update_pll_dividers(crtc, reduced_clock);
  4727. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4728. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4729. dpll = DPLL_VGA_MODE_DIS;
  4730. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4731. dpll |= DPLLB_MODE_LVDS;
  4732. else
  4733. dpll |= DPLLB_MODE_DAC_SERIAL;
  4734. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4735. dpll |= (crtc->config.pixel_multiplier - 1)
  4736. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4737. }
  4738. if (is_sdvo)
  4739. dpll |= DPLL_SDVO_HIGH_SPEED;
  4740. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4741. dpll |= DPLL_SDVO_HIGH_SPEED;
  4742. /* compute bitmask from p1 value */
  4743. if (IS_PINEVIEW(dev))
  4744. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4745. else {
  4746. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4747. if (IS_G4X(dev) && reduced_clock)
  4748. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4749. }
  4750. switch (clock->p2) {
  4751. case 5:
  4752. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4753. break;
  4754. case 7:
  4755. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4756. break;
  4757. case 10:
  4758. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4759. break;
  4760. case 14:
  4761. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4762. break;
  4763. }
  4764. if (INTEL_INFO(dev)->gen >= 4)
  4765. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4766. if (crtc->config.sdvo_tv_clock)
  4767. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4768. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4769. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4770. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4771. else
  4772. dpll |= PLL_REF_INPUT_DREFCLK;
  4773. dpll |= DPLL_VCO_ENABLE;
  4774. crtc->config.dpll_hw_state.dpll = dpll;
  4775. if (INTEL_INFO(dev)->gen >= 4) {
  4776. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4777. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4778. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4779. }
  4780. }
  4781. static void i8xx_update_pll(struct intel_crtc *crtc,
  4782. intel_clock_t *reduced_clock,
  4783. int num_connectors)
  4784. {
  4785. struct drm_device *dev = crtc->base.dev;
  4786. struct drm_i915_private *dev_priv = dev->dev_private;
  4787. u32 dpll;
  4788. struct dpll *clock = &crtc->config.dpll;
  4789. i9xx_update_pll_dividers(crtc, reduced_clock);
  4790. dpll = DPLL_VGA_MODE_DIS;
  4791. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4792. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4793. } else {
  4794. if (clock->p1 == 2)
  4795. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4796. else
  4797. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4798. if (clock->p2 == 4)
  4799. dpll |= PLL_P2_DIVIDE_BY_4;
  4800. }
  4801. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4802. dpll |= DPLL_DVO_2X_MODE;
  4803. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4804. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4805. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4806. else
  4807. dpll |= PLL_REF_INPUT_DREFCLK;
  4808. dpll |= DPLL_VCO_ENABLE;
  4809. crtc->config.dpll_hw_state.dpll = dpll;
  4810. }
  4811. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4812. {
  4813. struct drm_device *dev = intel_crtc->base.dev;
  4814. struct drm_i915_private *dev_priv = dev->dev_private;
  4815. enum pipe pipe = intel_crtc->pipe;
  4816. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4817. struct drm_display_mode *adjusted_mode =
  4818. &intel_crtc->config.adjusted_mode;
  4819. uint32_t crtc_vtotal, crtc_vblank_end;
  4820. int vsyncshift = 0;
  4821. /* We need to be careful not to changed the adjusted mode, for otherwise
  4822. * the hw state checker will get angry at the mismatch. */
  4823. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4824. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4825. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4826. /* the chip adds 2 halflines automatically */
  4827. crtc_vtotal -= 1;
  4828. crtc_vblank_end -= 1;
  4829. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4830. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  4831. else
  4832. vsyncshift = adjusted_mode->crtc_hsync_start -
  4833. adjusted_mode->crtc_htotal / 2;
  4834. if (vsyncshift < 0)
  4835. vsyncshift += adjusted_mode->crtc_htotal;
  4836. }
  4837. if (INTEL_INFO(dev)->gen > 3)
  4838. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4839. I915_WRITE(HTOTAL(cpu_transcoder),
  4840. (adjusted_mode->crtc_hdisplay - 1) |
  4841. ((adjusted_mode->crtc_htotal - 1) << 16));
  4842. I915_WRITE(HBLANK(cpu_transcoder),
  4843. (adjusted_mode->crtc_hblank_start - 1) |
  4844. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4845. I915_WRITE(HSYNC(cpu_transcoder),
  4846. (adjusted_mode->crtc_hsync_start - 1) |
  4847. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4848. I915_WRITE(VTOTAL(cpu_transcoder),
  4849. (adjusted_mode->crtc_vdisplay - 1) |
  4850. ((crtc_vtotal - 1) << 16));
  4851. I915_WRITE(VBLANK(cpu_transcoder),
  4852. (adjusted_mode->crtc_vblank_start - 1) |
  4853. ((crtc_vblank_end - 1) << 16));
  4854. I915_WRITE(VSYNC(cpu_transcoder),
  4855. (adjusted_mode->crtc_vsync_start - 1) |
  4856. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4857. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4858. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4859. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4860. * bits. */
  4861. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  4862. (pipe == PIPE_B || pipe == PIPE_C))
  4863. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  4864. /* pipesrc controls the size that is scaled from, which should
  4865. * always be the user's requested size.
  4866. */
  4867. I915_WRITE(PIPESRC(pipe),
  4868. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  4869. (intel_crtc->config.pipe_src_h - 1));
  4870. }
  4871. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  4872. struct intel_crtc_config *pipe_config)
  4873. {
  4874. struct drm_device *dev = crtc->base.dev;
  4875. struct drm_i915_private *dev_priv = dev->dev_private;
  4876. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  4877. uint32_t tmp;
  4878. tmp = I915_READ(HTOTAL(cpu_transcoder));
  4879. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  4880. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  4881. tmp = I915_READ(HBLANK(cpu_transcoder));
  4882. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  4883. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  4884. tmp = I915_READ(HSYNC(cpu_transcoder));
  4885. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  4886. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  4887. tmp = I915_READ(VTOTAL(cpu_transcoder));
  4888. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  4889. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  4890. tmp = I915_READ(VBLANK(cpu_transcoder));
  4891. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  4892. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  4893. tmp = I915_READ(VSYNC(cpu_transcoder));
  4894. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  4895. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  4896. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  4897. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  4898. pipe_config->adjusted_mode.crtc_vtotal += 1;
  4899. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  4900. }
  4901. tmp = I915_READ(PIPESRC(crtc->pipe));
  4902. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  4903. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  4904. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  4905. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  4906. }
  4907. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  4908. struct intel_crtc_config *pipe_config)
  4909. {
  4910. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  4911. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  4912. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  4913. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  4914. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  4915. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  4916. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  4917. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  4918. mode->flags = pipe_config->adjusted_mode.flags;
  4919. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  4920. mode->flags |= pipe_config->adjusted_mode.flags;
  4921. }
  4922. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  4923. {
  4924. struct drm_device *dev = intel_crtc->base.dev;
  4925. struct drm_i915_private *dev_priv = dev->dev_private;
  4926. uint32_t pipeconf;
  4927. pipeconf = 0;
  4928. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  4929. I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
  4930. pipeconf |= PIPECONF_ENABLE;
  4931. if (intel_crtc->config.double_wide)
  4932. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4933. /* only g4x and later have fancy bpc/dither controls */
  4934. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  4935. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  4936. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  4937. pipeconf |= PIPECONF_DITHER_EN |
  4938. PIPECONF_DITHER_TYPE_SP;
  4939. switch (intel_crtc->config.pipe_bpp) {
  4940. case 18:
  4941. pipeconf |= PIPECONF_6BPC;
  4942. break;
  4943. case 24:
  4944. pipeconf |= PIPECONF_8BPC;
  4945. break;
  4946. case 30:
  4947. pipeconf |= PIPECONF_10BPC;
  4948. break;
  4949. default:
  4950. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4951. BUG();
  4952. }
  4953. }
  4954. if (HAS_PIPE_CXSR(dev)) {
  4955. if (intel_crtc->lowfreq_avail) {
  4956. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4957. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4958. } else {
  4959. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4960. }
  4961. }
  4962. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  4963. if (INTEL_INFO(dev)->gen < 4 ||
  4964. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4965. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4966. else
  4967. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  4968. } else
  4969. pipeconf |= PIPECONF_PROGRESSIVE;
  4970. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  4971. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  4972. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  4973. POSTING_READ(PIPECONF(intel_crtc->pipe));
  4974. }
  4975. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4976. int x, int y,
  4977. struct drm_framebuffer *fb)
  4978. {
  4979. struct drm_device *dev = crtc->dev;
  4980. struct drm_i915_private *dev_priv = dev->dev_private;
  4981. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4982. int refclk, num_connectors = 0;
  4983. intel_clock_t clock, reduced_clock;
  4984. bool ok, has_reduced_clock = false;
  4985. bool is_lvds = false, is_dsi = false;
  4986. struct intel_encoder *encoder;
  4987. const intel_limit_t *limit;
  4988. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4989. switch (encoder->type) {
  4990. case INTEL_OUTPUT_LVDS:
  4991. is_lvds = true;
  4992. break;
  4993. case INTEL_OUTPUT_DSI:
  4994. is_dsi = true;
  4995. break;
  4996. }
  4997. num_connectors++;
  4998. }
  4999. if (is_dsi)
  5000. return 0;
  5001. if (!intel_crtc->config.clock_set) {
  5002. refclk = i9xx_get_refclk(crtc, num_connectors);
  5003. /*
  5004. * Returns a set of divisors for the desired target clock with
  5005. * the given refclk, or FALSE. The returned values represent
  5006. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5007. * 2) / p1 / p2.
  5008. */
  5009. limit = intel_limit(crtc, refclk);
  5010. ok = dev_priv->display.find_dpll(limit, crtc,
  5011. intel_crtc->config.port_clock,
  5012. refclk, NULL, &clock);
  5013. if (!ok) {
  5014. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5015. return -EINVAL;
  5016. }
  5017. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5018. /*
  5019. * Ensure we match the reduced clock's P to the target
  5020. * clock. If the clocks don't match, we can't switch
  5021. * the display clock by using the FP0/FP1. In such case
  5022. * we will disable the LVDS downclock feature.
  5023. */
  5024. has_reduced_clock =
  5025. dev_priv->display.find_dpll(limit, crtc,
  5026. dev_priv->lvds_downclock,
  5027. refclk, &clock,
  5028. &reduced_clock);
  5029. }
  5030. /* Compat-code for transition, will disappear. */
  5031. intel_crtc->config.dpll.n = clock.n;
  5032. intel_crtc->config.dpll.m1 = clock.m1;
  5033. intel_crtc->config.dpll.m2 = clock.m2;
  5034. intel_crtc->config.dpll.p1 = clock.p1;
  5035. intel_crtc->config.dpll.p2 = clock.p2;
  5036. }
  5037. if (IS_GEN2(dev)) {
  5038. i8xx_update_pll(intel_crtc,
  5039. has_reduced_clock ? &reduced_clock : NULL,
  5040. num_connectors);
  5041. } else if (IS_CHERRYVIEW(dev)) {
  5042. chv_update_pll(intel_crtc);
  5043. } else if (IS_VALLEYVIEW(dev)) {
  5044. vlv_update_pll(intel_crtc);
  5045. } else {
  5046. i9xx_update_pll(intel_crtc,
  5047. has_reduced_clock ? &reduced_clock : NULL,
  5048. num_connectors);
  5049. }
  5050. return 0;
  5051. }
  5052. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5053. struct intel_crtc_config *pipe_config)
  5054. {
  5055. struct drm_device *dev = crtc->base.dev;
  5056. struct drm_i915_private *dev_priv = dev->dev_private;
  5057. uint32_t tmp;
  5058. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5059. return;
  5060. tmp = I915_READ(PFIT_CONTROL);
  5061. if (!(tmp & PFIT_ENABLE))
  5062. return;
  5063. /* Check whether the pfit is attached to our pipe. */
  5064. if (INTEL_INFO(dev)->gen < 4) {
  5065. if (crtc->pipe != PIPE_B)
  5066. return;
  5067. } else {
  5068. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5069. return;
  5070. }
  5071. pipe_config->gmch_pfit.control = tmp;
  5072. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5073. if (INTEL_INFO(dev)->gen < 5)
  5074. pipe_config->gmch_pfit.lvds_border_bits =
  5075. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5076. }
  5077. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5078. struct intel_crtc_config *pipe_config)
  5079. {
  5080. struct drm_device *dev = crtc->base.dev;
  5081. struct drm_i915_private *dev_priv = dev->dev_private;
  5082. int pipe = pipe_config->cpu_transcoder;
  5083. intel_clock_t clock;
  5084. u32 mdiv;
  5085. int refclk = 100000;
  5086. mutex_lock(&dev_priv->dpio_lock);
  5087. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5088. mutex_unlock(&dev_priv->dpio_lock);
  5089. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5090. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5091. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5092. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5093. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5094. vlv_clock(refclk, &clock);
  5095. /* clock.dot is the fast clock */
  5096. pipe_config->port_clock = clock.dot / 5;
  5097. }
  5098. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5099. struct intel_plane_config *plane_config)
  5100. {
  5101. struct drm_device *dev = crtc->base.dev;
  5102. struct drm_i915_private *dev_priv = dev->dev_private;
  5103. u32 val, base, offset;
  5104. int pipe = crtc->pipe, plane = crtc->plane;
  5105. int fourcc, pixel_format;
  5106. int aligned_height;
  5107. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5108. if (!crtc->base.primary->fb) {
  5109. DRM_DEBUG_KMS("failed to alloc fb\n");
  5110. return;
  5111. }
  5112. val = I915_READ(DSPCNTR(plane));
  5113. if (INTEL_INFO(dev)->gen >= 4)
  5114. if (val & DISPPLANE_TILED)
  5115. plane_config->tiled = true;
  5116. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5117. fourcc = intel_format_to_fourcc(pixel_format);
  5118. crtc->base.primary->fb->pixel_format = fourcc;
  5119. crtc->base.primary->fb->bits_per_pixel =
  5120. drm_format_plane_cpp(fourcc, 0) * 8;
  5121. if (INTEL_INFO(dev)->gen >= 4) {
  5122. if (plane_config->tiled)
  5123. offset = I915_READ(DSPTILEOFF(plane));
  5124. else
  5125. offset = I915_READ(DSPLINOFF(plane));
  5126. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5127. } else {
  5128. base = I915_READ(DSPADDR(plane));
  5129. }
  5130. plane_config->base = base;
  5131. val = I915_READ(PIPESRC(pipe));
  5132. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5133. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5134. val = I915_READ(DSPSTRIDE(pipe));
  5135. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5136. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5137. plane_config->tiled);
  5138. plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
  5139. aligned_height, PAGE_SIZE);
  5140. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5141. pipe, plane, crtc->base.primary->fb->width,
  5142. crtc->base.primary->fb->height,
  5143. crtc->base.primary->fb->bits_per_pixel, base,
  5144. crtc->base.primary->fb->pitches[0],
  5145. plane_config->size);
  5146. }
  5147. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5148. struct intel_crtc_config *pipe_config)
  5149. {
  5150. struct drm_device *dev = crtc->base.dev;
  5151. struct drm_i915_private *dev_priv = dev->dev_private;
  5152. int pipe = pipe_config->cpu_transcoder;
  5153. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5154. intel_clock_t clock;
  5155. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5156. int refclk = 100000;
  5157. mutex_lock(&dev_priv->dpio_lock);
  5158. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5159. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5160. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5161. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5162. mutex_unlock(&dev_priv->dpio_lock);
  5163. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5164. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5165. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5166. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5167. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5168. chv_clock(refclk, &clock);
  5169. /* clock.dot is the fast clock */
  5170. pipe_config->port_clock = clock.dot / 5;
  5171. }
  5172. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5173. struct intel_crtc_config *pipe_config)
  5174. {
  5175. struct drm_device *dev = crtc->base.dev;
  5176. struct drm_i915_private *dev_priv = dev->dev_private;
  5177. uint32_t tmp;
  5178. if (!intel_display_power_enabled(dev_priv,
  5179. POWER_DOMAIN_PIPE(crtc->pipe)))
  5180. return false;
  5181. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5182. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5183. tmp = I915_READ(PIPECONF(crtc->pipe));
  5184. if (!(tmp & PIPECONF_ENABLE))
  5185. return false;
  5186. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5187. switch (tmp & PIPECONF_BPC_MASK) {
  5188. case PIPECONF_6BPC:
  5189. pipe_config->pipe_bpp = 18;
  5190. break;
  5191. case PIPECONF_8BPC:
  5192. pipe_config->pipe_bpp = 24;
  5193. break;
  5194. case PIPECONF_10BPC:
  5195. pipe_config->pipe_bpp = 30;
  5196. break;
  5197. default:
  5198. break;
  5199. }
  5200. }
  5201. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5202. pipe_config->limited_color_range = true;
  5203. if (INTEL_INFO(dev)->gen < 4)
  5204. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5205. intel_get_pipe_timings(crtc, pipe_config);
  5206. i9xx_get_pfit_config(crtc, pipe_config);
  5207. if (INTEL_INFO(dev)->gen >= 4) {
  5208. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5209. pipe_config->pixel_multiplier =
  5210. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5211. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5212. pipe_config->dpll_hw_state.dpll_md = tmp;
  5213. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5214. tmp = I915_READ(DPLL(crtc->pipe));
  5215. pipe_config->pixel_multiplier =
  5216. ((tmp & SDVO_MULTIPLIER_MASK)
  5217. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5218. } else {
  5219. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5220. * port and will be fixed up in the encoder->get_config
  5221. * function. */
  5222. pipe_config->pixel_multiplier = 1;
  5223. }
  5224. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5225. if (!IS_VALLEYVIEW(dev)) {
  5226. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5227. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5228. } else {
  5229. /* Mask out read-only status bits. */
  5230. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5231. DPLL_PORTC_READY_MASK |
  5232. DPLL_PORTB_READY_MASK);
  5233. }
  5234. if (IS_CHERRYVIEW(dev))
  5235. chv_crtc_clock_get(crtc, pipe_config);
  5236. else if (IS_VALLEYVIEW(dev))
  5237. vlv_crtc_clock_get(crtc, pipe_config);
  5238. else
  5239. i9xx_crtc_clock_get(crtc, pipe_config);
  5240. return true;
  5241. }
  5242. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5243. {
  5244. struct drm_i915_private *dev_priv = dev->dev_private;
  5245. struct drm_mode_config *mode_config = &dev->mode_config;
  5246. struct intel_encoder *encoder;
  5247. u32 val, final;
  5248. bool has_lvds = false;
  5249. bool has_cpu_edp = false;
  5250. bool has_panel = false;
  5251. bool has_ck505 = false;
  5252. bool can_ssc = false;
  5253. /* We need to take the global config into account */
  5254. list_for_each_entry(encoder, &mode_config->encoder_list,
  5255. base.head) {
  5256. switch (encoder->type) {
  5257. case INTEL_OUTPUT_LVDS:
  5258. has_panel = true;
  5259. has_lvds = true;
  5260. break;
  5261. case INTEL_OUTPUT_EDP:
  5262. has_panel = true;
  5263. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5264. has_cpu_edp = true;
  5265. break;
  5266. }
  5267. }
  5268. if (HAS_PCH_IBX(dev)) {
  5269. has_ck505 = dev_priv->vbt.display_clock_mode;
  5270. can_ssc = has_ck505;
  5271. } else {
  5272. has_ck505 = false;
  5273. can_ssc = true;
  5274. }
  5275. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5276. has_panel, has_lvds, has_ck505);
  5277. /* Ironlake: try to setup display ref clock before DPLL
  5278. * enabling. This is only under driver's control after
  5279. * PCH B stepping, previous chipset stepping should be
  5280. * ignoring this setting.
  5281. */
  5282. val = I915_READ(PCH_DREF_CONTROL);
  5283. /* As we must carefully and slowly disable/enable each source in turn,
  5284. * compute the final state we want first and check if we need to
  5285. * make any changes at all.
  5286. */
  5287. final = val;
  5288. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5289. if (has_ck505)
  5290. final |= DREF_NONSPREAD_CK505_ENABLE;
  5291. else
  5292. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5293. final &= ~DREF_SSC_SOURCE_MASK;
  5294. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5295. final &= ~DREF_SSC1_ENABLE;
  5296. if (has_panel) {
  5297. final |= DREF_SSC_SOURCE_ENABLE;
  5298. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5299. final |= DREF_SSC1_ENABLE;
  5300. if (has_cpu_edp) {
  5301. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5302. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5303. else
  5304. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5305. } else
  5306. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5307. } else {
  5308. final |= DREF_SSC_SOURCE_DISABLE;
  5309. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5310. }
  5311. if (final == val)
  5312. return;
  5313. /* Always enable nonspread source */
  5314. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5315. if (has_ck505)
  5316. val |= DREF_NONSPREAD_CK505_ENABLE;
  5317. else
  5318. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5319. if (has_panel) {
  5320. val &= ~DREF_SSC_SOURCE_MASK;
  5321. val |= DREF_SSC_SOURCE_ENABLE;
  5322. /* SSC must be turned on before enabling the CPU output */
  5323. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5324. DRM_DEBUG_KMS("Using SSC on panel\n");
  5325. val |= DREF_SSC1_ENABLE;
  5326. } else
  5327. val &= ~DREF_SSC1_ENABLE;
  5328. /* Get SSC going before enabling the outputs */
  5329. I915_WRITE(PCH_DREF_CONTROL, val);
  5330. POSTING_READ(PCH_DREF_CONTROL);
  5331. udelay(200);
  5332. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5333. /* Enable CPU source on CPU attached eDP */
  5334. if (has_cpu_edp) {
  5335. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5336. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5337. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5338. } else
  5339. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5340. } else
  5341. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5342. I915_WRITE(PCH_DREF_CONTROL, val);
  5343. POSTING_READ(PCH_DREF_CONTROL);
  5344. udelay(200);
  5345. } else {
  5346. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5347. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5348. /* Turn off CPU output */
  5349. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5350. I915_WRITE(PCH_DREF_CONTROL, val);
  5351. POSTING_READ(PCH_DREF_CONTROL);
  5352. udelay(200);
  5353. /* Turn off the SSC source */
  5354. val &= ~DREF_SSC_SOURCE_MASK;
  5355. val |= DREF_SSC_SOURCE_DISABLE;
  5356. /* Turn off SSC1 */
  5357. val &= ~DREF_SSC1_ENABLE;
  5358. I915_WRITE(PCH_DREF_CONTROL, val);
  5359. POSTING_READ(PCH_DREF_CONTROL);
  5360. udelay(200);
  5361. }
  5362. BUG_ON(val != final);
  5363. }
  5364. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5365. {
  5366. uint32_t tmp;
  5367. tmp = I915_READ(SOUTH_CHICKEN2);
  5368. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5369. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5370. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5371. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5372. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5373. tmp = I915_READ(SOUTH_CHICKEN2);
  5374. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5375. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5376. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5377. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5378. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5379. }
  5380. /* WaMPhyProgramming:hsw */
  5381. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5382. {
  5383. uint32_t tmp;
  5384. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5385. tmp &= ~(0xFF << 24);
  5386. tmp |= (0x12 << 24);
  5387. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5388. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5389. tmp |= (1 << 11);
  5390. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5391. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5392. tmp |= (1 << 11);
  5393. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5394. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5395. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5396. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5397. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5398. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5399. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5400. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5401. tmp &= ~(7 << 13);
  5402. tmp |= (5 << 13);
  5403. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5404. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5405. tmp &= ~(7 << 13);
  5406. tmp |= (5 << 13);
  5407. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5408. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5409. tmp &= ~0xFF;
  5410. tmp |= 0x1C;
  5411. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5412. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5413. tmp &= ~0xFF;
  5414. tmp |= 0x1C;
  5415. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5416. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5417. tmp &= ~(0xFF << 16);
  5418. tmp |= (0x1C << 16);
  5419. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5420. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5421. tmp &= ~(0xFF << 16);
  5422. tmp |= (0x1C << 16);
  5423. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5424. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5425. tmp |= (1 << 27);
  5426. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5427. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5428. tmp |= (1 << 27);
  5429. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5430. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5431. tmp &= ~(0xF << 28);
  5432. tmp |= (4 << 28);
  5433. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5434. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5435. tmp &= ~(0xF << 28);
  5436. tmp |= (4 << 28);
  5437. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5438. }
  5439. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5440. * Programming" based on the parameters passed:
  5441. * - Sequence to enable CLKOUT_DP
  5442. * - Sequence to enable CLKOUT_DP without spread
  5443. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5444. */
  5445. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5446. bool with_fdi)
  5447. {
  5448. struct drm_i915_private *dev_priv = dev->dev_private;
  5449. uint32_t reg, tmp;
  5450. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5451. with_spread = true;
  5452. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5453. with_fdi, "LP PCH doesn't have FDI\n"))
  5454. with_fdi = false;
  5455. mutex_lock(&dev_priv->dpio_lock);
  5456. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5457. tmp &= ~SBI_SSCCTL_DISABLE;
  5458. tmp |= SBI_SSCCTL_PATHALT;
  5459. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5460. udelay(24);
  5461. if (with_spread) {
  5462. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5463. tmp &= ~SBI_SSCCTL_PATHALT;
  5464. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5465. if (with_fdi) {
  5466. lpt_reset_fdi_mphy(dev_priv);
  5467. lpt_program_fdi_mphy(dev_priv);
  5468. }
  5469. }
  5470. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5471. SBI_GEN0 : SBI_DBUFF0;
  5472. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5473. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5474. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5475. mutex_unlock(&dev_priv->dpio_lock);
  5476. }
  5477. /* Sequence to disable CLKOUT_DP */
  5478. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5479. {
  5480. struct drm_i915_private *dev_priv = dev->dev_private;
  5481. uint32_t reg, tmp;
  5482. mutex_lock(&dev_priv->dpio_lock);
  5483. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5484. SBI_GEN0 : SBI_DBUFF0;
  5485. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5486. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5487. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5488. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5489. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5490. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5491. tmp |= SBI_SSCCTL_PATHALT;
  5492. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5493. udelay(32);
  5494. }
  5495. tmp |= SBI_SSCCTL_DISABLE;
  5496. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5497. }
  5498. mutex_unlock(&dev_priv->dpio_lock);
  5499. }
  5500. static void lpt_init_pch_refclk(struct drm_device *dev)
  5501. {
  5502. struct drm_mode_config *mode_config = &dev->mode_config;
  5503. struct intel_encoder *encoder;
  5504. bool has_vga = false;
  5505. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  5506. switch (encoder->type) {
  5507. case INTEL_OUTPUT_ANALOG:
  5508. has_vga = true;
  5509. break;
  5510. }
  5511. }
  5512. if (has_vga)
  5513. lpt_enable_clkout_dp(dev, true, true);
  5514. else
  5515. lpt_disable_clkout_dp(dev);
  5516. }
  5517. /*
  5518. * Initialize reference clocks when the driver loads
  5519. */
  5520. void intel_init_pch_refclk(struct drm_device *dev)
  5521. {
  5522. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5523. ironlake_init_pch_refclk(dev);
  5524. else if (HAS_PCH_LPT(dev))
  5525. lpt_init_pch_refclk(dev);
  5526. }
  5527. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5528. {
  5529. struct drm_device *dev = crtc->dev;
  5530. struct drm_i915_private *dev_priv = dev->dev_private;
  5531. struct intel_encoder *encoder;
  5532. int num_connectors = 0;
  5533. bool is_lvds = false;
  5534. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5535. switch (encoder->type) {
  5536. case INTEL_OUTPUT_LVDS:
  5537. is_lvds = true;
  5538. break;
  5539. }
  5540. num_connectors++;
  5541. }
  5542. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5543. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5544. dev_priv->vbt.lvds_ssc_freq);
  5545. return dev_priv->vbt.lvds_ssc_freq;
  5546. }
  5547. return 120000;
  5548. }
  5549. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5550. {
  5551. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5553. int pipe = intel_crtc->pipe;
  5554. uint32_t val;
  5555. val = 0;
  5556. switch (intel_crtc->config.pipe_bpp) {
  5557. case 18:
  5558. val |= PIPECONF_6BPC;
  5559. break;
  5560. case 24:
  5561. val |= PIPECONF_8BPC;
  5562. break;
  5563. case 30:
  5564. val |= PIPECONF_10BPC;
  5565. break;
  5566. case 36:
  5567. val |= PIPECONF_12BPC;
  5568. break;
  5569. default:
  5570. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5571. BUG();
  5572. }
  5573. if (intel_crtc->config.dither)
  5574. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5575. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5576. val |= PIPECONF_INTERLACED_ILK;
  5577. else
  5578. val |= PIPECONF_PROGRESSIVE;
  5579. if (intel_crtc->config.limited_color_range)
  5580. val |= PIPECONF_COLOR_RANGE_SELECT;
  5581. I915_WRITE(PIPECONF(pipe), val);
  5582. POSTING_READ(PIPECONF(pipe));
  5583. }
  5584. /*
  5585. * Set up the pipe CSC unit.
  5586. *
  5587. * Currently only full range RGB to limited range RGB conversion
  5588. * is supported, but eventually this should handle various
  5589. * RGB<->YCbCr scenarios as well.
  5590. */
  5591. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5592. {
  5593. struct drm_device *dev = crtc->dev;
  5594. struct drm_i915_private *dev_priv = dev->dev_private;
  5595. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5596. int pipe = intel_crtc->pipe;
  5597. uint16_t coeff = 0x7800; /* 1.0 */
  5598. /*
  5599. * TODO: Check what kind of values actually come out of the pipe
  5600. * with these coeff/postoff values and adjust to get the best
  5601. * accuracy. Perhaps we even need to take the bpc value into
  5602. * consideration.
  5603. */
  5604. if (intel_crtc->config.limited_color_range)
  5605. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5606. /*
  5607. * GY/GU and RY/RU should be the other way around according
  5608. * to BSpec, but reality doesn't agree. Just set them up in
  5609. * a way that results in the correct picture.
  5610. */
  5611. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5612. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5613. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5614. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5615. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5616. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5617. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5618. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5619. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5620. if (INTEL_INFO(dev)->gen > 6) {
  5621. uint16_t postoff = 0;
  5622. if (intel_crtc->config.limited_color_range)
  5623. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5624. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5625. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5626. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5627. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5628. } else {
  5629. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5630. if (intel_crtc->config.limited_color_range)
  5631. mode |= CSC_BLACK_SCREEN_OFFSET;
  5632. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5633. }
  5634. }
  5635. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5636. {
  5637. struct drm_device *dev = crtc->dev;
  5638. struct drm_i915_private *dev_priv = dev->dev_private;
  5639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5640. enum pipe pipe = intel_crtc->pipe;
  5641. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5642. uint32_t val;
  5643. val = 0;
  5644. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5645. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5646. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5647. val |= PIPECONF_INTERLACED_ILK;
  5648. else
  5649. val |= PIPECONF_PROGRESSIVE;
  5650. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5651. POSTING_READ(PIPECONF(cpu_transcoder));
  5652. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5653. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5654. if (IS_BROADWELL(dev)) {
  5655. val = 0;
  5656. switch (intel_crtc->config.pipe_bpp) {
  5657. case 18:
  5658. val |= PIPEMISC_DITHER_6_BPC;
  5659. break;
  5660. case 24:
  5661. val |= PIPEMISC_DITHER_8_BPC;
  5662. break;
  5663. case 30:
  5664. val |= PIPEMISC_DITHER_10_BPC;
  5665. break;
  5666. case 36:
  5667. val |= PIPEMISC_DITHER_12_BPC;
  5668. break;
  5669. default:
  5670. /* Case prevented by pipe_config_set_bpp. */
  5671. BUG();
  5672. }
  5673. if (intel_crtc->config.dither)
  5674. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5675. I915_WRITE(PIPEMISC(pipe), val);
  5676. }
  5677. }
  5678. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5679. intel_clock_t *clock,
  5680. bool *has_reduced_clock,
  5681. intel_clock_t *reduced_clock)
  5682. {
  5683. struct drm_device *dev = crtc->dev;
  5684. struct drm_i915_private *dev_priv = dev->dev_private;
  5685. struct intel_encoder *intel_encoder;
  5686. int refclk;
  5687. const intel_limit_t *limit;
  5688. bool ret, is_lvds = false;
  5689. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5690. switch (intel_encoder->type) {
  5691. case INTEL_OUTPUT_LVDS:
  5692. is_lvds = true;
  5693. break;
  5694. }
  5695. }
  5696. refclk = ironlake_get_refclk(crtc);
  5697. /*
  5698. * Returns a set of divisors for the desired target clock with the given
  5699. * refclk, or FALSE. The returned values represent the clock equation:
  5700. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5701. */
  5702. limit = intel_limit(crtc, refclk);
  5703. ret = dev_priv->display.find_dpll(limit, crtc,
  5704. to_intel_crtc(crtc)->config.port_clock,
  5705. refclk, NULL, clock);
  5706. if (!ret)
  5707. return false;
  5708. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5709. /*
  5710. * Ensure we match the reduced clock's P to the target clock.
  5711. * If the clocks don't match, we can't switch the display clock
  5712. * by using the FP0/FP1. In such case we will disable the LVDS
  5713. * downclock feature.
  5714. */
  5715. *has_reduced_clock =
  5716. dev_priv->display.find_dpll(limit, crtc,
  5717. dev_priv->lvds_downclock,
  5718. refclk, clock,
  5719. reduced_clock);
  5720. }
  5721. return true;
  5722. }
  5723. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5724. {
  5725. /*
  5726. * Account for spread spectrum to avoid
  5727. * oversubscribing the link. Max center spread
  5728. * is 2.5%; use 5% for safety's sake.
  5729. */
  5730. u32 bps = target_clock * bpp * 21 / 20;
  5731. return DIV_ROUND_UP(bps, link_bw * 8);
  5732. }
  5733. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  5734. {
  5735. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  5736. }
  5737. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  5738. u32 *fp,
  5739. intel_clock_t *reduced_clock, u32 *fp2)
  5740. {
  5741. struct drm_crtc *crtc = &intel_crtc->base;
  5742. struct drm_device *dev = crtc->dev;
  5743. struct drm_i915_private *dev_priv = dev->dev_private;
  5744. struct intel_encoder *intel_encoder;
  5745. uint32_t dpll;
  5746. int factor, num_connectors = 0;
  5747. bool is_lvds = false, is_sdvo = false;
  5748. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5749. switch (intel_encoder->type) {
  5750. case INTEL_OUTPUT_LVDS:
  5751. is_lvds = true;
  5752. break;
  5753. case INTEL_OUTPUT_SDVO:
  5754. case INTEL_OUTPUT_HDMI:
  5755. is_sdvo = true;
  5756. break;
  5757. }
  5758. num_connectors++;
  5759. }
  5760. /* Enable autotuning of the PLL clock (if permissible) */
  5761. factor = 21;
  5762. if (is_lvds) {
  5763. if ((intel_panel_use_ssc(dev_priv) &&
  5764. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  5765. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  5766. factor = 25;
  5767. } else if (intel_crtc->config.sdvo_tv_clock)
  5768. factor = 20;
  5769. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  5770. *fp |= FP_CB_TUNE;
  5771. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  5772. *fp2 |= FP_CB_TUNE;
  5773. dpll = 0;
  5774. if (is_lvds)
  5775. dpll |= DPLLB_MODE_LVDS;
  5776. else
  5777. dpll |= DPLLB_MODE_DAC_SERIAL;
  5778. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5779. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5780. if (is_sdvo)
  5781. dpll |= DPLL_SDVO_HIGH_SPEED;
  5782. if (intel_crtc->config.has_dp_encoder)
  5783. dpll |= DPLL_SDVO_HIGH_SPEED;
  5784. /* compute bitmask from p1 value */
  5785. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5786. /* also FPA1 */
  5787. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5788. switch (intel_crtc->config.dpll.p2) {
  5789. case 5:
  5790. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5791. break;
  5792. case 7:
  5793. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5794. break;
  5795. case 10:
  5796. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5797. break;
  5798. case 14:
  5799. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5800. break;
  5801. }
  5802. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5803. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5804. else
  5805. dpll |= PLL_REF_INPUT_DREFCLK;
  5806. return dpll | DPLL_VCO_ENABLE;
  5807. }
  5808. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5809. int x, int y,
  5810. struct drm_framebuffer *fb)
  5811. {
  5812. struct drm_device *dev = crtc->dev;
  5813. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5814. int num_connectors = 0;
  5815. intel_clock_t clock, reduced_clock;
  5816. u32 dpll = 0, fp = 0, fp2 = 0;
  5817. bool ok, has_reduced_clock = false;
  5818. bool is_lvds = false;
  5819. struct intel_encoder *encoder;
  5820. struct intel_shared_dpll *pll;
  5821. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5822. switch (encoder->type) {
  5823. case INTEL_OUTPUT_LVDS:
  5824. is_lvds = true;
  5825. break;
  5826. }
  5827. num_connectors++;
  5828. }
  5829. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5830. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5831. ok = ironlake_compute_clocks(crtc, &clock,
  5832. &has_reduced_clock, &reduced_clock);
  5833. if (!ok && !intel_crtc->config.clock_set) {
  5834. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5835. return -EINVAL;
  5836. }
  5837. /* Compat-code for transition, will disappear. */
  5838. if (!intel_crtc->config.clock_set) {
  5839. intel_crtc->config.dpll.n = clock.n;
  5840. intel_crtc->config.dpll.m1 = clock.m1;
  5841. intel_crtc->config.dpll.m2 = clock.m2;
  5842. intel_crtc->config.dpll.p1 = clock.p1;
  5843. intel_crtc->config.dpll.p2 = clock.p2;
  5844. }
  5845. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5846. if (intel_crtc->config.has_pch_encoder) {
  5847. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5848. if (has_reduced_clock)
  5849. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5850. dpll = ironlake_compute_dpll(intel_crtc,
  5851. &fp, &reduced_clock,
  5852. has_reduced_clock ? &fp2 : NULL);
  5853. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5854. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5855. if (has_reduced_clock)
  5856. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5857. else
  5858. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5859. pll = intel_get_shared_dpll(intel_crtc);
  5860. if (pll == NULL) {
  5861. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  5862. pipe_name(intel_crtc->pipe));
  5863. return -EINVAL;
  5864. }
  5865. } else
  5866. intel_put_shared_dpll(intel_crtc);
  5867. if (is_lvds && has_reduced_clock && i915.powersave)
  5868. intel_crtc->lowfreq_avail = true;
  5869. else
  5870. intel_crtc->lowfreq_avail = false;
  5871. return 0;
  5872. }
  5873. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  5874. struct intel_link_m_n *m_n)
  5875. {
  5876. struct drm_device *dev = crtc->base.dev;
  5877. struct drm_i915_private *dev_priv = dev->dev_private;
  5878. enum pipe pipe = crtc->pipe;
  5879. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  5880. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  5881. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  5882. & ~TU_SIZE_MASK;
  5883. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  5884. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  5885. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5886. }
  5887. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  5888. enum transcoder transcoder,
  5889. struct intel_link_m_n *m_n)
  5890. {
  5891. struct drm_device *dev = crtc->base.dev;
  5892. struct drm_i915_private *dev_priv = dev->dev_private;
  5893. enum pipe pipe = crtc->pipe;
  5894. if (INTEL_INFO(dev)->gen >= 5) {
  5895. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  5896. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  5897. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  5898. & ~TU_SIZE_MASK;
  5899. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  5900. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  5901. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5902. } else {
  5903. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  5904. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  5905. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  5906. & ~TU_SIZE_MASK;
  5907. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  5908. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  5909. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5910. }
  5911. }
  5912. void intel_dp_get_m_n(struct intel_crtc *crtc,
  5913. struct intel_crtc_config *pipe_config)
  5914. {
  5915. if (crtc->config.has_pch_encoder)
  5916. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  5917. else
  5918. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5919. &pipe_config->dp_m_n);
  5920. }
  5921. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  5922. struct intel_crtc_config *pipe_config)
  5923. {
  5924. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5925. &pipe_config->fdi_m_n);
  5926. }
  5927. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  5928. struct intel_crtc_config *pipe_config)
  5929. {
  5930. struct drm_device *dev = crtc->base.dev;
  5931. struct drm_i915_private *dev_priv = dev->dev_private;
  5932. uint32_t tmp;
  5933. tmp = I915_READ(PF_CTL(crtc->pipe));
  5934. if (tmp & PF_ENABLE) {
  5935. pipe_config->pch_pfit.enabled = true;
  5936. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  5937. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  5938. /* We currently do not free assignements of panel fitters on
  5939. * ivb/hsw (since we don't use the higher upscaling modes which
  5940. * differentiates them) so just WARN about this case for now. */
  5941. if (IS_GEN7(dev)) {
  5942. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  5943. PF_PIPE_SEL_IVB(crtc->pipe));
  5944. }
  5945. }
  5946. }
  5947. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  5948. struct intel_plane_config *plane_config)
  5949. {
  5950. struct drm_device *dev = crtc->base.dev;
  5951. struct drm_i915_private *dev_priv = dev->dev_private;
  5952. u32 val, base, offset;
  5953. int pipe = crtc->pipe, plane = crtc->plane;
  5954. int fourcc, pixel_format;
  5955. int aligned_height;
  5956. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5957. if (!crtc->base.primary->fb) {
  5958. DRM_DEBUG_KMS("failed to alloc fb\n");
  5959. return;
  5960. }
  5961. val = I915_READ(DSPCNTR(plane));
  5962. if (INTEL_INFO(dev)->gen >= 4)
  5963. if (val & DISPPLANE_TILED)
  5964. plane_config->tiled = true;
  5965. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5966. fourcc = intel_format_to_fourcc(pixel_format);
  5967. crtc->base.primary->fb->pixel_format = fourcc;
  5968. crtc->base.primary->fb->bits_per_pixel =
  5969. drm_format_plane_cpp(fourcc, 0) * 8;
  5970. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5971. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5972. offset = I915_READ(DSPOFFSET(plane));
  5973. } else {
  5974. if (plane_config->tiled)
  5975. offset = I915_READ(DSPTILEOFF(plane));
  5976. else
  5977. offset = I915_READ(DSPLINOFF(plane));
  5978. }
  5979. plane_config->base = base;
  5980. val = I915_READ(PIPESRC(pipe));
  5981. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5982. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5983. val = I915_READ(DSPSTRIDE(pipe));
  5984. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5985. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5986. plane_config->tiled);
  5987. plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
  5988. aligned_height, PAGE_SIZE);
  5989. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5990. pipe, plane, crtc->base.primary->fb->width,
  5991. crtc->base.primary->fb->height,
  5992. crtc->base.primary->fb->bits_per_pixel, base,
  5993. crtc->base.primary->fb->pitches[0],
  5994. plane_config->size);
  5995. }
  5996. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  5997. struct intel_crtc_config *pipe_config)
  5998. {
  5999. struct drm_device *dev = crtc->base.dev;
  6000. struct drm_i915_private *dev_priv = dev->dev_private;
  6001. uint32_t tmp;
  6002. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6003. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6004. tmp = I915_READ(PIPECONF(crtc->pipe));
  6005. if (!(tmp & PIPECONF_ENABLE))
  6006. return false;
  6007. switch (tmp & PIPECONF_BPC_MASK) {
  6008. case PIPECONF_6BPC:
  6009. pipe_config->pipe_bpp = 18;
  6010. break;
  6011. case PIPECONF_8BPC:
  6012. pipe_config->pipe_bpp = 24;
  6013. break;
  6014. case PIPECONF_10BPC:
  6015. pipe_config->pipe_bpp = 30;
  6016. break;
  6017. case PIPECONF_12BPC:
  6018. pipe_config->pipe_bpp = 36;
  6019. break;
  6020. default:
  6021. break;
  6022. }
  6023. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6024. pipe_config->limited_color_range = true;
  6025. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6026. struct intel_shared_dpll *pll;
  6027. pipe_config->has_pch_encoder = true;
  6028. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6029. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6030. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6031. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6032. if (HAS_PCH_IBX(dev_priv->dev)) {
  6033. pipe_config->shared_dpll =
  6034. (enum intel_dpll_id) crtc->pipe;
  6035. } else {
  6036. tmp = I915_READ(PCH_DPLL_SEL);
  6037. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6038. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6039. else
  6040. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6041. }
  6042. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6043. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6044. &pipe_config->dpll_hw_state));
  6045. tmp = pipe_config->dpll_hw_state.dpll;
  6046. pipe_config->pixel_multiplier =
  6047. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6048. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6049. ironlake_pch_clock_get(crtc, pipe_config);
  6050. } else {
  6051. pipe_config->pixel_multiplier = 1;
  6052. }
  6053. intel_get_pipe_timings(crtc, pipe_config);
  6054. ironlake_get_pfit_config(crtc, pipe_config);
  6055. return true;
  6056. }
  6057. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6058. {
  6059. struct drm_device *dev = dev_priv->dev;
  6060. struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
  6061. struct intel_crtc *crtc;
  6062. for_each_intel_crtc(dev, crtc)
  6063. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6064. pipe_name(crtc->pipe));
  6065. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6066. WARN(plls->spll_refcount, "SPLL enabled\n");
  6067. WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
  6068. WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
  6069. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6070. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6071. "CPU PWM1 enabled\n");
  6072. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6073. "CPU PWM2 enabled\n");
  6074. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6075. "PCH PWM1 enabled\n");
  6076. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6077. "Utility pin enabled\n");
  6078. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6079. /*
  6080. * In theory we can still leave IRQs enabled, as long as only the HPD
  6081. * interrupts remain enabled. We used to check for that, but since it's
  6082. * gen-specific and since we only disable LCPLL after we fully disable
  6083. * the interrupts, the check below should be enough.
  6084. */
  6085. WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
  6086. }
  6087. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6088. {
  6089. struct drm_device *dev = dev_priv->dev;
  6090. if (IS_HASWELL(dev)) {
  6091. mutex_lock(&dev_priv->rps.hw_lock);
  6092. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6093. val))
  6094. DRM_ERROR("Failed to disable D_COMP\n");
  6095. mutex_unlock(&dev_priv->rps.hw_lock);
  6096. } else {
  6097. I915_WRITE(D_COMP, val);
  6098. }
  6099. POSTING_READ(D_COMP);
  6100. }
  6101. /*
  6102. * This function implements pieces of two sequences from BSpec:
  6103. * - Sequence for display software to disable LCPLL
  6104. * - Sequence for display software to allow package C8+
  6105. * The steps implemented here are just the steps that actually touch the LCPLL
  6106. * register. Callers should take care of disabling all the display engine
  6107. * functions, doing the mode unset, fixing interrupts, etc.
  6108. */
  6109. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6110. bool switch_to_fclk, bool allow_power_down)
  6111. {
  6112. uint32_t val;
  6113. assert_can_disable_lcpll(dev_priv);
  6114. val = I915_READ(LCPLL_CTL);
  6115. if (switch_to_fclk) {
  6116. val |= LCPLL_CD_SOURCE_FCLK;
  6117. I915_WRITE(LCPLL_CTL, val);
  6118. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6119. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6120. DRM_ERROR("Switching to FCLK failed\n");
  6121. val = I915_READ(LCPLL_CTL);
  6122. }
  6123. val |= LCPLL_PLL_DISABLE;
  6124. I915_WRITE(LCPLL_CTL, val);
  6125. POSTING_READ(LCPLL_CTL);
  6126. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6127. DRM_ERROR("LCPLL still locked\n");
  6128. val = I915_READ(D_COMP);
  6129. val |= D_COMP_COMP_DISABLE;
  6130. hsw_write_dcomp(dev_priv, val);
  6131. ndelay(100);
  6132. if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
  6133. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6134. if (allow_power_down) {
  6135. val = I915_READ(LCPLL_CTL);
  6136. val |= LCPLL_POWER_DOWN_ALLOW;
  6137. I915_WRITE(LCPLL_CTL, val);
  6138. POSTING_READ(LCPLL_CTL);
  6139. }
  6140. }
  6141. /*
  6142. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6143. * source.
  6144. */
  6145. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6146. {
  6147. uint32_t val;
  6148. unsigned long irqflags;
  6149. val = I915_READ(LCPLL_CTL);
  6150. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6151. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6152. return;
  6153. /*
  6154. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6155. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6156. *
  6157. * The other problem is that hsw_restore_lcpll() is called as part of
  6158. * the runtime PM resume sequence, so we can't just call
  6159. * gen6_gt_force_wake_get() because that function calls
  6160. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6161. * while we are on the resume sequence. So to solve this problem we have
  6162. * to call special forcewake code that doesn't touch runtime PM and
  6163. * doesn't enable the forcewake delayed work.
  6164. */
  6165. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6166. if (dev_priv->uncore.forcewake_count++ == 0)
  6167. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6168. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6169. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6170. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6171. I915_WRITE(LCPLL_CTL, val);
  6172. POSTING_READ(LCPLL_CTL);
  6173. }
  6174. val = I915_READ(D_COMP);
  6175. val |= D_COMP_COMP_FORCE;
  6176. val &= ~D_COMP_COMP_DISABLE;
  6177. hsw_write_dcomp(dev_priv, val);
  6178. val = I915_READ(LCPLL_CTL);
  6179. val &= ~LCPLL_PLL_DISABLE;
  6180. I915_WRITE(LCPLL_CTL, val);
  6181. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6182. DRM_ERROR("LCPLL not locked yet\n");
  6183. if (val & LCPLL_CD_SOURCE_FCLK) {
  6184. val = I915_READ(LCPLL_CTL);
  6185. val &= ~LCPLL_CD_SOURCE_FCLK;
  6186. I915_WRITE(LCPLL_CTL, val);
  6187. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6188. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6189. DRM_ERROR("Switching back to LCPLL failed\n");
  6190. }
  6191. /* See the big comment above. */
  6192. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6193. if (--dev_priv->uncore.forcewake_count == 0)
  6194. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6195. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6196. }
  6197. /*
  6198. * Package states C8 and deeper are really deep PC states that can only be
  6199. * reached when all the devices on the system allow it, so even if the graphics
  6200. * device allows PC8+, it doesn't mean the system will actually get to these
  6201. * states. Our driver only allows PC8+ when going into runtime PM.
  6202. *
  6203. * The requirements for PC8+ are that all the outputs are disabled, the power
  6204. * well is disabled and most interrupts are disabled, and these are also
  6205. * requirements for runtime PM. When these conditions are met, we manually do
  6206. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6207. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6208. * hang the machine.
  6209. *
  6210. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6211. * the state of some registers, so when we come back from PC8+ we need to
  6212. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6213. * need to take care of the registers kept by RC6. Notice that this happens even
  6214. * if we don't put the device in PCI D3 state (which is what currently happens
  6215. * because of the runtime PM support).
  6216. *
  6217. * For more, read "Display Sequences for Package C8" on the hardware
  6218. * documentation.
  6219. */
  6220. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6221. {
  6222. struct drm_device *dev = dev_priv->dev;
  6223. uint32_t val;
  6224. DRM_DEBUG_KMS("Enabling package C8+\n");
  6225. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6226. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6227. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6228. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6229. }
  6230. lpt_disable_clkout_dp(dev);
  6231. hsw_disable_lcpll(dev_priv, true, true);
  6232. }
  6233. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6234. {
  6235. struct drm_device *dev = dev_priv->dev;
  6236. uint32_t val;
  6237. DRM_DEBUG_KMS("Disabling package C8+\n");
  6238. hsw_restore_lcpll(dev_priv);
  6239. lpt_init_pch_refclk(dev);
  6240. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6241. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6242. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6243. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6244. }
  6245. intel_prepare_ddi(dev);
  6246. }
  6247. static void snb_modeset_global_resources(struct drm_device *dev)
  6248. {
  6249. modeset_update_crtc_power_domains(dev);
  6250. }
  6251. static void haswell_modeset_global_resources(struct drm_device *dev)
  6252. {
  6253. modeset_update_crtc_power_domains(dev);
  6254. }
  6255. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6256. int x, int y,
  6257. struct drm_framebuffer *fb)
  6258. {
  6259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6260. if (!intel_ddi_pll_select(intel_crtc))
  6261. return -EINVAL;
  6262. intel_ddi_pll_enable(intel_crtc);
  6263. intel_crtc->lowfreq_avail = false;
  6264. return 0;
  6265. }
  6266. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6267. struct intel_crtc_config *pipe_config)
  6268. {
  6269. struct drm_device *dev = crtc->base.dev;
  6270. struct drm_i915_private *dev_priv = dev->dev_private;
  6271. enum intel_display_power_domain pfit_domain;
  6272. uint32_t tmp;
  6273. if (!intel_display_power_enabled(dev_priv,
  6274. POWER_DOMAIN_PIPE(crtc->pipe)))
  6275. return false;
  6276. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6277. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6278. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6279. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6280. enum pipe trans_edp_pipe;
  6281. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6282. default:
  6283. WARN(1, "unknown pipe linked to edp transcoder\n");
  6284. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6285. case TRANS_DDI_EDP_INPUT_A_ON:
  6286. trans_edp_pipe = PIPE_A;
  6287. break;
  6288. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6289. trans_edp_pipe = PIPE_B;
  6290. break;
  6291. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6292. trans_edp_pipe = PIPE_C;
  6293. break;
  6294. }
  6295. if (trans_edp_pipe == crtc->pipe)
  6296. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6297. }
  6298. if (!intel_display_power_enabled(dev_priv,
  6299. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6300. return false;
  6301. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6302. if (!(tmp & PIPECONF_ENABLE))
  6303. return false;
  6304. /*
  6305. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6306. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6307. * the PCH transcoder is on.
  6308. */
  6309. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6310. if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
  6311. I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6312. pipe_config->has_pch_encoder = true;
  6313. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6314. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6315. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6316. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6317. }
  6318. intel_get_pipe_timings(crtc, pipe_config);
  6319. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6320. if (intel_display_power_enabled(dev_priv, pfit_domain))
  6321. ironlake_get_pfit_config(crtc, pipe_config);
  6322. if (IS_HASWELL(dev))
  6323. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6324. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6325. pipe_config->pixel_multiplier = 1;
  6326. return true;
  6327. }
  6328. static struct {
  6329. int clock;
  6330. u32 config;
  6331. } hdmi_audio_clock[] = {
  6332. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6333. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6334. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6335. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6336. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6337. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6338. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6339. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6340. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6341. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6342. };
  6343. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6344. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6345. {
  6346. int i;
  6347. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6348. if (mode->clock == hdmi_audio_clock[i].clock)
  6349. break;
  6350. }
  6351. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6352. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6353. i = 1;
  6354. }
  6355. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6356. hdmi_audio_clock[i].clock,
  6357. hdmi_audio_clock[i].config);
  6358. return hdmi_audio_clock[i].config;
  6359. }
  6360. static bool intel_eld_uptodate(struct drm_connector *connector,
  6361. int reg_eldv, uint32_t bits_eldv,
  6362. int reg_elda, uint32_t bits_elda,
  6363. int reg_edid)
  6364. {
  6365. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6366. uint8_t *eld = connector->eld;
  6367. uint32_t i;
  6368. i = I915_READ(reg_eldv);
  6369. i &= bits_eldv;
  6370. if (!eld[0])
  6371. return !i;
  6372. if (!i)
  6373. return false;
  6374. i = I915_READ(reg_elda);
  6375. i &= ~bits_elda;
  6376. I915_WRITE(reg_elda, i);
  6377. for (i = 0; i < eld[2]; i++)
  6378. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6379. return false;
  6380. return true;
  6381. }
  6382. static void g4x_write_eld(struct drm_connector *connector,
  6383. struct drm_crtc *crtc,
  6384. struct drm_display_mode *mode)
  6385. {
  6386. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6387. uint8_t *eld = connector->eld;
  6388. uint32_t eldv;
  6389. uint32_t len;
  6390. uint32_t i;
  6391. i = I915_READ(G4X_AUD_VID_DID);
  6392. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6393. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6394. else
  6395. eldv = G4X_ELDV_DEVCTG;
  6396. if (intel_eld_uptodate(connector,
  6397. G4X_AUD_CNTL_ST, eldv,
  6398. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6399. G4X_HDMIW_HDMIEDID))
  6400. return;
  6401. i = I915_READ(G4X_AUD_CNTL_ST);
  6402. i &= ~(eldv | G4X_ELD_ADDR);
  6403. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6404. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6405. if (!eld[0])
  6406. return;
  6407. len = min_t(uint8_t, eld[2], len);
  6408. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6409. for (i = 0; i < len; i++)
  6410. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6411. i = I915_READ(G4X_AUD_CNTL_ST);
  6412. i |= eldv;
  6413. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6414. }
  6415. static void haswell_write_eld(struct drm_connector *connector,
  6416. struct drm_crtc *crtc,
  6417. struct drm_display_mode *mode)
  6418. {
  6419. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6420. uint8_t *eld = connector->eld;
  6421. uint32_t eldv;
  6422. uint32_t i;
  6423. int len;
  6424. int pipe = to_intel_crtc(crtc)->pipe;
  6425. int tmp;
  6426. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6427. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6428. int aud_config = HSW_AUD_CFG(pipe);
  6429. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6430. /* Audio output enable */
  6431. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6432. tmp = I915_READ(aud_cntrl_st2);
  6433. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6434. I915_WRITE(aud_cntrl_st2, tmp);
  6435. POSTING_READ(aud_cntrl_st2);
  6436. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6437. /* Set ELD valid state */
  6438. tmp = I915_READ(aud_cntrl_st2);
  6439. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6440. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6441. I915_WRITE(aud_cntrl_st2, tmp);
  6442. tmp = I915_READ(aud_cntrl_st2);
  6443. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6444. /* Enable HDMI mode */
  6445. tmp = I915_READ(aud_config);
  6446. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6447. /* clear N_programing_enable and N_value_index */
  6448. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6449. I915_WRITE(aud_config, tmp);
  6450. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6451. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6452. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6453. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6454. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6455. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6456. } else {
  6457. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6458. }
  6459. if (intel_eld_uptodate(connector,
  6460. aud_cntrl_st2, eldv,
  6461. aud_cntl_st, IBX_ELD_ADDRESS,
  6462. hdmiw_hdmiedid))
  6463. return;
  6464. i = I915_READ(aud_cntrl_st2);
  6465. i &= ~eldv;
  6466. I915_WRITE(aud_cntrl_st2, i);
  6467. if (!eld[0])
  6468. return;
  6469. i = I915_READ(aud_cntl_st);
  6470. i &= ~IBX_ELD_ADDRESS;
  6471. I915_WRITE(aud_cntl_st, i);
  6472. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6473. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6474. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6475. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6476. for (i = 0; i < len; i++)
  6477. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6478. i = I915_READ(aud_cntrl_st2);
  6479. i |= eldv;
  6480. I915_WRITE(aud_cntrl_st2, i);
  6481. }
  6482. static void ironlake_write_eld(struct drm_connector *connector,
  6483. struct drm_crtc *crtc,
  6484. struct drm_display_mode *mode)
  6485. {
  6486. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6487. uint8_t *eld = connector->eld;
  6488. uint32_t eldv;
  6489. uint32_t i;
  6490. int len;
  6491. int hdmiw_hdmiedid;
  6492. int aud_config;
  6493. int aud_cntl_st;
  6494. int aud_cntrl_st2;
  6495. int pipe = to_intel_crtc(crtc)->pipe;
  6496. if (HAS_PCH_IBX(connector->dev)) {
  6497. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6498. aud_config = IBX_AUD_CFG(pipe);
  6499. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6500. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6501. } else if (IS_VALLEYVIEW(connector->dev)) {
  6502. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6503. aud_config = VLV_AUD_CFG(pipe);
  6504. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6505. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6506. } else {
  6507. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6508. aud_config = CPT_AUD_CFG(pipe);
  6509. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6510. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6511. }
  6512. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6513. if (IS_VALLEYVIEW(connector->dev)) {
  6514. struct intel_encoder *intel_encoder;
  6515. struct intel_digital_port *intel_dig_port;
  6516. intel_encoder = intel_attached_encoder(connector);
  6517. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6518. i = intel_dig_port->port;
  6519. } else {
  6520. i = I915_READ(aud_cntl_st);
  6521. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6522. /* DIP_Port_Select, 0x1 = PortB */
  6523. }
  6524. if (!i) {
  6525. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6526. /* operate blindly on all ports */
  6527. eldv = IBX_ELD_VALIDB;
  6528. eldv |= IBX_ELD_VALIDB << 4;
  6529. eldv |= IBX_ELD_VALIDB << 8;
  6530. } else {
  6531. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6532. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6533. }
  6534. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6535. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6536. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6537. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6538. } else {
  6539. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6540. }
  6541. if (intel_eld_uptodate(connector,
  6542. aud_cntrl_st2, eldv,
  6543. aud_cntl_st, IBX_ELD_ADDRESS,
  6544. hdmiw_hdmiedid))
  6545. return;
  6546. i = I915_READ(aud_cntrl_st2);
  6547. i &= ~eldv;
  6548. I915_WRITE(aud_cntrl_st2, i);
  6549. if (!eld[0])
  6550. return;
  6551. i = I915_READ(aud_cntl_st);
  6552. i &= ~IBX_ELD_ADDRESS;
  6553. I915_WRITE(aud_cntl_st, i);
  6554. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6555. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6556. for (i = 0; i < len; i++)
  6557. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6558. i = I915_READ(aud_cntrl_st2);
  6559. i |= eldv;
  6560. I915_WRITE(aud_cntrl_st2, i);
  6561. }
  6562. void intel_write_eld(struct drm_encoder *encoder,
  6563. struct drm_display_mode *mode)
  6564. {
  6565. struct drm_crtc *crtc = encoder->crtc;
  6566. struct drm_connector *connector;
  6567. struct drm_device *dev = encoder->dev;
  6568. struct drm_i915_private *dev_priv = dev->dev_private;
  6569. connector = drm_select_eld(encoder, mode);
  6570. if (!connector)
  6571. return;
  6572. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6573. connector->base.id,
  6574. connector->name,
  6575. connector->encoder->base.id,
  6576. connector->encoder->name);
  6577. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6578. if (dev_priv->display.write_eld)
  6579. dev_priv->display.write_eld(connector, crtc, mode);
  6580. }
  6581. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6582. {
  6583. struct drm_device *dev = crtc->dev;
  6584. struct drm_i915_private *dev_priv = dev->dev_private;
  6585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6586. uint32_t cntl;
  6587. if (base != intel_crtc->cursor_base) {
  6588. /* On these chipsets we can only modify the base whilst
  6589. * the cursor is disabled.
  6590. */
  6591. if (intel_crtc->cursor_cntl) {
  6592. I915_WRITE(_CURACNTR, 0);
  6593. POSTING_READ(_CURACNTR);
  6594. intel_crtc->cursor_cntl = 0;
  6595. }
  6596. I915_WRITE(_CURABASE, base);
  6597. POSTING_READ(_CURABASE);
  6598. }
  6599. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  6600. cntl = 0;
  6601. if (base)
  6602. cntl = (CURSOR_ENABLE |
  6603. CURSOR_GAMMA_ENABLE |
  6604. CURSOR_FORMAT_ARGB);
  6605. if (intel_crtc->cursor_cntl != cntl) {
  6606. I915_WRITE(_CURACNTR, cntl);
  6607. POSTING_READ(_CURACNTR);
  6608. intel_crtc->cursor_cntl = cntl;
  6609. }
  6610. }
  6611. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6612. {
  6613. struct drm_device *dev = crtc->dev;
  6614. struct drm_i915_private *dev_priv = dev->dev_private;
  6615. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6616. int pipe = intel_crtc->pipe;
  6617. uint32_t cntl;
  6618. cntl = 0;
  6619. if (base) {
  6620. cntl = MCURSOR_GAMMA_ENABLE;
  6621. switch (intel_crtc->cursor_width) {
  6622. case 64:
  6623. cntl |= CURSOR_MODE_64_ARGB_AX;
  6624. break;
  6625. case 128:
  6626. cntl |= CURSOR_MODE_128_ARGB_AX;
  6627. break;
  6628. case 256:
  6629. cntl |= CURSOR_MODE_256_ARGB_AX;
  6630. break;
  6631. default:
  6632. WARN_ON(1);
  6633. return;
  6634. }
  6635. cntl |= pipe << 28; /* Connect to correct pipe */
  6636. }
  6637. if (intel_crtc->cursor_cntl != cntl) {
  6638. I915_WRITE(CURCNTR(pipe), cntl);
  6639. POSTING_READ(CURCNTR(pipe));
  6640. intel_crtc->cursor_cntl = cntl;
  6641. }
  6642. /* and commit changes on next vblank */
  6643. I915_WRITE(CURBASE(pipe), base);
  6644. POSTING_READ(CURBASE(pipe));
  6645. }
  6646. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  6647. {
  6648. struct drm_device *dev = crtc->dev;
  6649. struct drm_i915_private *dev_priv = dev->dev_private;
  6650. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6651. int pipe = intel_crtc->pipe;
  6652. uint32_t cntl;
  6653. cntl = 0;
  6654. if (base) {
  6655. cntl = MCURSOR_GAMMA_ENABLE;
  6656. switch (intel_crtc->cursor_width) {
  6657. case 64:
  6658. cntl |= CURSOR_MODE_64_ARGB_AX;
  6659. break;
  6660. case 128:
  6661. cntl |= CURSOR_MODE_128_ARGB_AX;
  6662. break;
  6663. case 256:
  6664. cntl |= CURSOR_MODE_256_ARGB_AX;
  6665. break;
  6666. default:
  6667. WARN_ON(1);
  6668. return;
  6669. }
  6670. }
  6671. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6672. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6673. if (intel_crtc->cursor_cntl != cntl) {
  6674. I915_WRITE(CURCNTR(pipe), cntl);
  6675. POSTING_READ(CURCNTR(pipe));
  6676. intel_crtc->cursor_cntl = cntl;
  6677. }
  6678. /* and commit changes on next vblank */
  6679. I915_WRITE(CURBASE(pipe), base);
  6680. POSTING_READ(CURBASE(pipe));
  6681. }
  6682. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6683. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6684. bool on)
  6685. {
  6686. struct drm_device *dev = crtc->dev;
  6687. struct drm_i915_private *dev_priv = dev->dev_private;
  6688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6689. int pipe = intel_crtc->pipe;
  6690. int x = intel_crtc->cursor_x;
  6691. int y = intel_crtc->cursor_y;
  6692. u32 base = 0, pos = 0;
  6693. if (on)
  6694. base = intel_crtc->cursor_addr;
  6695. if (x >= intel_crtc->config.pipe_src_w)
  6696. base = 0;
  6697. if (y >= intel_crtc->config.pipe_src_h)
  6698. base = 0;
  6699. if (x < 0) {
  6700. if (x + intel_crtc->cursor_width <= 0)
  6701. base = 0;
  6702. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6703. x = -x;
  6704. }
  6705. pos |= x << CURSOR_X_SHIFT;
  6706. if (y < 0) {
  6707. if (y + intel_crtc->cursor_height <= 0)
  6708. base = 0;
  6709. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6710. y = -y;
  6711. }
  6712. pos |= y << CURSOR_Y_SHIFT;
  6713. if (base == 0 && intel_crtc->cursor_base == 0)
  6714. return;
  6715. I915_WRITE(CURPOS(pipe), pos);
  6716. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
  6717. ivb_update_cursor(crtc, base);
  6718. else if (IS_845G(dev) || IS_I865G(dev))
  6719. i845_update_cursor(crtc, base);
  6720. else
  6721. i9xx_update_cursor(crtc, base);
  6722. intel_crtc->cursor_base = base;
  6723. }
  6724. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  6725. struct drm_file *file,
  6726. uint32_t handle,
  6727. uint32_t width, uint32_t height)
  6728. {
  6729. struct drm_device *dev = crtc->dev;
  6730. struct drm_i915_private *dev_priv = dev->dev_private;
  6731. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6732. struct drm_i915_gem_object *obj;
  6733. unsigned old_width;
  6734. uint32_t addr;
  6735. int ret;
  6736. /* if we want to turn off the cursor ignore width and height */
  6737. if (!handle) {
  6738. DRM_DEBUG_KMS("cursor off\n");
  6739. addr = 0;
  6740. obj = NULL;
  6741. mutex_lock(&dev->struct_mutex);
  6742. goto finish;
  6743. }
  6744. /* Check for which cursor types we support */
  6745. if (!((width == 64 && height == 64) ||
  6746. (width == 128 && height == 128 && !IS_GEN2(dev)) ||
  6747. (width == 256 && height == 256 && !IS_GEN2(dev)))) {
  6748. DRM_DEBUG("Cursor dimension not supported\n");
  6749. return -EINVAL;
  6750. }
  6751. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  6752. if (&obj->base == NULL)
  6753. return -ENOENT;
  6754. if (obj->base.size < width * height * 4) {
  6755. DRM_DEBUG_KMS("buffer is to small\n");
  6756. ret = -ENOMEM;
  6757. goto fail;
  6758. }
  6759. /* we only need to pin inside GTT if cursor is non-phy */
  6760. mutex_lock(&dev->struct_mutex);
  6761. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  6762. unsigned alignment;
  6763. if (obj->tiling_mode) {
  6764. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  6765. ret = -EINVAL;
  6766. goto fail_locked;
  6767. }
  6768. /* Note that the w/a also requires 2 PTE of padding following
  6769. * the bo. We currently fill all unused PTE with the shadow
  6770. * page and so we should always have valid PTE following the
  6771. * cursor preventing the VT-d warning.
  6772. */
  6773. alignment = 0;
  6774. if (need_vtd_wa(dev))
  6775. alignment = 64*1024;
  6776. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  6777. if (ret) {
  6778. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  6779. goto fail_locked;
  6780. }
  6781. ret = i915_gem_object_put_fence(obj);
  6782. if (ret) {
  6783. DRM_DEBUG_KMS("failed to release fence for cursor");
  6784. goto fail_unpin;
  6785. }
  6786. addr = i915_gem_obj_ggtt_offset(obj);
  6787. } else {
  6788. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6789. ret = i915_gem_object_attach_phys(obj, align);
  6790. if (ret) {
  6791. DRM_DEBUG_KMS("failed to attach phys object\n");
  6792. goto fail_locked;
  6793. }
  6794. addr = obj->phys_handle->busaddr;
  6795. }
  6796. if (IS_GEN2(dev))
  6797. I915_WRITE(CURSIZE, (height << 12) | width);
  6798. finish:
  6799. if (intel_crtc->cursor_bo) {
  6800. if (!INTEL_INFO(dev)->cursor_needs_physical)
  6801. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  6802. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  6803. }
  6804. mutex_unlock(&dev->struct_mutex);
  6805. old_width = intel_crtc->cursor_width;
  6806. intel_crtc->cursor_addr = addr;
  6807. intel_crtc->cursor_bo = obj;
  6808. intel_crtc->cursor_width = width;
  6809. intel_crtc->cursor_height = height;
  6810. if (intel_crtc->active) {
  6811. if (old_width != width)
  6812. intel_update_watermarks(crtc);
  6813. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6814. }
  6815. return 0;
  6816. fail_unpin:
  6817. i915_gem_object_unpin_from_display_plane(obj);
  6818. fail_locked:
  6819. mutex_unlock(&dev->struct_mutex);
  6820. fail:
  6821. drm_gem_object_unreference_unlocked(&obj->base);
  6822. return ret;
  6823. }
  6824. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  6825. {
  6826. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6827. intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
  6828. intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
  6829. if (intel_crtc->active)
  6830. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6831. return 0;
  6832. }
  6833. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6834. u16 *blue, uint32_t start, uint32_t size)
  6835. {
  6836. int end = (start + size > 256) ? 256 : start + size, i;
  6837. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6838. for (i = start; i < end; i++) {
  6839. intel_crtc->lut_r[i] = red[i] >> 8;
  6840. intel_crtc->lut_g[i] = green[i] >> 8;
  6841. intel_crtc->lut_b[i] = blue[i] >> 8;
  6842. }
  6843. intel_crtc_load_lut(crtc);
  6844. }
  6845. /* VESA 640x480x72Hz mode to set on the pipe */
  6846. static struct drm_display_mode load_detect_mode = {
  6847. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6848. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6849. };
  6850. struct drm_framebuffer *
  6851. __intel_framebuffer_create(struct drm_device *dev,
  6852. struct drm_mode_fb_cmd2 *mode_cmd,
  6853. struct drm_i915_gem_object *obj)
  6854. {
  6855. struct intel_framebuffer *intel_fb;
  6856. int ret;
  6857. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6858. if (!intel_fb) {
  6859. drm_gem_object_unreference_unlocked(&obj->base);
  6860. return ERR_PTR(-ENOMEM);
  6861. }
  6862. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  6863. if (ret)
  6864. goto err;
  6865. return &intel_fb->base;
  6866. err:
  6867. drm_gem_object_unreference_unlocked(&obj->base);
  6868. kfree(intel_fb);
  6869. return ERR_PTR(ret);
  6870. }
  6871. static struct drm_framebuffer *
  6872. intel_framebuffer_create(struct drm_device *dev,
  6873. struct drm_mode_fb_cmd2 *mode_cmd,
  6874. struct drm_i915_gem_object *obj)
  6875. {
  6876. struct drm_framebuffer *fb;
  6877. int ret;
  6878. ret = i915_mutex_lock_interruptible(dev);
  6879. if (ret)
  6880. return ERR_PTR(ret);
  6881. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  6882. mutex_unlock(&dev->struct_mutex);
  6883. return fb;
  6884. }
  6885. static u32
  6886. intel_framebuffer_pitch_for_width(int width, int bpp)
  6887. {
  6888. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  6889. return ALIGN(pitch, 64);
  6890. }
  6891. static u32
  6892. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  6893. {
  6894. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  6895. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  6896. }
  6897. static struct drm_framebuffer *
  6898. intel_framebuffer_create_for_mode(struct drm_device *dev,
  6899. struct drm_display_mode *mode,
  6900. int depth, int bpp)
  6901. {
  6902. struct drm_i915_gem_object *obj;
  6903. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  6904. obj = i915_gem_alloc_object(dev,
  6905. intel_framebuffer_size_for_mode(mode, bpp));
  6906. if (obj == NULL)
  6907. return ERR_PTR(-ENOMEM);
  6908. mode_cmd.width = mode->hdisplay;
  6909. mode_cmd.height = mode->vdisplay;
  6910. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  6911. bpp);
  6912. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  6913. return intel_framebuffer_create(dev, &mode_cmd, obj);
  6914. }
  6915. static struct drm_framebuffer *
  6916. mode_fits_in_fbdev(struct drm_device *dev,
  6917. struct drm_display_mode *mode)
  6918. {
  6919. #ifdef CONFIG_DRM_I915_FBDEV
  6920. struct drm_i915_private *dev_priv = dev->dev_private;
  6921. struct drm_i915_gem_object *obj;
  6922. struct drm_framebuffer *fb;
  6923. if (!dev_priv->fbdev)
  6924. return NULL;
  6925. if (!dev_priv->fbdev->fb)
  6926. return NULL;
  6927. obj = dev_priv->fbdev->fb->obj;
  6928. BUG_ON(!obj);
  6929. fb = &dev_priv->fbdev->fb->base;
  6930. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  6931. fb->bits_per_pixel))
  6932. return NULL;
  6933. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  6934. return NULL;
  6935. return fb;
  6936. #else
  6937. return NULL;
  6938. #endif
  6939. }
  6940. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  6941. struct drm_display_mode *mode,
  6942. struct intel_load_detect_pipe *old,
  6943. struct drm_modeset_acquire_ctx *ctx)
  6944. {
  6945. struct intel_crtc *intel_crtc;
  6946. struct intel_encoder *intel_encoder =
  6947. intel_attached_encoder(connector);
  6948. struct drm_crtc *possible_crtc;
  6949. struct drm_encoder *encoder = &intel_encoder->base;
  6950. struct drm_crtc *crtc = NULL;
  6951. struct drm_device *dev = encoder->dev;
  6952. struct drm_framebuffer *fb;
  6953. struct drm_mode_config *config = &dev->mode_config;
  6954. int ret, i = -1;
  6955. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6956. connector->base.id, connector->name,
  6957. encoder->base.id, encoder->name);
  6958. drm_modeset_acquire_init(ctx, 0);
  6959. retry:
  6960. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  6961. if (ret)
  6962. goto fail_unlock;
  6963. /*
  6964. * Algorithm gets a little messy:
  6965. *
  6966. * - if the connector already has an assigned crtc, use it (but make
  6967. * sure it's on first)
  6968. *
  6969. * - try to find the first unused crtc that can drive this connector,
  6970. * and use that if we find one
  6971. */
  6972. /* See if we already have a CRTC for this connector */
  6973. if (encoder->crtc) {
  6974. crtc = encoder->crtc;
  6975. ret = drm_modeset_lock(&crtc->mutex, ctx);
  6976. if (ret)
  6977. goto fail_unlock;
  6978. old->dpms_mode = connector->dpms;
  6979. old->load_detect_temp = false;
  6980. /* Make sure the crtc and connector are running */
  6981. if (connector->dpms != DRM_MODE_DPMS_ON)
  6982. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  6983. return true;
  6984. }
  6985. /* Find an unused one (if possible) */
  6986. for_each_crtc(dev, possible_crtc) {
  6987. i++;
  6988. if (!(encoder->possible_crtcs & (1 << i)))
  6989. continue;
  6990. if (!possible_crtc->enabled) {
  6991. crtc = possible_crtc;
  6992. break;
  6993. }
  6994. }
  6995. /*
  6996. * If we didn't find an unused CRTC, don't use any.
  6997. */
  6998. if (!crtc) {
  6999. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7000. goto fail_unlock;
  7001. }
  7002. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7003. if (ret)
  7004. goto fail_unlock;
  7005. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7006. to_intel_connector(connector)->new_encoder = intel_encoder;
  7007. intel_crtc = to_intel_crtc(crtc);
  7008. intel_crtc->new_enabled = true;
  7009. intel_crtc->new_config = &intel_crtc->config;
  7010. old->dpms_mode = connector->dpms;
  7011. old->load_detect_temp = true;
  7012. old->release_fb = NULL;
  7013. if (!mode)
  7014. mode = &load_detect_mode;
  7015. /* We need a framebuffer large enough to accommodate all accesses
  7016. * that the plane may generate whilst we perform load detection.
  7017. * We can not rely on the fbcon either being present (we get called
  7018. * during its initialisation to detect all boot displays, or it may
  7019. * not even exist) or that it is large enough to satisfy the
  7020. * requested mode.
  7021. */
  7022. fb = mode_fits_in_fbdev(dev, mode);
  7023. if (fb == NULL) {
  7024. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7025. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7026. old->release_fb = fb;
  7027. } else
  7028. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7029. if (IS_ERR(fb)) {
  7030. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7031. goto fail;
  7032. }
  7033. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7034. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7035. if (old->release_fb)
  7036. old->release_fb->funcs->destroy(old->release_fb);
  7037. goto fail;
  7038. }
  7039. /* let the connector get through one full cycle before testing */
  7040. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7041. return true;
  7042. fail:
  7043. intel_crtc->new_enabled = crtc->enabled;
  7044. if (intel_crtc->new_enabled)
  7045. intel_crtc->new_config = &intel_crtc->config;
  7046. else
  7047. intel_crtc->new_config = NULL;
  7048. fail_unlock:
  7049. if (ret == -EDEADLK) {
  7050. drm_modeset_backoff(ctx);
  7051. goto retry;
  7052. }
  7053. drm_modeset_drop_locks(ctx);
  7054. drm_modeset_acquire_fini(ctx);
  7055. return false;
  7056. }
  7057. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7058. struct intel_load_detect_pipe *old,
  7059. struct drm_modeset_acquire_ctx *ctx)
  7060. {
  7061. struct intel_encoder *intel_encoder =
  7062. intel_attached_encoder(connector);
  7063. struct drm_encoder *encoder = &intel_encoder->base;
  7064. struct drm_crtc *crtc = encoder->crtc;
  7065. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7066. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7067. connector->base.id, connector->name,
  7068. encoder->base.id, encoder->name);
  7069. if (old->load_detect_temp) {
  7070. to_intel_connector(connector)->new_encoder = NULL;
  7071. intel_encoder->new_crtc = NULL;
  7072. intel_crtc->new_enabled = false;
  7073. intel_crtc->new_config = NULL;
  7074. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7075. if (old->release_fb) {
  7076. drm_framebuffer_unregister_private(old->release_fb);
  7077. drm_framebuffer_unreference(old->release_fb);
  7078. }
  7079. goto unlock;
  7080. return;
  7081. }
  7082. /* Switch crtc and encoder back off if necessary */
  7083. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7084. connector->funcs->dpms(connector, old->dpms_mode);
  7085. unlock:
  7086. drm_modeset_drop_locks(ctx);
  7087. drm_modeset_acquire_fini(ctx);
  7088. }
  7089. static int i9xx_pll_refclk(struct drm_device *dev,
  7090. const struct intel_crtc_config *pipe_config)
  7091. {
  7092. struct drm_i915_private *dev_priv = dev->dev_private;
  7093. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7094. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7095. return dev_priv->vbt.lvds_ssc_freq;
  7096. else if (HAS_PCH_SPLIT(dev))
  7097. return 120000;
  7098. else if (!IS_GEN2(dev))
  7099. return 96000;
  7100. else
  7101. return 48000;
  7102. }
  7103. /* Returns the clock of the currently programmed mode of the given pipe. */
  7104. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7105. struct intel_crtc_config *pipe_config)
  7106. {
  7107. struct drm_device *dev = crtc->base.dev;
  7108. struct drm_i915_private *dev_priv = dev->dev_private;
  7109. int pipe = pipe_config->cpu_transcoder;
  7110. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7111. u32 fp;
  7112. intel_clock_t clock;
  7113. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7114. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7115. fp = pipe_config->dpll_hw_state.fp0;
  7116. else
  7117. fp = pipe_config->dpll_hw_state.fp1;
  7118. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7119. if (IS_PINEVIEW(dev)) {
  7120. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7121. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7122. } else {
  7123. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7124. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7125. }
  7126. if (!IS_GEN2(dev)) {
  7127. if (IS_PINEVIEW(dev))
  7128. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7129. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7130. else
  7131. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7132. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7133. switch (dpll & DPLL_MODE_MASK) {
  7134. case DPLLB_MODE_DAC_SERIAL:
  7135. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7136. 5 : 10;
  7137. break;
  7138. case DPLLB_MODE_LVDS:
  7139. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7140. 7 : 14;
  7141. break;
  7142. default:
  7143. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7144. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7145. return;
  7146. }
  7147. if (IS_PINEVIEW(dev))
  7148. pineview_clock(refclk, &clock);
  7149. else
  7150. i9xx_clock(refclk, &clock);
  7151. } else {
  7152. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7153. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7154. if (is_lvds) {
  7155. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7156. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7157. if (lvds & LVDS_CLKB_POWER_UP)
  7158. clock.p2 = 7;
  7159. else
  7160. clock.p2 = 14;
  7161. } else {
  7162. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7163. clock.p1 = 2;
  7164. else {
  7165. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7166. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7167. }
  7168. if (dpll & PLL_P2_DIVIDE_BY_4)
  7169. clock.p2 = 4;
  7170. else
  7171. clock.p2 = 2;
  7172. }
  7173. i9xx_clock(refclk, &clock);
  7174. }
  7175. /*
  7176. * This value includes pixel_multiplier. We will use
  7177. * port_clock to compute adjusted_mode.crtc_clock in the
  7178. * encoder's get_config() function.
  7179. */
  7180. pipe_config->port_clock = clock.dot;
  7181. }
  7182. int intel_dotclock_calculate(int link_freq,
  7183. const struct intel_link_m_n *m_n)
  7184. {
  7185. /*
  7186. * The calculation for the data clock is:
  7187. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7188. * But we want to avoid losing precison if possible, so:
  7189. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7190. *
  7191. * and the link clock is simpler:
  7192. * link_clock = (m * link_clock) / n
  7193. */
  7194. if (!m_n->link_n)
  7195. return 0;
  7196. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7197. }
  7198. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7199. struct intel_crtc_config *pipe_config)
  7200. {
  7201. struct drm_device *dev = crtc->base.dev;
  7202. /* read out port_clock from the DPLL */
  7203. i9xx_crtc_clock_get(crtc, pipe_config);
  7204. /*
  7205. * This value does not include pixel_multiplier.
  7206. * We will check that port_clock and adjusted_mode.crtc_clock
  7207. * agree once we know their relationship in the encoder's
  7208. * get_config() function.
  7209. */
  7210. pipe_config->adjusted_mode.crtc_clock =
  7211. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7212. &pipe_config->fdi_m_n);
  7213. }
  7214. /** Returns the currently programmed mode of the given pipe. */
  7215. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7216. struct drm_crtc *crtc)
  7217. {
  7218. struct drm_i915_private *dev_priv = dev->dev_private;
  7219. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7220. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7221. struct drm_display_mode *mode;
  7222. struct intel_crtc_config pipe_config;
  7223. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7224. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7225. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7226. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7227. enum pipe pipe = intel_crtc->pipe;
  7228. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7229. if (!mode)
  7230. return NULL;
  7231. /*
  7232. * Construct a pipe_config sufficient for getting the clock info
  7233. * back out of crtc_clock_get.
  7234. *
  7235. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7236. * to use a real value here instead.
  7237. */
  7238. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7239. pipe_config.pixel_multiplier = 1;
  7240. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7241. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7242. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7243. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7244. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7245. mode->hdisplay = (htot & 0xffff) + 1;
  7246. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7247. mode->hsync_start = (hsync & 0xffff) + 1;
  7248. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7249. mode->vdisplay = (vtot & 0xffff) + 1;
  7250. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7251. mode->vsync_start = (vsync & 0xffff) + 1;
  7252. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7253. drm_mode_set_name(mode);
  7254. return mode;
  7255. }
  7256. static void intel_increase_pllclock(struct drm_crtc *crtc)
  7257. {
  7258. struct drm_device *dev = crtc->dev;
  7259. struct drm_i915_private *dev_priv = dev->dev_private;
  7260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7261. int pipe = intel_crtc->pipe;
  7262. int dpll_reg = DPLL(pipe);
  7263. int dpll;
  7264. if (HAS_PCH_SPLIT(dev))
  7265. return;
  7266. if (!dev_priv->lvds_downclock_avail)
  7267. return;
  7268. dpll = I915_READ(dpll_reg);
  7269. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  7270. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  7271. assert_panel_unlocked(dev_priv, pipe);
  7272. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  7273. I915_WRITE(dpll_reg, dpll);
  7274. intel_wait_for_vblank(dev, pipe);
  7275. dpll = I915_READ(dpll_reg);
  7276. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  7277. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  7278. }
  7279. }
  7280. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7281. {
  7282. struct drm_device *dev = crtc->dev;
  7283. struct drm_i915_private *dev_priv = dev->dev_private;
  7284. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7285. if (HAS_PCH_SPLIT(dev))
  7286. return;
  7287. if (!dev_priv->lvds_downclock_avail)
  7288. return;
  7289. /*
  7290. * Since this is called by a timer, we should never get here in
  7291. * the manual case.
  7292. */
  7293. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7294. int pipe = intel_crtc->pipe;
  7295. int dpll_reg = DPLL(pipe);
  7296. int dpll;
  7297. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7298. assert_panel_unlocked(dev_priv, pipe);
  7299. dpll = I915_READ(dpll_reg);
  7300. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7301. I915_WRITE(dpll_reg, dpll);
  7302. intel_wait_for_vblank(dev, pipe);
  7303. dpll = I915_READ(dpll_reg);
  7304. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7305. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7306. }
  7307. }
  7308. void intel_mark_busy(struct drm_device *dev)
  7309. {
  7310. struct drm_i915_private *dev_priv = dev->dev_private;
  7311. if (dev_priv->mm.busy)
  7312. return;
  7313. intel_runtime_pm_get(dev_priv);
  7314. i915_update_gfx_val(dev_priv);
  7315. dev_priv->mm.busy = true;
  7316. }
  7317. void intel_mark_idle(struct drm_device *dev)
  7318. {
  7319. struct drm_i915_private *dev_priv = dev->dev_private;
  7320. struct drm_crtc *crtc;
  7321. if (!dev_priv->mm.busy)
  7322. return;
  7323. dev_priv->mm.busy = false;
  7324. if (!i915.powersave)
  7325. goto out;
  7326. for_each_crtc(dev, crtc) {
  7327. if (!crtc->primary->fb)
  7328. continue;
  7329. intel_decrease_pllclock(crtc);
  7330. }
  7331. if (INTEL_INFO(dev)->gen >= 6)
  7332. gen6_rps_idle(dev->dev_private);
  7333. out:
  7334. intel_runtime_pm_put(dev_priv);
  7335. }
  7336. void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
  7337. struct intel_engine_cs *ring)
  7338. {
  7339. struct drm_device *dev = obj->base.dev;
  7340. struct drm_crtc *crtc;
  7341. if (!i915.powersave)
  7342. return;
  7343. for_each_crtc(dev, crtc) {
  7344. if (!crtc->primary->fb)
  7345. continue;
  7346. if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
  7347. continue;
  7348. intel_increase_pllclock(crtc);
  7349. if (ring && intel_fbc_enabled(dev))
  7350. ring->fbc_dirty = true;
  7351. }
  7352. }
  7353. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7354. {
  7355. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7356. struct drm_device *dev = crtc->dev;
  7357. struct intel_unpin_work *work;
  7358. unsigned long flags;
  7359. spin_lock_irqsave(&dev->event_lock, flags);
  7360. work = intel_crtc->unpin_work;
  7361. intel_crtc->unpin_work = NULL;
  7362. spin_unlock_irqrestore(&dev->event_lock, flags);
  7363. if (work) {
  7364. cancel_work_sync(&work->work);
  7365. kfree(work);
  7366. }
  7367. intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
  7368. drm_crtc_cleanup(crtc);
  7369. kfree(intel_crtc);
  7370. }
  7371. static void intel_unpin_work_fn(struct work_struct *__work)
  7372. {
  7373. struct intel_unpin_work *work =
  7374. container_of(__work, struct intel_unpin_work, work);
  7375. struct drm_device *dev = work->crtc->dev;
  7376. mutex_lock(&dev->struct_mutex);
  7377. intel_unpin_fb_obj(work->old_fb_obj);
  7378. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7379. drm_gem_object_unreference(&work->old_fb_obj->base);
  7380. intel_update_fbc(dev);
  7381. mutex_unlock(&dev->struct_mutex);
  7382. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7383. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7384. kfree(work);
  7385. }
  7386. static void do_intel_finish_page_flip(struct drm_device *dev,
  7387. struct drm_crtc *crtc)
  7388. {
  7389. struct drm_i915_private *dev_priv = dev->dev_private;
  7390. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7391. struct intel_unpin_work *work;
  7392. unsigned long flags;
  7393. /* Ignore early vblank irqs */
  7394. if (intel_crtc == NULL)
  7395. return;
  7396. spin_lock_irqsave(&dev->event_lock, flags);
  7397. work = intel_crtc->unpin_work;
  7398. /* Ensure we don't miss a work->pending update ... */
  7399. smp_rmb();
  7400. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7401. spin_unlock_irqrestore(&dev->event_lock, flags);
  7402. return;
  7403. }
  7404. /* and that the unpin work is consistent wrt ->pending. */
  7405. smp_rmb();
  7406. intel_crtc->unpin_work = NULL;
  7407. if (work->event)
  7408. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  7409. drm_crtc_vblank_put(crtc);
  7410. spin_unlock_irqrestore(&dev->event_lock, flags);
  7411. wake_up_all(&dev_priv->pending_flip_queue);
  7412. queue_work(dev_priv->wq, &work->work);
  7413. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  7414. }
  7415. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7416. {
  7417. struct drm_i915_private *dev_priv = dev->dev_private;
  7418. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7419. do_intel_finish_page_flip(dev, crtc);
  7420. }
  7421. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7422. {
  7423. struct drm_i915_private *dev_priv = dev->dev_private;
  7424. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7425. do_intel_finish_page_flip(dev, crtc);
  7426. }
  7427. /* Is 'a' after or equal to 'b'? */
  7428. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7429. {
  7430. return !((a - b) & 0x80000000);
  7431. }
  7432. static bool page_flip_finished(struct intel_crtc *crtc)
  7433. {
  7434. struct drm_device *dev = crtc->base.dev;
  7435. struct drm_i915_private *dev_priv = dev->dev_private;
  7436. /*
  7437. * The relevant registers doen't exist on pre-ctg.
  7438. * As the flip done interrupt doesn't trigger for mmio
  7439. * flips on gmch platforms, a flip count check isn't
  7440. * really needed there. But since ctg has the registers,
  7441. * include it in the check anyway.
  7442. */
  7443. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7444. return true;
  7445. /*
  7446. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7447. * used the same base address. In that case the mmio flip might
  7448. * have completed, but the CS hasn't even executed the flip yet.
  7449. *
  7450. * A flip count check isn't enough as the CS might have updated
  7451. * the base address just after start of vblank, but before we
  7452. * managed to process the interrupt. This means we'd complete the
  7453. * CS flip too soon.
  7454. *
  7455. * Combining both checks should get us a good enough result. It may
  7456. * still happen that the CS flip has been executed, but has not
  7457. * yet actually completed. But in case the base address is the same
  7458. * anyway, we don't really care.
  7459. */
  7460. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7461. crtc->unpin_work->gtt_offset &&
  7462. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7463. crtc->unpin_work->flip_count);
  7464. }
  7465. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7466. {
  7467. struct drm_i915_private *dev_priv = dev->dev_private;
  7468. struct intel_crtc *intel_crtc =
  7469. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7470. unsigned long flags;
  7471. /* NB: An MMIO update of the plane base pointer will also
  7472. * generate a page-flip completion irq, i.e. every modeset
  7473. * is also accompanied by a spurious intel_prepare_page_flip().
  7474. */
  7475. spin_lock_irqsave(&dev->event_lock, flags);
  7476. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7477. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7478. spin_unlock_irqrestore(&dev->event_lock, flags);
  7479. }
  7480. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7481. {
  7482. /* Ensure that the work item is consistent when activating it ... */
  7483. smp_wmb();
  7484. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7485. /* and that it is marked active as soon as the irq could fire. */
  7486. smp_wmb();
  7487. }
  7488. static int intel_gen2_queue_flip(struct drm_device *dev,
  7489. struct drm_crtc *crtc,
  7490. struct drm_framebuffer *fb,
  7491. struct drm_i915_gem_object *obj,
  7492. struct intel_engine_cs *ring,
  7493. uint32_t flags)
  7494. {
  7495. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7496. u32 flip_mask;
  7497. int ret;
  7498. ret = intel_ring_begin(ring, 6);
  7499. if (ret)
  7500. return ret;
  7501. /* Can't queue multiple flips, so wait for the previous
  7502. * one to finish before executing the next.
  7503. */
  7504. if (intel_crtc->plane)
  7505. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7506. else
  7507. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7508. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7509. intel_ring_emit(ring, MI_NOOP);
  7510. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7511. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7512. intel_ring_emit(ring, fb->pitches[0]);
  7513. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7514. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7515. intel_mark_page_flip_active(intel_crtc);
  7516. __intel_ring_advance(ring);
  7517. return 0;
  7518. }
  7519. static int intel_gen3_queue_flip(struct drm_device *dev,
  7520. struct drm_crtc *crtc,
  7521. struct drm_framebuffer *fb,
  7522. struct drm_i915_gem_object *obj,
  7523. struct intel_engine_cs *ring,
  7524. uint32_t flags)
  7525. {
  7526. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7527. u32 flip_mask;
  7528. int ret;
  7529. ret = intel_ring_begin(ring, 6);
  7530. if (ret)
  7531. return ret;
  7532. if (intel_crtc->plane)
  7533. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7534. else
  7535. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7536. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7537. intel_ring_emit(ring, MI_NOOP);
  7538. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7539. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7540. intel_ring_emit(ring, fb->pitches[0]);
  7541. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7542. intel_ring_emit(ring, MI_NOOP);
  7543. intel_mark_page_flip_active(intel_crtc);
  7544. __intel_ring_advance(ring);
  7545. return 0;
  7546. }
  7547. static int intel_gen4_queue_flip(struct drm_device *dev,
  7548. struct drm_crtc *crtc,
  7549. struct drm_framebuffer *fb,
  7550. struct drm_i915_gem_object *obj,
  7551. struct intel_engine_cs *ring,
  7552. uint32_t flags)
  7553. {
  7554. struct drm_i915_private *dev_priv = dev->dev_private;
  7555. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7556. uint32_t pf, pipesrc;
  7557. int ret;
  7558. ret = intel_ring_begin(ring, 4);
  7559. if (ret)
  7560. return ret;
  7561. /* i965+ uses the linear or tiled offsets from the
  7562. * Display Registers (which do not change across a page-flip)
  7563. * so we need only reprogram the base address.
  7564. */
  7565. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7566. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7567. intel_ring_emit(ring, fb->pitches[0]);
  7568. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7569. obj->tiling_mode);
  7570. /* XXX Enabling the panel-fitter across page-flip is so far
  7571. * untested on non-native modes, so ignore it for now.
  7572. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7573. */
  7574. pf = 0;
  7575. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7576. intel_ring_emit(ring, pf | pipesrc);
  7577. intel_mark_page_flip_active(intel_crtc);
  7578. __intel_ring_advance(ring);
  7579. return 0;
  7580. }
  7581. static int intel_gen6_queue_flip(struct drm_device *dev,
  7582. struct drm_crtc *crtc,
  7583. struct drm_framebuffer *fb,
  7584. struct drm_i915_gem_object *obj,
  7585. struct intel_engine_cs *ring,
  7586. uint32_t flags)
  7587. {
  7588. struct drm_i915_private *dev_priv = dev->dev_private;
  7589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7590. uint32_t pf, pipesrc;
  7591. int ret;
  7592. ret = intel_ring_begin(ring, 4);
  7593. if (ret)
  7594. return ret;
  7595. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7596. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7597. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7598. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7599. /* Contrary to the suggestions in the documentation,
  7600. * "Enable Panel Fitter" does not seem to be required when page
  7601. * flipping with a non-native mode, and worse causes a normal
  7602. * modeset to fail.
  7603. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7604. */
  7605. pf = 0;
  7606. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7607. intel_ring_emit(ring, pf | pipesrc);
  7608. intel_mark_page_flip_active(intel_crtc);
  7609. __intel_ring_advance(ring);
  7610. return 0;
  7611. }
  7612. static int intel_gen7_queue_flip(struct drm_device *dev,
  7613. struct drm_crtc *crtc,
  7614. struct drm_framebuffer *fb,
  7615. struct drm_i915_gem_object *obj,
  7616. struct intel_engine_cs *ring,
  7617. uint32_t flags)
  7618. {
  7619. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7620. uint32_t plane_bit = 0;
  7621. int len, ret;
  7622. switch (intel_crtc->plane) {
  7623. case PLANE_A:
  7624. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7625. break;
  7626. case PLANE_B:
  7627. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7628. break;
  7629. case PLANE_C:
  7630. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7631. break;
  7632. default:
  7633. WARN_ONCE(1, "unknown plane in flip command\n");
  7634. return -ENODEV;
  7635. }
  7636. len = 4;
  7637. if (ring->id == RCS) {
  7638. len += 6;
  7639. /*
  7640. * On Gen 8, SRM is now taking an extra dword to accommodate
  7641. * 48bits addresses, and we need a NOOP for the batch size to
  7642. * stay even.
  7643. */
  7644. if (IS_GEN8(dev))
  7645. len += 2;
  7646. }
  7647. /*
  7648. * BSpec MI_DISPLAY_FLIP for IVB:
  7649. * "The full packet must be contained within the same cache line."
  7650. *
  7651. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7652. * cacheline, if we ever start emitting more commands before
  7653. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7654. * then do the cacheline alignment, and finally emit the
  7655. * MI_DISPLAY_FLIP.
  7656. */
  7657. ret = intel_ring_cacheline_align(ring);
  7658. if (ret)
  7659. return ret;
  7660. ret = intel_ring_begin(ring, len);
  7661. if (ret)
  7662. return ret;
  7663. /* Unmask the flip-done completion message. Note that the bspec says that
  7664. * we should do this for both the BCS and RCS, and that we must not unmask
  7665. * more than one flip event at any time (or ensure that one flip message
  7666. * can be sent by waiting for flip-done prior to queueing new flips).
  7667. * Experimentation says that BCS works despite DERRMR masking all
  7668. * flip-done completion events and that unmasking all planes at once
  7669. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7670. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7671. */
  7672. if (ring->id == RCS) {
  7673. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7674. intel_ring_emit(ring, DERRMR);
  7675. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7676. DERRMR_PIPEB_PRI_FLIP_DONE |
  7677. DERRMR_PIPEC_PRI_FLIP_DONE));
  7678. if (IS_GEN8(dev))
  7679. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7680. MI_SRM_LRM_GLOBAL_GTT);
  7681. else
  7682. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7683. MI_SRM_LRM_GLOBAL_GTT);
  7684. intel_ring_emit(ring, DERRMR);
  7685. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7686. if (IS_GEN8(dev)) {
  7687. intel_ring_emit(ring, 0);
  7688. intel_ring_emit(ring, MI_NOOP);
  7689. }
  7690. }
  7691. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7692. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7693. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7694. intel_ring_emit(ring, (MI_NOOP));
  7695. intel_mark_page_flip_active(intel_crtc);
  7696. __intel_ring_advance(ring);
  7697. return 0;
  7698. }
  7699. static int intel_default_queue_flip(struct drm_device *dev,
  7700. struct drm_crtc *crtc,
  7701. struct drm_framebuffer *fb,
  7702. struct drm_i915_gem_object *obj,
  7703. struct intel_engine_cs *ring,
  7704. uint32_t flags)
  7705. {
  7706. return -ENODEV;
  7707. }
  7708. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  7709. struct drm_framebuffer *fb,
  7710. struct drm_pending_vblank_event *event,
  7711. uint32_t page_flip_flags)
  7712. {
  7713. struct drm_device *dev = crtc->dev;
  7714. struct drm_i915_private *dev_priv = dev->dev_private;
  7715. struct drm_framebuffer *old_fb = crtc->primary->fb;
  7716. struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
  7717. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7718. struct intel_unpin_work *work;
  7719. struct intel_engine_cs *ring;
  7720. unsigned long flags;
  7721. int ret;
  7722. /* Can't change pixel format via MI display flips. */
  7723. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  7724. return -EINVAL;
  7725. /*
  7726. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  7727. * Note that pitch changes could also affect these register.
  7728. */
  7729. if (INTEL_INFO(dev)->gen > 3 &&
  7730. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  7731. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  7732. return -EINVAL;
  7733. if (i915_terminally_wedged(&dev_priv->gpu_error))
  7734. goto out_hang;
  7735. work = kzalloc(sizeof(*work), GFP_KERNEL);
  7736. if (work == NULL)
  7737. return -ENOMEM;
  7738. work->event = event;
  7739. work->crtc = crtc;
  7740. work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
  7741. INIT_WORK(&work->work, intel_unpin_work_fn);
  7742. ret = drm_crtc_vblank_get(crtc);
  7743. if (ret)
  7744. goto free_work;
  7745. /* We borrow the event spin lock for protecting unpin_work */
  7746. spin_lock_irqsave(&dev->event_lock, flags);
  7747. if (intel_crtc->unpin_work) {
  7748. spin_unlock_irqrestore(&dev->event_lock, flags);
  7749. kfree(work);
  7750. drm_crtc_vblank_put(crtc);
  7751. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  7752. return -EBUSY;
  7753. }
  7754. intel_crtc->unpin_work = work;
  7755. spin_unlock_irqrestore(&dev->event_lock, flags);
  7756. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  7757. flush_workqueue(dev_priv->wq);
  7758. ret = i915_mutex_lock_interruptible(dev);
  7759. if (ret)
  7760. goto cleanup;
  7761. /* Reference the objects for the scheduled work. */
  7762. drm_gem_object_reference(&work->old_fb_obj->base);
  7763. drm_gem_object_reference(&obj->base);
  7764. crtc->primary->fb = fb;
  7765. work->pending_flip_obj = obj;
  7766. work->enable_stall_check = true;
  7767. atomic_inc(&intel_crtc->unpin_work_count);
  7768. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  7769. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  7770. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(intel_crtc->pipe)) + 1;
  7771. if (IS_VALLEYVIEW(dev)) {
  7772. ring = &dev_priv->ring[BCS];
  7773. } else if (INTEL_INFO(dev)->gen >= 7) {
  7774. ring = obj->ring;
  7775. if (ring == NULL || ring->id != RCS)
  7776. ring = &dev_priv->ring[BCS];
  7777. } else {
  7778. ring = &dev_priv->ring[RCS];
  7779. }
  7780. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  7781. if (ret)
  7782. goto cleanup_pending;
  7783. work->gtt_offset =
  7784. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  7785. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring, page_flip_flags);
  7786. if (ret)
  7787. goto cleanup_unpin;
  7788. intel_disable_fbc(dev);
  7789. intel_mark_fb_busy(obj, NULL);
  7790. mutex_unlock(&dev->struct_mutex);
  7791. trace_i915_flip_request(intel_crtc->plane, obj);
  7792. return 0;
  7793. cleanup_unpin:
  7794. intel_unpin_fb_obj(obj);
  7795. cleanup_pending:
  7796. atomic_dec(&intel_crtc->unpin_work_count);
  7797. crtc->primary->fb = old_fb;
  7798. drm_gem_object_unreference(&work->old_fb_obj->base);
  7799. drm_gem_object_unreference(&obj->base);
  7800. mutex_unlock(&dev->struct_mutex);
  7801. cleanup:
  7802. spin_lock_irqsave(&dev->event_lock, flags);
  7803. intel_crtc->unpin_work = NULL;
  7804. spin_unlock_irqrestore(&dev->event_lock, flags);
  7805. drm_crtc_vblank_put(crtc);
  7806. free_work:
  7807. kfree(work);
  7808. if (ret == -EIO) {
  7809. out_hang:
  7810. intel_crtc_wait_for_pending_flips(crtc);
  7811. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  7812. if (ret == 0 && event)
  7813. drm_send_vblank_event(dev, intel_crtc->pipe, event);
  7814. }
  7815. return ret;
  7816. }
  7817. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  7818. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  7819. .load_lut = intel_crtc_load_lut,
  7820. };
  7821. /**
  7822. * intel_modeset_update_staged_output_state
  7823. *
  7824. * Updates the staged output configuration state, e.g. after we've read out the
  7825. * current hw state.
  7826. */
  7827. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  7828. {
  7829. struct intel_crtc *crtc;
  7830. struct intel_encoder *encoder;
  7831. struct intel_connector *connector;
  7832. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7833. base.head) {
  7834. connector->new_encoder =
  7835. to_intel_encoder(connector->base.encoder);
  7836. }
  7837. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7838. base.head) {
  7839. encoder->new_crtc =
  7840. to_intel_crtc(encoder->base.crtc);
  7841. }
  7842. for_each_intel_crtc(dev, crtc) {
  7843. crtc->new_enabled = crtc->base.enabled;
  7844. if (crtc->new_enabled)
  7845. crtc->new_config = &crtc->config;
  7846. else
  7847. crtc->new_config = NULL;
  7848. }
  7849. }
  7850. /**
  7851. * intel_modeset_commit_output_state
  7852. *
  7853. * This function copies the stage display pipe configuration to the real one.
  7854. */
  7855. static void intel_modeset_commit_output_state(struct drm_device *dev)
  7856. {
  7857. struct intel_crtc *crtc;
  7858. struct intel_encoder *encoder;
  7859. struct intel_connector *connector;
  7860. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7861. base.head) {
  7862. connector->base.encoder = &connector->new_encoder->base;
  7863. }
  7864. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7865. base.head) {
  7866. encoder->base.crtc = &encoder->new_crtc->base;
  7867. }
  7868. for_each_intel_crtc(dev, crtc) {
  7869. crtc->base.enabled = crtc->new_enabled;
  7870. }
  7871. }
  7872. static void
  7873. connected_sink_compute_bpp(struct intel_connector *connector,
  7874. struct intel_crtc_config *pipe_config)
  7875. {
  7876. int bpp = pipe_config->pipe_bpp;
  7877. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  7878. connector->base.base.id,
  7879. connector->base.name);
  7880. /* Don't use an invalid EDID bpc value */
  7881. if (connector->base.display_info.bpc &&
  7882. connector->base.display_info.bpc * 3 < bpp) {
  7883. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  7884. bpp, connector->base.display_info.bpc*3);
  7885. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  7886. }
  7887. /* Clamp bpp to 8 on screens without EDID 1.4 */
  7888. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  7889. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  7890. bpp);
  7891. pipe_config->pipe_bpp = 24;
  7892. }
  7893. }
  7894. static int
  7895. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  7896. struct drm_framebuffer *fb,
  7897. struct intel_crtc_config *pipe_config)
  7898. {
  7899. struct drm_device *dev = crtc->base.dev;
  7900. struct intel_connector *connector;
  7901. int bpp;
  7902. switch (fb->pixel_format) {
  7903. case DRM_FORMAT_C8:
  7904. bpp = 8*3; /* since we go through a colormap */
  7905. break;
  7906. case DRM_FORMAT_XRGB1555:
  7907. case DRM_FORMAT_ARGB1555:
  7908. /* checked in intel_framebuffer_init already */
  7909. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  7910. return -EINVAL;
  7911. case DRM_FORMAT_RGB565:
  7912. bpp = 6*3; /* min is 18bpp */
  7913. break;
  7914. case DRM_FORMAT_XBGR8888:
  7915. case DRM_FORMAT_ABGR8888:
  7916. /* checked in intel_framebuffer_init already */
  7917. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7918. return -EINVAL;
  7919. case DRM_FORMAT_XRGB8888:
  7920. case DRM_FORMAT_ARGB8888:
  7921. bpp = 8*3;
  7922. break;
  7923. case DRM_FORMAT_XRGB2101010:
  7924. case DRM_FORMAT_ARGB2101010:
  7925. case DRM_FORMAT_XBGR2101010:
  7926. case DRM_FORMAT_ABGR2101010:
  7927. /* checked in intel_framebuffer_init already */
  7928. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7929. return -EINVAL;
  7930. bpp = 10*3;
  7931. break;
  7932. /* TODO: gen4+ supports 16 bpc floating point, too. */
  7933. default:
  7934. DRM_DEBUG_KMS("unsupported depth\n");
  7935. return -EINVAL;
  7936. }
  7937. pipe_config->pipe_bpp = bpp;
  7938. /* Clamp display bpp to EDID value */
  7939. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7940. base.head) {
  7941. if (!connector->new_encoder ||
  7942. connector->new_encoder->new_crtc != crtc)
  7943. continue;
  7944. connected_sink_compute_bpp(connector, pipe_config);
  7945. }
  7946. return bpp;
  7947. }
  7948. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  7949. {
  7950. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  7951. "type: 0x%x flags: 0x%x\n",
  7952. mode->crtc_clock,
  7953. mode->crtc_hdisplay, mode->crtc_hsync_start,
  7954. mode->crtc_hsync_end, mode->crtc_htotal,
  7955. mode->crtc_vdisplay, mode->crtc_vsync_start,
  7956. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  7957. }
  7958. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  7959. struct intel_crtc_config *pipe_config,
  7960. const char *context)
  7961. {
  7962. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  7963. context, pipe_name(crtc->pipe));
  7964. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  7965. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  7966. pipe_config->pipe_bpp, pipe_config->dither);
  7967. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7968. pipe_config->has_pch_encoder,
  7969. pipe_config->fdi_lanes,
  7970. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  7971. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  7972. pipe_config->fdi_m_n.tu);
  7973. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7974. pipe_config->has_dp_encoder,
  7975. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  7976. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  7977. pipe_config->dp_m_n.tu);
  7978. DRM_DEBUG_KMS("requested mode:\n");
  7979. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  7980. DRM_DEBUG_KMS("adjusted mode:\n");
  7981. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  7982. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  7983. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  7984. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  7985. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  7986. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  7987. pipe_config->gmch_pfit.control,
  7988. pipe_config->gmch_pfit.pgm_ratios,
  7989. pipe_config->gmch_pfit.lvds_border_bits);
  7990. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  7991. pipe_config->pch_pfit.pos,
  7992. pipe_config->pch_pfit.size,
  7993. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  7994. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  7995. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  7996. }
  7997. static bool encoders_cloneable(const struct intel_encoder *a,
  7998. const struct intel_encoder *b)
  7999. {
  8000. /* masks could be asymmetric, so check both ways */
  8001. return a == b || (a->cloneable & (1 << b->type) &&
  8002. b->cloneable & (1 << a->type));
  8003. }
  8004. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8005. struct intel_encoder *encoder)
  8006. {
  8007. struct drm_device *dev = crtc->base.dev;
  8008. struct intel_encoder *source_encoder;
  8009. list_for_each_entry(source_encoder,
  8010. &dev->mode_config.encoder_list, base.head) {
  8011. if (source_encoder->new_crtc != crtc)
  8012. continue;
  8013. if (!encoders_cloneable(encoder, source_encoder))
  8014. return false;
  8015. }
  8016. return true;
  8017. }
  8018. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8019. {
  8020. struct drm_device *dev = crtc->base.dev;
  8021. struct intel_encoder *encoder;
  8022. list_for_each_entry(encoder,
  8023. &dev->mode_config.encoder_list, base.head) {
  8024. if (encoder->new_crtc != crtc)
  8025. continue;
  8026. if (!check_single_encoder_cloning(crtc, encoder))
  8027. return false;
  8028. }
  8029. return true;
  8030. }
  8031. static struct intel_crtc_config *
  8032. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8033. struct drm_framebuffer *fb,
  8034. struct drm_display_mode *mode)
  8035. {
  8036. struct drm_device *dev = crtc->dev;
  8037. struct intel_encoder *encoder;
  8038. struct intel_crtc_config *pipe_config;
  8039. int plane_bpp, ret = -EINVAL;
  8040. bool retry = true;
  8041. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8042. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8043. return ERR_PTR(-EINVAL);
  8044. }
  8045. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8046. if (!pipe_config)
  8047. return ERR_PTR(-ENOMEM);
  8048. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8049. drm_mode_copy(&pipe_config->requested_mode, mode);
  8050. pipe_config->cpu_transcoder =
  8051. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8052. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8053. /*
  8054. * Sanitize sync polarity flags based on requested ones. If neither
  8055. * positive or negative polarity is requested, treat this as meaning
  8056. * negative polarity.
  8057. */
  8058. if (!(pipe_config->adjusted_mode.flags &
  8059. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8060. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8061. if (!(pipe_config->adjusted_mode.flags &
  8062. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8063. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8064. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8065. * plane pixel format and any sink constraints into account. Returns the
  8066. * source plane bpp so that dithering can be selected on mismatches
  8067. * after encoders and crtc also have had their say. */
  8068. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8069. fb, pipe_config);
  8070. if (plane_bpp < 0)
  8071. goto fail;
  8072. /*
  8073. * Determine the real pipe dimensions. Note that stereo modes can
  8074. * increase the actual pipe size due to the frame doubling and
  8075. * insertion of additional space for blanks between the frame. This
  8076. * is stored in the crtc timings. We use the requested mode to do this
  8077. * computation to clearly distinguish it from the adjusted mode, which
  8078. * can be changed by the connectors in the below retry loop.
  8079. */
  8080. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8081. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8082. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8083. encoder_retry:
  8084. /* Ensure the port clock defaults are reset when retrying. */
  8085. pipe_config->port_clock = 0;
  8086. pipe_config->pixel_multiplier = 1;
  8087. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8088. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8089. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8090. * adjust it according to limitations or connector properties, and also
  8091. * a chance to reject the mode entirely.
  8092. */
  8093. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8094. base.head) {
  8095. if (&encoder->new_crtc->base != crtc)
  8096. continue;
  8097. if (!(encoder->compute_config(encoder, pipe_config))) {
  8098. DRM_DEBUG_KMS("Encoder config failure\n");
  8099. goto fail;
  8100. }
  8101. }
  8102. /* Set default port clock if not overwritten by the encoder. Needs to be
  8103. * done afterwards in case the encoder adjusts the mode. */
  8104. if (!pipe_config->port_clock)
  8105. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8106. * pipe_config->pixel_multiplier;
  8107. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8108. if (ret < 0) {
  8109. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8110. goto fail;
  8111. }
  8112. if (ret == RETRY) {
  8113. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8114. ret = -EINVAL;
  8115. goto fail;
  8116. }
  8117. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8118. retry = false;
  8119. goto encoder_retry;
  8120. }
  8121. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8122. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8123. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8124. return pipe_config;
  8125. fail:
  8126. kfree(pipe_config);
  8127. return ERR_PTR(ret);
  8128. }
  8129. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8130. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8131. static void
  8132. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8133. unsigned *prepare_pipes, unsigned *disable_pipes)
  8134. {
  8135. struct intel_crtc *intel_crtc;
  8136. struct drm_device *dev = crtc->dev;
  8137. struct intel_encoder *encoder;
  8138. struct intel_connector *connector;
  8139. struct drm_crtc *tmp_crtc;
  8140. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8141. /* Check which crtcs have changed outputs connected to them, these need
  8142. * to be part of the prepare_pipes mask. We don't (yet) support global
  8143. * modeset across multiple crtcs, so modeset_pipes will only have one
  8144. * bit set at most. */
  8145. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8146. base.head) {
  8147. if (connector->base.encoder == &connector->new_encoder->base)
  8148. continue;
  8149. if (connector->base.encoder) {
  8150. tmp_crtc = connector->base.encoder->crtc;
  8151. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8152. }
  8153. if (connector->new_encoder)
  8154. *prepare_pipes |=
  8155. 1 << connector->new_encoder->new_crtc->pipe;
  8156. }
  8157. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8158. base.head) {
  8159. if (encoder->base.crtc == &encoder->new_crtc->base)
  8160. continue;
  8161. if (encoder->base.crtc) {
  8162. tmp_crtc = encoder->base.crtc;
  8163. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8164. }
  8165. if (encoder->new_crtc)
  8166. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8167. }
  8168. /* Check for pipes that will be enabled/disabled ... */
  8169. for_each_intel_crtc(dev, intel_crtc) {
  8170. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8171. continue;
  8172. if (!intel_crtc->new_enabled)
  8173. *disable_pipes |= 1 << intel_crtc->pipe;
  8174. else
  8175. *prepare_pipes |= 1 << intel_crtc->pipe;
  8176. }
  8177. /* set_mode is also used to update properties on life display pipes. */
  8178. intel_crtc = to_intel_crtc(crtc);
  8179. if (intel_crtc->new_enabled)
  8180. *prepare_pipes |= 1 << intel_crtc->pipe;
  8181. /*
  8182. * For simplicity do a full modeset on any pipe where the output routing
  8183. * changed. We could be more clever, but that would require us to be
  8184. * more careful with calling the relevant encoder->mode_set functions.
  8185. */
  8186. if (*prepare_pipes)
  8187. *modeset_pipes = *prepare_pipes;
  8188. /* ... and mask these out. */
  8189. *modeset_pipes &= ~(*disable_pipes);
  8190. *prepare_pipes &= ~(*disable_pipes);
  8191. /*
  8192. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8193. * obies this rule, but the modeset restore mode of
  8194. * intel_modeset_setup_hw_state does not.
  8195. */
  8196. *modeset_pipes &= 1 << intel_crtc->pipe;
  8197. *prepare_pipes &= 1 << intel_crtc->pipe;
  8198. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8199. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8200. }
  8201. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8202. {
  8203. struct drm_encoder *encoder;
  8204. struct drm_device *dev = crtc->dev;
  8205. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8206. if (encoder->crtc == crtc)
  8207. return true;
  8208. return false;
  8209. }
  8210. static void
  8211. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8212. {
  8213. struct intel_encoder *intel_encoder;
  8214. struct intel_crtc *intel_crtc;
  8215. struct drm_connector *connector;
  8216. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  8217. base.head) {
  8218. if (!intel_encoder->base.crtc)
  8219. continue;
  8220. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8221. if (prepare_pipes & (1 << intel_crtc->pipe))
  8222. intel_encoder->connectors_active = false;
  8223. }
  8224. intel_modeset_commit_output_state(dev);
  8225. /* Double check state. */
  8226. for_each_intel_crtc(dev, intel_crtc) {
  8227. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8228. WARN_ON(intel_crtc->new_config &&
  8229. intel_crtc->new_config != &intel_crtc->config);
  8230. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8231. }
  8232. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8233. if (!connector->encoder || !connector->encoder->crtc)
  8234. continue;
  8235. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8236. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8237. struct drm_property *dpms_property =
  8238. dev->mode_config.dpms_property;
  8239. connector->dpms = DRM_MODE_DPMS_ON;
  8240. drm_object_property_set_value(&connector->base,
  8241. dpms_property,
  8242. DRM_MODE_DPMS_ON);
  8243. intel_encoder = to_intel_encoder(connector->encoder);
  8244. intel_encoder->connectors_active = true;
  8245. }
  8246. }
  8247. }
  8248. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8249. {
  8250. int diff;
  8251. if (clock1 == clock2)
  8252. return true;
  8253. if (!clock1 || !clock2)
  8254. return false;
  8255. diff = abs(clock1 - clock2);
  8256. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8257. return true;
  8258. return false;
  8259. }
  8260. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8261. list_for_each_entry((intel_crtc), \
  8262. &(dev)->mode_config.crtc_list, \
  8263. base.head) \
  8264. if (mask & (1 <<(intel_crtc)->pipe))
  8265. static bool
  8266. intel_pipe_config_compare(struct drm_device *dev,
  8267. struct intel_crtc_config *current_config,
  8268. struct intel_crtc_config *pipe_config)
  8269. {
  8270. #define PIPE_CONF_CHECK_X(name) \
  8271. if (current_config->name != pipe_config->name) { \
  8272. DRM_ERROR("mismatch in " #name " " \
  8273. "(expected 0x%08x, found 0x%08x)\n", \
  8274. current_config->name, \
  8275. pipe_config->name); \
  8276. return false; \
  8277. }
  8278. #define PIPE_CONF_CHECK_I(name) \
  8279. if (current_config->name != pipe_config->name) { \
  8280. DRM_ERROR("mismatch in " #name " " \
  8281. "(expected %i, found %i)\n", \
  8282. current_config->name, \
  8283. pipe_config->name); \
  8284. return false; \
  8285. }
  8286. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8287. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8288. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8289. "(expected %i, found %i)\n", \
  8290. current_config->name & (mask), \
  8291. pipe_config->name & (mask)); \
  8292. return false; \
  8293. }
  8294. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8295. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8296. DRM_ERROR("mismatch in " #name " " \
  8297. "(expected %i, found %i)\n", \
  8298. current_config->name, \
  8299. pipe_config->name); \
  8300. return false; \
  8301. }
  8302. #define PIPE_CONF_QUIRK(quirk) \
  8303. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8304. PIPE_CONF_CHECK_I(cpu_transcoder);
  8305. PIPE_CONF_CHECK_I(has_pch_encoder);
  8306. PIPE_CONF_CHECK_I(fdi_lanes);
  8307. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8308. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8309. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8310. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8311. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8312. PIPE_CONF_CHECK_I(has_dp_encoder);
  8313. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8314. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8315. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8316. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8317. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8318. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8319. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8320. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8321. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8322. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8323. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8324. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8325. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8326. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8327. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8328. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8329. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8330. PIPE_CONF_CHECK_I(pixel_multiplier);
  8331. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8332. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8333. IS_VALLEYVIEW(dev))
  8334. PIPE_CONF_CHECK_I(limited_color_range);
  8335. PIPE_CONF_CHECK_I(has_audio);
  8336. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8337. DRM_MODE_FLAG_INTERLACE);
  8338. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8339. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8340. DRM_MODE_FLAG_PHSYNC);
  8341. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8342. DRM_MODE_FLAG_NHSYNC);
  8343. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8344. DRM_MODE_FLAG_PVSYNC);
  8345. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8346. DRM_MODE_FLAG_NVSYNC);
  8347. }
  8348. PIPE_CONF_CHECK_I(pipe_src_w);
  8349. PIPE_CONF_CHECK_I(pipe_src_h);
  8350. /*
  8351. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8352. * screen. Since we don't yet re-compute the pipe config when moving
  8353. * just the lvds port away to another pipe the sw tracking won't match.
  8354. *
  8355. * Proper atomic modesets with recomputed global state will fix this.
  8356. * Until then just don't check gmch state for inherited modes.
  8357. */
  8358. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8359. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8360. /* pfit ratios are autocomputed by the hw on gen4+ */
  8361. if (INTEL_INFO(dev)->gen < 4)
  8362. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8363. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8364. }
  8365. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8366. if (current_config->pch_pfit.enabled) {
  8367. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8368. PIPE_CONF_CHECK_I(pch_pfit.size);
  8369. }
  8370. /* BDW+ don't expose a synchronous way to read the state */
  8371. if (IS_HASWELL(dev))
  8372. PIPE_CONF_CHECK_I(ips_enabled);
  8373. PIPE_CONF_CHECK_I(double_wide);
  8374. PIPE_CONF_CHECK_I(shared_dpll);
  8375. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8376. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8377. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8378. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8379. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8380. PIPE_CONF_CHECK_I(pipe_bpp);
  8381. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8382. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8383. #undef PIPE_CONF_CHECK_X
  8384. #undef PIPE_CONF_CHECK_I
  8385. #undef PIPE_CONF_CHECK_FLAGS
  8386. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8387. #undef PIPE_CONF_QUIRK
  8388. return true;
  8389. }
  8390. static void
  8391. check_connector_state(struct drm_device *dev)
  8392. {
  8393. struct intel_connector *connector;
  8394. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8395. base.head) {
  8396. /* This also checks the encoder/connector hw state with the
  8397. * ->get_hw_state callbacks. */
  8398. intel_connector_check_state(connector);
  8399. WARN(&connector->new_encoder->base != connector->base.encoder,
  8400. "connector's staged encoder doesn't match current encoder\n");
  8401. }
  8402. }
  8403. static void
  8404. check_encoder_state(struct drm_device *dev)
  8405. {
  8406. struct intel_encoder *encoder;
  8407. struct intel_connector *connector;
  8408. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8409. base.head) {
  8410. bool enabled = false;
  8411. bool active = false;
  8412. enum pipe pipe, tracked_pipe;
  8413. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8414. encoder->base.base.id,
  8415. encoder->base.name);
  8416. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8417. "encoder's stage crtc doesn't match current crtc\n");
  8418. WARN(encoder->connectors_active && !encoder->base.crtc,
  8419. "encoder's active_connectors set, but no crtc\n");
  8420. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8421. base.head) {
  8422. if (connector->base.encoder != &encoder->base)
  8423. continue;
  8424. enabled = true;
  8425. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8426. active = true;
  8427. }
  8428. WARN(!!encoder->base.crtc != enabled,
  8429. "encoder's enabled state mismatch "
  8430. "(expected %i, found %i)\n",
  8431. !!encoder->base.crtc, enabled);
  8432. WARN(active && !encoder->base.crtc,
  8433. "active encoder with no crtc\n");
  8434. WARN(encoder->connectors_active != active,
  8435. "encoder's computed active state doesn't match tracked active state "
  8436. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8437. active = encoder->get_hw_state(encoder, &pipe);
  8438. WARN(active != encoder->connectors_active,
  8439. "encoder's hw state doesn't match sw tracking "
  8440. "(expected %i, found %i)\n",
  8441. encoder->connectors_active, active);
  8442. if (!encoder->base.crtc)
  8443. continue;
  8444. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8445. WARN(active && pipe != tracked_pipe,
  8446. "active encoder's pipe doesn't match"
  8447. "(expected %i, found %i)\n",
  8448. tracked_pipe, pipe);
  8449. }
  8450. }
  8451. static void
  8452. check_crtc_state(struct drm_device *dev)
  8453. {
  8454. struct drm_i915_private *dev_priv = dev->dev_private;
  8455. struct intel_crtc *crtc;
  8456. struct intel_encoder *encoder;
  8457. struct intel_crtc_config pipe_config;
  8458. for_each_intel_crtc(dev, crtc) {
  8459. bool enabled = false;
  8460. bool active = false;
  8461. memset(&pipe_config, 0, sizeof(pipe_config));
  8462. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8463. crtc->base.base.id);
  8464. WARN(crtc->active && !crtc->base.enabled,
  8465. "active crtc, but not enabled in sw tracking\n");
  8466. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8467. base.head) {
  8468. if (encoder->base.crtc != &crtc->base)
  8469. continue;
  8470. enabled = true;
  8471. if (encoder->connectors_active)
  8472. active = true;
  8473. }
  8474. WARN(active != crtc->active,
  8475. "crtc's computed active state doesn't match tracked active state "
  8476. "(expected %i, found %i)\n", active, crtc->active);
  8477. WARN(enabled != crtc->base.enabled,
  8478. "crtc's computed enabled state doesn't match tracked enabled state "
  8479. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8480. active = dev_priv->display.get_pipe_config(crtc,
  8481. &pipe_config);
  8482. /* hw state is inconsistent with the pipe A quirk */
  8483. if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  8484. active = crtc->active;
  8485. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8486. base.head) {
  8487. enum pipe pipe;
  8488. if (encoder->base.crtc != &crtc->base)
  8489. continue;
  8490. if (encoder->get_hw_state(encoder, &pipe))
  8491. encoder->get_config(encoder, &pipe_config);
  8492. }
  8493. WARN(crtc->active != active,
  8494. "crtc active state doesn't match with hw state "
  8495. "(expected %i, found %i)\n", crtc->active, active);
  8496. if (active &&
  8497. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  8498. WARN(1, "pipe state doesn't match!\n");
  8499. intel_dump_pipe_config(crtc, &pipe_config,
  8500. "[hw state]");
  8501. intel_dump_pipe_config(crtc, &crtc->config,
  8502. "[sw state]");
  8503. }
  8504. }
  8505. }
  8506. static void
  8507. check_shared_dpll_state(struct drm_device *dev)
  8508. {
  8509. struct drm_i915_private *dev_priv = dev->dev_private;
  8510. struct intel_crtc *crtc;
  8511. struct intel_dpll_hw_state dpll_hw_state;
  8512. int i;
  8513. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8514. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  8515. int enabled_crtcs = 0, active_crtcs = 0;
  8516. bool active;
  8517. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  8518. DRM_DEBUG_KMS("%s\n", pll->name);
  8519. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  8520. WARN(pll->active > pll->refcount,
  8521. "more active pll users than references: %i vs %i\n",
  8522. pll->active, pll->refcount);
  8523. WARN(pll->active && !pll->on,
  8524. "pll in active use but not on in sw tracking\n");
  8525. WARN(pll->on && !pll->active,
  8526. "pll in on but not on in use in sw tracking\n");
  8527. WARN(pll->on != active,
  8528. "pll on state mismatch (expected %i, found %i)\n",
  8529. pll->on, active);
  8530. for_each_intel_crtc(dev, crtc) {
  8531. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  8532. enabled_crtcs++;
  8533. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  8534. active_crtcs++;
  8535. }
  8536. WARN(pll->active != active_crtcs,
  8537. "pll active crtcs mismatch (expected %i, found %i)\n",
  8538. pll->active, active_crtcs);
  8539. WARN(pll->refcount != enabled_crtcs,
  8540. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  8541. pll->refcount, enabled_crtcs);
  8542. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  8543. sizeof(dpll_hw_state)),
  8544. "pll hw state mismatch\n");
  8545. }
  8546. }
  8547. void
  8548. intel_modeset_check_state(struct drm_device *dev)
  8549. {
  8550. check_connector_state(dev);
  8551. check_encoder_state(dev);
  8552. check_crtc_state(dev);
  8553. check_shared_dpll_state(dev);
  8554. }
  8555. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  8556. int dotclock)
  8557. {
  8558. /*
  8559. * FDI already provided one idea for the dotclock.
  8560. * Yell if the encoder disagrees.
  8561. */
  8562. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  8563. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  8564. pipe_config->adjusted_mode.crtc_clock, dotclock);
  8565. }
  8566. static void update_scanline_offset(struct intel_crtc *crtc)
  8567. {
  8568. struct drm_device *dev = crtc->base.dev;
  8569. /*
  8570. * The scanline counter increments at the leading edge of hsync.
  8571. *
  8572. * On most platforms it starts counting from vtotal-1 on the
  8573. * first active line. That means the scanline counter value is
  8574. * always one less than what we would expect. Ie. just after
  8575. * start of vblank, which also occurs at start of hsync (on the
  8576. * last active line), the scanline counter will read vblank_start-1.
  8577. *
  8578. * On gen2 the scanline counter starts counting from 1 instead
  8579. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  8580. * to keep the value positive), instead of adding one.
  8581. *
  8582. * On HSW+ the behaviour of the scanline counter depends on the output
  8583. * type. For DP ports it behaves like most other platforms, but on HDMI
  8584. * there's an extra 1 line difference. So we need to add two instead of
  8585. * one to the value.
  8586. */
  8587. if (IS_GEN2(dev)) {
  8588. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  8589. int vtotal;
  8590. vtotal = mode->crtc_vtotal;
  8591. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  8592. vtotal /= 2;
  8593. crtc->scanline_offset = vtotal - 1;
  8594. } else if (HAS_DDI(dev) &&
  8595. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  8596. crtc->scanline_offset = 2;
  8597. } else
  8598. crtc->scanline_offset = 1;
  8599. }
  8600. static int __intel_set_mode(struct drm_crtc *crtc,
  8601. struct drm_display_mode *mode,
  8602. int x, int y, struct drm_framebuffer *fb)
  8603. {
  8604. struct drm_device *dev = crtc->dev;
  8605. struct drm_i915_private *dev_priv = dev->dev_private;
  8606. struct drm_display_mode *saved_mode;
  8607. struct intel_crtc_config *pipe_config = NULL;
  8608. struct intel_crtc *intel_crtc;
  8609. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  8610. int ret = 0;
  8611. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  8612. if (!saved_mode)
  8613. return -ENOMEM;
  8614. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  8615. &prepare_pipes, &disable_pipes);
  8616. *saved_mode = crtc->mode;
  8617. /* Hack: Because we don't (yet) support global modeset on multiple
  8618. * crtcs, we don't keep track of the new mode for more than one crtc.
  8619. * Hence simply check whether any bit is set in modeset_pipes in all the
  8620. * pieces of code that are not yet converted to deal with mutliple crtcs
  8621. * changing their mode at the same time. */
  8622. if (modeset_pipes) {
  8623. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  8624. if (IS_ERR(pipe_config)) {
  8625. ret = PTR_ERR(pipe_config);
  8626. pipe_config = NULL;
  8627. goto out;
  8628. }
  8629. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  8630. "[modeset]");
  8631. to_intel_crtc(crtc)->new_config = pipe_config;
  8632. }
  8633. /*
  8634. * See if the config requires any additional preparation, e.g.
  8635. * to adjust global state with pipes off. We need to do this
  8636. * here so we can get the modeset_pipe updated config for the new
  8637. * mode set on this crtc. For other crtcs we need to use the
  8638. * adjusted_mode bits in the crtc directly.
  8639. */
  8640. if (IS_VALLEYVIEW(dev)) {
  8641. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  8642. /* may have added more to prepare_pipes than we should */
  8643. prepare_pipes &= ~disable_pipes;
  8644. }
  8645. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  8646. intel_crtc_disable(&intel_crtc->base);
  8647. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  8648. if (intel_crtc->base.enabled)
  8649. dev_priv->display.crtc_disable(&intel_crtc->base);
  8650. }
  8651. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  8652. * to set it here already despite that we pass it down the callchain.
  8653. */
  8654. if (modeset_pipes) {
  8655. crtc->mode = *mode;
  8656. /* mode_set/enable/disable functions rely on a correct pipe
  8657. * config. */
  8658. to_intel_crtc(crtc)->config = *pipe_config;
  8659. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  8660. /*
  8661. * Calculate and store various constants which
  8662. * are later needed by vblank and swap-completion
  8663. * timestamping. They are derived from true hwmode.
  8664. */
  8665. drm_calc_timestamping_constants(crtc,
  8666. &pipe_config->adjusted_mode);
  8667. }
  8668. /* Only after disabling all output pipelines that will be changed can we
  8669. * update the the output configuration. */
  8670. intel_modeset_update_state(dev, prepare_pipes);
  8671. if (dev_priv->display.modeset_global_resources)
  8672. dev_priv->display.modeset_global_resources(dev);
  8673. /* Set up the DPLL and any encoders state that needs to adjust or depend
  8674. * on the DPLL.
  8675. */
  8676. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  8677. struct drm_framebuffer *old_fb;
  8678. mutex_lock(&dev->struct_mutex);
  8679. ret = intel_pin_and_fence_fb_obj(dev,
  8680. to_intel_framebuffer(fb)->obj,
  8681. NULL);
  8682. if (ret != 0) {
  8683. DRM_ERROR("pin & fence failed\n");
  8684. mutex_unlock(&dev->struct_mutex);
  8685. goto done;
  8686. }
  8687. old_fb = crtc->primary->fb;
  8688. if (old_fb)
  8689. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  8690. mutex_unlock(&dev->struct_mutex);
  8691. crtc->primary->fb = fb;
  8692. crtc->x = x;
  8693. crtc->y = y;
  8694. ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
  8695. x, y, fb);
  8696. if (ret)
  8697. goto done;
  8698. }
  8699. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  8700. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  8701. update_scanline_offset(intel_crtc);
  8702. dev_priv->display.crtc_enable(&intel_crtc->base);
  8703. }
  8704. /* FIXME: add subpixel order */
  8705. done:
  8706. if (ret && crtc->enabled)
  8707. crtc->mode = *saved_mode;
  8708. out:
  8709. kfree(pipe_config);
  8710. kfree(saved_mode);
  8711. return ret;
  8712. }
  8713. static int intel_set_mode(struct drm_crtc *crtc,
  8714. struct drm_display_mode *mode,
  8715. int x, int y, struct drm_framebuffer *fb)
  8716. {
  8717. int ret;
  8718. ret = __intel_set_mode(crtc, mode, x, y, fb);
  8719. if (ret == 0)
  8720. intel_modeset_check_state(crtc->dev);
  8721. return ret;
  8722. }
  8723. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  8724. {
  8725. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  8726. }
  8727. #undef for_each_intel_crtc_masked
  8728. static void intel_set_config_free(struct intel_set_config *config)
  8729. {
  8730. if (!config)
  8731. return;
  8732. kfree(config->save_connector_encoders);
  8733. kfree(config->save_encoder_crtcs);
  8734. kfree(config->save_crtc_enabled);
  8735. kfree(config);
  8736. }
  8737. static int intel_set_config_save_state(struct drm_device *dev,
  8738. struct intel_set_config *config)
  8739. {
  8740. struct drm_crtc *crtc;
  8741. struct drm_encoder *encoder;
  8742. struct drm_connector *connector;
  8743. int count;
  8744. config->save_crtc_enabled =
  8745. kcalloc(dev->mode_config.num_crtc,
  8746. sizeof(bool), GFP_KERNEL);
  8747. if (!config->save_crtc_enabled)
  8748. return -ENOMEM;
  8749. config->save_encoder_crtcs =
  8750. kcalloc(dev->mode_config.num_encoder,
  8751. sizeof(struct drm_crtc *), GFP_KERNEL);
  8752. if (!config->save_encoder_crtcs)
  8753. return -ENOMEM;
  8754. config->save_connector_encoders =
  8755. kcalloc(dev->mode_config.num_connector,
  8756. sizeof(struct drm_encoder *), GFP_KERNEL);
  8757. if (!config->save_connector_encoders)
  8758. return -ENOMEM;
  8759. /* Copy data. Note that driver private data is not affected.
  8760. * Should anything bad happen only the expected state is
  8761. * restored, not the drivers personal bookkeeping.
  8762. */
  8763. count = 0;
  8764. for_each_crtc(dev, crtc) {
  8765. config->save_crtc_enabled[count++] = crtc->enabled;
  8766. }
  8767. count = 0;
  8768. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  8769. config->save_encoder_crtcs[count++] = encoder->crtc;
  8770. }
  8771. count = 0;
  8772. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8773. config->save_connector_encoders[count++] = connector->encoder;
  8774. }
  8775. return 0;
  8776. }
  8777. static void intel_set_config_restore_state(struct drm_device *dev,
  8778. struct intel_set_config *config)
  8779. {
  8780. struct intel_crtc *crtc;
  8781. struct intel_encoder *encoder;
  8782. struct intel_connector *connector;
  8783. int count;
  8784. count = 0;
  8785. for_each_intel_crtc(dev, crtc) {
  8786. crtc->new_enabled = config->save_crtc_enabled[count++];
  8787. if (crtc->new_enabled)
  8788. crtc->new_config = &crtc->config;
  8789. else
  8790. crtc->new_config = NULL;
  8791. }
  8792. count = 0;
  8793. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8794. encoder->new_crtc =
  8795. to_intel_crtc(config->save_encoder_crtcs[count++]);
  8796. }
  8797. count = 0;
  8798. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  8799. connector->new_encoder =
  8800. to_intel_encoder(config->save_connector_encoders[count++]);
  8801. }
  8802. }
  8803. static bool
  8804. is_crtc_connector_off(struct drm_mode_set *set)
  8805. {
  8806. int i;
  8807. if (set->num_connectors == 0)
  8808. return false;
  8809. if (WARN_ON(set->connectors == NULL))
  8810. return false;
  8811. for (i = 0; i < set->num_connectors; i++)
  8812. if (set->connectors[i]->encoder &&
  8813. set->connectors[i]->encoder->crtc == set->crtc &&
  8814. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  8815. return true;
  8816. return false;
  8817. }
  8818. static void
  8819. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  8820. struct intel_set_config *config)
  8821. {
  8822. /* We should be able to check here if the fb has the same properties
  8823. * and then just flip_or_move it */
  8824. if (is_crtc_connector_off(set)) {
  8825. config->mode_changed = true;
  8826. } else if (set->crtc->primary->fb != set->fb) {
  8827. /* If we have no fb then treat it as a full mode set */
  8828. if (set->crtc->primary->fb == NULL) {
  8829. struct intel_crtc *intel_crtc =
  8830. to_intel_crtc(set->crtc);
  8831. if (intel_crtc->active && i915.fastboot) {
  8832. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  8833. config->fb_changed = true;
  8834. } else {
  8835. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  8836. config->mode_changed = true;
  8837. }
  8838. } else if (set->fb == NULL) {
  8839. config->mode_changed = true;
  8840. } else if (set->fb->pixel_format !=
  8841. set->crtc->primary->fb->pixel_format) {
  8842. config->mode_changed = true;
  8843. } else {
  8844. config->fb_changed = true;
  8845. }
  8846. }
  8847. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  8848. config->fb_changed = true;
  8849. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  8850. DRM_DEBUG_KMS("modes are different, full mode set\n");
  8851. drm_mode_debug_printmodeline(&set->crtc->mode);
  8852. drm_mode_debug_printmodeline(set->mode);
  8853. config->mode_changed = true;
  8854. }
  8855. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  8856. set->crtc->base.id, config->mode_changed, config->fb_changed);
  8857. }
  8858. static int
  8859. intel_modeset_stage_output_state(struct drm_device *dev,
  8860. struct drm_mode_set *set,
  8861. struct intel_set_config *config)
  8862. {
  8863. struct intel_connector *connector;
  8864. struct intel_encoder *encoder;
  8865. struct intel_crtc *crtc;
  8866. int ro;
  8867. /* The upper layers ensure that we either disable a crtc or have a list
  8868. * of connectors. For paranoia, double-check this. */
  8869. WARN_ON(!set->fb && (set->num_connectors != 0));
  8870. WARN_ON(set->fb && (set->num_connectors == 0));
  8871. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8872. base.head) {
  8873. /* Otherwise traverse passed in connector list and get encoders
  8874. * for them. */
  8875. for (ro = 0; ro < set->num_connectors; ro++) {
  8876. if (set->connectors[ro] == &connector->base) {
  8877. connector->new_encoder = connector->encoder;
  8878. break;
  8879. }
  8880. }
  8881. /* If we disable the crtc, disable all its connectors. Also, if
  8882. * the connector is on the changing crtc but not on the new
  8883. * connector list, disable it. */
  8884. if ((!set->fb || ro == set->num_connectors) &&
  8885. connector->base.encoder &&
  8886. connector->base.encoder->crtc == set->crtc) {
  8887. connector->new_encoder = NULL;
  8888. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  8889. connector->base.base.id,
  8890. connector->base.name);
  8891. }
  8892. if (&connector->new_encoder->base != connector->base.encoder) {
  8893. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  8894. config->mode_changed = true;
  8895. }
  8896. }
  8897. /* connector->new_encoder is now updated for all connectors. */
  8898. /* Update crtc of enabled connectors. */
  8899. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8900. base.head) {
  8901. struct drm_crtc *new_crtc;
  8902. if (!connector->new_encoder)
  8903. continue;
  8904. new_crtc = connector->new_encoder->base.crtc;
  8905. for (ro = 0; ro < set->num_connectors; ro++) {
  8906. if (set->connectors[ro] == &connector->base)
  8907. new_crtc = set->crtc;
  8908. }
  8909. /* Make sure the new CRTC will work with the encoder */
  8910. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  8911. new_crtc)) {
  8912. return -EINVAL;
  8913. }
  8914. connector->encoder->new_crtc = to_intel_crtc(new_crtc);
  8915. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  8916. connector->base.base.id,
  8917. connector->base.name,
  8918. new_crtc->base.id);
  8919. }
  8920. /* Check for any encoders that needs to be disabled. */
  8921. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8922. base.head) {
  8923. int num_connectors = 0;
  8924. list_for_each_entry(connector,
  8925. &dev->mode_config.connector_list,
  8926. base.head) {
  8927. if (connector->new_encoder == encoder) {
  8928. WARN_ON(!connector->new_encoder->new_crtc);
  8929. num_connectors++;
  8930. }
  8931. }
  8932. if (num_connectors == 0)
  8933. encoder->new_crtc = NULL;
  8934. else if (num_connectors > 1)
  8935. return -EINVAL;
  8936. /* Only now check for crtc changes so we don't miss encoders
  8937. * that will be disabled. */
  8938. if (&encoder->new_crtc->base != encoder->base.crtc) {
  8939. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  8940. config->mode_changed = true;
  8941. }
  8942. }
  8943. /* Now we've also updated encoder->new_crtc for all encoders. */
  8944. for_each_intel_crtc(dev, crtc) {
  8945. crtc->new_enabled = false;
  8946. list_for_each_entry(encoder,
  8947. &dev->mode_config.encoder_list,
  8948. base.head) {
  8949. if (encoder->new_crtc == crtc) {
  8950. crtc->new_enabled = true;
  8951. break;
  8952. }
  8953. }
  8954. if (crtc->new_enabled != crtc->base.enabled) {
  8955. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  8956. crtc->new_enabled ? "en" : "dis");
  8957. config->mode_changed = true;
  8958. }
  8959. if (crtc->new_enabled)
  8960. crtc->new_config = &crtc->config;
  8961. else
  8962. crtc->new_config = NULL;
  8963. }
  8964. return 0;
  8965. }
  8966. static void disable_crtc_nofb(struct intel_crtc *crtc)
  8967. {
  8968. struct drm_device *dev = crtc->base.dev;
  8969. struct intel_encoder *encoder;
  8970. struct intel_connector *connector;
  8971. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  8972. pipe_name(crtc->pipe));
  8973. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  8974. if (connector->new_encoder &&
  8975. connector->new_encoder->new_crtc == crtc)
  8976. connector->new_encoder = NULL;
  8977. }
  8978. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8979. if (encoder->new_crtc == crtc)
  8980. encoder->new_crtc = NULL;
  8981. }
  8982. crtc->new_enabled = false;
  8983. crtc->new_config = NULL;
  8984. }
  8985. static int intel_crtc_set_config(struct drm_mode_set *set)
  8986. {
  8987. struct drm_device *dev;
  8988. struct drm_mode_set save_set;
  8989. struct intel_set_config *config;
  8990. int ret;
  8991. BUG_ON(!set);
  8992. BUG_ON(!set->crtc);
  8993. BUG_ON(!set->crtc->helper_private);
  8994. /* Enforce sane interface api - has been abused by the fb helper. */
  8995. BUG_ON(!set->mode && set->fb);
  8996. BUG_ON(set->fb && set->num_connectors == 0);
  8997. if (set->fb) {
  8998. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  8999. set->crtc->base.id, set->fb->base.id,
  9000. (int)set->num_connectors, set->x, set->y);
  9001. } else {
  9002. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9003. }
  9004. dev = set->crtc->dev;
  9005. ret = -ENOMEM;
  9006. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9007. if (!config)
  9008. goto out_config;
  9009. ret = intel_set_config_save_state(dev, config);
  9010. if (ret)
  9011. goto out_config;
  9012. save_set.crtc = set->crtc;
  9013. save_set.mode = &set->crtc->mode;
  9014. save_set.x = set->crtc->x;
  9015. save_set.y = set->crtc->y;
  9016. save_set.fb = set->crtc->primary->fb;
  9017. /* Compute whether we need a full modeset, only an fb base update or no
  9018. * change at all. In the future we might also check whether only the
  9019. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9020. * such cases. */
  9021. intel_set_config_compute_mode_changes(set, config);
  9022. ret = intel_modeset_stage_output_state(dev, set, config);
  9023. if (ret)
  9024. goto fail;
  9025. if (config->mode_changed) {
  9026. ret = intel_set_mode(set->crtc, set->mode,
  9027. set->x, set->y, set->fb);
  9028. } else if (config->fb_changed) {
  9029. intel_crtc_wait_for_pending_flips(set->crtc);
  9030. ret = intel_pipe_set_base(set->crtc,
  9031. set->x, set->y, set->fb);
  9032. /*
  9033. * In the fastboot case this may be our only check of the
  9034. * state after boot. It would be better to only do it on
  9035. * the first update, but we don't have a nice way of doing that
  9036. * (and really, set_config isn't used much for high freq page
  9037. * flipping, so increasing its cost here shouldn't be a big
  9038. * deal).
  9039. */
  9040. if (i915.fastboot && ret == 0)
  9041. intel_modeset_check_state(set->crtc->dev);
  9042. }
  9043. if (ret) {
  9044. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9045. set->crtc->base.id, ret);
  9046. fail:
  9047. intel_set_config_restore_state(dev, config);
  9048. /*
  9049. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9050. * force the pipe off to avoid oopsing in the modeset code
  9051. * due to fb==NULL. This should only happen during boot since
  9052. * we don't yet reconstruct the FB from the hardware state.
  9053. */
  9054. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9055. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9056. /* Try to restore the config */
  9057. if (config->mode_changed &&
  9058. intel_set_mode(save_set.crtc, save_set.mode,
  9059. save_set.x, save_set.y, save_set.fb))
  9060. DRM_ERROR("failed to restore config after modeset failure\n");
  9061. }
  9062. out_config:
  9063. intel_set_config_free(config);
  9064. return ret;
  9065. }
  9066. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9067. .cursor_set = intel_crtc_cursor_set,
  9068. .cursor_move = intel_crtc_cursor_move,
  9069. .gamma_set = intel_crtc_gamma_set,
  9070. .set_config = intel_crtc_set_config,
  9071. .destroy = intel_crtc_destroy,
  9072. .page_flip = intel_crtc_page_flip,
  9073. };
  9074. static void intel_cpu_pll_init(struct drm_device *dev)
  9075. {
  9076. if (HAS_DDI(dev))
  9077. intel_ddi_pll_init(dev);
  9078. }
  9079. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9080. struct intel_shared_dpll *pll,
  9081. struct intel_dpll_hw_state *hw_state)
  9082. {
  9083. uint32_t val;
  9084. val = I915_READ(PCH_DPLL(pll->id));
  9085. hw_state->dpll = val;
  9086. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9087. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9088. return val & DPLL_VCO_ENABLE;
  9089. }
  9090. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9091. struct intel_shared_dpll *pll)
  9092. {
  9093. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9094. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9095. }
  9096. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9097. struct intel_shared_dpll *pll)
  9098. {
  9099. /* PCH refclock must be enabled first */
  9100. ibx_assert_pch_refclk_enabled(dev_priv);
  9101. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9102. /* Wait for the clocks to stabilize. */
  9103. POSTING_READ(PCH_DPLL(pll->id));
  9104. udelay(150);
  9105. /* The pixel multiplier can only be updated once the
  9106. * DPLL is enabled and the clocks are stable.
  9107. *
  9108. * So write it again.
  9109. */
  9110. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9111. POSTING_READ(PCH_DPLL(pll->id));
  9112. udelay(200);
  9113. }
  9114. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9115. struct intel_shared_dpll *pll)
  9116. {
  9117. struct drm_device *dev = dev_priv->dev;
  9118. struct intel_crtc *crtc;
  9119. /* Make sure no transcoder isn't still depending on us. */
  9120. for_each_intel_crtc(dev, crtc) {
  9121. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9122. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9123. }
  9124. I915_WRITE(PCH_DPLL(pll->id), 0);
  9125. POSTING_READ(PCH_DPLL(pll->id));
  9126. udelay(200);
  9127. }
  9128. static char *ibx_pch_dpll_names[] = {
  9129. "PCH DPLL A",
  9130. "PCH DPLL B",
  9131. };
  9132. static void ibx_pch_dpll_init(struct drm_device *dev)
  9133. {
  9134. struct drm_i915_private *dev_priv = dev->dev_private;
  9135. int i;
  9136. dev_priv->num_shared_dpll = 2;
  9137. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9138. dev_priv->shared_dplls[i].id = i;
  9139. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9140. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9141. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9142. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9143. dev_priv->shared_dplls[i].get_hw_state =
  9144. ibx_pch_dpll_get_hw_state;
  9145. }
  9146. }
  9147. static void intel_shared_dpll_init(struct drm_device *dev)
  9148. {
  9149. struct drm_i915_private *dev_priv = dev->dev_private;
  9150. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9151. ibx_pch_dpll_init(dev);
  9152. else
  9153. dev_priv->num_shared_dpll = 0;
  9154. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9155. }
  9156. static void intel_crtc_init(struct drm_device *dev, int pipe)
  9157. {
  9158. struct drm_i915_private *dev_priv = dev->dev_private;
  9159. struct intel_crtc *intel_crtc;
  9160. int i;
  9161. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  9162. if (intel_crtc == NULL)
  9163. return;
  9164. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  9165. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  9166. for (i = 0; i < 256; i++) {
  9167. intel_crtc->lut_r[i] = i;
  9168. intel_crtc->lut_g[i] = i;
  9169. intel_crtc->lut_b[i] = i;
  9170. }
  9171. /*
  9172. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  9173. * is hooked to plane B. Hence we want plane A feeding pipe B.
  9174. */
  9175. intel_crtc->pipe = pipe;
  9176. intel_crtc->plane = pipe;
  9177. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  9178. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  9179. intel_crtc->plane = !pipe;
  9180. }
  9181. intel_crtc->cursor_base = ~0;
  9182. intel_crtc->cursor_cntl = ~0;
  9183. init_waitqueue_head(&intel_crtc->vbl_wait);
  9184. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  9185. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  9186. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  9187. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  9188. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  9189. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  9190. }
  9191. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  9192. {
  9193. struct drm_encoder *encoder = connector->base.encoder;
  9194. struct drm_device *dev = connector->base.dev;
  9195. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  9196. if (!encoder)
  9197. return INVALID_PIPE;
  9198. return to_intel_crtc(encoder->crtc)->pipe;
  9199. }
  9200. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  9201. struct drm_file *file)
  9202. {
  9203. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  9204. struct drm_mode_object *drmmode_obj;
  9205. struct intel_crtc *crtc;
  9206. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  9207. return -ENODEV;
  9208. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  9209. DRM_MODE_OBJECT_CRTC);
  9210. if (!drmmode_obj) {
  9211. DRM_ERROR("no such CRTC id\n");
  9212. return -ENOENT;
  9213. }
  9214. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  9215. pipe_from_crtc_id->pipe = crtc->pipe;
  9216. return 0;
  9217. }
  9218. static int intel_encoder_clones(struct intel_encoder *encoder)
  9219. {
  9220. struct drm_device *dev = encoder->base.dev;
  9221. struct intel_encoder *source_encoder;
  9222. int index_mask = 0;
  9223. int entry = 0;
  9224. list_for_each_entry(source_encoder,
  9225. &dev->mode_config.encoder_list, base.head) {
  9226. if (encoders_cloneable(encoder, source_encoder))
  9227. index_mask |= (1 << entry);
  9228. entry++;
  9229. }
  9230. return index_mask;
  9231. }
  9232. static bool has_edp_a(struct drm_device *dev)
  9233. {
  9234. struct drm_i915_private *dev_priv = dev->dev_private;
  9235. if (!IS_MOBILE(dev))
  9236. return false;
  9237. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  9238. return false;
  9239. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  9240. return false;
  9241. return true;
  9242. }
  9243. const char *intel_output_name(int output)
  9244. {
  9245. static const char *names[] = {
  9246. [INTEL_OUTPUT_UNUSED] = "Unused",
  9247. [INTEL_OUTPUT_ANALOG] = "Analog",
  9248. [INTEL_OUTPUT_DVO] = "DVO",
  9249. [INTEL_OUTPUT_SDVO] = "SDVO",
  9250. [INTEL_OUTPUT_LVDS] = "LVDS",
  9251. [INTEL_OUTPUT_TVOUT] = "TV",
  9252. [INTEL_OUTPUT_HDMI] = "HDMI",
  9253. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  9254. [INTEL_OUTPUT_EDP] = "eDP",
  9255. [INTEL_OUTPUT_DSI] = "DSI",
  9256. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  9257. };
  9258. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  9259. return "Invalid";
  9260. return names[output];
  9261. }
  9262. static bool intel_crt_present(struct drm_device *dev)
  9263. {
  9264. struct drm_i915_private *dev_priv = dev->dev_private;
  9265. if (IS_ULT(dev))
  9266. return false;
  9267. if (IS_CHERRYVIEW(dev))
  9268. return false;
  9269. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  9270. return false;
  9271. return true;
  9272. }
  9273. static void intel_setup_outputs(struct drm_device *dev)
  9274. {
  9275. struct drm_i915_private *dev_priv = dev->dev_private;
  9276. struct intel_encoder *encoder;
  9277. bool dpd_is_edp = false;
  9278. intel_lvds_init(dev);
  9279. if (intel_crt_present(dev))
  9280. intel_crt_init(dev);
  9281. if (HAS_DDI(dev)) {
  9282. int found;
  9283. /* Haswell uses DDI functions to detect digital outputs */
  9284. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  9285. /* DDI A only supports eDP */
  9286. if (found)
  9287. intel_ddi_init(dev, PORT_A);
  9288. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  9289. * register */
  9290. found = I915_READ(SFUSE_STRAP);
  9291. if (found & SFUSE_STRAP_DDIB_DETECTED)
  9292. intel_ddi_init(dev, PORT_B);
  9293. if (found & SFUSE_STRAP_DDIC_DETECTED)
  9294. intel_ddi_init(dev, PORT_C);
  9295. if (found & SFUSE_STRAP_DDID_DETECTED)
  9296. intel_ddi_init(dev, PORT_D);
  9297. } else if (HAS_PCH_SPLIT(dev)) {
  9298. int found;
  9299. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  9300. if (has_edp_a(dev))
  9301. intel_dp_init(dev, DP_A, PORT_A);
  9302. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  9303. /* PCH SDVOB multiplex with HDMIB */
  9304. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  9305. if (!found)
  9306. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  9307. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  9308. intel_dp_init(dev, PCH_DP_B, PORT_B);
  9309. }
  9310. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  9311. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  9312. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  9313. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  9314. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  9315. intel_dp_init(dev, PCH_DP_C, PORT_C);
  9316. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  9317. intel_dp_init(dev, PCH_DP_D, PORT_D);
  9318. } else if (IS_VALLEYVIEW(dev)) {
  9319. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  9320. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  9321. PORT_B);
  9322. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  9323. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  9324. }
  9325. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  9326. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  9327. PORT_C);
  9328. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  9329. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  9330. }
  9331. if (IS_CHERRYVIEW(dev)) {
  9332. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
  9333. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  9334. PORT_D);
  9335. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  9336. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  9337. }
  9338. }
  9339. intel_dsi_init(dev);
  9340. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  9341. bool found = false;
  9342. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  9343. DRM_DEBUG_KMS("probing SDVOB\n");
  9344. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  9345. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  9346. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  9347. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  9348. }
  9349. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  9350. intel_dp_init(dev, DP_B, PORT_B);
  9351. }
  9352. /* Before G4X SDVOC doesn't have its own detect register */
  9353. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  9354. DRM_DEBUG_KMS("probing SDVOC\n");
  9355. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  9356. }
  9357. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  9358. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  9359. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  9360. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  9361. }
  9362. if (SUPPORTS_INTEGRATED_DP(dev))
  9363. intel_dp_init(dev, DP_C, PORT_C);
  9364. }
  9365. if (SUPPORTS_INTEGRATED_DP(dev) &&
  9366. (I915_READ(DP_D) & DP_DETECTED))
  9367. intel_dp_init(dev, DP_D, PORT_D);
  9368. } else if (IS_GEN2(dev))
  9369. intel_dvo_init(dev);
  9370. if (SUPPORTS_TV(dev))
  9371. intel_tv_init(dev);
  9372. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  9373. encoder->base.possible_crtcs = encoder->crtc_mask;
  9374. encoder->base.possible_clones =
  9375. intel_encoder_clones(encoder);
  9376. }
  9377. intel_init_pch_refclk(dev);
  9378. drm_helper_move_panel_connectors_to_head(dev);
  9379. }
  9380. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  9381. {
  9382. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9383. drm_framebuffer_cleanup(fb);
  9384. WARN_ON(!intel_fb->obj->framebuffer_references--);
  9385. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  9386. kfree(intel_fb);
  9387. }
  9388. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  9389. struct drm_file *file,
  9390. unsigned int *handle)
  9391. {
  9392. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9393. struct drm_i915_gem_object *obj = intel_fb->obj;
  9394. return drm_gem_handle_create(file, &obj->base, handle);
  9395. }
  9396. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  9397. .destroy = intel_user_framebuffer_destroy,
  9398. .create_handle = intel_user_framebuffer_create_handle,
  9399. };
  9400. static int intel_framebuffer_init(struct drm_device *dev,
  9401. struct intel_framebuffer *intel_fb,
  9402. struct drm_mode_fb_cmd2 *mode_cmd,
  9403. struct drm_i915_gem_object *obj)
  9404. {
  9405. int aligned_height;
  9406. int pitch_limit;
  9407. int ret;
  9408. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  9409. if (obj->tiling_mode == I915_TILING_Y) {
  9410. DRM_DEBUG("hardware does not support tiling Y\n");
  9411. return -EINVAL;
  9412. }
  9413. if (mode_cmd->pitches[0] & 63) {
  9414. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  9415. mode_cmd->pitches[0]);
  9416. return -EINVAL;
  9417. }
  9418. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  9419. pitch_limit = 32*1024;
  9420. } else if (INTEL_INFO(dev)->gen >= 4) {
  9421. if (obj->tiling_mode)
  9422. pitch_limit = 16*1024;
  9423. else
  9424. pitch_limit = 32*1024;
  9425. } else if (INTEL_INFO(dev)->gen >= 3) {
  9426. if (obj->tiling_mode)
  9427. pitch_limit = 8*1024;
  9428. else
  9429. pitch_limit = 16*1024;
  9430. } else
  9431. /* XXX DSPC is limited to 4k tiled */
  9432. pitch_limit = 8*1024;
  9433. if (mode_cmd->pitches[0] > pitch_limit) {
  9434. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  9435. obj->tiling_mode ? "tiled" : "linear",
  9436. mode_cmd->pitches[0], pitch_limit);
  9437. return -EINVAL;
  9438. }
  9439. if (obj->tiling_mode != I915_TILING_NONE &&
  9440. mode_cmd->pitches[0] != obj->stride) {
  9441. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  9442. mode_cmd->pitches[0], obj->stride);
  9443. return -EINVAL;
  9444. }
  9445. /* Reject formats not supported by any plane early. */
  9446. switch (mode_cmd->pixel_format) {
  9447. case DRM_FORMAT_C8:
  9448. case DRM_FORMAT_RGB565:
  9449. case DRM_FORMAT_XRGB8888:
  9450. case DRM_FORMAT_ARGB8888:
  9451. break;
  9452. case DRM_FORMAT_XRGB1555:
  9453. case DRM_FORMAT_ARGB1555:
  9454. if (INTEL_INFO(dev)->gen > 3) {
  9455. DRM_DEBUG("unsupported pixel format: %s\n",
  9456. drm_get_format_name(mode_cmd->pixel_format));
  9457. return -EINVAL;
  9458. }
  9459. break;
  9460. case DRM_FORMAT_XBGR8888:
  9461. case DRM_FORMAT_ABGR8888:
  9462. case DRM_FORMAT_XRGB2101010:
  9463. case DRM_FORMAT_ARGB2101010:
  9464. case DRM_FORMAT_XBGR2101010:
  9465. case DRM_FORMAT_ABGR2101010:
  9466. if (INTEL_INFO(dev)->gen < 4) {
  9467. DRM_DEBUG("unsupported pixel format: %s\n",
  9468. drm_get_format_name(mode_cmd->pixel_format));
  9469. return -EINVAL;
  9470. }
  9471. break;
  9472. case DRM_FORMAT_YUYV:
  9473. case DRM_FORMAT_UYVY:
  9474. case DRM_FORMAT_YVYU:
  9475. case DRM_FORMAT_VYUY:
  9476. if (INTEL_INFO(dev)->gen < 5) {
  9477. DRM_DEBUG("unsupported pixel format: %s\n",
  9478. drm_get_format_name(mode_cmd->pixel_format));
  9479. return -EINVAL;
  9480. }
  9481. break;
  9482. default:
  9483. DRM_DEBUG("unsupported pixel format: %s\n",
  9484. drm_get_format_name(mode_cmd->pixel_format));
  9485. return -EINVAL;
  9486. }
  9487. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  9488. if (mode_cmd->offsets[0] != 0)
  9489. return -EINVAL;
  9490. aligned_height = intel_align_height(dev, mode_cmd->height,
  9491. obj->tiling_mode);
  9492. /* FIXME drm helper for size checks (especially planar formats)? */
  9493. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  9494. return -EINVAL;
  9495. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  9496. intel_fb->obj = obj;
  9497. intel_fb->obj->framebuffer_references++;
  9498. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  9499. if (ret) {
  9500. DRM_ERROR("framebuffer init failed %d\n", ret);
  9501. return ret;
  9502. }
  9503. return 0;
  9504. }
  9505. static struct drm_framebuffer *
  9506. intel_user_framebuffer_create(struct drm_device *dev,
  9507. struct drm_file *filp,
  9508. struct drm_mode_fb_cmd2 *mode_cmd)
  9509. {
  9510. struct drm_i915_gem_object *obj;
  9511. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  9512. mode_cmd->handles[0]));
  9513. if (&obj->base == NULL)
  9514. return ERR_PTR(-ENOENT);
  9515. return intel_framebuffer_create(dev, mode_cmd, obj);
  9516. }
  9517. #ifndef CONFIG_DRM_I915_FBDEV
  9518. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  9519. {
  9520. }
  9521. #endif
  9522. static const struct drm_mode_config_funcs intel_mode_funcs = {
  9523. .fb_create = intel_user_framebuffer_create,
  9524. .output_poll_changed = intel_fbdev_output_poll_changed,
  9525. };
  9526. /* Set up chip specific display functions */
  9527. static void intel_init_display(struct drm_device *dev)
  9528. {
  9529. struct drm_i915_private *dev_priv = dev->dev_private;
  9530. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  9531. dev_priv->display.find_dpll = g4x_find_best_dpll;
  9532. else if (IS_CHERRYVIEW(dev))
  9533. dev_priv->display.find_dpll = chv_find_best_dpll;
  9534. else if (IS_VALLEYVIEW(dev))
  9535. dev_priv->display.find_dpll = vlv_find_best_dpll;
  9536. else if (IS_PINEVIEW(dev))
  9537. dev_priv->display.find_dpll = pnv_find_best_dpll;
  9538. else
  9539. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  9540. if (HAS_DDI(dev)) {
  9541. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  9542. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  9543. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  9544. dev_priv->display.crtc_enable = haswell_crtc_enable;
  9545. dev_priv->display.crtc_disable = haswell_crtc_disable;
  9546. dev_priv->display.off = haswell_crtc_off;
  9547. dev_priv->display.update_primary_plane =
  9548. ironlake_update_primary_plane;
  9549. } else if (HAS_PCH_SPLIT(dev)) {
  9550. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  9551. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  9552. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  9553. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  9554. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  9555. dev_priv->display.off = ironlake_crtc_off;
  9556. dev_priv->display.update_primary_plane =
  9557. ironlake_update_primary_plane;
  9558. } else if (IS_VALLEYVIEW(dev)) {
  9559. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  9560. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  9561. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  9562. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  9563. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  9564. dev_priv->display.off = i9xx_crtc_off;
  9565. dev_priv->display.update_primary_plane =
  9566. i9xx_update_primary_plane;
  9567. } else {
  9568. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  9569. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  9570. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  9571. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  9572. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  9573. dev_priv->display.off = i9xx_crtc_off;
  9574. dev_priv->display.update_primary_plane =
  9575. i9xx_update_primary_plane;
  9576. }
  9577. /* Returns the core display clock speed */
  9578. if (IS_VALLEYVIEW(dev))
  9579. dev_priv->display.get_display_clock_speed =
  9580. valleyview_get_display_clock_speed;
  9581. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  9582. dev_priv->display.get_display_clock_speed =
  9583. i945_get_display_clock_speed;
  9584. else if (IS_I915G(dev))
  9585. dev_priv->display.get_display_clock_speed =
  9586. i915_get_display_clock_speed;
  9587. else if (IS_I945GM(dev) || IS_845G(dev))
  9588. dev_priv->display.get_display_clock_speed =
  9589. i9xx_misc_get_display_clock_speed;
  9590. else if (IS_PINEVIEW(dev))
  9591. dev_priv->display.get_display_clock_speed =
  9592. pnv_get_display_clock_speed;
  9593. else if (IS_I915GM(dev))
  9594. dev_priv->display.get_display_clock_speed =
  9595. i915gm_get_display_clock_speed;
  9596. else if (IS_I865G(dev))
  9597. dev_priv->display.get_display_clock_speed =
  9598. i865_get_display_clock_speed;
  9599. else if (IS_I85X(dev))
  9600. dev_priv->display.get_display_clock_speed =
  9601. i855_get_display_clock_speed;
  9602. else /* 852, 830 */
  9603. dev_priv->display.get_display_clock_speed =
  9604. i830_get_display_clock_speed;
  9605. if (HAS_PCH_SPLIT(dev)) {
  9606. if (IS_GEN5(dev)) {
  9607. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  9608. dev_priv->display.write_eld = ironlake_write_eld;
  9609. } else if (IS_GEN6(dev)) {
  9610. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  9611. dev_priv->display.write_eld = ironlake_write_eld;
  9612. dev_priv->display.modeset_global_resources =
  9613. snb_modeset_global_resources;
  9614. } else if (IS_IVYBRIDGE(dev)) {
  9615. /* FIXME: detect B0+ stepping and use auto training */
  9616. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  9617. dev_priv->display.write_eld = ironlake_write_eld;
  9618. dev_priv->display.modeset_global_resources =
  9619. ivb_modeset_global_resources;
  9620. } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
  9621. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  9622. dev_priv->display.write_eld = haswell_write_eld;
  9623. dev_priv->display.modeset_global_resources =
  9624. haswell_modeset_global_resources;
  9625. }
  9626. } else if (IS_G4X(dev)) {
  9627. dev_priv->display.write_eld = g4x_write_eld;
  9628. } else if (IS_VALLEYVIEW(dev)) {
  9629. dev_priv->display.modeset_global_resources =
  9630. valleyview_modeset_global_resources;
  9631. dev_priv->display.write_eld = ironlake_write_eld;
  9632. }
  9633. /* Default just returns -ENODEV to indicate unsupported */
  9634. dev_priv->display.queue_flip = intel_default_queue_flip;
  9635. switch (INTEL_INFO(dev)->gen) {
  9636. case 2:
  9637. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  9638. break;
  9639. case 3:
  9640. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  9641. break;
  9642. case 4:
  9643. case 5:
  9644. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  9645. break;
  9646. case 6:
  9647. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  9648. break;
  9649. case 7:
  9650. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  9651. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  9652. break;
  9653. }
  9654. intel_panel_init_backlight_funcs(dev);
  9655. }
  9656. /*
  9657. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  9658. * resume, or other times. This quirk makes sure that's the case for
  9659. * affected systems.
  9660. */
  9661. static void quirk_pipea_force(struct drm_device *dev)
  9662. {
  9663. struct drm_i915_private *dev_priv = dev->dev_private;
  9664. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  9665. DRM_INFO("applying pipe a force quirk\n");
  9666. }
  9667. /*
  9668. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  9669. */
  9670. static void quirk_ssc_force_disable(struct drm_device *dev)
  9671. {
  9672. struct drm_i915_private *dev_priv = dev->dev_private;
  9673. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  9674. DRM_INFO("applying lvds SSC disable quirk\n");
  9675. }
  9676. /*
  9677. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  9678. * brightness value
  9679. */
  9680. static void quirk_invert_brightness(struct drm_device *dev)
  9681. {
  9682. struct drm_i915_private *dev_priv = dev->dev_private;
  9683. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  9684. DRM_INFO("applying inverted panel brightness quirk\n");
  9685. }
  9686. /* Some VBT's incorrectly indicate no backlight is present */
  9687. static void quirk_backlight_present(struct drm_device *dev)
  9688. {
  9689. struct drm_i915_private *dev_priv = dev->dev_private;
  9690. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  9691. DRM_INFO("applying backlight present quirk\n");
  9692. }
  9693. struct intel_quirk {
  9694. int device;
  9695. int subsystem_vendor;
  9696. int subsystem_device;
  9697. void (*hook)(struct drm_device *dev);
  9698. };
  9699. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  9700. struct intel_dmi_quirk {
  9701. void (*hook)(struct drm_device *dev);
  9702. const struct dmi_system_id (*dmi_id_list)[];
  9703. };
  9704. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  9705. {
  9706. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  9707. return 1;
  9708. }
  9709. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  9710. {
  9711. .dmi_id_list = &(const struct dmi_system_id[]) {
  9712. {
  9713. .callback = intel_dmi_reverse_brightness,
  9714. .ident = "NCR Corporation",
  9715. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  9716. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  9717. },
  9718. },
  9719. { } /* terminating entry */
  9720. },
  9721. .hook = quirk_invert_brightness,
  9722. },
  9723. };
  9724. static struct intel_quirk intel_quirks[] = {
  9725. /* HP Mini needs pipe A force quirk (LP: #322104) */
  9726. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  9727. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  9728. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  9729. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  9730. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  9731. /* Lenovo U160 cannot use SSC on LVDS */
  9732. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  9733. /* Sony Vaio Y cannot use SSC on LVDS */
  9734. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  9735. /* Acer Aspire 5734Z must invert backlight brightness */
  9736. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  9737. /* Acer/eMachines G725 */
  9738. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  9739. /* Acer/eMachines e725 */
  9740. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  9741. /* Acer/Packard Bell NCL20 */
  9742. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  9743. /* Acer Aspire 4736Z */
  9744. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  9745. /* Acer Aspire 5336 */
  9746. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  9747. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  9748. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  9749. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  9750. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  9751. /* HP Chromebook 14 (Celeron 2955U) */
  9752. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  9753. };
  9754. static void intel_init_quirks(struct drm_device *dev)
  9755. {
  9756. struct pci_dev *d = dev->pdev;
  9757. int i;
  9758. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  9759. struct intel_quirk *q = &intel_quirks[i];
  9760. if (d->device == q->device &&
  9761. (d->subsystem_vendor == q->subsystem_vendor ||
  9762. q->subsystem_vendor == PCI_ANY_ID) &&
  9763. (d->subsystem_device == q->subsystem_device ||
  9764. q->subsystem_device == PCI_ANY_ID))
  9765. q->hook(dev);
  9766. }
  9767. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  9768. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  9769. intel_dmi_quirks[i].hook(dev);
  9770. }
  9771. }
  9772. /* Disable the VGA plane that we never use */
  9773. static void i915_disable_vga(struct drm_device *dev)
  9774. {
  9775. struct drm_i915_private *dev_priv = dev->dev_private;
  9776. u8 sr1;
  9777. u32 vga_reg = i915_vgacntrl_reg(dev);
  9778. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  9779. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  9780. outb(SR01, VGA_SR_INDEX);
  9781. sr1 = inb(VGA_SR_DATA);
  9782. outb(sr1 | 1<<5, VGA_SR_DATA);
  9783. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  9784. udelay(300);
  9785. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  9786. POSTING_READ(vga_reg);
  9787. }
  9788. void intel_modeset_init_hw(struct drm_device *dev)
  9789. {
  9790. intel_prepare_ddi(dev);
  9791. intel_init_clock_gating(dev);
  9792. intel_reset_dpio(dev);
  9793. intel_enable_gt_powersave(dev);
  9794. }
  9795. void intel_modeset_suspend_hw(struct drm_device *dev)
  9796. {
  9797. intel_suspend_hw(dev);
  9798. }
  9799. void intel_modeset_init(struct drm_device *dev)
  9800. {
  9801. struct drm_i915_private *dev_priv = dev->dev_private;
  9802. int sprite, ret;
  9803. enum pipe pipe;
  9804. struct intel_crtc *crtc;
  9805. drm_mode_config_init(dev);
  9806. dev->mode_config.min_width = 0;
  9807. dev->mode_config.min_height = 0;
  9808. dev->mode_config.preferred_depth = 24;
  9809. dev->mode_config.prefer_shadow = 1;
  9810. dev->mode_config.funcs = &intel_mode_funcs;
  9811. intel_init_quirks(dev);
  9812. intel_init_pm(dev);
  9813. if (INTEL_INFO(dev)->num_pipes == 0)
  9814. return;
  9815. intel_init_display(dev);
  9816. if (IS_GEN2(dev)) {
  9817. dev->mode_config.max_width = 2048;
  9818. dev->mode_config.max_height = 2048;
  9819. } else if (IS_GEN3(dev)) {
  9820. dev->mode_config.max_width = 4096;
  9821. dev->mode_config.max_height = 4096;
  9822. } else {
  9823. dev->mode_config.max_width = 8192;
  9824. dev->mode_config.max_height = 8192;
  9825. }
  9826. if (IS_GEN2(dev)) {
  9827. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  9828. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  9829. } else {
  9830. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  9831. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  9832. }
  9833. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  9834. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  9835. INTEL_INFO(dev)->num_pipes,
  9836. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  9837. for_each_pipe(pipe) {
  9838. intel_crtc_init(dev, pipe);
  9839. for_each_sprite(pipe, sprite) {
  9840. ret = intel_plane_init(dev, pipe, sprite);
  9841. if (ret)
  9842. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  9843. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  9844. }
  9845. }
  9846. intel_init_dpio(dev);
  9847. intel_reset_dpio(dev);
  9848. intel_cpu_pll_init(dev);
  9849. intel_shared_dpll_init(dev);
  9850. /* Just disable it once at startup */
  9851. i915_disable_vga(dev);
  9852. intel_setup_outputs(dev);
  9853. /* Just in case the BIOS is doing something questionable. */
  9854. intel_disable_fbc(dev);
  9855. drm_modeset_lock_all(dev);
  9856. intel_modeset_setup_hw_state(dev, false);
  9857. drm_modeset_unlock_all(dev);
  9858. for_each_intel_crtc(dev, crtc) {
  9859. if (!crtc->active)
  9860. continue;
  9861. /*
  9862. * Note that reserving the BIOS fb up front prevents us
  9863. * from stuffing other stolen allocations like the ring
  9864. * on top. This prevents some ugliness at boot time, and
  9865. * can even allow for smooth boot transitions if the BIOS
  9866. * fb is large enough for the active pipe configuration.
  9867. */
  9868. if (dev_priv->display.get_plane_config) {
  9869. dev_priv->display.get_plane_config(crtc,
  9870. &crtc->plane_config);
  9871. /*
  9872. * If the fb is shared between multiple heads, we'll
  9873. * just get the first one.
  9874. */
  9875. intel_find_plane_obj(crtc, &crtc->plane_config);
  9876. }
  9877. }
  9878. }
  9879. static void intel_enable_pipe_a(struct drm_device *dev)
  9880. {
  9881. struct intel_connector *connector;
  9882. struct drm_connector *crt = NULL;
  9883. struct intel_load_detect_pipe load_detect_temp;
  9884. struct drm_modeset_acquire_ctx ctx;
  9885. /* We can't just switch on the pipe A, we need to set things up with a
  9886. * proper mode and output configuration. As a gross hack, enable pipe A
  9887. * by enabling the load detect pipe once. */
  9888. list_for_each_entry(connector,
  9889. &dev->mode_config.connector_list,
  9890. base.head) {
  9891. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  9892. crt = &connector->base;
  9893. break;
  9894. }
  9895. }
  9896. if (!crt)
  9897. return;
  9898. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
  9899. intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
  9900. }
  9901. static bool
  9902. intel_check_plane_mapping(struct intel_crtc *crtc)
  9903. {
  9904. struct drm_device *dev = crtc->base.dev;
  9905. struct drm_i915_private *dev_priv = dev->dev_private;
  9906. u32 reg, val;
  9907. if (INTEL_INFO(dev)->num_pipes == 1)
  9908. return true;
  9909. reg = DSPCNTR(!crtc->plane);
  9910. val = I915_READ(reg);
  9911. if ((val & DISPLAY_PLANE_ENABLE) &&
  9912. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  9913. return false;
  9914. return true;
  9915. }
  9916. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  9917. {
  9918. struct drm_device *dev = crtc->base.dev;
  9919. struct drm_i915_private *dev_priv = dev->dev_private;
  9920. u32 reg;
  9921. /* Clear any frame start delays used for debugging left by the BIOS */
  9922. reg = PIPECONF(crtc->config.cpu_transcoder);
  9923. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  9924. /* restore vblank interrupts to correct state */
  9925. if (crtc->active)
  9926. drm_vblank_on(dev, crtc->pipe);
  9927. else
  9928. drm_vblank_off(dev, crtc->pipe);
  9929. /* We need to sanitize the plane -> pipe mapping first because this will
  9930. * disable the crtc (and hence change the state) if it is wrong. Note
  9931. * that gen4+ has a fixed plane -> pipe mapping. */
  9932. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  9933. struct intel_connector *connector;
  9934. bool plane;
  9935. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  9936. crtc->base.base.id);
  9937. /* Pipe has the wrong plane attached and the plane is active.
  9938. * Temporarily change the plane mapping and disable everything
  9939. * ... */
  9940. plane = crtc->plane;
  9941. crtc->plane = !plane;
  9942. crtc->primary_enabled = true;
  9943. dev_priv->display.crtc_disable(&crtc->base);
  9944. crtc->plane = plane;
  9945. /* ... and break all links. */
  9946. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9947. base.head) {
  9948. if (connector->encoder->base.crtc != &crtc->base)
  9949. continue;
  9950. connector->base.dpms = DRM_MODE_DPMS_OFF;
  9951. connector->base.encoder = NULL;
  9952. }
  9953. /* multiple connectors may have the same encoder:
  9954. * handle them and break crtc link separately */
  9955. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9956. base.head)
  9957. if (connector->encoder->base.crtc == &crtc->base) {
  9958. connector->encoder->base.crtc = NULL;
  9959. connector->encoder->connectors_active = false;
  9960. }
  9961. WARN_ON(crtc->active);
  9962. crtc->base.enabled = false;
  9963. }
  9964. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  9965. crtc->pipe == PIPE_A && !crtc->active) {
  9966. /* BIOS forgot to enable pipe A, this mostly happens after
  9967. * resume. Force-enable the pipe to fix this, the update_dpms
  9968. * call below we restore the pipe to the right state, but leave
  9969. * the required bits on. */
  9970. intel_enable_pipe_a(dev);
  9971. }
  9972. /* Adjust the state of the output pipe according to whether we
  9973. * have active connectors/encoders. */
  9974. intel_crtc_update_dpms(&crtc->base);
  9975. if (crtc->active != crtc->base.enabled) {
  9976. struct intel_encoder *encoder;
  9977. /* This can happen either due to bugs in the get_hw_state
  9978. * functions or because the pipe is force-enabled due to the
  9979. * pipe A quirk. */
  9980. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  9981. crtc->base.base.id,
  9982. crtc->base.enabled ? "enabled" : "disabled",
  9983. crtc->active ? "enabled" : "disabled");
  9984. crtc->base.enabled = crtc->active;
  9985. /* Because we only establish the connector -> encoder ->
  9986. * crtc links if something is active, this means the
  9987. * crtc is now deactivated. Break the links. connector
  9988. * -> encoder links are only establish when things are
  9989. * actually up, hence no need to break them. */
  9990. WARN_ON(crtc->active);
  9991. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  9992. WARN_ON(encoder->connectors_active);
  9993. encoder->base.crtc = NULL;
  9994. }
  9995. }
  9996. if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
  9997. /*
  9998. * We start out with underrun reporting disabled to avoid races.
  9999. * For correct bookkeeping mark this on active crtcs.
  10000. *
  10001. * Also on gmch platforms we dont have any hardware bits to
  10002. * disable the underrun reporting. Which means we need to start
  10003. * out with underrun reporting disabled also on inactive pipes,
  10004. * since otherwise we'll complain about the garbage we read when
  10005. * e.g. coming up after runtime pm.
  10006. *
  10007. * No protection against concurrent access is required - at
  10008. * worst a fifo underrun happens which also sets this to false.
  10009. */
  10010. crtc->cpu_fifo_underrun_disabled = true;
  10011. crtc->pch_fifo_underrun_disabled = true;
  10012. update_scanline_offset(crtc);
  10013. }
  10014. }
  10015. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  10016. {
  10017. struct intel_connector *connector;
  10018. struct drm_device *dev = encoder->base.dev;
  10019. /* We need to check both for a crtc link (meaning that the
  10020. * encoder is active and trying to read from a pipe) and the
  10021. * pipe itself being active. */
  10022. bool has_active_crtc = encoder->base.crtc &&
  10023. to_intel_crtc(encoder->base.crtc)->active;
  10024. if (encoder->connectors_active && !has_active_crtc) {
  10025. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  10026. encoder->base.base.id,
  10027. encoder->base.name);
  10028. /* Connector is active, but has no active pipe. This is
  10029. * fallout from our resume register restoring. Disable
  10030. * the encoder manually again. */
  10031. if (encoder->base.crtc) {
  10032. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  10033. encoder->base.base.id,
  10034. encoder->base.name);
  10035. encoder->disable(encoder);
  10036. }
  10037. encoder->base.crtc = NULL;
  10038. encoder->connectors_active = false;
  10039. /* Inconsistent output/port/pipe state happens presumably due to
  10040. * a bug in one of the get_hw_state functions. Or someplace else
  10041. * in our code, like the register restore mess on resume. Clamp
  10042. * things to off as a safer default. */
  10043. list_for_each_entry(connector,
  10044. &dev->mode_config.connector_list,
  10045. base.head) {
  10046. if (connector->encoder != encoder)
  10047. continue;
  10048. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10049. connector->base.encoder = NULL;
  10050. }
  10051. }
  10052. /* Enabled encoders without active connectors will be fixed in
  10053. * the crtc fixup. */
  10054. }
  10055. void i915_redisable_vga_power_on(struct drm_device *dev)
  10056. {
  10057. struct drm_i915_private *dev_priv = dev->dev_private;
  10058. u32 vga_reg = i915_vgacntrl_reg(dev);
  10059. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  10060. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  10061. i915_disable_vga(dev);
  10062. }
  10063. }
  10064. void i915_redisable_vga(struct drm_device *dev)
  10065. {
  10066. struct drm_i915_private *dev_priv = dev->dev_private;
  10067. /* This function can be called both from intel_modeset_setup_hw_state or
  10068. * at a very early point in our resume sequence, where the power well
  10069. * structures are not yet restored. Since this function is at a very
  10070. * paranoid "someone might have enabled VGA while we were not looking"
  10071. * level, just check if the power well is enabled instead of trying to
  10072. * follow the "don't touch the power well if we don't need it" policy
  10073. * the rest of the driver uses. */
  10074. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
  10075. return;
  10076. i915_redisable_vga_power_on(dev);
  10077. }
  10078. static bool primary_get_hw_state(struct intel_crtc *crtc)
  10079. {
  10080. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  10081. if (!crtc->active)
  10082. return false;
  10083. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  10084. }
  10085. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  10086. {
  10087. struct drm_i915_private *dev_priv = dev->dev_private;
  10088. enum pipe pipe;
  10089. struct intel_crtc *crtc;
  10090. struct intel_encoder *encoder;
  10091. struct intel_connector *connector;
  10092. int i;
  10093. for_each_intel_crtc(dev, crtc) {
  10094. memset(&crtc->config, 0, sizeof(crtc->config));
  10095. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  10096. crtc->active = dev_priv->display.get_pipe_config(crtc,
  10097. &crtc->config);
  10098. crtc->base.enabled = crtc->active;
  10099. crtc->primary_enabled = primary_get_hw_state(crtc);
  10100. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  10101. crtc->base.base.id,
  10102. crtc->active ? "enabled" : "disabled");
  10103. }
  10104. /* FIXME: Smash this into the new shared dpll infrastructure. */
  10105. if (HAS_DDI(dev))
  10106. intel_ddi_setup_hw_pll_state(dev);
  10107. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10108. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10109. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  10110. pll->active = 0;
  10111. for_each_intel_crtc(dev, crtc) {
  10112. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10113. pll->active++;
  10114. }
  10115. pll->refcount = pll->active;
  10116. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  10117. pll->name, pll->refcount, pll->on);
  10118. }
  10119. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10120. base.head) {
  10121. pipe = 0;
  10122. if (encoder->get_hw_state(encoder, &pipe)) {
  10123. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10124. encoder->base.crtc = &crtc->base;
  10125. encoder->get_config(encoder, &crtc->config);
  10126. } else {
  10127. encoder->base.crtc = NULL;
  10128. }
  10129. encoder->connectors_active = false;
  10130. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  10131. encoder->base.base.id,
  10132. encoder->base.name,
  10133. encoder->base.crtc ? "enabled" : "disabled",
  10134. pipe_name(pipe));
  10135. }
  10136. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10137. base.head) {
  10138. if (connector->get_hw_state(connector)) {
  10139. connector->base.dpms = DRM_MODE_DPMS_ON;
  10140. connector->encoder->connectors_active = true;
  10141. connector->base.encoder = &connector->encoder->base;
  10142. } else {
  10143. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10144. connector->base.encoder = NULL;
  10145. }
  10146. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  10147. connector->base.base.id,
  10148. connector->base.name,
  10149. connector->base.encoder ? "enabled" : "disabled");
  10150. }
  10151. }
  10152. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  10153. * and i915 state tracking structures. */
  10154. void intel_modeset_setup_hw_state(struct drm_device *dev,
  10155. bool force_restore)
  10156. {
  10157. struct drm_i915_private *dev_priv = dev->dev_private;
  10158. enum pipe pipe;
  10159. struct intel_crtc *crtc;
  10160. struct intel_encoder *encoder;
  10161. int i;
  10162. intel_modeset_readout_hw_state(dev);
  10163. /*
  10164. * Now that we have the config, copy it to each CRTC struct
  10165. * Note that this could go away if we move to using crtc_config
  10166. * checking everywhere.
  10167. */
  10168. for_each_intel_crtc(dev, crtc) {
  10169. if (crtc->active && i915.fastboot) {
  10170. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  10171. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  10172. crtc->base.base.id);
  10173. drm_mode_debug_printmodeline(&crtc->base.mode);
  10174. }
  10175. }
  10176. /* HW state is read out, now we need to sanitize this mess. */
  10177. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10178. base.head) {
  10179. intel_sanitize_encoder(encoder);
  10180. }
  10181. for_each_pipe(pipe) {
  10182. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10183. intel_sanitize_crtc(crtc);
  10184. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  10185. }
  10186. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10187. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10188. if (!pll->on || pll->active)
  10189. continue;
  10190. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  10191. pll->disable(dev_priv, pll);
  10192. pll->on = false;
  10193. }
  10194. if (HAS_PCH_SPLIT(dev))
  10195. ilk_wm_get_hw_state(dev);
  10196. if (force_restore) {
  10197. i915_redisable_vga(dev);
  10198. /*
  10199. * We need to use raw interfaces for restoring state to avoid
  10200. * checking (bogus) intermediate states.
  10201. */
  10202. for_each_pipe(pipe) {
  10203. struct drm_crtc *crtc =
  10204. dev_priv->pipe_to_crtc_mapping[pipe];
  10205. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  10206. crtc->primary->fb);
  10207. }
  10208. } else {
  10209. intel_modeset_update_staged_output_state(dev);
  10210. }
  10211. intel_modeset_check_state(dev);
  10212. }
  10213. void intel_modeset_gem_init(struct drm_device *dev)
  10214. {
  10215. struct drm_crtc *c;
  10216. struct intel_framebuffer *fb;
  10217. mutex_lock(&dev->struct_mutex);
  10218. intel_init_gt_powersave(dev);
  10219. mutex_unlock(&dev->struct_mutex);
  10220. intel_modeset_init_hw(dev);
  10221. intel_setup_overlay(dev);
  10222. /*
  10223. * Make sure any fbs we allocated at startup are properly
  10224. * pinned & fenced. When we do the allocation it's too early
  10225. * for this.
  10226. */
  10227. mutex_lock(&dev->struct_mutex);
  10228. for_each_crtc(dev, c) {
  10229. if (!c->primary->fb)
  10230. continue;
  10231. fb = to_intel_framebuffer(c->primary->fb);
  10232. if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
  10233. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  10234. to_intel_crtc(c)->pipe);
  10235. drm_framebuffer_unreference(c->primary->fb);
  10236. c->primary->fb = NULL;
  10237. }
  10238. }
  10239. mutex_unlock(&dev->struct_mutex);
  10240. }
  10241. void intel_connector_unregister(struct intel_connector *intel_connector)
  10242. {
  10243. struct drm_connector *connector = &intel_connector->base;
  10244. intel_panel_destroy_backlight(connector);
  10245. drm_sysfs_connector_remove(connector);
  10246. }
  10247. void intel_modeset_cleanup(struct drm_device *dev)
  10248. {
  10249. struct drm_i915_private *dev_priv = dev->dev_private;
  10250. struct drm_crtc *crtc;
  10251. struct drm_connector *connector;
  10252. /*
  10253. * Interrupts and polling as the first thing to avoid creating havoc.
  10254. * Too much stuff here (turning of rps, connectors, ...) would
  10255. * experience fancy races otherwise.
  10256. */
  10257. drm_irq_uninstall(dev);
  10258. cancel_work_sync(&dev_priv->hotplug_work);
  10259. /*
  10260. * Due to the hpd irq storm handling the hotplug work can re-arm the
  10261. * poll handlers. Hence disable polling after hpd handling is shut down.
  10262. */
  10263. drm_kms_helper_poll_fini(dev);
  10264. mutex_lock(&dev->struct_mutex);
  10265. intel_unregister_dsm_handler();
  10266. for_each_crtc(dev, crtc) {
  10267. /* Skip inactive CRTCs */
  10268. if (!crtc->primary->fb)
  10269. continue;
  10270. intel_increase_pllclock(crtc);
  10271. }
  10272. intel_disable_fbc(dev);
  10273. intel_disable_gt_powersave(dev);
  10274. ironlake_teardown_rc6(dev);
  10275. mutex_unlock(&dev->struct_mutex);
  10276. /* flush any delayed tasks or pending work */
  10277. flush_scheduled_work();
  10278. /* destroy the backlight and sysfs files before encoders/connectors */
  10279. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10280. struct intel_connector *intel_connector;
  10281. intel_connector = to_intel_connector(connector);
  10282. intel_connector->unregister(intel_connector);
  10283. }
  10284. drm_mode_config_cleanup(dev);
  10285. intel_cleanup_overlay(dev);
  10286. mutex_lock(&dev->struct_mutex);
  10287. intel_cleanup_gt_powersave(dev);
  10288. mutex_unlock(&dev->struct_mutex);
  10289. }
  10290. /*
  10291. * Return which encoder is currently attached for connector.
  10292. */
  10293. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  10294. {
  10295. return &intel_attached_encoder(connector)->base;
  10296. }
  10297. void intel_connector_attach_encoder(struct intel_connector *connector,
  10298. struct intel_encoder *encoder)
  10299. {
  10300. connector->encoder = encoder;
  10301. drm_mode_connector_attach_encoder(&connector->base,
  10302. &encoder->base);
  10303. }
  10304. /*
  10305. * set vga decode state - true == enable VGA decode
  10306. */
  10307. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  10308. {
  10309. struct drm_i915_private *dev_priv = dev->dev_private;
  10310. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  10311. u16 gmch_ctrl;
  10312. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  10313. DRM_ERROR("failed to read control word\n");
  10314. return -EIO;
  10315. }
  10316. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  10317. return 0;
  10318. if (state)
  10319. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  10320. else
  10321. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  10322. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  10323. DRM_ERROR("failed to write control word\n");
  10324. return -EIO;
  10325. }
  10326. return 0;
  10327. }
  10328. struct intel_display_error_state {
  10329. u32 power_well_driver;
  10330. int num_transcoders;
  10331. struct intel_cursor_error_state {
  10332. u32 control;
  10333. u32 position;
  10334. u32 base;
  10335. u32 size;
  10336. } cursor[I915_MAX_PIPES];
  10337. struct intel_pipe_error_state {
  10338. bool power_domain_on;
  10339. u32 source;
  10340. u32 stat;
  10341. } pipe[I915_MAX_PIPES];
  10342. struct intel_plane_error_state {
  10343. u32 control;
  10344. u32 stride;
  10345. u32 size;
  10346. u32 pos;
  10347. u32 addr;
  10348. u32 surface;
  10349. u32 tile_offset;
  10350. } plane[I915_MAX_PIPES];
  10351. struct intel_transcoder_error_state {
  10352. bool power_domain_on;
  10353. enum transcoder cpu_transcoder;
  10354. u32 conf;
  10355. u32 htotal;
  10356. u32 hblank;
  10357. u32 hsync;
  10358. u32 vtotal;
  10359. u32 vblank;
  10360. u32 vsync;
  10361. } transcoder[4];
  10362. };
  10363. struct intel_display_error_state *
  10364. intel_display_capture_error_state(struct drm_device *dev)
  10365. {
  10366. struct drm_i915_private *dev_priv = dev->dev_private;
  10367. struct intel_display_error_state *error;
  10368. int transcoders[] = {
  10369. TRANSCODER_A,
  10370. TRANSCODER_B,
  10371. TRANSCODER_C,
  10372. TRANSCODER_EDP,
  10373. };
  10374. int i;
  10375. if (INTEL_INFO(dev)->num_pipes == 0)
  10376. return NULL;
  10377. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  10378. if (error == NULL)
  10379. return NULL;
  10380. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  10381. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  10382. for_each_pipe(i) {
  10383. error->pipe[i].power_domain_on =
  10384. intel_display_power_enabled_unlocked(dev_priv,
  10385. POWER_DOMAIN_PIPE(i));
  10386. if (!error->pipe[i].power_domain_on)
  10387. continue;
  10388. error->cursor[i].control = I915_READ(CURCNTR(i));
  10389. error->cursor[i].position = I915_READ(CURPOS(i));
  10390. error->cursor[i].base = I915_READ(CURBASE(i));
  10391. error->plane[i].control = I915_READ(DSPCNTR(i));
  10392. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  10393. if (INTEL_INFO(dev)->gen <= 3) {
  10394. error->plane[i].size = I915_READ(DSPSIZE(i));
  10395. error->plane[i].pos = I915_READ(DSPPOS(i));
  10396. }
  10397. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  10398. error->plane[i].addr = I915_READ(DSPADDR(i));
  10399. if (INTEL_INFO(dev)->gen >= 4) {
  10400. error->plane[i].surface = I915_READ(DSPSURF(i));
  10401. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  10402. }
  10403. error->pipe[i].source = I915_READ(PIPESRC(i));
  10404. if (!HAS_PCH_SPLIT(dev))
  10405. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  10406. }
  10407. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  10408. if (HAS_DDI(dev_priv->dev))
  10409. error->num_transcoders++; /* Account for eDP. */
  10410. for (i = 0; i < error->num_transcoders; i++) {
  10411. enum transcoder cpu_transcoder = transcoders[i];
  10412. error->transcoder[i].power_domain_on =
  10413. intel_display_power_enabled_unlocked(dev_priv,
  10414. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  10415. if (!error->transcoder[i].power_domain_on)
  10416. continue;
  10417. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  10418. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  10419. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  10420. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  10421. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  10422. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  10423. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  10424. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  10425. }
  10426. return error;
  10427. }
  10428. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  10429. void
  10430. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  10431. struct drm_device *dev,
  10432. struct intel_display_error_state *error)
  10433. {
  10434. int i;
  10435. if (!error)
  10436. return;
  10437. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  10438. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  10439. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  10440. error->power_well_driver);
  10441. for_each_pipe(i) {
  10442. err_printf(m, "Pipe [%d]:\n", i);
  10443. err_printf(m, " Power: %s\n",
  10444. error->pipe[i].power_domain_on ? "on" : "off");
  10445. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  10446. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  10447. err_printf(m, "Plane [%d]:\n", i);
  10448. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  10449. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  10450. if (INTEL_INFO(dev)->gen <= 3) {
  10451. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  10452. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  10453. }
  10454. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  10455. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  10456. if (INTEL_INFO(dev)->gen >= 4) {
  10457. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  10458. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  10459. }
  10460. err_printf(m, "Cursor [%d]:\n", i);
  10461. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  10462. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  10463. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  10464. }
  10465. for (i = 0; i < error->num_transcoders; i++) {
  10466. err_printf(m, "CPU transcoder: %c\n",
  10467. transcoder_name(error->transcoder[i].cpu_transcoder));
  10468. err_printf(m, " Power: %s\n",
  10469. error->transcoder[i].power_domain_on ? "on" : "off");
  10470. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  10471. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  10472. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  10473. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  10474. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  10475. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  10476. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  10477. }
  10478. }