drm_crtc.c 164 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. * Copyright (c) 2008 Red Hat Inc.
  5. *
  6. * DRM core CRTC related functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Keith Packard
  28. * Eric Anholt <eric@anholt.net>
  29. * Dave Airlie <airlied@linux.ie>
  30. * Jesse Barnes <jesse.barnes@intel.com>
  31. */
  32. #include <linux/ctype.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_edid.h>
  39. #include <drm/drm_fourcc.h>
  40. #include <drm/drm_modeset_lock.h>
  41. #include <drm/drm_atomic.h>
  42. #include "drm_crtc_internal.h"
  43. #include "drm_internal.h"
  44. static struct drm_framebuffer *
  45. internal_framebuffer_create(struct drm_device *dev,
  46. const struct drm_mode_fb_cmd2 *r,
  47. struct drm_file *file_priv);
  48. /* Avoid boilerplate. I'm tired of typing. */
  49. #define DRM_ENUM_NAME_FN(fnname, list) \
  50. const char *fnname(int val) \
  51. { \
  52. int i; \
  53. for (i = 0; i < ARRAY_SIZE(list); i++) { \
  54. if (list[i].type == val) \
  55. return list[i].name; \
  56. } \
  57. return "(unknown)"; \
  58. }
  59. /*
  60. * Global properties
  61. */
  62. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  63. { DRM_MODE_DPMS_ON, "On" },
  64. { DRM_MODE_DPMS_STANDBY, "Standby" },
  65. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  66. { DRM_MODE_DPMS_OFF, "Off" }
  67. };
  68. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  69. static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
  70. { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  71. { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  72. { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  73. };
  74. /*
  75. * Optional properties
  76. */
  77. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  78. { DRM_MODE_SCALE_NONE, "None" },
  79. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  80. { DRM_MODE_SCALE_CENTER, "Center" },
  81. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  82. };
  83. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  84. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  85. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  86. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  87. };
  88. /*
  89. * Non-global properties, but "required" for certain connectors.
  90. */
  91. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  92. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  93. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  94. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  95. };
  96. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  97. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  98. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  99. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  100. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  101. };
  102. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  103. drm_dvi_i_subconnector_enum_list)
  104. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  105. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  106. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  107. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  108. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  109. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  110. };
  111. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  112. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  113. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  114. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  115. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  116. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  117. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  118. };
  119. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  120. drm_tv_subconnector_enum_list)
  121. static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  122. { DRM_MODE_DIRTY_OFF, "Off" },
  123. { DRM_MODE_DIRTY_ON, "On" },
  124. { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  125. };
  126. struct drm_conn_prop_enum_list {
  127. int type;
  128. const char *name;
  129. struct ida ida;
  130. };
  131. /*
  132. * Connector and encoder types.
  133. */
  134. static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
  135. { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  136. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  137. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  138. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  139. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  140. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  141. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  142. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  143. { DRM_MODE_CONNECTOR_Component, "Component" },
  144. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  145. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  146. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  147. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  148. { DRM_MODE_CONNECTOR_TV, "TV" },
  149. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  150. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  151. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  152. };
  153. static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
  154. { DRM_MODE_ENCODER_NONE, "None" },
  155. { DRM_MODE_ENCODER_DAC, "DAC" },
  156. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  157. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  158. { DRM_MODE_ENCODER_TVDAC, "TV" },
  159. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  160. { DRM_MODE_ENCODER_DSI, "DSI" },
  161. { DRM_MODE_ENCODER_DPMST, "DP MST" },
  162. };
  163. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  164. { SubPixelUnknown, "Unknown" },
  165. { SubPixelHorizontalRGB, "Horizontal RGB" },
  166. { SubPixelHorizontalBGR, "Horizontal BGR" },
  167. { SubPixelVerticalRGB, "Vertical RGB" },
  168. { SubPixelVerticalBGR, "Vertical BGR" },
  169. { SubPixelNone, "None" },
  170. };
  171. void drm_connector_ida_init(void)
  172. {
  173. int i;
  174. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  175. ida_init(&drm_connector_enum_list[i].ida);
  176. }
  177. void drm_connector_ida_destroy(void)
  178. {
  179. int i;
  180. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  181. ida_destroy(&drm_connector_enum_list[i].ida);
  182. }
  183. /**
  184. * drm_get_connector_status_name - return a string for connector status
  185. * @status: connector status to compute name of
  186. *
  187. * In contrast to the other drm_get_*_name functions this one here returns a
  188. * const pointer and hence is threadsafe.
  189. */
  190. const char *drm_get_connector_status_name(enum drm_connector_status status)
  191. {
  192. if (status == connector_status_connected)
  193. return "connected";
  194. else if (status == connector_status_disconnected)
  195. return "disconnected";
  196. else
  197. return "unknown";
  198. }
  199. EXPORT_SYMBOL(drm_get_connector_status_name);
  200. /**
  201. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  202. * @order: enum of subpixel_order
  203. *
  204. * Note you could abuse this and return something out of bounds, but that
  205. * would be a caller error. No unscrubbed user data should make it here.
  206. */
  207. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  208. {
  209. return drm_subpixel_enum_list[order].name;
  210. }
  211. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  212. static char printable_char(int c)
  213. {
  214. return isascii(c) && isprint(c) ? c : '?';
  215. }
  216. /**
  217. * drm_get_format_name - return a string for drm fourcc format
  218. * @format: format to compute name of
  219. *
  220. * Note that the buffer used by this function is globally shared and owned by
  221. * the function itself.
  222. *
  223. * FIXME: This isn't really multithreading safe.
  224. */
  225. const char *drm_get_format_name(uint32_t format)
  226. {
  227. static char buf[32];
  228. snprintf(buf, sizeof(buf),
  229. "%c%c%c%c %s-endian (0x%08x)",
  230. printable_char(format & 0xff),
  231. printable_char((format >> 8) & 0xff),
  232. printable_char((format >> 16) & 0xff),
  233. printable_char((format >> 24) & 0x7f),
  234. format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
  235. format);
  236. return buf;
  237. }
  238. EXPORT_SYMBOL(drm_get_format_name);
  239. /*
  240. * Internal function to assign a slot in the object idr and optionally
  241. * register the object into the idr.
  242. */
  243. static int drm_mode_object_get_reg(struct drm_device *dev,
  244. struct drm_mode_object *obj,
  245. uint32_t obj_type,
  246. bool register_obj)
  247. {
  248. int ret;
  249. mutex_lock(&dev->mode_config.idr_mutex);
  250. ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
  251. if (ret >= 0) {
  252. /*
  253. * Set up the object linking under the protection of the idr
  254. * lock so that other users can't see inconsistent state.
  255. */
  256. obj->id = ret;
  257. obj->type = obj_type;
  258. }
  259. mutex_unlock(&dev->mode_config.idr_mutex);
  260. return ret < 0 ? ret : 0;
  261. }
  262. /**
  263. * drm_mode_object_get - allocate a new modeset identifier
  264. * @dev: DRM device
  265. * @obj: object pointer, used to generate unique ID
  266. * @obj_type: object type
  267. *
  268. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  269. * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
  270. * modeset identifiers are _not_ reference counted. Hence don't use this for
  271. * reference counted modeset objects like framebuffers.
  272. *
  273. * Returns:
  274. * Zero on success, error code on failure.
  275. */
  276. int drm_mode_object_get(struct drm_device *dev,
  277. struct drm_mode_object *obj, uint32_t obj_type)
  278. {
  279. return drm_mode_object_get_reg(dev, obj, obj_type, true);
  280. }
  281. static void drm_mode_object_register(struct drm_device *dev,
  282. struct drm_mode_object *obj)
  283. {
  284. mutex_lock(&dev->mode_config.idr_mutex);
  285. idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
  286. mutex_unlock(&dev->mode_config.idr_mutex);
  287. }
  288. /**
  289. * drm_mode_object_put - free a modeset identifer
  290. * @dev: DRM device
  291. * @object: object to free
  292. *
  293. * Free @id from @dev's unique identifier pool. Note that despite the _get
  294. * postfix modeset identifiers are _not_ reference counted. Hence don't use this
  295. * for reference counted modeset objects like framebuffers.
  296. */
  297. void drm_mode_object_put(struct drm_device *dev,
  298. struct drm_mode_object *object)
  299. {
  300. mutex_lock(&dev->mode_config.idr_mutex);
  301. idr_remove(&dev->mode_config.crtc_idr, object->id);
  302. mutex_unlock(&dev->mode_config.idr_mutex);
  303. }
  304. static struct drm_mode_object *_object_find(struct drm_device *dev,
  305. uint32_t id, uint32_t type)
  306. {
  307. struct drm_mode_object *obj = NULL;
  308. mutex_lock(&dev->mode_config.idr_mutex);
  309. obj = idr_find(&dev->mode_config.crtc_idr, id);
  310. if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  311. obj = NULL;
  312. if (obj && obj->id != id)
  313. obj = NULL;
  314. /* don't leak out unref'd fb's */
  315. if (obj &&
  316. (obj->type == DRM_MODE_OBJECT_FB ||
  317. obj->type == DRM_MODE_OBJECT_BLOB))
  318. obj = NULL;
  319. mutex_unlock(&dev->mode_config.idr_mutex);
  320. return obj;
  321. }
  322. /**
  323. * drm_mode_object_find - look up a drm object with static lifetime
  324. * @dev: drm device
  325. * @id: id of the mode object
  326. * @type: type of the mode object
  327. *
  328. * Note that framebuffers cannot be looked up with this functions - since those
  329. * are reference counted, they need special treatment. Even with
  330. * DRM_MODE_OBJECT_ANY (although that will simply return NULL
  331. * rather than WARN_ON()).
  332. */
  333. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  334. uint32_t id, uint32_t type)
  335. {
  336. struct drm_mode_object *obj = NULL;
  337. /* Framebuffers are reference counted and need their own lookup
  338. * function.*/
  339. WARN_ON(type == DRM_MODE_OBJECT_FB || type == DRM_MODE_OBJECT_BLOB);
  340. obj = _object_find(dev, id, type);
  341. return obj;
  342. }
  343. EXPORT_SYMBOL(drm_mode_object_find);
  344. /**
  345. * drm_framebuffer_init - initialize a framebuffer
  346. * @dev: DRM device
  347. * @fb: framebuffer to be initialized
  348. * @funcs: ... with these functions
  349. *
  350. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  351. * functions & device file and adds it to the master fd list.
  352. *
  353. * IMPORTANT:
  354. * This functions publishes the fb and makes it available for concurrent access
  355. * by other users. Which means by this point the fb _must_ be fully set up -
  356. * since all the fb attributes are invariant over its lifetime, no further
  357. * locking but only correct reference counting is required.
  358. *
  359. * Returns:
  360. * Zero on success, error code on failure.
  361. */
  362. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  363. const struct drm_framebuffer_funcs *funcs)
  364. {
  365. int ret;
  366. mutex_lock(&dev->mode_config.fb_lock);
  367. kref_init(&fb->refcount);
  368. INIT_LIST_HEAD(&fb->filp_head);
  369. fb->dev = dev;
  370. fb->funcs = funcs;
  371. ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  372. if (ret)
  373. goto out;
  374. dev->mode_config.num_fb++;
  375. list_add(&fb->head, &dev->mode_config.fb_list);
  376. out:
  377. mutex_unlock(&dev->mode_config.fb_lock);
  378. return ret;
  379. }
  380. EXPORT_SYMBOL(drm_framebuffer_init);
  381. /* dev->mode_config.fb_lock must be held! */
  382. static void __drm_framebuffer_unregister(struct drm_device *dev,
  383. struct drm_framebuffer *fb)
  384. {
  385. drm_mode_object_put(dev, &fb->base);
  386. fb->base.id = 0;
  387. }
  388. static void drm_framebuffer_free(struct kref *kref)
  389. {
  390. struct drm_framebuffer *fb =
  391. container_of(kref, struct drm_framebuffer, refcount);
  392. struct drm_device *dev = fb->dev;
  393. /*
  394. * The lookup idr holds a weak reference, which has not necessarily been
  395. * removed at this point. Check for that.
  396. */
  397. mutex_lock(&dev->mode_config.fb_lock);
  398. if (fb->base.id) {
  399. /* Mark fb as reaped and drop idr ref. */
  400. __drm_framebuffer_unregister(dev, fb);
  401. }
  402. mutex_unlock(&dev->mode_config.fb_lock);
  403. fb->funcs->destroy(fb);
  404. }
  405. static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
  406. uint32_t id)
  407. {
  408. struct drm_mode_object *obj = NULL;
  409. struct drm_framebuffer *fb;
  410. mutex_lock(&dev->mode_config.idr_mutex);
  411. obj = idr_find(&dev->mode_config.crtc_idr, id);
  412. if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
  413. fb = NULL;
  414. else
  415. fb = obj_to_fb(obj);
  416. mutex_unlock(&dev->mode_config.idr_mutex);
  417. return fb;
  418. }
  419. /**
  420. * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  421. * @dev: drm device
  422. * @id: id of the fb object
  423. *
  424. * If successful, this grabs an additional reference to the framebuffer -
  425. * callers need to make sure to eventually unreference the returned framebuffer
  426. * again, using @drm_framebuffer_unreference.
  427. */
  428. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  429. uint32_t id)
  430. {
  431. struct drm_framebuffer *fb;
  432. mutex_lock(&dev->mode_config.fb_lock);
  433. fb = __drm_framebuffer_lookup(dev, id);
  434. if (fb) {
  435. if (!kref_get_unless_zero(&fb->refcount))
  436. fb = NULL;
  437. }
  438. mutex_unlock(&dev->mode_config.fb_lock);
  439. return fb;
  440. }
  441. EXPORT_SYMBOL(drm_framebuffer_lookup);
  442. /**
  443. * drm_framebuffer_unreference - unref a framebuffer
  444. * @fb: framebuffer to unref
  445. *
  446. * This functions decrements the fb's refcount and frees it if it drops to zero.
  447. */
  448. void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  449. {
  450. DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  451. kref_put(&fb->refcount, drm_framebuffer_free);
  452. }
  453. EXPORT_SYMBOL(drm_framebuffer_unreference);
  454. /**
  455. * drm_framebuffer_reference - incr the fb refcnt
  456. * @fb: framebuffer
  457. *
  458. * This functions increments the fb's refcount.
  459. */
  460. void drm_framebuffer_reference(struct drm_framebuffer *fb)
  461. {
  462. DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  463. kref_get(&fb->refcount);
  464. }
  465. EXPORT_SYMBOL(drm_framebuffer_reference);
  466. /**
  467. * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  468. * @fb: fb to unregister
  469. *
  470. * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  471. * those used for fbdev. Note that the caller must hold a reference of it's own,
  472. * i.e. the object may not be destroyed through this call (since it'll lead to a
  473. * locking inversion).
  474. */
  475. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  476. {
  477. struct drm_device *dev;
  478. if (!fb)
  479. return;
  480. dev = fb->dev;
  481. mutex_lock(&dev->mode_config.fb_lock);
  482. /* Mark fb as reaped and drop idr ref. */
  483. __drm_framebuffer_unregister(dev, fb);
  484. mutex_unlock(&dev->mode_config.fb_lock);
  485. }
  486. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  487. /**
  488. * drm_framebuffer_cleanup - remove a framebuffer object
  489. * @fb: framebuffer to remove
  490. *
  491. * Cleanup framebuffer. This function is intended to be used from the drivers
  492. * ->destroy callback. It can also be used to clean up driver private
  493. * framebuffers embedded into a larger structure.
  494. *
  495. * Note that this function does not remove the fb from active usuage - if it is
  496. * still used anywhere, hilarity can ensue since userspace could call getfb on
  497. * the id and get back -EINVAL. Obviously no concern at driver unload time.
  498. *
  499. * Also, the framebuffer will not be removed from the lookup idr - for
  500. * user-created framebuffers this will happen in in the rmfb ioctl. For
  501. * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  502. * drm_framebuffer_unregister_private.
  503. */
  504. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  505. {
  506. struct drm_device *dev = fb->dev;
  507. mutex_lock(&dev->mode_config.fb_lock);
  508. list_del(&fb->head);
  509. dev->mode_config.num_fb--;
  510. mutex_unlock(&dev->mode_config.fb_lock);
  511. }
  512. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  513. /**
  514. * drm_framebuffer_remove - remove and unreference a framebuffer object
  515. * @fb: framebuffer to remove
  516. *
  517. * Scans all the CRTCs and planes in @dev's mode_config. If they're
  518. * using @fb, removes it, setting it to NULL. Then drops the reference to the
  519. * passed-in framebuffer. Might take the modeset locks.
  520. *
  521. * Note that this function optimizes the cleanup away if the caller holds the
  522. * last reference to the framebuffer. It is also guaranteed to not take the
  523. * modeset locks in this case.
  524. */
  525. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  526. {
  527. struct drm_device *dev;
  528. struct drm_crtc *crtc;
  529. struct drm_plane *plane;
  530. struct drm_mode_set set;
  531. int ret;
  532. if (!fb)
  533. return;
  534. dev = fb->dev;
  535. WARN_ON(!list_empty(&fb->filp_head));
  536. /*
  537. * drm ABI mandates that we remove any deleted framebuffers from active
  538. * useage. But since most sane clients only remove framebuffers they no
  539. * longer need, try to optimize this away.
  540. *
  541. * Since we're holding a reference ourselves, observing a refcount of 1
  542. * means that we're the last holder and can skip it. Also, the refcount
  543. * can never increase from 1 again, so we don't need any barriers or
  544. * locks.
  545. *
  546. * Note that userspace could try to race with use and instate a new
  547. * usage _after_ we've cleared all current ones. End result will be an
  548. * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  549. * in this manner.
  550. */
  551. if (atomic_read(&fb->refcount.refcount) > 1) {
  552. drm_modeset_lock_all(dev);
  553. /* remove from any CRTC */
  554. drm_for_each_crtc(crtc, dev) {
  555. if (crtc->primary->fb == fb) {
  556. /* should turn off the crtc */
  557. memset(&set, 0, sizeof(struct drm_mode_set));
  558. set.crtc = crtc;
  559. set.fb = NULL;
  560. ret = drm_mode_set_config_internal(&set);
  561. if (ret)
  562. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  563. }
  564. }
  565. drm_for_each_plane(plane, dev) {
  566. if (plane->fb == fb)
  567. drm_plane_force_disable(plane);
  568. }
  569. drm_modeset_unlock_all(dev);
  570. }
  571. drm_framebuffer_unreference(fb);
  572. }
  573. EXPORT_SYMBOL(drm_framebuffer_remove);
  574. DEFINE_WW_CLASS(crtc_ww_class);
  575. static unsigned int drm_num_crtcs(struct drm_device *dev)
  576. {
  577. unsigned int num = 0;
  578. struct drm_crtc *tmp;
  579. drm_for_each_crtc(tmp, dev) {
  580. num++;
  581. }
  582. return num;
  583. }
  584. /**
  585. * drm_crtc_init_with_planes - Initialise a new CRTC object with
  586. * specified primary and cursor planes.
  587. * @dev: DRM device
  588. * @crtc: CRTC object to init
  589. * @primary: Primary plane for CRTC
  590. * @cursor: Cursor plane for CRTC
  591. * @funcs: callbacks for the new CRTC
  592. * @name: printf style format string for the CRTC name, or NULL for default name
  593. *
  594. * Inits a new object created as base part of a driver crtc object.
  595. *
  596. * Returns:
  597. * Zero on success, error code on failure.
  598. */
  599. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  600. struct drm_plane *primary,
  601. struct drm_plane *cursor,
  602. const struct drm_crtc_funcs *funcs,
  603. const char *name, ...)
  604. {
  605. struct drm_mode_config *config = &dev->mode_config;
  606. int ret;
  607. WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
  608. WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
  609. crtc->dev = dev;
  610. crtc->funcs = funcs;
  611. drm_modeset_lock_init(&crtc->mutex);
  612. ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  613. if (ret)
  614. return ret;
  615. if (name) {
  616. va_list ap;
  617. va_start(ap, name);
  618. crtc->name = kvasprintf(GFP_KERNEL, name, ap);
  619. va_end(ap);
  620. } else {
  621. crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
  622. drm_num_crtcs(dev));
  623. }
  624. if (!crtc->name) {
  625. drm_mode_object_put(dev, &crtc->base);
  626. return -ENOMEM;
  627. }
  628. crtc->base.properties = &crtc->properties;
  629. list_add_tail(&crtc->head, &config->crtc_list);
  630. config->num_crtc++;
  631. crtc->primary = primary;
  632. crtc->cursor = cursor;
  633. if (primary)
  634. primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  635. if (cursor)
  636. cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
  637. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  638. drm_object_attach_property(&crtc->base, config->prop_active, 0);
  639. drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
  640. }
  641. return 0;
  642. }
  643. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  644. /**
  645. * drm_crtc_cleanup - Clean up the core crtc usage
  646. * @crtc: CRTC to cleanup
  647. *
  648. * This function cleans up @crtc and removes it from the DRM mode setting
  649. * core. Note that the function does *not* free the crtc structure itself,
  650. * this is the responsibility of the caller.
  651. */
  652. void drm_crtc_cleanup(struct drm_crtc *crtc)
  653. {
  654. struct drm_device *dev = crtc->dev;
  655. kfree(crtc->gamma_store);
  656. crtc->gamma_store = NULL;
  657. drm_modeset_lock_fini(&crtc->mutex);
  658. drm_mode_object_put(dev, &crtc->base);
  659. list_del(&crtc->head);
  660. dev->mode_config.num_crtc--;
  661. WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  662. if (crtc->state && crtc->funcs->atomic_destroy_state)
  663. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  664. kfree(crtc->name);
  665. memset(crtc, 0, sizeof(*crtc));
  666. }
  667. EXPORT_SYMBOL(drm_crtc_cleanup);
  668. /**
  669. * drm_crtc_index - find the index of a registered CRTC
  670. * @crtc: CRTC to find index for
  671. *
  672. * Given a registered CRTC, return the index of that CRTC within a DRM
  673. * device's list of CRTCs.
  674. */
  675. unsigned int drm_crtc_index(struct drm_crtc *crtc)
  676. {
  677. unsigned int index = 0;
  678. struct drm_crtc *tmp;
  679. drm_for_each_crtc(tmp, crtc->dev) {
  680. if (tmp == crtc)
  681. return index;
  682. index++;
  683. }
  684. BUG();
  685. }
  686. EXPORT_SYMBOL(drm_crtc_index);
  687. /*
  688. * drm_mode_remove - remove and free a mode
  689. * @connector: connector list to modify
  690. * @mode: mode to remove
  691. *
  692. * Remove @mode from @connector's mode list, then free it.
  693. */
  694. static void drm_mode_remove(struct drm_connector *connector,
  695. struct drm_display_mode *mode)
  696. {
  697. list_del(&mode->head);
  698. drm_mode_destroy(connector->dev, mode);
  699. }
  700. /**
  701. * drm_display_info_set_bus_formats - set the supported bus formats
  702. * @info: display info to store bus formats in
  703. * @formats: array containing the supported bus formats
  704. * @num_formats: the number of entries in the fmts array
  705. *
  706. * Store the supported bus formats in display info structure.
  707. * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  708. * a full list of available formats.
  709. */
  710. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  711. const u32 *formats,
  712. unsigned int num_formats)
  713. {
  714. u32 *fmts = NULL;
  715. if (!formats && num_formats)
  716. return -EINVAL;
  717. if (formats && num_formats) {
  718. fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  719. GFP_KERNEL);
  720. if (!fmts)
  721. return -ENOMEM;
  722. }
  723. kfree(info->bus_formats);
  724. info->bus_formats = fmts;
  725. info->num_bus_formats = num_formats;
  726. return 0;
  727. }
  728. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  729. /**
  730. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  731. * @connector: connector to quwery
  732. *
  733. * The kernel supports per-connector configration of its consoles through
  734. * use of the video= parameter. This function parses that option and
  735. * extracts the user's specified mode (or enable/disable status) for a
  736. * particular connector. This is typically only used during the early fbdev
  737. * setup.
  738. */
  739. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  740. {
  741. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  742. char *option = NULL;
  743. if (fb_get_options(connector->name, &option))
  744. return;
  745. if (!drm_mode_parse_command_line_for_connector(option,
  746. connector,
  747. mode))
  748. return;
  749. if (mode->force) {
  750. const char *s;
  751. switch (mode->force) {
  752. case DRM_FORCE_OFF:
  753. s = "OFF";
  754. break;
  755. case DRM_FORCE_ON_DIGITAL:
  756. s = "ON - dig";
  757. break;
  758. default:
  759. case DRM_FORCE_ON:
  760. s = "ON";
  761. break;
  762. }
  763. DRM_INFO("forcing %s connector %s\n", connector->name, s);
  764. connector->force = mode->force;
  765. }
  766. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  767. connector->name,
  768. mode->xres, mode->yres,
  769. mode->refresh_specified ? mode->refresh : 60,
  770. mode->rb ? " reduced blanking" : "",
  771. mode->margins ? " with margins" : "",
  772. mode->interlace ? " interlaced" : "");
  773. }
  774. /**
  775. * drm_connector_init - Init a preallocated connector
  776. * @dev: DRM device
  777. * @connector: the connector to init
  778. * @funcs: callbacks for this connector
  779. * @connector_type: user visible type of the connector
  780. *
  781. * Initialises a preallocated connector. Connectors should be
  782. * subclassed as part of driver connector objects.
  783. *
  784. * Returns:
  785. * Zero on success, error code on failure.
  786. */
  787. int drm_connector_init(struct drm_device *dev,
  788. struct drm_connector *connector,
  789. const struct drm_connector_funcs *funcs,
  790. int connector_type)
  791. {
  792. struct drm_mode_config *config = &dev->mode_config;
  793. int ret;
  794. struct ida *connector_ida =
  795. &drm_connector_enum_list[connector_type].ida;
  796. drm_modeset_lock_all(dev);
  797. ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
  798. if (ret)
  799. goto out_unlock;
  800. connector->base.properties = &connector->properties;
  801. connector->dev = dev;
  802. connector->funcs = funcs;
  803. connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
  804. if (connector->connector_id < 0) {
  805. ret = connector->connector_id;
  806. goto out_put;
  807. }
  808. connector->connector_type = connector_type;
  809. connector->connector_type_id =
  810. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  811. if (connector->connector_type_id < 0) {
  812. ret = connector->connector_type_id;
  813. goto out_put_id;
  814. }
  815. connector->name =
  816. kasprintf(GFP_KERNEL, "%s-%d",
  817. drm_connector_enum_list[connector_type].name,
  818. connector->connector_type_id);
  819. if (!connector->name) {
  820. ret = -ENOMEM;
  821. goto out_put_type_id;
  822. }
  823. INIT_LIST_HEAD(&connector->probed_modes);
  824. INIT_LIST_HEAD(&connector->modes);
  825. connector->edid_blob_ptr = NULL;
  826. connector->status = connector_status_unknown;
  827. drm_connector_get_cmdline_mode(connector);
  828. /* We should add connectors at the end to avoid upsetting the connector
  829. * index too much. */
  830. list_add_tail(&connector->head, &config->connector_list);
  831. config->num_connector++;
  832. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  833. drm_object_attach_property(&connector->base,
  834. config->edid_property,
  835. 0);
  836. drm_object_attach_property(&connector->base,
  837. config->dpms_property, 0);
  838. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  839. drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  840. }
  841. connector->debugfs_entry = NULL;
  842. out_put_type_id:
  843. if (ret)
  844. ida_remove(connector_ida, connector->connector_type_id);
  845. out_put_id:
  846. if (ret)
  847. ida_remove(&config->connector_ida, connector->connector_id);
  848. out_put:
  849. if (ret)
  850. drm_mode_object_put(dev, &connector->base);
  851. out_unlock:
  852. drm_modeset_unlock_all(dev);
  853. return ret;
  854. }
  855. EXPORT_SYMBOL(drm_connector_init);
  856. /**
  857. * drm_connector_cleanup - cleans up an initialised connector
  858. * @connector: connector to cleanup
  859. *
  860. * Cleans up the connector but doesn't free the object.
  861. */
  862. void drm_connector_cleanup(struct drm_connector *connector)
  863. {
  864. struct drm_device *dev = connector->dev;
  865. struct drm_display_mode *mode, *t;
  866. if (connector->tile_group) {
  867. drm_mode_put_tile_group(dev, connector->tile_group);
  868. connector->tile_group = NULL;
  869. }
  870. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  871. drm_mode_remove(connector, mode);
  872. list_for_each_entry_safe(mode, t, &connector->modes, head)
  873. drm_mode_remove(connector, mode);
  874. ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
  875. connector->connector_type_id);
  876. ida_remove(&dev->mode_config.connector_ida,
  877. connector->connector_id);
  878. kfree(connector->display_info.bus_formats);
  879. drm_mode_object_put(dev, &connector->base);
  880. kfree(connector->name);
  881. connector->name = NULL;
  882. list_del(&connector->head);
  883. dev->mode_config.num_connector--;
  884. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  885. if (connector->state && connector->funcs->atomic_destroy_state)
  886. connector->funcs->atomic_destroy_state(connector,
  887. connector->state);
  888. memset(connector, 0, sizeof(*connector));
  889. }
  890. EXPORT_SYMBOL(drm_connector_cleanup);
  891. /**
  892. * drm_connector_register - register a connector
  893. * @connector: the connector to register
  894. *
  895. * Register userspace interfaces for a connector
  896. *
  897. * Returns:
  898. * Zero on success, error code on failure.
  899. */
  900. int drm_connector_register(struct drm_connector *connector)
  901. {
  902. int ret;
  903. drm_mode_object_register(connector->dev, &connector->base);
  904. ret = drm_sysfs_connector_add(connector);
  905. if (ret)
  906. return ret;
  907. ret = drm_debugfs_connector_add(connector);
  908. if (ret) {
  909. drm_sysfs_connector_remove(connector);
  910. return ret;
  911. }
  912. return 0;
  913. }
  914. EXPORT_SYMBOL(drm_connector_register);
  915. /**
  916. * drm_connector_unregister - unregister a connector
  917. * @connector: the connector to unregister
  918. *
  919. * Unregister userspace interfaces for a connector
  920. */
  921. void drm_connector_unregister(struct drm_connector *connector)
  922. {
  923. drm_sysfs_connector_remove(connector);
  924. drm_debugfs_connector_remove(connector);
  925. }
  926. EXPORT_SYMBOL(drm_connector_unregister);
  927. /**
  928. * drm_connector_register_all - register all connectors
  929. * @dev: drm device
  930. *
  931. * This function registers all connectors in sysfs and other places so that
  932. * userspace can start to access them. Drivers can call it after calling
  933. * drm_dev_register() to complete the device registration, if they don't call
  934. * drm_connector_register() on each connector individually.
  935. *
  936. * When a device is unplugged and should be removed from userspace access,
  937. * call drm_connector_unregister_all(), which is the inverse of this
  938. * function.
  939. *
  940. * Returns:
  941. * Zero on success, error code on failure.
  942. */
  943. int drm_connector_register_all(struct drm_device *dev)
  944. {
  945. struct drm_connector *connector;
  946. int ret;
  947. mutex_lock(&dev->mode_config.mutex);
  948. drm_for_each_connector(connector, dev) {
  949. ret = drm_connector_register(connector);
  950. if (ret)
  951. goto err;
  952. }
  953. mutex_unlock(&dev->mode_config.mutex);
  954. return 0;
  955. err:
  956. mutex_unlock(&dev->mode_config.mutex);
  957. drm_connector_unregister_all(dev);
  958. return ret;
  959. }
  960. EXPORT_SYMBOL(drm_connector_register_all);
  961. /**
  962. * drm_connector_unregister_all - unregister connector userspace interfaces
  963. * @dev: drm device
  964. *
  965. * This functions unregisters all connectors from sysfs and other places so
  966. * that userspace can no longer access them. Drivers should call this as the
  967. * first step tearing down the device instace, or when the underlying
  968. * physical device disappeared (e.g. USB unplug), right before calling
  969. * drm_dev_unregister().
  970. */
  971. void drm_connector_unregister_all(struct drm_device *dev)
  972. {
  973. struct drm_connector *connector;
  974. /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
  975. drm_for_each_connector(connector, dev)
  976. drm_connector_unregister(connector);
  977. }
  978. EXPORT_SYMBOL(drm_connector_unregister_all);
  979. /**
  980. * drm_encoder_init - Init a preallocated encoder
  981. * @dev: drm device
  982. * @encoder: the encoder to init
  983. * @funcs: callbacks for this encoder
  984. * @encoder_type: user visible type of the encoder
  985. * @name: printf style format string for the encoder name, or NULL for default name
  986. *
  987. * Initialises a preallocated encoder. Encoder should be
  988. * subclassed as part of driver encoder objects.
  989. *
  990. * Returns:
  991. * Zero on success, error code on failure.
  992. */
  993. int drm_encoder_init(struct drm_device *dev,
  994. struct drm_encoder *encoder,
  995. const struct drm_encoder_funcs *funcs,
  996. int encoder_type, const char *name, ...)
  997. {
  998. int ret;
  999. drm_modeset_lock_all(dev);
  1000. ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  1001. if (ret)
  1002. goto out_unlock;
  1003. encoder->dev = dev;
  1004. encoder->encoder_type = encoder_type;
  1005. encoder->funcs = funcs;
  1006. if (name) {
  1007. va_list ap;
  1008. va_start(ap, name);
  1009. encoder->name = kvasprintf(GFP_KERNEL, name, ap);
  1010. va_end(ap);
  1011. } else {
  1012. encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
  1013. drm_encoder_enum_list[encoder_type].name,
  1014. encoder->base.id);
  1015. }
  1016. if (!encoder->name) {
  1017. ret = -ENOMEM;
  1018. goto out_put;
  1019. }
  1020. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  1021. dev->mode_config.num_encoder++;
  1022. out_put:
  1023. if (ret)
  1024. drm_mode_object_put(dev, &encoder->base);
  1025. out_unlock:
  1026. drm_modeset_unlock_all(dev);
  1027. return ret;
  1028. }
  1029. EXPORT_SYMBOL(drm_encoder_init);
  1030. /**
  1031. * drm_encoder_index - find the index of a registered encoder
  1032. * @encoder: encoder to find index for
  1033. *
  1034. * Given a registered encoder, return the index of that encoder within a DRM
  1035. * device's list of encoders.
  1036. */
  1037. unsigned int drm_encoder_index(struct drm_encoder *encoder)
  1038. {
  1039. unsigned int index = 0;
  1040. struct drm_encoder *tmp;
  1041. drm_for_each_encoder(tmp, encoder->dev) {
  1042. if (tmp == encoder)
  1043. return index;
  1044. index++;
  1045. }
  1046. BUG();
  1047. }
  1048. EXPORT_SYMBOL(drm_encoder_index);
  1049. /**
  1050. * drm_encoder_cleanup - cleans up an initialised encoder
  1051. * @encoder: encoder to cleanup
  1052. *
  1053. * Cleans up the encoder but doesn't free the object.
  1054. */
  1055. void drm_encoder_cleanup(struct drm_encoder *encoder)
  1056. {
  1057. struct drm_device *dev = encoder->dev;
  1058. drm_modeset_lock_all(dev);
  1059. drm_mode_object_put(dev, &encoder->base);
  1060. kfree(encoder->name);
  1061. list_del(&encoder->head);
  1062. dev->mode_config.num_encoder--;
  1063. drm_modeset_unlock_all(dev);
  1064. memset(encoder, 0, sizeof(*encoder));
  1065. }
  1066. EXPORT_SYMBOL(drm_encoder_cleanup);
  1067. static unsigned int drm_num_planes(struct drm_device *dev)
  1068. {
  1069. unsigned int num = 0;
  1070. struct drm_plane *tmp;
  1071. drm_for_each_plane(tmp, dev) {
  1072. num++;
  1073. }
  1074. return num;
  1075. }
  1076. /**
  1077. * drm_universal_plane_init - Initialize a new universal plane object
  1078. * @dev: DRM device
  1079. * @plane: plane object to init
  1080. * @possible_crtcs: bitmask of possible CRTCs
  1081. * @funcs: callbacks for the new plane
  1082. * @formats: array of supported formats (%DRM_FORMAT_*)
  1083. * @format_count: number of elements in @formats
  1084. * @type: type of plane (overlay, primary, cursor)
  1085. * @name: printf style format string for the plane name, or NULL for default name
  1086. *
  1087. * Initializes a plane object of type @type.
  1088. *
  1089. * Returns:
  1090. * Zero on success, error code on failure.
  1091. */
  1092. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1093. unsigned long possible_crtcs,
  1094. const struct drm_plane_funcs *funcs,
  1095. const uint32_t *formats, unsigned int format_count,
  1096. enum drm_plane_type type,
  1097. const char *name, ...)
  1098. {
  1099. struct drm_mode_config *config = &dev->mode_config;
  1100. int ret;
  1101. ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  1102. if (ret)
  1103. return ret;
  1104. drm_modeset_lock_init(&plane->mutex);
  1105. plane->base.properties = &plane->properties;
  1106. plane->dev = dev;
  1107. plane->funcs = funcs;
  1108. plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
  1109. GFP_KERNEL);
  1110. if (!plane->format_types) {
  1111. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  1112. drm_mode_object_put(dev, &plane->base);
  1113. return -ENOMEM;
  1114. }
  1115. if (name) {
  1116. va_list ap;
  1117. va_start(ap, name);
  1118. plane->name = kvasprintf(GFP_KERNEL, name, ap);
  1119. va_end(ap);
  1120. } else {
  1121. plane->name = kasprintf(GFP_KERNEL, "plane-%d",
  1122. drm_num_planes(dev));
  1123. }
  1124. if (!plane->name) {
  1125. kfree(plane->format_types);
  1126. drm_mode_object_put(dev, &plane->base);
  1127. return -ENOMEM;
  1128. }
  1129. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  1130. plane->format_count = format_count;
  1131. plane->possible_crtcs = possible_crtcs;
  1132. plane->type = type;
  1133. list_add_tail(&plane->head, &config->plane_list);
  1134. config->num_total_plane++;
  1135. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1136. config->num_overlay_plane++;
  1137. drm_object_attach_property(&plane->base,
  1138. config->plane_type_property,
  1139. plane->type);
  1140. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  1141. drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  1142. drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  1143. drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  1144. drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  1145. drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  1146. drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  1147. drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  1148. drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  1149. drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  1150. drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  1151. }
  1152. return 0;
  1153. }
  1154. EXPORT_SYMBOL(drm_universal_plane_init);
  1155. /**
  1156. * drm_plane_init - Initialize a legacy plane
  1157. * @dev: DRM device
  1158. * @plane: plane object to init
  1159. * @possible_crtcs: bitmask of possible CRTCs
  1160. * @funcs: callbacks for the new plane
  1161. * @formats: array of supported formats (%DRM_FORMAT_*)
  1162. * @format_count: number of elements in @formats
  1163. * @is_primary: plane type (primary vs overlay)
  1164. *
  1165. * Legacy API to initialize a DRM plane.
  1166. *
  1167. * New drivers should call drm_universal_plane_init() instead.
  1168. *
  1169. * Returns:
  1170. * Zero on success, error code on failure.
  1171. */
  1172. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1173. unsigned long possible_crtcs,
  1174. const struct drm_plane_funcs *funcs,
  1175. const uint32_t *formats, unsigned int format_count,
  1176. bool is_primary)
  1177. {
  1178. enum drm_plane_type type;
  1179. type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  1180. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  1181. formats, format_count, type, NULL);
  1182. }
  1183. EXPORT_SYMBOL(drm_plane_init);
  1184. /**
  1185. * drm_plane_cleanup - Clean up the core plane usage
  1186. * @plane: plane to cleanup
  1187. *
  1188. * This function cleans up @plane and removes it from the DRM mode setting
  1189. * core. Note that the function does *not* free the plane structure itself,
  1190. * this is the responsibility of the caller.
  1191. */
  1192. void drm_plane_cleanup(struct drm_plane *plane)
  1193. {
  1194. struct drm_device *dev = plane->dev;
  1195. drm_modeset_lock_all(dev);
  1196. kfree(plane->format_types);
  1197. drm_mode_object_put(dev, &plane->base);
  1198. BUG_ON(list_empty(&plane->head));
  1199. list_del(&plane->head);
  1200. dev->mode_config.num_total_plane--;
  1201. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1202. dev->mode_config.num_overlay_plane--;
  1203. drm_modeset_unlock_all(dev);
  1204. WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  1205. if (plane->state && plane->funcs->atomic_destroy_state)
  1206. plane->funcs->atomic_destroy_state(plane, plane->state);
  1207. kfree(plane->name);
  1208. memset(plane, 0, sizeof(*plane));
  1209. }
  1210. EXPORT_SYMBOL(drm_plane_cleanup);
  1211. /**
  1212. * drm_plane_index - find the index of a registered plane
  1213. * @plane: plane to find index for
  1214. *
  1215. * Given a registered plane, return the index of that CRTC within a DRM
  1216. * device's list of planes.
  1217. */
  1218. unsigned int drm_plane_index(struct drm_plane *plane)
  1219. {
  1220. unsigned int index = 0;
  1221. struct drm_plane *tmp;
  1222. drm_for_each_plane(tmp, plane->dev) {
  1223. if (tmp == plane)
  1224. return index;
  1225. index++;
  1226. }
  1227. BUG();
  1228. }
  1229. EXPORT_SYMBOL(drm_plane_index);
  1230. /**
  1231. * drm_plane_from_index - find the registered plane at an index
  1232. * @dev: DRM device
  1233. * @idx: index of registered plane to find for
  1234. *
  1235. * Given a plane index, return the registered plane from DRM device's
  1236. * list of planes with matching index.
  1237. */
  1238. struct drm_plane *
  1239. drm_plane_from_index(struct drm_device *dev, int idx)
  1240. {
  1241. struct drm_plane *plane;
  1242. unsigned int i = 0;
  1243. drm_for_each_plane(plane, dev) {
  1244. if (i == idx)
  1245. return plane;
  1246. i++;
  1247. }
  1248. return NULL;
  1249. }
  1250. EXPORT_SYMBOL(drm_plane_from_index);
  1251. /**
  1252. * drm_plane_force_disable - Forcibly disable a plane
  1253. * @plane: plane to disable
  1254. *
  1255. * Forces the plane to be disabled.
  1256. *
  1257. * Used when the plane's current framebuffer is destroyed,
  1258. * and when restoring fbdev mode.
  1259. */
  1260. void drm_plane_force_disable(struct drm_plane *plane)
  1261. {
  1262. int ret;
  1263. if (!plane->fb)
  1264. return;
  1265. plane->old_fb = plane->fb;
  1266. ret = plane->funcs->disable_plane(plane);
  1267. if (ret) {
  1268. DRM_ERROR("failed to disable plane with busy fb\n");
  1269. plane->old_fb = NULL;
  1270. return;
  1271. }
  1272. /* disconnect the plane from the fb and crtc: */
  1273. drm_framebuffer_unreference(plane->old_fb);
  1274. plane->old_fb = NULL;
  1275. plane->fb = NULL;
  1276. plane->crtc = NULL;
  1277. }
  1278. EXPORT_SYMBOL(drm_plane_force_disable);
  1279. static int drm_mode_create_standard_properties(struct drm_device *dev)
  1280. {
  1281. struct drm_property *prop;
  1282. /*
  1283. * Standard properties (apply to all connectors)
  1284. */
  1285. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  1286. DRM_MODE_PROP_IMMUTABLE,
  1287. "EDID", 0);
  1288. if (!prop)
  1289. return -ENOMEM;
  1290. dev->mode_config.edid_property = prop;
  1291. prop = drm_property_create_enum(dev, 0,
  1292. "DPMS", drm_dpms_enum_list,
  1293. ARRAY_SIZE(drm_dpms_enum_list));
  1294. if (!prop)
  1295. return -ENOMEM;
  1296. dev->mode_config.dpms_property = prop;
  1297. prop = drm_property_create(dev,
  1298. DRM_MODE_PROP_BLOB |
  1299. DRM_MODE_PROP_IMMUTABLE,
  1300. "PATH", 0);
  1301. if (!prop)
  1302. return -ENOMEM;
  1303. dev->mode_config.path_property = prop;
  1304. prop = drm_property_create(dev,
  1305. DRM_MODE_PROP_BLOB |
  1306. DRM_MODE_PROP_IMMUTABLE,
  1307. "TILE", 0);
  1308. if (!prop)
  1309. return -ENOMEM;
  1310. dev->mode_config.tile_property = prop;
  1311. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1312. "type", drm_plane_type_enum_list,
  1313. ARRAY_SIZE(drm_plane_type_enum_list));
  1314. if (!prop)
  1315. return -ENOMEM;
  1316. dev->mode_config.plane_type_property = prop;
  1317. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1318. "SRC_X", 0, UINT_MAX);
  1319. if (!prop)
  1320. return -ENOMEM;
  1321. dev->mode_config.prop_src_x = prop;
  1322. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1323. "SRC_Y", 0, UINT_MAX);
  1324. if (!prop)
  1325. return -ENOMEM;
  1326. dev->mode_config.prop_src_y = prop;
  1327. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1328. "SRC_W", 0, UINT_MAX);
  1329. if (!prop)
  1330. return -ENOMEM;
  1331. dev->mode_config.prop_src_w = prop;
  1332. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1333. "SRC_H", 0, UINT_MAX);
  1334. if (!prop)
  1335. return -ENOMEM;
  1336. dev->mode_config.prop_src_h = prop;
  1337. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1338. "CRTC_X", INT_MIN, INT_MAX);
  1339. if (!prop)
  1340. return -ENOMEM;
  1341. dev->mode_config.prop_crtc_x = prop;
  1342. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1343. "CRTC_Y", INT_MIN, INT_MAX);
  1344. if (!prop)
  1345. return -ENOMEM;
  1346. dev->mode_config.prop_crtc_y = prop;
  1347. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1348. "CRTC_W", 0, INT_MAX);
  1349. if (!prop)
  1350. return -ENOMEM;
  1351. dev->mode_config.prop_crtc_w = prop;
  1352. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1353. "CRTC_H", 0, INT_MAX);
  1354. if (!prop)
  1355. return -ENOMEM;
  1356. dev->mode_config.prop_crtc_h = prop;
  1357. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1358. "FB_ID", DRM_MODE_OBJECT_FB);
  1359. if (!prop)
  1360. return -ENOMEM;
  1361. dev->mode_config.prop_fb_id = prop;
  1362. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1363. "CRTC_ID", DRM_MODE_OBJECT_CRTC);
  1364. if (!prop)
  1365. return -ENOMEM;
  1366. dev->mode_config.prop_crtc_id = prop;
  1367. prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
  1368. "ACTIVE");
  1369. if (!prop)
  1370. return -ENOMEM;
  1371. dev->mode_config.prop_active = prop;
  1372. prop = drm_property_create(dev,
  1373. DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
  1374. "MODE_ID", 0);
  1375. if (!prop)
  1376. return -ENOMEM;
  1377. dev->mode_config.prop_mode_id = prop;
  1378. prop = drm_property_create(dev,
  1379. DRM_MODE_PROP_BLOB,
  1380. "DEGAMMA_LUT", 0);
  1381. if (!prop)
  1382. return -ENOMEM;
  1383. dev->mode_config.degamma_lut_property = prop;
  1384. prop = drm_property_create_range(dev,
  1385. DRM_MODE_PROP_IMMUTABLE,
  1386. "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
  1387. if (!prop)
  1388. return -ENOMEM;
  1389. dev->mode_config.degamma_lut_size_property = prop;
  1390. prop = drm_property_create(dev,
  1391. DRM_MODE_PROP_BLOB,
  1392. "CTM", 0);
  1393. if (!prop)
  1394. return -ENOMEM;
  1395. dev->mode_config.ctm_property = prop;
  1396. prop = drm_property_create(dev,
  1397. DRM_MODE_PROP_BLOB,
  1398. "GAMMA_LUT", 0);
  1399. if (!prop)
  1400. return -ENOMEM;
  1401. dev->mode_config.gamma_lut_property = prop;
  1402. prop = drm_property_create_range(dev,
  1403. DRM_MODE_PROP_IMMUTABLE,
  1404. "GAMMA_LUT_SIZE", 0, UINT_MAX);
  1405. if (!prop)
  1406. return -ENOMEM;
  1407. dev->mode_config.gamma_lut_size_property = prop;
  1408. return 0;
  1409. }
  1410. /**
  1411. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  1412. * @dev: DRM device
  1413. *
  1414. * Called by a driver the first time a DVI-I connector is made.
  1415. */
  1416. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  1417. {
  1418. struct drm_property *dvi_i_selector;
  1419. struct drm_property *dvi_i_subconnector;
  1420. if (dev->mode_config.dvi_i_select_subconnector_property)
  1421. return 0;
  1422. dvi_i_selector =
  1423. drm_property_create_enum(dev, 0,
  1424. "select subconnector",
  1425. drm_dvi_i_select_enum_list,
  1426. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  1427. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  1428. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1429. "subconnector",
  1430. drm_dvi_i_subconnector_enum_list,
  1431. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  1432. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  1433. return 0;
  1434. }
  1435. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  1436. /**
  1437. * drm_create_tv_properties - create TV specific connector properties
  1438. * @dev: DRM device
  1439. * @num_modes: number of different TV formats (modes) supported
  1440. * @modes: array of pointers to strings containing name of each format
  1441. *
  1442. * Called by a driver's TV initialization routine, this function creates
  1443. * the TV specific connector properties for a given device. Caller is
  1444. * responsible for allocating a list of format names and passing them to
  1445. * this routine.
  1446. */
  1447. int drm_mode_create_tv_properties(struct drm_device *dev,
  1448. unsigned int num_modes,
  1449. const char * const modes[])
  1450. {
  1451. struct drm_property *tv_selector;
  1452. struct drm_property *tv_subconnector;
  1453. unsigned int i;
  1454. if (dev->mode_config.tv_select_subconnector_property)
  1455. return 0;
  1456. /*
  1457. * Basic connector properties
  1458. */
  1459. tv_selector = drm_property_create_enum(dev, 0,
  1460. "select subconnector",
  1461. drm_tv_select_enum_list,
  1462. ARRAY_SIZE(drm_tv_select_enum_list));
  1463. if (!tv_selector)
  1464. goto nomem;
  1465. dev->mode_config.tv_select_subconnector_property = tv_selector;
  1466. tv_subconnector =
  1467. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1468. "subconnector",
  1469. drm_tv_subconnector_enum_list,
  1470. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1471. if (!tv_subconnector)
  1472. goto nomem;
  1473. dev->mode_config.tv_subconnector_property = tv_subconnector;
  1474. /*
  1475. * Other, TV specific properties: margins & TV modes.
  1476. */
  1477. dev->mode_config.tv_left_margin_property =
  1478. drm_property_create_range(dev, 0, "left margin", 0, 100);
  1479. if (!dev->mode_config.tv_left_margin_property)
  1480. goto nomem;
  1481. dev->mode_config.tv_right_margin_property =
  1482. drm_property_create_range(dev, 0, "right margin", 0, 100);
  1483. if (!dev->mode_config.tv_right_margin_property)
  1484. goto nomem;
  1485. dev->mode_config.tv_top_margin_property =
  1486. drm_property_create_range(dev, 0, "top margin", 0, 100);
  1487. if (!dev->mode_config.tv_top_margin_property)
  1488. goto nomem;
  1489. dev->mode_config.tv_bottom_margin_property =
  1490. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1491. if (!dev->mode_config.tv_bottom_margin_property)
  1492. goto nomem;
  1493. dev->mode_config.tv_mode_property =
  1494. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1495. "mode", num_modes);
  1496. if (!dev->mode_config.tv_mode_property)
  1497. goto nomem;
  1498. for (i = 0; i < num_modes; i++)
  1499. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  1500. i, modes[i]);
  1501. dev->mode_config.tv_brightness_property =
  1502. drm_property_create_range(dev, 0, "brightness", 0, 100);
  1503. if (!dev->mode_config.tv_brightness_property)
  1504. goto nomem;
  1505. dev->mode_config.tv_contrast_property =
  1506. drm_property_create_range(dev, 0, "contrast", 0, 100);
  1507. if (!dev->mode_config.tv_contrast_property)
  1508. goto nomem;
  1509. dev->mode_config.tv_flicker_reduction_property =
  1510. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1511. if (!dev->mode_config.tv_flicker_reduction_property)
  1512. goto nomem;
  1513. dev->mode_config.tv_overscan_property =
  1514. drm_property_create_range(dev, 0, "overscan", 0, 100);
  1515. if (!dev->mode_config.tv_overscan_property)
  1516. goto nomem;
  1517. dev->mode_config.tv_saturation_property =
  1518. drm_property_create_range(dev, 0, "saturation", 0, 100);
  1519. if (!dev->mode_config.tv_saturation_property)
  1520. goto nomem;
  1521. dev->mode_config.tv_hue_property =
  1522. drm_property_create_range(dev, 0, "hue", 0, 100);
  1523. if (!dev->mode_config.tv_hue_property)
  1524. goto nomem;
  1525. return 0;
  1526. nomem:
  1527. return -ENOMEM;
  1528. }
  1529. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1530. /**
  1531. * drm_mode_create_scaling_mode_property - create scaling mode property
  1532. * @dev: DRM device
  1533. *
  1534. * Called by a driver the first time it's needed, must be attached to desired
  1535. * connectors.
  1536. */
  1537. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1538. {
  1539. struct drm_property *scaling_mode;
  1540. if (dev->mode_config.scaling_mode_property)
  1541. return 0;
  1542. scaling_mode =
  1543. drm_property_create_enum(dev, 0, "scaling mode",
  1544. drm_scaling_mode_enum_list,
  1545. ARRAY_SIZE(drm_scaling_mode_enum_list));
  1546. dev->mode_config.scaling_mode_property = scaling_mode;
  1547. return 0;
  1548. }
  1549. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1550. /**
  1551. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1552. * @dev: DRM device
  1553. *
  1554. * Called by a driver the first time it's needed, must be attached to desired
  1555. * connectors.
  1556. *
  1557. * Returns:
  1558. * Zero on success, negative errno on failure.
  1559. */
  1560. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1561. {
  1562. if (dev->mode_config.aspect_ratio_property)
  1563. return 0;
  1564. dev->mode_config.aspect_ratio_property =
  1565. drm_property_create_enum(dev, 0, "aspect ratio",
  1566. drm_aspect_ratio_enum_list,
  1567. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1568. if (dev->mode_config.aspect_ratio_property == NULL)
  1569. return -ENOMEM;
  1570. return 0;
  1571. }
  1572. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1573. /**
  1574. * drm_mode_create_dirty_property - create dirty property
  1575. * @dev: DRM device
  1576. *
  1577. * Called by a driver the first time it's needed, must be attached to desired
  1578. * connectors.
  1579. */
  1580. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  1581. {
  1582. struct drm_property *dirty_info;
  1583. if (dev->mode_config.dirty_info_property)
  1584. return 0;
  1585. dirty_info =
  1586. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1587. "dirty",
  1588. drm_dirty_info_enum_list,
  1589. ARRAY_SIZE(drm_dirty_info_enum_list));
  1590. dev->mode_config.dirty_info_property = dirty_info;
  1591. return 0;
  1592. }
  1593. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  1594. /**
  1595. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  1596. * @dev: DRM device
  1597. *
  1598. * Create the the suggested x/y offset property for connectors.
  1599. */
  1600. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  1601. {
  1602. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  1603. return 0;
  1604. dev->mode_config.suggested_x_property =
  1605. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  1606. dev->mode_config.suggested_y_property =
  1607. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  1608. if (dev->mode_config.suggested_x_property == NULL ||
  1609. dev->mode_config.suggested_y_property == NULL)
  1610. return -ENOMEM;
  1611. return 0;
  1612. }
  1613. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  1614. /**
  1615. * drm_mode_getresources - get graphics configuration
  1616. * @dev: drm device for the ioctl
  1617. * @data: data pointer for the ioctl
  1618. * @file_priv: drm file for the ioctl call
  1619. *
  1620. * Construct a set of configuration description structures and return
  1621. * them to the user, including CRTC, connector and framebuffer configuration.
  1622. *
  1623. * Called by the user via ioctl.
  1624. *
  1625. * Returns:
  1626. * Zero on success, negative errno on failure.
  1627. */
  1628. int drm_mode_getresources(struct drm_device *dev, void *data,
  1629. struct drm_file *file_priv)
  1630. {
  1631. struct drm_mode_card_res *card_res = data;
  1632. struct list_head *lh;
  1633. struct drm_framebuffer *fb;
  1634. struct drm_connector *connector;
  1635. struct drm_crtc *crtc;
  1636. struct drm_encoder *encoder;
  1637. int ret = 0;
  1638. int connector_count = 0;
  1639. int crtc_count = 0;
  1640. int fb_count = 0;
  1641. int encoder_count = 0;
  1642. int copied = 0;
  1643. uint32_t __user *fb_id;
  1644. uint32_t __user *crtc_id;
  1645. uint32_t __user *connector_id;
  1646. uint32_t __user *encoder_id;
  1647. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1648. return -EINVAL;
  1649. mutex_lock(&file_priv->fbs_lock);
  1650. /*
  1651. * For the non-control nodes we need to limit the list of resources
  1652. * by IDs in the group list for this node
  1653. */
  1654. list_for_each(lh, &file_priv->fbs)
  1655. fb_count++;
  1656. /* handle this in 4 parts */
  1657. /* FBs */
  1658. if (card_res->count_fbs >= fb_count) {
  1659. copied = 0;
  1660. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1661. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1662. if (put_user(fb->base.id, fb_id + copied)) {
  1663. mutex_unlock(&file_priv->fbs_lock);
  1664. return -EFAULT;
  1665. }
  1666. copied++;
  1667. }
  1668. }
  1669. card_res->count_fbs = fb_count;
  1670. mutex_unlock(&file_priv->fbs_lock);
  1671. /* mode_config.mutex protects the connector list against e.g. DP MST
  1672. * connector hot-adding. CRTC/Plane lists are invariant. */
  1673. mutex_lock(&dev->mode_config.mutex);
  1674. drm_for_each_crtc(crtc, dev)
  1675. crtc_count++;
  1676. drm_for_each_connector(connector, dev)
  1677. connector_count++;
  1678. drm_for_each_encoder(encoder, dev)
  1679. encoder_count++;
  1680. card_res->max_height = dev->mode_config.max_height;
  1681. card_res->min_height = dev->mode_config.min_height;
  1682. card_res->max_width = dev->mode_config.max_width;
  1683. card_res->min_width = dev->mode_config.min_width;
  1684. /* CRTCs */
  1685. if (card_res->count_crtcs >= crtc_count) {
  1686. copied = 0;
  1687. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1688. drm_for_each_crtc(crtc, dev) {
  1689. DRM_DEBUG_KMS("[CRTC:%d:%s]\n",
  1690. crtc->base.id, crtc->name);
  1691. if (put_user(crtc->base.id, crtc_id + copied)) {
  1692. ret = -EFAULT;
  1693. goto out;
  1694. }
  1695. copied++;
  1696. }
  1697. }
  1698. card_res->count_crtcs = crtc_count;
  1699. /* Encoders */
  1700. if (card_res->count_encoders >= encoder_count) {
  1701. copied = 0;
  1702. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1703. drm_for_each_encoder(encoder, dev) {
  1704. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1705. encoder->name);
  1706. if (put_user(encoder->base.id, encoder_id +
  1707. copied)) {
  1708. ret = -EFAULT;
  1709. goto out;
  1710. }
  1711. copied++;
  1712. }
  1713. }
  1714. card_res->count_encoders = encoder_count;
  1715. /* Connectors */
  1716. if (card_res->count_connectors >= connector_count) {
  1717. copied = 0;
  1718. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1719. drm_for_each_connector(connector, dev) {
  1720. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1721. connector->base.id,
  1722. connector->name);
  1723. if (put_user(connector->base.id,
  1724. connector_id + copied)) {
  1725. ret = -EFAULT;
  1726. goto out;
  1727. }
  1728. copied++;
  1729. }
  1730. }
  1731. card_res->count_connectors = connector_count;
  1732. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1733. card_res->count_connectors, card_res->count_encoders);
  1734. out:
  1735. mutex_unlock(&dev->mode_config.mutex);
  1736. return ret;
  1737. }
  1738. /**
  1739. * drm_mode_getcrtc - get CRTC configuration
  1740. * @dev: drm device for the ioctl
  1741. * @data: data pointer for the ioctl
  1742. * @file_priv: drm file for the ioctl call
  1743. *
  1744. * Construct a CRTC configuration structure to return to the user.
  1745. *
  1746. * Called by the user via ioctl.
  1747. *
  1748. * Returns:
  1749. * Zero on success, negative errno on failure.
  1750. */
  1751. int drm_mode_getcrtc(struct drm_device *dev,
  1752. void *data, struct drm_file *file_priv)
  1753. {
  1754. struct drm_mode_crtc *crtc_resp = data;
  1755. struct drm_crtc *crtc;
  1756. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1757. return -EINVAL;
  1758. crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  1759. if (!crtc)
  1760. return -ENOENT;
  1761. drm_modeset_lock_crtc(crtc, crtc->primary);
  1762. crtc_resp->gamma_size = crtc->gamma_size;
  1763. if (crtc->primary->fb)
  1764. crtc_resp->fb_id = crtc->primary->fb->base.id;
  1765. else
  1766. crtc_resp->fb_id = 0;
  1767. if (crtc->state) {
  1768. crtc_resp->x = crtc->primary->state->src_x >> 16;
  1769. crtc_resp->y = crtc->primary->state->src_y >> 16;
  1770. if (crtc->state->enable) {
  1771. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
  1772. crtc_resp->mode_valid = 1;
  1773. } else {
  1774. crtc_resp->mode_valid = 0;
  1775. }
  1776. } else {
  1777. crtc_resp->x = crtc->x;
  1778. crtc_resp->y = crtc->y;
  1779. if (crtc->enabled) {
  1780. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1781. crtc_resp->mode_valid = 1;
  1782. } else {
  1783. crtc_resp->mode_valid = 0;
  1784. }
  1785. }
  1786. drm_modeset_unlock_crtc(crtc);
  1787. return 0;
  1788. }
  1789. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1790. const struct drm_file *file_priv)
  1791. {
  1792. /*
  1793. * If user-space hasn't configured the driver to expose the stereo 3D
  1794. * modes, don't expose them.
  1795. */
  1796. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1797. return false;
  1798. return true;
  1799. }
  1800. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1801. {
  1802. /* For atomic drivers only state objects are synchronously updated and
  1803. * protected by modeset locks, so check those first. */
  1804. if (connector->state)
  1805. return connector->state->best_encoder;
  1806. return connector->encoder;
  1807. }
  1808. /* helper for getconnector and getproperties ioctls */
  1809. static int get_properties(struct drm_mode_object *obj, bool atomic,
  1810. uint32_t __user *prop_ptr, uint64_t __user *prop_values,
  1811. uint32_t *arg_count_props)
  1812. {
  1813. int props_count;
  1814. int i, ret, copied;
  1815. props_count = obj->properties->count;
  1816. if (!atomic)
  1817. props_count -= obj->properties->atomic_count;
  1818. if ((*arg_count_props >= props_count) && props_count) {
  1819. for (i = 0, copied = 0; copied < props_count; i++) {
  1820. struct drm_property *prop = obj->properties->properties[i];
  1821. uint64_t val;
  1822. if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  1823. continue;
  1824. ret = drm_object_property_get_value(obj, prop, &val);
  1825. if (ret)
  1826. return ret;
  1827. if (put_user(prop->base.id, prop_ptr + copied))
  1828. return -EFAULT;
  1829. if (put_user(val, prop_values + copied))
  1830. return -EFAULT;
  1831. copied++;
  1832. }
  1833. }
  1834. *arg_count_props = props_count;
  1835. return 0;
  1836. }
  1837. /**
  1838. * drm_mode_getconnector - get connector configuration
  1839. * @dev: drm device for the ioctl
  1840. * @data: data pointer for the ioctl
  1841. * @file_priv: drm file for the ioctl call
  1842. *
  1843. * Construct a connector configuration structure to return to the user.
  1844. *
  1845. * Called by the user via ioctl.
  1846. *
  1847. * Returns:
  1848. * Zero on success, negative errno on failure.
  1849. */
  1850. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1851. struct drm_file *file_priv)
  1852. {
  1853. struct drm_mode_get_connector *out_resp = data;
  1854. struct drm_connector *connector;
  1855. struct drm_encoder *encoder;
  1856. struct drm_display_mode *mode;
  1857. int mode_count = 0;
  1858. int encoders_count = 0;
  1859. int ret = 0;
  1860. int copied = 0;
  1861. int i;
  1862. struct drm_mode_modeinfo u_mode;
  1863. struct drm_mode_modeinfo __user *mode_ptr;
  1864. uint32_t __user *encoder_ptr;
  1865. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1866. return -EINVAL;
  1867. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1868. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1869. mutex_lock(&dev->mode_config.mutex);
  1870. connector = drm_connector_find(dev, out_resp->connector_id);
  1871. if (!connector) {
  1872. ret = -ENOENT;
  1873. goto out_unlock;
  1874. }
  1875. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  1876. if (connector->encoder_ids[i] != 0)
  1877. encoders_count++;
  1878. if (out_resp->count_modes == 0) {
  1879. connector->funcs->fill_modes(connector,
  1880. dev->mode_config.max_width,
  1881. dev->mode_config.max_height);
  1882. }
  1883. /* delayed so we get modes regardless of pre-fill_modes state */
  1884. list_for_each_entry(mode, &connector->modes, head)
  1885. if (drm_mode_expose_to_userspace(mode, file_priv))
  1886. mode_count++;
  1887. out_resp->connector_id = connector->base.id;
  1888. out_resp->connector_type = connector->connector_type;
  1889. out_resp->connector_type_id = connector->connector_type_id;
  1890. out_resp->mm_width = connector->display_info.width_mm;
  1891. out_resp->mm_height = connector->display_info.height_mm;
  1892. out_resp->subpixel = connector->display_info.subpixel_order;
  1893. out_resp->connection = connector->status;
  1894. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1895. encoder = drm_connector_get_encoder(connector);
  1896. if (encoder)
  1897. out_resp->encoder_id = encoder->base.id;
  1898. else
  1899. out_resp->encoder_id = 0;
  1900. /*
  1901. * This ioctl is called twice, once to determine how much space is
  1902. * needed, and the 2nd time to fill it.
  1903. */
  1904. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1905. copied = 0;
  1906. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1907. list_for_each_entry(mode, &connector->modes, head) {
  1908. if (!drm_mode_expose_to_userspace(mode, file_priv))
  1909. continue;
  1910. drm_mode_convert_to_umode(&u_mode, mode);
  1911. if (copy_to_user(mode_ptr + copied,
  1912. &u_mode, sizeof(u_mode))) {
  1913. ret = -EFAULT;
  1914. goto out;
  1915. }
  1916. copied++;
  1917. }
  1918. }
  1919. out_resp->count_modes = mode_count;
  1920. ret = get_properties(&connector->base, file_priv->atomic,
  1921. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  1922. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  1923. &out_resp->count_props);
  1924. if (ret)
  1925. goto out;
  1926. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1927. copied = 0;
  1928. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1929. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1930. if (connector->encoder_ids[i] != 0) {
  1931. if (put_user(connector->encoder_ids[i],
  1932. encoder_ptr + copied)) {
  1933. ret = -EFAULT;
  1934. goto out;
  1935. }
  1936. copied++;
  1937. }
  1938. }
  1939. }
  1940. out_resp->count_encoders = encoders_count;
  1941. out:
  1942. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1943. out_unlock:
  1944. mutex_unlock(&dev->mode_config.mutex);
  1945. return ret;
  1946. }
  1947. static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
  1948. {
  1949. struct drm_connector *connector;
  1950. struct drm_device *dev = encoder->dev;
  1951. bool uses_atomic = false;
  1952. /* For atomic drivers only state objects are synchronously updated and
  1953. * protected by modeset locks, so check those first. */
  1954. drm_for_each_connector(connector, dev) {
  1955. if (!connector->state)
  1956. continue;
  1957. uses_atomic = true;
  1958. if (connector->state->best_encoder != encoder)
  1959. continue;
  1960. return connector->state->crtc;
  1961. }
  1962. /* Don't return stale data (e.g. pending async disable). */
  1963. if (uses_atomic)
  1964. return NULL;
  1965. return encoder->crtc;
  1966. }
  1967. /**
  1968. * drm_mode_getencoder - get encoder configuration
  1969. * @dev: drm device for the ioctl
  1970. * @data: data pointer for the ioctl
  1971. * @file_priv: drm file for the ioctl call
  1972. *
  1973. * Construct a encoder configuration structure to return to the user.
  1974. *
  1975. * Called by the user via ioctl.
  1976. *
  1977. * Returns:
  1978. * Zero on success, negative errno on failure.
  1979. */
  1980. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1981. struct drm_file *file_priv)
  1982. {
  1983. struct drm_mode_get_encoder *enc_resp = data;
  1984. struct drm_encoder *encoder;
  1985. struct drm_crtc *crtc;
  1986. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1987. return -EINVAL;
  1988. encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  1989. if (!encoder)
  1990. return -ENOENT;
  1991. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1992. crtc = drm_encoder_get_crtc(encoder);
  1993. if (crtc)
  1994. enc_resp->crtc_id = crtc->base.id;
  1995. else
  1996. enc_resp->crtc_id = 0;
  1997. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1998. enc_resp->encoder_type = encoder->encoder_type;
  1999. enc_resp->encoder_id = encoder->base.id;
  2000. enc_resp->possible_crtcs = encoder->possible_crtcs;
  2001. enc_resp->possible_clones = encoder->possible_clones;
  2002. return 0;
  2003. }
  2004. /**
  2005. * drm_mode_getplane_res - enumerate all plane resources
  2006. * @dev: DRM device
  2007. * @data: ioctl data
  2008. * @file_priv: DRM file info
  2009. *
  2010. * Construct a list of plane ids to return to the user.
  2011. *
  2012. * Called by the user via ioctl.
  2013. *
  2014. * Returns:
  2015. * Zero on success, negative errno on failure.
  2016. */
  2017. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  2018. struct drm_file *file_priv)
  2019. {
  2020. struct drm_mode_get_plane_res *plane_resp = data;
  2021. struct drm_mode_config *config;
  2022. struct drm_plane *plane;
  2023. uint32_t __user *plane_ptr;
  2024. int copied = 0;
  2025. unsigned num_planes;
  2026. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2027. return -EINVAL;
  2028. config = &dev->mode_config;
  2029. if (file_priv->universal_planes)
  2030. num_planes = config->num_total_plane;
  2031. else
  2032. num_planes = config->num_overlay_plane;
  2033. /*
  2034. * This ioctl is called twice, once to determine how much space is
  2035. * needed, and the 2nd time to fill it.
  2036. */
  2037. if (num_planes &&
  2038. (plane_resp->count_planes >= num_planes)) {
  2039. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  2040. /* Plane lists are invariant, no locking needed. */
  2041. drm_for_each_plane(plane, dev) {
  2042. /*
  2043. * Unless userspace set the 'universal planes'
  2044. * capability bit, only advertise overlays.
  2045. */
  2046. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  2047. !file_priv->universal_planes)
  2048. continue;
  2049. if (put_user(plane->base.id, plane_ptr + copied))
  2050. return -EFAULT;
  2051. copied++;
  2052. }
  2053. }
  2054. plane_resp->count_planes = num_planes;
  2055. return 0;
  2056. }
  2057. /**
  2058. * drm_mode_getplane - get plane configuration
  2059. * @dev: DRM device
  2060. * @data: ioctl data
  2061. * @file_priv: DRM file info
  2062. *
  2063. * Construct a plane configuration structure to return to the user.
  2064. *
  2065. * Called by the user via ioctl.
  2066. *
  2067. * Returns:
  2068. * Zero on success, negative errno on failure.
  2069. */
  2070. int drm_mode_getplane(struct drm_device *dev, void *data,
  2071. struct drm_file *file_priv)
  2072. {
  2073. struct drm_mode_get_plane *plane_resp = data;
  2074. struct drm_plane *plane;
  2075. uint32_t __user *format_ptr;
  2076. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2077. return -EINVAL;
  2078. plane = drm_plane_find(dev, plane_resp->plane_id);
  2079. if (!plane)
  2080. return -ENOENT;
  2081. drm_modeset_lock(&plane->mutex, NULL);
  2082. if (plane->crtc)
  2083. plane_resp->crtc_id = plane->crtc->base.id;
  2084. else
  2085. plane_resp->crtc_id = 0;
  2086. if (plane->fb)
  2087. plane_resp->fb_id = plane->fb->base.id;
  2088. else
  2089. plane_resp->fb_id = 0;
  2090. drm_modeset_unlock(&plane->mutex);
  2091. plane_resp->plane_id = plane->base.id;
  2092. plane_resp->possible_crtcs = plane->possible_crtcs;
  2093. plane_resp->gamma_size = 0;
  2094. /*
  2095. * This ioctl is called twice, once to determine how much space is
  2096. * needed, and the 2nd time to fill it.
  2097. */
  2098. if (plane->format_count &&
  2099. (plane_resp->count_format_types >= plane->format_count)) {
  2100. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  2101. if (copy_to_user(format_ptr,
  2102. plane->format_types,
  2103. sizeof(uint32_t) * plane->format_count)) {
  2104. return -EFAULT;
  2105. }
  2106. }
  2107. plane_resp->count_format_types = plane->format_count;
  2108. return 0;
  2109. }
  2110. /**
  2111. * drm_plane_check_pixel_format - Check if the plane supports the pixel format
  2112. * @plane: plane to check for format support
  2113. * @format: the pixel format
  2114. *
  2115. * Returns:
  2116. * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
  2117. * otherwise.
  2118. */
  2119. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  2120. {
  2121. unsigned int i;
  2122. for (i = 0; i < plane->format_count; i++) {
  2123. if (format == plane->format_types[i])
  2124. return 0;
  2125. }
  2126. return -EINVAL;
  2127. }
  2128. static int check_src_coords(uint32_t src_x, uint32_t src_y,
  2129. uint32_t src_w, uint32_t src_h,
  2130. const struct drm_framebuffer *fb)
  2131. {
  2132. unsigned int fb_width, fb_height;
  2133. fb_width = fb->width << 16;
  2134. fb_height = fb->height << 16;
  2135. /* Make sure source coordinates are inside the fb. */
  2136. if (src_w > fb_width ||
  2137. src_x > fb_width - src_w ||
  2138. src_h > fb_height ||
  2139. src_y > fb_height - src_h) {
  2140. DRM_DEBUG_KMS("Invalid source coordinates "
  2141. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  2142. src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  2143. src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  2144. src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  2145. src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
  2146. return -ENOSPC;
  2147. }
  2148. return 0;
  2149. }
  2150. /*
  2151. * setplane_internal - setplane handler for internal callers
  2152. *
  2153. * Note that we assume an extra reference has already been taken on fb. If the
  2154. * update fails, this reference will be dropped before return; if it succeeds,
  2155. * the previous framebuffer (if any) will be unreferenced instead.
  2156. *
  2157. * src_{x,y,w,h} are provided in 16.16 fixed point format
  2158. */
  2159. static int __setplane_internal(struct drm_plane *plane,
  2160. struct drm_crtc *crtc,
  2161. struct drm_framebuffer *fb,
  2162. int32_t crtc_x, int32_t crtc_y,
  2163. uint32_t crtc_w, uint32_t crtc_h,
  2164. /* src_{x,y,w,h} values are 16.16 fixed point */
  2165. uint32_t src_x, uint32_t src_y,
  2166. uint32_t src_w, uint32_t src_h)
  2167. {
  2168. int ret = 0;
  2169. /* No fb means shut it down */
  2170. if (!fb) {
  2171. plane->old_fb = plane->fb;
  2172. ret = plane->funcs->disable_plane(plane);
  2173. if (!ret) {
  2174. plane->crtc = NULL;
  2175. plane->fb = NULL;
  2176. } else {
  2177. plane->old_fb = NULL;
  2178. }
  2179. goto out;
  2180. }
  2181. /* Check whether this plane is usable on this CRTC */
  2182. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  2183. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  2184. ret = -EINVAL;
  2185. goto out;
  2186. }
  2187. /* Check whether this plane supports the fb pixel format. */
  2188. ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
  2189. if (ret) {
  2190. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2191. drm_get_format_name(fb->pixel_format));
  2192. goto out;
  2193. }
  2194. /* Give drivers some help against integer overflows */
  2195. if (crtc_w > INT_MAX ||
  2196. crtc_x > INT_MAX - (int32_t) crtc_w ||
  2197. crtc_h > INT_MAX ||
  2198. crtc_y > INT_MAX - (int32_t) crtc_h) {
  2199. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  2200. crtc_w, crtc_h, crtc_x, crtc_y);
  2201. ret = -ERANGE;
  2202. goto out;
  2203. }
  2204. ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
  2205. if (ret)
  2206. goto out;
  2207. plane->old_fb = plane->fb;
  2208. ret = plane->funcs->update_plane(plane, crtc, fb,
  2209. crtc_x, crtc_y, crtc_w, crtc_h,
  2210. src_x, src_y, src_w, src_h);
  2211. if (!ret) {
  2212. plane->crtc = crtc;
  2213. plane->fb = fb;
  2214. fb = NULL;
  2215. } else {
  2216. plane->old_fb = NULL;
  2217. }
  2218. out:
  2219. if (fb)
  2220. drm_framebuffer_unreference(fb);
  2221. if (plane->old_fb)
  2222. drm_framebuffer_unreference(plane->old_fb);
  2223. plane->old_fb = NULL;
  2224. return ret;
  2225. }
  2226. static int setplane_internal(struct drm_plane *plane,
  2227. struct drm_crtc *crtc,
  2228. struct drm_framebuffer *fb,
  2229. int32_t crtc_x, int32_t crtc_y,
  2230. uint32_t crtc_w, uint32_t crtc_h,
  2231. /* src_{x,y,w,h} values are 16.16 fixed point */
  2232. uint32_t src_x, uint32_t src_y,
  2233. uint32_t src_w, uint32_t src_h)
  2234. {
  2235. int ret;
  2236. drm_modeset_lock_all(plane->dev);
  2237. ret = __setplane_internal(plane, crtc, fb,
  2238. crtc_x, crtc_y, crtc_w, crtc_h,
  2239. src_x, src_y, src_w, src_h);
  2240. drm_modeset_unlock_all(plane->dev);
  2241. return ret;
  2242. }
  2243. /**
  2244. * drm_mode_setplane - configure a plane's configuration
  2245. * @dev: DRM device
  2246. * @data: ioctl data*
  2247. * @file_priv: DRM file info
  2248. *
  2249. * Set plane configuration, including placement, fb, scaling, and other factors.
  2250. * Or pass a NULL fb to disable (planes may be disabled without providing a
  2251. * valid crtc).
  2252. *
  2253. * Returns:
  2254. * Zero on success, negative errno on failure.
  2255. */
  2256. int drm_mode_setplane(struct drm_device *dev, void *data,
  2257. struct drm_file *file_priv)
  2258. {
  2259. struct drm_mode_set_plane *plane_req = data;
  2260. struct drm_plane *plane;
  2261. struct drm_crtc *crtc = NULL;
  2262. struct drm_framebuffer *fb = NULL;
  2263. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2264. return -EINVAL;
  2265. /*
  2266. * First, find the plane, crtc, and fb objects. If not available,
  2267. * we don't bother to call the driver.
  2268. */
  2269. plane = drm_plane_find(dev, plane_req->plane_id);
  2270. if (!plane) {
  2271. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  2272. plane_req->plane_id);
  2273. return -ENOENT;
  2274. }
  2275. if (plane_req->fb_id) {
  2276. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  2277. if (!fb) {
  2278. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  2279. plane_req->fb_id);
  2280. return -ENOENT;
  2281. }
  2282. crtc = drm_crtc_find(dev, plane_req->crtc_id);
  2283. if (!crtc) {
  2284. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  2285. plane_req->crtc_id);
  2286. return -ENOENT;
  2287. }
  2288. }
  2289. /*
  2290. * setplane_internal will take care of deref'ing either the old or new
  2291. * framebuffer depending on success.
  2292. */
  2293. return setplane_internal(plane, crtc, fb,
  2294. plane_req->crtc_x, plane_req->crtc_y,
  2295. plane_req->crtc_w, plane_req->crtc_h,
  2296. plane_req->src_x, plane_req->src_y,
  2297. plane_req->src_w, plane_req->src_h);
  2298. }
  2299. /**
  2300. * drm_mode_set_config_internal - helper to call ->set_config
  2301. * @set: modeset config to set
  2302. *
  2303. * This is a little helper to wrap internal calls to the ->set_config driver
  2304. * interface. The only thing it adds is correct refcounting dance.
  2305. *
  2306. * Returns:
  2307. * Zero on success, negative errno on failure.
  2308. */
  2309. int drm_mode_set_config_internal(struct drm_mode_set *set)
  2310. {
  2311. struct drm_crtc *crtc = set->crtc;
  2312. struct drm_framebuffer *fb;
  2313. struct drm_crtc *tmp;
  2314. int ret;
  2315. /*
  2316. * NOTE: ->set_config can also disable other crtcs (if we steal all
  2317. * connectors from it), hence we need to refcount the fbs across all
  2318. * crtcs. Atomic modeset will have saner semantics ...
  2319. */
  2320. drm_for_each_crtc(tmp, crtc->dev)
  2321. tmp->primary->old_fb = tmp->primary->fb;
  2322. fb = set->fb;
  2323. ret = crtc->funcs->set_config(set);
  2324. if (ret == 0) {
  2325. crtc->primary->crtc = crtc;
  2326. crtc->primary->fb = fb;
  2327. }
  2328. drm_for_each_crtc(tmp, crtc->dev) {
  2329. if (tmp->primary->fb)
  2330. drm_framebuffer_reference(tmp->primary->fb);
  2331. if (tmp->primary->old_fb)
  2332. drm_framebuffer_unreference(tmp->primary->old_fb);
  2333. tmp->primary->old_fb = NULL;
  2334. }
  2335. return ret;
  2336. }
  2337. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2338. /**
  2339. * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  2340. * @mode: mode to query
  2341. * @hdisplay: hdisplay value to fill in
  2342. * @vdisplay: vdisplay value to fill in
  2343. *
  2344. * The vdisplay value will be doubled if the specified mode is a stereo mode of
  2345. * the appropriate layout.
  2346. */
  2347. void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
  2348. int *hdisplay, int *vdisplay)
  2349. {
  2350. struct drm_display_mode adjusted;
  2351. drm_mode_copy(&adjusted, mode);
  2352. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  2353. *hdisplay = adjusted.crtc_hdisplay;
  2354. *vdisplay = adjusted.crtc_vdisplay;
  2355. }
  2356. EXPORT_SYMBOL(drm_crtc_get_hv_timing);
  2357. /**
  2358. * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2359. * CRTC viewport
  2360. * @crtc: CRTC that framebuffer will be displayed on
  2361. * @x: x panning
  2362. * @y: y panning
  2363. * @mode: mode that framebuffer will be displayed under
  2364. * @fb: framebuffer to check size of
  2365. */
  2366. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2367. int x, int y,
  2368. const struct drm_display_mode *mode,
  2369. const struct drm_framebuffer *fb)
  2370. {
  2371. int hdisplay, vdisplay;
  2372. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  2373. if (crtc->state &&
  2374. crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
  2375. BIT(DRM_ROTATE_270)))
  2376. swap(hdisplay, vdisplay);
  2377. return check_src_coords(x << 16, y << 16,
  2378. hdisplay << 16, vdisplay << 16, fb);
  2379. }
  2380. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2381. /**
  2382. * drm_mode_setcrtc - set CRTC configuration
  2383. * @dev: drm device for the ioctl
  2384. * @data: data pointer for the ioctl
  2385. * @file_priv: drm file for the ioctl call
  2386. *
  2387. * Build a new CRTC configuration based on user request.
  2388. *
  2389. * Called by the user via ioctl.
  2390. *
  2391. * Returns:
  2392. * Zero on success, negative errno on failure.
  2393. */
  2394. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2395. struct drm_file *file_priv)
  2396. {
  2397. struct drm_mode_config *config = &dev->mode_config;
  2398. struct drm_mode_crtc *crtc_req = data;
  2399. struct drm_crtc *crtc;
  2400. struct drm_connector **connector_set = NULL, *connector;
  2401. struct drm_framebuffer *fb = NULL;
  2402. struct drm_display_mode *mode = NULL;
  2403. struct drm_mode_set set;
  2404. uint32_t __user *set_connectors_ptr;
  2405. int ret;
  2406. int i;
  2407. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2408. return -EINVAL;
  2409. /*
  2410. * Universal plane src offsets are only 16.16, prevent havoc for
  2411. * drivers using universal plane code internally.
  2412. */
  2413. if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  2414. return -ERANGE;
  2415. drm_modeset_lock_all(dev);
  2416. crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2417. if (!crtc) {
  2418. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2419. ret = -ENOENT;
  2420. goto out;
  2421. }
  2422. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  2423. if (crtc_req->mode_valid) {
  2424. /* If we have a mode we need a framebuffer. */
  2425. /* If we pass -1, set the mode with the currently bound fb */
  2426. if (crtc_req->fb_id == -1) {
  2427. if (!crtc->primary->fb) {
  2428. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2429. ret = -EINVAL;
  2430. goto out;
  2431. }
  2432. fb = crtc->primary->fb;
  2433. /* Make refcounting symmetric with the lookup path. */
  2434. drm_framebuffer_reference(fb);
  2435. } else {
  2436. fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2437. if (!fb) {
  2438. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2439. crtc_req->fb_id);
  2440. ret = -ENOENT;
  2441. goto out;
  2442. }
  2443. }
  2444. mode = drm_mode_create(dev);
  2445. if (!mode) {
  2446. ret = -ENOMEM;
  2447. goto out;
  2448. }
  2449. ret = drm_mode_convert_umode(mode, &crtc_req->mode);
  2450. if (ret) {
  2451. DRM_DEBUG_KMS("Invalid mode\n");
  2452. goto out;
  2453. }
  2454. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  2455. /*
  2456. * Check whether the primary plane supports the fb pixel format.
  2457. * Drivers not implementing the universal planes API use a
  2458. * default formats list provided by the DRM core which doesn't
  2459. * match real hardware capabilities. Skip the check in that
  2460. * case.
  2461. */
  2462. if (!crtc->primary->format_default) {
  2463. ret = drm_plane_check_pixel_format(crtc->primary,
  2464. fb->pixel_format);
  2465. if (ret) {
  2466. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2467. drm_get_format_name(fb->pixel_format));
  2468. goto out;
  2469. }
  2470. }
  2471. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2472. mode, fb);
  2473. if (ret)
  2474. goto out;
  2475. }
  2476. if (crtc_req->count_connectors == 0 && mode) {
  2477. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2478. ret = -EINVAL;
  2479. goto out;
  2480. }
  2481. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2482. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2483. crtc_req->count_connectors);
  2484. ret = -EINVAL;
  2485. goto out;
  2486. }
  2487. if (crtc_req->count_connectors > 0) {
  2488. u32 out_id;
  2489. /* Avoid unbounded kernel memory allocation */
  2490. if (crtc_req->count_connectors > config->num_connector) {
  2491. ret = -EINVAL;
  2492. goto out;
  2493. }
  2494. connector_set = kmalloc_array(crtc_req->count_connectors,
  2495. sizeof(struct drm_connector *),
  2496. GFP_KERNEL);
  2497. if (!connector_set) {
  2498. ret = -ENOMEM;
  2499. goto out;
  2500. }
  2501. for (i = 0; i < crtc_req->count_connectors; i++) {
  2502. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2503. if (get_user(out_id, &set_connectors_ptr[i])) {
  2504. ret = -EFAULT;
  2505. goto out;
  2506. }
  2507. connector = drm_connector_find(dev, out_id);
  2508. if (!connector) {
  2509. DRM_DEBUG_KMS("Connector id %d unknown\n",
  2510. out_id);
  2511. ret = -ENOENT;
  2512. goto out;
  2513. }
  2514. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2515. connector->base.id,
  2516. connector->name);
  2517. connector_set[i] = connector;
  2518. }
  2519. }
  2520. set.crtc = crtc;
  2521. set.x = crtc_req->x;
  2522. set.y = crtc_req->y;
  2523. set.mode = mode;
  2524. set.connectors = connector_set;
  2525. set.num_connectors = crtc_req->count_connectors;
  2526. set.fb = fb;
  2527. ret = drm_mode_set_config_internal(&set);
  2528. out:
  2529. if (fb)
  2530. drm_framebuffer_unreference(fb);
  2531. kfree(connector_set);
  2532. drm_mode_destroy(dev, mode);
  2533. drm_modeset_unlock_all(dev);
  2534. return ret;
  2535. }
  2536. /**
  2537. * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
  2538. * universal plane handler call
  2539. * @crtc: crtc to update cursor for
  2540. * @req: data pointer for the ioctl
  2541. * @file_priv: drm file for the ioctl call
  2542. *
  2543. * Legacy cursor ioctl's work directly with driver buffer handles. To
  2544. * translate legacy ioctl calls into universal plane handler calls, we need to
  2545. * wrap the native buffer handle in a drm_framebuffer.
  2546. *
  2547. * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
  2548. * buffer with a pitch of 4*width; the universal plane interface should be used
  2549. * directly in cases where the hardware can support other buffer settings and
  2550. * userspace wants to make use of these capabilities.
  2551. *
  2552. * Returns:
  2553. * Zero on success, negative errno on failure.
  2554. */
  2555. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  2556. struct drm_mode_cursor2 *req,
  2557. struct drm_file *file_priv)
  2558. {
  2559. struct drm_device *dev = crtc->dev;
  2560. struct drm_framebuffer *fb = NULL;
  2561. struct drm_mode_fb_cmd2 fbreq = {
  2562. .width = req->width,
  2563. .height = req->height,
  2564. .pixel_format = DRM_FORMAT_ARGB8888,
  2565. .pitches = { req->width * 4 },
  2566. .handles = { req->handle },
  2567. };
  2568. int32_t crtc_x, crtc_y;
  2569. uint32_t crtc_w = 0, crtc_h = 0;
  2570. uint32_t src_w = 0, src_h = 0;
  2571. int ret = 0;
  2572. BUG_ON(!crtc->cursor);
  2573. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  2574. /*
  2575. * Obtain fb we'll be using (either new or existing) and take an extra
  2576. * reference to it if fb != null. setplane will take care of dropping
  2577. * the reference if the plane update fails.
  2578. */
  2579. if (req->flags & DRM_MODE_CURSOR_BO) {
  2580. if (req->handle) {
  2581. fb = internal_framebuffer_create(dev, &fbreq, file_priv);
  2582. if (IS_ERR(fb)) {
  2583. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  2584. return PTR_ERR(fb);
  2585. }
  2586. } else {
  2587. fb = NULL;
  2588. }
  2589. } else {
  2590. fb = crtc->cursor->fb;
  2591. if (fb)
  2592. drm_framebuffer_reference(fb);
  2593. }
  2594. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2595. crtc_x = req->x;
  2596. crtc_y = req->y;
  2597. } else {
  2598. crtc_x = crtc->cursor_x;
  2599. crtc_y = crtc->cursor_y;
  2600. }
  2601. if (fb) {
  2602. crtc_w = fb->width;
  2603. crtc_h = fb->height;
  2604. src_w = fb->width << 16;
  2605. src_h = fb->height << 16;
  2606. }
  2607. /*
  2608. * setplane_internal will take care of deref'ing either the old or new
  2609. * framebuffer depending on success.
  2610. */
  2611. ret = __setplane_internal(crtc->cursor, crtc, fb,
  2612. crtc_x, crtc_y, crtc_w, crtc_h,
  2613. 0, 0, src_w, src_h);
  2614. /* Update successful; save new cursor position, if necessary */
  2615. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  2616. crtc->cursor_x = req->x;
  2617. crtc->cursor_y = req->y;
  2618. }
  2619. return ret;
  2620. }
  2621. static int drm_mode_cursor_common(struct drm_device *dev,
  2622. struct drm_mode_cursor2 *req,
  2623. struct drm_file *file_priv)
  2624. {
  2625. struct drm_crtc *crtc;
  2626. int ret = 0;
  2627. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2628. return -EINVAL;
  2629. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2630. return -EINVAL;
  2631. crtc = drm_crtc_find(dev, req->crtc_id);
  2632. if (!crtc) {
  2633. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2634. return -ENOENT;
  2635. }
  2636. /*
  2637. * If this crtc has a universal cursor plane, call that plane's update
  2638. * handler rather than using legacy cursor handlers.
  2639. */
  2640. drm_modeset_lock_crtc(crtc, crtc->cursor);
  2641. if (crtc->cursor) {
  2642. ret = drm_mode_cursor_universal(crtc, req, file_priv);
  2643. goto out;
  2644. }
  2645. if (req->flags & DRM_MODE_CURSOR_BO) {
  2646. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2647. ret = -ENXIO;
  2648. goto out;
  2649. }
  2650. /* Turns off the cursor if handle is 0 */
  2651. if (crtc->funcs->cursor_set2)
  2652. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2653. req->width, req->height, req->hot_x, req->hot_y);
  2654. else
  2655. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2656. req->width, req->height);
  2657. }
  2658. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2659. if (crtc->funcs->cursor_move) {
  2660. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2661. } else {
  2662. ret = -EFAULT;
  2663. goto out;
  2664. }
  2665. }
  2666. out:
  2667. drm_modeset_unlock_crtc(crtc);
  2668. return ret;
  2669. }
  2670. /**
  2671. * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2672. * @dev: drm device for the ioctl
  2673. * @data: data pointer for the ioctl
  2674. * @file_priv: drm file for the ioctl call
  2675. *
  2676. * Set the cursor configuration based on user request.
  2677. *
  2678. * Called by the user via ioctl.
  2679. *
  2680. * Returns:
  2681. * Zero on success, negative errno on failure.
  2682. */
  2683. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2684. void *data, struct drm_file *file_priv)
  2685. {
  2686. struct drm_mode_cursor *req = data;
  2687. struct drm_mode_cursor2 new_req;
  2688. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2689. new_req.hot_x = new_req.hot_y = 0;
  2690. return drm_mode_cursor_common(dev, &new_req, file_priv);
  2691. }
  2692. /**
  2693. * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2694. * @dev: drm device for the ioctl
  2695. * @data: data pointer for the ioctl
  2696. * @file_priv: drm file for the ioctl call
  2697. *
  2698. * Set the cursor configuration based on user request. This implements the 2nd
  2699. * version of the cursor ioctl, which allows userspace to additionally specify
  2700. * the hotspot of the pointer.
  2701. *
  2702. * Called by the user via ioctl.
  2703. *
  2704. * Returns:
  2705. * Zero on success, negative errno on failure.
  2706. */
  2707. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2708. void *data, struct drm_file *file_priv)
  2709. {
  2710. struct drm_mode_cursor2 *req = data;
  2711. return drm_mode_cursor_common(dev, req, file_priv);
  2712. }
  2713. /**
  2714. * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2715. * @bpp: bits per pixels
  2716. * @depth: bit depth per pixel
  2717. *
  2718. * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2719. * Useful in fbdev emulation code, since that deals in those values.
  2720. */
  2721. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2722. {
  2723. uint32_t fmt;
  2724. switch (bpp) {
  2725. case 8:
  2726. fmt = DRM_FORMAT_C8;
  2727. break;
  2728. case 16:
  2729. if (depth == 15)
  2730. fmt = DRM_FORMAT_XRGB1555;
  2731. else
  2732. fmt = DRM_FORMAT_RGB565;
  2733. break;
  2734. case 24:
  2735. fmt = DRM_FORMAT_RGB888;
  2736. break;
  2737. case 32:
  2738. if (depth == 24)
  2739. fmt = DRM_FORMAT_XRGB8888;
  2740. else if (depth == 30)
  2741. fmt = DRM_FORMAT_XRGB2101010;
  2742. else
  2743. fmt = DRM_FORMAT_ARGB8888;
  2744. break;
  2745. default:
  2746. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2747. fmt = DRM_FORMAT_XRGB8888;
  2748. break;
  2749. }
  2750. return fmt;
  2751. }
  2752. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2753. /**
  2754. * drm_mode_addfb - add an FB to the graphics configuration
  2755. * @dev: drm device for the ioctl
  2756. * @data: data pointer for the ioctl
  2757. * @file_priv: drm file for the ioctl call
  2758. *
  2759. * Add a new FB to the specified CRTC, given a user request. This is the
  2760. * original addfb ioctl which only supported RGB formats.
  2761. *
  2762. * Called by the user via ioctl.
  2763. *
  2764. * Returns:
  2765. * Zero on success, negative errno on failure.
  2766. */
  2767. int drm_mode_addfb(struct drm_device *dev,
  2768. void *data, struct drm_file *file_priv)
  2769. {
  2770. struct drm_mode_fb_cmd *or = data;
  2771. struct drm_mode_fb_cmd2 r = {};
  2772. int ret;
  2773. /* convert to new format and call new ioctl */
  2774. r.fb_id = or->fb_id;
  2775. r.width = or->width;
  2776. r.height = or->height;
  2777. r.pitches[0] = or->pitch;
  2778. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2779. r.handles[0] = or->handle;
  2780. ret = drm_mode_addfb2(dev, &r, file_priv);
  2781. if (ret)
  2782. return ret;
  2783. or->fb_id = r.fb_id;
  2784. return 0;
  2785. }
  2786. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2787. {
  2788. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2789. switch (format) {
  2790. case DRM_FORMAT_C8:
  2791. case DRM_FORMAT_RGB332:
  2792. case DRM_FORMAT_BGR233:
  2793. case DRM_FORMAT_XRGB4444:
  2794. case DRM_FORMAT_XBGR4444:
  2795. case DRM_FORMAT_RGBX4444:
  2796. case DRM_FORMAT_BGRX4444:
  2797. case DRM_FORMAT_ARGB4444:
  2798. case DRM_FORMAT_ABGR4444:
  2799. case DRM_FORMAT_RGBA4444:
  2800. case DRM_FORMAT_BGRA4444:
  2801. case DRM_FORMAT_XRGB1555:
  2802. case DRM_FORMAT_XBGR1555:
  2803. case DRM_FORMAT_RGBX5551:
  2804. case DRM_FORMAT_BGRX5551:
  2805. case DRM_FORMAT_ARGB1555:
  2806. case DRM_FORMAT_ABGR1555:
  2807. case DRM_FORMAT_RGBA5551:
  2808. case DRM_FORMAT_BGRA5551:
  2809. case DRM_FORMAT_RGB565:
  2810. case DRM_FORMAT_BGR565:
  2811. case DRM_FORMAT_RGB888:
  2812. case DRM_FORMAT_BGR888:
  2813. case DRM_FORMAT_XRGB8888:
  2814. case DRM_FORMAT_XBGR8888:
  2815. case DRM_FORMAT_RGBX8888:
  2816. case DRM_FORMAT_BGRX8888:
  2817. case DRM_FORMAT_ARGB8888:
  2818. case DRM_FORMAT_ABGR8888:
  2819. case DRM_FORMAT_RGBA8888:
  2820. case DRM_FORMAT_BGRA8888:
  2821. case DRM_FORMAT_XRGB2101010:
  2822. case DRM_FORMAT_XBGR2101010:
  2823. case DRM_FORMAT_RGBX1010102:
  2824. case DRM_FORMAT_BGRX1010102:
  2825. case DRM_FORMAT_ARGB2101010:
  2826. case DRM_FORMAT_ABGR2101010:
  2827. case DRM_FORMAT_RGBA1010102:
  2828. case DRM_FORMAT_BGRA1010102:
  2829. case DRM_FORMAT_YUYV:
  2830. case DRM_FORMAT_YVYU:
  2831. case DRM_FORMAT_UYVY:
  2832. case DRM_FORMAT_VYUY:
  2833. case DRM_FORMAT_AYUV:
  2834. case DRM_FORMAT_NV12:
  2835. case DRM_FORMAT_NV21:
  2836. case DRM_FORMAT_NV16:
  2837. case DRM_FORMAT_NV61:
  2838. case DRM_FORMAT_NV24:
  2839. case DRM_FORMAT_NV42:
  2840. case DRM_FORMAT_YUV410:
  2841. case DRM_FORMAT_YVU410:
  2842. case DRM_FORMAT_YUV411:
  2843. case DRM_FORMAT_YVU411:
  2844. case DRM_FORMAT_YUV420:
  2845. case DRM_FORMAT_YVU420:
  2846. case DRM_FORMAT_YUV422:
  2847. case DRM_FORMAT_YVU422:
  2848. case DRM_FORMAT_YUV444:
  2849. case DRM_FORMAT_YVU444:
  2850. return 0;
  2851. default:
  2852. DRM_DEBUG_KMS("invalid pixel format %s\n",
  2853. drm_get_format_name(r->pixel_format));
  2854. return -EINVAL;
  2855. }
  2856. }
  2857. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  2858. {
  2859. int ret, hsub, vsub, num_planes, i;
  2860. ret = format_check(r);
  2861. if (ret) {
  2862. DRM_DEBUG_KMS("bad framebuffer format %s\n",
  2863. drm_get_format_name(r->pixel_format));
  2864. return ret;
  2865. }
  2866. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  2867. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  2868. num_planes = drm_format_num_planes(r->pixel_format);
  2869. if (r->width == 0 || r->width % hsub) {
  2870. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  2871. return -EINVAL;
  2872. }
  2873. if (r->height == 0 || r->height % vsub) {
  2874. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  2875. return -EINVAL;
  2876. }
  2877. for (i = 0; i < num_planes; i++) {
  2878. unsigned int width = r->width / (i != 0 ? hsub : 1);
  2879. unsigned int height = r->height / (i != 0 ? vsub : 1);
  2880. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  2881. if (!r->handles[i]) {
  2882. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  2883. return -EINVAL;
  2884. }
  2885. if ((uint64_t) width * cpp > UINT_MAX)
  2886. return -ERANGE;
  2887. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2888. return -ERANGE;
  2889. if (r->pitches[i] < width * cpp) {
  2890. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2891. return -EINVAL;
  2892. }
  2893. if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
  2894. DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
  2895. r->modifier[i], i);
  2896. return -EINVAL;
  2897. }
  2898. /* modifier specific checks: */
  2899. switch (r->modifier[i]) {
  2900. case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
  2901. /* NOTE: the pitch restriction may be lifted later if it turns
  2902. * out that no hw has this restriction:
  2903. */
  2904. if (r->pixel_format != DRM_FORMAT_NV12 ||
  2905. width % 128 || height % 32 ||
  2906. r->pitches[i] % 128) {
  2907. DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
  2908. return -EINVAL;
  2909. }
  2910. break;
  2911. default:
  2912. break;
  2913. }
  2914. }
  2915. for (i = num_planes; i < 4; i++) {
  2916. if (r->modifier[i]) {
  2917. DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
  2918. return -EINVAL;
  2919. }
  2920. /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
  2921. if (!(r->flags & DRM_MODE_FB_MODIFIERS))
  2922. continue;
  2923. if (r->handles[i]) {
  2924. DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
  2925. return -EINVAL;
  2926. }
  2927. if (r->pitches[i]) {
  2928. DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
  2929. return -EINVAL;
  2930. }
  2931. if (r->offsets[i]) {
  2932. DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
  2933. return -EINVAL;
  2934. }
  2935. }
  2936. return 0;
  2937. }
  2938. static struct drm_framebuffer *
  2939. internal_framebuffer_create(struct drm_device *dev,
  2940. const struct drm_mode_fb_cmd2 *r,
  2941. struct drm_file *file_priv)
  2942. {
  2943. struct drm_mode_config *config = &dev->mode_config;
  2944. struct drm_framebuffer *fb;
  2945. int ret;
  2946. if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
  2947. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2948. return ERR_PTR(-EINVAL);
  2949. }
  2950. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2951. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2952. r->width, config->min_width, config->max_width);
  2953. return ERR_PTR(-EINVAL);
  2954. }
  2955. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2956. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2957. r->height, config->min_height, config->max_height);
  2958. return ERR_PTR(-EINVAL);
  2959. }
  2960. if (r->flags & DRM_MODE_FB_MODIFIERS &&
  2961. !dev->mode_config.allow_fb_modifiers) {
  2962. DRM_DEBUG_KMS("driver does not support fb modifiers\n");
  2963. return ERR_PTR(-EINVAL);
  2964. }
  2965. ret = framebuffer_check(r);
  2966. if (ret)
  2967. return ERR_PTR(ret);
  2968. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2969. if (IS_ERR(fb)) {
  2970. DRM_DEBUG_KMS("could not create framebuffer\n");
  2971. return fb;
  2972. }
  2973. return fb;
  2974. }
  2975. /**
  2976. * drm_mode_addfb2 - add an FB to the graphics configuration
  2977. * @dev: drm device for the ioctl
  2978. * @data: data pointer for the ioctl
  2979. * @file_priv: drm file for the ioctl call
  2980. *
  2981. * Add a new FB to the specified CRTC, given a user request with format. This is
  2982. * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  2983. * and uses fourcc codes as pixel format specifiers.
  2984. *
  2985. * Called by the user via ioctl.
  2986. *
  2987. * Returns:
  2988. * Zero on success, negative errno on failure.
  2989. */
  2990. int drm_mode_addfb2(struct drm_device *dev,
  2991. void *data, struct drm_file *file_priv)
  2992. {
  2993. struct drm_mode_fb_cmd2 *r = data;
  2994. struct drm_framebuffer *fb;
  2995. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2996. return -EINVAL;
  2997. fb = internal_framebuffer_create(dev, r, file_priv);
  2998. if (IS_ERR(fb))
  2999. return PTR_ERR(fb);
  3000. /* Transfer ownership to the filp for reaping on close */
  3001. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  3002. mutex_lock(&file_priv->fbs_lock);
  3003. r->fb_id = fb->base.id;
  3004. list_add(&fb->filp_head, &file_priv->fbs);
  3005. mutex_unlock(&file_priv->fbs_lock);
  3006. return 0;
  3007. }
  3008. /**
  3009. * drm_mode_rmfb - remove an FB from the configuration
  3010. * @dev: drm device for the ioctl
  3011. * @data: data pointer for the ioctl
  3012. * @file_priv: drm file for the ioctl call
  3013. *
  3014. * Remove the FB specified by the user.
  3015. *
  3016. * Called by the user via ioctl.
  3017. *
  3018. * Returns:
  3019. * Zero on success, negative errno on failure.
  3020. */
  3021. int drm_mode_rmfb(struct drm_device *dev,
  3022. void *data, struct drm_file *file_priv)
  3023. {
  3024. struct drm_framebuffer *fb = NULL;
  3025. struct drm_framebuffer *fbl = NULL;
  3026. uint32_t *id = data;
  3027. int found = 0;
  3028. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3029. return -EINVAL;
  3030. mutex_lock(&file_priv->fbs_lock);
  3031. mutex_lock(&dev->mode_config.fb_lock);
  3032. fb = __drm_framebuffer_lookup(dev, *id);
  3033. if (!fb)
  3034. goto fail_lookup;
  3035. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  3036. if (fb == fbl)
  3037. found = 1;
  3038. if (!found)
  3039. goto fail_lookup;
  3040. list_del_init(&fb->filp_head);
  3041. mutex_unlock(&dev->mode_config.fb_lock);
  3042. mutex_unlock(&file_priv->fbs_lock);
  3043. drm_framebuffer_unreference(fb);
  3044. return 0;
  3045. fail_lookup:
  3046. mutex_unlock(&dev->mode_config.fb_lock);
  3047. mutex_unlock(&file_priv->fbs_lock);
  3048. return -ENOENT;
  3049. }
  3050. /**
  3051. * drm_mode_getfb - get FB info
  3052. * @dev: drm device for the ioctl
  3053. * @data: data pointer for the ioctl
  3054. * @file_priv: drm file for the ioctl call
  3055. *
  3056. * Lookup the FB given its ID and return info about it.
  3057. *
  3058. * Called by the user via ioctl.
  3059. *
  3060. * Returns:
  3061. * Zero on success, negative errno on failure.
  3062. */
  3063. int drm_mode_getfb(struct drm_device *dev,
  3064. void *data, struct drm_file *file_priv)
  3065. {
  3066. struct drm_mode_fb_cmd *r = data;
  3067. struct drm_framebuffer *fb;
  3068. int ret;
  3069. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3070. return -EINVAL;
  3071. fb = drm_framebuffer_lookup(dev, r->fb_id);
  3072. if (!fb)
  3073. return -ENOENT;
  3074. r->height = fb->height;
  3075. r->width = fb->width;
  3076. r->depth = fb->depth;
  3077. r->bpp = fb->bits_per_pixel;
  3078. r->pitch = fb->pitches[0];
  3079. if (fb->funcs->create_handle) {
  3080. if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
  3081. drm_is_control_client(file_priv)) {
  3082. ret = fb->funcs->create_handle(fb, file_priv,
  3083. &r->handle);
  3084. } else {
  3085. /* GET_FB() is an unprivileged ioctl so we must not
  3086. * return a buffer-handle to non-master processes! For
  3087. * backwards-compatibility reasons, we cannot make
  3088. * GET_FB() privileged, so just return an invalid handle
  3089. * for non-masters. */
  3090. r->handle = 0;
  3091. ret = 0;
  3092. }
  3093. } else {
  3094. ret = -ENODEV;
  3095. }
  3096. drm_framebuffer_unreference(fb);
  3097. return ret;
  3098. }
  3099. /**
  3100. * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  3101. * @dev: drm device for the ioctl
  3102. * @data: data pointer for the ioctl
  3103. * @file_priv: drm file for the ioctl call
  3104. *
  3105. * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  3106. * rectangle list. Generic userspace which does frontbuffer rendering must call
  3107. * this ioctl to flush out the changes on manual-update display outputs, e.g.
  3108. * usb display-link, mipi manual update panels or edp panel self refresh modes.
  3109. *
  3110. * Modesetting drivers which always update the frontbuffer do not need to
  3111. * implement the corresponding ->dirty framebuffer callback.
  3112. *
  3113. * Called by the user via ioctl.
  3114. *
  3115. * Returns:
  3116. * Zero on success, negative errno on failure.
  3117. */
  3118. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  3119. void *data, struct drm_file *file_priv)
  3120. {
  3121. struct drm_clip_rect __user *clips_ptr;
  3122. struct drm_clip_rect *clips = NULL;
  3123. struct drm_mode_fb_dirty_cmd *r = data;
  3124. struct drm_framebuffer *fb;
  3125. unsigned flags;
  3126. int num_clips;
  3127. int ret;
  3128. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3129. return -EINVAL;
  3130. fb = drm_framebuffer_lookup(dev, r->fb_id);
  3131. if (!fb)
  3132. return -ENOENT;
  3133. num_clips = r->num_clips;
  3134. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  3135. if (!num_clips != !clips_ptr) {
  3136. ret = -EINVAL;
  3137. goto out_err1;
  3138. }
  3139. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  3140. /* If userspace annotates copy, clips must come in pairs */
  3141. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  3142. ret = -EINVAL;
  3143. goto out_err1;
  3144. }
  3145. if (num_clips && clips_ptr) {
  3146. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  3147. ret = -EINVAL;
  3148. goto out_err1;
  3149. }
  3150. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  3151. if (!clips) {
  3152. ret = -ENOMEM;
  3153. goto out_err1;
  3154. }
  3155. ret = copy_from_user(clips, clips_ptr,
  3156. num_clips * sizeof(*clips));
  3157. if (ret) {
  3158. ret = -EFAULT;
  3159. goto out_err2;
  3160. }
  3161. }
  3162. if (fb->funcs->dirty) {
  3163. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  3164. clips, num_clips);
  3165. } else {
  3166. ret = -ENOSYS;
  3167. }
  3168. out_err2:
  3169. kfree(clips);
  3170. out_err1:
  3171. drm_framebuffer_unreference(fb);
  3172. return ret;
  3173. }
  3174. /**
  3175. * drm_fb_release - remove and free the FBs on this file
  3176. * @priv: drm file for the ioctl
  3177. *
  3178. * Destroy all the FBs associated with @filp.
  3179. *
  3180. * Called by the user via ioctl.
  3181. *
  3182. * Returns:
  3183. * Zero on success, negative errno on failure.
  3184. */
  3185. void drm_fb_release(struct drm_file *priv)
  3186. {
  3187. struct drm_framebuffer *fb, *tfb;
  3188. /*
  3189. * When the file gets released that means no one else can access the fb
  3190. * list any more, so no need to grab fpriv->fbs_lock. And we need to
  3191. * avoid upsetting lockdep since the universal cursor code adds a
  3192. * framebuffer while holding mutex locks.
  3193. *
  3194. * Note that a real deadlock between fpriv->fbs_lock and the modeset
  3195. * locks is impossible here since no one else but this function can get
  3196. * at it any more.
  3197. */
  3198. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  3199. list_del_init(&fb->filp_head);
  3200. /* This drops the fpriv->fbs reference. */
  3201. drm_framebuffer_unreference(fb);
  3202. }
  3203. }
  3204. /**
  3205. * drm_property_create - create a new property type
  3206. * @dev: drm device
  3207. * @flags: flags specifying the property type
  3208. * @name: name of the property
  3209. * @num_values: number of pre-defined values
  3210. *
  3211. * This creates a new generic drm property which can then be attached to a drm
  3212. * object with drm_object_attach_property. The returned property object must be
  3213. * freed with drm_property_destroy.
  3214. *
  3215. * Note that the DRM core keeps a per-device list of properties and that, if
  3216. * drm_mode_config_cleanup() is called, it will destroy all properties created
  3217. * by the driver.
  3218. *
  3219. * Returns:
  3220. * A pointer to the newly created property on success, NULL on failure.
  3221. */
  3222. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  3223. const char *name, int num_values)
  3224. {
  3225. struct drm_property *property = NULL;
  3226. int ret;
  3227. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  3228. if (!property)
  3229. return NULL;
  3230. property->dev = dev;
  3231. if (num_values) {
  3232. property->values = kcalloc(num_values, sizeof(uint64_t),
  3233. GFP_KERNEL);
  3234. if (!property->values)
  3235. goto fail;
  3236. }
  3237. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  3238. if (ret)
  3239. goto fail;
  3240. property->flags = flags;
  3241. property->num_values = num_values;
  3242. INIT_LIST_HEAD(&property->enum_list);
  3243. if (name) {
  3244. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  3245. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  3246. }
  3247. list_add_tail(&property->head, &dev->mode_config.property_list);
  3248. WARN_ON(!drm_property_type_valid(property));
  3249. return property;
  3250. fail:
  3251. kfree(property->values);
  3252. kfree(property);
  3253. return NULL;
  3254. }
  3255. EXPORT_SYMBOL(drm_property_create);
  3256. /**
  3257. * drm_property_create_enum - create a new enumeration property type
  3258. * @dev: drm device
  3259. * @flags: flags specifying the property type
  3260. * @name: name of the property
  3261. * @props: enumeration lists with property values
  3262. * @num_values: number of pre-defined values
  3263. *
  3264. * This creates a new generic drm property which can then be attached to a drm
  3265. * object with drm_object_attach_property. The returned property object must be
  3266. * freed with drm_property_destroy.
  3267. *
  3268. * Userspace is only allowed to set one of the predefined values for enumeration
  3269. * properties.
  3270. *
  3271. * Returns:
  3272. * A pointer to the newly created property on success, NULL on failure.
  3273. */
  3274. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  3275. const char *name,
  3276. const struct drm_prop_enum_list *props,
  3277. int num_values)
  3278. {
  3279. struct drm_property *property;
  3280. int i, ret;
  3281. flags |= DRM_MODE_PROP_ENUM;
  3282. property = drm_property_create(dev, flags, name, num_values);
  3283. if (!property)
  3284. return NULL;
  3285. for (i = 0; i < num_values; i++) {
  3286. ret = drm_property_add_enum(property, i,
  3287. props[i].type,
  3288. props[i].name);
  3289. if (ret) {
  3290. drm_property_destroy(dev, property);
  3291. return NULL;
  3292. }
  3293. }
  3294. return property;
  3295. }
  3296. EXPORT_SYMBOL(drm_property_create_enum);
  3297. /**
  3298. * drm_property_create_bitmask - create a new bitmask property type
  3299. * @dev: drm device
  3300. * @flags: flags specifying the property type
  3301. * @name: name of the property
  3302. * @props: enumeration lists with property bitflags
  3303. * @num_props: size of the @props array
  3304. * @supported_bits: bitmask of all supported enumeration values
  3305. *
  3306. * This creates a new bitmask drm property which can then be attached to a drm
  3307. * object with drm_object_attach_property. The returned property object must be
  3308. * freed with drm_property_destroy.
  3309. *
  3310. * Compared to plain enumeration properties userspace is allowed to set any
  3311. * or'ed together combination of the predefined property bitflag values
  3312. *
  3313. * Returns:
  3314. * A pointer to the newly created property on success, NULL on failure.
  3315. */
  3316. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  3317. int flags, const char *name,
  3318. const struct drm_prop_enum_list *props,
  3319. int num_props,
  3320. uint64_t supported_bits)
  3321. {
  3322. struct drm_property *property;
  3323. int i, ret, index = 0;
  3324. int num_values = hweight64(supported_bits);
  3325. flags |= DRM_MODE_PROP_BITMASK;
  3326. property = drm_property_create(dev, flags, name, num_values);
  3327. if (!property)
  3328. return NULL;
  3329. for (i = 0; i < num_props; i++) {
  3330. if (!(supported_bits & (1ULL << props[i].type)))
  3331. continue;
  3332. if (WARN_ON(index >= num_values)) {
  3333. drm_property_destroy(dev, property);
  3334. return NULL;
  3335. }
  3336. ret = drm_property_add_enum(property, index++,
  3337. props[i].type,
  3338. props[i].name);
  3339. if (ret) {
  3340. drm_property_destroy(dev, property);
  3341. return NULL;
  3342. }
  3343. }
  3344. return property;
  3345. }
  3346. EXPORT_SYMBOL(drm_property_create_bitmask);
  3347. static struct drm_property *property_create_range(struct drm_device *dev,
  3348. int flags, const char *name,
  3349. uint64_t min, uint64_t max)
  3350. {
  3351. struct drm_property *property;
  3352. property = drm_property_create(dev, flags, name, 2);
  3353. if (!property)
  3354. return NULL;
  3355. property->values[0] = min;
  3356. property->values[1] = max;
  3357. return property;
  3358. }
  3359. /**
  3360. * drm_property_create_range - create a new unsigned ranged property type
  3361. * @dev: drm device
  3362. * @flags: flags specifying the property type
  3363. * @name: name of the property
  3364. * @min: minimum value of the property
  3365. * @max: maximum value of the property
  3366. *
  3367. * This creates a new generic drm property which can then be attached to a drm
  3368. * object with drm_object_attach_property. The returned property object must be
  3369. * freed with drm_property_destroy.
  3370. *
  3371. * Userspace is allowed to set any unsigned integer value in the (min, max)
  3372. * range inclusive.
  3373. *
  3374. * Returns:
  3375. * A pointer to the newly created property on success, NULL on failure.
  3376. */
  3377. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  3378. const char *name,
  3379. uint64_t min, uint64_t max)
  3380. {
  3381. return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
  3382. name, min, max);
  3383. }
  3384. EXPORT_SYMBOL(drm_property_create_range);
  3385. /**
  3386. * drm_property_create_signed_range - create a new signed ranged property type
  3387. * @dev: drm device
  3388. * @flags: flags specifying the property type
  3389. * @name: name of the property
  3390. * @min: minimum value of the property
  3391. * @max: maximum value of the property
  3392. *
  3393. * This creates a new generic drm property which can then be attached to a drm
  3394. * object with drm_object_attach_property. The returned property object must be
  3395. * freed with drm_property_destroy.
  3396. *
  3397. * Userspace is allowed to set any signed integer value in the (min, max)
  3398. * range inclusive.
  3399. *
  3400. * Returns:
  3401. * A pointer to the newly created property on success, NULL on failure.
  3402. */
  3403. struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
  3404. int flags, const char *name,
  3405. int64_t min, int64_t max)
  3406. {
  3407. return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
  3408. name, I642U64(min), I642U64(max));
  3409. }
  3410. EXPORT_SYMBOL(drm_property_create_signed_range);
  3411. /**
  3412. * drm_property_create_object - create a new object property type
  3413. * @dev: drm device
  3414. * @flags: flags specifying the property type
  3415. * @name: name of the property
  3416. * @type: object type from DRM_MODE_OBJECT_* defines
  3417. *
  3418. * This creates a new generic drm property which can then be attached to a drm
  3419. * object with drm_object_attach_property. The returned property object must be
  3420. * freed with drm_property_destroy.
  3421. *
  3422. * Userspace is only allowed to set this to any property value of the given
  3423. * @type. Only useful for atomic properties, which is enforced.
  3424. *
  3425. * Returns:
  3426. * A pointer to the newly created property on success, NULL on failure.
  3427. */
  3428. struct drm_property *drm_property_create_object(struct drm_device *dev,
  3429. int flags, const char *name, uint32_t type)
  3430. {
  3431. struct drm_property *property;
  3432. flags |= DRM_MODE_PROP_OBJECT;
  3433. if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
  3434. return NULL;
  3435. property = drm_property_create(dev, flags, name, 1);
  3436. if (!property)
  3437. return NULL;
  3438. property->values[0] = type;
  3439. return property;
  3440. }
  3441. EXPORT_SYMBOL(drm_property_create_object);
  3442. /**
  3443. * drm_property_create_bool - create a new boolean property type
  3444. * @dev: drm device
  3445. * @flags: flags specifying the property type
  3446. * @name: name of the property
  3447. *
  3448. * This creates a new generic drm property which can then be attached to a drm
  3449. * object with drm_object_attach_property. The returned property object must be
  3450. * freed with drm_property_destroy.
  3451. *
  3452. * This is implemented as a ranged property with only {0, 1} as valid values.
  3453. *
  3454. * Returns:
  3455. * A pointer to the newly created property on success, NULL on failure.
  3456. */
  3457. struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
  3458. const char *name)
  3459. {
  3460. return drm_property_create_range(dev, flags, name, 0, 1);
  3461. }
  3462. EXPORT_SYMBOL(drm_property_create_bool);
  3463. /**
  3464. * drm_property_add_enum - add a possible value to an enumeration property
  3465. * @property: enumeration property to change
  3466. * @index: index of the new enumeration
  3467. * @value: value of the new enumeration
  3468. * @name: symbolic name of the new enumeration
  3469. *
  3470. * This functions adds enumerations to a property.
  3471. *
  3472. * It's use is deprecated, drivers should use one of the more specific helpers
  3473. * to directly create the property with all enumerations already attached.
  3474. *
  3475. * Returns:
  3476. * Zero on success, error code on failure.
  3477. */
  3478. int drm_property_add_enum(struct drm_property *property, int index,
  3479. uint64_t value, const char *name)
  3480. {
  3481. struct drm_property_enum *prop_enum;
  3482. if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3483. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
  3484. return -EINVAL;
  3485. /*
  3486. * Bitmask enum properties have the additional constraint of values
  3487. * from 0 to 63
  3488. */
  3489. if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
  3490. (value > 63))
  3491. return -EINVAL;
  3492. if (!list_empty(&property->enum_list)) {
  3493. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3494. if (prop_enum->value == value) {
  3495. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3496. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3497. return 0;
  3498. }
  3499. }
  3500. }
  3501. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  3502. if (!prop_enum)
  3503. return -ENOMEM;
  3504. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3505. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3506. prop_enum->value = value;
  3507. property->values[index] = value;
  3508. list_add_tail(&prop_enum->head, &property->enum_list);
  3509. return 0;
  3510. }
  3511. EXPORT_SYMBOL(drm_property_add_enum);
  3512. /**
  3513. * drm_property_destroy - destroy a drm property
  3514. * @dev: drm device
  3515. * @property: property to destry
  3516. *
  3517. * This function frees a property including any attached resources like
  3518. * enumeration values.
  3519. */
  3520. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  3521. {
  3522. struct drm_property_enum *prop_enum, *pt;
  3523. list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
  3524. list_del(&prop_enum->head);
  3525. kfree(prop_enum);
  3526. }
  3527. if (property->num_values)
  3528. kfree(property->values);
  3529. drm_mode_object_put(dev, &property->base);
  3530. list_del(&property->head);
  3531. kfree(property);
  3532. }
  3533. EXPORT_SYMBOL(drm_property_destroy);
  3534. /**
  3535. * drm_object_attach_property - attach a property to a modeset object
  3536. * @obj: drm modeset object
  3537. * @property: property to attach
  3538. * @init_val: initial value of the property
  3539. *
  3540. * This attaches the given property to the modeset object with the given initial
  3541. * value. Currently this function cannot fail since the properties are stored in
  3542. * a statically sized array.
  3543. */
  3544. void drm_object_attach_property(struct drm_mode_object *obj,
  3545. struct drm_property *property,
  3546. uint64_t init_val)
  3547. {
  3548. int count = obj->properties->count;
  3549. if (count == DRM_OBJECT_MAX_PROPERTY) {
  3550. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  3551. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  3552. "you see this message on the same object type.\n",
  3553. obj->type);
  3554. return;
  3555. }
  3556. obj->properties->properties[count] = property;
  3557. obj->properties->values[count] = init_val;
  3558. obj->properties->count++;
  3559. if (property->flags & DRM_MODE_PROP_ATOMIC)
  3560. obj->properties->atomic_count++;
  3561. }
  3562. EXPORT_SYMBOL(drm_object_attach_property);
  3563. /**
  3564. * drm_object_property_set_value - set the value of a property
  3565. * @obj: drm mode object to set property value for
  3566. * @property: property to set
  3567. * @val: value the property should be set to
  3568. *
  3569. * This functions sets a given property on a given object. This function only
  3570. * changes the software state of the property, it does not call into the
  3571. * driver's ->set_property callback.
  3572. *
  3573. * Returns:
  3574. * Zero on success, error code on failure.
  3575. */
  3576. int drm_object_property_set_value(struct drm_mode_object *obj,
  3577. struct drm_property *property, uint64_t val)
  3578. {
  3579. int i;
  3580. for (i = 0; i < obj->properties->count; i++) {
  3581. if (obj->properties->properties[i] == property) {
  3582. obj->properties->values[i] = val;
  3583. return 0;
  3584. }
  3585. }
  3586. return -EINVAL;
  3587. }
  3588. EXPORT_SYMBOL(drm_object_property_set_value);
  3589. /**
  3590. * drm_object_property_get_value - retrieve the value of a property
  3591. * @obj: drm mode object to get property value from
  3592. * @property: property to retrieve
  3593. * @val: storage for the property value
  3594. *
  3595. * This function retrieves the softare state of the given property for the given
  3596. * property. Since there is no driver callback to retrieve the current property
  3597. * value this might be out of sync with the hardware, depending upon the driver
  3598. * and property.
  3599. *
  3600. * Returns:
  3601. * Zero on success, error code on failure.
  3602. */
  3603. int drm_object_property_get_value(struct drm_mode_object *obj,
  3604. struct drm_property *property, uint64_t *val)
  3605. {
  3606. int i;
  3607. /* read-only properties bypass atomic mechanism and still store
  3608. * their value in obj->properties->values[].. mostly to avoid
  3609. * having to deal w/ EDID and similar props in atomic paths:
  3610. */
  3611. if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
  3612. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  3613. return drm_atomic_get_property(obj, property, val);
  3614. for (i = 0; i < obj->properties->count; i++) {
  3615. if (obj->properties->properties[i] == property) {
  3616. *val = obj->properties->values[i];
  3617. return 0;
  3618. }
  3619. }
  3620. return -EINVAL;
  3621. }
  3622. EXPORT_SYMBOL(drm_object_property_get_value);
  3623. /**
  3624. * drm_mode_getproperty_ioctl - get the property metadata
  3625. * @dev: DRM device
  3626. * @data: ioctl data
  3627. * @file_priv: DRM file info
  3628. *
  3629. * This function retrieves the metadata for a given property, like the different
  3630. * possible values for an enum property or the limits for a range property.
  3631. *
  3632. * Blob properties are special
  3633. *
  3634. * Called by the user via ioctl.
  3635. *
  3636. * Returns:
  3637. * Zero on success, negative errno on failure.
  3638. */
  3639. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  3640. void *data, struct drm_file *file_priv)
  3641. {
  3642. struct drm_mode_get_property *out_resp = data;
  3643. struct drm_property *property;
  3644. int enum_count = 0;
  3645. int value_count = 0;
  3646. int ret = 0, i;
  3647. int copied;
  3648. struct drm_property_enum *prop_enum;
  3649. struct drm_mode_property_enum __user *enum_ptr;
  3650. uint64_t __user *values_ptr;
  3651. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3652. return -EINVAL;
  3653. drm_modeset_lock_all(dev);
  3654. property = drm_property_find(dev, out_resp->prop_id);
  3655. if (!property) {
  3656. ret = -ENOENT;
  3657. goto done;
  3658. }
  3659. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3660. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3661. list_for_each_entry(prop_enum, &property->enum_list, head)
  3662. enum_count++;
  3663. }
  3664. value_count = property->num_values;
  3665. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  3666. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  3667. out_resp->flags = property->flags;
  3668. if ((out_resp->count_values >= value_count) && value_count) {
  3669. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  3670. for (i = 0; i < value_count; i++) {
  3671. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  3672. ret = -EFAULT;
  3673. goto done;
  3674. }
  3675. }
  3676. }
  3677. out_resp->count_values = value_count;
  3678. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3679. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3680. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  3681. copied = 0;
  3682. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  3683. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3684. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  3685. ret = -EFAULT;
  3686. goto done;
  3687. }
  3688. if (copy_to_user(&enum_ptr[copied].name,
  3689. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  3690. ret = -EFAULT;
  3691. goto done;
  3692. }
  3693. copied++;
  3694. }
  3695. }
  3696. out_resp->count_enum_blobs = enum_count;
  3697. }
  3698. /*
  3699. * NOTE: The idea seems to have been to use this to read all the blob
  3700. * property values. But nothing ever added them to the corresponding
  3701. * list, userspace always used the special-purpose get_blob ioctl to
  3702. * read the value for a blob property. It also doesn't make a lot of
  3703. * sense to return values here when everything else is just metadata for
  3704. * the property itself.
  3705. */
  3706. if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  3707. out_resp->count_enum_blobs = 0;
  3708. done:
  3709. drm_modeset_unlock_all(dev);
  3710. return ret;
  3711. }
  3712. /**
  3713. * drm_property_create_blob - Create new blob property
  3714. *
  3715. * Creates a new blob property for a specified DRM device, optionally
  3716. * copying data.
  3717. *
  3718. * @dev: DRM device to create property for
  3719. * @length: Length to allocate for blob data
  3720. * @data: If specified, copies data into blob
  3721. *
  3722. * Returns:
  3723. * New blob property with a single reference on success, or an ERR_PTR
  3724. * value on failure.
  3725. */
  3726. struct drm_property_blob *
  3727. drm_property_create_blob(struct drm_device *dev, size_t length,
  3728. const void *data)
  3729. {
  3730. struct drm_property_blob *blob;
  3731. int ret;
  3732. if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
  3733. return ERR_PTR(-EINVAL);
  3734. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  3735. if (!blob)
  3736. return ERR_PTR(-ENOMEM);
  3737. /* This must be explicitly initialised, so we can safely call list_del
  3738. * on it in the removal handler, even if it isn't in a file list. */
  3739. INIT_LIST_HEAD(&blob->head_file);
  3740. blob->length = length;
  3741. blob->dev = dev;
  3742. if (data)
  3743. memcpy(blob->data, data, length);
  3744. mutex_lock(&dev->mode_config.blob_lock);
  3745. ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  3746. if (ret) {
  3747. kfree(blob);
  3748. mutex_unlock(&dev->mode_config.blob_lock);
  3749. return ERR_PTR(-EINVAL);
  3750. }
  3751. kref_init(&blob->refcount);
  3752. list_add_tail(&blob->head_global,
  3753. &dev->mode_config.property_blob_list);
  3754. mutex_unlock(&dev->mode_config.blob_lock);
  3755. return blob;
  3756. }
  3757. EXPORT_SYMBOL(drm_property_create_blob);
  3758. /**
  3759. * drm_property_free_blob - Blob property destructor
  3760. *
  3761. * Internal free function for blob properties; must not be used directly.
  3762. *
  3763. * @kref: Reference
  3764. */
  3765. static void drm_property_free_blob(struct kref *kref)
  3766. {
  3767. struct drm_property_blob *blob =
  3768. container_of(kref, struct drm_property_blob, refcount);
  3769. WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
  3770. list_del(&blob->head_global);
  3771. list_del(&blob->head_file);
  3772. drm_mode_object_put(blob->dev, &blob->base);
  3773. kfree(blob);
  3774. }
  3775. /**
  3776. * drm_property_unreference_blob - Unreference a blob property
  3777. *
  3778. * Drop a reference on a blob property. May free the object.
  3779. *
  3780. * @blob: Pointer to blob property
  3781. */
  3782. void drm_property_unreference_blob(struct drm_property_blob *blob)
  3783. {
  3784. struct drm_device *dev;
  3785. if (!blob)
  3786. return;
  3787. dev = blob->dev;
  3788. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3789. if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
  3790. &dev->mode_config.blob_lock))
  3791. mutex_unlock(&dev->mode_config.blob_lock);
  3792. else
  3793. might_lock(&dev->mode_config.blob_lock);
  3794. }
  3795. EXPORT_SYMBOL(drm_property_unreference_blob);
  3796. /**
  3797. * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
  3798. *
  3799. * Drop a reference on a blob property. May free the object. This must be
  3800. * called with blob_lock held.
  3801. *
  3802. * @blob: Pointer to blob property
  3803. */
  3804. static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
  3805. {
  3806. if (!blob)
  3807. return;
  3808. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3809. kref_put(&blob->refcount, drm_property_free_blob);
  3810. }
  3811. /**
  3812. * drm_property_destroy_user_blobs - destroy all blobs created by this client
  3813. * @dev: DRM device
  3814. * @file_priv: destroy all blobs owned by this file handle
  3815. */
  3816. void drm_property_destroy_user_blobs(struct drm_device *dev,
  3817. struct drm_file *file_priv)
  3818. {
  3819. struct drm_property_blob *blob, *bt;
  3820. mutex_lock(&dev->mode_config.blob_lock);
  3821. list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
  3822. list_del_init(&blob->head_file);
  3823. drm_property_unreference_blob_locked(blob);
  3824. }
  3825. mutex_unlock(&dev->mode_config.blob_lock);
  3826. }
  3827. /**
  3828. * drm_property_reference_blob - Take a reference on an existing property
  3829. *
  3830. * Take a new reference on an existing blob property.
  3831. *
  3832. * @blob: Pointer to blob property
  3833. */
  3834. struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
  3835. {
  3836. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3837. kref_get(&blob->refcount);
  3838. return blob;
  3839. }
  3840. EXPORT_SYMBOL(drm_property_reference_blob);
  3841. /*
  3842. * Like drm_property_lookup_blob, but does not return an additional reference.
  3843. * Must be called with blob_lock held.
  3844. */
  3845. static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
  3846. uint32_t id)
  3847. {
  3848. struct drm_mode_object *obj = NULL;
  3849. struct drm_property_blob *blob;
  3850. WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
  3851. mutex_lock(&dev->mode_config.idr_mutex);
  3852. obj = idr_find(&dev->mode_config.crtc_idr, id);
  3853. if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
  3854. blob = NULL;
  3855. else
  3856. blob = obj_to_blob(obj);
  3857. mutex_unlock(&dev->mode_config.idr_mutex);
  3858. return blob;
  3859. }
  3860. /**
  3861. * drm_property_lookup_blob - look up a blob property and take a reference
  3862. * @dev: drm device
  3863. * @id: id of the blob property
  3864. *
  3865. * If successful, this takes an additional reference to the blob property.
  3866. * callers need to make sure to eventually unreference the returned property
  3867. * again, using @drm_property_unreference_blob.
  3868. */
  3869. struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
  3870. uint32_t id)
  3871. {
  3872. struct drm_property_blob *blob;
  3873. mutex_lock(&dev->mode_config.blob_lock);
  3874. blob = __drm_property_lookup_blob(dev, id);
  3875. if (blob) {
  3876. if (!kref_get_unless_zero(&blob->refcount))
  3877. blob = NULL;
  3878. }
  3879. mutex_unlock(&dev->mode_config.blob_lock);
  3880. return blob;
  3881. }
  3882. EXPORT_SYMBOL(drm_property_lookup_blob);
  3883. /**
  3884. * drm_property_replace_global_blob - atomically replace existing blob property
  3885. * @dev: drm device
  3886. * @replace: location of blob property pointer to be replaced
  3887. * @length: length of data for new blob, or 0 for no data
  3888. * @data: content for new blob, or NULL for no data
  3889. * @obj_holds_id: optional object for property holding blob ID
  3890. * @prop_holds_id: optional property holding blob ID
  3891. * @return 0 on success or error on failure
  3892. *
  3893. * This function will atomically replace a global property in the blob list,
  3894. * optionally updating a property which holds the ID of that property. It is
  3895. * guaranteed to be atomic: no caller will be allowed to see intermediate
  3896. * results, and either the entire operation will succeed and clean up the
  3897. * previous property, or it will fail and the state will be unchanged.
  3898. *
  3899. * If length is 0 or data is NULL, no new blob will be created, and the holding
  3900. * property, if specified, will be set to 0.
  3901. *
  3902. * Access to the replace pointer is assumed to be protected by the caller, e.g.
  3903. * by holding the relevant modesetting object lock for its parent.
  3904. *
  3905. * For example, a drm_connector has a 'PATH' property, which contains the ID
  3906. * of a blob property with the value of the MST path information. Calling this
  3907. * function with replace pointing to the connector's path_blob_ptr, length and
  3908. * data set for the new path information, obj_holds_id set to the connector's
  3909. * base object, and prop_holds_id set to the path property name, will perform
  3910. * a completely atomic update. The access to path_blob_ptr is protected by the
  3911. * caller holding a lock on the connector.
  3912. */
  3913. static int drm_property_replace_global_blob(struct drm_device *dev,
  3914. struct drm_property_blob **replace,
  3915. size_t length,
  3916. const void *data,
  3917. struct drm_mode_object *obj_holds_id,
  3918. struct drm_property *prop_holds_id)
  3919. {
  3920. struct drm_property_blob *new_blob = NULL;
  3921. struct drm_property_blob *old_blob = NULL;
  3922. int ret;
  3923. WARN_ON(replace == NULL);
  3924. old_blob = *replace;
  3925. if (length && data) {
  3926. new_blob = drm_property_create_blob(dev, length, data);
  3927. if (IS_ERR(new_blob))
  3928. return PTR_ERR(new_blob);
  3929. }
  3930. /* This does not need to be synchronised with blob_lock, as the
  3931. * get_properties ioctl locks all modesetting objects, and
  3932. * obj_holds_id must be locked before calling here, so we cannot
  3933. * have its value out of sync with the list membership modified
  3934. * below under blob_lock. */
  3935. if (obj_holds_id) {
  3936. ret = drm_object_property_set_value(obj_holds_id,
  3937. prop_holds_id,
  3938. new_blob ?
  3939. new_blob->base.id : 0);
  3940. if (ret != 0)
  3941. goto err_created;
  3942. }
  3943. drm_property_unreference_blob(old_blob);
  3944. *replace = new_blob;
  3945. return 0;
  3946. err_created:
  3947. drm_property_unreference_blob(new_blob);
  3948. return ret;
  3949. }
  3950. /**
  3951. * drm_mode_getblob_ioctl - get the contents of a blob property value
  3952. * @dev: DRM device
  3953. * @data: ioctl data
  3954. * @file_priv: DRM file info
  3955. *
  3956. * This function retrieves the contents of a blob property. The value stored in
  3957. * an object's blob property is just a normal modeset object id.
  3958. *
  3959. * Called by the user via ioctl.
  3960. *
  3961. * Returns:
  3962. * Zero on success, negative errno on failure.
  3963. */
  3964. int drm_mode_getblob_ioctl(struct drm_device *dev,
  3965. void *data, struct drm_file *file_priv)
  3966. {
  3967. struct drm_mode_get_blob *out_resp = data;
  3968. struct drm_property_blob *blob;
  3969. int ret = 0;
  3970. void __user *blob_ptr;
  3971. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3972. return -EINVAL;
  3973. drm_modeset_lock_all(dev);
  3974. mutex_lock(&dev->mode_config.blob_lock);
  3975. blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
  3976. if (!blob) {
  3977. ret = -ENOENT;
  3978. goto done;
  3979. }
  3980. if (out_resp->length == blob->length) {
  3981. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  3982. if (copy_to_user(blob_ptr, blob->data, blob->length)) {
  3983. ret = -EFAULT;
  3984. goto done;
  3985. }
  3986. }
  3987. out_resp->length = blob->length;
  3988. done:
  3989. mutex_unlock(&dev->mode_config.blob_lock);
  3990. drm_modeset_unlock_all(dev);
  3991. return ret;
  3992. }
  3993. /**
  3994. * drm_mode_createblob_ioctl - create a new blob property
  3995. * @dev: DRM device
  3996. * @data: ioctl data
  3997. * @file_priv: DRM file info
  3998. *
  3999. * This function creates a new blob property with user-defined values. In order
  4000. * to give us sensible validation and checking when creating, rather than at
  4001. * every potential use, we also require a type to be provided upfront.
  4002. *
  4003. * Called by the user via ioctl.
  4004. *
  4005. * Returns:
  4006. * Zero on success, negative errno on failure.
  4007. */
  4008. int drm_mode_createblob_ioctl(struct drm_device *dev,
  4009. void *data, struct drm_file *file_priv)
  4010. {
  4011. struct drm_mode_create_blob *out_resp = data;
  4012. struct drm_property_blob *blob;
  4013. void __user *blob_ptr;
  4014. int ret = 0;
  4015. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4016. return -EINVAL;
  4017. blob = drm_property_create_blob(dev, out_resp->length, NULL);
  4018. if (IS_ERR(blob))
  4019. return PTR_ERR(blob);
  4020. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  4021. if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
  4022. ret = -EFAULT;
  4023. goto out_blob;
  4024. }
  4025. /* Dropping the lock between create_blob and our access here is safe
  4026. * as only the same file_priv can remove the blob; at this point, it is
  4027. * not associated with any file_priv. */
  4028. mutex_lock(&dev->mode_config.blob_lock);
  4029. out_resp->blob_id = blob->base.id;
  4030. list_add_tail(&blob->head_file, &file_priv->blobs);
  4031. mutex_unlock(&dev->mode_config.blob_lock);
  4032. return 0;
  4033. out_blob:
  4034. drm_property_unreference_blob(blob);
  4035. return ret;
  4036. }
  4037. /**
  4038. * drm_mode_destroyblob_ioctl - destroy a user blob property
  4039. * @dev: DRM device
  4040. * @data: ioctl data
  4041. * @file_priv: DRM file info
  4042. *
  4043. * Destroy an existing user-defined blob property.
  4044. *
  4045. * Called by the user via ioctl.
  4046. *
  4047. * Returns:
  4048. * Zero on success, negative errno on failure.
  4049. */
  4050. int drm_mode_destroyblob_ioctl(struct drm_device *dev,
  4051. void *data, struct drm_file *file_priv)
  4052. {
  4053. struct drm_mode_destroy_blob *out_resp = data;
  4054. struct drm_property_blob *blob = NULL, *bt;
  4055. bool found = false;
  4056. int ret = 0;
  4057. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4058. return -EINVAL;
  4059. mutex_lock(&dev->mode_config.blob_lock);
  4060. blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
  4061. if (!blob) {
  4062. ret = -ENOENT;
  4063. goto err;
  4064. }
  4065. /* Ensure the property was actually created by this user. */
  4066. list_for_each_entry(bt, &file_priv->blobs, head_file) {
  4067. if (bt == blob) {
  4068. found = true;
  4069. break;
  4070. }
  4071. }
  4072. if (!found) {
  4073. ret = -EPERM;
  4074. goto err;
  4075. }
  4076. /* We must drop head_file here, because we may not be the last
  4077. * reference on the blob. */
  4078. list_del_init(&blob->head_file);
  4079. drm_property_unreference_blob_locked(blob);
  4080. mutex_unlock(&dev->mode_config.blob_lock);
  4081. return 0;
  4082. err:
  4083. mutex_unlock(&dev->mode_config.blob_lock);
  4084. return ret;
  4085. }
  4086. /**
  4087. * drm_mode_connector_set_path_property - set tile property on connector
  4088. * @connector: connector to set property on.
  4089. * @path: path to use for property; must not be NULL.
  4090. *
  4091. * This creates a property to expose to userspace to specify a
  4092. * connector path. This is mainly used for DisplayPort MST where
  4093. * connectors have a topology and we want to allow userspace to give
  4094. * them more meaningful names.
  4095. *
  4096. * Returns:
  4097. * Zero on success, negative errno on failure.
  4098. */
  4099. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  4100. const char *path)
  4101. {
  4102. struct drm_device *dev = connector->dev;
  4103. int ret;
  4104. ret = drm_property_replace_global_blob(dev,
  4105. &connector->path_blob_ptr,
  4106. strlen(path) + 1,
  4107. path,
  4108. &connector->base,
  4109. dev->mode_config.path_property);
  4110. return ret;
  4111. }
  4112. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  4113. /**
  4114. * drm_mode_connector_set_tile_property - set tile property on connector
  4115. * @connector: connector to set property on.
  4116. *
  4117. * This looks up the tile information for a connector, and creates a
  4118. * property for userspace to parse if it exists. The property is of
  4119. * the form of 8 integers using ':' as a separator.
  4120. *
  4121. * Returns:
  4122. * Zero on success, errno on failure.
  4123. */
  4124. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  4125. {
  4126. struct drm_device *dev = connector->dev;
  4127. char tile[256];
  4128. int ret;
  4129. if (!connector->has_tile) {
  4130. ret = drm_property_replace_global_blob(dev,
  4131. &connector->tile_blob_ptr,
  4132. 0,
  4133. NULL,
  4134. &connector->base,
  4135. dev->mode_config.tile_property);
  4136. return ret;
  4137. }
  4138. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  4139. connector->tile_group->id, connector->tile_is_single_monitor,
  4140. connector->num_h_tile, connector->num_v_tile,
  4141. connector->tile_h_loc, connector->tile_v_loc,
  4142. connector->tile_h_size, connector->tile_v_size);
  4143. ret = drm_property_replace_global_blob(dev,
  4144. &connector->tile_blob_ptr,
  4145. strlen(tile) + 1,
  4146. tile,
  4147. &connector->base,
  4148. dev->mode_config.tile_property);
  4149. return ret;
  4150. }
  4151. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  4152. /**
  4153. * drm_mode_connector_update_edid_property - update the edid property of a connector
  4154. * @connector: drm connector
  4155. * @edid: new value of the edid property
  4156. *
  4157. * This function creates a new blob modeset object and assigns its id to the
  4158. * connector's edid property.
  4159. *
  4160. * Returns:
  4161. * Zero on success, negative errno on failure.
  4162. */
  4163. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  4164. const struct edid *edid)
  4165. {
  4166. struct drm_device *dev = connector->dev;
  4167. size_t size = 0;
  4168. int ret;
  4169. /* ignore requests to set edid when overridden */
  4170. if (connector->override_edid)
  4171. return 0;
  4172. if (edid)
  4173. size = EDID_LENGTH * (1 + edid->extensions);
  4174. ret = drm_property_replace_global_blob(dev,
  4175. &connector->edid_blob_ptr,
  4176. size,
  4177. edid,
  4178. &connector->base,
  4179. dev->mode_config.edid_property);
  4180. return ret;
  4181. }
  4182. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  4183. /* Some properties could refer to dynamic refcnt'd objects, or things that
  4184. * need special locking to handle lifetime issues (ie. to ensure the prop
  4185. * value doesn't become invalid part way through the property update due to
  4186. * race). The value returned by reference via 'obj' should be passed back
  4187. * to drm_property_change_valid_put() after the property is set (and the
  4188. * object to which the property is attached has a chance to take it's own
  4189. * reference).
  4190. */
  4191. bool drm_property_change_valid_get(struct drm_property *property,
  4192. uint64_t value, struct drm_mode_object **ref)
  4193. {
  4194. int i;
  4195. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  4196. return false;
  4197. *ref = NULL;
  4198. if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
  4199. if (value < property->values[0] || value > property->values[1])
  4200. return false;
  4201. return true;
  4202. } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
  4203. int64_t svalue = U642I64(value);
  4204. if (svalue < U642I64(property->values[0]) ||
  4205. svalue > U642I64(property->values[1]))
  4206. return false;
  4207. return true;
  4208. } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  4209. uint64_t valid_mask = 0;
  4210. for (i = 0; i < property->num_values; i++)
  4211. valid_mask |= (1ULL << property->values[i]);
  4212. return !(value & ~valid_mask);
  4213. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  4214. struct drm_property_blob *blob;
  4215. if (value == 0)
  4216. return true;
  4217. blob = drm_property_lookup_blob(property->dev, value);
  4218. if (blob) {
  4219. *ref = &blob->base;
  4220. return true;
  4221. } else {
  4222. return false;
  4223. }
  4224. } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4225. /* a zero value for an object property translates to null: */
  4226. if (value == 0)
  4227. return true;
  4228. /* handle refcnt'd objects specially: */
  4229. if (property->values[0] == DRM_MODE_OBJECT_FB) {
  4230. struct drm_framebuffer *fb;
  4231. fb = drm_framebuffer_lookup(property->dev, value);
  4232. if (fb) {
  4233. *ref = &fb->base;
  4234. return true;
  4235. } else {
  4236. return false;
  4237. }
  4238. } else {
  4239. return _object_find(property->dev, value, property->values[0]) != NULL;
  4240. }
  4241. }
  4242. for (i = 0; i < property->num_values; i++)
  4243. if (property->values[i] == value)
  4244. return true;
  4245. return false;
  4246. }
  4247. void drm_property_change_valid_put(struct drm_property *property,
  4248. struct drm_mode_object *ref)
  4249. {
  4250. if (!ref)
  4251. return;
  4252. if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4253. if (property->values[0] == DRM_MODE_OBJECT_FB)
  4254. drm_framebuffer_unreference(obj_to_fb(ref));
  4255. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  4256. drm_property_unreference_blob(obj_to_blob(ref));
  4257. }
  4258. /**
  4259. * drm_mode_connector_property_set_ioctl - set the current value of a connector property
  4260. * @dev: DRM device
  4261. * @data: ioctl data
  4262. * @file_priv: DRM file info
  4263. *
  4264. * This function sets the current value for a connectors's property. It also
  4265. * calls into a driver's ->set_property callback to update the hardware state
  4266. *
  4267. * Called by the user via ioctl.
  4268. *
  4269. * Returns:
  4270. * Zero on success, negative errno on failure.
  4271. */
  4272. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  4273. void *data, struct drm_file *file_priv)
  4274. {
  4275. struct drm_mode_connector_set_property *conn_set_prop = data;
  4276. struct drm_mode_obj_set_property obj_set_prop = {
  4277. .value = conn_set_prop->value,
  4278. .prop_id = conn_set_prop->prop_id,
  4279. .obj_id = conn_set_prop->connector_id,
  4280. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  4281. };
  4282. /* It does all the locking and checking we need */
  4283. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  4284. }
  4285. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  4286. struct drm_property *property,
  4287. uint64_t value)
  4288. {
  4289. int ret = -EINVAL;
  4290. struct drm_connector *connector = obj_to_connector(obj);
  4291. /* Do DPMS ourselves */
  4292. if (property == connector->dev->mode_config.dpms_property) {
  4293. ret = (*connector->funcs->dpms)(connector, (int)value);
  4294. } else if (connector->funcs->set_property)
  4295. ret = connector->funcs->set_property(connector, property, value);
  4296. /* store the property value if successful */
  4297. if (!ret)
  4298. drm_object_property_set_value(&connector->base, property, value);
  4299. return ret;
  4300. }
  4301. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  4302. struct drm_property *property,
  4303. uint64_t value)
  4304. {
  4305. int ret = -EINVAL;
  4306. struct drm_crtc *crtc = obj_to_crtc(obj);
  4307. if (crtc->funcs->set_property)
  4308. ret = crtc->funcs->set_property(crtc, property, value);
  4309. if (!ret)
  4310. drm_object_property_set_value(obj, property, value);
  4311. return ret;
  4312. }
  4313. /**
  4314. * drm_mode_plane_set_obj_prop - set the value of a property
  4315. * @plane: drm plane object to set property value for
  4316. * @property: property to set
  4317. * @value: value the property should be set to
  4318. *
  4319. * This functions sets a given property on a given plane object. This function
  4320. * calls the driver's ->set_property callback and changes the software state of
  4321. * the property if the callback succeeds.
  4322. *
  4323. * Returns:
  4324. * Zero on success, error code on failure.
  4325. */
  4326. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  4327. struct drm_property *property,
  4328. uint64_t value)
  4329. {
  4330. int ret = -EINVAL;
  4331. struct drm_mode_object *obj = &plane->base;
  4332. if (plane->funcs->set_property)
  4333. ret = plane->funcs->set_property(plane, property, value);
  4334. if (!ret)
  4335. drm_object_property_set_value(obj, property, value);
  4336. return ret;
  4337. }
  4338. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  4339. /**
  4340. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  4341. * @dev: DRM device
  4342. * @data: ioctl data
  4343. * @file_priv: DRM file info
  4344. *
  4345. * This function retrieves the current value for an object's property. Compared
  4346. * to the connector specific ioctl this one is extended to also work on crtc and
  4347. * plane objects.
  4348. *
  4349. * Called by the user via ioctl.
  4350. *
  4351. * Returns:
  4352. * Zero on success, negative errno on failure.
  4353. */
  4354. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  4355. struct drm_file *file_priv)
  4356. {
  4357. struct drm_mode_obj_get_properties *arg = data;
  4358. struct drm_mode_object *obj;
  4359. int ret = 0;
  4360. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4361. return -EINVAL;
  4362. drm_modeset_lock_all(dev);
  4363. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4364. if (!obj) {
  4365. ret = -ENOENT;
  4366. goto out;
  4367. }
  4368. if (!obj->properties) {
  4369. ret = -EINVAL;
  4370. goto out;
  4371. }
  4372. ret = get_properties(obj, file_priv->atomic,
  4373. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  4374. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  4375. &arg->count_props);
  4376. out:
  4377. drm_modeset_unlock_all(dev);
  4378. return ret;
  4379. }
  4380. /**
  4381. * drm_mode_obj_set_property_ioctl - set the current value of an object's property
  4382. * @dev: DRM device
  4383. * @data: ioctl data
  4384. * @file_priv: DRM file info
  4385. *
  4386. * This function sets the current value for an object's property. It also calls
  4387. * into a driver's ->set_property callback to update the hardware state.
  4388. * Compared to the connector specific ioctl this one is extended to also work on
  4389. * crtc and plane objects.
  4390. *
  4391. * Called by the user via ioctl.
  4392. *
  4393. * Returns:
  4394. * Zero on success, negative errno on failure.
  4395. */
  4396. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  4397. struct drm_file *file_priv)
  4398. {
  4399. struct drm_mode_obj_set_property *arg = data;
  4400. struct drm_mode_object *arg_obj;
  4401. struct drm_mode_object *prop_obj;
  4402. struct drm_property *property;
  4403. int i, ret = -EINVAL;
  4404. struct drm_mode_object *ref;
  4405. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4406. return -EINVAL;
  4407. drm_modeset_lock_all(dev);
  4408. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4409. if (!arg_obj) {
  4410. ret = -ENOENT;
  4411. goto out;
  4412. }
  4413. if (!arg_obj->properties)
  4414. goto out;
  4415. for (i = 0; i < arg_obj->properties->count; i++)
  4416. if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
  4417. break;
  4418. if (i == arg_obj->properties->count)
  4419. goto out;
  4420. prop_obj = drm_mode_object_find(dev, arg->prop_id,
  4421. DRM_MODE_OBJECT_PROPERTY);
  4422. if (!prop_obj) {
  4423. ret = -ENOENT;
  4424. goto out;
  4425. }
  4426. property = obj_to_property(prop_obj);
  4427. if (!drm_property_change_valid_get(property, arg->value, &ref))
  4428. goto out;
  4429. switch (arg_obj->type) {
  4430. case DRM_MODE_OBJECT_CONNECTOR:
  4431. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  4432. arg->value);
  4433. break;
  4434. case DRM_MODE_OBJECT_CRTC:
  4435. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  4436. break;
  4437. case DRM_MODE_OBJECT_PLANE:
  4438. ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
  4439. property, arg->value);
  4440. break;
  4441. }
  4442. drm_property_change_valid_put(property, ref);
  4443. out:
  4444. drm_modeset_unlock_all(dev);
  4445. return ret;
  4446. }
  4447. /**
  4448. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  4449. * @connector: connector to attach
  4450. * @encoder: encoder to attach @connector to
  4451. *
  4452. * This function links up a connector to an encoder. Note that the routing
  4453. * restrictions between encoders and crtcs are exposed to userspace through the
  4454. * possible_clones and possible_crtcs bitmasks.
  4455. *
  4456. * Returns:
  4457. * Zero on success, negative errno on failure.
  4458. */
  4459. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  4460. struct drm_encoder *encoder)
  4461. {
  4462. int i;
  4463. /*
  4464. * In the past, drivers have attempted to model the static association
  4465. * of connector to encoder in simple connector/encoder devices using a
  4466. * direct assignment of connector->encoder = encoder. This connection
  4467. * is a logical one and the responsibility of the core, so drivers are
  4468. * expected not to mess with this.
  4469. *
  4470. * Note that the error return should've been enough here, but a large
  4471. * majority of drivers ignores the return value, so add in a big WARN
  4472. * to get people's attention.
  4473. */
  4474. if (WARN_ON(connector->encoder))
  4475. return -EINVAL;
  4476. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  4477. if (connector->encoder_ids[i] == 0) {
  4478. connector->encoder_ids[i] = encoder->base.id;
  4479. return 0;
  4480. }
  4481. }
  4482. return -ENOMEM;
  4483. }
  4484. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  4485. /**
  4486. * drm_mode_crtc_set_gamma_size - set the gamma table size
  4487. * @crtc: CRTC to set the gamma table size for
  4488. * @gamma_size: size of the gamma table
  4489. *
  4490. * Drivers which support gamma tables should set this to the supported gamma
  4491. * table size when initializing the CRTC. Currently the drm core only supports a
  4492. * fixed gamma table size.
  4493. *
  4494. * Returns:
  4495. * Zero on success, negative errno on failure.
  4496. */
  4497. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  4498. int gamma_size)
  4499. {
  4500. crtc->gamma_size = gamma_size;
  4501. crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
  4502. GFP_KERNEL);
  4503. if (!crtc->gamma_store) {
  4504. crtc->gamma_size = 0;
  4505. return -ENOMEM;
  4506. }
  4507. return 0;
  4508. }
  4509. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  4510. /**
  4511. * drm_mode_gamma_set_ioctl - set the gamma table
  4512. * @dev: DRM device
  4513. * @data: ioctl data
  4514. * @file_priv: DRM file info
  4515. *
  4516. * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
  4517. * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
  4518. *
  4519. * Called by the user via ioctl.
  4520. *
  4521. * Returns:
  4522. * Zero on success, negative errno on failure.
  4523. */
  4524. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  4525. void *data, struct drm_file *file_priv)
  4526. {
  4527. struct drm_mode_crtc_lut *crtc_lut = data;
  4528. struct drm_crtc *crtc;
  4529. void *r_base, *g_base, *b_base;
  4530. int size;
  4531. int ret = 0;
  4532. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4533. return -EINVAL;
  4534. drm_modeset_lock_all(dev);
  4535. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4536. if (!crtc) {
  4537. ret = -ENOENT;
  4538. goto out;
  4539. }
  4540. if (crtc->funcs->gamma_set == NULL) {
  4541. ret = -ENOSYS;
  4542. goto out;
  4543. }
  4544. /* memcpy into gamma store */
  4545. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4546. ret = -EINVAL;
  4547. goto out;
  4548. }
  4549. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4550. r_base = crtc->gamma_store;
  4551. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  4552. ret = -EFAULT;
  4553. goto out;
  4554. }
  4555. g_base = r_base + size;
  4556. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  4557. ret = -EFAULT;
  4558. goto out;
  4559. }
  4560. b_base = g_base + size;
  4561. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  4562. ret = -EFAULT;
  4563. goto out;
  4564. }
  4565. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  4566. out:
  4567. drm_modeset_unlock_all(dev);
  4568. return ret;
  4569. }
  4570. /**
  4571. * drm_mode_gamma_get_ioctl - get the gamma table
  4572. * @dev: DRM device
  4573. * @data: ioctl data
  4574. * @file_priv: DRM file info
  4575. *
  4576. * Copy the current gamma table into the storage provided. This also provides
  4577. * the gamma table size the driver expects, which can be used to size the
  4578. * allocated storage.
  4579. *
  4580. * Called by the user via ioctl.
  4581. *
  4582. * Returns:
  4583. * Zero on success, negative errno on failure.
  4584. */
  4585. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  4586. void *data, struct drm_file *file_priv)
  4587. {
  4588. struct drm_mode_crtc_lut *crtc_lut = data;
  4589. struct drm_crtc *crtc;
  4590. void *r_base, *g_base, *b_base;
  4591. int size;
  4592. int ret = 0;
  4593. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4594. return -EINVAL;
  4595. drm_modeset_lock_all(dev);
  4596. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4597. if (!crtc) {
  4598. ret = -ENOENT;
  4599. goto out;
  4600. }
  4601. /* memcpy into gamma store */
  4602. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4603. ret = -EINVAL;
  4604. goto out;
  4605. }
  4606. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4607. r_base = crtc->gamma_store;
  4608. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  4609. ret = -EFAULT;
  4610. goto out;
  4611. }
  4612. g_base = r_base + size;
  4613. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  4614. ret = -EFAULT;
  4615. goto out;
  4616. }
  4617. b_base = g_base + size;
  4618. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  4619. ret = -EFAULT;
  4620. goto out;
  4621. }
  4622. out:
  4623. drm_modeset_unlock_all(dev);
  4624. return ret;
  4625. }
  4626. /**
  4627. * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
  4628. * @dev: DRM device
  4629. * @data: ioctl data
  4630. * @file_priv: DRM file info
  4631. *
  4632. * This schedules an asynchronous update on a given CRTC, called page flip.
  4633. * Optionally a drm event is generated to signal the completion of the event.
  4634. * Generic drivers cannot assume that a pageflip with changed framebuffer
  4635. * properties (including driver specific metadata like tiling layout) will work,
  4636. * but some drivers support e.g. pixel format changes through the pageflip
  4637. * ioctl.
  4638. *
  4639. * Called by the user via ioctl.
  4640. *
  4641. * Returns:
  4642. * Zero on success, negative errno on failure.
  4643. */
  4644. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  4645. void *data, struct drm_file *file_priv)
  4646. {
  4647. struct drm_mode_crtc_page_flip *page_flip = data;
  4648. struct drm_crtc *crtc;
  4649. struct drm_framebuffer *fb = NULL;
  4650. struct drm_pending_vblank_event *e = NULL;
  4651. int ret = -EINVAL;
  4652. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  4653. page_flip->reserved != 0)
  4654. return -EINVAL;
  4655. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  4656. return -EINVAL;
  4657. crtc = drm_crtc_find(dev, page_flip->crtc_id);
  4658. if (!crtc)
  4659. return -ENOENT;
  4660. drm_modeset_lock_crtc(crtc, crtc->primary);
  4661. if (crtc->primary->fb == NULL) {
  4662. /* The framebuffer is currently unbound, presumably
  4663. * due to a hotplug event, that userspace has not
  4664. * yet discovered.
  4665. */
  4666. ret = -EBUSY;
  4667. goto out;
  4668. }
  4669. if (crtc->funcs->page_flip == NULL)
  4670. goto out;
  4671. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  4672. if (!fb) {
  4673. ret = -ENOENT;
  4674. goto out;
  4675. }
  4676. if (crtc->state) {
  4677. const struct drm_plane_state *state = crtc->primary->state;
  4678. ret = check_src_coords(state->src_x, state->src_y,
  4679. state->src_w, state->src_h, fb);
  4680. } else {
  4681. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  4682. }
  4683. if (ret)
  4684. goto out;
  4685. if (crtc->primary->fb->pixel_format != fb->pixel_format) {
  4686. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  4687. ret = -EINVAL;
  4688. goto out;
  4689. }
  4690. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4691. e = kzalloc(sizeof *e, GFP_KERNEL);
  4692. if (!e) {
  4693. ret = -ENOMEM;
  4694. goto out;
  4695. }
  4696. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  4697. e->event.base.length = sizeof(e->event);
  4698. e->event.user_data = page_flip->user_data;
  4699. ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
  4700. if (ret) {
  4701. kfree(e);
  4702. goto out;
  4703. }
  4704. }
  4705. crtc->primary->old_fb = crtc->primary->fb;
  4706. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  4707. if (ret) {
  4708. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
  4709. drm_event_cancel_free(dev, &e->base);
  4710. /* Keep the old fb, don't unref it. */
  4711. crtc->primary->old_fb = NULL;
  4712. } else {
  4713. crtc->primary->fb = fb;
  4714. /* Unref only the old framebuffer. */
  4715. fb = NULL;
  4716. }
  4717. out:
  4718. if (fb)
  4719. drm_framebuffer_unreference(fb);
  4720. if (crtc->primary->old_fb)
  4721. drm_framebuffer_unreference(crtc->primary->old_fb);
  4722. crtc->primary->old_fb = NULL;
  4723. drm_modeset_unlock_crtc(crtc);
  4724. return ret;
  4725. }
  4726. /**
  4727. * drm_mode_config_reset - call ->reset callbacks
  4728. * @dev: drm device
  4729. *
  4730. * This functions calls all the crtc's, encoder's and connector's ->reset
  4731. * callback. Drivers can use this in e.g. their driver load or resume code to
  4732. * reset hardware and software state.
  4733. */
  4734. void drm_mode_config_reset(struct drm_device *dev)
  4735. {
  4736. struct drm_crtc *crtc;
  4737. struct drm_plane *plane;
  4738. struct drm_encoder *encoder;
  4739. struct drm_connector *connector;
  4740. drm_for_each_plane(plane, dev)
  4741. if (plane->funcs->reset)
  4742. plane->funcs->reset(plane);
  4743. drm_for_each_crtc(crtc, dev)
  4744. if (crtc->funcs->reset)
  4745. crtc->funcs->reset(crtc);
  4746. drm_for_each_encoder(encoder, dev)
  4747. if (encoder->funcs->reset)
  4748. encoder->funcs->reset(encoder);
  4749. mutex_lock(&dev->mode_config.mutex);
  4750. drm_for_each_connector(connector, dev)
  4751. if (connector->funcs->reset)
  4752. connector->funcs->reset(connector);
  4753. mutex_unlock(&dev->mode_config.mutex);
  4754. }
  4755. EXPORT_SYMBOL(drm_mode_config_reset);
  4756. /**
  4757. * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
  4758. * @dev: DRM device
  4759. * @data: ioctl data
  4760. * @file_priv: DRM file info
  4761. *
  4762. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  4763. * TTM or something else entirely) and returns the resulting buffer handle. This
  4764. * handle can then be wrapped up into a framebuffer modeset object.
  4765. *
  4766. * Note that userspace is not allowed to use such objects for render
  4767. * acceleration - drivers must create their own private ioctls for such a use
  4768. * case.
  4769. *
  4770. * Called by the user via ioctl.
  4771. *
  4772. * Returns:
  4773. * Zero on success, negative errno on failure.
  4774. */
  4775. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  4776. void *data, struct drm_file *file_priv)
  4777. {
  4778. struct drm_mode_create_dumb *args = data;
  4779. u32 cpp, stride, size;
  4780. if (!dev->driver->dumb_create)
  4781. return -ENOSYS;
  4782. if (!args->width || !args->height || !args->bpp)
  4783. return -EINVAL;
  4784. /* overflow checks for 32bit size calculations */
  4785. /* NOTE: DIV_ROUND_UP() can overflow */
  4786. cpp = DIV_ROUND_UP(args->bpp, 8);
  4787. if (!cpp || cpp > 0xffffffffU / args->width)
  4788. return -EINVAL;
  4789. stride = cpp * args->width;
  4790. if (args->height > 0xffffffffU / stride)
  4791. return -EINVAL;
  4792. /* test for wrap-around */
  4793. size = args->height * stride;
  4794. if (PAGE_ALIGN(size) == 0)
  4795. return -EINVAL;
  4796. /*
  4797. * handle, pitch and size are output parameters. Zero them out to
  4798. * prevent drivers from accidentally using uninitialized data. Since
  4799. * not all existing userspace is clearing these fields properly we
  4800. * cannot reject IOCTL with garbage in them.
  4801. */
  4802. args->handle = 0;
  4803. args->pitch = 0;
  4804. args->size = 0;
  4805. return dev->driver->dumb_create(file_priv, dev, args);
  4806. }
  4807. /**
  4808. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  4809. * @dev: DRM device
  4810. * @data: ioctl data
  4811. * @file_priv: DRM file info
  4812. *
  4813. * Allocate an offset in the drm device node's address space to be able to
  4814. * memory map a dumb buffer.
  4815. *
  4816. * Called by the user via ioctl.
  4817. *
  4818. * Returns:
  4819. * Zero on success, negative errno on failure.
  4820. */
  4821. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  4822. void *data, struct drm_file *file_priv)
  4823. {
  4824. struct drm_mode_map_dumb *args = data;
  4825. /* call driver ioctl to get mmap offset */
  4826. if (!dev->driver->dumb_map_offset)
  4827. return -ENOSYS;
  4828. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  4829. }
  4830. /**
  4831. * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
  4832. * @dev: DRM device
  4833. * @data: ioctl data
  4834. * @file_priv: DRM file info
  4835. *
  4836. * This destroys the userspace handle for the given dumb backing storage buffer.
  4837. * Since buffer objects must be reference counted in the kernel a buffer object
  4838. * won't be immediately freed if a framebuffer modeset object still uses it.
  4839. *
  4840. * Called by the user via ioctl.
  4841. *
  4842. * Returns:
  4843. * Zero on success, negative errno on failure.
  4844. */
  4845. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  4846. void *data, struct drm_file *file_priv)
  4847. {
  4848. struct drm_mode_destroy_dumb *args = data;
  4849. if (!dev->driver->dumb_destroy)
  4850. return -ENOSYS;
  4851. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  4852. }
  4853. /**
  4854. * drm_fb_get_bpp_depth - get the bpp/depth values for format
  4855. * @format: pixel format (DRM_FORMAT_*)
  4856. * @depth: storage for the depth value
  4857. * @bpp: storage for the bpp value
  4858. *
  4859. * This only supports RGB formats here for compat with code that doesn't use
  4860. * pixel formats directly yet.
  4861. */
  4862. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  4863. int *bpp)
  4864. {
  4865. switch (format) {
  4866. case DRM_FORMAT_C8:
  4867. case DRM_FORMAT_RGB332:
  4868. case DRM_FORMAT_BGR233:
  4869. *depth = 8;
  4870. *bpp = 8;
  4871. break;
  4872. case DRM_FORMAT_XRGB1555:
  4873. case DRM_FORMAT_XBGR1555:
  4874. case DRM_FORMAT_RGBX5551:
  4875. case DRM_FORMAT_BGRX5551:
  4876. case DRM_FORMAT_ARGB1555:
  4877. case DRM_FORMAT_ABGR1555:
  4878. case DRM_FORMAT_RGBA5551:
  4879. case DRM_FORMAT_BGRA5551:
  4880. *depth = 15;
  4881. *bpp = 16;
  4882. break;
  4883. case DRM_FORMAT_RGB565:
  4884. case DRM_FORMAT_BGR565:
  4885. *depth = 16;
  4886. *bpp = 16;
  4887. break;
  4888. case DRM_FORMAT_RGB888:
  4889. case DRM_FORMAT_BGR888:
  4890. *depth = 24;
  4891. *bpp = 24;
  4892. break;
  4893. case DRM_FORMAT_XRGB8888:
  4894. case DRM_FORMAT_XBGR8888:
  4895. case DRM_FORMAT_RGBX8888:
  4896. case DRM_FORMAT_BGRX8888:
  4897. *depth = 24;
  4898. *bpp = 32;
  4899. break;
  4900. case DRM_FORMAT_XRGB2101010:
  4901. case DRM_FORMAT_XBGR2101010:
  4902. case DRM_FORMAT_RGBX1010102:
  4903. case DRM_FORMAT_BGRX1010102:
  4904. case DRM_FORMAT_ARGB2101010:
  4905. case DRM_FORMAT_ABGR2101010:
  4906. case DRM_FORMAT_RGBA1010102:
  4907. case DRM_FORMAT_BGRA1010102:
  4908. *depth = 30;
  4909. *bpp = 32;
  4910. break;
  4911. case DRM_FORMAT_ARGB8888:
  4912. case DRM_FORMAT_ABGR8888:
  4913. case DRM_FORMAT_RGBA8888:
  4914. case DRM_FORMAT_BGRA8888:
  4915. *depth = 32;
  4916. *bpp = 32;
  4917. break;
  4918. default:
  4919. DRM_DEBUG_KMS("unsupported pixel format %s\n",
  4920. drm_get_format_name(format));
  4921. *depth = 0;
  4922. *bpp = 0;
  4923. break;
  4924. }
  4925. }
  4926. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  4927. /**
  4928. * drm_format_num_planes - get the number of planes for format
  4929. * @format: pixel format (DRM_FORMAT_*)
  4930. *
  4931. * Returns:
  4932. * The number of planes used by the specified pixel format.
  4933. */
  4934. int drm_format_num_planes(uint32_t format)
  4935. {
  4936. switch (format) {
  4937. case DRM_FORMAT_YUV410:
  4938. case DRM_FORMAT_YVU410:
  4939. case DRM_FORMAT_YUV411:
  4940. case DRM_FORMAT_YVU411:
  4941. case DRM_FORMAT_YUV420:
  4942. case DRM_FORMAT_YVU420:
  4943. case DRM_FORMAT_YUV422:
  4944. case DRM_FORMAT_YVU422:
  4945. case DRM_FORMAT_YUV444:
  4946. case DRM_FORMAT_YVU444:
  4947. return 3;
  4948. case DRM_FORMAT_NV12:
  4949. case DRM_FORMAT_NV21:
  4950. case DRM_FORMAT_NV16:
  4951. case DRM_FORMAT_NV61:
  4952. case DRM_FORMAT_NV24:
  4953. case DRM_FORMAT_NV42:
  4954. return 2;
  4955. default:
  4956. return 1;
  4957. }
  4958. }
  4959. EXPORT_SYMBOL(drm_format_num_planes);
  4960. /**
  4961. * drm_format_plane_cpp - determine the bytes per pixel value
  4962. * @format: pixel format (DRM_FORMAT_*)
  4963. * @plane: plane index
  4964. *
  4965. * Returns:
  4966. * The bytes per pixel value for the specified plane.
  4967. */
  4968. int drm_format_plane_cpp(uint32_t format, int plane)
  4969. {
  4970. unsigned int depth;
  4971. int bpp;
  4972. if (plane >= drm_format_num_planes(format))
  4973. return 0;
  4974. switch (format) {
  4975. case DRM_FORMAT_YUYV:
  4976. case DRM_FORMAT_YVYU:
  4977. case DRM_FORMAT_UYVY:
  4978. case DRM_FORMAT_VYUY:
  4979. return 2;
  4980. case DRM_FORMAT_NV12:
  4981. case DRM_FORMAT_NV21:
  4982. case DRM_FORMAT_NV16:
  4983. case DRM_FORMAT_NV61:
  4984. case DRM_FORMAT_NV24:
  4985. case DRM_FORMAT_NV42:
  4986. return plane ? 2 : 1;
  4987. case DRM_FORMAT_YUV410:
  4988. case DRM_FORMAT_YVU410:
  4989. case DRM_FORMAT_YUV411:
  4990. case DRM_FORMAT_YVU411:
  4991. case DRM_FORMAT_YUV420:
  4992. case DRM_FORMAT_YVU420:
  4993. case DRM_FORMAT_YUV422:
  4994. case DRM_FORMAT_YVU422:
  4995. case DRM_FORMAT_YUV444:
  4996. case DRM_FORMAT_YVU444:
  4997. return 1;
  4998. default:
  4999. drm_fb_get_bpp_depth(format, &depth, &bpp);
  5000. return bpp >> 3;
  5001. }
  5002. }
  5003. EXPORT_SYMBOL(drm_format_plane_cpp);
  5004. /**
  5005. * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  5006. * @format: pixel format (DRM_FORMAT_*)
  5007. *
  5008. * Returns:
  5009. * The horizontal chroma subsampling factor for the
  5010. * specified pixel format.
  5011. */
  5012. int drm_format_horz_chroma_subsampling(uint32_t format)
  5013. {
  5014. switch (format) {
  5015. case DRM_FORMAT_YUV411:
  5016. case DRM_FORMAT_YVU411:
  5017. case DRM_FORMAT_YUV410:
  5018. case DRM_FORMAT_YVU410:
  5019. return 4;
  5020. case DRM_FORMAT_YUYV:
  5021. case DRM_FORMAT_YVYU:
  5022. case DRM_FORMAT_UYVY:
  5023. case DRM_FORMAT_VYUY:
  5024. case DRM_FORMAT_NV12:
  5025. case DRM_FORMAT_NV21:
  5026. case DRM_FORMAT_NV16:
  5027. case DRM_FORMAT_NV61:
  5028. case DRM_FORMAT_YUV422:
  5029. case DRM_FORMAT_YVU422:
  5030. case DRM_FORMAT_YUV420:
  5031. case DRM_FORMAT_YVU420:
  5032. return 2;
  5033. default:
  5034. return 1;
  5035. }
  5036. }
  5037. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  5038. /**
  5039. * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  5040. * @format: pixel format (DRM_FORMAT_*)
  5041. *
  5042. * Returns:
  5043. * The vertical chroma subsampling factor for the
  5044. * specified pixel format.
  5045. */
  5046. int drm_format_vert_chroma_subsampling(uint32_t format)
  5047. {
  5048. switch (format) {
  5049. case DRM_FORMAT_YUV410:
  5050. case DRM_FORMAT_YVU410:
  5051. return 4;
  5052. case DRM_FORMAT_YUV420:
  5053. case DRM_FORMAT_YVU420:
  5054. case DRM_FORMAT_NV12:
  5055. case DRM_FORMAT_NV21:
  5056. return 2;
  5057. default:
  5058. return 1;
  5059. }
  5060. }
  5061. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  5062. /**
  5063. * drm_format_plane_width - width of the plane given the first plane
  5064. * @width: width of the first plane
  5065. * @format: pixel format
  5066. * @plane: plane index
  5067. *
  5068. * Returns:
  5069. * The width of @plane, given that the width of the first plane is @width.
  5070. */
  5071. int drm_format_plane_width(int width, uint32_t format, int plane)
  5072. {
  5073. if (plane >= drm_format_num_planes(format))
  5074. return 0;
  5075. if (plane == 0)
  5076. return width;
  5077. return width / drm_format_horz_chroma_subsampling(format);
  5078. }
  5079. EXPORT_SYMBOL(drm_format_plane_width);
  5080. /**
  5081. * drm_format_plane_height - height of the plane given the first plane
  5082. * @height: height of the first plane
  5083. * @format: pixel format
  5084. * @plane: plane index
  5085. *
  5086. * Returns:
  5087. * The height of @plane, given that the height of the first plane is @height.
  5088. */
  5089. int drm_format_plane_height(int height, uint32_t format, int plane)
  5090. {
  5091. if (plane >= drm_format_num_planes(format))
  5092. return 0;
  5093. if (plane == 0)
  5094. return height;
  5095. return height / drm_format_vert_chroma_subsampling(format);
  5096. }
  5097. EXPORT_SYMBOL(drm_format_plane_height);
  5098. /**
  5099. * drm_rotation_simplify() - Try to simplify the rotation
  5100. * @rotation: Rotation to be simplified
  5101. * @supported_rotations: Supported rotations
  5102. *
  5103. * Attempt to simplify the rotation to a form that is supported.
  5104. * Eg. if the hardware supports everything except DRM_REFLECT_X
  5105. * one could call this function like this:
  5106. *
  5107. * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
  5108. * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
  5109. * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
  5110. *
  5111. * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
  5112. * transforms the hardware supports, this function may not
  5113. * be able to produce a supported transform, so the caller should
  5114. * check the result afterwards.
  5115. */
  5116. unsigned int drm_rotation_simplify(unsigned int rotation,
  5117. unsigned int supported_rotations)
  5118. {
  5119. if (rotation & ~supported_rotations) {
  5120. rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
  5121. rotation = (rotation & DRM_REFLECT_MASK) |
  5122. BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
  5123. }
  5124. return rotation;
  5125. }
  5126. EXPORT_SYMBOL(drm_rotation_simplify);
  5127. /**
  5128. * drm_mode_config_init - initialize DRM mode_configuration structure
  5129. * @dev: DRM device
  5130. *
  5131. * Initialize @dev's mode_config structure, used for tracking the graphics
  5132. * configuration of @dev.
  5133. *
  5134. * Since this initializes the modeset locks, no locking is possible. Which is no
  5135. * problem, since this should happen single threaded at init time. It is the
  5136. * driver's problem to ensure this guarantee.
  5137. *
  5138. */
  5139. void drm_mode_config_init(struct drm_device *dev)
  5140. {
  5141. mutex_init(&dev->mode_config.mutex);
  5142. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  5143. mutex_init(&dev->mode_config.idr_mutex);
  5144. mutex_init(&dev->mode_config.fb_lock);
  5145. mutex_init(&dev->mode_config.blob_lock);
  5146. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  5147. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  5148. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  5149. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  5150. INIT_LIST_HEAD(&dev->mode_config.property_list);
  5151. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  5152. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  5153. idr_init(&dev->mode_config.crtc_idr);
  5154. idr_init(&dev->mode_config.tile_idr);
  5155. ida_init(&dev->mode_config.connector_ida);
  5156. drm_modeset_lock_all(dev);
  5157. drm_mode_create_standard_properties(dev);
  5158. drm_modeset_unlock_all(dev);
  5159. /* Just to be sure */
  5160. dev->mode_config.num_fb = 0;
  5161. dev->mode_config.num_connector = 0;
  5162. dev->mode_config.num_crtc = 0;
  5163. dev->mode_config.num_encoder = 0;
  5164. dev->mode_config.num_overlay_plane = 0;
  5165. dev->mode_config.num_total_plane = 0;
  5166. }
  5167. EXPORT_SYMBOL(drm_mode_config_init);
  5168. /**
  5169. * drm_mode_config_cleanup - free up DRM mode_config info
  5170. * @dev: DRM device
  5171. *
  5172. * Free up all the connectors and CRTCs associated with this DRM device, then
  5173. * free up the framebuffers and associated buffer objects.
  5174. *
  5175. * Note that since this /should/ happen single-threaded at driver/device
  5176. * teardown time, no locking is required. It's the driver's job to ensure that
  5177. * this guarantee actually holds true.
  5178. *
  5179. * FIXME: cleanup any dangling user buffer objects too
  5180. */
  5181. void drm_mode_config_cleanup(struct drm_device *dev)
  5182. {
  5183. struct drm_connector *connector, *ot;
  5184. struct drm_crtc *crtc, *ct;
  5185. struct drm_encoder *encoder, *enct;
  5186. struct drm_framebuffer *fb, *fbt;
  5187. struct drm_property *property, *pt;
  5188. struct drm_property_blob *blob, *bt;
  5189. struct drm_plane *plane, *plt;
  5190. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  5191. head) {
  5192. encoder->funcs->destroy(encoder);
  5193. }
  5194. list_for_each_entry_safe(connector, ot,
  5195. &dev->mode_config.connector_list, head) {
  5196. connector->funcs->destroy(connector);
  5197. }
  5198. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  5199. head) {
  5200. drm_property_destroy(dev, property);
  5201. }
  5202. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  5203. head) {
  5204. plane->funcs->destroy(plane);
  5205. }
  5206. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  5207. crtc->funcs->destroy(crtc);
  5208. }
  5209. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  5210. head_global) {
  5211. drm_property_unreference_blob(blob);
  5212. }
  5213. /*
  5214. * Single-threaded teardown context, so it's not required to grab the
  5215. * fb_lock to protect against concurrent fb_list access. Contrary, it
  5216. * would actually deadlock with the drm_framebuffer_cleanup function.
  5217. *
  5218. * Also, if there are any framebuffers left, that's a driver leak now,
  5219. * so politely WARN about this.
  5220. */
  5221. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  5222. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  5223. drm_framebuffer_free(&fb->refcount);
  5224. }
  5225. ida_destroy(&dev->mode_config.connector_ida);
  5226. idr_destroy(&dev->mode_config.tile_idr);
  5227. idr_destroy(&dev->mode_config.crtc_idr);
  5228. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  5229. }
  5230. EXPORT_SYMBOL(drm_mode_config_cleanup);
  5231. struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
  5232. unsigned int supported_rotations)
  5233. {
  5234. static const struct drm_prop_enum_list props[] = {
  5235. { DRM_ROTATE_0, "rotate-0" },
  5236. { DRM_ROTATE_90, "rotate-90" },
  5237. { DRM_ROTATE_180, "rotate-180" },
  5238. { DRM_ROTATE_270, "rotate-270" },
  5239. { DRM_REFLECT_X, "reflect-x" },
  5240. { DRM_REFLECT_Y, "reflect-y" },
  5241. };
  5242. return drm_property_create_bitmask(dev, 0, "rotation",
  5243. props, ARRAY_SIZE(props),
  5244. supported_rotations);
  5245. }
  5246. EXPORT_SYMBOL(drm_mode_create_rotation_property);
  5247. /**
  5248. * DOC: Tile group
  5249. *
  5250. * Tile groups are used to represent tiled monitors with a unique
  5251. * integer identifier. Tiled monitors using DisplayID v1.3 have
  5252. * a unique 8-byte handle, we store this in a tile group, so we
  5253. * have a common identifier for all tiles in a monitor group.
  5254. */
  5255. static void drm_tile_group_free(struct kref *kref)
  5256. {
  5257. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  5258. struct drm_device *dev = tg->dev;
  5259. mutex_lock(&dev->mode_config.idr_mutex);
  5260. idr_remove(&dev->mode_config.tile_idr, tg->id);
  5261. mutex_unlock(&dev->mode_config.idr_mutex);
  5262. kfree(tg);
  5263. }
  5264. /**
  5265. * drm_mode_put_tile_group - drop a reference to a tile group.
  5266. * @dev: DRM device
  5267. * @tg: tile group to drop reference to.
  5268. *
  5269. * drop reference to tile group and free if 0.
  5270. */
  5271. void drm_mode_put_tile_group(struct drm_device *dev,
  5272. struct drm_tile_group *tg)
  5273. {
  5274. kref_put(&tg->refcount, drm_tile_group_free);
  5275. }
  5276. /**
  5277. * drm_mode_get_tile_group - get a reference to an existing tile group
  5278. * @dev: DRM device
  5279. * @topology: 8-bytes unique per monitor.
  5280. *
  5281. * Use the unique bytes to get a reference to an existing tile group.
  5282. *
  5283. * RETURNS:
  5284. * tile group or NULL if not found.
  5285. */
  5286. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  5287. char topology[8])
  5288. {
  5289. struct drm_tile_group *tg;
  5290. int id;
  5291. mutex_lock(&dev->mode_config.idr_mutex);
  5292. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  5293. if (!memcmp(tg->group_data, topology, 8)) {
  5294. if (!kref_get_unless_zero(&tg->refcount))
  5295. tg = NULL;
  5296. mutex_unlock(&dev->mode_config.idr_mutex);
  5297. return tg;
  5298. }
  5299. }
  5300. mutex_unlock(&dev->mode_config.idr_mutex);
  5301. return NULL;
  5302. }
  5303. EXPORT_SYMBOL(drm_mode_get_tile_group);
  5304. /**
  5305. * drm_mode_create_tile_group - create a tile group from a displayid description
  5306. * @dev: DRM device
  5307. * @topology: 8-bytes unique per monitor.
  5308. *
  5309. * Create a tile group for the unique monitor, and get a unique
  5310. * identifier for the tile group.
  5311. *
  5312. * RETURNS:
  5313. * new tile group or error.
  5314. */
  5315. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  5316. char topology[8])
  5317. {
  5318. struct drm_tile_group *tg;
  5319. int ret;
  5320. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  5321. if (!tg)
  5322. return ERR_PTR(-ENOMEM);
  5323. kref_init(&tg->refcount);
  5324. memcpy(tg->group_data, topology, 8);
  5325. tg->dev = dev;
  5326. mutex_lock(&dev->mode_config.idr_mutex);
  5327. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  5328. if (ret >= 0) {
  5329. tg->id = ret;
  5330. } else {
  5331. kfree(tg);
  5332. tg = ERR_PTR(ret);
  5333. }
  5334. mutex_unlock(&dev->mode_config.idr_mutex);
  5335. return tg;
  5336. }
  5337. EXPORT_SYMBOL(drm_mode_create_tile_group);