intel_display.c 197 KB

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