intel_display.c 240 KB

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