intel_display.c 253 KB

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