drm_crtc.c 159 KB

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