intel_display.c 244 KB

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