intel_display.c 195 KB

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