intel_display.c 267 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868
  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. bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
  43. static void intel_increase_pllclock(struct drm_crtc *crtc);
  44. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  45. typedef struct {
  46. int min, max;
  47. } intel_range_t;
  48. typedef struct {
  49. int dot_limit;
  50. int p2_slow, p2_fast;
  51. } intel_p2_t;
  52. #define INTEL_P2_NUM 2
  53. typedef struct intel_limit intel_limit_t;
  54. struct intel_limit {
  55. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  56. intel_p2_t p2;
  57. };
  58. /* FDI */
  59. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  60. int
  61. intel_pch_rawclk(struct drm_device *dev)
  62. {
  63. struct drm_i915_private *dev_priv = dev->dev_private;
  64. WARN_ON(!HAS_PCH_SPLIT(dev));
  65. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  66. }
  67. static inline u32 /* units of 100MHz */
  68. intel_fdi_link_freq(struct drm_device *dev)
  69. {
  70. if (IS_GEN5(dev)) {
  71. struct drm_i915_private *dev_priv = dev->dev_private;
  72. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  73. } else
  74. return 27;
  75. }
  76. static const intel_limit_t intel_limits_i8xx_dvo = {
  77. .dot = { .min = 25000, .max = 350000 },
  78. .vco = { .min = 930000, .max = 1400000 },
  79. .n = { .min = 3, .max = 16 },
  80. .m = { .min = 96, .max = 140 },
  81. .m1 = { .min = 18, .max = 26 },
  82. .m2 = { .min = 6, .max = 16 },
  83. .p = { .min = 4, .max = 128 },
  84. .p1 = { .min = 2, .max = 33 },
  85. .p2 = { .dot_limit = 165000,
  86. .p2_slow = 4, .p2_fast = 2 },
  87. };
  88. static const intel_limit_t intel_limits_i8xx_lvds = {
  89. .dot = { .min = 25000, .max = 350000 },
  90. .vco = { .min = 930000, .max = 1400000 },
  91. .n = { .min = 3, .max = 16 },
  92. .m = { .min = 96, .max = 140 },
  93. .m1 = { .min = 18, .max = 26 },
  94. .m2 = { .min = 6, .max = 16 },
  95. .p = { .min = 4, .max = 128 },
  96. .p1 = { .min = 1, .max = 6 },
  97. .p2 = { .dot_limit = 165000,
  98. .p2_slow = 14, .p2_fast = 7 },
  99. };
  100. static const intel_limit_t intel_limits_i9xx_sdvo = {
  101. .dot = { .min = 20000, .max = 400000 },
  102. .vco = { .min = 1400000, .max = 2800000 },
  103. .n = { .min = 1, .max = 6 },
  104. .m = { .min = 70, .max = 120 },
  105. .m1 = { .min = 8, .max = 18 },
  106. .m2 = { .min = 3, .max = 7 },
  107. .p = { .min = 5, .max = 80 },
  108. .p1 = { .min = 1, .max = 8 },
  109. .p2 = { .dot_limit = 200000,
  110. .p2_slow = 10, .p2_fast = 5 },
  111. };
  112. static const intel_limit_t intel_limits_i9xx_lvds = {
  113. .dot = { .min = 20000, .max = 400000 },
  114. .vco = { .min = 1400000, .max = 2800000 },
  115. .n = { .min = 1, .max = 6 },
  116. .m = { .min = 70, .max = 120 },
  117. .m1 = { .min = 8, .max = 18 },
  118. .m2 = { .min = 3, .max = 7 },
  119. .p = { .min = 7, .max = 98 },
  120. .p1 = { .min = 1, .max = 8 },
  121. .p2 = { .dot_limit = 112000,
  122. .p2_slow = 14, .p2_fast = 7 },
  123. };
  124. static const intel_limit_t intel_limits_g4x_sdvo = {
  125. .dot = { .min = 25000, .max = 270000 },
  126. .vco = { .min = 1750000, .max = 3500000},
  127. .n = { .min = 1, .max = 4 },
  128. .m = { .min = 104, .max = 138 },
  129. .m1 = { .min = 17, .max = 23 },
  130. .m2 = { .min = 5, .max = 11 },
  131. .p = { .min = 10, .max = 30 },
  132. .p1 = { .min = 1, .max = 3},
  133. .p2 = { .dot_limit = 270000,
  134. .p2_slow = 10,
  135. .p2_fast = 10
  136. },
  137. };
  138. static const intel_limit_t intel_limits_g4x_hdmi = {
  139. .dot = { .min = 22000, .max = 400000 },
  140. .vco = { .min = 1750000, .max = 3500000},
  141. .n = { .min = 1, .max = 4 },
  142. .m = { .min = 104, .max = 138 },
  143. .m1 = { .min = 16, .max = 23 },
  144. .m2 = { .min = 5, .max = 11 },
  145. .p = { .min = 5, .max = 80 },
  146. .p1 = { .min = 1, .max = 8},
  147. .p2 = { .dot_limit = 165000,
  148. .p2_slow = 10, .p2_fast = 5 },
  149. };
  150. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  151. .dot = { .min = 20000, .max = 115000 },
  152. .vco = { .min = 1750000, .max = 3500000 },
  153. .n = { .min = 1, .max = 3 },
  154. .m = { .min = 104, .max = 138 },
  155. .m1 = { .min = 17, .max = 23 },
  156. .m2 = { .min = 5, .max = 11 },
  157. .p = { .min = 28, .max = 112 },
  158. .p1 = { .min = 2, .max = 8 },
  159. .p2 = { .dot_limit = 0,
  160. .p2_slow = 14, .p2_fast = 14
  161. },
  162. };
  163. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  164. .dot = { .min = 80000, .max = 224000 },
  165. .vco = { .min = 1750000, .max = 3500000 },
  166. .n = { .min = 1, .max = 3 },
  167. .m = { .min = 104, .max = 138 },
  168. .m1 = { .min = 17, .max = 23 },
  169. .m2 = { .min = 5, .max = 11 },
  170. .p = { .min = 14, .max = 42 },
  171. .p1 = { .min = 2, .max = 6 },
  172. .p2 = { .dot_limit = 0,
  173. .p2_slow = 7, .p2_fast = 7
  174. },
  175. };
  176. static const intel_limit_t intel_limits_pineview_sdvo = {
  177. .dot = { .min = 20000, .max = 400000},
  178. .vco = { .min = 1700000, .max = 3500000 },
  179. /* Pineview's Ncounter is a ring counter */
  180. .n = { .min = 3, .max = 6 },
  181. .m = { .min = 2, .max = 256 },
  182. /* Pineview only has one combined m divider, which we treat as m2. */
  183. .m1 = { .min = 0, .max = 0 },
  184. .m2 = { .min = 0, .max = 254 },
  185. .p = { .min = 5, .max = 80 },
  186. .p1 = { .min = 1, .max = 8 },
  187. .p2 = { .dot_limit = 200000,
  188. .p2_slow = 10, .p2_fast = 5 },
  189. };
  190. static const intel_limit_t intel_limits_pineview_lvds = {
  191. .dot = { .min = 20000, .max = 400000 },
  192. .vco = { .min = 1700000, .max = 3500000 },
  193. .n = { .min = 3, .max = 6 },
  194. .m = { .min = 2, .max = 256 },
  195. .m1 = { .min = 0, .max = 0 },
  196. .m2 = { .min = 0, .max = 254 },
  197. .p = { .min = 7, .max = 112 },
  198. .p1 = { .min = 1, .max = 8 },
  199. .p2 = { .dot_limit = 112000,
  200. .p2_slow = 14, .p2_fast = 14 },
  201. };
  202. /* Ironlake / Sandybridge
  203. *
  204. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  205. * the range value for them is (actual_value - 2).
  206. */
  207. static const intel_limit_t intel_limits_ironlake_dac = {
  208. .dot = { .min = 25000, .max = 350000 },
  209. .vco = { .min = 1760000, .max = 3510000 },
  210. .n = { .min = 1, .max = 5 },
  211. .m = { .min = 79, .max = 127 },
  212. .m1 = { .min = 12, .max = 22 },
  213. .m2 = { .min = 5, .max = 9 },
  214. .p = { .min = 5, .max = 80 },
  215. .p1 = { .min = 1, .max = 8 },
  216. .p2 = { .dot_limit = 225000,
  217. .p2_slow = 10, .p2_fast = 5 },
  218. };
  219. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  220. .dot = { .min = 25000, .max = 350000 },
  221. .vco = { .min = 1760000, .max = 3510000 },
  222. .n = { .min = 1, .max = 3 },
  223. .m = { .min = 79, .max = 118 },
  224. .m1 = { .min = 12, .max = 22 },
  225. .m2 = { .min = 5, .max = 9 },
  226. .p = { .min = 28, .max = 112 },
  227. .p1 = { .min = 2, .max = 8 },
  228. .p2 = { .dot_limit = 225000,
  229. .p2_slow = 14, .p2_fast = 14 },
  230. };
  231. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  232. .dot = { .min = 25000, .max = 350000 },
  233. .vco = { .min = 1760000, .max = 3510000 },
  234. .n = { .min = 1, .max = 3 },
  235. .m = { .min = 79, .max = 127 },
  236. .m1 = { .min = 12, .max = 22 },
  237. .m2 = { .min = 5, .max = 9 },
  238. .p = { .min = 14, .max = 56 },
  239. .p1 = { .min = 2, .max = 8 },
  240. .p2 = { .dot_limit = 225000,
  241. .p2_slow = 7, .p2_fast = 7 },
  242. };
  243. /* LVDS 100mhz refclk limits. */
  244. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 1760000, .max = 3510000 },
  247. .n = { .min = 1, .max = 2 },
  248. .m = { .min = 79, .max = 126 },
  249. .m1 = { .min = 12, .max = 22 },
  250. .m2 = { .min = 5, .max = 9 },
  251. .p = { .min = 28, .max = 112 },
  252. .p1 = { .min = 2, .max = 8 },
  253. .p2 = { .dot_limit = 225000,
  254. .p2_slow = 14, .p2_fast = 14 },
  255. };
  256. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 1760000, .max = 3510000 },
  259. .n = { .min = 1, .max = 3 },
  260. .m = { .min = 79, .max = 126 },
  261. .m1 = { .min = 12, .max = 22 },
  262. .m2 = { .min = 5, .max = 9 },
  263. .p = { .min = 14, .max = 42 },
  264. .p1 = { .min = 2, .max = 6 },
  265. .p2 = { .dot_limit = 225000,
  266. .p2_slow = 7, .p2_fast = 7 },
  267. };
  268. static const intel_limit_t intel_limits_vlv_dac = {
  269. .dot = { .min = 25000, .max = 270000 },
  270. .vco = { .min = 4000000, .max = 6000000 },
  271. .n = { .min = 1, .max = 7 },
  272. .m = { .min = 22, .max = 450 }, /* guess */
  273. .m1 = { .min = 2, .max = 3 },
  274. .m2 = { .min = 11, .max = 156 },
  275. .p = { .min = 10, .max = 30 },
  276. .p1 = { .min = 1, .max = 3 },
  277. .p2 = { .dot_limit = 270000,
  278. .p2_slow = 2, .p2_fast = 20 },
  279. };
  280. static const intel_limit_t intel_limits_vlv_hdmi = {
  281. .dot = { .min = 25000, .max = 270000 },
  282. .vco = { .min = 4000000, .max = 6000000 },
  283. .n = { .min = 1, .max = 7 },
  284. .m = { .min = 60, .max = 300 }, /* guess */
  285. .m1 = { .min = 2, .max = 3 },
  286. .m2 = { .min = 11, .max = 156 },
  287. .p = { .min = 10, .max = 30 },
  288. .p1 = { .min = 2, .max = 3 },
  289. .p2 = { .dot_limit = 270000,
  290. .p2_slow = 2, .p2_fast = 20 },
  291. };
  292. static const intel_limit_t intel_limits_vlv_dp = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 4000000, .max = 6000000 },
  295. .n = { .min = 1, .max = 7 },
  296. .m = { .min = 22, .max = 450 },
  297. .m1 = { .min = 2, .max = 3 },
  298. .m2 = { .min = 11, .max = 156 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3 },
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 2, .p2_fast = 20 },
  303. };
  304. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  305. int refclk)
  306. {
  307. struct drm_device *dev = crtc->dev;
  308. const intel_limit_t *limit;
  309. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  310. if (intel_is_dual_link_lvds(dev)) {
  311. if (refclk == 100000)
  312. limit = &intel_limits_ironlake_dual_lvds_100m;
  313. else
  314. limit = &intel_limits_ironlake_dual_lvds;
  315. } else {
  316. if (refclk == 100000)
  317. limit = &intel_limits_ironlake_single_lvds_100m;
  318. else
  319. limit = &intel_limits_ironlake_single_lvds;
  320. }
  321. } else
  322. limit = &intel_limits_ironlake_dac;
  323. return limit;
  324. }
  325. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  326. {
  327. struct drm_device *dev = crtc->dev;
  328. const intel_limit_t *limit;
  329. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  330. if (intel_is_dual_link_lvds(dev))
  331. limit = &intel_limits_g4x_dual_channel_lvds;
  332. else
  333. limit = &intel_limits_g4x_single_channel_lvds;
  334. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  335. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  336. limit = &intel_limits_g4x_hdmi;
  337. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  338. limit = &intel_limits_g4x_sdvo;
  339. } else /* The option is for other outputs */
  340. limit = &intel_limits_i9xx_sdvo;
  341. return limit;
  342. }
  343. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  344. {
  345. struct drm_device *dev = crtc->dev;
  346. const intel_limit_t *limit;
  347. if (HAS_PCH_SPLIT(dev))
  348. limit = intel_ironlake_limit(crtc, refclk);
  349. else if (IS_G4X(dev)) {
  350. limit = intel_g4x_limit(crtc);
  351. } else if (IS_PINEVIEW(dev)) {
  352. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  353. limit = &intel_limits_pineview_lvds;
  354. else
  355. limit = &intel_limits_pineview_sdvo;
  356. } else if (IS_VALLEYVIEW(dev)) {
  357. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
  358. limit = &intel_limits_vlv_dac;
  359. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  360. limit = &intel_limits_vlv_hdmi;
  361. else
  362. limit = &intel_limits_vlv_dp;
  363. } else if (!IS_GEN2(dev)) {
  364. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  365. limit = &intel_limits_i9xx_lvds;
  366. else
  367. limit = &intel_limits_i9xx_sdvo;
  368. } else {
  369. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  370. limit = &intel_limits_i8xx_lvds;
  371. else
  372. limit = &intel_limits_i8xx_dvo;
  373. }
  374. return limit;
  375. }
  376. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  377. static void pineview_clock(int refclk, intel_clock_t *clock)
  378. {
  379. clock->m = clock->m2 + 2;
  380. clock->p = clock->p1 * clock->p2;
  381. clock->vco = refclk * clock->m / clock->n;
  382. clock->dot = clock->vco / clock->p;
  383. }
  384. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  385. {
  386. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  387. }
  388. static void i9xx_clock(int refclk, intel_clock_t *clock)
  389. {
  390. clock->m = i9xx_dpll_compute_m(clock);
  391. clock->p = clock->p1 * clock->p2;
  392. clock->vco = refclk * clock->m / (clock->n + 2);
  393. clock->dot = clock->vco / clock->p;
  394. }
  395. /**
  396. * Returns whether any output on the specified pipe is of the specified type
  397. */
  398. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  399. {
  400. struct drm_device *dev = crtc->dev;
  401. struct intel_encoder *encoder;
  402. for_each_encoder_on_crtc(dev, crtc, encoder)
  403. if (encoder->type == type)
  404. return true;
  405. return false;
  406. }
  407. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  408. /**
  409. * Returns whether the given set of divisors are valid for a given refclk with
  410. * the given connectors.
  411. */
  412. static bool intel_PLL_is_valid(struct drm_device *dev,
  413. const intel_limit_t *limit,
  414. const intel_clock_t *clock)
  415. {
  416. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  417. INTELPllInvalid("p1 out of range\n");
  418. if (clock->p < limit->p.min || limit->p.max < clock->p)
  419. INTELPllInvalid("p out of range\n");
  420. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  421. INTELPllInvalid("m2 out of range\n");
  422. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  423. INTELPllInvalid("m1 out of range\n");
  424. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  425. INTELPllInvalid("m1 <= m2\n");
  426. if (clock->m < limit->m.min || limit->m.max < clock->m)
  427. INTELPllInvalid("m out of range\n");
  428. if (clock->n < limit->n.min || limit->n.max < clock->n)
  429. INTELPllInvalid("n out of range\n");
  430. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  431. INTELPllInvalid("vco out of range\n");
  432. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  433. * connector, etc., rather than just a single range.
  434. */
  435. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  436. INTELPllInvalid("dot out of range\n");
  437. return true;
  438. }
  439. static bool
  440. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  441. int target, int refclk, intel_clock_t *match_clock,
  442. intel_clock_t *best_clock)
  443. {
  444. struct drm_device *dev = crtc->dev;
  445. intel_clock_t clock;
  446. int err = target;
  447. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  448. /*
  449. * For LVDS just rely on its current settings for dual-channel.
  450. * We haven't figured out how to reliably set up different
  451. * single/dual channel state, if we even can.
  452. */
  453. if (intel_is_dual_link_lvds(dev))
  454. clock.p2 = limit->p2.p2_fast;
  455. else
  456. clock.p2 = limit->p2.p2_slow;
  457. } else {
  458. if (target < limit->p2.dot_limit)
  459. clock.p2 = limit->p2.p2_slow;
  460. else
  461. clock.p2 = limit->p2.p2_fast;
  462. }
  463. memset(best_clock, 0, sizeof(*best_clock));
  464. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  465. clock.m1++) {
  466. for (clock.m2 = limit->m2.min;
  467. clock.m2 <= limit->m2.max; clock.m2++) {
  468. if (clock.m2 >= clock.m1)
  469. break;
  470. for (clock.n = limit->n.min;
  471. clock.n <= limit->n.max; clock.n++) {
  472. for (clock.p1 = limit->p1.min;
  473. clock.p1 <= limit->p1.max; clock.p1++) {
  474. int this_err;
  475. i9xx_clock(refclk, &clock);
  476. if (!intel_PLL_is_valid(dev, limit,
  477. &clock))
  478. continue;
  479. if (match_clock &&
  480. clock.p != match_clock->p)
  481. continue;
  482. this_err = abs(clock.dot - target);
  483. if (this_err < err) {
  484. *best_clock = clock;
  485. err = this_err;
  486. }
  487. }
  488. }
  489. }
  490. }
  491. return (err != target);
  492. }
  493. static bool
  494. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  495. int target, int refclk, intel_clock_t *match_clock,
  496. intel_clock_t *best_clock)
  497. {
  498. struct drm_device *dev = crtc->dev;
  499. intel_clock_t clock;
  500. int err = target;
  501. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  502. /*
  503. * For LVDS just rely on its current settings for dual-channel.
  504. * We haven't figured out how to reliably set up different
  505. * single/dual channel state, if we even can.
  506. */
  507. if (intel_is_dual_link_lvds(dev))
  508. clock.p2 = limit->p2.p2_fast;
  509. else
  510. clock.p2 = limit->p2.p2_slow;
  511. } else {
  512. if (target < limit->p2.dot_limit)
  513. clock.p2 = limit->p2.p2_slow;
  514. else
  515. clock.p2 = limit->p2.p2_fast;
  516. }
  517. memset(best_clock, 0, sizeof(*best_clock));
  518. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  519. clock.m1++) {
  520. for (clock.m2 = limit->m2.min;
  521. clock.m2 <= limit->m2.max; clock.m2++) {
  522. for (clock.n = limit->n.min;
  523. clock.n <= limit->n.max; clock.n++) {
  524. for (clock.p1 = limit->p1.min;
  525. clock.p1 <= limit->p1.max; clock.p1++) {
  526. int this_err;
  527. pineview_clock(refclk, &clock);
  528. if (!intel_PLL_is_valid(dev, limit,
  529. &clock))
  530. continue;
  531. if (match_clock &&
  532. clock.p != match_clock->p)
  533. continue;
  534. this_err = abs(clock.dot - target);
  535. if (this_err < err) {
  536. *best_clock = clock;
  537. err = this_err;
  538. }
  539. }
  540. }
  541. }
  542. }
  543. return (err != target);
  544. }
  545. static bool
  546. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  547. int target, int refclk, intel_clock_t *match_clock,
  548. intel_clock_t *best_clock)
  549. {
  550. struct drm_device *dev = crtc->dev;
  551. intel_clock_t clock;
  552. int max_n;
  553. bool found;
  554. /* approximately equals target * 0.00585 */
  555. int err_most = (target >> 8) + (target >> 9);
  556. found = false;
  557. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  558. if (intel_is_dual_link_lvds(dev))
  559. clock.p2 = limit->p2.p2_fast;
  560. else
  561. clock.p2 = limit->p2.p2_slow;
  562. } else {
  563. if (target < limit->p2.dot_limit)
  564. clock.p2 = limit->p2.p2_slow;
  565. else
  566. clock.p2 = limit->p2.p2_fast;
  567. }
  568. memset(best_clock, 0, sizeof(*best_clock));
  569. max_n = limit->n.max;
  570. /* based on hardware requirement, prefer smaller n to precision */
  571. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  572. /* based on hardware requirement, prefere larger m1,m2 */
  573. for (clock.m1 = limit->m1.max;
  574. clock.m1 >= limit->m1.min; clock.m1--) {
  575. for (clock.m2 = limit->m2.max;
  576. clock.m2 >= limit->m2.min; clock.m2--) {
  577. for (clock.p1 = limit->p1.max;
  578. clock.p1 >= limit->p1.min; clock.p1--) {
  579. int this_err;
  580. i9xx_clock(refclk, &clock);
  581. if (!intel_PLL_is_valid(dev, limit,
  582. &clock))
  583. continue;
  584. this_err = abs(clock.dot - target);
  585. if (this_err < err_most) {
  586. *best_clock = clock;
  587. err_most = this_err;
  588. max_n = clock.n;
  589. found = true;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. return found;
  596. }
  597. static bool
  598. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  599. int target, int refclk, intel_clock_t *match_clock,
  600. intel_clock_t *best_clock)
  601. {
  602. u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
  603. u32 m, n, fastclk;
  604. u32 updrate, minupdate, fracbits, p;
  605. unsigned long bestppm, ppm, absppm;
  606. int dotclk, flag;
  607. flag = 0;
  608. dotclk = target * 1000;
  609. bestppm = 1000000;
  610. ppm = absppm = 0;
  611. fastclk = dotclk / (2*100);
  612. updrate = 0;
  613. minupdate = 19200;
  614. fracbits = 1;
  615. n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
  616. bestm1 = bestm2 = bestp1 = bestp2 = 0;
  617. /* based on hardware requirement, prefer smaller n to precision */
  618. for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
  619. updrate = refclk / n;
  620. for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
  621. for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
  622. if (p2 > 10)
  623. p2 = p2 - 1;
  624. p = p1 * p2;
  625. /* based on hardware requirement, prefer bigger m1,m2 values */
  626. for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
  627. m2 = (((2*(fastclk * p * n / m1 )) +
  628. refclk) / (2*refclk));
  629. m = m1 * m2;
  630. vco = updrate * m;
  631. if (vco >= limit->vco.min && vco < limit->vco.max) {
  632. ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
  633. absppm = (ppm > 0) ? ppm : (-ppm);
  634. if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
  635. bestppm = 0;
  636. flag = 1;
  637. }
  638. if (absppm < bestppm - 10) {
  639. bestppm = absppm;
  640. flag = 1;
  641. }
  642. if (flag) {
  643. bestn = n;
  644. bestm1 = m1;
  645. bestm2 = m2;
  646. bestp1 = p1;
  647. bestp2 = p2;
  648. flag = 0;
  649. }
  650. }
  651. }
  652. }
  653. }
  654. }
  655. best_clock->n = bestn;
  656. best_clock->m1 = bestm1;
  657. best_clock->m2 = bestm2;
  658. best_clock->p1 = bestp1;
  659. best_clock->p2 = bestp2;
  660. return true;
  661. }
  662. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  663. enum pipe pipe)
  664. {
  665. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  667. return intel_crtc->config.cpu_transcoder;
  668. }
  669. static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
  670. {
  671. struct drm_i915_private *dev_priv = dev->dev_private;
  672. u32 frame, frame_reg = PIPEFRAME(pipe);
  673. frame = I915_READ(frame_reg);
  674. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  675. DRM_DEBUG_KMS("vblank wait timed out\n");
  676. }
  677. /**
  678. * intel_wait_for_vblank - wait for vblank on a given pipe
  679. * @dev: drm device
  680. * @pipe: pipe to wait for
  681. *
  682. * Wait for vblank to occur on a given pipe. Needed for various bits of
  683. * mode setting code.
  684. */
  685. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  686. {
  687. struct drm_i915_private *dev_priv = dev->dev_private;
  688. int pipestat_reg = PIPESTAT(pipe);
  689. if (INTEL_INFO(dev)->gen >= 5) {
  690. ironlake_wait_for_vblank(dev, pipe);
  691. return;
  692. }
  693. /* Clear existing vblank status. Note this will clear any other
  694. * sticky status fields as well.
  695. *
  696. * This races with i915_driver_irq_handler() with the result
  697. * that either function could miss a vblank event. Here it is not
  698. * fatal, as we will either wait upon the next vblank interrupt or
  699. * timeout. Generally speaking intel_wait_for_vblank() is only
  700. * called during modeset at which time the GPU should be idle and
  701. * should *not* be performing page flips and thus not waiting on
  702. * vblanks...
  703. * Currently, the result of us stealing a vblank from the irq
  704. * handler is that a single frame will be skipped during swapbuffers.
  705. */
  706. I915_WRITE(pipestat_reg,
  707. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  708. /* Wait for vblank interrupt bit to set */
  709. if (wait_for(I915_READ(pipestat_reg) &
  710. PIPE_VBLANK_INTERRUPT_STATUS,
  711. 50))
  712. DRM_DEBUG_KMS("vblank wait timed out\n");
  713. }
  714. /*
  715. * intel_wait_for_pipe_off - wait for pipe to turn off
  716. * @dev: drm device
  717. * @pipe: pipe to wait for
  718. *
  719. * After disabling a pipe, we can't wait for vblank in the usual way,
  720. * spinning on the vblank interrupt status bit, since we won't actually
  721. * see an interrupt when the pipe is disabled.
  722. *
  723. * On Gen4 and above:
  724. * wait for the pipe register state bit to turn off
  725. *
  726. * Otherwise:
  727. * wait for the display line value to settle (it usually
  728. * ends up stopping at the start of the next frame).
  729. *
  730. */
  731. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  732. {
  733. struct drm_i915_private *dev_priv = dev->dev_private;
  734. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  735. pipe);
  736. if (INTEL_INFO(dev)->gen >= 4) {
  737. int reg = PIPECONF(cpu_transcoder);
  738. /* Wait for the Pipe State to go off */
  739. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  740. 100))
  741. WARN(1, "pipe_off wait timed out\n");
  742. } else {
  743. u32 last_line, line_mask;
  744. int reg = PIPEDSL(pipe);
  745. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  746. if (IS_GEN2(dev))
  747. line_mask = DSL_LINEMASK_GEN2;
  748. else
  749. line_mask = DSL_LINEMASK_GEN3;
  750. /* Wait for the display line to settle */
  751. do {
  752. last_line = I915_READ(reg) & line_mask;
  753. mdelay(5);
  754. } while (((I915_READ(reg) & line_mask) != last_line) &&
  755. time_after(timeout, jiffies));
  756. if (time_after(jiffies, timeout))
  757. WARN(1, "pipe_off wait timed out\n");
  758. }
  759. }
  760. /*
  761. * ibx_digital_port_connected - is the specified port connected?
  762. * @dev_priv: i915 private structure
  763. * @port: the port to test
  764. *
  765. * Returns true if @port is connected, false otherwise.
  766. */
  767. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  768. struct intel_digital_port *port)
  769. {
  770. u32 bit;
  771. if (HAS_PCH_IBX(dev_priv->dev)) {
  772. switch(port->port) {
  773. case PORT_B:
  774. bit = SDE_PORTB_HOTPLUG;
  775. break;
  776. case PORT_C:
  777. bit = SDE_PORTC_HOTPLUG;
  778. break;
  779. case PORT_D:
  780. bit = SDE_PORTD_HOTPLUG;
  781. break;
  782. default:
  783. return true;
  784. }
  785. } else {
  786. switch(port->port) {
  787. case PORT_B:
  788. bit = SDE_PORTB_HOTPLUG_CPT;
  789. break;
  790. case PORT_C:
  791. bit = SDE_PORTC_HOTPLUG_CPT;
  792. break;
  793. case PORT_D:
  794. bit = SDE_PORTD_HOTPLUG_CPT;
  795. break;
  796. default:
  797. return true;
  798. }
  799. }
  800. return I915_READ(SDEISR) & bit;
  801. }
  802. static const char *state_string(bool enabled)
  803. {
  804. return enabled ? "on" : "off";
  805. }
  806. /* Only for pre-ILK configs */
  807. static void assert_pll(struct drm_i915_private *dev_priv,
  808. enum pipe pipe, bool state)
  809. {
  810. int reg;
  811. u32 val;
  812. bool cur_state;
  813. reg = DPLL(pipe);
  814. val = I915_READ(reg);
  815. cur_state = !!(val & DPLL_VCO_ENABLE);
  816. WARN(cur_state != state,
  817. "PLL state assertion failure (expected %s, current %s)\n",
  818. state_string(state), state_string(cur_state));
  819. }
  820. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  821. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  822. /* For ILK+ */
  823. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  824. struct intel_pch_pll *pll,
  825. struct intel_crtc *crtc,
  826. bool state)
  827. {
  828. u32 val;
  829. bool cur_state;
  830. if (HAS_PCH_LPT(dev_priv->dev)) {
  831. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  832. return;
  833. }
  834. if (WARN (!pll,
  835. "asserting PCH PLL %s with no PLL\n", state_string(state)))
  836. return;
  837. val = I915_READ(pll->pll_reg);
  838. cur_state = !!(val & DPLL_VCO_ENABLE);
  839. WARN(cur_state != state,
  840. "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
  841. pll->pll_reg, state_string(state), state_string(cur_state), val);
  842. /* Make sure the selected PLL is correctly attached to the transcoder */
  843. if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
  844. u32 pch_dpll;
  845. pch_dpll = I915_READ(PCH_DPLL_SEL);
  846. cur_state = pll->pll_reg == _PCH_DPLL_B;
  847. if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
  848. "PLL[%d] not attached to this transcoder %c: %08x\n",
  849. cur_state, pipe_name(crtc->pipe), pch_dpll)) {
  850. cur_state = !!(val >> (4*crtc->pipe + 3));
  851. WARN(cur_state != state,
  852. "PLL[%d] not %s on this transcoder %c: %08x\n",
  853. pll->pll_reg == _PCH_DPLL_B,
  854. state_string(state),
  855. pipe_name(crtc->pipe),
  856. val);
  857. }
  858. }
  859. }
  860. #define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
  861. #define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
  862. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  863. enum pipe pipe, bool state)
  864. {
  865. int reg;
  866. u32 val;
  867. bool cur_state;
  868. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  869. pipe);
  870. if (HAS_DDI(dev_priv->dev)) {
  871. /* DDI does not have a specific FDI_TX register */
  872. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  873. val = I915_READ(reg);
  874. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  875. } else {
  876. reg = FDI_TX_CTL(pipe);
  877. val = I915_READ(reg);
  878. cur_state = !!(val & FDI_TX_ENABLE);
  879. }
  880. WARN(cur_state != state,
  881. "FDI TX state assertion failure (expected %s, current %s)\n",
  882. state_string(state), state_string(cur_state));
  883. }
  884. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  885. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  886. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  887. enum pipe pipe, bool state)
  888. {
  889. int reg;
  890. u32 val;
  891. bool cur_state;
  892. reg = FDI_RX_CTL(pipe);
  893. val = I915_READ(reg);
  894. cur_state = !!(val & FDI_RX_ENABLE);
  895. WARN(cur_state != state,
  896. "FDI RX state assertion failure (expected %s, current %s)\n",
  897. state_string(state), state_string(cur_state));
  898. }
  899. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  900. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  901. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  902. enum pipe pipe)
  903. {
  904. int reg;
  905. u32 val;
  906. /* ILK FDI PLL is always enabled */
  907. if (dev_priv->info->gen == 5)
  908. return;
  909. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  910. if (HAS_DDI(dev_priv->dev))
  911. return;
  912. reg = FDI_TX_CTL(pipe);
  913. val = I915_READ(reg);
  914. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  915. }
  916. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  917. enum pipe pipe)
  918. {
  919. int reg;
  920. u32 val;
  921. reg = FDI_RX_CTL(pipe);
  922. val = I915_READ(reg);
  923. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  924. }
  925. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  926. enum pipe pipe)
  927. {
  928. int pp_reg, lvds_reg;
  929. u32 val;
  930. enum pipe panel_pipe = PIPE_A;
  931. bool locked = true;
  932. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  933. pp_reg = PCH_PP_CONTROL;
  934. lvds_reg = PCH_LVDS;
  935. } else {
  936. pp_reg = PP_CONTROL;
  937. lvds_reg = LVDS;
  938. }
  939. val = I915_READ(pp_reg);
  940. if (!(val & PANEL_POWER_ON) ||
  941. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  942. locked = false;
  943. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  944. panel_pipe = PIPE_B;
  945. WARN(panel_pipe == pipe && locked,
  946. "panel assertion failure, pipe %c regs locked\n",
  947. pipe_name(pipe));
  948. }
  949. void assert_pipe(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 we need the pipe A quirk it must be always on */
  958. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  959. state = true;
  960. if (!intel_display_power_enabled(dev_priv->dev,
  961. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  962. cur_state = false;
  963. } else {
  964. reg = PIPECONF(cpu_transcoder);
  965. val = I915_READ(reg);
  966. cur_state = !!(val & PIPECONF_ENABLE);
  967. }
  968. WARN(cur_state != state,
  969. "pipe %c assertion failure (expected %s, current %s)\n",
  970. pipe_name(pipe), state_string(state), state_string(cur_state));
  971. }
  972. static void assert_plane(struct drm_i915_private *dev_priv,
  973. enum plane plane, bool state)
  974. {
  975. int reg;
  976. u32 val;
  977. bool cur_state;
  978. reg = DSPCNTR(plane);
  979. val = I915_READ(reg);
  980. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  981. WARN(cur_state != state,
  982. "plane %c assertion failure (expected %s, current %s)\n",
  983. plane_name(plane), state_string(state), state_string(cur_state));
  984. }
  985. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  986. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  987. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  988. enum pipe pipe)
  989. {
  990. int reg, i;
  991. u32 val;
  992. int cur_pipe;
  993. /* Planes are fixed to pipes on ILK+ */
  994. if (HAS_PCH_SPLIT(dev_priv->dev) || IS_VALLEYVIEW(dev_priv->dev)) {
  995. reg = DSPCNTR(pipe);
  996. val = I915_READ(reg);
  997. WARN((val & DISPLAY_PLANE_ENABLE),
  998. "plane %c assertion failure, should be disabled but not\n",
  999. plane_name(pipe));
  1000. return;
  1001. }
  1002. /* Need to check both planes against the pipe */
  1003. for (i = 0; i < 2; i++) {
  1004. reg = DSPCNTR(i);
  1005. val = I915_READ(reg);
  1006. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1007. DISPPLANE_SEL_PIPE_SHIFT;
  1008. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1009. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1010. plane_name(i), pipe_name(pipe));
  1011. }
  1012. }
  1013. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1014. enum pipe pipe)
  1015. {
  1016. int reg, i;
  1017. u32 val;
  1018. if (!IS_VALLEYVIEW(dev_priv->dev))
  1019. return;
  1020. /* Need to check both planes against the pipe */
  1021. for (i = 0; i < dev_priv->num_plane; i++) {
  1022. reg = SPCNTR(pipe, i);
  1023. val = I915_READ(reg);
  1024. WARN((val & SP_ENABLE),
  1025. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1026. sprite_name(pipe, i), pipe_name(pipe));
  1027. }
  1028. }
  1029. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1030. {
  1031. u32 val;
  1032. bool enabled;
  1033. if (HAS_PCH_LPT(dev_priv->dev)) {
  1034. DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
  1035. return;
  1036. }
  1037. val = I915_READ(PCH_DREF_CONTROL);
  1038. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1039. DREF_SUPERSPREAD_SOURCE_MASK));
  1040. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1041. }
  1042. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1043. enum pipe pipe)
  1044. {
  1045. int reg;
  1046. u32 val;
  1047. bool enabled;
  1048. reg = PCH_TRANSCONF(pipe);
  1049. val = I915_READ(reg);
  1050. enabled = !!(val & TRANS_ENABLE);
  1051. WARN(enabled,
  1052. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1053. pipe_name(pipe));
  1054. }
  1055. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, u32 port_sel, u32 val)
  1057. {
  1058. if ((val & DP_PORT_EN) == 0)
  1059. return false;
  1060. if (HAS_PCH_CPT(dev_priv->dev)) {
  1061. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1062. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1063. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1064. return false;
  1065. } else {
  1066. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1067. return false;
  1068. }
  1069. return true;
  1070. }
  1071. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1072. enum pipe pipe, u32 val)
  1073. {
  1074. if ((val & SDVO_ENABLE) == 0)
  1075. return false;
  1076. if (HAS_PCH_CPT(dev_priv->dev)) {
  1077. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1078. return false;
  1079. } else {
  1080. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1081. return false;
  1082. }
  1083. return true;
  1084. }
  1085. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1086. enum pipe pipe, u32 val)
  1087. {
  1088. if ((val & LVDS_PORT_EN) == 0)
  1089. return false;
  1090. if (HAS_PCH_CPT(dev_priv->dev)) {
  1091. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1092. return false;
  1093. } else {
  1094. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1095. return false;
  1096. }
  1097. return true;
  1098. }
  1099. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1100. enum pipe pipe, u32 val)
  1101. {
  1102. if ((val & ADPA_DAC_ENABLE) == 0)
  1103. return false;
  1104. if (HAS_PCH_CPT(dev_priv->dev)) {
  1105. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1106. return false;
  1107. } else {
  1108. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1109. return false;
  1110. }
  1111. return true;
  1112. }
  1113. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1114. enum pipe pipe, int reg, u32 port_sel)
  1115. {
  1116. u32 val = I915_READ(reg);
  1117. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1118. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1119. reg, pipe_name(pipe));
  1120. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1121. && (val & DP_PIPEB_SELECT),
  1122. "IBX PCH dp port still using transcoder B\n");
  1123. }
  1124. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1125. enum pipe pipe, int reg)
  1126. {
  1127. u32 val = I915_READ(reg);
  1128. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1129. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1130. reg, pipe_name(pipe));
  1131. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1132. && (val & SDVO_PIPE_B_SELECT),
  1133. "IBX PCH hdmi port still using transcoder B\n");
  1134. }
  1135. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1136. enum pipe pipe)
  1137. {
  1138. int reg;
  1139. u32 val;
  1140. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1141. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1142. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1143. reg = PCH_ADPA;
  1144. val = I915_READ(reg);
  1145. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1146. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1147. pipe_name(pipe));
  1148. reg = PCH_LVDS;
  1149. val = I915_READ(reg);
  1150. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1151. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1152. pipe_name(pipe));
  1153. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1154. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1155. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1156. }
  1157. /**
  1158. * intel_enable_pll - enable a PLL
  1159. * @dev_priv: i915 private structure
  1160. * @pipe: pipe PLL to enable
  1161. *
  1162. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  1163. * make sure the PLL reg is writable first though, since the panel write
  1164. * protect mechanism may be enabled.
  1165. *
  1166. * Note! This is for pre-ILK only.
  1167. *
  1168. * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
  1169. */
  1170. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1171. {
  1172. int reg;
  1173. u32 val;
  1174. assert_pipe_disabled(dev_priv, pipe);
  1175. /* No really, not for ILK+ */
  1176. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
  1177. /* PLL is protected by panel, make sure we can write it */
  1178. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1179. assert_panel_unlocked(dev_priv, pipe);
  1180. reg = DPLL(pipe);
  1181. val = I915_READ(reg);
  1182. val |= DPLL_VCO_ENABLE;
  1183. /* We do this three times for luck */
  1184. I915_WRITE(reg, val);
  1185. POSTING_READ(reg);
  1186. udelay(150); /* wait for warmup */
  1187. I915_WRITE(reg, val);
  1188. POSTING_READ(reg);
  1189. udelay(150); /* wait for warmup */
  1190. I915_WRITE(reg, val);
  1191. POSTING_READ(reg);
  1192. udelay(150); /* wait for warmup */
  1193. }
  1194. /**
  1195. * intel_disable_pll - disable a PLL
  1196. * @dev_priv: i915 private structure
  1197. * @pipe: pipe PLL to disable
  1198. *
  1199. * Disable the PLL for @pipe, making sure the pipe is off first.
  1200. *
  1201. * Note! This is for pre-ILK only.
  1202. */
  1203. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1204. {
  1205. int reg;
  1206. u32 val;
  1207. /* Don't disable pipe A or pipe A PLLs if needed */
  1208. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1209. return;
  1210. /* Make sure the pipe isn't still relying on us */
  1211. assert_pipe_disabled(dev_priv, pipe);
  1212. reg = DPLL(pipe);
  1213. val = I915_READ(reg);
  1214. val &= ~DPLL_VCO_ENABLE;
  1215. I915_WRITE(reg, val);
  1216. POSTING_READ(reg);
  1217. }
  1218. void vlv_wait_port_ready(struct drm_i915_private *dev_priv, int port)
  1219. {
  1220. u32 port_mask;
  1221. if (!port)
  1222. port_mask = DPLL_PORTB_READY_MASK;
  1223. else
  1224. port_mask = DPLL_PORTC_READY_MASK;
  1225. if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
  1226. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1227. 'B' + port, I915_READ(DPLL(0)));
  1228. }
  1229. /**
  1230. * ironlake_enable_pch_pll - enable PCH PLL
  1231. * @dev_priv: i915 private structure
  1232. * @pipe: pipe PLL to enable
  1233. *
  1234. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1235. * drives the transcoder clock.
  1236. */
  1237. static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
  1238. {
  1239. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1240. struct intel_pch_pll *pll;
  1241. int reg;
  1242. u32 val;
  1243. /* PCH PLLs only available on ILK, SNB and IVB */
  1244. BUG_ON(dev_priv->info->gen < 5);
  1245. pll = intel_crtc->pch_pll;
  1246. if (pll == NULL)
  1247. return;
  1248. if (WARN_ON(pll->refcount == 0))
  1249. return;
  1250. DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
  1251. pll->pll_reg, pll->active, pll->on,
  1252. intel_crtc->base.base.id);
  1253. /* PCH refclock must be enabled first */
  1254. assert_pch_refclk_enabled(dev_priv);
  1255. if (pll->active++ && pll->on) {
  1256. assert_pch_pll_enabled(dev_priv, pll, NULL);
  1257. return;
  1258. }
  1259. DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
  1260. reg = pll->pll_reg;
  1261. val = I915_READ(reg);
  1262. val |= DPLL_VCO_ENABLE;
  1263. I915_WRITE(reg, val);
  1264. POSTING_READ(reg);
  1265. udelay(200);
  1266. pll->on = true;
  1267. }
  1268. static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
  1269. {
  1270. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1271. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  1272. int reg;
  1273. u32 val;
  1274. /* PCH only available on ILK+ */
  1275. BUG_ON(dev_priv->info->gen < 5);
  1276. if (pll == NULL)
  1277. return;
  1278. if (WARN_ON(pll->refcount == 0))
  1279. return;
  1280. DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
  1281. pll->pll_reg, pll->active, pll->on,
  1282. intel_crtc->base.base.id);
  1283. if (WARN_ON(pll->active == 0)) {
  1284. assert_pch_pll_disabled(dev_priv, pll, NULL);
  1285. return;
  1286. }
  1287. if (--pll->active) {
  1288. assert_pch_pll_enabled(dev_priv, pll, NULL);
  1289. return;
  1290. }
  1291. DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
  1292. /* Make sure transcoder isn't still depending on us */
  1293. assert_pch_transcoder_disabled(dev_priv, intel_crtc->pipe);
  1294. reg = pll->pll_reg;
  1295. val = I915_READ(reg);
  1296. val &= ~DPLL_VCO_ENABLE;
  1297. I915_WRITE(reg, val);
  1298. POSTING_READ(reg);
  1299. udelay(200);
  1300. pll->on = false;
  1301. }
  1302. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1303. enum pipe pipe)
  1304. {
  1305. struct drm_device *dev = dev_priv->dev;
  1306. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1307. uint32_t reg, val, pipeconf_val;
  1308. /* PCH only available on ILK+ */
  1309. BUG_ON(dev_priv->info->gen < 5);
  1310. /* Make sure PCH DPLL is enabled */
  1311. assert_pch_pll_enabled(dev_priv,
  1312. to_intel_crtc(crtc)->pch_pll,
  1313. to_intel_crtc(crtc));
  1314. /* FDI must be feeding us bits for PCH ports */
  1315. assert_fdi_tx_enabled(dev_priv, pipe);
  1316. assert_fdi_rx_enabled(dev_priv, pipe);
  1317. if (HAS_PCH_CPT(dev)) {
  1318. /* Workaround: Set the timing override bit before enabling the
  1319. * pch transcoder. */
  1320. reg = TRANS_CHICKEN2(pipe);
  1321. val = I915_READ(reg);
  1322. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1323. I915_WRITE(reg, val);
  1324. }
  1325. reg = PCH_TRANSCONF(pipe);
  1326. val = I915_READ(reg);
  1327. pipeconf_val = I915_READ(PIPECONF(pipe));
  1328. if (HAS_PCH_IBX(dev_priv->dev)) {
  1329. /*
  1330. * make the BPC in transcoder be consistent with
  1331. * that in pipeconf reg.
  1332. */
  1333. val &= ~PIPECONF_BPC_MASK;
  1334. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1335. }
  1336. val &= ~TRANS_INTERLACE_MASK;
  1337. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1338. if (HAS_PCH_IBX(dev_priv->dev) &&
  1339. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1340. val |= TRANS_LEGACY_INTERLACED_ILK;
  1341. else
  1342. val |= TRANS_INTERLACED;
  1343. else
  1344. val |= TRANS_PROGRESSIVE;
  1345. I915_WRITE(reg, val | TRANS_ENABLE);
  1346. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1347. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1348. }
  1349. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1350. enum transcoder cpu_transcoder)
  1351. {
  1352. u32 val, pipeconf_val;
  1353. /* PCH only available on ILK+ */
  1354. BUG_ON(dev_priv->info->gen < 5);
  1355. /* FDI must be feeding us bits for PCH ports */
  1356. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1357. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1358. /* Workaround: set timing override bit. */
  1359. val = I915_READ(_TRANSA_CHICKEN2);
  1360. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1361. I915_WRITE(_TRANSA_CHICKEN2, val);
  1362. val = TRANS_ENABLE;
  1363. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1364. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1365. PIPECONF_INTERLACED_ILK)
  1366. val |= TRANS_INTERLACED;
  1367. else
  1368. val |= TRANS_PROGRESSIVE;
  1369. I915_WRITE(LPT_TRANSCONF, val);
  1370. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1371. DRM_ERROR("Failed to enable PCH transcoder\n");
  1372. }
  1373. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1374. enum pipe pipe)
  1375. {
  1376. struct drm_device *dev = dev_priv->dev;
  1377. uint32_t reg, val;
  1378. /* FDI relies on the transcoder */
  1379. assert_fdi_tx_disabled(dev_priv, pipe);
  1380. assert_fdi_rx_disabled(dev_priv, pipe);
  1381. /* Ports must be off as well */
  1382. assert_pch_ports_disabled(dev_priv, pipe);
  1383. reg = PCH_TRANSCONF(pipe);
  1384. val = I915_READ(reg);
  1385. val &= ~TRANS_ENABLE;
  1386. I915_WRITE(reg, val);
  1387. /* wait for PCH transcoder off, transcoder state */
  1388. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1389. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1390. if (!HAS_PCH_IBX(dev)) {
  1391. /* Workaround: Clear the timing override chicken bit again. */
  1392. reg = TRANS_CHICKEN2(pipe);
  1393. val = I915_READ(reg);
  1394. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1395. I915_WRITE(reg, val);
  1396. }
  1397. }
  1398. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1399. {
  1400. u32 val;
  1401. val = I915_READ(LPT_TRANSCONF);
  1402. val &= ~TRANS_ENABLE;
  1403. I915_WRITE(LPT_TRANSCONF, val);
  1404. /* wait for PCH transcoder off, transcoder state */
  1405. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1406. DRM_ERROR("Failed to disable PCH transcoder\n");
  1407. /* Workaround: clear timing override bit. */
  1408. val = I915_READ(_TRANSA_CHICKEN2);
  1409. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1410. I915_WRITE(_TRANSA_CHICKEN2, val);
  1411. }
  1412. /**
  1413. * intel_enable_pipe - enable a pipe, asserting requirements
  1414. * @dev_priv: i915 private structure
  1415. * @pipe: pipe to enable
  1416. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1417. *
  1418. * Enable @pipe, making sure that various hardware specific requirements
  1419. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1420. *
  1421. * @pipe should be %PIPE_A or %PIPE_B.
  1422. *
  1423. * Will wait until the pipe is actually running (i.e. first vblank) before
  1424. * returning.
  1425. */
  1426. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1427. bool pch_port)
  1428. {
  1429. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1430. pipe);
  1431. enum pipe pch_transcoder;
  1432. int reg;
  1433. u32 val;
  1434. assert_planes_disabled(dev_priv, pipe);
  1435. assert_sprites_disabled(dev_priv, pipe);
  1436. if (HAS_PCH_LPT(dev_priv->dev))
  1437. pch_transcoder = TRANSCODER_A;
  1438. else
  1439. pch_transcoder = pipe;
  1440. /*
  1441. * A pipe without a PLL won't actually be able to drive bits from
  1442. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1443. * need the check.
  1444. */
  1445. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1446. assert_pll_enabled(dev_priv, pipe);
  1447. else {
  1448. if (pch_port) {
  1449. /* if driving the PCH, we need FDI enabled */
  1450. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1451. assert_fdi_tx_pll_enabled(dev_priv,
  1452. (enum pipe) cpu_transcoder);
  1453. }
  1454. /* FIXME: assert CPU port conditions for SNB+ */
  1455. }
  1456. reg = PIPECONF(cpu_transcoder);
  1457. val = I915_READ(reg);
  1458. if (val & PIPECONF_ENABLE)
  1459. return;
  1460. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1461. intel_wait_for_vblank(dev_priv->dev, pipe);
  1462. }
  1463. /**
  1464. * intel_disable_pipe - disable a pipe, asserting requirements
  1465. * @dev_priv: i915 private structure
  1466. * @pipe: pipe to disable
  1467. *
  1468. * Disable @pipe, making sure that various hardware specific requirements
  1469. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1470. *
  1471. * @pipe should be %PIPE_A or %PIPE_B.
  1472. *
  1473. * Will wait until the pipe has shut down before returning.
  1474. */
  1475. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1476. enum pipe pipe)
  1477. {
  1478. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1479. pipe);
  1480. int reg;
  1481. u32 val;
  1482. /*
  1483. * Make sure planes won't keep trying to pump pixels to us,
  1484. * or we might hang the display.
  1485. */
  1486. assert_planes_disabled(dev_priv, pipe);
  1487. assert_sprites_disabled(dev_priv, pipe);
  1488. /* Don't disable pipe A or pipe A PLLs if needed */
  1489. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1490. return;
  1491. reg = PIPECONF(cpu_transcoder);
  1492. val = I915_READ(reg);
  1493. if ((val & PIPECONF_ENABLE) == 0)
  1494. return;
  1495. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1496. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1497. }
  1498. /*
  1499. * Plane regs are double buffered, going from enabled->disabled needs a
  1500. * trigger in order to latch. The display address reg provides this.
  1501. */
  1502. void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1503. enum plane plane)
  1504. {
  1505. if (dev_priv->info->gen >= 4)
  1506. I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
  1507. else
  1508. I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
  1509. }
  1510. /**
  1511. * intel_enable_plane - enable a display plane on a given pipe
  1512. * @dev_priv: i915 private structure
  1513. * @plane: plane to enable
  1514. * @pipe: pipe being fed
  1515. *
  1516. * Enable @plane on @pipe, making sure that @pipe is running first.
  1517. */
  1518. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1519. enum plane plane, enum pipe pipe)
  1520. {
  1521. int reg;
  1522. u32 val;
  1523. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1524. assert_pipe_enabled(dev_priv, pipe);
  1525. reg = DSPCNTR(plane);
  1526. val = I915_READ(reg);
  1527. if (val & DISPLAY_PLANE_ENABLE)
  1528. return;
  1529. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1530. intel_flush_display_plane(dev_priv, plane);
  1531. intel_wait_for_vblank(dev_priv->dev, pipe);
  1532. }
  1533. /**
  1534. * intel_disable_plane - disable a display plane
  1535. * @dev_priv: i915 private structure
  1536. * @plane: plane to disable
  1537. * @pipe: pipe consuming the data
  1538. *
  1539. * Disable @plane; should be an independent operation.
  1540. */
  1541. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1542. enum plane plane, enum pipe pipe)
  1543. {
  1544. int reg;
  1545. u32 val;
  1546. reg = DSPCNTR(plane);
  1547. val = I915_READ(reg);
  1548. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1549. return;
  1550. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1551. intel_flush_display_plane(dev_priv, plane);
  1552. intel_wait_for_vblank(dev_priv->dev, pipe);
  1553. }
  1554. static bool need_vtd_wa(struct drm_device *dev)
  1555. {
  1556. #ifdef CONFIG_INTEL_IOMMU
  1557. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1558. return true;
  1559. #endif
  1560. return false;
  1561. }
  1562. int
  1563. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1564. struct drm_i915_gem_object *obj,
  1565. struct intel_ring_buffer *pipelined)
  1566. {
  1567. struct drm_i915_private *dev_priv = dev->dev_private;
  1568. u32 alignment;
  1569. int ret;
  1570. switch (obj->tiling_mode) {
  1571. case I915_TILING_NONE:
  1572. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1573. alignment = 128 * 1024;
  1574. else if (INTEL_INFO(dev)->gen >= 4)
  1575. alignment = 4 * 1024;
  1576. else
  1577. alignment = 64 * 1024;
  1578. break;
  1579. case I915_TILING_X:
  1580. /* pin() will align the object as required by fence */
  1581. alignment = 0;
  1582. break;
  1583. case I915_TILING_Y:
  1584. /* Despite that we check this in framebuffer_init userspace can
  1585. * screw us over and change the tiling after the fact. Only
  1586. * pinned buffers can't change their tiling. */
  1587. DRM_DEBUG_DRIVER("Y tiled not allowed for scan out buffers\n");
  1588. return -EINVAL;
  1589. default:
  1590. BUG();
  1591. }
  1592. /* Note that the w/a also requires 64 PTE of padding following the
  1593. * bo. We currently fill all unused PTE with the shadow page and so
  1594. * we should always have valid PTE following the scanout preventing
  1595. * the VT-d warning.
  1596. */
  1597. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1598. alignment = 256 * 1024;
  1599. dev_priv->mm.interruptible = false;
  1600. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1601. if (ret)
  1602. goto err_interruptible;
  1603. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1604. * fence, whereas 965+ only requires a fence if using
  1605. * framebuffer compression. For simplicity, we always install
  1606. * a fence as the cost is not that onerous.
  1607. */
  1608. ret = i915_gem_object_get_fence(obj);
  1609. if (ret)
  1610. goto err_unpin;
  1611. i915_gem_object_pin_fence(obj);
  1612. dev_priv->mm.interruptible = true;
  1613. return 0;
  1614. err_unpin:
  1615. i915_gem_object_unpin(obj);
  1616. err_interruptible:
  1617. dev_priv->mm.interruptible = true;
  1618. return ret;
  1619. }
  1620. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1621. {
  1622. i915_gem_object_unpin_fence(obj);
  1623. i915_gem_object_unpin(obj);
  1624. }
  1625. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1626. * is assumed to be a power-of-two. */
  1627. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1628. unsigned int tiling_mode,
  1629. unsigned int cpp,
  1630. unsigned int pitch)
  1631. {
  1632. if (tiling_mode != I915_TILING_NONE) {
  1633. unsigned int tile_rows, tiles;
  1634. tile_rows = *y / 8;
  1635. *y %= 8;
  1636. tiles = *x / (512/cpp);
  1637. *x %= 512/cpp;
  1638. return tile_rows * pitch * 8 + tiles * 4096;
  1639. } else {
  1640. unsigned int offset;
  1641. offset = *y * pitch + *x * cpp;
  1642. *y = 0;
  1643. *x = (offset & 4095) / cpp;
  1644. return offset & -4096;
  1645. }
  1646. }
  1647. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1648. int x, int y)
  1649. {
  1650. struct drm_device *dev = crtc->dev;
  1651. struct drm_i915_private *dev_priv = dev->dev_private;
  1652. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1653. struct intel_framebuffer *intel_fb;
  1654. struct drm_i915_gem_object *obj;
  1655. int plane = intel_crtc->plane;
  1656. unsigned long linear_offset;
  1657. u32 dspcntr;
  1658. u32 reg;
  1659. switch (plane) {
  1660. case 0:
  1661. case 1:
  1662. break;
  1663. default:
  1664. DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
  1665. return -EINVAL;
  1666. }
  1667. intel_fb = to_intel_framebuffer(fb);
  1668. obj = intel_fb->obj;
  1669. reg = DSPCNTR(plane);
  1670. dspcntr = I915_READ(reg);
  1671. /* Mask out pixel format bits in case we change it */
  1672. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1673. switch (fb->pixel_format) {
  1674. case DRM_FORMAT_C8:
  1675. dspcntr |= DISPPLANE_8BPP;
  1676. break;
  1677. case DRM_FORMAT_XRGB1555:
  1678. case DRM_FORMAT_ARGB1555:
  1679. dspcntr |= DISPPLANE_BGRX555;
  1680. break;
  1681. case DRM_FORMAT_RGB565:
  1682. dspcntr |= DISPPLANE_BGRX565;
  1683. break;
  1684. case DRM_FORMAT_XRGB8888:
  1685. case DRM_FORMAT_ARGB8888:
  1686. dspcntr |= DISPPLANE_BGRX888;
  1687. break;
  1688. case DRM_FORMAT_XBGR8888:
  1689. case DRM_FORMAT_ABGR8888:
  1690. dspcntr |= DISPPLANE_RGBX888;
  1691. break;
  1692. case DRM_FORMAT_XRGB2101010:
  1693. case DRM_FORMAT_ARGB2101010:
  1694. dspcntr |= DISPPLANE_BGRX101010;
  1695. break;
  1696. case DRM_FORMAT_XBGR2101010:
  1697. case DRM_FORMAT_ABGR2101010:
  1698. dspcntr |= DISPPLANE_RGBX101010;
  1699. break;
  1700. default:
  1701. BUG();
  1702. }
  1703. if (INTEL_INFO(dev)->gen >= 4) {
  1704. if (obj->tiling_mode != I915_TILING_NONE)
  1705. dspcntr |= DISPPLANE_TILED;
  1706. else
  1707. dspcntr &= ~DISPPLANE_TILED;
  1708. }
  1709. I915_WRITE(reg, dspcntr);
  1710. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1711. if (INTEL_INFO(dev)->gen >= 4) {
  1712. intel_crtc->dspaddr_offset =
  1713. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  1714. fb->bits_per_pixel / 8,
  1715. fb->pitches[0]);
  1716. linear_offset -= intel_crtc->dspaddr_offset;
  1717. } else {
  1718. intel_crtc->dspaddr_offset = linear_offset;
  1719. }
  1720. DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
  1721. obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
  1722. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1723. if (INTEL_INFO(dev)->gen >= 4) {
  1724. I915_MODIFY_DISPBASE(DSPSURF(plane),
  1725. obj->gtt_offset + intel_crtc->dspaddr_offset);
  1726. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1727. I915_WRITE(DSPLINOFF(plane), linear_offset);
  1728. } else
  1729. I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
  1730. POSTING_READ(reg);
  1731. return 0;
  1732. }
  1733. static int ironlake_update_plane(struct drm_crtc *crtc,
  1734. struct drm_framebuffer *fb, int x, int y)
  1735. {
  1736. struct drm_device *dev = crtc->dev;
  1737. struct drm_i915_private *dev_priv = dev->dev_private;
  1738. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1739. struct intel_framebuffer *intel_fb;
  1740. struct drm_i915_gem_object *obj;
  1741. int plane = intel_crtc->plane;
  1742. unsigned long linear_offset;
  1743. u32 dspcntr;
  1744. u32 reg;
  1745. switch (plane) {
  1746. case 0:
  1747. case 1:
  1748. case 2:
  1749. break;
  1750. default:
  1751. DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
  1752. return -EINVAL;
  1753. }
  1754. intel_fb = to_intel_framebuffer(fb);
  1755. obj = intel_fb->obj;
  1756. reg = DSPCNTR(plane);
  1757. dspcntr = I915_READ(reg);
  1758. /* Mask out pixel format bits in case we change it */
  1759. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1760. switch (fb->pixel_format) {
  1761. case DRM_FORMAT_C8:
  1762. dspcntr |= DISPPLANE_8BPP;
  1763. break;
  1764. case DRM_FORMAT_RGB565:
  1765. dspcntr |= DISPPLANE_BGRX565;
  1766. break;
  1767. case DRM_FORMAT_XRGB8888:
  1768. case DRM_FORMAT_ARGB8888:
  1769. dspcntr |= DISPPLANE_BGRX888;
  1770. break;
  1771. case DRM_FORMAT_XBGR8888:
  1772. case DRM_FORMAT_ABGR8888:
  1773. dspcntr |= DISPPLANE_RGBX888;
  1774. break;
  1775. case DRM_FORMAT_XRGB2101010:
  1776. case DRM_FORMAT_ARGB2101010:
  1777. dspcntr |= DISPPLANE_BGRX101010;
  1778. break;
  1779. case DRM_FORMAT_XBGR2101010:
  1780. case DRM_FORMAT_ABGR2101010:
  1781. dspcntr |= DISPPLANE_RGBX101010;
  1782. break;
  1783. default:
  1784. BUG();
  1785. }
  1786. if (obj->tiling_mode != I915_TILING_NONE)
  1787. dspcntr |= DISPPLANE_TILED;
  1788. else
  1789. dspcntr &= ~DISPPLANE_TILED;
  1790. /* must disable */
  1791. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1792. I915_WRITE(reg, dspcntr);
  1793. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1794. intel_crtc->dspaddr_offset =
  1795. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  1796. fb->bits_per_pixel / 8,
  1797. fb->pitches[0]);
  1798. linear_offset -= intel_crtc->dspaddr_offset;
  1799. DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
  1800. obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
  1801. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1802. I915_MODIFY_DISPBASE(DSPSURF(plane),
  1803. obj->gtt_offset + intel_crtc->dspaddr_offset);
  1804. if (IS_HASWELL(dev)) {
  1805. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  1806. } else {
  1807. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1808. I915_WRITE(DSPLINOFF(plane), linear_offset);
  1809. }
  1810. POSTING_READ(reg);
  1811. return 0;
  1812. }
  1813. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1814. static int
  1815. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1816. int x, int y, enum mode_set_atomic state)
  1817. {
  1818. struct drm_device *dev = crtc->dev;
  1819. struct drm_i915_private *dev_priv = dev->dev_private;
  1820. if (dev_priv->display.disable_fbc)
  1821. dev_priv->display.disable_fbc(dev);
  1822. intel_increase_pllclock(crtc);
  1823. return dev_priv->display.update_plane(crtc, fb, x, y);
  1824. }
  1825. void intel_display_handle_reset(struct drm_device *dev)
  1826. {
  1827. struct drm_i915_private *dev_priv = dev->dev_private;
  1828. struct drm_crtc *crtc;
  1829. /*
  1830. * Flips in the rings have been nuked by the reset,
  1831. * so complete all pending flips so that user space
  1832. * will get its events and not get stuck.
  1833. *
  1834. * Also update the base address of all primary
  1835. * planes to the the last fb to make sure we're
  1836. * showing the correct fb after a reset.
  1837. *
  1838. * Need to make two loops over the crtcs so that we
  1839. * don't try to grab a crtc mutex before the
  1840. * pending_flip_queue really got woken up.
  1841. */
  1842. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1843. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1844. enum plane plane = intel_crtc->plane;
  1845. intel_prepare_page_flip(dev, plane);
  1846. intel_finish_page_flip_plane(dev, plane);
  1847. }
  1848. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1849. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1850. mutex_lock(&crtc->mutex);
  1851. if (intel_crtc->active)
  1852. dev_priv->display.update_plane(crtc, crtc->fb,
  1853. crtc->x, crtc->y);
  1854. mutex_unlock(&crtc->mutex);
  1855. }
  1856. }
  1857. static int
  1858. intel_finish_fb(struct drm_framebuffer *old_fb)
  1859. {
  1860. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1861. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1862. bool was_interruptible = dev_priv->mm.interruptible;
  1863. int ret;
  1864. /* Big Hammer, we also need to ensure that any pending
  1865. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1866. * current scanout is retired before unpinning the old
  1867. * framebuffer.
  1868. *
  1869. * This should only fail upon a hung GPU, in which case we
  1870. * can safely continue.
  1871. */
  1872. dev_priv->mm.interruptible = false;
  1873. ret = i915_gem_object_finish_gpu(obj);
  1874. dev_priv->mm.interruptible = was_interruptible;
  1875. return ret;
  1876. }
  1877. static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
  1878. {
  1879. struct drm_device *dev = crtc->dev;
  1880. struct drm_i915_master_private *master_priv;
  1881. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1882. if (!dev->primary->master)
  1883. return;
  1884. master_priv = dev->primary->master->driver_priv;
  1885. if (!master_priv->sarea_priv)
  1886. return;
  1887. switch (intel_crtc->pipe) {
  1888. case 0:
  1889. master_priv->sarea_priv->pipeA_x = x;
  1890. master_priv->sarea_priv->pipeA_y = y;
  1891. break;
  1892. case 1:
  1893. master_priv->sarea_priv->pipeB_x = x;
  1894. master_priv->sarea_priv->pipeB_y = y;
  1895. break;
  1896. default:
  1897. break;
  1898. }
  1899. }
  1900. static int
  1901. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1902. struct drm_framebuffer *fb)
  1903. {
  1904. struct drm_device *dev = crtc->dev;
  1905. struct drm_i915_private *dev_priv = dev->dev_private;
  1906. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1907. struct drm_framebuffer *old_fb;
  1908. int ret;
  1909. /* no fb bound */
  1910. if (!fb) {
  1911. DRM_ERROR("No FB bound\n");
  1912. return 0;
  1913. }
  1914. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  1915. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  1916. plane_name(intel_crtc->plane),
  1917. INTEL_INFO(dev)->num_pipes);
  1918. return -EINVAL;
  1919. }
  1920. mutex_lock(&dev->struct_mutex);
  1921. ret = intel_pin_and_fence_fb_obj(dev,
  1922. to_intel_framebuffer(fb)->obj,
  1923. NULL);
  1924. if (ret != 0) {
  1925. mutex_unlock(&dev->struct_mutex);
  1926. DRM_ERROR("pin & fence failed\n");
  1927. return ret;
  1928. }
  1929. ret = dev_priv->display.update_plane(crtc, fb, x, y);
  1930. if (ret) {
  1931. intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
  1932. mutex_unlock(&dev->struct_mutex);
  1933. DRM_ERROR("failed to update base address\n");
  1934. return ret;
  1935. }
  1936. old_fb = crtc->fb;
  1937. crtc->fb = fb;
  1938. crtc->x = x;
  1939. crtc->y = y;
  1940. if (old_fb) {
  1941. if (intel_crtc->active && old_fb != fb)
  1942. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1943. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  1944. }
  1945. intel_update_fbc(dev);
  1946. mutex_unlock(&dev->struct_mutex);
  1947. intel_crtc_update_sarea_pos(crtc, x, y);
  1948. return 0;
  1949. }
  1950. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1951. {
  1952. struct drm_device *dev = crtc->dev;
  1953. struct drm_i915_private *dev_priv = dev->dev_private;
  1954. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1955. int pipe = intel_crtc->pipe;
  1956. u32 reg, temp;
  1957. /* enable normal train */
  1958. reg = FDI_TX_CTL(pipe);
  1959. temp = I915_READ(reg);
  1960. if (IS_IVYBRIDGE(dev)) {
  1961. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  1962. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  1963. } else {
  1964. temp &= ~FDI_LINK_TRAIN_NONE;
  1965. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1966. }
  1967. I915_WRITE(reg, temp);
  1968. reg = FDI_RX_CTL(pipe);
  1969. temp = I915_READ(reg);
  1970. if (HAS_PCH_CPT(dev)) {
  1971. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1972. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1973. } else {
  1974. temp &= ~FDI_LINK_TRAIN_NONE;
  1975. temp |= FDI_LINK_TRAIN_NONE;
  1976. }
  1977. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1978. /* wait one idle pattern time */
  1979. POSTING_READ(reg);
  1980. udelay(1000);
  1981. /* IVB wants error correction enabled */
  1982. if (IS_IVYBRIDGE(dev))
  1983. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  1984. FDI_FE_ERRC_ENABLE);
  1985. }
  1986. static bool pipe_has_enabled_pch(struct intel_crtc *intel_crtc)
  1987. {
  1988. return intel_crtc->base.enabled && intel_crtc->config.has_pch_encoder;
  1989. }
  1990. static void ivb_modeset_global_resources(struct drm_device *dev)
  1991. {
  1992. struct drm_i915_private *dev_priv = dev->dev_private;
  1993. struct intel_crtc *pipe_B_crtc =
  1994. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  1995. struct intel_crtc *pipe_C_crtc =
  1996. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  1997. uint32_t temp;
  1998. /*
  1999. * When everything is off disable fdi C so that we could enable fdi B
  2000. * with all lanes. Note that we don't care about enabled pipes without
  2001. * an enabled pch encoder.
  2002. */
  2003. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2004. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2005. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2006. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2007. temp = I915_READ(SOUTH_CHICKEN1);
  2008. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2009. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2010. I915_WRITE(SOUTH_CHICKEN1, temp);
  2011. }
  2012. }
  2013. /* The FDI link training functions for ILK/Ibexpeak. */
  2014. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2015. {
  2016. struct drm_device *dev = crtc->dev;
  2017. struct drm_i915_private *dev_priv = dev->dev_private;
  2018. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2019. int pipe = intel_crtc->pipe;
  2020. int plane = intel_crtc->plane;
  2021. u32 reg, temp, tries;
  2022. /* FDI needs bits from pipe & plane first */
  2023. assert_pipe_enabled(dev_priv, pipe);
  2024. assert_plane_enabled(dev_priv, plane);
  2025. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2026. for train result */
  2027. reg = FDI_RX_IMR(pipe);
  2028. temp = I915_READ(reg);
  2029. temp &= ~FDI_RX_SYMBOL_LOCK;
  2030. temp &= ~FDI_RX_BIT_LOCK;
  2031. I915_WRITE(reg, temp);
  2032. I915_READ(reg);
  2033. udelay(150);
  2034. /* enable CPU FDI TX and PCH FDI RX */
  2035. reg = FDI_TX_CTL(pipe);
  2036. temp = I915_READ(reg);
  2037. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2038. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2039. temp &= ~FDI_LINK_TRAIN_NONE;
  2040. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2041. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2042. reg = FDI_RX_CTL(pipe);
  2043. temp = I915_READ(reg);
  2044. temp &= ~FDI_LINK_TRAIN_NONE;
  2045. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2046. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2047. POSTING_READ(reg);
  2048. udelay(150);
  2049. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2050. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2051. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2052. FDI_RX_PHASE_SYNC_POINTER_EN);
  2053. reg = FDI_RX_IIR(pipe);
  2054. for (tries = 0; tries < 5; tries++) {
  2055. temp = I915_READ(reg);
  2056. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2057. if ((temp & FDI_RX_BIT_LOCK)) {
  2058. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2059. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2060. break;
  2061. }
  2062. }
  2063. if (tries == 5)
  2064. DRM_ERROR("FDI train 1 fail!\n");
  2065. /* Train 2 */
  2066. reg = FDI_TX_CTL(pipe);
  2067. temp = I915_READ(reg);
  2068. temp &= ~FDI_LINK_TRAIN_NONE;
  2069. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2070. I915_WRITE(reg, temp);
  2071. reg = FDI_RX_CTL(pipe);
  2072. temp = I915_READ(reg);
  2073. temp &= ~FDI_LINK_TRAIN_NONE;
  2074. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2075. I915_WRITE(reg, temp);
  2076. POSTING_READ(reg);
  2077. udelay(150);
  2078. reg = FDI_RX_IIR(pipe);
  2079. for (tries = 0; tries < 5; tries++) {
  2080. temp = I915_READ(reg);
  2081. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2082. if (temp & FDI_RX_SYMBOL_LOCK) {
  2083. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2084. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2085. break;
  2086. }
  2087. }
  2088. if (tries == 5)
  2089. DRM_ERROR("FDI train 2 fail!\n");
  2090. DRM_DEBUG_KMS("FDI train done\n");
  2091. }
  2092. static const int snb_b_fdi_train_param[] = {
  2093. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2094. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2095. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2096. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2097. };
  2098. /* The FDI link training functions for SNB/Cougarpoint. */
  2099. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2100. {
  2101. struct drm_device *dev = crtc->dev;
  2102. struct drm_i915_private *dev_priv = dev->dev_private;
  2103. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2104. int pipe = intel_crtc->pipe;
  2105. u32 reg, temp, i, retry;
  2106. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2107. for train result */
  2108. reg = FDI_RX_IMR(pipe);
  2109. temp = I915_READ(reg);
  2110. temp &= ~FDI_RX_SYMBOL_LOCK;
  2111. temp &= ~FDI_RX_BIT_LOCK;
  2112. I915_WRITE(reg, temp);
  2113. POSTING_READ(reg);
  2114. udelay(150);
  2115. /* enable CPU FDI TX and PCH FDI RX */
  2116. reg = FDI_TX_CTL(pipe);
  2117. temp = I915_READ(reg);
  2118. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2119. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2120. temp &= ~FDI_LINK_TRAIN_NONE;
  2121. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2122. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2123. /* SNB-B */
  2124. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2125. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2126. I915_WRITE(FDI_RX_MISC(pipe),
  2127. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2128. reg = FDI_RX_CTL(pipe);
  2129. temp = I915_READ(reg);
  2130. if (HAS_PCH_CPT(dev)) {
  2131. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2132. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2133. } else {
  2134. temp &= ~FDI_LINK_TRAIN_NONE;
  2135. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2136. }
  2137. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2138. POSTING_READ(reg);
  2139. udelay(150);
  2140. for (i = 0; i < 4; i++) {
  2141. reg = FDI_TX_CTL(pipe);
  2142. temp = I915_READ(reg);
  2143. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2144. temp |= snb_b_fdi_train_param[i];
  2145. I915_WRITE(reg, temp);
  2146. POSTING_READ(reg);
  2147. udelay(500);
  2148. for (retry = 0; retry < 5; retry++) {
  2149. reg = FDI_RX_IIR(pipe);
  2150. temp = I915_READ(reg);
  2151. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2152. if (temp & FDI_RX_BIT_LOCK) {
  2153. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2154. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2155. break;
  2156. }
  2157. udelay(50);
  2158. }
  2159. if (retry < 5)
  2160. break;
  2161. }
  2162. if (i == 4)
  2163. DRM_ERROR("FDI train 1 fail!\n");
  2164. /* Train 2 */
  2165. reg = FDI_TX_CTL(pipe);
  2166. temp = I915_READ(reg);
  2167. temp &= ~FDI_LINK_TRAIN_NONE;
  2168. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2169. if (IS_GEN6(dev)) {
  2170. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2171. /* SNB-B */
  2172. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2173. }
  2174. I915_WRITE(reg, temp);
  2175. reg = FDI_RX_CTL(pipe);
  2176. temp = I915_READ(reg);
  2177. if (HAS_PCH_CPT(dev)) {
  2178. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2179. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2180. } else {
  2181. temp &= ~FDI_LINK_TRAIN_NONE;
  2182. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2183. }
  2184. I915_WRITE(reg, temp);
  2185. POSTING_READ(reg);
  2186. udelay(150);
  2187. for (i = 0; i < 4; i++) {
  2188. reg = FDI_TX_CTL(pipe);
  2189. temp = I915_READ(reg);
  2190. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2191. temp |= snb_b_fdi_train_param[i];
  2192. I915_WRITE(reg, temp);
  2193. POSTING_READ(reg);
  2194. udelay(500);
  2195. for (retry = 0; retry < 5; retry++) {
  2196. reg = FDI_RX_IIR(pipe);
  2197. temp = I915_READ(reg);
  2198. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2199. if (temp & FDI_RX_SYMBOL_LOCK) {
  2200. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2201. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2202. break;
  2203. }
  2204. udelay(50);
  2205. }
  2206. if (retry < 5)
  2207. break;
  2208. }
  2209. if (i == 4)
  2210. DRM_ERROR("FDI train 2 fail!\n");
  2211. DRM_DEBUG_KMS("FDI train done.\n");
  2212. }
  2213. /* Manual link training for Ivy Bridge A0 parts */
  2214. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2215. {
  2216. struct drm_device *dev = crtc->dev;
  2217. struct drm_i915_private *dev_priv = dev->dev_private;
  2218. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2219. int pipe = intel_crtc->pipe;
  2220. u32 reg, temp, i;
  2221. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2222. for train result */
  2223. reg = FDI_RX_IMR(pipe);
  2224. temp = I915_READ(reg);
  2225. temp &= ~FDI_RX_SYMBOL_LOCK;
  2226. temp &= ~FDI_RX_BIT_LOCK;
  2227. I915_WRITE(reg, temp);
  2228. POSTING_READ(reg);
  2229. udelay(150);
  2230. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2231. I915_READ(FDI_RX_IIR(pipe)));
  2232. /* enable CPU FDI TX and PCH FDI RX */
  2233. reg = FDI_TX_CTL(pipe);
  2234. temp = I915_READ(reg);
  2235. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2236. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2237. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2238. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2239. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2240. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2241. temp |= FDI_COMPOSITE_SYNC;
  2242. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2243. I915_WRITE(FDI_RX_MISC(pipe),
  2244. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2245. reg = FDI_RX_CTL(pipe);
  2246. temp = I915_READ(reg);
  2247. temp &= ~FDI_LINK_TRAIN_AUTO;
  2248. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2249. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2250. temp |= FDI_COMPOSITE_SYNC;
  2251. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2252. POSTING_READ(reg);
  2253. udelay(150);
  2254. for (i = 0; i < 4; i++) {
  2255. reg = FDI_TX_CTL(pipe);
  2256. temp = I915_READ(reg);
  2257. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2258. temp |= snb_b_fdi_train_param[i];
  2259. I915_WRITE(reg, temp);
  2260. POSTING_READ(reg);
  2261. udelay(500);
  2262. reg = FDI_RX_IIR(pipe);
  2263. temp = I915_READ(reg);
  2264. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2265. if (temp & FDI_RX_BIT_LOCK ||
  2266. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2267. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2268. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
  2269. break;
  2270. }
  2271. }
  2272. if (i == 4)
  2273. DRM_ERROR("FDI train 1 fail!\n");
  2274. /* Train 2 */
  2275. reg = FDI_TX_CTL(pipe);
  2276. temp = I915_READ(reg);
  2277. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2278. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2279. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2280. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2281. I915_WRITE(reg, temp);
  2282. reg = FDI_RX_CTL(pipe);
  2283. temp = I915_READ(reg);
  2284. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2285. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2286. I915_WRITE(reg, temp);
  2287. POSTING_READ(reg);
  2288. udelay(150);
  2289. for (i = 0; i < 4; i++) {
  2290. reg = FDI_TX_CTL(pipe);
  2291. temp = I915_READ(reg);
  2292. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2293. temp |= snb_b_fdi_train_param[i];
  2294. I915_WRITE(reg, temp);
  2295. POSTING_READ(reg);
  2296. udelay(500);
  2297. reg = FDI_RX_IIR(pipe);
  2298. temp = I915_READ(reg);
  2299. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2300. if (temp & FDI_RX_SYMBOL_LOCK) {
  2301. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2302. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
  2303. break;
  2304. }
  2305. }
  2306. if (i == 4)
  2307. DRM_ERROR("FDI train 2 fail!\n");
  2308. DRM_DEBUG_KMS("FDI train done.\n");
  2309. }
  2310. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2311. {
  2312. struct drm_device *dev = intel_crtc->base.dev;
  2313. struct drm_i915_private *dev_priv = dev->dev_private;
  2314. int pipe = intel_crtc->pipe;
  2315. u32 reg, temp;
  2316. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2317. reg = FDI_RX_CTL(pipe);
  2318. temp = I915_READ(reg);
  2319. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2320. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2321. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2322. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2323. POSTING_READ(reg);
  2324. udelay(200);
  2325. /* Switch from Rawclk to PCDclk */
  2326. temp = I915_READ(reg);
  2327. I915_WRITE(reg, temp | FDI_PCDCLK);
  2328. POSTING_READ(reg);
  2329. udelay(200);
  2330. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2331. reg = FDI_TX_CTL(pipe);
  2332. temp = I915_READ(reg);
  2333. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2334. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2335. POSTING_READ(reg);
  2336. udelay(100);
  2337. }
  2338. }
  2339. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2340. {
  2341. struct drm_device *dev = intel_crtc->base.dev;
  2342. struct drm_i915_private *dev_priv = dev->dev_private;
  2343. int pipe = intel_crtc->pipe;
  2344. u32 reg, temp;
  2345. /* Switch from PCDclk to Rawclk */
  2346. reg = FDI_RX_CTL(pipe);
  2347. temp = I915_READ(reg);
  2348. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2349. /* Disable CPU FDI TX PLL */
  2350. reg = FDI_TX_CTL(pipe);
  2351. temp = I915_READ(reg);
  2352. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2353. POSTING_READ(reg);
  2354. udelay(100);
  2355. reg = FDI_RX_CTL(pipe);
  2356. temp = I915_READ(reg);
  2357. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2358. /* Wait for the clocks to turn off. */
  2359. POSTING_READ(reg);
  2360. udelay(100);
  2361. }
  2362. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2363. {
  2364. struct drm_device *dev = crtc->dev;
  2365. struct drm_i915_private *dev_priv = dev->dev_private;
  2366. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2367. int pipe = intel_crtc->pipe;
  2368. u32 reg, temp;
  2369. /* disable CPU FDI tx and PCH FDI rx */
  2370. reg = FDI_TX_CTL(pipe);
  2371. temp = I915_READ(reg);
  2372. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2373. POSTING_READ(reg);
  2374. reg = FDI_RX_CTL(pipe);
  2375. temp = I915_READ(reg);
  2376. temp &= ~(0x7 << 16);
  2377. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2378. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2379. POSTING_READ(reg);
  2380. udelay(100);
  2381. /* Ironlake workaround, disable clock pointer after downing FDI */
  2382. if (HAS_PCH_IBX(dev)) {
  2383. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2384. }
  2385. /* still set train pattern 1 */
  2386. reg = FDI_TX_CTL(pipe);
  2387. temp = I915_READ(reg);
  2388. temp &= ~FDI_LINK_TRAIN_NONE;
  2389. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2390. I915_WRITE(reg, temp);
  2391. reg = FDI_RX_CTL(pipe);
  2392. temp = I915_READ(reg);
  2393. if (HAS_PCH_CPT(dev)) {
  2394. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2395. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2396. } else {
  2397. temp &= ~FDI_LINK_TRAIN_NONE;
  2398. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2399. }
  2400. /* BPC in FDI rx is consistent with that in PIPECONF */
  2401. temp &= ~(0x07 << 16);
  2402. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2403. I915_WRITE(reg, temp);
  2404. POSTING_READ(reg);
  2405. udelay(100);
  2406. }
  2407. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2408. {
  2409. struct drm_device *dev = crtc->dev;
  2410. struct drm_i915_private *dev_priv = dev->dev_private;
  2411. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2412. unsigned long flags;
  2413. bool pending;
  2414. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2415. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2416. return false;
  2417. spin_lock_irqsave(&dev->event_lock, flags);
  2418. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2419. spin_unlock_irqrestore(&dev->event_lock, flags);
  2420. return pending;
  2421. }
  2422. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2423. {
  2424. struct drm_device *dev = crtc->dev;
  2425. struct drm_i915_private *dev_priv = dev->dev_private;
  2426. if (crtc->fb == NULL)
  2427. return;
  2428. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2429. wait_event(dev_priv->pending_flip_queue,
  2430. !intel_crtc_has_pending_flip(crtc));
  2431. mutex_lock(&dev->struct_mutex);
  2432. intel_finish_fb(crtc->fb);
  2433. mutex_unlock(&dev->struct_mutex);
  2434. }
  2435. /* Program iCLKIP clock to the desired frequency */
  2436. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2437. {
  2438. struct drm_device *dev = crtc->dev;
  2439. struct drm_i915_private *dev_priv = dev->dev_private;
  2440. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2441. u32 temp;
  2442. mutex_lock(&dev_priv->dpio_lock);
  2443. /* It is necessary to ungate the pixclk gate prior to programming
  2444. * the divisors, and gate it back when it is done.
  2445. */
  2446. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2447. /* Disable SSCCTL */
  2448. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2449. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2450. SBI_SSCCTL_DISABLE,
  2451. SBI_ICLK);
  2452. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2453. if (crtc->mode.clock == 20000) {
  2454. auxdiv = 1;
  2455. divsel = 0x41;
  2456. phaseinc = 0x20;
  2457. } else {
  2458. /* The iCLK virtual clock root frequency is in MHz,
  2459. * but the crtc->mode.clock in in KHz. To get the divisors,
  2460. * it is necessary to divide one by another, so we
  2461. * convert the virtual clock precision to KHz here for higher
  2462. * precision.
  2463. */
  2464. u32 iclk_virtual_root_freq = 172800 * 1000;
  2465. u32 iclk_pi_range = 64;
  2466. u32 desired_divisor, msb_divisor_value, pi_value;
  2467. desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
  2468. msb_divisor_value = desired_divisor / iclk_pi_range;
  2469. pi_value = desired_divisor % iclk_pi_range;
  2470. auxdiv = 0;
  2471. divsel = msb_divisor_value - 2;
  2472. phaseinc = pi_value;
  2473. }
  2474. /* This should not happen with any sane values */
  2475. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2476. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2477. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2478. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2479. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2480. crtc->mode.clock,
  2481. auxdiv,
  2482. divsel,
  2483. phasedir,
  2484. phaseinc);
  2485. /* Program SSCDIVINTPHASE6 */
  2486. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2487. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2488. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2489. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2490. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2491. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2492. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2493. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2494. /* Program SSCAUXDIV */
  2495. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2496. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2497. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2498. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2499. /* Enable modulator and associated divider */
  2500. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2501. temp &= ~SBI_SSCCTL_DISABLE;
  2502. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2503. /* Wait for initialization time */
  2504. udelay(24);
  2505. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2506. mutex_unlock(&dev_priv->dpio_lock);
  2507. }
  2508. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2509. enum pipe pch_transcoder)
  2510. {
  2511. struct drm_device *dev = crtc->base.dev;
  2512. struct drm_i915_private *dev_priv = dev->dev_private;
  2513. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2514. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2515. I915_READ(HTOTAL(cpu_transcoder)));
  2516. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2517. I915_READ(HBLANK(cpu_transcoder)));
  2518. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2519. I915_READ(HSYNC(cpu_transcoder)));
  2520. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2521. I915_READ(VTOTAL(cpu_transcoder)));
  2522. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2523. I915_READ(VBLANK(cpu_transcoder)));
  2524. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2525. I915_READ(VSYNC(cpu_transcoder)));
  2526. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2527. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2528. }
  2529. /*
  2530. * Enable PCH resources required for PCH ports:
  2531. * - PCH PLLs
  2532. * - FDI training & RX/TX
  2533. * - update transcoder timings
  2534. * - DP transcoding bits
  2535. * - transcoder
  2536. */
  2537. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2538. {
  2539. struct drm_device *dev = crtc->dev;
  2540. struct drm_i915_private *dev_priv = dev->dev_private;
  2541. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2542. int pipe = intel_crtc->pipe;
  2543. u32 reg, temp;
  2544. assert_pch_transcoder_disabled(dev_priv, pipe);
  2545. /* Write the TU size bits before fdi link training, so that error
  2546. * detection works. */
  2547. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2548. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2549. /* For PCH output, training FDI link */
  2550. dev_priv->display.fdi_link_train(crtc);
  2551. /* XXX: pch pll's can be enabled any time before we enable the PCH
  2552. * transcoder, and we actually should do this to not upset any PCH
  2553. * transcoder that already use the clock when we share it.
  2554. *
  2555. * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
  2556. * unconditionally resets the pll - we need that to have the right LVDS
  2557. * enable sequence. */
  2558. ironlake_enable_pch_pll(intel_crtc);
  2559. if (HAS_PCH_CPT(dev)) {
  2560. u32 sel;
  2561. temp = I915_READ(PCH_DPLL_SEL);
  2562. switch (pipe) {
  2563. default:
  2564. case 0:
  2565. temp |= TRANSA_DPLL_ENABLE;
  2566. sel = TRANSA_DPLLB_SEL;
  2567. break;
  2568. case 1:
  2569. temp |= TRANSB_DPLL_ENABLE;
  2570. sel = TRANSB_DPLLB_SEL;
  2571. break;
  2572. case 2:
  2573. temp |= TRANSC_DPLL_ENABLE;
  2574. sel = TRANSC_DPLLB_SEL;
  2575. break;
  2576. }
  2577. if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
  2578. temp |= sel;
  2579. else
  2580. temp &= ~sel;
  2581. I915_WRITE(PCH_DPLL_SEL, temp);
  2582. }
  2583. /* set transcoder timing, panel must allow it */
  2584. assert_panel_unlocked(dev_priv, pipe);
  2585. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  2586. intel_fdi_normal_train(crtc);
  2587. /* For PCH DP, enable TRANS_DP_CTL */
  2588. if (HAS_PCH_CPT(dev) &&
  2589. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  2590. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2591. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  2592. reg = TRANS_DP_CTL(pipe);
  2593. temp = I915_READ(reg);
  2594. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2595. TRANS_DP_SYNC_MASK |
  2596. TRANS_DP_BPC_MASK);
  2597. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2598. TRANS_DP_ENH_FRAMING);
  2599. temp |= bpc << 9; /* same format but at 11:9 */
  2600. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2601. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2602. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2603. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2604. switch (intel_trans_dp_port_sel(crtc)) {
  2605. case PCH_DP_B:
  2606. temp |= TRANS_DP_PORT_SEL_B;
  2607. break;
  2608. case PCH_DP_C:
  2609. temp |= TRANS_DP_PORT_SEL_C;
  2610. break;
  2611. case PCH_DP_D:
  2612. temp |= TRANS_DP_PORT_SEL_D;
  2613. break;
  2614. default:
  2615. BUG();
  2616. }
  2617. I915_WRITE(reg, temp);
  2618. }
  2619. ironlake_enable_pch_transcoder(dev_priv, pipe);
  2620. }
  2621. static void lpt_pch_enable(struct drm_crtc *crtc)
  2622. {
  2623. struct drm_device *dev = crtc->dev;
  2624. struct drm_i915_private *dev_priv = dev->dev_private;
  2625. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2626. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  2627. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  2628. lpt_program_iclkip(crtc);
  2629. /* Set transcoder timing. */
  2630. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  2631. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  2632. }
  2633. static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
  2634. {
  2635. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  2636. if (pll == NULL)
  2637. return;
  2638. if (pll->refcount == 0) {
  2639. WARN(1, "bad PCH PLL refcount\n");
  2640. return;
  2641. }
  2642. --pll->refcount;
  2643. intel_crtc->pch_pll = NULL;
  2644. }
  2645. static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
  2646. {
  2647. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  2648. struct intel_pch_pll *pll;
  2649. int i;
  2650. pll = intel_crtc->pch_pll;
  2651. if (pll) {
  2652. DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
  2653. intel_crtc->base.base.id, pll->pll_reg);
  2654. goto prepare;
  2655. }
  2656. if (HAS_PCH_IBX(dev_priv->dev)) {
  2657. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  2658. i = intel_crtc->pipe;
  2659. pll = &dev_priv->pch_plls[i];
  2660. DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
  2661. intel_crtc->base.base.id, pll->pll_reg);
  2662. goto found;
  2663. }
  2664. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2665. pll = &dev_priv->pch_plls[i];
  2666. /* Only want to check enabled timings first */
  2667. if (pll->refcount == 0)
  2668. continue;
  2669. if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
  2670. fp == I915_READ(pll->fp0_reg)) {
  2671. DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
  2672. intel_crtc->base.base.id,
  2673. pll->pll_reg, pll->refcount, pll->active);
  2674. goto found;
  2675. }
  2676. }
  2677. /* Ok no matching timings, maybe there's a free one? */
  2678. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2679. pll = &dev_priv->pch_plls[i];
  2680. if (pll->refcount == 0) {
  2681. DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
  2682. intel_crtc->base.base.id, pll->pll_reg);
  2683. goto found;
  2684. }
  2685. }
  2686. return NULL;
  2687. found:
  2688. intel_crtc->pch_pll = pll;
  2689. pll->refcount++;
  2690. DRM_DEBUG_DRIVER("using pll %d for pipe %c\n", i, pipe_name(intel_crtc->pipe));
  2691. prepare: /* separate function? */
  2692. DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
  2693. /* Wait for the clocks to stabilize before rewriting the regs */
  2694. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2695. POSTING_READ(pll->pll_reg);
  2696. udelay(150);
  2697. I915_WRITE(pll->fp0_reg, fp);
  2698. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2699. pll->on = false;
  2700. return pll;
  2701. }
  2702. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  2703. {
  2704. struct drm_i915_private *dev_priv = dev->dev_private;
  2705. int dslreg = PIPEDSL(pipe);
  2706. u32 temp;
  2707. temp = I915_READ(dslreg);
  2708. udelay(500);
  2709. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  2710. if (wait_for(I915_READ(dslreg) != temp, 5))
  2711. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  2712. }
  2713. }
  2714. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  2715. {
  2716. struct drm_device *dev = crtc->base.dev;
  2717. struct drm_i915_private *dev_priv = dev->dev_private;
  2718. int pipe = crtc->pipe;
  2719. if (crtc->config.pch_pfit.size) {
  2720. /* Force use of hard-coded filter coefficients
  2721. * as some pre-programmed values are broken,
  2722. * e.g. x201.
  2723. */
  2724. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  2725. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  2726. PF_PIPE_SEL_IVB(pipe));
  2727. else
  2728. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2729. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  2730. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  2731. }
  2732. }
  2733. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2734. {
  2735. struct drm_device *dev = crtc->dev;
  2736. struct drm_i915_private *dev_priv = dev->dev_private;
  2737. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2738. struct intel_encoder *encoder;
  2739. int pipe = intel_crtc->pipe;
  2740. int plane = intel_crtc->plane;
  2741. u32 temp;
  2742. WARN_ON(!crtc->enabled);
  2743. if (intel_crtc->active)
  2744. return;
  2745. intel_crtc->active = true;
  2746. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  2747. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  2748. intel_update_watermarks(dev);
  2749. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2750. temp = I915_READ(PCH_LVDS);
  2751. if ((temp & LVDS_PORT_EN) == 0)
  2752. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2753. }
  2754. if (intel_crtc->config.has_pch_encoder) {
  2755. /* Note: FDI PLL enabling _must_ be done before we enable the
  2756. * cpu pipes, hence this is separate from all the other fdi/pch
  2757. * enabling. */
  2758. ironlake_fdi_pll_enable(intel_crtc);
  2759. } else {
  2760. assert_fdi_tx_disabled(dev_priv, pipe);
  2761. assert_fdi_rx_disabled(dev_priv, pipe);
  2762. }
  2763. for_each_encoder_on_crtc(dev, crtc, encoder)
  2764. if (encoder->pre_enable)
  2765. encoder->pre_enable(encoder);
  2766. /* Enable panel fitting for LVDS */
  2767. ironlake_pfit_enable(intel_crtc);
  2768. /*
  2769. * On ILK+ LUT must be loaded before the pipe is running but with
  2770. * clocks enabled
  2771. */
  2772. intel_crtc_load_lut(crtc);
  2773. intel_enable_pipe(dev_priv, pipe,
  2774. intel_crtc->config.has_pch_encoder);
  2775. intel_enable_plane(dev_priv, plane, pipe);
  2776. intel_crtc_update_cursor(crtc, true);
  2777. if (intel_crtc->config.has_pch_encoder)
  2778. ironlake_pch_enable(crtc);
  2779. mutex_lock(&dev->struct_mutex);
  2780. intel_update_fbc(dev);
  2781. mutex_unlock(&dev->struct_mutex);
  2782. for_each_encoder_on_crtc(dev, crtc, encoder)
  2783. encoder->enable(encoder);
  2784. if (HAS_PCH_CPT(dev))
  2785. cpt_verify_modeset(dev, intel_crtc->pipe);
  2786. /*
  2787. * There seems to be a race in PCH platform hw (at least on some
  2788. * outputs) where an enabled pipe still completes any pageflip right
  2789. * away (as if the pipe is off) instead of waiting for vblank. As soon
  2790. * as the first vblank happend, everything works as expected. Hence just
  2791. * wait for one vblank before returning to avoid strange things
  2792. * happening.
  2793. */
  2794. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2795. }
  2796. /* IPS only exists on ULT machines and is tied to pipe A. */
  2797. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  2798. {
  2799. return IS_ULT(crtc->base.dev) && crtc->pipe == PIPE_A;
  2800. }
  2801. static void hsw_enable_ips(struct intel_crtc *crtc)
  2802. {
  2803. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  2804. if (!crtc->config.ips_enabled)
  2805. return;
  2806. /* We can only enable IPS after we enable a plane and wait for a vblank.
  2807. * We guarantee that the plane is enabled by calling intel_enable_ips
  2808. * only after intel_enable_plane. And intel_enable_plane already waits
  2809. * for a vblank, so all we need to do here is to enable the IPS bit. */
  2810. assert_plane_enabled(dev_priv, crtc->plane);
  2811. I915_WRITE(IPS_CTL, IPS_ENABLE);
  2812. }
  2813. static void hsw_disable_ips(struct intel_crtc *crtc)
  2814. {
  2815. struct drm_device *dev = crtc->base.dev;
  2816. struct drm_i915_private *dev_priv = dev->dev_private;
  2817. if (!crtc->config.ips_enabled)
  2818. return;
  2819. assert_plane_enabled(dev_priv, crtc->plane);
  2820. I915_WRITE(IPS_CTL, 0);
  2821. /* We need to wait for a vblank before we can disable the plane. */
  2822. intel_wait_for_vblank(dev, crtc->pipe);
  2823. }
  2824. static void haswell_crtc_enable(struct drm_crtc *crtc)
  2825. {
  2826. struct drm_device *dev = crtc->dev;
  2827. struct drm_i915_private *dev_priv = dev->dev_private;
  2828. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2829. struct intel_encoder *encoder;
  2830. int pipe = intel_crtc->pipe;
  2831. int plane = intel_crtc->plane;
  2832. WARN_ON(!crtc->enabled);
  2833. if (intel_crtc->active)
  2834. return;
  2835. intel_crtc->active = true;
  2836. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  2837. if (intel_crtc->config.has_pch_encoder)
  2838. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  2839. intel_update_watermarks(dev);
  2840. if (intel_crtc->config.has_pch_encoder)
  2841. dev_priv->display.fdi_link_train(crtc);
  2842. for_each_encoder_on_crtc(dev, crtc, encoder)
  2843. if (encoder->pre_enable)
  2844. encoder->pre_enable(encoder);
  2845. intel_ddi_enable_pipe_clock(intel_crtc);
  2846. /* Enable panel fitting for eDP */
  2847. ironlake_pfit_enable(intel_crtc);
  2848. /*
  2849. * On ILK+ LUT must be loaded before the pipe is running but with
  2850. * clocks enabled
  2851. */
  2852. intel_crtc_load_lut(crtc);
  2853. intel_ddi_set_pipe_settings(crtc);
  2854. intel_ddi_enable_transcoder_func(crtc);
  2855. intel_enable_pipe(dev_priv, pipe,
  2856. intel_crtc->config.has_pch_encoder);
  2857. intel_enable_plane(dev_priv, plane, pipe);
  2858. intel_crtc_update_cursor(crtc, true);
  2859. hsw_enable_ips(intel_crtc);
  2860. if (intel_crtc->config.has_pch_encoder)
  2861. lpt_pch_enable(crtc);
  2862. mutex_lock(&dev->struct_mutex);
  2863. intel_update_fbc(dev);
  2864. mutex_unlock(&dev->struct_mutex);
  2865. for_each_encoder_on_crtc(dev, crtc, encoder)
  2866. encoder->enable(encoder);
  2867. /*
  2868. * There seems to be a race in PCH platform hw (at least on some
  2869. * outputs) where an enabled pipe still completes any pageflip right
  2870. * away (as if the pipe is off) instead of waiting for vblank. As soon
  2871. * as the first vblank happend, everything works as expected. Hence just
  2872. * wait for one vblank before returning to avoid strange things
  2873. * happening.
  2874. */
  2875. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2876. }
  2877. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  2878. {
  2879. struct drm_device *dev = crtc->base.dev;
  2880. struct drm_i915_private *dev_priv = dev->dev_private;
  2881. int pipe = crtc->pipe;
  2882. /* To avoid upsetting the power well on haswell only disable the pfit if
  2883. * it's in use. The hw state code will make sure we get this right. */
  2884. if (crtc->config.pch_pfit.size) {
  2885. I915_WRITE(PF_CTL(pipe), 0);
  2886. I915_WRITE(PF_WIN_POS(pipe), 0);
  2887. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2888. }
  2889. }
  2890. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2891. {
  2892. struct drm_device *dev = crtc->dev;
  2893. struct drm_i915_private *dev_priv = dev->dev_private;
  2894. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2895. struct intel_encoder *encoder;
  2896. int pipe = intel_crtc->pipe;
  2897. int plane = intel_crtc->plane;
  2898. u32 reg, temp;
  2899. if (!intel_crtc->active)
  2900. return;
  2901. for_each_encoder_on_crtc(dev, crtc, encoder)
  2902. encoder->disable(encoder);
  2903. intel_crtc_wait_for_pending_flips(crtc);
  2904. drm_vblank_off(dev, pipe);
  2905. intel_crtc_update_cursor(crtc, false);
  2906. intel_disable_plane(dev_priv, plane, pipe);
  2907. if (dev_priv->cfb_plane == plane)
  2908. intel_disable_fbc(dev);
  2909. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  2910. intel_disable_pipe(dev_priv, pipe);
  2911. ironlake_pfit_disable(intel_crtc);
  2912. for_each_encoder_on_crtc(dev, crtc, encoder)
  2913. if (encoder->post_disable)
  2914. encoder->post_disable(encoder);
  2915. ironlake_fdi_disable(crtc);
  2916. ironlake_disable_pch_transcoder(dev_priv, pipe);
  2917. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  2918. if (HAS_PCH_CPT(dev)) {
  2919. /* disable TRANS_DP_CTL */
  2920. reg = TRANS_DP_CTL(pipe);
  2921. temp = I915_READ(reg);
  2922. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2923. temp |= TRANS_DP_PORT_SEL_NONE;
  2924. I915_WRITE(reg, temp);
  2925. /* disable DPLL_SEL */
  2926. temp = I915_READ(PCH_DPLL_SEL);
  2927. switch (pipe) {
  2928. case 0:
  2929. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2930. break;
  2931. case 1:
  2932. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2933. break;
  2934. case 2:
  2935. /* C shares PLL A or B */
  2936. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2937. break;
  2938. default:
  2939. BUG(); /* wtf */
  2940. }
  2941. I915_WRITE(PCH_DPLL_SEL, temp);
  2942. }
  2943. /* disable PCH DPLL */
  2944. intel_disable_pch_pll(intel_crtc);
  2945. ironlake_fdi_pll_disable(intel_crtc);
  2946. intel_crtc->active = false;
  2947. intel_update_watermarks(dev);
  2948. mutex_lock(&dev->struct_mutex);
  2949. intel_update_fbc(dev);
  2950. mutex_unlock(&dev->struct_mutex);
  2951. }
  2952. static void haswell_crtc_disable(struct drm_crtc *crtc)
  2953. {
  2954. struct drm_device *dev = crtc->dev;
  2955. struct drm_i915_private *dev_priv = dev->dev_private;
  2956. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2957. struct intel_encoder *encoder;
  2958. int pipe = intel_crtc->pipe;
  2959. int plane = intel_crtc->plane;
  2960. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  2961. if (!intel_crtc->active)
  2962. return;
  2963. for_each_encoder_on_crtc(dev, crtc, encoder)
  2964. encoder->disable(encoder);
  2965. intel_crtc_wait_for_pending_flips(crtc);
  2966. drm_vblank_off(dev, pipe);
  2967. intel_crtc_update_cursor(crtc, false);
  2968. /* FBC must be disabled before disabling the plane on HSW. */
  2969. if (dev_priv->cfb_plane == plane)
  2970. intel_disable_fbc(dev);
  2971. hsw_disable_ips(intel_crtc);
  2972. intel_disable_plane(dev_priv, plane, pipe);
  2973. if (intel_crtc->config.has_pch_encoder)
  2974. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  2975. intel_disable_pipe(dev_priv, pipe);
  2976. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  2977. ironlake_pfit_disable(intel_crtc);
  2978. intel_ddi_disable_pipe_clock(intel_crtc);
  2979. for_each_encoder_on_crtc(dev, crtc, encoder)
  2980. if (encoder->post_disable)
  2981. encoder->post_disable(encoder);
  2982. if (intel_crtc->config.has_pch_encoder) {
  2983. lpt_disable_pch_transcoder(dev_priv);
  2984. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  2985. intel_ddi_fdi_disable(crtc);
  2986. }
  2987. intel_crtc->active = false;
  2988. intel_update_watermarks(dev);
  2989. mutex_lock(&dev->struct_mutex);
  2990. intel_update_fbc(dev);
  2991. mutex_unlock(&dev->struct_mutex);
  2992. }
  2993. static void ironlake_crtc_off(struct drm_crtc *crtc)
  2994. {
  2995. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2996. intel_put_pch_pll(intel_crtc);
  2997. }
  2998. static void haswell_crtc_off(struct drm_crtc *crtc)
  2999. {
  3000. intel_ddi_put_crtc_pll(crtc);
  3001. }
  3002. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3003. {
  3004. if (!enable && intel_crtc->overlay) {
  3005. struct drm_device *dev = intel_crtc->base.dev;
  3006. struct drm_i915_private *dev_priv = dev->dev_private;
  3007. mutex_lock(&dev->struct_mutex);
  3008. dev_priv->mm.interruptible = false;
  3009. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3010. dev_priv->mm.interruptible = true;
  3011. mutex_unlock(&dev->struct_mutex);
  3012. }
  3013. /* Let userspace switch the overlay on again. In most cases userspace
  3014. * has to recompute where to put it anyway.
  3015. */
  3016. }
  3017. /**
  3018. * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
  3019. * cursor plane briefly if not already running after enabling the display
  3020. * plane.
  3021. * This workaround avoids occasional blank screens when self refresh is
  3022. * enabled.
  3023. */
  3024. static void
  3025. g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
  3026. {
  3027. u32 cntl = I915_READ(CURCNTR(pipe));
  3028. if ((cntl & CURSOR_MODE) == 0) {
  3029. u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
  3030. I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
  3031. I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
  3032. intel_wait_for_vblank(dev_priv->dev, pipe);
  3033. I915_WRITE(CURCNTR(pipe), cntl);
  3034. I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
  3035. I915_WRITE(FW_BLC_SELF, fw_bcl_self);
  3036. }
  3037. }
  3038. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3039. {
  3040. struct drm_device *dev = crtc->base.dev;
  3041. struct drm_i915_private *dev_priv = dev->dev_private;
  3042. struct intel_crtc_config *pipe_config = &crtc->config;
  3043. if (!crtc->config.gmch_pfit.control)
  3044. return;
  3045. /*
  3046. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3047. * according to register description and PRM.
  3048. */
  3049. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3050. assert_pipe_disabled(dev_priv, crtc->pipe);
  3051. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3052. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3053. /* Border color in case we don't scale up to the full screen. Black by
  3054. * default, change to something else for debugging. */
  3055. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3056. }
  3057. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3058. {
  3059. struct drm_device *dev = crtc->dev;
  3060. struct drm_i915_private *dev_priv = dev->dev_private;
  3061. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3062. struct intel_encoder *encoder;
  3063. int pipe = intel_crtc->pipe;
  3064. int plane = intel_crtc->plane;
  3065. WARN_ON(!crtc->enabled);
  3066. if (intel_crtc->active)
  3067. return;
  3068. intel_crtc->active = true;
  3069. intel_update_watermarks(dev);
  3070. mutex_lock(&dev_priv->dpio_lock);
  3071. for_each_encoder_on_crtc(dev, crtc, encoder)
  3072. if (encoder->pre_pll_enable)
  3073. encoder->pre_pll_enable(encoder);
  3074. intel_enable_pll(dev_priv, pipe);
  3075. for_each_encoder_on_crtc(dev, crtc, encoder)
  3076. if (encoder->pre_enable)
  3077. encoder->pre_enable(encoder);
  3078. /* VLV wants encoder enabling _before_ the pipe is up. */
  3079. for_each_encoder_on_crtc(dev, crtc, encoder)
  3080. encoder->enable(encoder);
  3081. /* Enable panel fitting for eDP */
  3082. i9xx_pfit_enable(intel_crtc);
  3083. intel_crtc_load_lut(crtc);
  3084. intel_enable_pipe(dev_priv, pipe, false);
  3085. intel_enable_plane(dev_priv, plane, pipe);
  3086. intel_crtc_update_cursor(crtc, true);
  3087. intel_update_fbc(dev);
  3088. /* Give the overlay scaler a chance to enable if it's on this pipe */
  3089. intel_crtc_dpms_overlay(intel_crtc, true);
  3090. mutex_unlock(&dev_priv->dpio_lock);
  3091. }
  3092. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3093. {
  3094. struct drm_device *dev = crtc->dev;
  3095. struct drm_i915_private *dev_priv = dev->dev_private;
  3096. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3097. struct intel_encoder *encoder;
  3098. int pipe = intel_crtc->pipe;
  3099. int plane = intel_crtc->plane;
  3100. WARN_ON(!crtc->enabled);
  3101. if (intel_crtc->active)
  3102. return;
  3103. intel_crtc->active = true;
  3104. intel_update_watermarks(dev);
  3105. intel_enable_pll(dev_priv, pipe);
  3106. for_each_encoder_on_crtc(dev, crtc, encoder)
  3107. if (encoder->pre_enable)
  3108. encoder->pre_enable(encoder);
  3109. /* Enable panel fitting for LVDS */
  3110. i9xx_pfit_enable(intel_crtc);
  3111. intel_crtc_load_lut(crtc);
  3112. intel_enable_pipe(dev_priv, pipe, false);
  3113. intel_enable_plane(dev_priv, plane, pipe);
  3114. intel_crtc_update_cursor(crtc, true);
  3115. if (IS_G4X(dev))
  3116. g4x_fixup_plane(dev_priv, pipe);
  3117. intel_update_fbc(dev);
  3118. /* Give the overlay scaler a chance to enable if it's on this pipe */
  3119. intel_crtc_dpms_overlay(intel_crtc, true);
  3120. for_each_encoder_on_crtc(dev, crtc, encoder)
  3121. encoder->enable(encoder);
  3122. }
  3123. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  3124. {
  3125. struct drm_device *dev = crtc->base.dev;
  3126. struct drm_i915_private *dev_priv = dev->dev_private;
  3127. if (!crtc->config.gmch_pfit.control)
  3128. return;
  3129. assert_pipe_disabled(dev_priv, crtc->pipe);
  3130. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  3131. I915_READ(PFIT_CONTROL));
  3132. I915_WRITE(PFIT_CONTROL, 0);
  3133. }
  3134. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  3135. {
  3136. struct drm_device *dev = crtc->dev;
  3137. struct drm_i915_private *dev_priv = dev->dev_private;
  3138. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3139. struct intel_encoder *encoder;
  3140. int pipe = intel_crtc->pipe;
  3141. int plane = intel_crtc->plane;
  3142. if (!intel_crtc->active)
  3143. return;
  3144. for_each_encoder_on_crtc(dev, crtc, encoder)
  3145. encoder->disable(encoder);
  3146. /* Give the overlay scaler a chance to disable if it's on this pipe */
  3147. intel_crtc_wait_for_pending_flips(crtc);
  3148. drm_vblank_off(dev, pipe);
  3149. intel_crtc_dpms_overlay(intel_crtc, false);
  3150. intel_crtc_update_cursor(crtc, false);
  3151. if (dev_priv->cfb_plane == plane)
  3152. intel_disable_fbc(dev);
  3153. intel_disable_plane(dev_priv, plane, pipe);
  3154. intel_disable_pipe(dev_priv, pipe);
  3155. i9xx_pfit_disable(intel_crtc);
  3156. for_each_encoder_on_crtc(dev, crtc, encoder)
  3157. if (encoder->post_disable)
  3158. encoder->post_disable(encoder);
  3159. intel_disable_pll(dev_priv, pipe);
  3160. intel_crtc->active = false;
  3161. intel_update_fbc(dev);
  3162. intel_update_watermarks(dev);
  3163. }
  3164. static void i9xx_crtc_off(struct drm_crtc *crtc)
  3165. {
  3166. }
  3167. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  3168. bool enabled)
  3169. {
  3170. struct drm_device *dev = crtc->dev;
  3171. struct drm_i915_master_private *master_priv;
  3172. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3173. int pipe = intel_crtc->pipe;
  3174. if (!dev->primary->master)
  3175. return;
  3176. master_priv = dev->primary->master->driver_priv;
  3177. if (!master_priv->sarea_priv)
  3178. return;
  3179. switch (pipe) {
  3180. case 0:
  3181. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  3182. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  3183. break;
  3184. case 1:
  3185. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  3186. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  3187. break;
  3188. default:
  3189. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  3190. break;
  3191. }
  3192. }
  3193. /**
  3194. * Sets the power management mode of the pipe and plane.
  3195. */
  3196. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  3197. {
  3198. struct drm_device *dev = crtc->dev;
  3199. struct drm_i915_private *dev_priv = dev->dev_private;
  3200. struct intel_encoder *intel_encoder;
  3201. bool enable = false;
  3202. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3203. enable |= intel_encoder->connectors_active;
  3204. if (enable)
  3205. dev_priv->display.crtc_enable(crtc);
  3206. else
  3207. dev_priv->display.crtc_disable(crtc);
  3208. intel_crtc_update_sarea(crtc, enable);
  3209. }
  3210. static void intel_crtc_disable(struct drm_crtc *crtc)
  3211. {
  3212. struct drm_device *dev = crtc->dev;
  3213. struct drm_connector *connector;
  3214. struct drm_i915_private *dev_priv = dev->dev_private;
  3215. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3216. /* crtc should still be enabled when we disable it. */
  3217. WARN_ON(!crtc->enabled);
  3218. dev_priv->display.crtc_disable(crtc);
  3219. intel_crtc->eld_vld = false;
  3220. intel_crtc_update_sarea(crtc, false);
  3221. dev_priv->display.off(crtc);
  3222. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  3223. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  3224. if (crtc->fb) {
  3225. mutex_lock(&dev->struct_mutex);
  3226. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  3227. mutex_unlock(&dev->struct_mutex);
  3228. crtc->fb = NULL;
  3229. }
  3230. /* Update computed state. */
  3231. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  3232. if (!connector->encoder || !connector->encoder->crtc)
  3233. continue;
  3234. if (connector->encoder->crtc != crtc)
  3235. continue;
  3236. connector->dpms = DRM_MODE_DPMS_OFF;
  3237. to_intel_encoder(connector->encoder)->connectors_active = false;
  3238. }
  3239. }
  3240. void intel_modeset_disable(struct drm_device *dev)
  3241. {
  3242. struct drm_crtc *crtc;
  3243. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3244. if (crtc->enabled)
  3245. intel_crtc_disable(crtc);
  3246. }
  3247. }
  3248. void intel_encoder_destroy(struct drm_encoder *encoder)
  3249. {
  3250. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3251. drm_encoder_cleanup(encoder);
  3252. kfree(intel_encoder);
  3253. }
  3254. /* Simple dpms helper for encodres with just one connector, no cloning and only
  3255. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  3256. * state of the entire output pipe. */
  3257. void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  3258. {
  3259. if (mode == DRM_MODE_DPMS_ON) {
  3260. encoder->connectors_active = true;
  3261. intel_crtc_update_dpms(encoder->base.crtc);
  3262. } else {
  3263. encoder->connectors_active = false;
  3264. intel_crtc_update_dpms(encoder->base.crtc);
  3265. }
  3266. }
  3267. /* Cross check the actual hw state with our own modeset state tracking (and it's
  3268. * internal consistency). */
  3269. static void intel_connector_check_state(struct intel_connector *connector)
  3270. {
  3271. if (connector->get_hw_state(connector)) {
  3272. struct intel_encoder *encoder = connector->encoder;
  3273. struct drm_crtc *crtc;
  3274. bool encoder_enabled;
  3275. enum pipe pipe;
  3276. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  3277. connector->base.base.id,
  3278. drm_get_connector_name(&connector->base));
  3279. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  3280. "wrong connector dpms state\n");
  3281. WARN(connector->base.encoder != &encoder->base,
  3282. "active connector not linked to encoder\n");
  3283. WARN(!encoder->connectors_active,
  3284. "encoder->connectors_active not set\n");
  3285. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  3286. WARN(!encoder_enabled, "encoder not enabled\n");
  3287. if (WARN_ON(!encoder->base.crtc))
  3288. return;
  3289. crtc = encoder->base.crtc;
  3290. WARN(!crtc->enabled, "crtc not enabled\n");
  3291. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  3292. WARN(pipe != to_intel_crtc(crtc)->pipe,
  3293. "encoder active on the wrong pipe\n");
  3294. }
  3295. }
  3296. /* Even simpler default implementation, if there's really no special case to
  3297. * consider. */
  3298. void intel_connector_dpms(struct drm_connector *connector, int mode)
  3299. {
  3300. struct intel_encoder *encoder = intel_attached_encoder(connector);
  3301. /* All the simple cases only support two dpms states. */
  3302. if (mode != DRM_MODE_DPMS_ON)
  3303. mode = DRM_MODE_DPMS_OFF;
  3304. if (mode == connector->dpms)
  3305. return;
  3306. connector->dpms = mode;
  3307. /* Only need to change hw state when actually enabled */
  3308. if (encoder->base.crtc)
  3309. intel_encoder_dpms(encoder, mode);
  3310. else
  3311. WARN_ON(encoder->connectors_active != false);
  3312. intel_modeset_check_state(connector->dev);
  3313. }
  3314. /* Simple connector->get_hw_state implementation for encoders that support only
  3315. * one connector and no cloning and hence the encoder state determines the state
  3316. * of the connector. */
  3317. bool intel_connector_get_hw_state(struct intel_connector *connector)
  3318. {
  3319. enum pipe pipe = 0;
  3320. struct intel_encoder *encoder = connector->encoder;
  3321. return encoder->get_hw_state(encoder, &pipe);
  3322. }
  3323. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  3324. struct intel_crtc_config *pipe_config)
  3325. {
  3326. struct drm_i915_private *dev_priv = dev->dev_private;
  3327. struct intel_crtc *pipe_B_crtc =
  3328. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  3329. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  3330. pipe_name(pipe), pipe_config->fdi_lanes);
  3331. if (pipe_config->fdi_lanes > 4) {
  3332. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  3333. pipe_name(pipe), pipe_config->fdi_lanes);
  3334. return false;
  3335. }
  3336. if (IS_HASWELL(dev)) {
  3337. if (pipe_config->fdi_lanes > 2) {
  3338. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  3339. pipe_config->fdi_lanes);
  3340. return false;
  3341. } else {
  3342. return true;
  3343. }
  3344. }
  3345. if (INTEL_INFO(dev)->num_pipes == 2)
  3346. return true;
  3347. /* Ivybridge 3 pipe is really complicated */
  3348. switch (pipe) {
  3349. case PIPE_A:
  3350. return true;
  3351. case PIPE_B:
  3352. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  3353. pipe_config->fdi_lanes > 2) {
  3354. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  3355. pipe_name(pipe), pipe_config->fdi_lanes);
  3356. return false;
  3357. }
  3358. return true;
  3359. case PIPE_C:
  3360. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  3361. pipe_B_crtc->config.fdi_lanes <= 2) {
  3362. if (pipe_config->fdi_lanes > 2) {
  3363. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  3364. pipe_name(pipe), pipe_config->fdi_lanes);
  3365. return false;
  3366. }
  3367. } else {
  3368. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  3369. return false;
  3370. }
  3371. return true;
  3372. default:
  3373. BUG();
  3374. }
  3375. }
  3376. #define RETRY 1
  3377. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  3378. struct intel_crtc_config *pipe_config)
  3379. {
  3380. struct drm_device *dev = intel_crtc->base.dev;
  3381. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  3382. int lane, link_bw, fdi_dotclock;
  3383. bool setup_ok, needs_recompute = false;
  3384. retry:
  3385. /* FDI is a binary signal running at ~2.7GHz, encoding
  3386. * each output octet as 10 bits. The actual frequency
  3387. * is stored as a divider into a 100MHz clock, and the
  3388. * mode pixel clock is stored in units of 1KHz.
  3389. * Hence the bw of each lane in terms of the mode signal
  3390. * is:
  3391. */
  3392. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3393. fdi_dotclock = adjusted_mode->clock;
  3394. fdi_dotclock /= pipe_config->pixel_multiplier;
  3395. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  3396. pipe_config->pipe_bpp);
  3397. pipe_config->fdi_lanes = lane;
  3398. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  3399. link_bw, &pipe_config->fdi_m_n);
  3400. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  3401. intel_crtc->pipe, pipe_config);
  3402. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  3403. pipe_config->pipe_bpp -= 2*3;
  3404. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  3405. pipe_config->pipe_bpp);
  3406. needs_recompute = true;
  3407. pipe_config->bw_constrained = true;
  3408. goto retry;
  3409. }
  3410. if (needs_recompute)
  3411. return RETRY;
  3412. return setup_ok ? 0 : -EINVAL;
  3413. }
  3414. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  3415. struct intel_crtc_config *pipe_config)
  3416. {
  3417. pipe_config->ips_enabled = i915_enable_ips &&
  3418. hsw_crtc_supports_ips(crtc) &&
  3419. pipe_config->pipe_bpp == 24;
  3420. }
  3421. static int intel_crtc_compute_config(struct drm_crtc *crtc,
  3422. struct intel_crtc_config *pipe_config)
  3423. {
  3424. struct drm_device *dev = crtc->dev;
  3425. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  3426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3427. if (HAS_PCH_SPLIT(dev)) {
  3428. /* FDI link clock is fixed at 2.7G */
  3429. if (pipe_config->requested_mode.clock * 3
  3430. > IRONLAKE_FDI_FREQ * 4)
  3431. return -EINVAL;
  3432. }
  3433. /* All interlaced capable intel hw wants timings in frames. Note though
  3434. * that intel_lvds_mode_fixup does some funny tricks with the crtc
  3435. * timings, so we need to be careful not to clobber these.*/
  3436. if (!pipe_config->timings_set)
  3437. drm_mode_set_crtcinfo(adjusted_mode, 0);
  3438. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  3439. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  3440. */
  3441. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  3442. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  3443. return -EINVAL;
  3444. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  3445. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  3446. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  3447. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  3448. * for lvds. */
  3449. pipe_config->pipe_bpp = 8*3;
  3450. }
  3451. if (IS_HASWELL(dev))
  3452. hsw_compute_ips_config(intel_crtc, pipe_config);
  3453. if (pipe_config->has_pch_encoder)
  3454. return ironlake_fdi_compute_config(intel_crtc, pipe_config);
  3455. return 0;
  3456. }
  3457. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  3458. {
  3459. return 400000; /* FIXME */
  3460. }
  3461. static int i945_get_display_clock_speed(struct drm_device *dev)
  3462. {
  3463. return 400000;
  3464. }
  3465. static int i915_get_display_clock_speed(struct drm_device *dev)
  3466. {
  3467. return 333000;
  3468. }
  3469. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  3470. {
  3471. return 200000;
  3472. }
  3473. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  3474. {
  3475. u16 gcfgc = 0;
  3476. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  3477. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  3478. return 133000;
  3479. else {
  3480. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  3481. case GC_DISPLAY_CLOCK_333_MHZ:
  3482. return 333000;
  3483. default:
  3484. case GC_DISPLAY_CLOCK_190_200_MHZ:
  3485. return 190000;
  3486. }
  3487. }
  3488. }
  3489. static int i865_get_display_clock_speed(struct drm_device *dev)
  3490. {
  3491. return 266000;
  3492. }
  3493. static int i855_get_display_clock_speed(struct drm_device *dev)
  3494. {
  3495. u16 hpllcc = 0;
  3496. /* Assume that the hardware is in the high speed state. This
  3497. * should be the default.
  3498. */
  3499. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  3500. case GC_CLOCK_133_200:
  3501. case GC_CLOCK_100_200:
  3502. return 200000;
  3503. case GC_CLOCK_166_250:
  3504. return 250000;
  3505. case GC_CLOCK_100_133:
  3506. return 133000;
  3507. }
  3508. /* Shouldn't happen */
  3509. return 0;
  3510. }
  3511. static int i830_get_display_clock_speed(struct drm_device *dev)
  3512. {
  3513. return 133000;
  3514. }
  3515. static void
  3516. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  3517. {
  3518. while (*num > DATA_LINK_M_N_MASK ||
  3519. *den > DATA_LINK_M_N_MASK) {
  3520. *num >>= 1;
  3521. *den >>= 1;
  3522. }
  3523. }
  3524. static void compute_m_n(unsigned int m, unsigned int n,
  3525. uint32_t *ret_m, uint32_t *ret_n)
  3526. {
  3527. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  3528. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  3529. intel_reduce_m_n_ratio(ret_m, ret_n);
  3530. }
  3531. void
  3532. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  3533. int pixel_clock, int link_clock,
  3534. struct intel_link_m_n *m_n)
  3535. {
  3536. m_n->tu = 64;
  3537. compute_m_n(bits_per_pixel * pixel_clock,
  3538. link_clock * nlanes * 8,
  3539. &m_n->gmch_m, &m_n->gmch_n);
  3540. compute_m_n(pixel_clock, link_clock,
  3541. &m_n->link_m, &m_n->link_n);
  3542. }
  3543. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3544. {
  3545. if (i915_panel_use_ssc >= 0)
  3546. return i915_panel_use_ssc != 0;
  3547. return dev_priv->vbt.lvds_use_ssc
  3548. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  3549. }
  3550. static int vlv_get_refclk(struct drm_crtc *crtc)
  3551. {
  3552. struct drm_device *dev = crtc->dev;
  3553. struct drm_i915_private *dev_priv = dev->dev_private;
  3554. int refclk = 27000; /* for DP & HDMI */
  3555. return 100000; /* only one validated so far */
  3556. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  3557. refclk = 96000;
  3558. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  3559. if (intel_panel_use_ssc(dev_priv))
  3560. refclk = 100000;
  3561. else
  3562. refclk = 96000;
  3563. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
  3564. refclk = 100000;
  3565. }
  3566. return refclk;
  3567. }
  3568. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  3569. {
  3570. struct drm_device *dev = crtc->dev;
  3571. struct drm_i915_private *dev_priv = dev->dev_private;
  3572. int refclk;
  3573. if (IS_VALLEYVIEW(dev)) {
  3574. refclk = vlv_get_refclk(crtc);
  3575. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3576. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3577. refclk = dev_priv->vbt.lvds_ssc_freq * 1000;
  3578. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3579. refclk / 1000);
  3580. } else if (!IS_GEN2(dev)) {
  3581. refclk = 96000;
  3582. } else {
  3583. refclk = 48000;
  3584. }
  3585. return refclk;
  3586. }
  3587. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  3588. {
  3589. return (1 << dpll->n) << 16 | dpll->m1 << 8 | dpll->m2;
  3590. }
  3591. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  3592. {
  3593. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  3594. }
  3595. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  3596. intel_clock_t *reduced_clock)
  3597. {
  3598. struct drm_device *dev = crtc->base.dev;
  3599. struct drm_i915_private *dev_priv = dev->dev_private;
  3600. int pipe = crtc->pipe;
  3601. u32 fp, fp2 = 0;
  3602. if (IS_PINEVIEW(dev)) {
  3603. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  3604. if (reduced_clock)
  3605. fp2 = pnv_dpll_compute_fp(reduced_clock);
  3606. } else {
  3607. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  3608. if (reduced_clock)
  3609. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  3610. }
  3611. I915_WRITE(FP0(pipe), fp);
  3612. crtc->lowfreq_avail = false;
  3613. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  3614. reduced_clock && i915_powersave) {
  3615. I915_WRITE(FP1(pipe), fp2);
  3616. crtc->lowfreq_avail = true;
  3617. } else {
  3618. I915_WRITE(FP1(pipe), fp);
  3619. }
  3620. }
  3621. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv)
  3622. {
  3623. u32 reg_val;
  3624. /*
  3625. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  3626. * and set it to a reasonable value instead.
  3627. */
  3628. reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
  3629. reg_val &= 0xffffff00;
  3630. reg_val |= 0x00000030;
  3631. vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
  3632. reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
  3633. reg_val &= 0x8cffffff;
  3634. reg_val = 0x8c000000;
  3635. vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
  3636. reg_val = vlv_dpio_read(dev_priv, DPIO_IREF(1));
  3637. reg_val &= 0xffffff00;
  3638. vlv_dpio_write(dev_priv, DPIO_IREF(1), reg_val);
  3639. reg_val = vlv_dpio_read(dev_priv, DPIO_CALIBRATION);
  3640. reg_val &= 0x00ffffff;
  3641. reg_val |= 0xb0000000;
  3642. vlv_dpio_write(dev_priv, DPIO_CALIBRATION, reg_val);
  3643. }
  3644. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  3645. struct intel_link_m_n *m_n)
  3646. {
  3647. struct drm_device *dev = crtc->base.dev;
  3648. struct drm_i915_private *dev_priv = dev->dev_private;
  3649. int pipe = crtc->pipe;
  3650. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  3651. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  3652. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  3653. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  3654. }
  3655. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  3656. struct intel_link_m_n *m_n)
  3657. {
  3658. struct drm_device *dev = crtc->base.dev;
  3659. struct drm_i915_private *dev_priv = dev->dev_private;
  3660. int pipe = crtc->pipe;
  3661. enum transcoder transcoder = crtc->config.cpu_transcoder;
  3662. if (INTEL_INFO(dev)->gen >= 5) {
  3663. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  3664. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  3665. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  3666. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  3667. } else {
  3668. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  3669. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  3670. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  3671. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  3672. }
  3673. }
  3674. static void intel_dp_set_m_n(struct intel_crtc *crtc)
  3675. {
  3676. if (crtc->config.has_pch_encoder)
  3677. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  3678. else
  3679. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  3680. }
  3681. static void vlv_update_pll(struct intel_crtc *crtc)
  3682. {
  3683. struct drm_device *dev = crtc->base.dev;
  3684. struct drm_i915_private *dev_priv = dev->dev_private;
  3685. struct intel_encoder *encoder;
  3686. int pipe = crtc->pipe;
  3687. u32 dpll, mdiv;
  3688. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  3689. bool is_hdmi;
  3690. u32 coreclk, reg_val, dpll_md;
  3691. mutex_lock(&dev_priv->dpio_lock);
  3692. is_hdmi = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  3693. bestn = crtc->config.dpll.n;
  3694. bestm1 = crtc->config.dpll.m1;
  3695. bestm2 = crtc->config.dpll.m2;
  3696. bestp1 = crtc->config.dpll.p1;
  3697. bestp2 = crtc->config.dpll.p2;
  3698. /* See eDP HDMI DPIO driver vbios notes doc */
  3699. /* PLL B needs special handling */
  3700. if (pipe)
  3701. vlv_pllb_recal_opamp(dev_priv);
  3702. /* Set up Tx target for periodic Rcomp update */
  3703. vlv_dpio_write(dev_priv, DPIO_IREF_BCAST, 0x0100000f);
  3704. /* Disable target IRef on PLL */
  3705. reg_val = vlv_dpio_read(dev_priv, DPIO_IREF_CTL(pipe));
  3706. reg_val &= 0x00ffffff;
  3707. vlv_dpio_write(dev_priv, DPIO_IREF_CTL(pipe), reg_val);
  3708. /* Disable fast lock */
  3709. vlv_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x610);
  3710. /* Set idtafcrecal before PLL is enabled */
  3711. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  3712. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  3713. mdiv |= ((bestn << DPIO_N_SHIFT));
  3714. mdiv |= (1 << DPIO_K_SHIFT);
  3715. /*
  3716. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  3717. * but we don't support that).
  3718. * Note: don't use the DAC post divider as it seems unstable.
  3719. */
  3720. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  3721. vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
  3722. mdiv |= DPIO_ENABLE_CALIBRATION;
  3723. vlv_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
  3724. /* Set HBR and RBR LPF coefficients */
  3725. if (crtc->config.port_clock == 162000 ||
  3726. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  3727. vlv_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
  3728. 0x005f0021);
  3729. else
  3730. vlv_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe),
  3731. 0x00d0000f);
  3732. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  3733. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  3734. /* Use SSC source */
  3735. if (!pipe)
  3736. vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
  3737. 0x0df40000);
  3738. else
  3739. vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
  3740. 0x0df70000);
  3741. } else { /* HDMI or VGA */
  3742. /* Use bend source */
  3743. if (!pipe)
  3744. vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
  3745. 0x0df70000);
  3746. else
  3747. vlv_dpio_write(dev_priv, DPIO_REFSFR(pipe),
  3748. 0x0df40000);
  3749. }
  3750. coreclk = vlv_dpio_read(dev_priv, DPIO_CORE_CLK(pipe));
  3751. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  3752. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  3753. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  3754. coreclk |= 0x01000000;
  3755. vlv_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), coreclk);
  3756. vlv_dpio_write(dev_priv, DPIO_PLL_CML(pipe), 0x87871000);
  3757. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  3758. if (encoder->pre_pll_enable)
  3759. encoder->pre_pll_enable(encoder);
  3760. /* Enable DPIO clock input */
  3761. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  3762. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  3763. if (pipe)
  3764. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  3765. dpll |= DPLL_VCO_ENABLE;
  3766. I915_WRITE(DPLL(pipe), dpll);
  3767. POSTING_READ(DPLL(pipe));
  3768. udelay(150);
  3769. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  3770. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  3771. dpll_md = (crtc->config.pixel_multiplier - 1)
  3772. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3773. I915_WRITE(DPLL_MD(pipe), dpll_md);
  3774. POSTING_READ(DPLL_MD(pipe));
  3775. if (crtc->config.has_dp_encoder)
  3776. intel_dp_set_m_n(crtc);
  3777. mutex_unlock(&dev_priv->dpio_lock);
  3778. }
  3779. static void i9xx_update_pll(struct intel_crtc *crtc,
  3780. intel_clock_t *reduced_clock,
  3781. int num_connectors)
  3782. {
  3783. struct drm_device *dev = crtc->base.dev;
  3784. struct drm_i915_private *dev_priv = dev->dev_private;
  3785. struct intel_encoder *encoder;
  3786. int pipe = crtc->pipe;
  3787. u32 dpll;
  3788. bool is_sdvo;
  3789. struct dpll *clock = &crtc->config.dpll;
  3790. i9xx_update_pll_dividers(crtc, reduced_clock);
  3791. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  3792. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  3793. dpll = DPLL_VGA_MODE_DIS;
  3794. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  3795. dpll |= DPLLB_MODE_LVDS;
  3796. else
  3797. dpll |= DPLLB_MODE_DAC_SERIAL;
  3798. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  3799. dpll |= (crtc->config.pixel_multiplier - 1)
  3800. << SDVO_MULTIPLIER_SHIFT_HIRES;
  3801. }
  3802. if (is_sdvo)
  3803. dpll |= DPLL_DVO_HIGH_SPEED;
  3804. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  3805. dpll |= DPLL_DVO_HIGH_SPEED;
  3806. /* compute bitmask from p1 value */
  3807. if (IS_PINEVIEW(dev))
  3808. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3809. else {
  3810. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3811. if (IS_G4X(dev) && reduced_clock)
  3812. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3813. }
  3814. switch (clock->p2) {
  3815. case 5:
  3816. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3817. break;
  3818. case 7:
  3819. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3820. break;
  3821. case 10:
  3822. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3823. break;
  3824. case 14:
  3825. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3826. break;
  3827. }
  3828. if (INTEL_INFO(dev)->gen >= 4)
  3829. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3830. if (crtc->config.sdvo_tv_clock)
  3831. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3832. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  3833. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3834. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3835. else
  3836. dpll |= PLL_REF_INPUT_DREFCLK;
  3837. dpll |= DPLL_VCO_ENABLE;
  3838. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3839. POSTING_READ(DPLL(pipe));
  3840. udelay(150);
  3841. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  3842. if (encoder->pre_pll_enable)
  3843. encoder->pre_pll_enable(encoder);
  3844. if (crtc->config.has_dp_encoder)
  3845. intel_dp_set_m_n(crtc);
  3846. I915_WRITE(DPLL(pipe), dpll);
  3847. /* Wait for the clocks to stabilize. */
  3848. POSTING_READ(DPLL(pipe));
  3849. udelay(150);
  3850. if (INTEL_INFO(dev)->gen >= 4) {
  3851. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  3852. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3853. I915_WRITE(DPLL_MD(pipe), dpll_md);
  3854. } else {
  3855. /* The pixel multiplier can only be updated once the
  3856. * DPLL is enabled and the clocks are stable.
  3857. *
  3858. * So write it again.
  3859. */
  3860. I915_WRITE(DPLL(pipe), dpll);
  3861. }
  3862. }
  3863. static void i8xx_update_pll(struct intel_crtc *crtc,
  3864. intel_clock_t *reduced_clock,
  3865. int num_connectors)
  3866. {
  3867. struct drm_device *dev = crtc->base.dev;
  3868. struct drm_i915_private *dev_priv = dev->dev_private;
  3869. struct intel_encoder *encoder;
  3870. int pipe = crtc->pipe;
  3871. u32 dpll;
  3872. struct dpll *clock = &crtc->config.dpll;
  3873. i9xx_update_pll_dividers(crtc, reduced_clock);
  3874. dpll = DPLL_VGA_MODE_DIS;
  3875. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  3876. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3877. } else {
  3878. if (clock->p1 == 2)
  3879. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3880. else
  3881. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3882. if (clock->p2 == 4)
  3883. dpll |= PLL_P2_DIVIDE_BY_4;
  3884. }
  3885. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  3886. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3887. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3888. else
  3889. dpll |= PLL_REF_INPUT_DREFCLK;
  3890. dpll |= DPLL_VCO_ENABLE;
  3891. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3892. POSTING_READ(DPLL(pipe));
  3893. udelay(150);
  3894. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  3895. if (encoder->pre_pll_enable)
  3896. encoder->pre_pll_enable(encoder);
  3897. I915_WRITE(DPLL(pipe), dpll);
  3898. /* Wait for the clocks to stabilize. */
  3899. POSTING_READ(DPLL(pipe));
  3900. udelay(150);
  3901. /* The pixel multiplier can only be updated once the
  3902. * DPLL is enabled and the clocks are stable.
  3903. *
  3904. * So write it again.
  3905. */
  3906. I915_WRITE(DPLL(pipe), dpll);
  3907. }
  3908. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  3909. {
  3910. struct drm_device *dev = intel_crtc->base.dev;
  3911. struct drm_i915_private *dev_priv = dev->dev_private;
  3912. enum pipe pipe = intel_crtc->pipe;
  3913. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3914. struct drm_display_mode *adjusted_mode =
  3915. &intel_crtc->config.adjusted_mode;
  3916. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  3917. uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
  3918. /* We need to be careful not to changed the adjusted mode, for otherwise
  3919. * the hw state checker will get angry at the mismatch. */
  3920. crtc_vtotal = adjusted_mode->crtc_vtotal;
  3921. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  3922. if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3923. /* the chip adds 2 halflines automatically */
  3924. crtc_vtotal -= 1;
  3925. crtc_vblank_end -= 1;
  3926. vsyncshift = adjusted_mode->crtc_hsync_start
  3927. - adjusted_mode->crtc_htotal / 2;
  3928. } else {
  3929. vsyncshift = 0;
  3930. }
  3931. if (INTEL_INFO(dev)->gen > 3)
  3932. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  3933. I915_WRITE(HTOTAL(cpu_transcoder),
  3934. (adjusted_mode->crtc_hdisplay - 1) |
  3935. ((adjusted_mode->crtc_htotal - 1) << 16));
  3936. I915_WRITE(HBLANK(cpu_transcoder),
  3937. (adjusted_mode->crtc_hblank_start - 1) |
  3938. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3939. I915_WRITE(HSYNC(cpu_transcoder),
  3940. (adjusted_mode->crtc_hsync_start - 1) |
  3941. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3942. I915_WRITE(VTOTAL(cpu_transcoder),
  3943. (adjusted_mode->crtc_vdisplay - 1) |
  3944. ((crtc_vtotal - 1) << 16));
  3945. I915_WRITE(VBLANK(cpu_transcoder),
  3946. (adjusted_mode->crtc_vblank_start - 1) |
  3947. ((crtc_vblank_end - 1) << 16));
  3948. I915_WRITE(VSYNC(cpu_transcoder),
  3949. (adjusted_mode->crtc_vsync_start - 1) |
  3950. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3951. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  3952. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  3953. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  3954. * bits. */
  3955. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  3956. (pipe == PIPE_B || pipe == PIPE_C))
  3957. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  3958. /* pipesrc controls the size that is scaled from, which should
  3959. * always be the user's requested size.
  3960. */
  3961. I915_WRITE(PIPESRC(pipe),
  3962. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3963. }
  3964. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  3965. struct intel_crtc_config *pipe_config)
  3966. {
  3967. struct drm_device *dev = crtc->base.dev;
  3968. struct drm_i915_private *dev_priv = dev->dev_private;
  3969. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  3970. uint32_t tmp;
  3971. tmp = I915_READ(HTOTAL(cpu_transcoder));
  3972. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  3973. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  3974. tmp = I915_READ(HBLANK(cpu_transcoder));
  3975. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  3976. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  3977. tmp = I915_READ(HSYNC(cpu_transcoder));
  3978. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  3979. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  3980. tmp = I915_READ(VTOTAL(cpu_transcoder));
  3981. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  3982. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  3983. tmp = I915_READ(VBLANK(cpu_transcoder));
  3984. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  3985. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  3986. tmp = I915_READ(VSYNC(cpu_transcoder));
  3987. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  3988. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  3989. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  3990. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  3991. pipe_config->adjusted_mode.crtc_vtotal += 1;
  3992. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  3993. }
  3994. tmp = I915_READ(PIPESRC(crtc->pipe));
  3995. pipe_config->requested_mode.vdisplay = (tmp & 0xffff) + 1;
  3996. pipe_config->requested_mode.hdisplay = ((tmp >> 16) & 0xffff) + 1;
  3997. }
  3998. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  3999. {
  4000. struct drm_device *dev = intel_crtc->base.dev;
  4001. struct drm_i915_private *dev_priv = dev->dev_private;
  4002. uint32_t pipeconf;
  4003. pipeconf = I915_READ(PIPECONF(intel_crtc->pipe));
  4004. if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  4005. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  4006. * core speed.
  4007. *
  4008. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  4009. * pipe == 0 check?
  4010. */
  4011. if (intel_crtc->config.requested_mode.clock >
  4012. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  4013. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4014. else
  4015. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  4016. }
  4017. /* only g4x and later have fancy bpc/dither controls */
  4018. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  4019. pipeconf &= ~(PIPECONF_BPC_MASK |
  4020. PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
  4021. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  4022. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  4023. pipeconf |= PIPECONF_DITHER_EN |
  4024. PIPECONF_DITHER_TYPE_SP;
  4025. switch (intel_crtc->config.pipe_bpp) {
  4026. case 18:
  4027. pipeconf |= PIPECONF_6BPC;
  4028. break;
  4029. case 24:
  4030. pipeconf |= PIPECONF_8BPC;
  4031. break;
  4032. case 30:
  4033. pipeconf |= PIPECONF_10BPC;
  4034. break;
  4035. default:
  4036. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4037. BUG();
  4038. }
  4039. }
  4040. if (HAS_PIPE_CXSR(dev)) {
  4041. if (intel_crtc->lowfreq_avail) {
  4042. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4043. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4044. } else {
  4045. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4046. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4047. }
  4048. }
  4049. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  4050. if (!IS_GEN2(dev) &&
  4051. intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  4052. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4053. else
  4054. pipeconf |= PIPECONF_PROGRESSIVE;
  4055. if (IS_VALLEYVIEW(dev)) {
  4056. if (intel_crtc->config.limited_color_range)
  4057. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  4058. else
  4059. pipeconf &= ~PIPECONF_COLOR_RANGE_SELECT;
  4060. }
  4061. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  4062. POSTING_READ(PIPECONF(intel_crtc->pipe));
  4063. }
  4064. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4065. int x, int y,
  4066. struct drm_framebuffer *fb)
  4067. {
  4068. struct drm_device *dev = crtc->dev;
  4069. struct drm_i915_private *dev_priv = dev->dev_private;
  4070. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4071. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  4072. int pipe = intel_crtc->pipe;
  4073. int plane = intel_crtc->plane;
  4074. int refclk, num_connectors = 0;
  4075. intel_clock_t clock, reduced_clock;
  4076. u32 dspcntr;
  4077. bool ok, has_reduced_clock = false;
  4078. bool is_lvds = false;
  4079. struct intel_encoder *encoder;
  4080. const intel_limit_t *limit;
  4081. int ret;
  4082. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4083. switch (encoder->type) {
  4084. case INTEL_OUTPUT_LVDS:
  4085. is_lvds = true;
  4086. break;
  4087. }
  4088. num_connectors++;
  4089. }
  4090. refclk = i9xx_get_refclk(crtc, num_connectors);
  4091. /*
  4092. * Returns a set of divisors for the desired target clock with the given
  4093. * refclk, or FALSE. The returned values represent the clock equation:
  4094. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4095. */
  4096. limit = intel_limit(crtc, refclk);
  4097. ok = dev_priv->display.find_dpll(limit, crtc,
  4098. intel_crtc->config.port_clock,
  4099. refclk, NULL, &clock);
  4100. if (!ok && !intel_crtc->config.clock_set) {
  4101. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4102. return -EINVAL;
  4103. }
  4104. /* Ensure that the cursor is valid for the new mode before changing... */
  4105. intel_crtc_update_cursor(crtc, true);
  4106. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4107. /*
  4108. * Ensure we match the reduced clock's P to the target clock.
  4109. * If the clocks don't match, we can't switch the display clock
  4110. * by using the FP0/FP1. In such case we will disable the LVDS
  4111. * downclock feature.
  4112. */
  4113. has_reduced_clock =
  4114. dev_priv->display.find_dpll(limit, crtc,
  4115. dev_priv->lvds_downclock,
  4116. refclk, &clock,
  4117. &reduced_clock);
  4118. }
  4119. /* Compat-code for transition, will disappear. */
  4120. if (!intel_crtc->config.clock_set) {
  4121. intel_crtc->config.dpll.n = clock.n;
  4122. intel_crtc->config.dpll.m1 = clock.m1;
  4123. intel_crtc->config.dpll.m2 = clock.m2;
  4124. intel_crtc->config.dpll.p1 = clock.p1;
  4125. intel_crtc->config.dpll.p2 = clock.p2;
  4126. }
  4127. if (IS_GEN2(dev))
  4128. i8xx_update_pll(intel_crtc,
  4129. has_reduced_clock ? &reduced_clock : NULL,
  4130. num_connectors);
  4131. else if (IS_VALLEYVIEW(dev))
  4132. vlv_update_pll(intel_crtc);
  4133. else
  4134. i9xx_update_pll(intel_crtc,
  4135. has_reduced_clock ? &reduced_clock : NULL,
  4136. num_connectors);
  4137. /* Set up the display plane register */
  4138. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4139. if (!IS_VALLEYVIEW(dev)) {
  4140. if (pipe == 0)
  4141. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4142. else
  4143. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4144. }
  4145. intel_set_pipe_timings(intel_crtc);
  4146. /* pipesrc and dspsize control the size that is scaled from,
  4147. * which should always be the user's requested size.
  4148. */
  4149. I915_WRITE(DSPSIZE(plane),
  4150. ((mode->vdisplay - 1) << 16) |
  4151. (mode->hdisplay - 1));
  4152. I915_WRITE(DSPPOS(plane), 0);
  4153. i9xx_set_pipeconf(intel_crtc);
  4154. I915_WRITE(DSPCNTR(plane), dspcntr);
  4155. POSTING_READ(DSPCNTR(plane));
  4156. ret = intel_pipe_set_base(crtc, x, y, fb);
  4157. intel_update_watermarks(dev);
  4158. return ret;
  4159. }
  4160. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  4161. struct intel_crtc_config *pipe_config)
  4162. {
  4163. struct drm_device *dev = crtc->base.dev;
  4164. struct drm_i915_private *dev_priv = dev->dev_private;
  4165. uint32_t tmp;
  4166. tmp = I915_READ(PFIT_CONTROL);
  4167. if (INTEL_INFO(dev)->gen < 4) {
  4168. if (crtc->pipe != PIPE_B)
  4169. return;
  4170. /* gen2/3 store dither state in pfit control, needs to match */
  4171. pipe_config->gmch_pfit.control = tmp & PANEL_8TO6_DITHER_ENABLE;
  4172. } else {
  4173. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  4174. return;
  4175. }
  4176. if (!(tmp & PFIT_ENABLE))
  4177. return;
  4178. pipe_config->gmch_pfit.control = I915_READ(PFIT_CONTROL);
  4179. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  4180. if (INTEL_INFO(dev)->gen < 5)
  4181. pipe_config->gmch_pfit.lvds_border_bits =
  4182. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  4183. }
  4184. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  4185. struct intel_crtc_config *pipe_config)
  4186. {
  4187. struct drm_device *dev = crtc->base.dev;
  4188. struct drm_i915_private *dev_priv = dev->dev_private;
  4189. uint32_t tmp;
  4190. pipe_config->cpu_transcoder = crtc->pipe;
  4191. tmp = I915_READ(PIPECONF(crtc->pipe));
  4192. if (!(tmp & PIPECONF_ENABLE))
  4193. return false;
  4194. intel_get_pipe_timings(crtc, pipe_config);
  4195. i9xx_get_pfit_config(crtc, pipe_config);
  4196. return true;
  4197. }
  4198. static void ironlake_init_pch_refclk(struct drm_device *dev)
  4199. {
  4200. struct drm_i915_private *dev_priv = dev->dev_private;
  4201. struct drm_mode_config *mode_config = &dev->mode_config;
  4202. struct intel_encoder *encoder;
  4203. u32 val, final;
  4204. bool has_lvds = false;
  4205. bool has_cpu_edp = false;
  4206. bool has_panel = false;
  4207. bool has_ck505 = false;
  4208. bool can_ssc = false;
  4209. /* We need to take the global config into account */
  4210. list_for_each_entry(encoder, &mode_config->encoder_list,
  4211. base.head) {
  4212. switch (encoder->type) {
  4213. case INTEL_OUTPUT_LVDS:
  4214. has_panel = true;
  4215. has_lvds = true;
  4216. break;
  4217. case INTEL_OUTPUT_EDP:
  4218. has_panel = true;
  4219. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  4220. has_cpu_edp = true;
  4221. break;
  4222. }
  4223. }
  4224. if (HAS_PCH_IBX(dev)) {
  4225. has_ck505 = dev_priv->vbt.display_clock_mode;
  4226. can_ssc = has_ck505;
  4227. } else {
  4228. has_ck505 = false;
  4229. can_ssc = true;
  4230. }
  4231. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  4232. has_panel, has_lvds, has_ck505);
  4233. /* Ironlake: try to setup display ref clock before DPLL
  4234. * enabling. This is only under driver's control after
  4235. * PCH B stepping, previous chipset stepping should be
  4236. * ignoring this setting.
  4237. */
  4238. val = I915_READ(PCH_DREF_CONTROL);
  4239. /* As we must carefully and slowly disable/enable each source in turn,
  4240. * compute the final state we want first and check if we need to
  4241. * make any changes at all.
  4242. */
  4243. final = val;
  4244. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  4245. if (has_ck505)
  4246. final |= DREF_NONSPREAD_CK505_ENABLE;
  4247. else
  4248. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  4249. final &= ~DREF_SSC_SOURCE_MASK;
  4250. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4251. final &= ~DREF_SSC1_ENABLE;
  4252. if (has_panel) {
  4253. final |= DREF_SSC_SOURCE_ENABLE;
  4254. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  4255. final |= DREF_SSC1_ENABLE;
  4256. if (has_cpu_edp) {
  4257. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  4258. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4259. else
  4260. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4261. } else
  4262. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4263. } else {
  4264. final |= DREF_SSC_SOURCE_DISABLE;
  4265. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4266. }
  4267. if (final == val)
  4268. return;
  4269. /* Always enable nonspread source */
  4270. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  4271. if (has_ck505)
  4272. val |= DREF_NONSPREAD_CK505_ENABLE;
  4273. else
  4274. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  4275. if (has_panel) {
  4276. val &= ~DREF_SSC_SOURCE_MASK;
  4277. val |= DREF_SSC_SOURCE_ENABLE;
  4278. /* SSC must be turned on before enabling the CPU output */
  4279. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4280. DRM_DEBUG_KMS("Using SSC on panel\n");
  4281. val |= DREF_SSC1_ENABLE;
  4282. } else
  4283. val &= ~DREF_SSC1_ENABLE;
  4284. /* Get SSC going before enabling the outputs */
  4285. I915_WRITE(PCH_DREF_CONTROL, val);
  4286. POSTING_READ(PCH_DREF_CONTROL);
  4287. udelay(200);
  4288. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4289. /* Enable CPU source on CPU attached eDP */
  4290. if (has_cpu_edp) {
  4291. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4292. DRM_DEBUG_KMS("Using SSC on eDP\n");
  4293. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4294. }
  4295. else
  4296. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4297. } else
  4298. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4299. I915_WRITE(PCH_DREF_CONTROL, val);
  4300. POSTING_READ(PCH_DREF_CONTROL);
  4301. udelay(200);
  4302. } else {
  4303. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  4304. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4305. /* Turn off CPU output */
  4306. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4307. I915_WRITE(PCH_DREF_CONTROL, val);
  4308. POSTING_READ(PCH_DREF_CONTROL);
  4309. udelay(200);
  4310. /* Turn off the SSC source */
  4311. val &= ~DREF_SSC_SOURCE_MASK;
  4312. val |= DREF_SSC_SOURCE_DISABLE;
  4313. /* Turn off SSC1 */
  4314. val &= ~DREF_SSC1_ENABLE;
  4315. I915_WRITE(PCH_DREF_CONTROL, val);
  4316. POSTING_READ(PCH_DREF_CONTROL);
  4317. udelay(200);
  4318. }
  4319. BUG_ON(val != final);
  4320. }
  4321. /* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
  4322. static void lpt_init_pch_refclk(struct drm_device *dev)
  4323. {
  4324. struct drm_i915_private *dev_priv = dev->dev_private;
  4325. struct drm_mode_config *mode_config = &dev->mode_config;
  4326. struct intel_encoder *encoder;
  4327. bool has_vga = false;
  4328. bool is_sdv = false;
  4329. u32 tmp;
  4330. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4331. switch (encoder->type) {
  4332. case INTEL_OUTPUT_ANALOG:
  4333. has_vga = true;
  4334. break;
  4335. }
  4336. }
  4337. if (!has_vga)
  4338. return;
  4339. mutex_lock(&dev_priv->dpio_lock);
  4340. /* XXX: Rip out SDV support once Haswell ships for real. */
  4341. if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
  4342. is_sdv = true;
  4343. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4344. tmp &= ~SBI_SSCCTL_DISABLE;
  4345. tmp |= SBI_SSCCTL_PATHALT;
  4346. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4347. udelay(24);
  4348. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4349. tmp &= ~SBI_SSCCTL_PATHALT;
  4350. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4351. if (!is_sdv) {
  4352. tmp = I915_READ(SOUTH_CHICKEN2);
  4353. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  4354. I915_WRITE(SOUTH_CHICKEN2, tmp);
  4355. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  4356. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  4357. DRM_ERROR("FDI mPHY reset assert timeout\n");
  4358. tmp = I915_READ(SOUTH_CHICKEN2);
  4359. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  4360. I915_WRITE(SOUTH_CHICKEN2, tmp);
  4361. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  4362. FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
  4363. 100))
  4364. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  4365. }
  4366. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  4367. tmp &= ~(0xFF << 24);
  4368. tmp |= (0x12 << 24);
  4369. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  4370. if (is_sdv) {
  4371. tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
  4372. tmp |= 0x7FFF;
  4373. intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
  4374. }
  4375. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  4376. tmp |= (1 << 11);
  4377. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  4378. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  4379. tmp |= (1 << 11);
  4380. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  4381. if (is_sdv) {
  4382. tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
  4383. tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
  4384. intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
  4385. tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
  4386. tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
  4387. intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
  4388. tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
  4389. tmp |= (0x3F << 8);
  4390. intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
  4391. tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
  4392. tmp |= (0x3F << 8);
  4393. intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
  4394. }
  4395. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  4396. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  4397. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  4398. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  4399. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  4400. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  4401. if (!is_sdv) {
  4402. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  4403. tmp &= ~(7 << 13);
  4404. tmp |= (5 << 13);
  4405. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  4406. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  4407. tmp &= ~(7 << 13);
  4408. tmp |= (5 << 13);
  4409. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  4410. }
  4411. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  4412. tmp &= ~0xFF;
  4413. tmp |= 0x1C;
  4414. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  4415. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  4416. tmp &= ~0xFF;
  4417. tmp |= 0x1C;
  4418. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  4419. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  4420. tmp &= ~(0xFF << 16);
  4421. tmp |= (0x1C << 16);
  4422. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  4423. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  4424. tmp &= ~(0xFF << 16);
  4425. tmp |= (0x1C << 16);
  4426. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  4427. if (!is_sdv) {
  4428. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  4429. tmp |= (1 << 27);
  4430. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  4431. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  4432. tmp |= (1 << 27);
  4433. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  4434. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  4435. tmp &= ~(0xF << 28);
  4436. tmp |= (4 << 28);
  4437. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  4438. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  4439. tmp &= ~(0xF << 28);
  4440. tmp |= (4 << 28);
  4441. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  4442. }
  4443. /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
  4444. tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
  4445. tmp |= SBI_DBUFF0_ENABLE;
  4446. intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
  4447. mutex_unlock(&dev_priv->dpio_lock);
  4448. }
  4449. /*
  4450. * Initialize reference clocks when the driver loads
  4451. */
  4452. void intel_init_pch_refclk(struct drm_device *dev)
  4453. {
  4454. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  4455. ironlake_init_pch_refclk(dev);
  4456. else if (HAS_PCH_LPT(dev))
  4457. lpt_init_pch_refclk(dev);
  4458. }
  4459. static int ironlake_get_refclk(struct drm_crtc *crtc)
  4460. {
  4461. struct drm_device *dev = crtc->dev;
  4462. struct drm_i915_private *dev_priv = dev->dev_private;
  4463. struct intel_encoder *encoder;
  4464. int num_connectors = 0;
  4465. bool is_lvds = false;
  4466. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4467. switch (encoder->type) {
  4468. case INTEL_OUTPUT_LVDS:
  4469. is_lvds = true;
  4470. break;
  4471. }
  4472. num_connectors++;
  4473. }
  4474. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4475. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4476. dev_priv->vbt.lvds_ssc_freq);
  4477. return dev_priv->vbt.lvds_ssc_freq * 1000;
  4478. }
  4479. return 120000;
  4480. }
  4481. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  4482. {
  4483. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4484. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4485. int pipe = intel_crtc->pipe;
  4486. uint32_t val;
  4487. val = I915_READ(PIPECONF(pipe));
  4488. val &= ~PIPECONF_BPC_MASK;
  4489. switch (intel_crtc->config.pipe_bpp) {
  4490. case 18:
  4491. val |= PIPECONF_6BPC;
  4492. break;
  4493. case 24:
  4494. val |= PIPECONF_8BPC;
  4495. break;
  4496. case 30:
  4497. val |= PIPECONF_10BPC;
  4498. break;
  4499. case 36:
  4500. val |= PIPECONF_12BPC;
  4501. break;
  4502. default:
  4503. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4504. BUG();
  4505. }
  4506. val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
  4507. if (intel_crtc->config.dither)
  4508. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  4509. val &= ~PIPECONF_INTERLACE_MASK;
  4510. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  4511. val |= PIPECONF_INTERLACED_ILK;
  4512. else
  4513. val |= PIPECONF_PROGRESSIVE;
  4514. if (intel_crtc->config.limited_color_range)
  4515. val |= PIPECONF_COLOR_RANGE_SELECT;
  4516. else
  4517. val &= ~PIPECONF_COLOR_RANGE_SELECT;
  4518. I915_WRITE(PIPECONF(pipe), val);
  4519. POSTING_READ(PIPECONF(pipe));
  4520. }
  4521. /*
  4522. * Set up the pipe CSC unit.
  4523. *
  4524. * Currently only full range RGB to limited range RGB conversion
  4525. * is supported, but eventually this should handle various
  4526. * RGB<->YCbCr scenarios as well.
  4527. */
  4528. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  4529. {
  4530. struct drm_device *dev = crtc->dev;
  4531. struct drm_i915_private *dev_priv = dev->dev_private;
  4532. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4533. int pipe = intel_crtc->pipe;
  4534. uint16_t coeff = 0x7800; /* 1.0 */
  4535. /*
  4536. * TODO: Check what kind of values actually come out of the pipe
  4537. * with these coeff/postoff values and adjust to get the best
  4538. * accuracy. Perhaps we even need to take the bpc value into
  4539. * consideration.
  4540. */
  4541. if (intel_crtc->config.limited_color_range)
  4542. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  4543. /*
  4544. * GY/GU and RY/RU should be the other way around according
  4545. * to BSpec, but reality doesn't agree. Just set them up in
  4546. * a way that results in the correct picture.
  4547. */
  4548. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  4549. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  4550. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  4551. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  4552. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  4553. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  4554. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  4555. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  4556. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  4557. if (INTEL_INFO(dev)->gen > 6) {
  4558. uint16_t postoff = 0;
  4559. if (intel_crtc->config.limited_color_range)
  4560. postoff = (16 * (1 << 13) / 255) & 0x1fff;
  4561. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  4562. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  4563. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  4564. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  4565. } else {
  4566. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  4567. if (intel_crtc->config.limited_color_range)
  4568. mode |= CSC_BLACK_SCREEN_OFFSET;
  4569. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  4570. }
  4571. }
  4572. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  4573. {
  4574. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4575. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4576. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4577. uint32_t val;
  4578. val = I915_READ(PIPECONF(cpu_transcoder));
  4579. val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
  4580. if (intel_crtc->config.dither)
  4581. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  4582. val &= ~PIPECONF_INTERLACE_MASK_HSW;
  4583. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  4584. val |= PIPECONF_INTERLACED_ILK;
  4585. else
  4586. val |= PIPECONF_PROGRESSIVE;
  4587. I915_WRITE(PIPECONF(cpu_transcoder), val);
  4588. POSTING_READ(PIPECONF(cpu_transcoder));
  4589. }
  4590. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  4591. intel_clock_t *clock,
  4592. bool *has_reduced_clock,
  4593. intel_clock_t *reduced_clock)
  4594. {
  4595. struct drm_device *dev = crtc->dev;
  4596. struct drm_i915_private *dev_priv = dev->dev_private;
  4597. struct intel_encoder *intel_encoder;
  4598. int refclk;
  4599. const intel_limit_t *limit;
  4600. bool ret, is_lvds = false;
  4601. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4602. switch (intel_encoder->type) {
  4603. case INTEL_OUTPUT_LVDS:
  4604. is_lvds = true;
  4605. break;
  4606. }
  4607. }
  4608. refclk = ironlake_get_refclk(crtc);
  4609. /*
  4610. * Returns a set of divisors for the desired target clock with the given
  4611. * refclk, or FALSE. The returned values represent the clock equation:
  4612. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4613. */
  4614. limit = intel_limit(crtc, refclk);
  4615. ret = dev_priv->display.find_dpll(limit, crtc,
  4616. to_intel_crtc(crtc)->config.port_clock,
  4617. refclk, NULL, clock);
  4618. if (!ret)
  4619. return false;
  4620. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4621. /*
  4622. * Ensure we match the reduced clock's P to the target clock.
  4623. * If the clocks don't match, we can't switch the display clock
  4624. * by using the FP0/FP1. In such case we will disable the LVDS
  4625. * downclock feature.
  4626. */
  4627. *has_reduced_clock =
  4628. dev_priv->display.find_dpll(limit, crtc,
  4629. dev_priv->lvds_downclock,
  4630. refclk, clock,
  4631. reduced_clock);
  4632. }
  4633. return true;
  4634. }
  4635. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  4636. {
  4637. struct drm_i915_private *dev_priv = dev->dev_private;
  4638. uint32_t temp;
  4639. temp = I915_READ(SOUTH_CHICKEN1);
  4640. if (temp & FDI_BC_BIFURCATION_SELECT)
  4641. return;
  4642. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  4643. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  4644. temp |= FDI_BC_BIFURCATION_SELECT;
  4645. DRM_DEBUG_KMS("enabling fdi C rx\n");
  4646. I915_WRITE(SOUTH_CHICKEN1, temp);
  4647. POSTING_READ(SOUTH_CHICKEN1);
  4648. }
  4649. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  4650. {
  4651. struct drm_device *dev = intel_crtc->base.dev;
  4652. struct drm_i915_private *dev_priv = dev->dev_private;
  4653. switch (intel_crtc->pipe) {
  4654. case PIPE_A:
  4655. break;
  4656. case PIPE_B:
  4657. if (intel_crtc->config.fdi_lanes > 2)
  4658. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  4659. else
  4660. cpt_enable_fdi_bc_bifurcation(dev);
  4661. break;
  4662. case PIPE_C:
  4663. cpt_enable_fdi_bc_bifurcation(dev);
  4664. break;
  4665. default:
  4666. BUG();
  4667. }
  4668. }
  4669. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  4670. {
  4671. /*
  4672. * Account for spread spectrum to avoid
  4673. * oversubscribing the link. Max center spread
  4674. * is 2.5%; use 5% for safety's sake.
  4675. */
  4676. u32 bps = target_clock * bpp * 21 / 20;
  4677. return bps / (link_bw * 8) + 1;
  4678. }
  4679. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  4680. {
  4681. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  4682. }
  4683. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  4684. u32 *fp,
  4685. intel_clock_t *reduced_clock, u32 *fp2)
  4686. {
  4687. struct drm_crtc *crtc = &intel_crtc->base;
  4688. struct drm_device *dev = crtc->dev;
  4689. struct drm_i915_private *dev_priv = dev->dev_private;
  4690. struct intel_encoder *intel_encoder;
  4691. uint32_t dpll;
  4692. int factor, num_connectors = 0;
  4693. bool is_lvds = false, is_sdvo = false;
  4694. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4695. switch (intel_encoder->type) {
  4696. case INTEL_OUTPUT_LVDS:
  4697. is_lvds = true;
  4698. break;
  4699. case INTEL_OUTPUT_SDVO:
  4700. case INTEL_OUTPUT_HDMI:
  4701. is_sdvo = true;
  4702. break;
  4703. }
  4704. num_connectors++;
  4705. }
  4706. /* Enable autotuning of the PLL clock (if permissible) */
  4707. factor = 21;
  4708. if (is_lvds) {
  4709. if ((intel_panel_use_ssc(dev_priv) &&
  4710. dev_priv->vbt.lvds_ssc_freq == 100) ||
  4711. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  4712. factor = 25;
  4713. } else if (intel_crtc->config.sdvo_tv_clock)
  4714. factor = 20;
  4715. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  4716. *fp |= FP_CB_TUNE;
  4717. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  4718. *fp2 |= FP_CB_TUNE;
  4719. dpll = 0;
  4720. if (is_lvds)
  4721. dpll |= DPLLB_MODE_LVDS;
  4722. else
  4723. dpll |= DPLLB_MODE_DAC_SERIAL;
  4724. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  4725. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  4726. if (is_sdvo)
  4727. dpll |= DPLL_DVO_HIGH_SPEED;
  4728. if (intel_crtc->config.has_dp_encoder)
  4729. dpll |= DPLL_DVO_HIGH_SPEED;
  4730. /* compute bitmask from p1 value */
  4731. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4732. /* also FPA1 */
  4733. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4734. switch (intel_crtc->config.dpll.p2) {
  4735. case 5:
  4736. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4737. break;
  4738. case 7:
  4739. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4740. break;
  4741. case 10:
  4742. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4743. break;
  4744. case 14:
  4745. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4746. break;
  4747. }
  4748. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4749. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4750. else
  4751. dpll |= PLL_REF_INPUT_DREFCLK;
  4752. return dpll;
  4753. }
  4754. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  4755. int x, int y,
  4756. struct drm_framebuffer *fb)
  4757. {
  4758. struct drm_device *dev = crtc->dev;
  4759. struct drm_i915_private *dev_priv = dev->dev_private;
  4760. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4761. int pipe = intel_crtc->pipe;
  4762. int plane = intel_crtc->plane;
  4763. int num_connectors = 0;
  4764. intel_clock_t clock, reduced_clock;
  4765. u32 dpll = 0, fp = 0, fp2 = 0;
  4766. bool ok, has_reduced_clock = false;
  4767. bool is_lvds = false;
  4768. struct intel_encoder *encoder;
  4769. int ret;
  4770. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4771. switch (encoder->type) {
  4772. case INTEL_OUTPUT_LVDS:
  4773. is_lvds = true;
  4774. break;
  4775. }
  4776. num_connectors++;
  4777. }
  4778. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  4779. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  4780. ok = ironlake_compute_clocks(crtc, &clock,
  4781. &has_reduced_clock, &reduced_clock);
  4782. if (!ok && !intel_crtc->config.clock_set) {
  4783. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4784. return -EINVAL;
  4785. }
  4786. /* Compat-code for transition, will disappear. */
  4787. if (!intel_crtc->config.clock_set) {
  4788. intel_crtc->config.dpll.n = clock.n;
  4789. intel_crtc->config.dpll.m1 = clock.m1;
  4790. intel_crtc->config.dpll.m2 = clock.m2;
  4791. intel_crtc->config.dpll.p1 = clock.p1;
  4792. intel_crtc->config.dpll.p2 = clock.p2;
  4793. }
  4794. /* Ensure that the cursor is valid for the new mode before changing... */
  4795. intel_crtc_update_cursor(crtc, true);
  4796. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  4797. if (intel_crtc->config.has_pch_encoder) {
  4798. struct intel_pch_pll *pll;
  4799. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  4800. if (has_reduced_clock)
  4801. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  4802. dpll = ironlake_compute_dpll(intel_crtc,
  4803. &fp, &reduced_clock,
  4804. has_reduced_clock ? &fp2 : NULL);
  4805. pll = intel_get_pch_pll(intel_crtc, dpll, fp);
  4806. if (pll == NULL) {
  4807. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  4808. pipe_name(pipe));
  4809. return -EINVAL;
  4810. }
  4811. } else
  4812. intel_put_pch_pll(intel_crtc);
  4813. if (intel_crtc->config.has_dp_encoder)
  4814. intel_dp_set_m_n(intel_crtc);
  4815. for_each_encoder_on_crtc(dev, crtc, encoder)
  4816. if (encoder->pre_pll_enable)
  4817. encoder->pre_pll_enable(encoder);
  4818. if (intel_crtc->pch_pll) {
  4819. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  4820. /* Wait for the clocks to stabilize. */
  4821. POSTING_READ(intel_crtc->pch_pll->pll_reg);
  4822. udelay(150);
  4823. /* The pixel multiplier can only be updated once the
  4824. * DPLL is enabled and the clocks are stable.
  4825. *
  4826. * So write it again.
  4827. */
  4828. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  4829. }
  4830. intel_crtc->lowfreq_avail = false;
  4831. if (intel_crtc->pch_pll) {
  4832. if (is_lvds && has_reduced_clock && i915_powersave) {
  4833. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
  4834. intel_crtc->lowfreq_avail = true;
  4835. } else {
  4836. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
  4837. }
  4838. }
  4839. intel_set_pipe_timings(intel_crtc);
  4840. if (intel_crtc->config.has_pch_encoder) {
  4841. intel_cpu_transcoder_set_m_n(intel_crtc,
  4842. &intel_crtc->config.fdi_m_n);
  4843. }
  4844. if (IS_IVYBRIDGE(dev))
  4845. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  4846. ironlake_set_pipeconf(crtc);
  4847. /* Set up the display plane register */
  4848. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  4849. POSTING_READ(DSPCNTR(plane));
  4850. ret = intel_pipe_set_base(crtc, x, y, fb);
  4851. intel_update_watermarks(dev);
  4852. return ret;
  4853. }
  4854. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  4855. struct intel_crtc_config *pipe_config)
  4856. {
  4857. struct drm_device *dev = crtc->base.dev;
  4858. struct drm_i915_private *dev_priv = dev->dev_private;
  4859. enum transcoder transcoder = pipe_config->cpu_transcoder;
  4860. pipe_config->fdi_m_n.link_m = I915_READ(PIPE_LINK_M1(transcoder));
  4861. pipe_config->fdi_m_n.link_n = I915_READ(PIPE_LINK_N1(transcoder));
  4862. pipe_config->fdi_m_n.gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  4863. & ~TU_SIZE_MASK;
  4864. pipe_config->fdi_m_n.gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  4865. pipe_config->fdi_m_n.tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  4866. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  4867. }
  4868. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  4869. struct intel_crtc_config *pipe_config)
  4870. {
  4871. struct drm_device *dev = crtc->base.dev;
  4872. struct drm_i915_private *dev_priv = dev->dev_private;
  4873. uint32_t tmp;
  4874. tmp = I915_READ(PF_CTL(crtc->pipe));
  4875. if (tmp & PF_ENABLE) {
  4876. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  4877. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  4878. /* We currently do not free assignements of panel fitters on
  4879. * ivb/hsw (since we don't use the higher upscaling modes which
  4880. * differentiates them) so just WARN about this case for now. */
  4881. if (IS_GEN7(dev)) {
  4882. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  4883. PF_PIPE_SEL_IVB(crtc->pipe));
  4884. }
  4885. }
  4886. }
  4887. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  4888. struct intel_crtc_config *pipe_config)
  4889. {
  4890. struct drm_device *dev = crtc->base.dev;
  4891. struct drm_i915_private *dev_priv = dev->dev_private;
  4892. uint32_t tmp;
  4893. pipe_config->cpu_transcoder = crtc->pipe;
  4894. tmp = I915_READ(PIPECONF(crtc->pipe));
  4895. if (!(tmp & PIPECONF_ENABLE))
  4896. return false;
  4897. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  4898. pipe_config->has_pch_encoder = true;
  4899. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  4900. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  4901. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  4902. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  4903. }
  4904. intel_get_pipe_timings(crtc, pipe_config);
  4905. ironlake_get_pfit_config(crtc, pipe_config);
  4906. return true;
  4907. }
  4908. static void haswell_modeset_global_resources(struct drm_device *dev)
  4909. {
  4910. bool enable = false;
  4911. struct intel_crtc *crtc;
  4912. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  4913. if (!crtc->base.enabled)
  4914. continue;
  4915. if (crtc->pipe != PIPE_A || crtc->config.pch_pfit.size ||
  4916. crtc->config.cpu_transcoder != TRANSCODER_EDP)
  4917. enable = true;
  4918. }
  4919. intel_set_power_well(dev, enable);
  4920. }
  4921. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  4922. int x, int y,
  4923. struct drm_framebuffer *fb)
  4924. {
  4925. struct drm_device *dev = crtc->dev;
  4926. struct drm_i915_private *dev_priv = dev->dev_private;
  4927. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4928. int plane = intel_crtc->plane;
  4929. int ret;
  4930. if (!intel_ddi_pll_mode_set(crtc))
  4931. return -EINVAL;
  4932. /* Ensure that the cursor is valid for the new mode before changing... */
  4933. intel_crtc_update_cursor(crtc, true);
  4934. if (intel_crtc->config.has_dp_encoder)
  4935. intel_dp_set_m_n(intel_crtc);
  4936. intel_crtc->lowfreq_avail = false;
  4937. intel_set_pipe_timings(intel_crtc);
  4938. if (intel_crtc->config.has_pch_encoder) {
  4939. intel_cpu_transcoder_set_m_n(intel_crtc,
  4940. &intel_crtc->config.fdi_m_n);
  4941. }
  4942. haswell_set_pipeconf(crtc);
  4943. intel_set_pipe_csc(crtc);
  4944. /* Set up the display plane register */
  4945. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  4946. POSTING_READ(DSPCNTR(plane));
  4947. ret = intel_pipe_set_base(crtc, x, y, fb);
  4948. intel_update_watermarks(dev);
  4949. return ret;
  4950. }
  4951. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  4952. struct intel_crtc_config *pipe_config)
  4953. {
  4954. struct drm_device *dev = crtc->base.dev;
  4955. struct drm_i915_private *dev_priv = dev->dev_private;
  4956. enum intel_display_power_domain pfit_domain;
  4957. uint32_t tmp;
  4958. pipe_config->cpu_transcoder = crtc->pipe;
  4959. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  4960. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  4961. enum pipe trans_edp_pipe;
  4962. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  4963. default:
  4964. WARN(1, "unknown pipe linked to edp transcoder\n");
  4965. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  4966. case TRANS_DDI_EDP_INPUT_A_ON:
  4967. trans_edp_pipe = PIPE_A;
  4968. break;
  4969. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  4970. trans_edp_pipe = PIPE_B;
  4971. break;
  4972. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  4973. trans_edp_pipe = PIPE_C;
  4974. break;
  4975. }
  4976. if (trans_edp_pipe == crtc->pipe)
  4977. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  4978. }
  4979. if (!intel_display_power_enabled(dev,
  4980. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  4981. return false;
  4982. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  4983. if (!(tmp & PIPECONF_ENABLE))
  4984. return false;
  4985. /*
  4986. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  4987. * DDI E. So just check whether this pipe is wired to DDI E and whether
  4988. * the PCH transcoder is on.
  4989. */
  4990. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  4991. if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
  4992. I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  4993. pipe_config->has_pch_encoder = true;
  4994. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  4995. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  4996. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  4997. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  4998. }
  4999. intel_get_pipe_timings(crtc, pipe_config);
  5000. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  5001. if (intel_display_power_enabled(dev, pfit_domain))
  5002. ironlake_get_pfit_config(crtc, pipe_config);
  5003. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  5004. (I915_READ(IPS_CTL) & IPS_ENABLE);
  5005. return true;
  5006. }
  5007. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  5008. int x, int y,
  5009. struct drm_framebuffer *fb)
  5010. {
  5011. struct drm_device *dev = crtc->dev;
  5012. struct drm_i915_private *dev_priv = dev->dev_private;
  5013. struct drm_encoder_helper_funcs *encoder_funcs;
  5014. struct intel_encoder *encoder;
  5015. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5016. struct drm_display_mode *adjusted_mode =
  5017. &intel_crtc->config.adjusted_mode;
  5018. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  5019. int pipe = intel_crtc->pipe;
  5020. int ret;
  5021. drm_vblank_pre_modeset(dev, pipe);
  5022. ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
  5023. drm_vblank_post_modeset(dev, pipe);
  5024. if (ret != 0)
  5025. return ret;
  5026. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5027. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  5028. encoder->base.base.id,
  5029. drm_get_encoder_name(&encoder->base),
  5030. mode->base.id, mode->name);
  5031. if (encoder->mode_set) {
  5032. encoder->mode_set(encoder);
  5033. } else {
  5034. encoder_funcs = encoder->base.helper_private;
  5035. encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
  5036. }
  5037. }
  5038. return 0;
  5039. }
  5040. static bool intel_eld_uptodate(struct drm_connector *connector,
  5041. int reg_eldv, uint32_t bits_eldv,
  5042. int reg_elda, uint32_t bits_elda,
  5043. int reg_edid)
  5044. {
  5045. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5046. uint8_t *eld = connector->eld;
  5047. uint32_t i;
  5048. i = I915_READ(reg_eldv);
  5049. i &= bits_eldv;
  5050. if (!eld[0])
  5051. return !i;
  5052. if (!i)
  5053. return false;
  5054. i = I915_READ(reg_elda);
  5055. i &= ~bits_elda;
  5056. I915_WRITE(reg_elda, i);
  5057. for (i = 0; i < eld[2]; i++)
  5058. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  5059. return false;
  5060. return true;
  5061. }
  5062. static void g4x_write_eld(struct drm_connector *connector,
  5063. struct drm_crtc *crtc)
  5064. {
  5065. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5066. uint8_t *eld = connector->eld;
  5067. uint32_t eldv;
  5068. uint32_t len;
  5069. uint32_t i;
  5070. i = I915_READ(G4X_AUD_VID_DID);
  5071. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  5072. eldv = G4X_ELDV_DEVCL_DEVBLC;
  5073. else
  5074. eldv = G4X_ELDV_DEVCTG;
  5075. if (intel_eld_uptodate(connector,
  5076. G4X_AUD_CNTL_ST, eldv,
  5077. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  5078. G4X_HDMIW_HDMIEDID))
  5079. return;
  5080. i = I915_READ(G4X_AUD_CNTL_ST);
  5081. i &= ~(eldv | G4X_ELD_ADDR);
  5082. len = (i >> 9) & 0x1f; /* ELD buffer size */
  5083. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5084. if (!eld[0])
  5085. return;
  5086. len = min_t(uint8_t, eld[2], len);
  5087. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5088. for (i = 0; i < len; i++)
  5089. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  5090. i = I915_READ(G4X_AUD_CNTL_ST);
  5091. i |= eldv;
  5092. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5093. }
  5094. static void haswell_write_eld(struct drm_connector *connector,
  5095. struct drm_crtc *crtc)
  5096. {
  5097. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5098. uint8_t *eld = connector->eld;
  5099. struct drm_device *dev = crtc->dev;
  5100. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5101. uint32_t eldv;
  5102. uint32_t i;
  5103. int len;
  5104. int pipe = to_intel_crtc(crtc)->pipe;
  5105. int tmp;
  5106. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  5107. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  5108. int aud_config = HSW_AUD_CFG(pipe);
  5109. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  5110. DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
  5111. /* Audio output enable */
  5112. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  5113. tmp = I915_READ(aud_cntrl_st2);
  5114. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  5115. I915_WRITE(aud_cntrl_st2, tmp);
  5116. /* Wait for 1 vertical blank */
  5117. intel_wait_for_vblank(dev, pipe);
  5118. /* Set ELD valid state */
  5119. tmp = I915_READ(aud_cntrl_st2);
  5120. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
  5121. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  5122. I915_WRITE(aud_cntrl_st2, tmp);
  5123. tmp = I915_READ(aud_cntrl_st2);
  5124. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
  5125. /* Enable HDMI mode */
  5126. tmp = I915_READ(aud_config);
  5127. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
  5128. /* clear N_programing_enable and N_value_index */
  5129. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  5130. I915_WRITE(aud_config, tmp);
  5131. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  5132. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  5133. intel_crtc->eld_vld = true;
  5134. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5135. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5136. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5137. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5138. } else
  5139. I915_WRITE(aud_config, 0);
  5140. if (intel_eld_uptodate(connector,
  5141. aud_cntrl_st2, eldv,
  5142. aud_cntl_st, IBX_ELD_ADDRESS,
  5143. hdmiw_hdmiedid))
  5144. return;
  5145. i = I915_READ(aud_cntrl_st2);
  5146. i &= ~eldv;
  5147. I915_WRITE(aud_cntrl_st2, i);
  5148. if (!eld[0])
  5149. return;
  5150. i = I915_READ(aud_cntl_st);
  5151. i &= ~IBX_ELD_ADDRESS;
  5152. I915_WRITE(aud_cntl_st, i);
  5153. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  5154. DRM_DEBUG_DRIVER("port num:%d\n", i);
  5155. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5156. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5157. for (i = 0; i < len; i++)
  5158. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5159. i = I915_READ(aud_cntrl_st2);
  5160. i |= eldv;
  5161. I915_WRITE(aud_cntrl_st2, i);
  5162. }
  5163. static void ironlake_write_eld(struct drm_connector *connector,
  5164. struct drm_crtc *crtc)
  5165. {
  5166. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5167. uint8_t *eld = connector->eld;
  5168. uint32_t eldv;
  5169. uint32_t i;
  5170. int len;
  5171. int hdmiw_hdmiedid;
  5172. int aud_config;
  5173. int aud_cntl_st;
  5174. int aud_cntrl_st2;
  5175. int pipe = to_intel_crtc(crtc)->pipe;
  5176. if (HAS_PCH_IBX(connector->dev)) {
  5177. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  5178. aud_config = IBX_AUD_CFG(pipe);
  5179. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  5180. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  5181. } else {
  5182. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  5183. aud_config = CPT_AUD_CFG(pipe);
  5184. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  5185. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  5186. }
  5187. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  5188. i = I915_READ(aud_cntl_st);
  5189. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  5190. if (!i) {
  5191. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  5192. /* operate blindly on all ports */
  5193. eldv = IBX_ELD_VALIDB;
  5194. eldv |= IBX_ELD_VALIDB << 4;
  5195. eldv |= IBX_ELD_VALIDB << 8;
  5196. } else {
  5197. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  5198. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  5199. }
  5200. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5201. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5202. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5203. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5204. } else
  5205. I915_WRITE(aud_config, 0);
  5206. if (intel_eld_uptodate(connector,
  5207. aud_cntrl_st2, eldv,
  5208. aud_cntl_st, IBX_ELD_ADDRESS,
  5209. hdmiw_hdmiedid))
  5210. return;
  5211. i = I915_READ(aud_cntrl_st2);
  5212. i &= ~eldv;
  5213. I915_WRITE(aud_cntrl_st2, i);
  5214. if (!eld[0])
  5215. return;
  5216. i = I915_READ(aud_cntl_st);
  5217. i &= ~IBX_ELD_ADDRESS;
  5218. I915_WRITE(aud_cntl_st, i);
  5219. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5220. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5221. for (i = 0; i < len; i++)
  5222. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5223. i = I915_READ(aud_cntrl_st2);
  5224. i |= eldv;
  5225. I915_WRITE(aud_cntrl_st2, i);
  5226. }
  5227. void intel_write_eld(struct drm_encoder *encoder,
  5228. struct drm_display_mode *mode)
  5229. {
  5230. struct drm_crtc *crtc = encoder->crtc;
  5231. struct drm_connector *connector;
  5232. struct drm_device *dev = encoder->dev;
  5233. struct drm_i915_private *dev_priv = dev->dev_private;
  5234. connector = drm_select_eld(encoder, mode);
  5235. if (!connector)
  5236. return;
  5237. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5238. connector->base.id,
  5239. drm_get_connector_name(connector),
  5240. connector->encoder->base.id,
  5241. drm_get_encoder_name(connector->encoder));
  5242. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  5243. if (dev_priv->display.write_eld)
  5244. dev_priv->display.write_eld(connector, crtc);
  5245. }
  5246. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  5247. void intel_crtc_load_lut(struct drm_crtc *crtc)
  5248. {
  5249. struct drm_device *dev = crtc->dev;
  5250. struct drm_i915_private *dev_priv = dev->dev_private;
  5251. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5252. enum pipe pipe = intel_crtc->pipe;
  5253. int palreg = PALETTE(pipe);
  5254. int i;
  5255. bool reenable_ips = false;
  5256. /* The clocks have to be on to load the palette. */
  5257. if (!crtc->enabled || !intel_crtc->active)
  5258. return;
  5259. /* use legacy palette for Ironlake */
  5260. if (HAS_PCH_SPLIT(dev))
  5261. palreg = LGC_PALETTE(pipe);
  5262. /* Workaround : Do not read or write the pipe palette/gamma data while
  5263. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  5264. */
  5265. if (intel_crtc->config.ips_enabled &&
  5266. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  5267. GAMMA_MODE_MODE_SPLIT)) {
  5268. hsw_disable_ips(intel_crtc);
  5269. reenable_ips = true;
  5270. }
  5271. for (i = 0; i < 256; i++) {
  5272. I915_WRITE(palreg + 4 * i,
  5273. (intel_crtc->lut_r[i] << 16) |
  5274. (intel_crtc->lut_g[i] << 8) |
  5275. intel_crtc->lut_b[i]);
  5276. }
  5277. if (reenable_ips)
  5278. hsw_enable_ips(intel_crtc);
  5279. }
  5280. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  5281. {
  5282. struct drm_device *dev = crtc->dev;
  5283. struct drm_i915_private *dev_priv = dev->dev_private;
  5284. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5285. bool visible = base != 0;
  5286. u32 cntl;
  5287. if (intel_crtc->cursor_visible == visible)
  5288. return;
  5289. cntl = I915_READ(_CURACNTR);
  5290. if (visible) {
  5291. /* On these chipsets we can only modify the base whilst
  5292. * the cursor is disabled.
  5293. */
  5294. I915_WRITE(_CURABASE, base);
  5295. cntl &= ~(CURSOR_FORMAT_MASK);
  5296. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  5297. cntl |= CURSOR_ENABLE |
  5298. CURSOR_GAMMA_ENABLE |
  5299. CURSOR_FORMAT_ARGB;
  5300. } else
  5301. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  5302. I915_WRITE(_CURACNTR, cntl);
  5303. intel_crtc->cursor_visible = visible;
  5304. }
  5305. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  5306. {
  5307. struct drm_device *dev = crtc->dev;
  5308. struct drm_i915_private *dev_priv = dev->dev_private;
  5309. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5310. int pipe = intel_crtc->pipe;
  5311. bool visible = base != 0;
  5312. if (intel_crtc->cursor_visible != visible) {
  5313. uint32_t cntl = I915_READ(CURCNTR(pipe));
  5314. if (base) {
  5315. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  5316. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5317. cntl |= pipe << 28; /* Connect to correct pipe */
  5318. } else {
  5319. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5320. cntl |= CURSOR_MODE_DISABLE;
  5321. }
  5322. I915_WRITE(CURCNTR(pipe), cntl);
  5323. intel_crtc->cursor_visible = visible;
  5324. }
  5325. /* and commit changes on next vblank */
  5326. I915_WRITE(CURBASE(pipe), base);
  5327. }
  5328. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  5329. {
  5330. struct drm_device *dev = crtc->dev;
  5331. struct drm_i915_private *dev_priv = dev->dev_private;
  5332. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5333. int pipe = intel_crtc->pipe;
  5334. bool visible = base != 0;
  5335. if (intel_crtc->cursor_visible != visible) {
  5336. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  5337. if (base) {
  5338. cntl &= ~CURSOR_MODE;
  5339. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5340. } else {
  5341. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5342. cntl |= CURSOR_MODE_DISABLE;
  5343. }
  5344. if (IS_HASWELL(dev))
  5345. cntl |= CURSOR_PIPE_CSC_ENABLE;
  5346. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  5347. intel_crtc->cursor_visible = visible;
  5348. }
  5349. /* and commit changes on next vblank */
  5350. I915_WRITE(CURBASE_IVB(pipe), base);
  5351. }
  5352. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  5353. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  5354. bool on)
  5355. {
  5356. struct drm_device *dev = crtc->dev;
  5357. struct drm_i915_private *dev_priv = dev->dev_private;
  5358. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5359. int pipe = intel_crtc->pipe;
  5360. int x = intel_crtc->cursor_x;
  5361. int y = intel_crtc->cursor_y;
  5362. u32 base, pos;
  5363. bool visible;
  5364. pos = 0;
  5365. if (on && crtc->enabled && crtc->fb) {
  5366. base = intel_crtc->cursor_addr;
  5367. if (x > (int) crtc->fb->width)
  5368. base = 0;
  5369. if (y > (int) crtc->fb->height)
  5370. base = 0;
  5371. } else
  5372. base = 0;
  5373. if (x < 0) {
  5374. if (x + intel_crtc->cursor_width < 0)
  5375. base = 0;
  5376. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  5377. x = -x;
  5378. }
  5379. pos |= x << CURSOR_X_SHIFT;
  5380. if (y < 0) {
  5381. if (y + intel_crtc->cursor_height < 0)
  5382. base = 0;
  5383. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  5384. y = -y;
  5385. }
  5386. pos |= y << CURSOR_Y_SHIFT;
  5387. visible = base != 0;
  5388. if (!visible && !intel_crtc->cursor_visible)
  5389. return;
  5390. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  5391. I915_WRITE(CURPOS_IVB(pipe), pos);
  5392. ivb_update_cursor(crtc, base);
  5393. } else {
  5394. I915_WRITE(CURPOS(pipe), pos);
  5395. if (IS_845G(dev) || IS_I865G(dev))
  5396. i845_update_cursor(crtc, base);
  5397. else
  5398. i9xx_update_cursor(crtc, base);
  5399. }
  5400. }
  5401. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  5402. struct drm_file *file,
  5403. uint32_t handle,
  5404. uint32_t width, uint32_t height)
  5405. {
  5406. struct drm_device *dev = crtc->dev;
  5407. struct drm_i915_private *dev_priv = dev->dev_private;
  5408. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5409. struct drm_i915_gem_object *obj;
  5410. uint32_t addr;
  5411. int ret;
  5412. /* if we want to turn off the cursor ignore width and height */
  5413. if (!handle) {
  5414. DRM_DEBUG_KMS("cursor off\n");
  5415. addr = 0;
  5416. obj = NULL;
  5417. mutex_lock(&dev->struct_mutex);
  5418. goto finish;
  5419. }
  5420. /* Currently we only support 64x64 cursors */
  5421. if (width != 64 || height != 64) {
  5422. DRM_ERROR("we currently only support 64x64 cursors\n");
  5423. return -EINVAL;
  5424. }
  5425. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  5426. if (&obj->base == NULL)
  5427. return -ENOENT;
  5428. if (obj->base.size < width * height * 4) {
  5429. DRM_ERROR("buffer is to small\n");
  5430. ret = -ENOMEM;
  5431. goto fail;
  5432. }
  5433. /* we only need to pin inside GTT if cursor is non-phy */
  5434. mutex_lock(&dev->struct_mutex);
  5435. if (!dev_priv->info->cursor_needs_physical) {
  5436. unsigned alignment;
  5437. if (obj->tiling_mode) {
  5438. DRM_ERROR("cursor cannot be tiled\n");
  5439. ret = -EINVAL;
  5440. goto fail_locked;
  5441. }
  5442. /* Note that the w/a also requires 2 PTE of padding following
  5443. * the bo. We currently fill all unused PTE with the shadow
  5444. * page and so we should always have valid PTE following the
  5445. * cursor preventing the VT-d warning.
  5446. */
  5447. alignment = 0;
  5448. if (need_vtd_wa(dev))
  5449. alignment = 64*1024;
  5450. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  5451. if (ret) {
  5452. DRM_ERROR("failed to move cursor bo into the GTT\n");
  5453. goto fail_locked;
  5454. }
  5455. ret = i915_gem_object_put_fence(obj);
  5456. if (ret) {
  5457. DRM_ERROR("failed to release fence for cursor");
  5458. goto fail_unpin;
  5459. }
  5460. addr = obj->gtt_offset;
  5461. } else {
  5462. int align = IS_I830(dev) ? 16 * 1024 : 256;
  5463. ret = i915_gem_attach_phys_object(dev, obj,
  5464. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  5465. align);
  5466. if (ret) {
  5467. DRM_ERROR("failed to attach phys object\n");
  5468. goto fail_locked;
  5469. }
  5470. addr = obj->phys_obj->handle->busaddr;
  5471. }
  5472. if (IS_GEN2(dev))
  5473. I915_WRITE(CURSIZE, (height << 12) | width);
  5474. finish:
  5475. if (intel_crtc->cursor_bo) {
  5476. if (dev_priv->info->cursor_needs_physical) {
  5477. if (intel_crtc->cursor_bo != obj)
  5478. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  5479. } else
  5480. i915_gem_object_unpin(intel_crtc->cursor_bo);
  5481. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  5482. }
  5483. mutex_unlock(&dev->struct_mutex);
  5484. intel_crtc->cursor_addr = addr;
  5485. intel_crtc->cursor_bo = obj;
  5486. intel_crtc->cursor_width = width;
  5487. intel_crtc->cursor_height = height;
  5488. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  5489. return 0;
  5490. fail_unpin:
  5491. i915_gem_object_unpin(obj);
  5492. fail_locked:
  5493. mutex_unlock(&dev->struct_mutex);
  5494. fail:
  5495. drm_gem_object_unreference_unlocked(&obj->base);
  5496. return ret;
  5497. }
  5498. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  5499. {
  5500. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5501. intel_crtc->cursor_x = x;
  5502. intel_crtc->cursor_y = y;
  5503. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  5504. return 0;
  5505. }
  5506. /** Sets the color ramps on behalf of RandR */
  5507. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  5508. u16 blue, int regno)
  5509. {
  5510. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5511. intel_crtc->lut_r[regno] = red >> 8;
  5512. intel_crtc->lut_g[regno] = green >> 8;
  5513. intel_crtc->lut_b[regno] = blue >> 8;
  5514. }
  5515. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  5516. u16 *blue, int regno)
  5517. {
  5518. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5519. *red = intel_crtc->lut_r[regno] << 8;
  5520. *green = intel_crtc->lut_g[regno] << 8;
  5521. *blue = intel_crtc->lut_b[regno] << 8;
  5522. }
  5523. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  5524. u16 *blue, uint32_t start, uint32_t size)
  5525. {
  5526. int end = (start + size > 256) ? 256 : start + size, i;
  5527. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5528. for (i = start; i < end; i++) {
  5529. intel_crtc->lut_r[i] = red[i] >> 8;
  5530. intel_crtc->lut_g[i] = green[i] >> 8;
  5531. intel_crtc->lut_b[i] = blue[i] >> 8;
  5532. }
  5533. intel_crtc_load_lut(crtc);
  5534. }
  5535. /* VESA 640x480x72Hz mode to set on the pipe */
  5536. static struct drm_display_mode load_detect_mode = {
  5537. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  5538. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  5539. };
  5540. static struct drm_framebuffer *
  5541. intel_framebuffer_create(struct drm_device *dev,
  5542. struct drm_mode_fb_cmd2 *mode_cmd,
  5543. struct drm_i915_gem_object *obj)
  5544. {
  5545. struct intel_framebuffer *intel_fb;
  5546. int ret;
  5547. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5548. if (!intel_fb) {
  5549. drm_gem_object_unreference_unlocked(&obj->base);
  5550. return ERR_PTR(-ENOMEM);
  5551. }
  5552. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  5553. if (ret) {
  5554. drm_gem_object_unreference_unlocked(&obj->base);
  5555. kfree(intel_fb);
  5556. return ERR_PTR(ret);
  5557. }
  5558. return &intel_fb->base;
  5559. }
  5560. static u32
  5561. intel_framebuffer_pitch_for_width(int width, int bpp)
  5562. {
  5563. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  5564. return ALIGN(pitch, 64);
  5565. }
  5566. static u32
  5567. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  5568. {
  5569. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  5570. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  5571. }
  5572. static struct drm_framebuffer *
  5573. intel_framebuffer_create_for_mode(struct drm_device *dev,
  5574. struct drm_display_mode *mode,
  5575. int depth, int bpp)
  5576. {
  5577. struct drm_i915_gem_object *obj;
  5578. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  5579. obj = i915_gem_alloc_object(dev,
  5580. intel_framebuffer_size_for_mode(mode, bpp));
  5581. if (obj == NULL)
  5582. return ERR_PTR(-ENOMEM);
  5583. mode_cmd.width = mode->hdisplay;
  5584. mode_cmd.height = mode->vdisplay;
  5585. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  5586. bpp);
  5587. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  5588. return intel_framebuffer_create(dev, &mode_cmd, obj);
  5589. }
  5590. static struct drm_framebuffer *
  5591. mode_fits_in_fbdev(struct drm_device *dev,
  5592. struct drm_display_mode *mode)
  5593. {
  5594. struct drm_i915_private *dev_priv = dev->dev_private;
  5595. struct drm_i915_gem_object *obj;
  5596. struct drm_framebuffer *fb;
  5597. if (dev_priv->fbdev == NULL)
  5598. return NULL;
  5599. obj = dev_priv->fbdev->ifb.obj;
  5600. if (obj == NULL)
  5601. return NULL;
  5602. fb = &dev_priv->fbdev->ifb.base;
  5603. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  5604. fb->bits_per_pixel))
  5605. return NULL;
  5606. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  5607. return NULL;
  5608. return fb;
  5609. }
  5610. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  5611. struct drm_display_mode *mode,
  5612. struct intel_load_detect_pipe *old)
  5613. {
  5614. struct intel_crtc *intel_crtc;
  5615. struct intel_encoder *intel_encoder =
  5616. intel_attached_encoder(connector);
  5617. struct drm_crtc *possible_crtc;
  5618. struct drm_encoder *encoder = &intel_encoder->base;
  5619. struct drm_crtc *crtc = NULL;
  5620. struct drm_device *dev = encoder->dev;
  5621. struct drm_framebuffer *fb;
  5622. int i = -1;
  5623. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5624. connector->base.id, drm_get_connector_name(connector),
  5625. encoder->base.id, drm_get_encoder_name(encoder));
  5626. /*
  5627. * Algorithm gets a little messy:
  5628. *
  5629. * - if the connector already has an assigned crtc, use it (but make
  5630. * sure it's on first)
  5631. *
  5632. * - try to find the first unused crtc that can drive this connector,
  5633. * and use that if we find one
  5634. */
  5635. /* See if we already have a CRTC for this connector */
  5636. if (encoder->crtc) {
  5637. crtc = encoder->crtc;
  5638. mutex_lock(&crtc->mutex);
  5639. old->dpms_mode = connector->dpms;
  5640. old->load_detect_temp = false;
  5641. /* Make sure the crtc and connector are running */
  5642. if (connector->dpms != DRM_MODE_DPMS_ON)
  5643. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  5644. return true;
  5645. }
  5646. /* Find an unused one (if possible) */
  5647. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  5648. i++;
  5649. if (!(encoder->possible_crtcs & (1 << i)))
  5650. continue;
  5651. if (!possible_crtc->enabled) {
  5652. crtc = possible_crtc;
  5653. break;
  5654. }
  5655. }
  5656. /*
  5657. * If we didn't find an unused CRTC, don't use any.
  5658. */
  5659. if (!crtc) {
  5660. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  5661. return false;
  5662. }
  5663. mutex_lock(&crtc->mutex);
  5664. intel_encoder->new_crtc = to_intel_crtc(crtc);
  5665. to_intel_connector(connector)->new_encoder = intel_encoder;
  5666. intel_crtc = to_intel_crtc(crtc);
  5667. old->dpms_mode = connector->dpms;
  5668. old->load_detect_temp = true;
  5669. old->release_fb = NULL;
  5670. if (!mode)
  5671. mode = &load_detect_mode;
  5672. /* We need a framebuffer large enough to accommodate all accesses
  5673. * that the plane may generate whilst we perform load detection.
  5674. * We can not rely on the fbcon either being present (we get called
  5675. * during its initialisation to detect all boot displays, or it may
  5676. * not even exist) or that it is large enough to satisfy the
  5677. * requested mode.
  5678. */
  5679. fb = mode_fits_in_fbdev(dev, mode);
  5680. if (fb == NULL) {
  5681. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  5682. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  5683. old->release_fb = fb;
  5684. } else
  5685. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  5686. if (IS_ERR(fb)) {
  5687. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  5688. mutex_unlock(&crtc->mutex);
  5689. return false;
  5690. }
  5691. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  5692. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  5693. if (old->release_fb)
  5694. old->release_fb->funcs->destroy(old->release_fb);
  5695. mutex_unlock(&crtc->mutex);
  5696. return false;
  5697. }
  5698. /* let the connector get through one full cycle before testing */
  5699. intel_wait_for_vblank(dev, intel_crtc->pipe);
  5700. return true;
  5701. }
  5702. void intel_release_load_detect_pipe(struct drm_connector *connector,
  5703. struct intel_load_detect_pipe *old)
  5704. {
  5705. struct intel_encoder *intel_encoder =
  5706. intel_attached_encoder(connector);
  5707. struct drm_encoder *encoder = &intel_encoder->base;
  5708. struct drm_crtc *crtc = encoder->crtc;
  5709. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5710. connector->base.id, drm_get_connector_name(connector),
  5711. encoder->base.id, drm_get_encoder_name(encoder));
  5712. if (old->load_detect_temp) {
  5713. to_intel_connector(connector)->new_encoder = NULL;
  5714. intel_encoder->new_crtc = NULL;
  5715. intel_set_mode(crtc, NULL, 0, 0, NULL);
  5716. if (old->release_fb) {
  5717. drm_framebuffer_unregister_private(old->release_fb);
  5718. drm_framebuffer_unreference(old->release_fb);
  5719. }
  5720. mutex_unlock(&crtc->mutex);
  5721. return;
  5722. }
  5723. /* Switch crtc and encoder back off if necessary */
  5724. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  5725. connector->funcs->dpms(connector, old->dpms_mode);
  5726. mutex_unlock(&crtc->mutex);
  5727. }
  5728. /* Returns the clock of the currently programmed mode of the given pipe. */
  5729. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  5730. {
  5731. struct drm_i915_private *dev_priv = dev->dev_private;
  5732. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5733. int pipe = intel_crtc->pipe;
  5734. u32 dpll = I915_READ(DPLL(pipe));
  5735. u32 fp;
  5736. intel_clock_t clock;
  5737. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  5738. fp = I915_READ(FP0(pipe));
  5739. else
  5740. fp = I915_READ(FP1(pipe));
  5741. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  5742. if (IS_PINEVIEW(dev)) {
  5743. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  5744. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5745. } else {
  5746. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  5747. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5748. }
  5749. if (!IS_GEN2(dev)) {
  5750. if (IS_PINEVIEW(dev))
  5751. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  5752. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  5753. else
  5754. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  5755. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5756. switch (dpll & DPLL_MODE_MASK) {
  5757. case DPLLB_MODE_DAC_SERIAL:
  5758. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  5759. 5 : 10;
  5760. break;
  5761. case DPLLB_MODE_LVDS:
  5762. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  5763. 7 : 14;
  5764. break;
  5765. default:
  5766. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  5767. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  5768. return 0;
  5769. }
  5770. if (IS_PINEVIEW(dev))
  5771. pineview_clock(96000, &clock);
  5772. else
  5773. i9xx_clock(96000, &clock);
  5774. } else {
  5775. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  5776. if (is_lvds) {
  5777. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  5778. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5779. clock.p2 = 14;
  5780. if ((dpll & PLL_REF_INPUT_MASK) ==
  5781. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  5782. /* XXX: might not be 66MHz */
  5783. i9xx_clock(66000, &clock);
  5784. } else
  5785. i9xx_clock(48000, &clock);
  5786. } else {
  5787. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  5788. clock.p1 = 2;
  5789. else {
  5790. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  5791. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  5792. }
  5793. if (dpll & PLL_P2_DIVIDE_BY_4)
  5794. clock.p2 = 4;
  5795. else
  5796. clock.p2 = 2;
  5797. i9xx_clock(48000, &clock);
  5798. }
  5799. }
  5800. /* XXX: It would be nice to validate the clocks, but we can't reuse
  5801. * i830PllIsValid() because it relies on the xf86_config connector
  5802. * configuration being accurate, which it isn't necessarily.
  5803. */
  5804. return clock.dot;
  5805. }
  5806. /** Returns the currently programmed mode of the given pipe. */
  5807. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  5808. struct drm_crtc *crtc)
  5809. {
  5810. struct drm_i915_private *dev_priv = dev->dev_private;
  5811. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5812. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5813. struct drm_display_mode *mode;
  5814. int htot = I915_READ(HTOTAL(cpu_transcoder));
  5815. int hsync = I915_READ(HSYNC(cpu_transcoder));
  5816. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  5817. int vsync = I915_READ(VSYNC(cpu_transcoder));
  5818. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  5819. if (!mode)
  5820. return NULL;
  5821. mode->clock = intel_crtc_clock_get(dev, crtc);
  5822. mode->hdisplay = (htot & 0xffff) + 1;
  5823. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  5824. mode->hsync_start = (hsync & 0xffff) + 1;
  5825. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  5826. mode->vdisplay = (vtot & 0xffff) + 1;
  5827. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  5828. mode->vsync_start = (vsync & 0xffff) + 1;
  5829. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  5830. drm_mode_set_name(mode);
  5831. return mode;
  5832. }
  5833. static void intel_increase_pllclock(struct drm_crtc *crtc)
  5834. {
  5835. struct drm_device *dev = crtc->dev;
  5836. drm_i915_private_t *dev_priv = dev->dev_private;
  5837. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5838. int pipe = intel_crtc->pipe;
  5839. int dpll_reg = DPLL(pipe);
  5840. int dpll;
  5841. if (HAS_PCH_SPLIT(dev))
  5842. return;
  5843. if (!dev_priv->lvds_downclock_avail)
  5844. return;
  5845. dpll = I915_READ(dpll_reg);
  5846. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  5847. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  5848. assert_panel_unlocked(dev_priv, pipe);
  5849. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  5850. I915_WRITE(dpll_reg, dpll);
  5851. intel_wait_for_vblank(dev, pipe);
  5852. dpll = I915_READ(dpll_reg);
  5853. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  5854. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  5855. }
  5856. }
  5857. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  5858. {
  5859. struct drm_device *dev = crtc->dev;
  5860. drm_i915_private_t *dev_priv = dev->dev_private;
  5861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5862. if (HAS_PCH_SPLIT(dev))
  5863. return;
  5864. if (!dev_priv->lvds_downclock_avail)
  5865. return;
  5866. /*
  5867. * Since this is called by a timer, we should never get here in
  5868. * the manual case.
  5869. */
  5870. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  5871. int pipe = intel_crtc->pipe;
  5872. int dpll_reg = DPLL(pipe);
  5873. int dpll;
  5874. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  5875. assert_panel_unlocked(dev_priv, pipe);
  5876. dpll = I915_READ(dpll_reg);
  5877. dpll |= DISPLAY_RATE_SELECT_FPA1;
  5878. I915_WRITE(dpll_reg, dpll);
  5879. intel_wait_for_vblank(dev, pipe);
  5880. dpll = I915_READ(dpll_reg);
  5881. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  5882. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  5883. }
  5884. }
  5885. void intel_mark_busy(struct drm_device *dev)
  5886. {
  5887. i915_update_gfx_val(dev->dev_private);
  5888. }
  5889. void intel_mark_idle(struct drm_device *dev)
  5890. {
  5891. struct drm_crtc *crtc;
  5892. if (!i915_powersave)
  5893. return;
  5894. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5895. if (!crtc->fb)
  5896. continue;
  5897. intel_decrease_pllclock(crtc);
  5898. }
  5899. }
  5900. void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
  5901. {
  5902. struct drm_device *dev = obj->base.dev;
  5903. struct drm_crtc *crtc;
  5904. if (!i915_powersave)
  5905. return;
  5906. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5907. if (!crtc->fb)
  5908. continue;
  5909. if (to_intel_framebuffer(crtc->fb)->obj == obj)
  5910. intel_increase_pllclock(crtc);
  5911. }
  5912. }
  5913. static void intel_crtc_destroy(struct drm_crtc *crtc)
  5914. {
  5915. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5916. struct drm_device *dev = crtc->dev;
  5917. struct intel_unpin_work *work;
  5918. unsigned long flags;
  5919. spin_lock_irqsave(&dev->event_lock, flags);
  5920. work = intel_crtc->unpin_work;
  5921. intel_crtc->unpin_work = NULL;
  5922. spin_unlock_irqrestore(&dev->event_lock, flags);
  5923. if (work) {
  5924. cancel_work_sync(&work->work);
  5925. kfree(work);
  5926. }
  5927. intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
  5928. drm_crtc_cleanup(crtc);
  5929. kfree(intel_crtc);
  5930. }
  5931. static void intel_unpin_work_fn(struct work_struct *__work)
  5932. {
  5933. struct intel_unpin_work *work =
  5934. container_of(__work, struct intel_unpin_work, work);
  5935. struct drm_device *dev = work->crtc->dev;
  5936. mutex_lock(&dev->struct_mutex);
  5937. intel_unpin_fb_obj(work->old_fb_obj);
  5938. drm_gem_object_unreference(&work->pending_flip_obj->base);
  5939. drm_gem_object_unreference(&work->old_fb_obj->base);
  5940. intel_update_fbc(dev);
  5941. mutex_unlock(&dev->struct_mutex);
  5942. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  5943. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  5944. kfree(work);
  5945. }
  5946. static void do_intel_finish_page_flip(struct drm_device *dev,
  5947. struct drm_crtc *crtc)
  5948. {
  5949. drm_i915_private_t *dev_priv = dev->dev_private;
  5950. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5951. struct intel_unpin_work *work;
  5952. unsigned long flags;
  5953. /* Ignore early vblank irqs */
  5954. if (intel_crtc == NULL)
  5955. return;
  5956. spin_lock_irqsave(&dev->event_lock, flags);
  5957. work = intel_crtc->unpin_work;
  5958. /* Ensure we don't miss a work->pending update ... */
  5959. smp_rmb();
  5960. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  5961. spin_unlock_irqrestore(&dev->event_lock, flags);
  5962. return;
  5963. }
  5964. /* and that the unpin work is consistent wrt ->pending. */
  5965. smp_rmb();
  5966. intel_crtc->unpin_work = NULL;
  5967. if (work->event)
  5968. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  5969. drm_vblank_put(dev, intel_crtc->pipe);
  5970. spin_unlock_irqrestore(&dev->event_lock, flags);
  5971. wake_up_all(&dev_priv->pending_flip_queue);
  5972. queue_work(dev_priv->wq, &work->work);
  5973. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  5974. }
  5975. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  5976. {
  5977. drm_i915_private_t *dev_priv = dev->dev_private;
  5978. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5979. do_intel_finish_page_flip(dev, crtc);
  5980. }
  5981. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  5982. {
  5983. drm_i915_private_t *dev_priv = dev->dev_private;
  5984. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  5985. do_intel_finish_page_flip(dev, crtc);
  5986. }
  5987. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  5988. {
  5989. drm_i915_private_t *dev_priv = dev->dev_private;
  5990. struct intel_crtc *intel_crtc =
  5991. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  5992. unsigned long flags;
  5993. /* NB: An MMIO update of the plane base pointer will also
  5994. * generate a page-flip completion irq, i.e. every modeset
  5995. * is also accompanied by a spurious intel_prepare_page_flip().
  5996. */
  5997. spin_lock_irqsave(&dev->event_lock, flags);
  5998. if (intel_crtc->unpin_work)
  5999. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  6000. spin_unlock_irqrestore(&dev->event_lock, flags);
  6001. }
  6002. inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  6003. {
  6004. /* Ensure that the work item is consistent when activating it ... */
  6005. smp_wmb();
  6006. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  6007. /* and that it is marked active as soon as the irq could fire. */
  6008. smp_wmb();
  6009. }
  6010. static int intel_gen2_queue_flip(struct drm_device *dev,
  6011. struct drm_crtc *crtc,
  6012. struct drm_framebuffer *fb,
  6013. struct drm_i915_gem_object *obj)
  6014. {
  6015. struct drm_i915_private *dev_priv = dev->dev_private;
  6016. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6017. u32 flip_mask;
  6018. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6019. int ret;
  6020. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6021. if (ret)
  6022. goto err;
  6023. ret = intel_ring_begin(ring, 6);
  6024. if (ret)
  6025. goto err_unpin;
  6026. /* Can't queue multiple flips, so wait for the previous
  6027. * one to finish before executing the next.
  6028. */
  6029. if (intel_crtc->plane)
  6030. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6031. else
  6032. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6033. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  6034. intel_ring_emit(ring, MI_NOOP);
  6035. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6036. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6037. intel_ring_emit(ring, fb->pitches[0]);
  6038. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  6039. intel_ring_emit(ring, 0); /* aux display base address, unused */
  6040. intel_mark_page_flip_active(intel_crtc);
  6041. intel_ring_advance(ring);
  6042. return 0;
  6043. err_unpin:
  6044. intel_unpin_fb_obj(obj);
  6045. err:
  6046. return ret;
  6047. }
  6048. static int intel_gen3_queue_flip(struct drm_device *dev,
  6049. struct drm_crtc *crtc,
  6050. struct drm_framebuffer *fb,
  6051. struct drm_i915_gem_object *obj)
  6052. {
  6053. struct drm_i915_private *dev_priv = dev->dev_private;
  6054. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6055. u32 flip_mask;
  6056. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6057. int ret;
  6058. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6059. if (ret)
  6060. goto err;
  6061. ret = intel_ring_begin(ring, 6);
  6062. if (ret)
  6063. goto err_unpin;
  6064. if (intel_crtc->plane)
  6065. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6066. else
  6067. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6068. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  6069. intel_ring_emit(ring, MI_NOOP);
  6070. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  6071. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6072. intel_ring_emit(ring, fb->pitches[0]);
  6073. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  6074. intel_ring_emit(ring, MI_NOOP);
  6075. intel_mark_page_flip_active(intel_crtc);
  6076. intel_ring_advance(ring);
  6077. return 0;
  6078. err_unpin:
  6079. intel_unpin_fb_obj(obj);
  6080. err:
  6081. return ret;
  6082. }
  6083. static int intel_gen4_queue_flip(struct drm_device *dev,
  6084. struct drm_crtc *crtc,
  6085. struct drm_framebuffer *fb,
  6086. struct drm_i915_gem_object *obj)
  6087. {
  6088. struct drm_i915_private *dev_priv = dev->dev_private;
  6089. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6090. uint32_t pf, pipesrc;
  6091. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6092. int ret;
  6093. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6094. if (ret)
  6095. goto err;
  6096. ret = intel_ring_begin(ring, 4);
  6097. if (ret)
  6098. goto err_unpin;
  6099. /* i965+ uses the linear or tiled offsets from the
  6100. * Display Registers (which do not change across a page-flip)
  6101. * so we need only reprogram the base address.
  6102. */
  6103. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6104. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6105. intel_ring_emit(ring, fb->pitches[0]);
  6106. intel_ring_emit(ring,
  6107. (obj->gtt_offset + intel_crtc->dspaddr_offset) |
  6108. obj->tiling_mode);
  6109. /* XXX Enabling the panel-fitter across page-flip is so far
  6110. * untested on non-native modes, so ignore it for now.
  6111. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  6112. */
  6113. pf = 0;
  6114. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6115. intel_ring_emit(ring, pf | pipesrc);
  6116. intel_mark_page_flip_active(intel_crtc);
  6117. intel_ring_advance(ring);
  6118. return 0;
  6119. err_unpin:
  6120. intel_unpin_fb_obj(obj);
  6121. err:
  6122. return ret;
  6123. }
  6124. static int intel_gen6_queue_flip(struct drm_device *dev,
  6125. struct drm_crtc *crtc,
  6126. struct drm_framebuffer *fb,
  6127. struct drm_i915_gem_object *obj)
  6128. {
  6129. struct drm_i915_private *dev_priv = dev->dev_private;
  6130. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6131. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6132. uint32_t pf, pipesrc;
  6133. int ret;
  6134. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6135. if (ret)
  6136. goto err;
  6137. ret = intel_ring_begin(ring, 4);
  6138. if (ret)
  6139. goto err_unpin;
  6140. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6141. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6142. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  6143. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  6144. /* Contrary to the suggestions in the documentation,
  6145. * "Enable Panel Fitter" does not seem to be required when page
  6146. * flipping with a non-native mode, and worse causes a normal
  6147. * modeset to fail.
  6148. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  6149. */
  6150. pf = 0;
  6151. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6152. intel_ring_emit(ring, pf | pipesrc);
  6153. intel_mark_page_flip_active(intel_crtc);
  6154. intel_ring_advance(ring);
  6155. return 0;
  6156. err_unpin:
  6157. intel_unpin_fb_obj(obj);
  6158. err:
  6159. return ret;
  6160. }
  6161. /*
  6162. * On gen7 we currently use the blit ring because (in early silicon at least)
  6163. * the render ring doesn't give us interrpts for page flip completion, which
  6164. * means clients will hang after the first flip is queued. Fortunately the
  6165. * blit ring generates interrupts properly, so use it instead.
  6166. */
  6167. static int intel_gen7_queue_flip(struct drm_device *dev,
  6168. struct drm_crtc *crtc,
  6169. struct drm_framebuffer *fb,
  6170. struct drm_i915_gem_object *obj)
  6171. {
  6172. struct drm_i915_private *dev_priv = dev->dev_private;
  6173. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6174. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  6175. uint32_t plane_bit = 0;
  6176. int ret;
  6177. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6178. if (ret)
  6179. goto err;
  6180. switch(intel_crtc->plane) {
  6181. case PLANE_A:
  6182. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  6183. break;
  6184. case PLANE_B:
  6185. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  6186. break;
  6187. case PLANE_C:
  6188. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  6189. break;
  6190. default:
  6191. WARN_ONCE(1, "unknown plane in flip command\n");
  6192. ret = -ENODEV;
  6193. goto err_unpin;
  6194. }
  6195. ret = intel_ring_begin(ring, 4);
  6196. if (ret)
  6197. goto err_unpin;
  6198. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  6199. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  6200. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  6201. intel_ring_emit(ring, (MI_NOOP));
  6202. intel_mark_page_flip_active(intel_crtc);
  6203. intel_ring_advance(ring);
  6204. return 0;
  6205. err_unpin:
  6206. intel_unpin_fb_obj(obj);
  6207. err:
  6208. return ret;
  6209. }
  6210. static int intel_default_queue_flip(struct drm_device *dev,
  6211. struct drm_crtc *crtc,
  6212. struct drm_framebuffer *fb,
  6213. struct drm_i915_gem_object *obj)
  6214. {
  6215. return -ENODEV;
  6216. }
  6217. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  6218. struct drm_framebuffer *fb,
  6219. struct drm_pending_vblank_event *event)
  6220. {
  6221. struct drm_device *dev = crtc->dev;
  6222. struct drm_i915_private *dev_priv = dev->dev_private;
  6223. struct drm_framebuffer *old_fb = crtc->fb;
  6224. struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
  6225. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6226. struct intel_unpin_work *work;
  6227. unsigned long flags;
  6228. int ret;
  6229. /* Can't change pixel format via MI display flips. */
  6230. if (fb->pixel_format != crtc->fb->pixel_format)
  6231. return -EINVAL;
  6232. /*
  6233. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  6234. * Note that pitch changes could also affect these register.
  6235. */
  6236. if (INTEL_INFO(dev)->gen > 3 &&
  6237. (fb->offsets[0] != crtc->fb->offsets[0] ||
  6238. fb->pitches[0] != crtc->fb->pitches[0]))
  6239. return -EINVAL;
  6240. work = kzalloc(sizeof *work, GFP_KERNEL);
  6241. if (work == NULL)
  6242. return -ENOMEM;
  6243. work->event = event;
  6244. work->crtc = crtc;
  6245. work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
  6246. INIT_WORK(&work->work, intel_unpin_work_fn);
  6247. ret = drm_vblank_get(dev, intel_crtc->pipe);
  6248. if (ret)
  6249. goto free_work;
  6250. /* We borrow the event spin lock for protecting unpin_work */
  6251. spin_lock_irqsave(&dev->event_lock, flags);
  6252. if (intel_crtc->unpin_work) {
  6253. spin_unlock_irqrestore(&dev->event_lock, flags);
  6254. kfree(work);
  6255. drm_vblank_put(dev, intel_crtc->pipe);
  6256. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  6257. return -EBUSY;
  6258. }
  6259. intel_crtc->unpin_work = work;
  6260. spin_unlock_irqrestore(&dev->event_lock, flags);
  6261. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  6262. flush_workqueue(dev_priv->wq);
  6263. ret = i915_mutex_lock_interruptible(dev);
  6264. if (ret)
  6265. goto cleanup;
  6266. /* Reference the objects for the scheduled work. */
  6267. drm_gem_object_reference(&work->old_fb_obj->base);
  6268. drm_gem_object_reference(&obj->base);
  6269. crtc->fb = fb;
  6270. work->pending_flip_obj = obj;
  6271. work->enable_stall_check = true;
  6272. atomic_inc(&intel_crtc->unpin_work_count);
  6273. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  6274. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  6275. if (ret)
  6276. goto cleanup_pending;
  6277. intel_disable_fbc(dev);
  6278. intel_mark_fb_busy(obj);
  6279. mutex_unlock(&dev->struct_mutex);
  6280. trace_i915_flip_request(intel_crtc->plane, obj);
  6281. return 0;
  6282. cleanup_pending:
  6283. atomic_dec(&intel_crtc->unpin_work_count);
  6284. crtc->fb = old_fb;
  6285. drm_gem_object_unreference(&work->old_fb_obj->base);
  6286. drm_gem_object_unreference(&obj->base);
  6287. mutex_unlock(&dev->struct_mutex);
  6288. cleanup:
  6289. spin_lock_irqsave(&dev->event_lock, flags);
  6290. intel_crtc->unpin_work = NULL;
  6291. spin_unlock_irqrestore(&dev->event_lock, flags);
  6292. drm_vblank_put(dev, intel_crtc->pipe);
  6293. free_work:
  6294. kfree(work);
  6295. return ret;
  6296. }
  6297. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  6298. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  6299. .load_lut = intel_crtc_load_lut,
  6300. };
  6301. static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
  6302. struct drm_crtc *crtc)
  6303. {
  6304. struct drm_device *dev;
  6305. struct drm_crtc *tmp;
  6306. int crtc_mask = 1;
  6307. WARN(!crtc, "checking null crtc?\n");
  6308. dev = crtc->dev;
  6309. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  6310. if (tmp == crtc)
  6311. break;
  6312. crtc_mask <<= 1;
  6313. }
  6314. if (encoder->possible_crtcs & crtc_mask)
  6315. return true;
  6316. return false;
  6317. }
  6318. /**
  6319. * intel_modeset_update_staged_output_state
  6320. *
  6321. * Updates the staged output configuration state, e.g. after we've read out the
  6322. * current hw state.
  6323. */
  6324. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  6325. {
  6326. struct intel_encoder *encoder;
  6327. struct intel_connector *connector;
  6328. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6329. base.head) {
  6330. connector->new_encoder =
  6331. to_intel_encoder(connector->base.encoder);
  6332. }
  6333. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6334. base.head) {
  6335. encoder->new_crtc =
  6336. to_intel_crtc(encoder->base.crtc);
  6337. }
  6338. }
  6339. /**
  6340. * intel_modeset_commit_output_state
  6341. *
  6342. * This function copies the stage display pipe configuration to the real one.
  6343. */
  6344. static void intel_modeset_commit_output_state(struct drm_device *dev)
  6345. {
  6346. struct intel_encoder *encoder;
  6347. struct intel_connector *connector;
  6348. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6349. base.head) {
  6350. connector->base.encoder = &connector->new_encoder->base;
  6351. }
  6352. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6353. base.head) {
  6354. encoder->base.crtc = &encoder->new_crtc->base;
  6355. }
  6356. }
  6357. static void
  6358. connected_sink_compute_bpp(struct intel_connector * connector,
  6359. struct intel_crtc_config *pipe_config)
  6360. {
  6361. int bpp = pipe_config->pipe_bpp;
  6362. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  6363. connector->base.base.id,
  6364. drm_get_connector_name(&connector->base));
  6365. /* Don't use an invalid EDID bpc value */
  6366. if (connector->base.display_info.bpc &&
  6367. connector->base.display_info.bpc * 3 < bpp) {
  6368. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  6369. bpp, connector->base.display_info.bpc*3);
  6370. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  6371. }
  6372. /* Clamp bpp to 8 on screens without EDID 1.4 */
  6373. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  6374. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  6375. bpp);
  6376. pipe_config->pipe_bpp = 24;
  6377. }
  6378. }
  6379. static int
  6380. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  6381. struct drm_framebuffer *fb,
  6382. struct intel_crtc_config *pipe_config)
  6383. {
  6384. struct drm_device *dev = crtc->base.dev;
  6385. struct intel_connector *connector;
  6386. int bpp;
  6387. switch (fb->pixel_format) {
  6388. case DRM_FORMAT_C8:
  6389. bpp = 8*3; /* since we go through a colormap */
  6390. break;
  6391. case DRM_FORMAT_XRGB1555:
  6392. case DRM_FORMAT_ARGB1555:
  6393. /* checked in intel_framebuffer_init already */
  6394. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  6395. return -EINVAL;
  6396. case DRM_FORMAT_RGB565:
  6397. bpp = 6*3; /* min is 18bpp */
  6398. break;
  6399. case DRM_FORMAT_XBGR8888:
  6400. case DRM_FORMAT_ABGR8888:
  6401. /* checked in intel_framebuffer_init already */
  6402. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  6403. return -EINVAL;
  6404. case DRM_FORMAT_XRGB8888:
  6405. case DRM_FORMAT_ARGB8888:
  6406. bpp = 8*3;
  6407. break;
  6408. case DRM_FORMAT_XRGB2101010:
  6409. case DRM_FORMAT_ARGB2101010:
  6410. case DRM_FORMAT_XBGR2101010:
  6411. case DRM_FORMAT_ABGR2101010:
  6412. /* checked in intel_framebuffer_init already */
  6413. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  6414. return -EINVAL;
  6415. bpp = 10*3;
  6416. break;
  6417. /* TODO: gen4+ supports 16 bpc floating point, too. */
  6418. default:
  6419. DRM_DEBUG_KMS("unsupported depth\n");
  6420. return -EINVAL;
  6421. }
  6422. pipe_config->pipe_bpp = bpp;
  6423. /* Clamp display bpp to EDID value */
  6424. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6425. base.head) {
  6426. if (!connector->new_encoder ||
  6427. connector->new_encoder->new_crtc != crtc)
  6428. continue;
  6429. connected_sink_compute_bpp(connector, pipe_config);
  6430. }
  6431. return bpp;
  6432. }
  6433. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  6434. struct intel_crtc_config *pipe_config,
  6435. const char *context)
  6436. {
  6437. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  6438. context, pipe_name(crtc->pipe));
  6439. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  6440. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  6441. pipe_config->pipe_bpp, pipe_config->dither);
  6442. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  6443. pipe_config->has_pch_encoder,
  6444. pipe_config->fdi_lanes,
  6445. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  6446. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  6447. pipe_config->fdi_m_n.tu);
  6448. DRM_DEBUG_KMS("requested mode:\n");
  6449. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  6450. DRM_DEBUG_KMS("adjusted mode:\n");
  6451. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  6452. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  6453. pipe_config->gmch_pfit.control,
  6454. pipe_config->gmch_pfit.pgm_ratios,
  6455. pipe_config->gmch_pfit.lvds_border_bits);
  6456. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x\n",
  6457. pipe_config->pch_pfit.pos,
  6458. pipe_config->pch_pfit.size);
  6459. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  6460. }
  6461. static bool check_encoder_cloning(struct drm_crtc *crtc)
  6462. {
  6463. int num_encoders = 0;
  6464. bool uncloneable_encoders = false;
  6465. struct intel_encoder *encoder;
  6466. list_for_each_entry(encoder, &crtc->dev->mode_config.encoder_list,
  6467. base.head) {
  6468. if (&encoder->new_crtc->base != crtc)
  6469. continue;
  6470. num_encoders++;
  6471. if (!encoder->cloneable)
  6472. uncloneable_encoders = true;
  6473. }
  6474. return !(num_encoders > 1 && uncloneable_encoders);
  6475. }
  6476. static struct intel_crtc_config *
  6477. intel_modeset_pipe_config(struct drm_crtc *crtc,
  6478. struct drm_framebuffer *fb,
  6479. struct drm_display_mode *mode)
  6480. {
  6481. struct drm_device *dev = crtc->dev;
  6482. struct drm_encoder_helper_funcs *encoder_funcs;
  6483. struct intel_encoder *encoder;
  6484. struct intel_crtc_config *pipe_config;
  6485. int plane_bpp, ret = -EINVAL;
  6486. bool retry = true;
  6487. if (!check_encoder_cloning(crtc)) {
  6488. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  6489. return ERR_PTR(-EINVAL);
  6490. }
  6491. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6492. if (!pipe_config)
  6493. return ERR_PTR(-ENOMEM);
  6494. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  6495. drm_mode_copy(&pipe_config->requested_mode, mode);
  6496. pipe_config->cpu_transcoder = to_intel_crtc(crtc)->pipe;
  6497. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  6498. * plane pixel format and any sink constraints into account. Returns the
  6499. * source plane bpp so that dithering can be selected on mismatches
  6500. * after encoders and crtc also have had their say. */
  6501. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  6502. fb, pipe_config);
  6503. if (plane_bpp < 0)
  6504. goto fail;
  6505. encoder_retry:
  6506. /* Ensure the port clock defaults are reset when retrying. */
  6507. pipe_config->port_clock = 0;
  6508. pipe_config->pixel_multiplier = 1;
  6509. /* Pass our mode to the connectors and the CRTC to give them a chance to
  6510. * adjust it according to limitations or connector properties, and also
  6511. * a chance to reject the mode entirely.
  6512. */
  6513. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6514. base.head) {
  6515. if (&encoder->new_crtc->base != crtc)
  6516. continue;
  6517. if (encoder->compute_config) {
  6518. if (!(encoder->compute_config(encoder, pipe_config))) {
  6519. DRM_DEBUG_KMS("Encoder config failure\n");
  6520. goto fail;
  6521. }
  6522. continue;
  6523. }
  6524. encoder_funcs = encoder->base.helper_private;
  6525. if (!(encoder_funcs->mode_fixup(&encoder->base,
  6526. &pipe_config->requested_mode,
  6527. &pipe_config->adjusted_mode))) {
  6528. DRM_DEBUG_KMS("Encoder fixup failed\n");
  6529. goto fail;
  6530. }
  6531. }
  6532. /* Set default port clock if not overwritten by the encoder. Needs to be
  6533. * done afterwards in case the encoder adjusts the mode. */
  6534. if (!pipe_config->port_clock)
  6535. pipe_config->port_clock = pipe_config->adjusted_mode.clock;
  6536. ret = intel_crtc_compute_config(crtc, pipe_config);
  6537. if (ret < 0) {
  6538. DRM_DEBUG_KMS("CRTC fixup failed\n");
  6539. goto fail;
  6540. }
  6541. if (ret == RETRY) {
  6542. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  6543. ret = -EINVAL;
  6544. goto fail;
  6545. }
  6546. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  6547. retry = false;
  6548. goto encoder_retry;
  6549. }
  6550. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  6551. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  6552. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  6553. return pipe_config;
  6554. fail:
  6555. kfree(pipe_config);
  6556. return ERR_PTR(ret);
  6557. }
  6558. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  6559. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  6560. static void
  6561. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  6562. unsigned *prepare_pipes, unsigned *disable_pipes)
  6563. {
  6564. struct intel_crtc *intel_crtc;
  6565. struct drm_device *dev = crtc->dev;
  6566. struct intel_encoder *encoder;
  6567. struct intel_connector *connector;
  6568. struct drm_crtc *tmp_crtc;
  6569. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  6570. /* Check which crtcs have changed outputs connected to them, these need
  6571. * to be part of the prepare_pipes mask. We don't (yet) support global
  6572. * modeset across multiple crtcs, so modeset_pipes will only have one
  6573. * bit set at most. */
  6574. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6575. base.head) {
  6576. if (connector->base.encoder == &connector->new_encoder->base)
  6577. continue;
  6578. if (connector->base.encoder) {
  6579. tmp_crtc = connector->base.encoder->crtc;
  6580. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  6581. }
  6582. if (connector->new_encoder)
  6583. *prepare_pipes |=
  6584. 1 << connector->new_encoder->new_crtc->pipe;
  6585. }
  6586. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6587. base.head) {
  6588. if (encoder->base.crtc == &encoder->new_crtc->base)
  6589. continue;
  6590. if (encoder->base.crtc) {
  6591. tmp_crtc = encoder->base.crtc;
  6592. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  6593. }
  6594. if (encoder->new_crtc)
  6595. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  6596. }
  6597. /* Check for any pipes that will be fully disabled ... */
  6598. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  6599. base.head) {
  6600. bool used = false;
  6601. /* Don't try to disable disabled crtcs. */
  6602. if (!intel_crtc->base.enabled)
  6603. continue;
  6604. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6605. base.head) {
  6606. if (encoder->new_crtc == intel_crtc)
  6607. used = true;
  6608. }
  6609. if (!used)
  6610. *disable_pipes |= 1 << intel_crtc->pipe;
  6611. }
  6612. /* set_mode is also used to update properties on life display pipes. */
  6613. intel_crtc = to_intel_crtc(crtc);
  6614. if (crtc->enabled)
  6615. *prepare_pipes |= 1 << intel_crtc->pipe;
  6616. /*
  6617. * For simplicity do a full modeset on any pipe where the output routing
  6618. * changed. We could be more clever, but that would require us to be
  6619. * more careful with calling the relevant encoder->mode_set functions.
  6620. */
  6621. if (*prepare_pipes)
  6622. *modeset_pipes = *prepare_pipes;
  6623. /* ... and mask these out. */
  6624. *modeset_pipes &= ~(*disable_pipes);
  6625. *prepare_pipes &= ~(*disable_pipes);
  6626. /*
  6627. * HACK: We don't (yet) fully support global modesets. intel_set_config
  6628. * obies this rule, but the modeset restore mode of
  6629. * intel_modeset_setup_hw_state does not.
  6630. */
  6631. *modeset_pipes &= 1 << intel_crtc->pipe;
  6632. *prepare_pipes &= 1 << intel_crtc->pipe;
  6633. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  6634. *modeset_pipes, *prepare_pipes, *disable_pipes);
  6635. }
  6636. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  6637. {
  6638. struct drm_encoder *encoder;
  6639. struct drm_device *dev = crtc->dev;
  6640. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  6641. if (encoder->crtc == crtc)
  6642. return true;
  6643. return false;
  6644. }
  6645. static void
  6646. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  6647. {
  6648. struct intel_encoder *intel_encoder;
  6649. struct intel_crtc *intel_crtc;
  6650. struct drm_connector *connector;
  6651. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  6652. base.head) {
  6653. if (!intel_encoder->base.crtc)
  6654. continue;
  6655. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  6656. if (prepare_pipes & (1 << intel_crtc->pipe))
  6657. intel_encoder->connectors_active = false;
  6658. }
  6659. intel_modeset_commit_output_state(dev);
  6660. /* Update computed state. */
  6661. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  6662. base.head) {
  6663. intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
  6664. }
  6665. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  6666. if (!connector->encoder || !connector->encoder->crtc)
  6667. continue;
  6668. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  6669. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  6670. struct drm_property *dpms_property =
  6671. dev->mode_config.dpms_property;
  6672. connector->dpms = DRM_MODE_DPMS_ON;
  6673. drm_object_property_set_value(&connector->base,
  6674. dpms_property,
  6675. DRM_MODE_DPMS_ON);
  6676. intel_encoder = to_intel_encoder(connector->encoder);
  6677. intel_encoder->connectors_active = true;
  6678. }
  6679. }
  6680. }
  6681. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  6682. list_for_each_entry((intel_crtc), \
  6683. &(dev)->mode_config.crtc_list, \
  6684. base.head) \
  6685. if (mask & (1 <<(intel_crtc)->pipe))
  6686. static bool
  6687. intel_pipe_config_compare(struct drm_device *dev,
  6688. struct intel_crtc_config *current_config,
  6689. struct intel_crtc_config *pipe_config)
  6690. {
  6691. #define PIPE_CONF_CHECK_I(name) \
  6692. if (current_config->name != pipe_config->name) { \
  6693. DRM_ERROR("mismatch in " #name " " \
  6694. "(expected %i, found %i)\n", \
  6695. current_config->name, \
  6696. pipe_config->name); \
  6697. return false; \
  6698. }
  6699. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  6700. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  6701. DRM_ERROR("mismatch in " #name " " \
  6702. "(expected %i, found %i)\n", \
  6703. current_config->name & (mask), \
  6704. pipe_config->name & (mask)); \
  6705. return false; \
  6706. }
  6707. PIPE_CONF_CHECK_I(cpu_transcoder);
  6708. PIPE_CONF_CHECK_I(has_pch_encoder);
  6709. PIPE_CONF_CHECK_I(fdi_lanes);
  6710. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  6711. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  6712. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  6713. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  6714. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  6715. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  6716. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  6717. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  6718. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  6719. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  6720. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  6721. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  6722. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  6723. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  6724. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  6725. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  6726. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  6727. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  6728. DRM_MODE_FLAG_INTERLACE);
  6729. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  6730. DRM_MODE_FLAG_PHSYNC);
  6731. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  6732. DRM_MODE_FLAG_NHSYNC);
  6733. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  6734. DRM_MODE_FLAG_PVSYNC);
  6735. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  6736. DRM_MODE_FLAG_NVSYNC);
  6737. PIPE_CONF_CHECK_I(requested_mode.hdisplay);
  6738. PIPE_CONF_CHECK_I(requested_mode.vdisplay);
  6739. PIPE_CONF_CHECK_I(gmch_pfit.control);
  6740. /* pfit ratios are autocomputed by the hw on gen4+ */
  6741. if (INTEL_INFO(dev)->gen < 4)
  6742. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  6743. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  6744. PIPE_CONF_CHECK_I(pch_pfit.pos);
  6745. PIPE_CONF_CHECK_I(pch_pfit.size);
  6746. PIPE_CONF_CHECK_I(ips_enabled);
  6747. #undef PIPE_CONF_CHECK_I
  6748. #undef PIPE_CONF_CHECK_FLAGS
  6749. return true;
  6750. }
  6751. void
  6752. intel_modeset_check_state(struct drm_device *dev)
  6753. {
  6754. drm_i915_private_t *dev_priv = dev->dev_private;
  6755. struct intel_crtc *crtc;
  6756. struct intel_encoder *encoder;
  6757. struct intel_connector *connector;
  6758. struct intel_crtc_config pipe_config;
  6759. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6760. base.head) {
  6761. /* This also checks the encoder/connector hw state with the
  6762. * ->get_hw_state callbacks. */
  6763. intel_connector_check_state(connector);
  6764. WARN(&connector->new_encoder->base != connector->base.encoder,
  6765. "connector's staged encoder doesn't match current encoder\n");
  6766. }
  6767. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6768. base.head) {
  6769. bool enabled = false;
  6770. bool active = false;
  6771. enum pipe pipe, tracked_pipe;
  6772. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  6773. encoder->base.base.id,
  6774. drm_get_encoder_name(&encoder->base));
  6775. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  6776. "encoder's stage crtc doesn't match current crtc\n");
  6777. WARN(encoder->connectors_active && !encoder->base.crtc,
  6778. "encoder's active_connectors set, but no crtc\n");
  6779. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6780. base.head) {
  6781. if (connector->base.encoder != &encoder->base)
  6782. continue;
  6783. enabled = true;
  6784. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  6785. active = true;
  6786. }
  6787. WARN(!!encoder->base.crtc != enabled,
  6788. "encoder's enabled state mismatch "
  6789. "(expected %i, found %i)\n",
  6790. !!encoder->base.crtc, enabled);
  6791. WARN(active && !encoder->base.crtc,
  6792. "active encoder with no crtc\n");
  6793. WARN(encoder->connectors_active != active,
  6794. "encoder's computed active state doesn't match tracked active state "
  6795. "(expected %i, found %i)\n", active, encoder->connectors_active);
  6796. active = encoder->get_hw_state(encoder, &pipe);
  6797. WARN(active != encoder->connectors_active,
  6798. "encoder's hw state doesn't match sw tracking "
  6799. "(expected %i, found %i)\n",
  6800. encoder->connectors_active, active);
  6801. if (!encoder->base.crtc)
  6802. continue;
  6803. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  6804. WARN(active && pipe != tracked_pipe,
  6805. "active encoder's pipe doesn't match"
  6806. "(expected %i, found %i)\n",
  6807. tracked_pipe, pipe);
  6808. }
  6809. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  6810. base.head) {
  6811. bool enabled = false;
  6812. bool active = false;
  6813. memset(&pipe_config, 0, sizeof(pipe_config));
  6814. DRM_DEBUG_KMS("[CRTC:%d]\n",
  6815. crtc->base.base.id);
  6816. WARN(crtc->active && !crtc->base.enabled,
  6817. "active crtc, but not enabled in sw tracking\n");
  6818. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6819. base.head) {
  6820. if (encoder->base.crtc != &crtc->base)
  6821. continue;
  6822. enabled = true;
  6823. if (encoder->connectors_active)
  6824. active = true;
  6825. if (encoder->get_config)
  6826. encoder->get_config(encoder, &pipe_config);
  6827. }
  6828. WARN(active != crtc->active,
  6829. "crtc's computed active state doesn't match tracked active state "
  6830. "(expected %i, found %i)\n", active, crtc->active);
  6831. WARN(enabled != crtc->base.enabled,
  6832. "crtc's computed enabled state doesn't match tracked enabled state "
  6833. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  6834. active = dev_priv->display.get_pipe_config(crtc,
  6835. &pipe_config);
  6836. WARN(crtc->active != active,
  6837. "crtc active state doesn't match with hw state "
  6838. "(expected %i, found %i)\n", crtc->active, active);
  6839. if (active &&
  6840. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  6841. WARN(1, "pipe state doesn't match!\n");
  6842. intel_dump_pipe_config(crtc, &pipe_config,
  6843. "[hw state]");
  6844. intel_dump_pipe_config(crtc, &crtc->config,
  6845. "[sw state]");
  6846. }
  6847. }
  6848. }
  6849. static int __intel_set_mode(struct drm_crtc *crtc,
  6850. struct drm_display_mode *mode,
  6851. int x, int y, struct drm_framebuffer *fb)
  6852. {
  6853. struct drm_device *dev = crtc->dev;
  6854. drm_i915_private_t *dev_priv = dev->dev_private;
  6855. struct drm_display_mode *saved_mode, *saved_hwmode;
  6856. struct intel_crtc_config *pipe_config = NULL;
  6857. struct intel_crtc *intel_crtc;
  6858. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  6859. int ret = 0;
  6860. saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
  6861. if (!saved_mode)
  6862. return -ENOMEM;
  6863. saved_hwmode = saved_mode + 1;
  6864. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  6865. &prepare_pipes, &disable_pipes);
  6866. *saved_hwmode = crtc->hwmode;
  6867. *saved_mode = crtc->mode;
  6868. /* Hack: Because we don't (yet) support global modeset on multiple
  6869. * crtcs, we don't keep track of the new mode for more than one crtc.
  6870. * Hence simply check whether any bit is set in modeset_pipes in all the
  6871. * pieces of code that are not yet converted to deal with mutliple crtcs
  6872. * changing their mode at the same time. */
  6873. if (modeset_pipes) {
  6874. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  6875. if (IS_ERR(pipe_config)) {
  6876. ret = PTR_ERR(pipe_config);
  6877. pipe_config = NULL;
  6878. goto out;
  6879. }
  6880. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  6881. "[modeset]");
  6882. }
  6883. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  6884. intel_crtc_disable(&intel_crtc->base);
  6885. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  6886. if (intel_crtc->base.enabled)
  6887. dev_priv->display.crtc_disable(&intel_crtc->base);
  6888. }
  6889. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  6890. * to set it here already despite that we pass it down the callchain.
  6891. */
  6892. if (modeset_pipes) {
  6893. crtc->mode = *mode;
  6894. /* mode_set/enable/disable functions rely on a correct pipe
  6895. * config. */
  6896. to_intel_crtc(crtc)->config = *pipe_config;
  6897. }
  6898. /* Only after disabling all output pipelines that will be changed can we
  6899. * update the the output configuration. */
  6900. intel_modeset_update_state(dev, prepare_pipes);
  6901. if (dev_priv->display.modeset_global_resources)
  6902. dev_priv->display.modeset_global_resources(dev);
  6903. /* Set up the DPLL and any encoders state that needs to adjust or depend
  6904. * on the DPLL.
  6905. */
  6906. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  6907. ret = intel_crtc_mode_set(&intel_crtc->base,
  6908. x, y, fb);
  6909. if (ret)
  6910. goto done;
  6911. }
  6912. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  6913. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
  6914. dev_priv->display.crtc_enable(&intel_crtc->base);
  6915. if (modeset_pipes) {
  6916. /* Store real post-adjustment hardware mode. */
  6917. crtc->hwmode = pipe_config->adjusted_mode;
  6918. /* Calculate and store various constants which
  6919. * are later needed by vblank and swap-completion
  6920. * timestamping. They are derived from true hwmode.
  6921. */
  6922. drm_calc_timestamping_constants(crtc);
  6923. }
  6924. /* FIXME: add subpixel order */
  6925. done:
  6926. if (ret && crtc->enabled) {
  6927. crtc->hwmode = *saved_hwmode;
  6928. crtc->mode = *saved_mode;
  6929. }
  6930. out:
  6931. kfree(pipe_config);
  6932. kfree(saved_mode);
  6933. return ret;
  6934. }
  6935. int intel_set_mode(struct drm_crtc *crtc,
  6936. struct drm_display_mode *mode,
  6937. int x, int y, struct drm_framebuffer *fb)
  6938. {
  6939. int ret;
  6940. ret = __intel_set_mode(crtc, mode, x, y, fb);
  6941. if (ret == 0)
  6942. intel_modeset_check_state(crtc->dev);
  6943. return ret;
  6944. }
  6945. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  6946. {
  6947. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
  6948. }
  6949. #undef for_each_intel_crtc_masked
  6950. static void intel_set_config_free(struct intel_set_config *config)
  6951. {
  6952. if (!config)
  6953. return;
  6954. kfree(config->save_connector_encoders);
  6955. kfree(config->save_encoder_crtcs);
  6956. kfree(config);
  6957. }
  6958. static int intel_set_config_save_state(struct drm_device *dev,
  6959. struct intel_set_config *config)
  6960. {
  6961. struct drm_encoder *encoder;
  6962. struct drm_connector *connector;
  6963. int count;
  6964. config->save_encoder_crtcs =
  6965. kcalloc(dev->mode_config.num_encoder,
  6966. sizeof(struct drm_crtc *), GFP_KERNEL);
  6967. if (!config->save_encoder_crtcs)
  6968. return -ENOMEM;
  6969. config->save_connector_encoders =
  6970. kcalloc(dev->mode_config.num_connector,
  6971. sizeof(struct drm_encoder *), GFP_KERNEL);
  6972. if (!config->save_connector_encoders)
  6973. return -ENOMEM;
  6974. /* Copy data. Note that driver private data is not affected.
  6975. * Should anything bad happen only the expected state is
  6976. * restored, not the drivers personal bookkeeping.
  6977. */
  6978. count = 0;
  6979. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  6980. config->save_encoder_crtcs[count++] = encoder->crtc;
  6981. }
  6982. count = 0;
  6983. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  6984. config->save_connector_encoders[count++] = connector->encoder;
  6985. }
  6986. return 0;
  6987. }
  6988. static void intel_set_config_restore_state(struct drm_device *dev,
  6989. struct intel_set_config *config)
  6990. {
  6991. struct intel_encoder *encoder;
  6992. struct intel_connector *connector;
  6993. int count;
  6994. count = 0;
  6995. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  6996. encoder->new_crtc =
  6997. to_intel_crtc(config->save_encoder_crtcs[count++]);
  6998. }
  6999. count = 0;
  7000. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  7001. connector->new_encoder =
  7002. to_intel_encoder(config->save_connector_encoders[count++]);
  7003. }
  7004. }
  7005. static void
  7006. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  7007. struct intel_set_config *config)
  7008. {
  7009. /* We should be able to check here if the fb has the same properties
  7010. * and then just flip_or_move it */
  7011. if (set->crtc->fb != set->fb) {
  7012. /* If we have no fb then treat it as a full mode set */
  7013. if (set->crtc->fb == NULL) {
  7014. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  7015. config->mode_changed = true;
  7016. } else if (set->fb == NULL) {
  7017. config->mode_changed = true;
  7018. } else if (set->fb->pixel_format !=
  7019. set->crtc->fb->pixel_format) {
  7020. config->mode_changed = true;
  7021. } else
  7022. config->fb_changed = true;
  7023. }
  7024. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  7025. config->fb_changed = true;
  7026. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  7027. DRM_DEBUG_KMS("modes are different, full mode set\n");
  7028. drm_mode_debug_printmodeline(&set->crtc->mode);
  7029. drm_mode_debug_printmodeline(set->mode);
  7030. config->mode_changed = true;
  7031. }
  7032. }
  7033. static int
  7034. intel_modeset_stage_output_state(struct drm_device *dev,
  7035. struct drm_mode_set *set,
  7036. struct intel_set_config *config)
  7037. {
  7038. struct drm_crtc *new_crtc;
  7039. struct intel_connector *connector;
  7040. struct intel_encoder *encoder;
  7041. int count, ro;
  7042. /* The upper layers ensure that we either disable a crtc or have a list
  7043. * of connectors. For paranoia, double-check this. */
  7044. WARN_ON(!set->fb && (set->num_connectors != 0));
  7045. WARN_ON(set->fb && (set->num_connectors == 0));
  7046. count = 0;
  7047. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7048. base.head) {
  7049. /* Otherwise traverse passed in connector list and get encoders
  7050. * for them. */
  7051. for (ro = 0; ro < set->num_connectors; ro++) {
  7052. if (set->connectors[ro] == &connector->base) {
  7053. connector->new_encoder = connector->encoder;
  7054. break;
  7055. }
  7056. }
  7057. /* If we disable the crtc, disable all its connectors. Also, if
  7058. * the connector is on the changing crtc but not on the new
  7059. * connector list, disable it. */
  7060. if ((!set->fb || ro == set->num_connectors) &&
  7061. connector->base.encoder &&
  7062. connector->base.encoder->crtc == set->crtc) {
  7063. connector->new_encoder = NULL;
  7064. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  7065. connector->base.base.id,
  7066. drm_get_connector_name(&connector->base));
  7067. }
  7068. if (&connector->new_encoder->base != connector->base.encoder) {
  7069. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  7070. config->mode_changed = true;
  7071. }
  7072. }
  7073. /* connector->new_encoder is now updated for all connectors. */
  7074. /* Update crtc of enabled connectors. */
  7075. count = 0;
  7076. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7077. base.head) {
  7078. if (!connector->new_encoder)
  7079. continue;
  7080. new_crtc = connector->new_encoder->base.crtc;
  7081. for (ro = 0; ro < set->num_connectors; ro++) {
  7082. if (set->connectors[ro] == &connector->base)
  7083. new_crtc = set->crtc;
  7084. }
  7085. /* Make sure the new CRTC will work with the encoder */
  7086. if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
  7087. new_crtc)) {
  7088. return -EINVAL;
  7089. }
  7090. connector->encoder->new_crtc = to_intel_crtc(new_crtc);
  7091. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  7092. connector->base.base.id,
  7093. drm_get_connector_name(&connector->base),
  7094. new_crtc->base.id);
  7095. }
  7096. /* Check for any encoders that needs to be disabled. */
  7097. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7098. base.head) {
  7099. list_for_each_entry(connector,
  7100. &dev->mode_config.connector_list,
  7101. base.head) {
  7102. if (connector->new_encoder == encoder) {
  7103. WARN_ON(!connector->new_encoder->new_crtc);
  7104. goto next_encoder;
  7105. }
  7106. }
  7107. encoder->new_crtc = NULL;
  7108. next_encoder:
  7109. /* Only now check for crtc changes so we don't miss encoders
  7110. * that will be disabled. */
  7111. if (&encoder->new_crtc->base != encoder->base.crtc) {
  7112. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  7113. config->mode_changed = true;
  7114. }
  7115. }
  7116. /* Now we've also updated encoder->new_crtc for all encoders. */
  7117. return 0;
  7118. }
  7119. static int intel_crtc_set_config(struct drm_mode_set *set)
  7120. {
  7121. struct drm_device *dev;
  7122. struct drm_mode_set save_set;
  7123. struct intel_set_config *config;
  7124. int ret;
  7125. BUG_ON(!set);
  7126. BUG_ON(!set->crtc);
  7127. BUG_ON(!set->crtc->helper_private);
  7128. /* Enforce sane interface api - has been abused by the fb helper. */
  7129. BUG_ON(!set->mode && set->fb);
  7130. BUG_ON(set->fb && set->num_connectors == 0);
  7131. if (set->fb) {
  7132. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  7133. set->crtc->base.id, set->fb->base.id,
  7134. (int)set->num_connectors, set->x, set->y);
  7135. } else {
  7136. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  7137. }
  7138. dev = set->crtc->dev;
  7139. ret = -ENOMEM;
  7140. config = kzalloc(sizeof(*config), GFP_KERNEL);
  7141. if (!config)
  7142. goto out_config;
  7143. ret = intel_set_config_save_state(dev, config);
  7144. if (ret)
  7145. goto out_config;
  7146. save_set.crtc = set->crtc;
  7147. save_set.mode = &set->crtc->mode;
  7148. save_set.x = set->crtc->x;
  7149. save_set.y = set->crtc->y;
  7150. save_set.fb = set->crtc->fb;
  7151. /* Compute whether we need a full modeset, only an fb base update or no
  7152. * change at all. In the future we might also check whether only the
  7153. * mode changed, e.g. for LVDS where we only change the panel fitter in
  7154. * such cases. */
  7155. intel_set_config_compute_mode_changes(set, config);
  7156. ret = intel_modeset_stage_output_state(dev, set, config);
  7157. if (ret)
  7158. goto fail;
  7159. if (config->mode_changed) {
  7160. ret = intel_set_mode(set->crtc, set->mode,
  7161. set->x, set->y, set->fb);
  7162. if (ret) {
  7163. DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
  7164. set->crtc->base.id, ret);
  7165. goto fail;
  7166. }
  7167. } else if (config->fb_changed) {
  7168. intel_crtc_wait_for_pending_flips(set->crtc);
  7169. ret = intel_pipe_set_base(set->crtc,
  7170. set->x, set->y, set->fb);
  7171. }
  7172. intel_set_config_free(config);
  7173. return 0;
  7174. fail:
  7175. intel_set_config_restore_state(dev, config);
  7176. /* Try to restore the config */
  7177. if (config->mode_changed &&
  7178. intel_set_mode(save_set.crtc, save_set.mode,
  7179. save_set.x, save_set.y, save_set.fb))
  7180. DRM_ERROR("failed to restore config after modeset failure\n");
  7181. out_config:
  7182. intel_set_config_free(config);
  7183. return ret;
  7184. }
  7185. static const struct drm_crtc_funcs intel_crtc_funcs = {
  7186. .cursor_set = intel_crtc_cursor_set,
  7187. .cursor_move = intel_crtc_cursor_move,
  7188. .gamma_set = intel_crtc_gamma_set,
  7189. .set_config = intel_crtc_set_config,
  7190. .destroy = intel_crtc_destroy,
  7191. .page_flip = intel_crtc_page_flip,
  7192. };
  7193. static void intel_cpu_pll_init(struct drm_device *dev)
  7194. {
  7195. if (HAS_DDI(dev))
  7196. intel_ddi_pll_init(dev);
  7197. }
  7198. static void intel_pch_pll_init(struct drm_device *dev)
  7199. {
  7200. drm_i915_private_t *dev_priv = dev->dev_private;
  7201. int i;
  7202. if (dev_priv->num_pch_pll == 0) {
  7203. DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
  7204. return;
  7205. }
  7206. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  7207. dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
  7208. dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
  7209. dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
  7210. }
  7211. }
  7212. static void intel_crtc_init(struct drm_device *dev, int pipe)
  7213. {
  7214. drm_i915_private_t *dev_priv = dev->dev_private;
  7215. struct intel_crtc *intel_crtc;
  7216. int i;
  7217. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  7218. if (intel_crtc == NULL)
  7219. return;
  7220. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  7221. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  7222. for (i = 0; i < 256; i++) {
  7223. intel_crtc->lut_r[i] = i;
  7224. intel_crtc->lut_g[i] = i;
  7225. intel_crtc->lut_b[i] = i;
  7226. }
  7227. /* Swap pipes & planes for FBC on pre-965 */
  7228. intel_crtc->pipe = pipe;
  7229. intel_crtc->plane = pipe;
  7230. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  7231. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  7232. intel_crtc->plane = !pipe;
  7233. }
  7234. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  7235. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  7236. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  7237. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  7238. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  7239. }
  7240. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  7241. struct drm_file *file)
  7242. {
  7243. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  7244. struct drm_mode_object *drmmode_obj;
  7245. struct intel_crtc *crtc;
  7246. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  7247. return -ENODEV;
  7248. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  7249. DRM_MODE_OBJECT_CRTC);
  7250. if (!drmmode_obj) {
  7251. DRM_ERROR("no such CRTC id\n");
  7252. return -EINVAL;
  7253. }
  7254. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  7255. pipe_from_crtc_id->pipe = crtc->pipe;
  7256. return 0;
  7257. }
  7258. static int intel_encoder_clones(struct intel_encoder *encoder)
  7259. {
  7260. struct drm_device *dev = encoder->base.dev;
  7261. struct intel_encoder *source_encoder;
  7262. int index_mask = 0;
  7263. int entry = 0;
  7264. list_for_each_entry(source_encoder,
  7265. &dev->mode_config.encoder_list, base.head) {
  7266. if (encoder == source_encoder)
  7267. index_mask |= (1 << entry);
  7268. /* Intel hw has only one MUX where enocoders could be cloned. */
  7269. if (encoder->cloneable && source_encoder->cloneable)
  7270. index_mask |= (1 << entry);
  7271. entry++;
  7272. }
  7273. return index_mask;
  7274. }
  7275. static bool has_edp_a(struct drm_device *dev)
  7276. {
  7277. struct drm_i915_private *dev_priv = dev->dev_private;
  7278. if (!IS_MOBILE(dev))
  7279. return false;
  7280. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  7281. return false;
  7282. if (IS_GEN5(dev) &&
  7283. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  7284. return false;
  7285. return true;
  7286. }
  7287. static void intel_setup_outputs(struct drm_device *dev)
  7288. {
  7289. struct drm_i915_private *dev_priv = dev->dev_private;
  7290. struct intel_encoder *encoder;
  7291. bool dpd_is_edp = false;
  7292. bool has_lvds;
  7293. has_lvds = intel_lvds_init(dev);
  7294. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  7295. /* disable the panel fitter on everything but LVDS */
  7296. I915_WRITE(PFIT_CONTROL, 0);
  7297. }
  7298. if (!IS_ULT(dev))
  7299. intel_crt_init(dev);
  7300. if (HAS_DDI(dev)) {
  7301. int found;
  7302. /* Haswell uses DDI functions to detect digital outputs */
  7303. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  7304. /* DDI A only supports eDP */
  7305. if (found)
  7306. intel_ddi_init(dev, PORT_A);
  7307. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  7308. * register */
  7309. found = I915_READ(SFUSE_STRAP);
  7310. if (found & SFUSE_STRAP_DDIB_DETECTED)
  7311. intel_ddi_init(dev, PORT_B);
  7312. if (found & SFUSE_STRAP_DDIC_DETECTED)
  7313. intel_ddi_init(dev, PORT_C);
  7314. if (found & SFUSE_STRAP_DDID_DETECTED)
  7315. intel_ddi_init(dev, PORT_D);
  7316. } else if (HAS_PCH_SPLIT(dev)) {
  7317. int found;
  7318. dpd_is_edp = intel_dpd_is_edp(dev);
  7319. if (has_edp_a(dev))
  7320. intel_dp_init(dev, DP_A, PORT_A);
  7321. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  7322. /* PCH SDVOB multiplex with HDMIB */
  7323. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  7324. if (!found)
  7325. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  7326. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  7327. intel_dp_init(dev, PCH_DP_B, PORT_B);
  7328. }
  7329. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  7330. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  7331. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  7332. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  7333. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  7334. intel_dp_init(dev, PCH_DP_C, PORT_C);
  7335. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  7336. intel_dp_init(dev, PCH_DP_D, PORT_D);
  7337. } else if (IS_VALLEYVIEW(dev)) {
  7338. /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
  7339. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  7340. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  7341. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  7342. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  7343. PORT_B);
  7344. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  7345. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  7346. }
  7347. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  7348. bool found = false;
  7349. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  7350. DRM_DEBUG_KMS("probing SDVOB\n");
  7351. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  7352. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  7353. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  7354. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  7355. }
  7356. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  7357. intel_dp_init(dev, DP_B, PORT_B);
  7358. }
  7359. /* Before G4X SDVOC doesn't have its own detect register */
  7360. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  7361. DRM_DEBUG_KMS("probing SDVOC\n");
  7362. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  7363. }
  7364. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  7365. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  7366. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  7367. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  7368. }
  7369. if (SUPPORTS_INTEGRATED_DP(dev))
  7370. intel_dp_init(dev, DP_C, PORT_C);
  7371. }
  7372. if (SUPPORTS_INTEGRATED_DP(dev) &&
  7373. (I915_READ(DP_D) & DP_DETECTED))
  7374. intel_dp_init(dev, DP_D, PORT_D);
  7375. } else if (IS_GEN2(dev))
  7376. intel_dvo_init(dev);
  7377. if (SUPPORTS_TV(dev))
  7378. intel_tv_init(dev);
  7379. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  7380. encoder->base.possible_crtcs = encoder->crtc_mask;
  7381. encoder->base.possible_clones =
  7382. intel_encoder_clones(encoder);
  7383. }
  7384. intel_init_pch_refclk(dev);
  7385. drm_helper_move_panel_connectors_to_head(dev);
  7386. }
  7387. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  7388. {
  7389. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7390. drm_framebuffer_cleanup(fb);
  7391. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  7392. kfree(intel_fb);
  7393. }
  7394. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  7395. struct drm_file *file,
  7396. unsigned int *handle)
  7397. {
  7398. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7399. struct drm_i915_gem_object *obj = intel_fb->obj;
  7400. return drm_gem_handle_create(file, &obj->base, handle);
  7401. }
  7402. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  7403. .destroy = intel_user_framebuffer_destroy,
  7404. .create_handle = intel_user_framebuffer_create_handle,
  7405. };
  7406. int intel_framebuffer_init(struct drm_device *dev,
  7407. struct intel_framebuffer *intel_fb,
  7408. struct drm_mode_fb_cmd2 *mode_cmd,
  7409. struct drm_i915_gem_object *obj)
  7410. {
  7411. int ret;
  7412. if (obj->tiling_mode == I915_TILING_Y) {
  7413. DRM_DEBUG("hardware does not support tiling Y\n");
  7414. return -EINVAL;
  7415. }
  7416. if (mode_cmd->pitches[0] & 63) {
  7417. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  7418. mode_cmd->pitches[0]);
  7419. return -EINVAL;
  7420. }
  7421. /* FIXME <= Gen4 stride limits are bit unclear */
  7422. if (mode_cmd->pitches[0] > 32768) {
  7423. DRM_DEBUG("pitch (%d) must be at less than 32768\n",
  7424. mode_cmd->pitches[0]);
  7425. return -EINVAL;
  7426. }
  7427. if (obj->tiling_mode != I915_TILING_NONE &&
  7428. mode_cmd->pitches[0] != obj->stride) {
  7429. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  7430. mode_cmd->pitches[0], obj->stride);
  7431. return -EINVAL;
  7432. }
  7433. /* Reject formats not supported by any plane early. */
  7434. switch (mode_cmd->pixel_format) {
  7435. case DRM_FORMAT_C8:
  7436. case DRM_FORMAT_RGB565:
  7437. case DRM_FORMAT_XRGB8888:
  7438. case DRM_FORMAT_ARGB8888:
  7439. break;
  7440. case DRM_FORMAT_XRGB1555:
  7441. case DRM_FORMAT_ARGB1555:
  7442. if (INTEL_INFO(dev)->gen > 3) {
  7443. DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
  7444. return -EINVAL;
  7445. }
  7446. break;
  7447. case DRM_FORMAT_XBGR8888:
  7448. case DRM_FORMAT_ABGR8888:
  7449. case DRM_FORMAT_XRGB2101010:
  7450. case DRM_FORMAT_ARGB2101010:
  7451. case DRM_FORMAT_XBGR2101010:
  7452. case DRM_FORMAT_ABGR2101010:
  7453. if (INTEL_INFO(dev)->gen < 4) {
  7454. DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
  7455. return -EINVAL;
  7456. }
  7457. break;
  7458. case DRM_FORMAT_YUYV:
  7459. case DRM_FORMAT_UYVY:
  7460. case DRM_FORMAT_YVYU:
  7461. case DRM_FORMAT_VYUY:
  7462. if (INTEL_INFO(dev)->gen < 5) {
  7463. DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
  7464. return -EINVAL;
  7465. }
  7466. break;
  7467. default:
  7468. DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
  7469. return -EINVAL;
  7470. }
  7471. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  7472. if (mode_cmd->offsets[0] != 0)
  7473. return -EINVAL;
  7474. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  7475. intel_fb->obj = obj;
  7476. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  7477. if (ret) {
  7478. DRM_ERROR("framebuffer init failed %d\n", ret);
  7479. return ret;
  7480. }
  7481. return 0;
  7482. }
  7483. static struct drm_framebuffer *
  7484. intel_user_framebuffer_create(struct drm_device *dev,
  7485. struct drm_file *filp,
  7486. struct drm_mode_fb_cmd2 *mode_cmd)
  7487. {
  7488. struct drm_i915_gem_object *obj;
  7489. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  7490. mode_cmd->handles[0]));
  7491. if (&obj->base == NULL)
  7492. return ERR_PTR(-ENOENT);
  7493. return intel_framebuffer_create(dev, mode_cmd, obj);
  7494. }
  7495. static const struct drm_mode_config_funcs intel_mode_funcs = {
  7496. .fb_create = intel_user_framebuffer_create,
  7497. .output_poll_changed = intel_fb_output_poll_changed,
  7498. };
  7499. /* Set up chip specific display functions */
  7500. static void intel_init_display(struct drm_device *dev)
  7501. {
  7502. struct drm_i915_private *dev_priv = dev->dev_private;
  7503. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  7504. dev_priv->display.find_dpll = g4x_find_best_dpll;
  7505. else if (IS_VALLEYVIEW(dev))
  7506. dev_priv->display.find_dpll = vlv_find_best_dpll;
  7507. else if (IS_PINEVIEW(dev))
  7508. dev_priv->display.find_dpll = pnv_find_best_dpll;
  7509. else
  7510. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  7511. if (HAS_DDI(dev)) {
  7512. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  7513. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  7514. dev_priv->display.crtc_enable = haswell_crtc_enable;
  7515. dev_priv->display.crtc_disable = haswell_crtc_disable;
  7516. dev_priv->display.off = haswell_crtc_off;
  7517. dev_priv->display.update_plane = ironlake_update_plane;
  7518. } else if (HAS_PCH_SPLIT(dev)) {
  7519. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  7520. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  7521. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  7522. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  7523. dev_priv->display.off = ironlake_crtc_off;
  7524. dev_priv->display.update_plane = ironlake_update_plane;
  7525. } else if (IS_VALLEYVIEW(dev)) {
  7526. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  7527. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  7528. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  7529. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  7530. dev_priv->display.off = i9xx_crtc_off;
  7531. dev_priv->display.update_plane = i9xx_update_plane;
  7532. } else {
  7533. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  7534. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  7535. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  7536. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  7537. dev_priv->display.off = i9xx_crtc_off;
  7538. dev_priv->display.update_plane = i9xx_update_plane;
  7539. }
  7540. /* Returns the core display clock speed */
  7541. if (IS_VALLEYVIEW(dev))
  7542. dev_priv->display.get_display_clock_speed =
  7543. valleyview_get_display_clock_speed;
  7544. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  7545. dev_priv->display.get_display_clock_speed =
  7546. i945_get_display_clock_speed;
  7547. else if (IS_I915G(dev))
  7548. dev_priv->display.get_display_clock_speed =
  7549. i915_get_display_clock_speed;
  7550. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  7551. dev_priv->display.get_display_clock_speed =
  7552. i9xx_misc_get_display_clock_speed;
  7553. else if (IS_I915GM(dev))
  7554. dev_priv->display.get_display_clock_speed =
  7555. i915gm_get_display_clock_speed;
  7556. else if (IS_I865G(dev))
  7557. dev_priv->display.get_display_clock_speed =
  7558. i865_get_display_clock_speed;
  7559. else if (IS_I85X(dev))
  7560. dev_priv->display.get_display_clock_speed =
  7561. i855_get_display_clock_speed;
  7562. else /* 852, 830 */
  7563. dev_priv->display.get_display_clock_speed =
  7564. i830_get_display_clock_speed;
  7565. if (HAS_PCH_SPLIT(dev)) {
  7566. if (IS_GEN5(dev)) {
  7567. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  7568. dev_priv->display.write_eld = ironlake_write_eld;
  7569. } else if (IS_GEN6(dev)) {
  7570. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  7571. dev_priv->display.write_eld = ironlake_write_eld;
  7572. } else if (IS_IVYBRIDGE(dev)) {
  7573. /* FIXME: detect B0+ stepping and use auto training */
  7574. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  7575. dev_priv->display.write_eld = ironlake_write_eld;
  7576. dev_priv->display.modeset_global_resources =
  7577. ivb_modeset_global_resources;
  7578. } else if (IS_HASWELL(dev)) {
  7579. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  7580. dev_priv->display.write_eld = haswell_write_eld;
  7581. dev_priv->display.modeset_global_resources =
  7582. haswell_modeset_global_resources;
  7583. }
  7584. } else if (IS_G4X(dev)) {
  7585. dev_priv->display.write_eld = g4x_write_eld;
  7586. }
  7587. /* Default just returns -ENODEV to indicate unsupported */
  7588. dev_priv->display.queue_flip = intel_default_queue_flip;
  7589. switch (INTEL_INFO(dev)->gen) {
  7590. case 2:
  7591. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  7592. break;
  7593. case 3:
  7594. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  7595. break;
  7596. case 4:
  7597. case 5:
  7598. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  7599. break;
  7600. case 6:
  7601. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  7602. break;
  7603. case 7:
  7604. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  7605. break;
  7606. }
  7607. }
  7608. /*
  7609. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  7610. * resume, or other times. This quirk makes sure that's the case for
  7611. * affected systems.
  7612. */
  7613. static void quirk_pipea_force(struct drm_device *dev)
  7614. {
  7615. struct drm_i915_private *dev_priv = dev->dev_private;
  7616. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  7617. DRM_INFO("applying pipe a force quirk\n");
  7618. }
  7619. /*
  7620. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  7621. */
  7622. static void quirk_ssc_force_disable(struct drm_device *dev)
  7623. {
  7624. struct drm_i915_private *dev_priv = dev->dev_private;
  7625. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  7626. DRM_INFO("applying lvds SSC disable quirk\n");
  7627. }
  7628. /*
  7629. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  7630. * brightness value
  7631. */
  7632. static void quirk_invert_brightness(struct drm_device *dev)
  7633. {
  7634. struct drm_i915_private *dev_priv = dev->dev_private;
  7635. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  7636. DRM_INFO("applying inverted panel brightness quirk\n");
  7637. }
  7638. struct intel_quirk {
  7639. int device;
  7640. int subsystem_vendor;
  7641. int subsystem_device;
  7642. void (*hook)(struct drm_device *dev);
  7643. };
  7644. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  7645. struct intel_dmi_quirk {
  7646. void (*hook)(struct drm_device *dev);
  7647. const struct dmi_system_id (*dmi_id_list)[];
  7648. };
  7649. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  7650. {
  7651. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  7652. return 1;
  7653. }
  7654. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  7655. {
  7656. .dmi_id_list = &(const struct dmi_system_id[]) {
  7657. {
  7658. .callback = intel_dmi_reverse_brightness,
  7659. .ident = "NCR Corporation",
  7660. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  7661. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  7662. },
  7663. },
  7664. { } /* terminating entry */
  7665. },
  7666. .hook = quirk_invert_brightness,
  7667. },
  7668. };
  7669. static struct intel_quirk intel_quirks[] = {
  7670. /* HP Mini needs pipe A force quirk (LP: #322104) */
  7671. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  7672. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  7673. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  7674. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  7675. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  7676. /* 830/845 need to leave pipe A & dpll A up */
  7677. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7678. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7679. /* Lenovo U160 cannot use SSC on LVDS */
  7680. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  7681. /* Sony Vaio Y cannot use SSC on LVDS */
  7682. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  7683. /* Acer Aspire 5734Z must invert backlight brightness */
  7684. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  7685. /* Acer/eMachines G725 */
  7686. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  7687. /* Acer/eMachines e725 */
  7688. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  7689. /* Acer/Packard Bell NCL20 */
  7690. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  7691. /* Acer Aspire 4736Z */
  7692. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  7693. };
  7694. static void intel_init_quirks(struct drm_device *dev)
  7695. {
  7696. struct pci_dev *d = dev->pdev;
  7697. int i;
  7698. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  7699. struct intel_quirk *q = &intel_quirks[i];
  7700. if (d->device == q->device &&
  7701. (d->subsystem_vendor == q->subsystem_vendor ||
  7702. q->subsystem_vendor == PCI_ANY_ID) &&
  7703. (d->subsystem_device == q->subsystem_device ||
  7704. q->subsystem_device == PCI_ANY_ID))
  7705. q->hook(dev);
  7706. }
  7707. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  7708. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  7709. intel_dmi_quirks[i].hook(dev);
  7710. }
  7711. }
  7712. /* Disable the VGA plane that we never use */
  7713. static void i915_disable_vga(struct drm_device *dev)
  7714. {
  7715. struct drm_i915_private *dev_priv = dev->dev_private;
  7716. u8 sr1;
  7717. u32 vga_reg = i915_vgacntrl_reg(dev);
  7718. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  7719. outb(SR01, VGA_SR_INDEX);
  7720. sr1 = inb(VGA_SR_DATA);
  7721. outb(sr1 | 1<<5, VGA_SR_DATA);
  7722. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  7723. udelay(300);
  7724. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  7725. POSTING_READ(vga_reg);
  7726. }
  7727. void intel_modeset_init_hw(struct drm_device *dev)
  7728. {
  7729. intel_init_power_well(dev);
  7730. intel_prepare_ddi(dev);
  7731. intel_init_clock_gating(dev);
  7732. mutex_lock(&dev->struct_mutex);
  7733. intel_enable_gt_powersave(dev);
  7734. mutex_unlock(&dev->struct_mutex);
  7735. }
  7736. void intel_modeset_suspend_hw(struct drm_device *dev)
  7737. {
  7738. intel_suspend_hw(dev);
  7739. }
  7740. void intel_modeset_init(struct drm_device *dev)
  7741. {
  7742. struct drm_i915_private *dev_priv = dev->dev_private;
  7743. int i, j, ret;
  7744. drm_mode_config_init(dev);
  7745. dev->mode_config.min_width = 0;
  7746. dev->mode_config.min_height = 0;
  7747. dev->mode_config.preferred_depth = 24;
  7748. dev->mode_config.prefer_shadow = 1;
  7749. dev->mode_config.funcs = &intel_mode_funcs;
  7750. intel_init_quirks(dev);
  7751. intel_init_pm(dev);
  7752. if (INTEL_INFO(dev)->num_pipes == 0)
  7753. return;
  7754. intel_init_display(dev);
  7755. if (IS_GEN2(dev)) {
  7756. dev->mode_config.max_width = 2048;
  7757. dev->mode_config.max_height = 2048;
  7758. } else if (IS_GEN3(dev)) {
  7759. dev->mode_config.max_width = 4096;
  7760. dev->mode_config.max_height = 4096;
  7761. } else {
  7762. dev->mode_config.max_width = 8192;
  7763. dev->mode_config.max_height = 8192;
  7764. }
  7765. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  7766. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  7767. INTEL_INFO(dev)->num_pipes,
  7768. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  7769. for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
  7770. intel_crtc_init(dev, i);
  7771. for (j = 0; j < dev_priv->num_plane; j++) {
  7772. ret = intel_plane_init(dev, i, j);
  7773. if (ret)
  7774. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  7775. pipe_name(i), sprite_name(i, j), ret);
  7776. }
  7777. }
  7778. intel_cpu_pll_init(dev);
  7779. intel_pch_pll_init(dev);
  7780. /* Just disable it once at startup */
  7781. i915_disable_vga(dev);
  7782. intel_setup_outputs(dev);
  7783. /* Just in case the BIOS is doing something questionable. */
  7784. intel_disable_fbc(dev);
  7785. }
  7786. static void
  7787. intel_connector_break_all_links(struct intel_connector *connector)
  7788. {
  7789. connector->base.dpms = DRM_MODE_DPMS_OFF;
  7790. connector->base.encoder = NULL;
  7791. connector->encoder->connectors_active = false;
  7792. connector->encoder->base.crtc = NULL;
  7793. }
  7794. static void intel_enable_pipe_a(struct drm_device *dev)
  7795. {
  7796. struct intel_connector *connector;
  7797. struct drm_connector *crt = NULL;
  7798. struct intel_load_detect_pipe load_detect_temp;
  7799. /* We can't just switch on the pipe A, we need to set things up with a
  7800. * proper mode and output configuration. As a gross hack, enable pipe A
  7801. * by enabling the load detect pipe once. */
  7802. list_for_each_entry(connector,
  7803. &dev->mode_config.connector_list,
  7804. base.head) {
  7805. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  7806. crt = &connector->base;
  7807. break;
  7808. }
  7809. }
  7810. if (!crt)
  7811. return;
  7812. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
  7813. intel_release_load_detect_pipe(crt, &load_detect_temp);
  7814. }
  7815. static bool
  7816. intel_check_plane_mapping(struct intel_crtc *crtc)
  7817. {
  7818. struct drm_device *dev = crtc->base.dev;
  7819. struct drm_i915_private *dev_priv = dev->dev_private;
  7820. u32 reg, val;
  7821. if (INTEL_INFO(dev)->num_pipes == 1)
  7822. return true;
  7823. reg = DSPCNTR(!crtc->plane);
  7824. val = I915_READ(reg);
  7825. if ((val & DISPLAY_PLANE_ENABLE) &&
  7826. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  7827. return false;
  7828. return true;
  7829. }
  7830. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  7831. {
  7832. struct drm_device *dev = crtc->base.dev;
  7833. struct drm_i915_private *dev_priv = dev->dev_private;
  7834. u32 reg;
  7835. /* Clear any frame start delays used for debugging left by the BIOS */
  7836. reg = PIPECONF(crtc->config.cpu_transcoder);
  7837. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  7838. /* We need to sanitize the plane -> pipe mapping first because this will
  7839. * disable the crtc (and hence change the state) if it is wrong. Note
  7840. * that gen4+ has a fixed plane -> pipe mapping. */
  7841. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  7842. struct intel_connector *connector;
  7843. bool plane;
  7844. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  7845. crtc->base.base.id);
  7846. /* Pipe has the wrong plane attached and the plane is active.
  7847. * Temporarily change the plane mapping and disable everything
  7848. * ... */
  7849. plane = crtc->plane;
  7850. crtc->plane = !plane;
  7851. dev_priv->display.crtc_disable(&crtc->base);
  7852. crtc->plane = plane;
  7853. /* ... and break all links. */
  7854. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7855. base.head) {
  7856. if (connector->encoder->base.crtc != &crtc->base)
  7857. continue;
  7858. intel_connector_break_all_links(connector);
  7859. }
  7860. WARN_ON(crtc->active);
  7861. crtc->base.enabled = false;
  7862. }
  7863. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  7864. crtc->pipe == PIPE_A && !crtc->active) {
  7865. /* BIOS forgot to enable pipe A, this mostly happens after
  7866. * resume. Force-enable the pipe to fix this, the update_dpms
  7867. * call below we restore the pipe to the right state, but leave
  7868. * the required bits on. */
  7869. intel_enable_pipe_a(dev);
  7870. }
  7871. /* Adjust the state of the output pipe according to whether we
  7872. * have active connectors/encoders. */
  7873. intel_crtc_update_dpms(&crtc->base);
  7874. if (crtc->active != crtc->base.enabled) {
  7875. struct intel_encoder *encoder;
  7876. /* This can happen either due to bugs in the get_hw_state
  7877. * functions or because the pipe is force-enabled due to the
  7878. * pipe A quirk. */
  7879. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  7880. crtc->base.base.id,
  7881. crtc->base.enabled ? "enabled" : "disabled",
  7882. crtc->active ? "enabled" : "disabled");
  7883. crtc->base.enabled = crtc->active;
  7884. /* Because we only establish the connector -> encoder ->
  7885. * crtc links if something is active, this means the
  7886. * crtc is now deactivated. Break the links. connector
  7887. * -> encoder links are only establish when things are
  7888. * actually up, hence no need to break them. */
  7889. WARN_ON(crtc->active);
  7890. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  7891. WARN_ON(encoder->connectors_active);
  7892. encoder->base.crtc = NULL;
  7893. }
  7894. }
  7895. }
  7896. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  7897. {
  7898. struct intel_connector *connector;
  7899. struct drm_device *dev = encoder->base.dev;
  7900. /* We need to check both for a crtc link (meaning that the
  7901. * encoder is active and trying to read from a pipe) and the
  7902. * pipe itself being active. */
  7903. bool has_active_crtc = encoder->base.crtc &&
  7904. to_intel_crtc(encoder->base.crtc)->active;
  7905. if (encoder->connectors_active && !has_active_crtc) {
  7906. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  7907. encoder->base.base.id,
  7908. drm_get_encoder_name(&encoder->base));
  7909. /* Connector is active, but has no active pipe. This is
  7910. * fallout from our resume register restoring. Disable
  7911. * the encoder manually again. */
  7912. if (encoder->base.crtc) {
  7913. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  7914. encoder->base.base.id,
  7915. drm_get_encoder_name(&encoder->base));
  7916. encoder->disable(encoder);
  7917. }
  7918. /* Inconsistent output/port/pipe state happens presumably due to
  7919. * a bug in one of the get_hw_state functions. Or someplace else
  7920. * in our code, like the register restore mess on resume. Clamp
  7921. * things to off as a safer default. */
  7922. list_for_each_entry(connector,
  7923. &dev->mode_config.connector_list,
  7924. base.head) {
  7925. if (connector->encoder != encoder)
  7926. continue;
  7927. intel_connector_break_all_links(connector);
  7928. }
  7929. }
  7930. /* Enabled encoders without active connectors will be fixed in
  7931. * the crtc fixup. */
  7932. }
  7933. void i915_redisable_vga(struct drm_device *dev)
  7934. {
  7935. struct drm_i915_private *dev_priv = dev->dev_private;
  7936. u32 vga_reg = i915_vgacntrl_reg(dev);
  7937. if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
  7938. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  7939. i915_disable_vga(dev);
  7940. }
  7941. }
  7942. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  7943. * and i915 state tracking structures. */
  7944. void intel_modeset_setup_hw_state(struct drm_device *dev,
  7945. bool force_restore)
  7946. {
  7947. struct drm_i915_private *dev_priv = dev->dev_private;
  7948. enum pipe pipe;
  7949. struct drm_plane *plane;
  7950. struct intel_crtc *crtc;
  7951. struct intel_encoder *encoder;
  7952. struct intel_connector *connector;
  7953. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  7954. base.head) {
  7955. memset(&crtc->config, 0, sizeof(crtc->config));
  7956. crtc->active = dev_priv->display.get_pipe_config(crtc,
  7957. &crtc->config);
  7958. crtc->base.enabled = crtc->active;
  7959. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  7960. crtc->base.base.id,
  7961. crtc->active ? "enabled" : "disabled");
  7962. }
  7963. if (HAS_DDI(dev))
  7964. intel_ddi_setup_hw_pll_state(dev);
  7965. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7966. base.head) {
  7967. pipe = 0;
  7968. if (encoder->get_hw_state(encoder, &pipe)) {
  7969. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  7970. encoder->base.crtc = &crtc->base;
  7971. if (encoder->get_config)
  7972. encoder->get_config(encoder, &crtc->config);
  7973. } else {
  7974. encoder->base.crtc = NULL;
  7975. }
  7976. encoder->connectors_active = false;
  7977. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
  7978. encoder->base.base.id,
  7979. drm_get_encoder_name(&encoder->base),
  7980. encoder->base.crtc ? "enabled" : "disabled",
  7981. pipe);
  7982. }
  7983. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7984. base.head) {
  7985. if (connector->get_hw_state(connector)) {
  7986. connector->base.dpms = DRM_MODE_DPMS_ON;
  7987. connector->encoder->connectors_active = true;
  7988. connector->base.encoder = &connector->encoder->base;
  7989. } else {
  7990. connector->base.dpms = DRM_MODE_DPMS_OFF;
  7991. connector->base.encoder = NULL;
  7992. }
  7993. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  7994. connector->base.base.id,
  7995. drm_get_connector_name(&connector->base),
  7996. connector->base.encoder ? "enabled" : "disabled");
  7997. }
  7998. /* HW state is read out, now we need to sanitize this mess. */
  7999. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8000. base.head) {
  8001. intel_sanitize_encoder(encoder);
  8002. }
  8003. for_each_pipe(pipe) {
  8004. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  8005. intel_sanitize_crtc(crtc);
  8006. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  8007. }
  8008. if (force_restore) {
  8009. /*
  8010. * We need to use raw interfaces for restoring state to avoid
  8011. * checking (bogus) intermediate states.
  8012. */
  8013. for_each_pipe(pipe) {
  8014. struct drm_crtc *crtc =
  8015. dev_priv->pipe_to_crtc_mapping[pipe];
  8016. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  8017. crtc->fb);
  8018. }
  8019. list_for_each_entry(plane, &dev->mode_config.plane_list, head)
  8020. intel_plane_restore(plane);
  8021. i915_redisable_vga(dev);
  8022. } else {
  8023. intel_modeset_update_staged_output_state(dev);
  8024. }
  8025. intel_modeset_check_state(dev);
  8026. drm_mode_config_reset(dev);
  8027. }
  8028. void intel_modeset_gem_init(struct drm_device *dev)
  8029. {
  8030. intel_modeset_init_hw(dev);
  8031. intel_setup_overlay(dev);
  8032. intel_modeset_setup_hw_state(dev, false);
  8033. }
  8034. void intel_modeset_cleanup(struct drm_device *dev)
  8035. {
  8036. struct drm_i915_private *dev_priv = dev->dev_private;
  8037. struct drm_crtc *crtc;
  8038. struct intel_crtc *intel_crtc;
  8039. /*
  8040. * Interrupts and polling as the first thing to avoid creating havoc.
  8041. * Too much stuff here (turning of rps, connectors, ...) would
  8042. * experience fancy races otherwise.
  8043. */
  8044. drm_irq_uninstall(dev);
  8045. cancel_work_sync(&dev_priv->hotplug_work);
  8046. /*
  8047. * Due to the hpd irq storm handling the hotplug work can re-arm the
  8048. * poll handlers. Hence disable polling after hpd handling is shut down.
  8049. */
  8050. drm_kms_helper_poll_fini(dev);
  8051. mutex_lock(&dev->struct_mutex);
  8052. intel_unregister_dsm_handler();
  8053. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  8054. /* Skip inactive CRTCs */
  8055. if (!crtc->fb)
  8056. continue;
  8057. intel_crtc = to_intel_crtc(crtc);
  8058. intel_increase_pllclock(crtc);
  8059. }
  8060. intel_disable_fbc(dev);
  8061. intel_disable_gt_powersave(dev);
  8062. ironlake_teardown_rc6(dev);
  8063. mutex_unlock(&dev->struct_mutex);
  8064. /* flush any delayed tasks or pending work */
  8065. flush_scheduled_work();
  8066. /* destroy backlight, if any, before the connectors */
  8067. intel_panel_destroy_backlight(dev);
  8068. drm_mode_config_cleanup(dev);
  8069. intel_cleanup_overlay(dev);
  8070. }
  8071. /*
  8072. * Return which encoder is currently attached for connector.
  8073. */
  8074. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  8075. {
  8076. return &intel_attached_encoder(connector)->base;
  8077. }
  8078. void intel_connector_attach_encoder(struct intel_connector *connector,
  8079. struct intel_encoder *encoder)
  8080. {
  8081. connector->encoder = encoder;
  8082. drm_mode_connector_attach_encoder(&connector->base,
  8083. &encoder->base);
  8084. }
  8085. /*
  8086. * set vga decode state - true == enable VGA decode
  8087. */
  8088. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  8089. {
  8090. struct drm_i915_private *dev_priv = dev->dev_private;
  8091. u16 gmch_ctrl;
  8092. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  8093. if (state)
  8094. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  8095. else
  8096. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  8097. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  8098. return 0;
  8099. }
  8100. #ifdef CONFIG_DEBUG_FS
  8101. #include <linux/seq_file.h>
  8102. struct intel_display_error_state {
  8103. u32 power_well_driver;
  8104. struct intel_cursor_error_state {
  8105. u32 control;
  8106. u32 position;
  8107. u32 base;
  8108. u32 size;
  8109. } cursor[I915_MAX_PIPES];
  8110. struct intel_pipe_error_state {
  8111. enum transcoder cpu_transcoder;
  8112. u32 conf;
  8113. u32 source;
  8114. u32 htotal;
  8115. u32 hblank;
  8116. u32 hsync;
  8117. u32 vtotal;
  8118. u32 vblank;
  8119. u32 vsync;
  8120. } pipe[I915_MAX_PIPES];
  8121. struct intel_plane_error_state {
  8122. u32 control;
  8123. u32 stride;
  8124. u32 size;
  8125. u32 pos;
  8126. u32 addr;
  8127. u32 surface;
  8128. u32 tile_offset;
  8129. } plane[I915_MAX_PIPES];
  8130. };
  8131. struct intel_display_error_state *
  8132. intel_display_capture_error_state(struct drm_device *dev)
  8133. {
  8134. drm_i915_private_t *dev_priv = dev->dev_private;
  8135. struct intel_display_error_state *error;
  8136. enum transcoder cpu_transcoder;
  8137. int i;
  8138. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  8139. if (error == NULL)
  8140. return NULL;
  8141. if (HAS_POWER_WELL(dev))
  8142. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  8143. for_each_pipe(i) {
  8144. cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
  8145. error->pipe[i].cpu_transcoder = cpu_transcoder;
  8146. if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
  8147. error->cursor[i].control = I915_READ(CURCNTR(i));
  8148. error->cursor[i].position = I915_READ(CURPOS(i));
  8149. error->cursor[i].base = I915_READ(CURBASE(i));
  8150. } else {
  8151. error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
  8152. error->cursor[i].position = I915_READ(CURPOS_IVB(i));
  8153. error->cursor[i].base = I915_READ(CURBASE_IVB(i));
  8154. }
  8155. error->plane[i].control = I915_READ(DSPCNTR(i));
  8156. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  8157. if (INTEL_INFO(dev)->gen <= 3) {
  8158. error->plane[i].size = I915_READ(DSPSIZE(i));
  8159. error->plane[i].pos = I915_READ(DSPPOS(i));
  8160. }
  8161. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  8162. error->plane[i].addr = I915_READ(DSPADDR(i));
  8163. if (INTEL_INFO(dev)->gen >= 4) {
  8164. error->plane[i].surface = I915_READ(DSPSURF(i));
  8165. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  8166. }
  8167. error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  8168. error->pipe[i].source = I915_READ(PIPESRC(i));
  8169. error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  8170. error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  8171. error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  8172. error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  8173. error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  8174. error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  8175. }
  8176. /* In the code above we read the registers without checking if the power
  8177. * well was on, so here we have to clear the FPGA_DBG_RM_NOCLAIM bit to
  8178. * prevent the next I915_WRITE from detecting it and printing an error
  8179. * message. */
  8180. if (HAS_POWER_WELL(dev))
  8181. I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
  8182. return error;
  8183. }
  8184. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  8185. void
  8186. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  8187. struct drm_device *dev,
  8188. struct intel_display_error_state *error)
  8189. {
  8190. int i;
  8191. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  8192. if (HAS_POWER_WELL(dev))
  8193. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  8194. error->power_well_driver);
  8195. for_each_pipe(i) {
  8196. err_printf(m, "Pipe [%d]:\n", i);
  8197. err_printf(m, " CPU transcoder: %c\n",
  8198. transcoder_name(error->pipe[i].cpu_transcoder));
  8199. err_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  8200. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  8201. err_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  8202. err_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  8203. err_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  8204. err_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  8205. err_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  8206. err_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  8207. err_printf(m, "Plane [%d]:\n", i);
  8208. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  8209. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  8210. if (INTEL_INFO(dev)->gen <= 3) {
  8211. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  8212. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  8213. }
  8214. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  8215. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  8216. if (INTEL_INFO(dev)->gen >= 4) {
  8217. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  8218. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  8219. }
  8220. err_printf(m, "Cursor [%d]:\n", i);
  8221. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  8222. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  8223. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  8224. }
  8225. }
  8226. #endif