intel_display.c 189 KB

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