intel_display.c 249 KB

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