intel_display.c 239 KB

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