intel_display.c 190 KB

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