intel_display.c 182 KB

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