drm_crtc.c 160 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959
  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. 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. /*
  2320. * Check whether the primary plane supports the fb pixel format.
  2321. * Drivers not implementing the universal planes API use a
  2322. * default formats list provided by the DRM core which doesn't
  2323. * match real hardware capabilities. Skip the check in that
  2324. * case.
  2325. */
  2326. if (!crtc->primary->format_default) {
  2327. ret = drm_plane_check_pixel_format(crtc->primary,
  2328. fb->pixel_format);
  2329. if (ret) {
  2330. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2331. drm_get_format_name(fb->pixel_format));
  2332. goto out;
  2333. }
  2334. }
  2335. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2336. mode, fb);
  2337. if (ret)
  2338. goto out;
  2339. }
  2340. if (crtc_req->count_connectors == 0 && mode) {
  2341. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2342. ret = -EINVAL;
  2343. goto out;
  2344. }
  2345. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2346. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2347. crtc_req->count_connectors);
  2348. ret = -EINVAL;
  2349. goto out;
  2350. }
  2351. if (crtc_req->count_connectors > 0) {
  2352. u32 out_id;
  2353. /* Avoid unbounded kernel memory allocation */
  2354. if (crtc_req->count_connectors > config->num_connector) {
  2355. ret = -EINVAL;
  2356. goto out;
  2357. }
  2358. connector_set = kmalloc_array(crtc_req->count_connectors,
  2359. sizeof(struct drm_connector *),
  2360. GFP_KERNEL);
  2361. if (!connector_set) {
  2362. ret = -ENOMEM;
  2363. goto out;
  2364. }
  2365. for (i = 0; i < crtc_req->count_connectors; i++) {
  2366. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2367. if (get_user(out_id, &set_connectors_ptr[i])) {
  2368. ret = -EFAULT;
  2369. goto out;
  2370. }
  2371. connector = drm_connector_find(dev, out_id);
  2372. if (!connector) {
  2373. DRM_DEBUG_KMS("Connector id %d unknown\n",
  2374. out_id);
  2375. ret = -ENOENT;
  2376. goto out;
  2377. }
  2378. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2379. connector->base.id,
  2380. connector->name);
  2381. connector_set[i] = connector;
  2382. }
  2383. }
  2384. set.crtc = crtc;
  2385. set.x = crtc_req->x;
  2386. set.y = crtc_req->y;
  2387. set.mode = mode;
  2388. set.connectors = connector_set;
  2389. set.num_connectors = crtc_req->count_connectors;
  2390. set.fb = fb;
  2391. ret = drm_mode_set_config_internal(&set);
  2392. out:
  2393. if (fb)
  2394. drm_framebuffer_unreference(fb);
  2395. kfree(connector_set);
  2396. drm_mode_destroy(dev, mode);
  2397. drm_modeset_unlock_all(dev);
  2398. return ret;
  2399. }
  2400. /**
  2401. * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
  2402. * universal plane handler call
  2403. * @crtc: crtc to update cursor for
  2404. * @req: data pointer for the ioctl
  2405. * @file_priv: drm file for the ioctl call
  2406. *
  2407. * Legacy cursor ioctl's work directly with driver buffer handles. To
  2408. * translate legacy ioctl calls into universal plane handler calls, we need to
  2409. * wrap the native buffer handle in a drm_framebuffer.
  2410. *
  2411. * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
  2412. * buffer with a pitch of 4*width; the universal plane interface should be used
  2413. * directly in cases where the hardware can support other buffer settings and
  2414. * userspace wants to make use of these capabilities.
  2415. *
  2416. * Returns:
  2417. * Zero on success, negative errno on failure.
  2418. */
  2419. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  2420. struct drm_mode_cursor2 *req,
  2421. struct drm_file *file_priv)
  2422. {
  2423. struct drm_device *dev = crtc->dev;
  2424. struct drm_framebuffer *fb = NULL;
  2425. struct drm_mode_fb_cmd2 fbreq = {
  2426. .width = req->width,
  2427. .height = req->height,
  2428. .pixel_format = DRM_FORMAT_ARGB8888,
  2429. .pitches = { req->width * 4 },
  2430. .handles = { req->handle },
  2431. };
  2432. int32_t crtc_x, crtc_y;
  2433. uint32_t crtc_w = 0, crtc_h = 0;
  2434. uint32_t src_w = 0, src_h = 0;
  2435. int ret = 0;
  2436. BUG_ON(!crtc->cursor);
  2437. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  2438. /*
  2439. * Obtain fb we'll be using (either new or existing) and take an extra
  2440. * reference to it if fb != null. setplane will take care of dropping
  2441. * the reference if the plane update fails.
  2442. */
  2443. if (req->flags & DRM_MODE_CURSOR_BO) {
  2444. if (req->handle) {
  2445. fb = internal_framebuffer_create(dev, &fbreq, file_priv);
  2446. if (IS_ERR(fb)) {
  2447. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  2448. return PTR_ERR(fb);
  2449. }
  2450. } else {
  2451. fb = NULL;
  2452. }
  2453. } else {
  2454. fb = crtc->cursor->fb;
  2455. if (fb)
  2456. drm_framebuffer_reference(fb);
  2457. }
  2458. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2459. crtc_x = req->x;
  2460. crtc_y = req->y;
  2461. } else {
  2462. crtc_x = crtc->cursor_x;
  2463. crtc_y = crtc->cursor_y;
  2464. }
  2465. if (fb) {
  2466. crtc_w = fb->width;
  2467. crtc_h = fb->height;
  2468. src_w = fb->width << 16;
  2469. src_h = fb->height << 16;
  2470. }
  2471. /*
  2472. * setplane_internal will take care of deref'ing either the old or new
  2473. * framebuffer depending on success.
  2474. */
  2475. ret = __setplane_internal(crtc->cursor, crtc, fb,
  2476. crtc_x, crtc_y, crtc_w, crtc_h,
  2477. 0, 0, src_w, src_h);
  2478. /* Update successful; save new cursor position, if necessary */
  2479. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  2480. crtc->cursor_x = req->x;
  2481. crtc->cursor_y = req->y;
  2482. }
  2483. return ret;
  2484. }
  2485. static int drm_mode_cursor_common(struct drm_device *dev,
  2486. struct drm_mode_cursor2 *req,
  2487. struct drm_file *file_priv)
  2488. {
  2489. struct drm_crtc *crtc;
  2490. int ret = 0;
  2491. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2492. return -EINVAL;
  2493. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2494. return -EINVAL;
  2495. crtc = drm_crtc_find(dev, req->crtc_id);
  2496. if (!crtc) {
  2497. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2498. return -ENOENT;
  2499. }
  2500. /*
  2501. * If this crtc has a universal cursor plane, call that plane's update
  2502. * handler rather than using legacy cursor handlers.
  2503. */
  2504. drm_modeset_lock_crtc(crtc, crtc->cursor);
  2505. if (crtc->cursor) {
  2506. ret = drm_mode_cursor_universal(crtc, req, file_priv);
  2507. goto out;
  2508. }
  2509. if (req->flags & DRM_MODE_CURSOR_BO) {
  2510. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2511. ret = -ENXIO;
  2512. goto out;
  2513. }
  2514. /* Turns off the cursor if handle is 0 */
  2515. if (crtc->funcs->cursor_set2)
  2516. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2517. req->width, req->height, req->hot_x, req->hot_y);
  2518. else
  2519. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2520. req->width, req->height);
  2521. }
  2522. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2523. if (crtc->funcs->cursor_move) {
  2524. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2525. } else {
  2526. ret = -EFAULT;
  2527. goto out;
  2528. }
  2529. }
  2530. out:
  2531. drm_modeset_unlock_crtc(crtc);
  2532. return ret;
  2533. }
  2534. /**
  2535. * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2536. * @dev: drm device for the ioctl
  2537. * @data: data pointer for the ioctl
  2538. * @file_priv: drm file for the ioctl call
  2539. *
  2540. * Set the cursor configuration based on user request.
  2541. *
  2542. * Called by the user via ioctl.
  2543. *
  2544. * Returns:
  2545. * Zero on success, negative errno on failure.
  2546. */
  2547. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2548. void *data, struct drm_file *file_priv)
  2549. {
  2550. struct drm_mode_cursor *req = data;
  2551. struct drm_mode_cursor2 new_req;
  2552. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2553. new_req.hot_x = new_req.hot_y = 0;
  2554. return drm_mode_cursor_common(dev, &new_req, file_priv);
  2555. }
  2556. /**
  2557. * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2558. * @dev: drm device for the ioctl
  2559. * @data: data pointer for the ioctl
  2560. * @file_priv: drm file for the ioctl call
  2561. *
  2562. * Set the cursor configuration based on user request. This implements the 2nd
  2563. * version of the cursor ioctl, which allows userspace to additionally specify
  2564. * the hotspot of the pointer.
  2565. *
  2566. * Called by the user via ioctl.
  2567. *
  2568. * Returns:
  2569. * Zero on success, negative errno on failure.
  2570. */
  2571. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2572. void *data, struct drm_file *file_priv)
  2573. {
  2574. struct drm_mode_cursor2 *req = data;
  2575. return drm_mode_cursor_common(dev, req, file_priv);
  2576. }
  2577. /**
  2578. * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2579. * @bpp: bits per pixels
  2580. * @depth: bit depth per pixel
  2581. *
  2582. * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2583. * Useful in fbdev emulation code, since that deals in those values.
  2584. */
  2585. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2586. {
  2587. uint32_t fmt;
  2588. switch (bpp) {
  2589. case 8:
  2590. fmt = DRM_FORMAT_C8;
  2591. break;
  2592. case 16:
  2593. if (depth == 15)
  2594. fmt = DRM_FORMAT_XRGB1555;
  2595. else
  2596. fmt = DRM_FORMAT_RGB565;
  2597. break;
  2598. case 24:
  2599. fmt = DRM_FORMAT_RGB888;
  2600. break;
  2601. case 32:
  2602. if (depth == 24)
  2603. fmt = DRM_FORMAT_XRGB8888;
  2604. else if (depth == 30)
  2605. fmt = DRM_FORMAT_XRGB2101010;
  2606. else
  2607. fmt = DRM_FORMAT_ARGB8888;
  2608. break;
  2609. default:
  2610. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2611. fmt = DRM_FORMAT_XRGB8888;
  2612. break;
  2613. }
  2614. return fmt;
  2615. }
  2616. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2617. /**
  2618. * drm_mode_addfb - add an FB to the graphics configuration
  2619. * @dev: drm device for the ioctl
  2620. * @data: data pointer for the ioctl
  2621. * @file_priv: drm file for the ioctl call
  2622. *
  2623. * Add a new FB to the specified CRTC, given a user request. This is the
  2624. * original addfb ioctl which only supported RGB formats.
  2625. *
  2626. * Called by the user via ioctl.
  2627. *
  2628. * Returns:
  2629. * Zero on success, negative errno on failure.
  2630. */
  2631. int drm_mode_addfb(struct drm_device *dev,
  2632. void *data, struct drm_file *file_priv)
  2633. {
  2634. struct drm_mode_fb_cmd *or = data;
  2635. struct drm_mode_fb_cmd2 r = {};
  2636. int ret;
  2637. /* convert to new format and call new ioctl */
  2638. r.fb_id = or->fb_id;
  2639. r.width = or->width;
  2640. r.height = or->height;
  2641. r.pitches[0] = or->pitch;
  2642. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2643. r.handles[0] = or->handle;
  2644. ret = drm_mode_addfb2(dev, &r, file_priv);
  2645. if (ret)
  2646. return ret;
  2647. or->fb_id = r.fb_id;
  2648. return 0;
  2649. }
  2650. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2651. {
  2652. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2653. switch (format) {
  2654. case DRM_FORMAT_C8:
  2655. case DRM_FORMAT_RGB332:
  2656. case DRM_FORMAT_BGR233:
  2657. case DRM_FORMAT_XRGB4444:
  2658. case DRM_FORMAT_XBGR4444:
  2659. case DRM_FORMAT_RGBX4444:
  2660. case DRM_FORMAT_BGRX4444:
  2661. case DRM_FORMAT_ARGB4444:
  2662. case DRM_FORMAT_ABGR4444:
  2663. case DRM_FORMAT_RGBA4444:
  2664. case DRM_FORMAT_BGRA4444:
  2665. case DRM_FORMAT_XRGB1555:
  2666. case DRM_FORMAT_XBGR1555:
  2667. case DRM_FORMAT_RGBX5551:
  2668. case DRM_FORMAT_BGRX5551:
  2669. case DRM_FORMAT_ARGB1555:
  2670. case DRM_FORMAT_ABGR1555:
  2671. case DRM_FORMAT_RGBA5551:
  2672. case DRM_FORMAT_BGRA5551:
  2673. case DRM_FORMAT_RGB565:
  2674. case DRM_FORMAT_BGR565:
  2675. case DRM_FORMAT_RGB888:
  2676. case DRM_FORMAT_BGR888:
  2677. case DRM_FORMAT_XRGB8888:
  2678. case DRM_FORMAT_XBGR8888:
  2679. case DRM_FORMAT_RGBX8888:
  2680. case DRM_FORMAT_BGRX8888:
  2681. case DRM_FORMAT_ARGB8888:
  2682. case DRM_FORMAT_ABGR8888:
  2683. case DRM_FORMAT_RGBA8888:
  2684. case DRM_FORMAT_BGRA8888:
  2685. case DRM_FORMAT_XRGB2101010:
  2686. case DRM_FORMAT_XBGR2101010:
  2687. case DRM_FORMAT_RGBX1010102:
  2688. case DRM_FORMAT_BGRX1010102:
  2689. case DRM_FORMAT_ARGB2101010:
  2690. case DRM_FORMAT_ABGR2101010:
  2691. case DRM_FORMAT_RGBA1010102:
  2692. case DRM_FORMAT_BGRA1010102:
  2693. case DRM_FORMAT_YUYV:
  2694. case DRM_FORMAT_YVYU:
  2695. case DRM_FORMAT_UYVY:
  2696. case DRM_FORMAT_VYUY:
  2697. case DRM_FORMAT_AYUV:
  2698. case DRM_FORMAT_NV12:
  2699. case DRM_FORMAT_NV21:
  2700. case DRM_FORMAT_NV16:
  2701. case DRM_FORMAT_NV61:
  2702. case DRM_FORMAT_NV24:
  2703. case DRM_FORMAT_NV42:
  2704. case DRM_FORMAT_YUV410:
  2705. case DRM_FORMAT_YVU410:
  2706. case DRM_FORMAT_YUV411:
  2707. case DRM_FORMAT_YVU411:
  2708. case DRM_FORMAT_YUV420:
  2709. case DRM_FORMAT_YVU420:
  2710. case DRM_FORMAT_YUV422:
  2711. case DRM_FORMAT_YVU422:
  2712. case DRM_FORMAT_YUV444:
  2713. case DRM_FORMAT_YVU444:
  2714. return 0;
  2715. default:
  2716. DRM_DEBUG_KMS("invalid pixel format %s\n",
  2717. drm_get_format_name(r->pixel_format));
  2718. return -EINVAL;
  2719. }
  2720. }
  2721. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  2722. {
  2723. int ret, hsub, vsub, num_planes, i;
  2724. ret = format_check(r);
  2725. if (ret) {
  2726. DRM_DEBUG_KMS("bad framebuffer format %s\n",
  2727. drm_get_format_name(r->pixel_format));
  2728. return ret;
  2729. }
  2730. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  2731. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  2732. num_planes = drm_format_num_planes(r->pixel_format);
  2733. if (r->width == 0 || r->width % hsub) {
  2734. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  2735. return -EINVAL;
  2736. }
  2737. if (r->height == 0 || r->height % vsub) {
  2738. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  2739. return -EINVAL;
  2740. }
  2741. for (i = 0; i < num_planes; i++) {
  2742. unsigned int width = r->width / (i != 0 ? hsub : 1);
  2743. unsigned int height = r->height / (i != 0 ? vsub : 1);
  2744. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  2745. if (!r->handles[i]) {
  2746. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  2747. return -EINVAL;
  2748. }
  2749. if ((uint64_t) width * cpp > UINT_MAX)
  2750. return -ERANGE;
  2751. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2752. return -ERANGE;
  2753. if (r->pitches[i] < width * cpp) {
  2754. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2755. return -EINVAL;
  2756. }
  2757. if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
  2758. DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
  2759. r->modifier[i], i);
  2760. return -EINVAL;
  2761. }
  2762. /* modifier specific checks: */
  2763. switch (r->modifier[i]) {
  2764. case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
  2765. /* NOTE: the pitch restriction may be lifted later if it turns
  2766. * out that no hw has this restriction:
  2767. */
  2768. if (r->pixel_format != DRM_FORMAT_NV12 ||
  2769. width % 128 || height % 32 ||
  2770. r->pitches[i] % 128) {
  2771. DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
  2772. return -EINVAL;
  2773. }
  2774. break;
  2775. default:
  2776. break;
  2777. }
  2778. }
  2779. for (i = num_planes; i < 4; i++) {
  2780. if (r->modifier[i]) {
  2781. DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
  2782. return -EINVAL;
  2783. }
  2784. /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
  2785. if (!(r->flags & DRM_MODE_FB_MODIFIERS))
  2786. continue;
  2787. if (r->handles[i]) {
  2788. DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
  2789. return -EINVAL;
  2790. }
  2791. if (r->pitches[i]) {
  2792. DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
  2793. return -EINVAL;
  2794. }
  2795. if (r->offsets[i]) {
  2796. DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
  2797. return -EINVAL;
  2798. }
  2799. }
  2800. return 0;
  2801. }
  2802. static struct drm_framebuffer *
  2803. internal_framebuffer_create(struct drm_device *dev,
  2804. struct drm_mode_fb_cmd2 *r,
  2805. struct drm_file *file_priv)
  2806. {
  2807. struct drm_mode_config *config = &dev->mode_config;
  2808. struct drm_framebuffer *fb;
  2809. int ret;
  2810. if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
  2811. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2812. return ERR_PTR(-EINVAL);
  2813. }
  2814. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2815. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2816. r->width, config->min_width, config->max_width);
  2817. return ERR_PTR(-EINVAL);
  2818. }
  2819. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2820. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2821. r->height, config->min_height, config->max_height);
  2822. return ERR_PTR(-EINVAL);
  2823. }
  2824. if (r->flags & DRM_MODE_FB_MODIFIERS &&
  2825. !dev->mode_config.allow_fb_modifiers) {
  2826. DRM_DEBUG_KMS("driver does not support fb modifiers\n");
  2827. return ERR_PTR(-EINVAL);
  2828. }
  2829. ret = framebuffer_check(r);
  2830. if (ret)
  2831. return ERR_PTR(ret);
  2832. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2833. if (IS_ERR(fb)) {
  2834. DRM_DEBUG_KMS("could not create framebuffer\n");
  2835. return fb;
  2836. }
  2837. return fb;
  2838. }
  2839. /**
  2840. * drm_mode_addfb2 - add an FB to the graphics configuration
  2841. * @dev: drm device for the ioctl
  2842. * @data: data pointer for the ioctl
  2843. * @file_priv: drm file for the ioctl call
  2844. *
  2845. * Add a new FB to the specified CRTC, given a user request with format. This is
  2846. * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  2847. * and uses fourcc codes as pixel format specifiers.
  2848. *
  2849. * Called by the user via ioctl.
  2850. *
  2851. * Returns:
  2852. * Zero on success, negative errno on failure.
  2853. */
  2854. int drm_mode_addfb2(struct drm_device *dev,
  2855. void *data, struct drm_file *file_priv)
  2856. {
  2857. struct drm_mode_fb_cmd2 *r = data;
  2858. struct drm_framebuffer *fb;
  2859. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2860. return -EINVAL;
  2861. fb = internal_framebuffer_create(dev, r, file_priv);
  2862. if (IS_ERR(fb))
  2863. return PTR_ERR(fb);
  2864. /* Transfer ownership to the filp for reaping on close */
  2865. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2866. mutex_lock(&file_priv->fbs_lock);
  2867. r->fb_id = fb->base.id;
  2868. list_add(&fb->filp_head, &file_priv->fbs);
  2869. mutex_unlock(&file_priv->fbs_lock);
  2870. return 0;
  2871. }
  2872. struct drm_mode_rmfb_work {
  2873. struct work_struct work;
  2874. struct list_head fbs;
  2875. };
  2876. static void drm_mode_rmfb_work_fn(struct work_struct *w)
  2877. {
  2878. struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
  2879. while (!list_empty(&arg->fbs)) {
  2880. struct drm_framebuffer *fb =
  2881. list_first_entry(&arg->fbs, typeof(*fb), filp_head);
  2882. list_del_init(&fb->filp_head);
  2883. drm_framebuffer_remove(fb);
  2884. }
  2885. }
  2886. /**
  2887. * drm_mode_rmfb - remove an FB from the configuration
  2888. * @dev: drm device for the ioctl
  2889. * @data: data pointer for the ioctl
  2890. * @file_priv: drm file for the ioctl call
  2891. *
  2892. * Remove the FB specified by the user.
  2893. *
  2894. * Called by the user via ioctl.
  2895. *
  2896. * Returns:
  2897. * Zero on success, negative errno on failure.
  2898. */
  2899. int drm_mode_rmfb(struct drm_device *dev,
  2900. void *data, struct drm_file *file_priv)
  2901. {
  2902. struct drm_framebuffer *fb = NULL;
  2903. struct drm_framebuffer *fbl = NULL;
  2904. uint32_t *id = data;
  2905. int found = 0;
  2906. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2907. return -EINVAL;
  2908. mutex_lock(&file_priv->fbs_lock);
  2909. mutex_lock(&dev->mode_config.fb_lock);
  2910. fb = __drm_framebuffer_lookup(dev, *id);
  2911. if (!fb)
  2912. goto fail_lookup;
  2913. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  2914. if (fb == fbl)
  2915. found = 1;
  2916. if (!found)
  2917. goto fail_lookup;
  2918. list_del_init(&fb->filp_head);
  2919. mutex_unlock(&dev->mode_config.fb_lock);
  2920. mutex_unlock(&file_priv->fbs_lock);
  2921. /*
  2922. * we now own the reference that was stored in the fbs list
  2923. *
  2924. * drm_framebuffer_remove may fail with -EINTR on pending signals,
  2925. * so run this in a separate stack as there's no way to correctly
  2926. * handle this after the fb is already removed from the lookup table.
  2927. */
  2928. if (atomic_read(&fb->refcount.refcount) > 1) {
  2929. struct drm_mode_rmfb_work arg;
  2930. INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
  2931. INIT_LIST_HEAD(&arg.fbs);
  2932. list_add_tail(&fb->filp_head, &arg.fbs);
  2933. schedule_work(&arg.work);
  2934. flush_work(&arg.work);
  2935. destroy_work_on_stack(&arg.work);
  2936. } else
  2937. drm_framebuffer_unreference(fb);
  2938. return 0;
  2939. fail_lookup:
  2940. mutex_unlock(&dev->mode_config.fb_lock);
  2941. mutex_unlock(&file_priv->fbs_lock);
  2942. return -ENOENT;
  2943. }
  2944. /**
  2945. * drm_mode_getfb - get FB info
  2946. * @dev: drm device for the ioctl
  2947. * @data: data pointer for the ioctl
  2948. * @file_priv: drm file for the ioctl call
  2949. *
  2950. * Lookup the FB given its ID and return info about it.
  2951. *
  2952. * Called by the user via ioctl.
  2953. *
  2954. * Returns:
  2955. * Zero on success, negative errno on failure.
  2956. */
  2957. int drm_mode_getfb(struct drm_device *dev,
  2958. void *data, struct drm_file *file_priv)
  2959. {
  2960. struct drm_mode_fb_cmd *r = data;
  2961. struct drm_framebuffer *fb;
  2962. int ret;
  2963. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2964. return -EINVAL;
  2965. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2966. if (!fb)
  2967. return -ENOENT;
  2968. r->height = fb->height;
  2969. r->width = fb->width;
  2970. r->depth = fb->depth;
  2971. r->bpp = fb->bits_per_pixel;
  2972. r->pitch = fb->pitches[0];
  2973. if (fb->funcs->create_handle) {
  2974. if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
  2975. drm_is_control_client(file_priv)) {
  2976. ret = fb->funcs->create_handle(fb, file_priv,
  2977. &r->handle);
  2978. } else {
  2979. /* GET_FB() is an unprivileged ioctl so we must not
  2980. * return a buffer-handle to non-master processes! For
  2981. * backwards-compatibility reasons, we cannot make
  2982. * GET_FB() privileged, so just return an invalid handle
  2983. * for non-masters. */
  2984. r->handle = 0;
  2985. ret = 0;
  2986. }
  2987. } else {
  2988. ret = -ENODEV;
  2989. }
  2990. drm_framebuffer_unreference(fb);
  2991. return ret;
  2992. }
  2993. /**
  2994. * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  2995. * @dev: drm device for the ioctl
  2996. * @data: data pointer for the ioctl
  2997. * @file_priv: drm file for the ioctl call
  2998. *
  2999. * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  3000. * rectangle list. Generic userspace which does frontbuffer rendering must call
  3001. * this ioctl to flush out the changes on manual-update display outputs, e.g.
  3002. * usb display-link, mipi manual update panels or edp panel self refresh modes.
  3003. *
  3004. * Modesetting drivers which always update the frontbuffer do not need to
  3005. * implement the corresponding ->dirty framebuffer callback.
  3006. *
  3007. * Called by the user via ioctl.
  3008. *
  3009. * Returns:
  3010. * Zero on success, negative errno on failure.
  3011. */
  3012. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  3013. void *data, struct drm_file *file_priv)
  3014. {
  3015. struct drm_clip_rect __user *clips_ptr;
  3016. struct drm_clip_rect *clips = NULL;
  3017. struct drm_mode_fb_dirty_cmd *r = data;
  3018. struct drm_framebuffer *fb;
  3019. unsigned flags;
  3020. int num_clips;
  3021. int ret;
  3022. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3023. return -EINVAL;
  3024. fb = drm_framebuffer_lookup(dev, r->fb_id);
  3025. if (!fb)
  3026. return -ENOENT;
  3027. num_clips = r->num_clips;
  3028. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  3029. if (!num_clips != !clips_ptr) {
  3030. ret = -EINVAL;
  3031. goto out_err1;
  3032. }
  3033. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  3034. /* If userspace annotates copy, clips must come in pairs */
  3035. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  3036. ret = -EINVAL;
  3037. goto out_err1;
  3038. }
  3039. if (num_clips && clips_ptr) {
  3040. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  3041. ret = -EINVAL;
  3042. goto out_err1;
  3043. }
  3044. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  3045. if (!clips) {
  3046. ret = -ENOMEM;
  3047. goto out_err1;
  3048. }
  3049. ret = copy_from_user(clips, clips_ptr,
  3050. num_clips * sizeof(*clips));
  3051. if (ret) {
  3052. ret = -EFAULT;
  3053. goto out_err2;
  3054. }
  3055. }
  3056. if (fb->funcs->dirty) {
  3057. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  3058. clips, num_clips);
  3059. } else {
  3060. ret = -ENOSYS;
  3061. }
  3062. out_err2:
  3063. kfree(clips);
  3064. out_err1:
  3065. drm_framebuffer_unreference(fb);
  3066. return ret;
  3067. }
  3068. /**
  3069. * drm_fb_release - remove and free the FBs on this file
  3070. * @priv: drm file for the ioctl
  3071. *
  3072. * Destroy all the FBs associated with @filp.
  3073. *
  3074. * Called by the user via ioctl.
  3075. *
  3076. * Returns:
  3077. * Zero on success, negative errno on failure.
  3078. */
  3079. void drm_fb_release(struct drm_file *priv)
  3080. {
  3081. struct drm_framebuffer *fb, *tfb;
  3082. struct drm_mode_rmfb_work arg;
  3083. INIT_LIST_HEAD(&arg.fbs);
  3084. /*
  3085. * When the file gets released that means no one else can access the fb
  3086. * list any more, so no need to grab fpriv->fbs_lock. And we need to
  3087. * avoid upsetting lockdep since the universal cursor code adds a
  3088. * framebuffer while holding mutex locks.
  3089. *
  3090. * Note that a real deadlock between fpriv->fbs_lock and the modeset
  3091. * locks is impossible here since no one else but this function can get
  3092. * at it any more.
  3093. */
  3094. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  3095. if (atomic_read(&fb->refcount.refcount) > 1) {
  3096. list_move_tail(&fb->filp_head, &arg.fbs);
  3097. } else {
  3098. list_del_init(&fb->filp_head);
  3099. /* This drops the fpriv->fbs reference. */
  3100. drm_framebuffer_unreference(fb);
  3101. }
  3102. }
  3103. if (!list_empty(&arg.fbs)) {
  3104. INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
  3105. schedule_work(&arg.work);
  3106. flush_work(&arg.work);
  3107. destroy_work_on_stack(&arg.work);
  3108. }
  3109. }
  3110. /**
  3111. * drm_property_create - create a new property type
  3112. * @dev: drm device
  3113. * @flags: flags specifying the property type
  3114. * @name: name of the property
  3115. * @num_values: number of pre-defined values
  3116. *
  3117. * This creates a new generic drm property which can then be attached to a drm
  3118. * object with drm_object_attach_property. The returned property object must be
  3119. * freed with drm_property_destroy.
  3120. *
  3121. * Note that the DRM core keeps a per-device list of properties and that, if
  3122. * drm_mode_config_cleanup() is called, it will destroy all properties created
  3123. * by the driver.
  3124. *
  3125. * Returns:
  3126. * A pointer to the newly created property on success, NULL on failure.
  3127. */
  3128. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  3129. const char *name, int num_values)
  3130. {
  3131. struct drm_property *property = NULL;
  3132. int ret;
  3133. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  3134. if (!property)
  3135. return NULL;
  3136. property->dev = dev;
  3137. if (num_values) {
  3138. property->values = kcalloc(num_values, sizeof(uint64_t),
  3139. GFP_KERNEL);
  3140. if (!property->values)
  3141. goto fail;
  3142. }
  3143. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  3144. if (ret)
  3145. goto fail;
  3146. property->flags = flags;
  3147. property->num_values = num_values;
  3148. INIT_LIST_HEAD(&property->enum_list);
  3149. if (name) {
  3150. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  3151. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  3152. }
  3153. list_add_tail(&property->head, &dev->mode_config.property_list);
  3154. WARN_ON(!drm_property_type_valid(property));
  3155. return property;
  3156. fail:
  3157. kfree(property->values);
  3158. kfree(property);
  3159. return NULL;
  3160. }
  3161. EXPORT_SYMBOL(drm_property_create);
  3162. /**
  3163. * drm_property_create_enum - create a new enumeration 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 values
  3168. * @num_values: number of pre-defined values
  3169. *
  3170. * This creates a new generic drm property which can then be attached to a drm
  3171. * object with drm_object_attach_property. The returned property object must be
  3172. * freed with drm_property_destroy.
  3173. *
  3174. * Userspace is only allowed to set one of the predefined values for enumeration
  3175. * properties.
  3176. *
  3177. * Returns:
  3178. * A pointer to the newly created property on success, NULL on failure.
  3179. */
  3180. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  3181. const char *name,
  3182. const struct drm_prop_enum_list *props,
  3183. int num_values)
  3184. {
  3185. struct drm_property *property;
  3186. int i, ret;
  3187. flags |= DRM_MODE_PROP_ENUM;
  3188. property = drm_property_create(dev, flags, name, num_values);
  3189. if (!property)
  3190. return NULL;
  3191. for (i = 0; i < num_values; i++) {
  3192. ret = drm_property_add_enum(property, i,
  3193. props[i].type,
  3194. props[i].name);
  3195. if (ret) {
  3196. drm_property_destroy(dev, property);
  3197. return NULL;
  3198. }
  3199. }
  3200. return property;
  3201. }
  3202. EXPORT_SYMBOL(drm_property_create_enum);
  3203. /**
  3204. * drm_property_create_bitmask - create a new bitmask property type
  3205. * @dev: drm device
  3206. * @flags: flags specifying the property type
  3207. * @name: name of the property
  3208. * @props: enumeration lists with property bitflags
  3209. * @num_props: size of the @props array
  3210. * @supported_bits: bitmask of all supported enumeration values
  3211. *
  3212. * This creates a new bitmask drm property which can then be attached to a drm
  3213. * object with drm_object_attach_property. The returned property object must be
  3214. * freed with drm_property_destroy.
  3215. *
  3216. * Compared to plain enumeration properties userspace is allowed to set any
  3217. * or'ed together combination of the predefined property bitflag values
  3218. *
  3219. * Returns:
  3220. * A pointer to the newly created property on success, NULL on failure.
  3221. */
  3222. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  3223. int flags, const char *name,
  3224. const struct drm_prop_enum_list *props,
  3225. int num_props,
  3226. uint64_t supported_bits)
  3227. {
  3228. struct drm_property *property;
  3229. int i, ret, index = 0;
  3230. int num_values = hweight64(supported_bits);
  3231. flags |= DRM_MODE_PROP_BITMASK;
  3232. property = drm_property_create(dev, flags, name, num_values);
  3233. if (!property)
  3234. return NULL;
  3235. for (i = 0; i < num_props; i++) {
  3236. if (!(supported_bits & (1ULL << props[i].type)))
  3237. continue;
  3238. if (WARN_ON(index >= num_values)) {
  3239. drm_property_destroy(dev, property);
  3240. return NULL;
  3241. }
  3242. ret = drm_property_add_enum(property, index++,
  3243. props[i].type,
  3244. props[i].name);
  3245. if (ret) {
  3246. drm_property_destroy(dev, property);
  3247. return NULL;
  3248. }
  3249. }
  3250. return property;
  3251. }
  3252. EXPORT_SYMBOL(drm_property_create_bitmask);
  3253. static struct drm_property *property_create_range(struct drm_device *dev,
  3254. int flags, const char *name,
  3255. uint64_t min, uint64_t max)
  3256. {
  3257. struct drm_property *property;
  3258. property = drm_property_create(dev, flags, name, 2);
  3259. if (!property)
  3260. return NULL;
  3261. property->values[0] = min;
  3262. property->values[1] = max;
  3263. return property;
  3264. }
  3265. /**
  3266. * drm_property_create_range - create a new unsigned ranged property type
  3267. * @dev: drm device
  3268. * @flags: flags specifying the property type
  3269. * @name: name of the property
  3270. * @min: minimum value of the property
  3271. * @max: maximum value of the property
  3272. *
  3273. * This creates a new generic drm property which can then be attached to a drm
  3274. * object with drm_object_attach_property. The returned property object must be
  3275. * freed with drm_property_destroy.
  3276. *
  3277. * Userspace is allowed to set any unsigned integer value in the (min, max)
  3278. * range inclusive.
  3279. *
  3280. * Returns:
  3281. * A pointer to the newly created property on success, NULL on failure.
  3282. */
  3283. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  3284. const char *name,
  3285. uint64_t min, uint64_t max)
  3286. {
  3287. return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
  3288. name, min, max);
  3289. }
  3290. EXPORT_SYMBOL(drm_property_create_range);
  3291. /**
  3292. * drm_property_create_signed_range - create a new signed ranged property type
  3293. * @dev: drm device
  3294. * @flags: flags specifying the property type
  3295. * @name: name of the property
  3296. * @min: minimum value of the property
  3297. * @max: maximum value of the property
  3298. *
  3299. * This creates a new generic drm property which can then be attached to a drm
  3300. * object with drm_object_attach_property. The returned property object must be
  3301. * freed with drm_property_destroy.
  3302. *
  3303. * Userspace is allowed to set any signed integer value in the (min, max)
  3304. * range inclusive.
  3305. *
  3306. * Returns:
  3307. * A pointer to the newly created property on success, NULL on failure.
  3308. */
  3309. struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
  3310. int flags, const char *name,
  3311. int64_t min, int64_t max)
  3312. {
  3313. return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
  3314. name, I642U64(min), I642U64(max));
  3315. }
  3316. EXPORT_SYMBOL(drm_property_create_signed_range);
  3317. /**
  3318. * drm_property_create_object - create a new object property type
  3319. * @dev: drm device
  3320. * @flags: flags specifying the property type
  3321. * @name: name of the property
  3322. * @type: object type from DRM_MODE_OBJECT_* defines
  3323. *
  3324. * This creates a new generic drm property which can then be attached to a drm
  3325. * object with drm_object_attach_property. The returned property object must be
  3326. * freed with drm_property_destroy.
  3327. *
  3328. * Userspace is only allowed to set this to any property value of the given
  3329. * @type. Only useful for atomic properties, which is enforced.
  3330. *
  3331. * Returns:
  3332. * A pointer to the newly created property on success, NULL on failure.
  3333. */
  3334. struct drm_property *drm_property_create_object(struct drm_device *dev,
  3335. int flags, const char *name, uint32_t type)
  3336. {
  3337. struct drm_property *property;
  3338. flags |= DRM_MODE_PROP_OBJECT;
  3339. if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
  3340. return NULL;
  3341. property = drm_property_create(dev, flags, name, 1);
  3342. if (!property)
  3343. return NULL;
  3344. property->values[0] = type;
  3345. return property;
  3346. }
  3347. EXPORT_SYMBOL(drm_property_create_object);
  3348. /**
  3349. * drm_property_create_bool - create a new boolean property type
  3350. * @dev: drm device
  3351. * @flags: flags specifying the property type
  3352. * @name: name of the property
  3353. *
  3354. * This creates a new generic drm property which can then be attached to a drm
  3355. * object with drm_object_attach_property. The returned property object must be
  3356. * freed with drm_property_destroy.
  3357. *
  3358. * This is implemented as a ranged property with only {0, 1} as valid values.
  3359. *
  3360. * Returns:
  3361. * A pointer to the newly created property on success, NULL on failure.
  3362. */
  3363. struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
  3364. const char *name)
  3365. {
  3366. return drm_property_create_range(dev, flags, name, 0, 1);
  3367. }
  3368. EXPORT_SYMBOL(drm_property_create_bool);
  3369. /**
  3370. * drm_property_add_enum - add a possible value to an enumeration property
  3371. * @property: enumeration property to change
  3372. * @index: index of the new enumeration
  3373. * @value: value of the new enumeration
  3374. * @name: symbolic name of the new enumeration
  3375. *
  3376. * This functions adds enumerations to a property.
  3377. *
  3378. * It's use is deprecated, drivers should use one of the more specific helpers
  3379. * to directly create the property with all enumerations already attached.
  3380. *
  3381. * Returns:
  3382. * Zero on success, error code on failure.
  3383. */
  3384. int drm_property_add_enum(struct drm_property *property, int index,
  3385. uint64_t value, const char *name)
  3386. {
  3387. struct drm_property_enum *prop_enum;
  3388. if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3389. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
  3390. return -EINVAL;
  3391. /*
  3392. * Bitmask enum properties have the additional constraint of values
  3393. * from 0 to 63
  3394. */
  3395. if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
  3396. (value > 63))
  3397. return -EINVAL;
  3398. if (!list_empty(&property->enum_list)) {
  3399. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3400. if (prop_enum->value == value) {
  3401. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3402. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3403. return 0;
  3404. }
  3405. }
  3406. }
  3407. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  3408. if (!prop_enum)
  3409. return -ENOMEM;
  3410. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3411. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3412. prop_enum->value = value;
  3413. property->values[index] = value;
  3414. list_add_tail(&prop_enum->head, &property->enum_list);
  3415. return 0;
  3416. }
  3417. EXPORT_SYMBOL(drm_property_add_enum);
  3418. /**
  3419. * drm_property_destroy - destroy a drm property
  3420. * @dev: drm device
  3421. * @property: property to destry
  3422. *
  3423. * This function frees a property including any attached resources like
  3424. * enumeration values.
  3425. */
  3426. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  3427. {
  3428. struct drm_property_enum *prop_enum, *pt;
  3429. list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
  3430. list_del(&prop_enum->head);
  3431. kfree(prop_enum);
  3432. }
  3433. if (property->num_values)
  3434. kfree(property->values);
  3435. drm_mode_object_put(dev, &property->base);
  3436. list_del(&property->head);
  3437. kfree(property);
  3438. }
  3439. EXPORT_SYMBOL(drm_property_destroy);
  3440. /**
  3441. * drm_object_attach_property - attach a property to a modeset object
  3442. * @obj: drm modeset object
  3443. * @property: property to attach
  3444. * @init_val: initial value of the property
  3445. *
  3446. * This attaches the given property to the modeset object with the given initial
  3447. * value. Currently this function cannot fail since the properties are stored in
  3448. * a statically sized array.
  3449. */
  3450. void drm_object_attach_property(struct drm_mode_object *obj,
  3451. struct drm_property *property,
  3452. uint64_t init_val)
  3453. {
  3454. int count = obj->properties->count;
  3455. if (count == DRM_OBJECT_MAX_PROPERTY) {
  3456. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  3457. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  3458. "you see this message on the same object type.\n",
  3459. obj->type);
  3460. return;
  3461. }
  3462. obj->properties->properties[count] = property;
  3463. obj->properties->values[count] = init_val;
  3464. obj->properties->count++;
  3465. if (property->flags & DRM_MODE_PROP_ATOMIC)
  3466. obj->properties->atomic_count++;
  3467. }
  3468. EXPORT_SYMBOL(drm_object_attach_property);
  3469. /**
  3470. * drm_object_property_set_value - set the value of a property
  3471. * @obj: drm mode object to set property value for
  3472. * @property: property to set
  3473. * @val: value the property should be set to
  3474. *
  3475. * This functions sets a given property on a given object. This function only
  3476. * changes the software state of the property, it does not call into the
  3477. * driver's ->set_property callback.
  3478. *
  3479. * Returns:
  3480. * Zero on success, error code on failure.
  3481. */
  3482. int drm_object_property_set_value(struct drm_mode_object *obj,
  3483. struct drm_property *property, uint64_t val)
  3484. {
  3485. int i;
  3486. for (i = 0; i < obj->properties->count; i++) {
  3487. if (obj->properties->properties[i] == property) {
  3488. obj->properties->values[i] = val;
  3489. return 0;
  3490. }
  3491. }
  3492. return -EINVAL;
  3493. }
  3494. EXPORT_SYMBOL(drm_object_property_set_value);
  3495. /**
  3496. * drm_object_property_get_value - retrieve the value of a property
  3497. * @obj: drm mode object to get property value from
  3498. * @property: property to retrieve
  3499. * @val: storage for the property value
  3500. *
  3501. * This function retrieves the softare state of the given property for the given
  3502. * property. Since there is no driver callback to retrieve the current property
  3503. * value this might be out of sync with the hardware, depending upon the driver
  3504. * and property.
  3505. *
  3506. * Returns:
  3507. * Zero on success, error code on failure.
  3508. */
  3509. int drm_object_property_get_value(struct drm_mode_object *obj,
  3510. struct drm_property *property, uint64_t *val)
  3511. {
  3512. int i;
  3513. /* read-only properties bypass atomic mechanism and still store
  3514. * their value in obj->properties->values[].. mostly to avoid
  3515. * having to deal w/ EDID and similar props in atomic paths:
  3516. */
  3517. if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
  3518. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  3519. return drm_atomic_get_property(obj, property, val);
  3520. for (i = 0; i < obj->properties->count; i++) {
  3521. if (obj->properties->properties[i] == property) {
  3522. *val = obj->properties->values[i];
  3523. return 0;
  3524. }
  3525. }
  3526. return -EINVAL;
  3527. }
  3528. EXPORT_SYMBOL(drm_object_property_get_value);
  3529. /**
  3530. * drm_mode_getproperty_ioctl - get the property metadata
  3531. * @dev: DRM device
  3532. * @data: ioctl data
  3533. * @file_priv: DRM file info
  3534. *
  3535. * This function retrieves the metadata for a given property, like the different
  3536. * possible values for an enum property or the limits for a range property.
  3537. *
  3538. * Blob properties are special
  3539. *
  3540. * Called by the user via ioctl.
  3541. *
  3542. * Returns:
  3543. * Zero on success, negative errno on failure.
  3544. */
  3545. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  3546. void *data, struct drm_file *file_priv)
  3547. {
  3548. struct drm_mode_get_property *out_resp = data;
  3549. struct drm_property *property;
  3550. int enum_count = 0;
  3551. int value_count = 0;
  3552. int ret = 0, i;
  3553. int copied;
  3554. struct drm_property_enum *prop_enum;
  3555. struct drm_mode_property_enum __user *enum_ptr;
  3556. uint64_t __user *values_ptr;
  3557. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3558. return -EINVAL;
  3559. drm_modeset_lock_all(dev);
  3560. property = drm_property_find(dev, out_resp->prop_id);
  3561. if (!property) {
  3562. ret = -ENOENT;
  3563. goto done;
  3564. }
  3565. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3566. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3567. list_for_each_entry(prop_enum, &property->enum_list, head)
  3568. enum_count++;
  3569. }
  3570. value_count = property->num_values;
  3571. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  3572. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  3573. out_resp->flags = property->flags;
  3574. if ((out_resp->count_values >= value_count) && value_count) {
  3575. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  3576. for (i = 0; i < value_count; i++) {
  3577. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  3578. ret = -EFAULT;
  3579. goto done;
  3580. }
  3581. }
  3582. }
  3583. out_resp->count_values = value_count;
  3584. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3585. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3586. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  3587. copied = 0;
  3588. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  3589. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3590. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  3591. ret = -EFAULT;
  3592. goto done;
  3593. }
  3594. if (copy_to_user(&enum_ptr[copied].name,
  3595. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  3596. ret = -EFAULT;
  3597. goto done;
  3598. }
  3599. copied++;
  3600. }
  3601. }
  3602. out_resp->count_enum_blobs = enum_count;
  3603. }
  3604. /*
  3605. * NOTE: The idea seems to have been to use this to read all the blob
  3606. * property values. But nothing ever added them to the corresponding
  3607. * list, userspace always used the special-purpose get_blob ioctl to
  3608. * read the value for a blob property. It also doesn't make a lot of
  3609. * sense to return values here when everything else is just metadata for
  3610. * the property itself.
  3611. */
  3612. if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  3613. out_resp->count_enum_blobs = 0;
  3614. done:
  3615. drm_modeset_unlock_all(dev);
  3616. return ret;
  3617. }
  3618. /**
  3619. * drm_property_create_blob - Create new blob property
  3620. *
  3621. * Creates a new blob property for a specified DRM device, optionally
  3622. * copying data.
  3623. *
  3624. * @dev: DRM device to create property for
  3625. * @length: Length to allocate for blob data
  3626. * @data: If specified, copies data into blob
  3627. *
  3628. * Returns:
  3629. * New blob property with a single reference on success, or an ERR_PTR
  3630. * value on failure.
  3631. */
  3632. struct drm_property_blob *
  3633. drm_property_create_blob(struct drm_device *dev, size_t length,
  3634. const void *data)
  3635. {
  3636. struct drm_property_blob *blob;
  3637. int ret;
  3638. if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
  3639. return ERR_PTR(-EINVAL);
  3640. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  3641. if (!blob)
  3642. return ERR_PTR(-ENOMEM);
  3643. /* This must be explicitly initialised, so we can safely call list_del
  3644. * on it in the removal handler, even if it isn't in a file list. */
  3645. INIT_LIST_HEAD(&blob->head_file);
  3646. blob->length = length;
  3647. blob->dev = dev;
  3648. if (data)
  3649. memcpy(blob->data, data, length);
  3650. mutex_lock(&dev->mode_config.blob_lock);
  3651. ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  3652. if (ret) {
  3653. kfree(blob);
  3654. mutex_unlock(&dev->mode_config.blob_lock);
  3655. return ERR_PTR(-EINVAL);
  3656. }
  3657. kref_init(&blob->refcount);
  3658. list_add_tail(&blob->head_global,
  3659. &dev->mode_config.property_blob_list);
  3660. mutex_unlock(&dev->mode_config.blob_lock);
  3661. return blob;
  3662. }
  3663. EXPORT_SYMBOL(drm_property_create_blob);
  3664. /**
  3665. * drm_property_free_blob - Blob property destructor
  3666. *
  3667. * Internal free function for blob properties; must not be used directly.
  3668. *
  3669. * @kref: Reference
  3670. */
  3671. static void drm_property_free_blob(struct kref *kref)
  3672. {
  3673. struct drm_property_blob *blob =
  3674. container_of(kref, struct drm_property_blob, refcount);
  3675. WARN_ON(!mutex_is_locked(&blob->dev->mode_config.blob_lock));
  3676. list_del(&blob->head_global);
  3677. list_del(&blob->head_file);
  3678. drm_mode_object_put(blob->dev, &blob->base);
  3679. kfree(blob);
  3680. }
  3681. /**
  3682. * drm_property_unreference_blob - Unreference a blob property
  3683. *
  3684. * Drop a reference on a blob property. May free the object.
  3685. *
  3686. * @blob: Pointer to blob property
  3687. */
  3688. void drm_property_unreference_blob(struct drm_property_blob *blob)
  3689. {
  3690. struct drm_device *dev;
  3691. if (!blob)
  3692. return;
  3693. dev = blob->dev;
  3694. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3695. if (kref_put_mutex(&blob->refcount, drm_property_free_blob,
  3696. &dev->mode_config.blob_lock))
  3697. mutex_unlock(&dev->mode_config.blob_lock);
  3698. else
  3699. might_lock(&dev->mode_config.blob_lock);
  3700. }
  3701. EXPORT_SYMBOL(drm_property_unreference_blob);
  3702. /**
  3703. * drm_property_unreference_blob_locked - Unreference a blob property with blob_lock held
  3704. *
  3705. * Drop a reference on a blob property. May free the object. This must be
  3706. * called with blob_lock held.
  3707. *
  3708. * @blob: Pointer to blob property
  3709. */
  3710. static void drm_property_unreference_blob_locked(struct drm_property_blob *blob)
  3711. {
  3712. if (!blob)
  3713. return;
  3714. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3715. kref_put(&blob->refcount, drm_property_free_blob);
  3716. }
  3717. /**
  3718. * drm_property_destroy_user_blobs - destroy all blobs created by this client
  3719. * @dev: DRM device
  3720. * @file_priv: destroy all blobs owned by this file handle
  3721. */
  3722. void drm_property_destroy_user_blobs(struct drm_device *dev,
  3723. struct drm_file *file_priv)
  3724. {
  3725. struct drm_property_blob *blob, *bt;
  3726. mutex_lock(&dev->mode_config.blob_lock);
  3727. list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
  3728. list_del_init(&blob->head_file);
  3729. drm_property_unreference_blob_locked(blob);
  3730. }
  3731. mutex_unlock(&dev->mode_config.blob_lock);
  3732. }
  3733. /**
  3734. * drm_property_reference_blob - Take a reference on an existing property
  3735. *
  3736. * Take a new reference on an existing blob property.
  3737. *
  3738. * @blob: Pointer to blob property
  3739. */
  3740. struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
  3741. {
  3742. DRM_DEBUG("%p: blob ID: %d (%d)\n", blob, blob->base.id, atomic_read(&blob->refcount.refcount));
  3743. kref_get(&blob->refcount);
  3744. return blob;
  3745. }
  3746. EXPORT_SYMBOL(drm_property_reference_blob);
  3747. /*
  3748. * Like drm_property_lookup_blob, but does not return an additional reference.
  3749. * Must be called with blob_lock held.
  3750. */
  3751. static struct drm_property_blob *__drm_property_lookup_blob(struct drm_device *dev,
  3752. uint32_t id)
  3753. {
  3754. struct drm_mode_object *obj = NULL;
  3755. struct drm_property_blob *blob;
  3756. WARN_ON(!mutex_is_locked(&dev->mode_config.blob_lock));
  3757. mutex_lock(&dev->mode_config.idr_mutex);
  3758. obj = idr_find(&dev->mode_config.crtc_idr, id);
  3759. if (!obj || (obj->type != DRM_MODE_OBJECT_BLOB) || (obj->id != id))
  3760. blob = NULL;
  3761. else
  3762. blob = obj_to_blob(obj);
  3763. mutex_unlock(&dev->mode_config.idr_mutex);
  3764. return blob;
  3765. }
  3766. /**
  3767. * drm_property_lookup_blob - look up a blob property and take a reference
  3768. * @dev: drm device
  3769. * @id: id of the blob property
  3770. *
  3771. * If successful, this takes an additional reference to the blob property.
  3772. * callers need to make sure to eventually unreference the returned property
  3773. * again, using @drm_property_unreference_blob.
  3774. */
  3775. struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
  3776. uint32_t id)
  3777. {
  3778. struct drm_property_blob *blob;
  3779. mutex_lock(&dev->mode_config.blob_lock);
  3780. blob = __drm_property_lookup_blob(dev, id);
  3781. if (blob) {
  3782. if (!kref_get_unless_zero(&blob->refcount))
  3783. blob = NULL;
  3784. }
  3785. mutex_unlock(&dev->mode_config.blob_lock);
  3786. return blob;
  3787. }
  3788. EXPORT_SYMBOL(drm_property_lookup_blob);
  3789. /**
  3790. * drm_property_replace_global_blob - atomically replace existing blob property
  3791. * @dev: drm device
  3792. * @replace: location of blob property pointer to be replaced
  3793. * @length: length of data for new blob, or 0 for no data
  3794. * @data: content for new blob, or NULL for no data
  3795. * @obj_holds_id: optional object for property holding blob ID
  3796. * @prop_holds_id: optional property holding blob ID
  3797. * @return 0 on success or error on failure
  3798. *
  3799. * This function will atomically replace a global property in the blob list,
  3800. * optionally updating a property which holds the ID of that property. It is
  3801. * guaranteed to be atomic: no caller will be allowed to see intermediate
  3802. * results, and either the entire operation will succeed and clean up the
  3803. * previous property, or it will fail and the state will be unchanged.
  3804. *
  3805. * If length is 0 or data is NULL, no new blob will be created, and the holding
  3806. * property, if specified, will be set to 0.
  3807. *
  3808. * Access to the replace pointer is assumed to be protected by the caller, e.g.
  3809. * by holding the relevant modesetting object lock for its parent.
  3810. *
  3811. * For example, a drm_connector has a 'PATH' property, which contains the ID
  3812. * of a blob property with the value of the MST path information. Calling this
  3813. * function with replace pointing to the connector's path_blob_ptr, length and
  3814. * data set for the new path information, obj_holds_id set to the connector's
  3815. * base object, and prop_holds_id set to the path property name, will perform
  3816. * a completely atomic update. The access to path_blob_ptr is protected by the
  3817. * caller holding a lock on the connector.
  3818. */
  3819. static int drm_property_replace_global_blob(struct drm_device *dev,
  3820. struct drm_property_blob **replace,
  3821. size_t length,
  3822. const void *data,
  3823. struct drm_mode_object *obj_holds_id,
  3824. struct drm_property *prop_holds_id)
  3825. {
  3826. struct drm_property_blob *new_blob = NULL;
  3827. struct drm_property_blob *old_blob = NULL;
  3828. int ret;
  3829. WARN_ON(replace == NULL);
  3830. old_blob = *replace;
  3831. if (length && data) {
  3832. new_blob = drm_property_create_blob(dev, length, data);
  3833. if (IS_ERR(new_blob))
  3834. return PTR_ERR(new_blob);
  3835. }
  3836. /* This does not need to be synchronised with blob_lock, as the
  3837. * get_properties ioctl locks all modesetting objects, and
  3838. * obj_holds_id must be locked before calling here, so we cannot
  3839. * have its value out of sync with the list membership modified
  3840. * below under blob_lock. */
  3841. if (obj_holds_id) {
  3842. ret = drm_object_property_set_value(obj_holds_id,
  3843. prop_holds_id,
  3844. new_blob ?
  3845. new_blob->base.id : 0);
  3846. if (ret != 0)
  3847. goto err_created;
  3848. }
  3849. drm_property_unreference_blob(old_blob);
  3850. *replace = new_blob;
  3851. return 0;
  3852. err_created:
  3853. drm_property_unreference_blob(new_blob);
  3854. return ret;
  3855. }
  3856. /**
  3857. * drm_mode_getblob_ioctl - get the contents of a blob property value
  3858. * @dev: DRM device
  3859. * @data: ioctl data
  3860. * @file_priv: DRM file info
  3861. *
  3862. * This function retrieves the contents of a blob property. The value stored in
  3863. * an object's blob property is just a normal modeset object id.
  3864. *
  3865. * Called by the user via ioctl.
  3866. *
  3867. * Returns:
  3868. * Zero on success, negative errno on failure.
  3869. */
  3870. int drm_mode_getblob_ioctl(struct drm_device *dev,
  3871. void *data, struct drm_file *file_priv)
  3872. {
  3873. struct drm_mode_get_blob *out_resp = data;
  3874. struct drm_property_blob *blob;
  3875. int ret = 0;
  3876. void __user *blob_ptr;
  3877. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3878. return -EINVAL;
  3879. drm_modeset_lock_all(dev);
  3880. mutex_lock(&dev->mode_config.blob_lock);
  3881. blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
  3882. if (!blob) {
  3883. ret = -ENOENT;
  3884. goto done;
  3885. }
  3886. if (out_resp->length == blob->length) {
  3887. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  3888. if (copy_to_user(blob_ptr, blob->data, blob->length)) {
  3889. ret = -EFAULT;
  3890. goto done;
  3891. }
  3892. }
  3893. out_resp->length = blob->length;
  3894. done:
  3895. mutex_unlock(&dev->mode_config.blob_lock);
  3896. drm_modeset_unlock_all(dev);
  3897. return ret;
  3898. }
  3899. /**
  3900. * drm_mode_createblob_ioctl - create a new blob property
  3901. * @dev: DRM device
  3902. * @data: ioctl data
  3903. * @file_priv: DRM file info
  3904. *
  3905. * This function creates a new blob property with user-defined values. In order
  3906. * to give us sensible validation and checking when creating, rather than at
  3907. * every potential use, we also require a type to be provided upfront.
  3908. *
  3909. * Called by the user via ioctl.
  3910. *
  3911. * Returns:
  3912. * Zero on success, negative errno on failure.
  3913. */
  3914. int drm_mode_createblob_ioctl(struct drm_device *dev,
  3915. void *data, struct drm_file *file_priv)
  3916. {
  3917. struct drm_mode_create_blob *out_resp = data;
  3918. struct drm_property_blob *blob;
  3919. void __user *blob_ptr;
  3920. int ret = 0;
  3921. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3922. return -EINVAL;
  3923. blob = drm_property_create_blob(dev, out_resp->length, NULL);
  3924. if (IS_ERR(blob))
  3925. return PTR_ERR(blob);
  3926. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  3927. if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
  3928. ret = -EFAULT;
  3929. goto out_blob;
  3930. }
  3931. /* Dropping the lock between create_blob and our access here is safe
  3932. * as only the same file_priv can remove the blob; at this point, it is
  3933. * not associated with any file_priv. */
  3934. mutex_lock(&dev->mode_config.blob_lock);
  3935. out_resp->blob_id = blob->base.id;
  3936. list_add_tail(&blob->head_file, &file_priv->blobs);
  3937. mutex_unlock(&dev->mode_config.blob_lock);
  3938. return 0;
  3939. out_blob:
  3940. drm_property_unreference_blob(blob);
  3941. return ret;
  3942. }
  3943. /**
  3944. * drm_mode_destroyblob_ioctl - destroy a user blob property
  3945. * @dev: DRM device
  3946. * @data: ioctl data
  3947. * @file_priv: DRM file info
  3948. *
  3949. * Destroy an existing user-defined blob property.
  3950. *
  3951. * Called by the user via ioctl.
  3952. *
  3953. * Returns:
  3954. * Zero on success, negative errno on failure.
  3955. */
  3956. int drm_mode_destroyblob_ioctl(struct drm_device *dev,
  3957. void *data, struct drm_file *file_priv)
  3958. {
  3959. struct drm_mode_destroy_blob *out_resp = data;
  3960. struct drm_property_blob *blob = NULL, *bt;
  3961. bool found = false;
  3962. int ret = 0;
  3963. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3964. return -EINVAL;
  3965. mutex_lock(&dev->mode_config.blob_lock);
  3966. blob = __drm_property_lookup_blob(dev, out_resp->blob_id);
  3967. if (!blob) {
  3968. ret = -ENOENT;
  3969. goto err;
  3970. }
  3971. /* Ensure the property was actually created by this user. */
  3972. list_for_each_entry(bt, &file_priv->blobs, head_file) {
  3973. if (bt == blob) {
  3974. found = true;
  3975. break;
  3976. }
  3977. }
  3978. if (!found) {
  3979. ret = -EPERM;
  3980. goto err;
  3981. }
  3982. /* We must drop head_file here, because we may not be the last
  3983. * reference on the blob. */
  3984. list_del_init(&blob->head_file);
  3985. drm_property_unreference_blob_locked(blob);
  3986. mutex_unlock(&dev->mode_config.blob_lock);
  3987. return 0;
  3988. err:
  3989. mutex_unlock(&dev->mode_config.blob_lock);
  3990. return ret;
  3991. }
  3992. /**
  3993. * drm_mode_connector_set_path_property - set tile property on connector
  3994. * @connector: connector to set property on.
  3995. * @path: path to use for property; must not be NULL.
  3996. *
  3997. * This creates a property to expose to userspace to specify a
  3998. * connector path. This is mainly used for DisplayPort MST where
  3999. * connectors have a topology and we want to allow userspace to give
  4000. * them more meaningful names.
  4001. *
  4002. * Returns:
  4003. * Zero on success, negative errno on failure.
  4004. */
  4005. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  4006. const char *path)
  4007. {
  4008. struct drm_device *dev = connector->dev;
  4009. int ret;
  4010. ret = drm_property_replace_global_blob(dev,
  4011. &connector->path_blob_ptr,
  4012. strlen(path) + 1,
  4013. path,
  4014. &connector->base,
  4015. dev->mode_config.path_property);
  4016. return ret;
  4017. }
  4018. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  4019. /**
  4020. * drm_mode_connector_set_tile_property - set tile property on connector
  4021. * @connector: connector to set property on.
  4022. *
  4023. * This looks up the tile information for a connector, and creates a
  4024. * property for userspace to parse if it exists. The property is of
  4025. * the form of 8 integers using ':' as a separator.
  4026. *
  4027. * Returns:
  4028. * Zero on success, errno on failure.
  4029. */
  4030. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  4031. {
  4032. struct drm_device *dev = connector->dev;
  4033. char tile[256];
  4034. int ret;
  4035. if (!connector->has_tile) {
  4036. ret = drm_property_replace_global_blob(dev,
  4037. &connector->tile_blob_ptr,
  4038. 0,
  4039. NULL,
  4040. &connector->base,
  4041. dev->mode_config.tile_property);
  4042. return ret;
  4043. }
  4044. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  4045. connector->tile_group->id, connector->tile_is_single_monitor,
  4046. connector->num_h_tile, connector->num_v_tile,
  4047. connector->tile_h_loc, connector->tile_v_loc,
  4048. connector->tile_h_size, connector->tile_v_size);
  4049. ret = drm_property_replace_global_blob(dev,
  4050. &connector->tile_blob_ptr,
  4051. strlen(tile) + 1,
  4052. tile,
  4053. &connector->base,
  4054. dev->mode_config.tile_property);
  4055. return ret;
  4056. }
  4057. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  4058. /**
  4059. * drm_mode_connector_update_edid_property - update the edid property of a connector
  4060. * @connector: drm connector
  4061. * @edid: new value of the edid property
  4062. *
  4063. * This function creates a new blob modeset object and assigns its id to the
  4064. * connector's edid property.
  4065. *
  4066. * Returns:
  4067. * Zero on success, negative errno on failure.
  4068. */
  4069. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  4070. const struct edid *edid)
  4071. {
  4072. struct drm_device *dev = connector->dev;
  4073. size_t size = 0;
  4074. int ret;
  4075. /* ignore requests to set edid when overridden */
  4076. if (connector->override_edid)
  4077. return 0;
  4078. if (edid)
  4079. size = EDID_LENGTH * (1 + edid->extensions);
  4080. ret = drm_property_replace_global_blob(dev,
  4081. &connector->edid_blob_ptr,
  4082. size,
  4083. edid,
  4084. &connector->base,
  4085. dev->mode_config.edid_property);
  4086. return ret;
  4087. }
  4088. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  4089. /* Some properties could refer to dynamic refcnt'd objects, or things that
  4090. * need special locking to handle lifetime issues (ie. to ensure the prop
  4091. * value doesn't become invalid part way through the property update due to
  4092. * race). The value returned by reference via 'obj' should be passed back
  4093. * to drm_property_change_valid_put() after the property is set (and the
  4094. * object to which the property is attached has a chance to take it's own
  4095. * reference).
  4096. */
  4097. bool drm_property_change_valid_get(struct drm_property *property,
  4098. uint64_t value, struct drm_mode_object **ref)
  4099. {
  4100. int i;
  4101. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  4102. return false;
  4103. *ref = NULL;
  4104. if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
  4105. if (value < property->values[0] || value > property->values[1])
  4106. return false;
  4107. return true;
  4108. } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
  4109. int64_t svalue = U642I64(value);
  4110. if (svalue < U642I64(property->values[0]) ||
  4111. svalue > U642I64(property->values[1]))
  4112. return false;
  4113. return true;
  4114. } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  4115. uint64_t valid_mask = 0;
  4116. for (i = 0; i < property->num_values; i++)
  4117. valid_mask |= (1ULL << property->values[i]);
  4118. return !(value & ~valid_mask);
  4119. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  4120. struct drm_property_blob *blob;
  4121. if (value == 0)
  4122. return true;
  4123. blob = drm_property_lookup_blob(property->dev, value);
  4124. if (blob) {
  4125. *ref = &blob->base;
  4126. return true;
  4127. } else {
  4128. return false;
  4129. }
  4130. } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4131. /* a zero value for an object property translates to null: */
  4132. if (value == 0)
  4133. return true;
  4134. /* handle refcnt'd objects specially: */
  4135. if (property->values[0] == DRM_MODE_OBJECT_FB) {
  4136. struct drm_framebuffer *fb;
  4137. fb = drm_framebuffer_lookup(property->dev, value);
  4138. if (fb) {
  4139. *ref = &fb->base;
  4140. return true;
  4141. } else {
  4142. return false;
  4143. }
  4144. } else {
  4145. return _object_find(property->dev, value, property->values[0]) != NULL;
  4146. }
  4147. }
  4148. for (i = 0; i < property->num_values; i++)
  4149. if (property->values[i] == value)
  4150. return true;
  4151. return false;
  4152. }
  4153. void drm_property_change_valid_put(struct drm_property *property,
  4154. struct drm_mode_object *ref)
  4155. {
  4156. if (!ref)
  4157. return;
  4158. if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4159. if (property->values[0] == DRM_MODE_OBJECT_FB)
  4160. drm_framebuffer_unreference(obj_to_fb(ref));
  4161. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  4162. drm_property_unreference_blob(obj_to_blob(ref));
  4163. }
  4164. /**
  4165. * drm_mode_connector_property_set_ioctl - set the current value of a connector property
  4166. * @dev: DRM device
  4167. * @data: ioctl data
  4168. * @file_priv: DRM file info
  4169. *
  4170. * This function sets the current value for a connectors's property. It also
  4171. * calls into a driver's ->set_property callback to update the hardware state
  4172. *
  4173. * Called by the user via ioctl.
  4174. *
  4175. * Returns:
  4176. * Zero on success, negative errno on failure.
  4177. */
  4178. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  4179. void *data, struct drm_file *file_priv)
  4180. {
  4181. struct drm_mode_connector_set_property *conn_set_prop = data;
  4182. struct drm_mode_obj_set_property obj_set_prop = {
  4183. .value = conn_set_prop->value,
  4184. .prop_id = conn_set_prop->prop_id,
  4185. .obj_id = conn_set_prop->connector_id,
  4186. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  4187. };
  4188. /* It does all the locking and checking we need */
  4189. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  4190. }
  4191. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  4192. struct drm_property *property,
  4193. uint64_t value)
  4194. {
  4195. int ret = -EINVAL;
  4196. struct drm_connector *connector = obj_to_connector(obj);
  4197. /* Do DPMS ourselves */
  4198. if (property == connector->dev->mode_config.dpms_property) {
  4199. ret = 0;
  4200. if (connector->funcs->dpms)
  4201. ret = (*connector->funcs->dpms)(connector, (int)value);
  4202. } else if (connector->funcs->set_property)
  4203. ret = connector->funcs->set_property(connector, property, value);
  4204. /* store the property value if successful */
  4205. if (!ret)
  4206. drm_object_property_set_value(&connector->base, property, value);
  4207. return ret;
  4208. }
  4209. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  4210. struct drm_property *property,
  4211. uint64_t value)
  4212. {
  4213. int ret = -EINVAL;
  4214. struct drm_crtc *crtc = obj_to_crtc(obj);
  4215. if (crtc->funcs->set_property)
  4216. ret = crtc->funcs->set_property(crtc, property, value);
  4217. if (!ret)
  4218. drm_object_property_set_value(obj, property, value);
  4219. return ret;
  4220. }
  4221. /**
  4222. * drm_mode_plane_set_obj_prop - set the value of a property
  4223. * @plane: drm plane object to set property value for
  4224. * @property: property to set
  4225. * @value: value the property should be set to
  4226. *
  4227. * This functions sets a given property on a given plane object. This function
  4228. * calls the driver's ->set_property callback and changes the software state of
  4229. * the property if the callback succeeds.
  4230. *
  4231. * Returns:
  4232. * Zero on success, error code on failure.
  4233. */
  4234. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  4235. struct drm_property *property,
  4236. uint64_t value)
  4237. {
  4238. int ret = -EINVAL;
  4239. struct drm_mode_object *obj = &plane->base;
  4240. if (plane->funcs->set_property)
  4241. ret = plane->funcs->set_property(plane, property, value);
  4242. if (!ret)
  4243. drm_object_property_set_value(obj, property, value);
  4244. return ret;
  4245. }
  4246. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  4247. /**
  4248. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  4249. * @dev: DRM device
  4250. * @data: ioctl data
  4251. * @file_priv: DRM file info
  4252. *
  4253. * This function retrieves the current value for an object's property. Compared
  4254. * to the connector specific ioctl this one is extended to also work on crtc and
  4255. * plane objects.
  4256. *
  4257. * Called by the user via ioctl.
  4258. *
  4259. * Returns:
  4260. * Zero on success, negative errno on failure.
  4261. */
  4262. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  4263. struct drm_file *file_priv)
  4264. {
  4265. struct drm_mode_obj_get_properties *arg = data;
  4266. struct drm_mode_object *obj;
  4267. int ret = 0;
  4268. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4269. return -EINVAL;
  4270. drm_modeset_lock_all(dev);
  4271. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4272. if (!obj) {
  4273. ret = -ENOENT;
  4274. goto out;
  4275. }
  4276. if (!obj->properties) {
  4277. ret = -EINVAL;
  4278. goto out;
  4279. }
  4280. ret = get_properties(obj, file_priv->atomic,
  4281. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  4282. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  4283. &arg->count_props);
  4284. out:
  4285. drm_modeset_unlock_all(dev);
  4286. return ret;
  4287. }
  4288. /**
  4289. * drm_mode_obj_set_property_ioctl - set the current value of an object's property
  4290. * @dev: DRM device
  4291. * @data: ioctl data
  4292. * @file_priv: DRM file info
  4293. *
  4294. * This function sets the current value for an object's property. It also calls
  4295. * into a driver's ->set_property callback to update the hardware state.
  4296. * Compared to the connector specific ioctl this one is extended to also work on
  4297. * crtc and plane objects.
  4298. *
  4299. * Called by the user via ioctl.
  4300. *
  4301. * Returns:
  4302. * Zero on success, negative errno on failure.
  4303. */
  4304. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  4305. struct drm_file *file_priv)
  4306. {
  4307. struct drm_mode_obj_set_property *arg = data;
  4308. struct drm_mode_object *arg_obj;
  4309. struct drm_mode_object *prop_obj;
  4310. struct drm_property *property;
  4311. int i, ret = -EINVAL;
  4312. struct drm_mode_object *ref;
  4313. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4314. return -EINVAL;
  4315. drm_modeset_lock_all(dev);
  4316. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4317. if (!arg_obj) {
  4318. ret = -ENOENT;
  4319. goto out;
  4320. }
  4321. if (!arg_obj->properties)
  4322. goto out;
  4323. for (i = 0; i < arg_obj->properties->count; i++)
  4324. if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
  4325. break;
  4326. if (i == arg_obj->properties->count)
  4327. goto out;
  4328. prop_obj = drm_mode_object_find(dev, arg->prop_id,
  4329. DRM_MODE_OBJECT_PROPERTY);
  4330. if (!prop_obj) {
  4331. ret = -ENOENT;
  4332. goto out;
  4333. }
  4334. property = obj_to_property(prop_obj);
  4335. if (!drm_property_change_valid_get(property, arg->value, &ref))
  4336. goto out;
  4337. switch (arg_obj->type) {
  4338. case DRM_MODE_OBJECT_CONNECTOR:
  4339. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  4340. arg->value);
  4341. break;
  4342. case DRM_MODE_OBJECT_CRTC:
  4343. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  4344. break;
  4345. case DRM_MODE_OBJECT_PLANE:
  4346. ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
  4347. property, arg->value);
  4348. break;
  4349. }
  4350. drm_property_change_valid_put(property, ref);
  4351. out:
  4352. drm_modeset_unlock_all(dev);
  4353. return ret;
  4354. }
  4355. /**
  4356. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  4357. * @connector: connector to attach
  4358. * @encoder: encoder to attach @connector to
  4359. *
  4360. * This function links up a connector to an encoder. Note that the routing
  4361. * restrictions between encoders and crtcs are exposed to userspace through the
  4362. * possible_clones and possible_crtcs bitmasks.
  4363. *
  4364. * Returns:
  4365. * Zero on success, negative errno on failure.
  4366. */
  4367. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  4368. struct drm_encoder *encoder)
  4369. {
  4370. int i;
  4371. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  4372. if (connector->encoder_ids[i] == 0) {
  4373. connector->encoder_ids[i] = encoder->base.id;
  4374. return 0;
  4375. }
  4376. }
  4377. return -ENOMEM;
  4378. }
  4379. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  4380. /**
  4381. * drm_mode_crtc_set_gamma_size - set the gamma table size
  4382. * @crtc: CRTC to set the gamma table size for
  4383. * @gamma_size: size of the gamma table
  4384. *
  4385. * Drivers which support gamma tables should set this to the supported gamma
  4386. * table size when initializing the CRTC. Currently the drm core only supports a
  4387. * fixed gamma table size.
  4388. *
  4389. * Returns:
  4390. * Zero on success, negative errno on failure.
  4391. */
  4392. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  4393. int gamma_size)
  4394. {
  4395. crtc->gamma_size = gamma_size;
  4396. crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
  4397. GFP_KERNEL);
  4398. if (!crtc->gamma_store) {
  4399. crtc->gamma_size = 0;
  4400. return -ENOMEM;
  4401. }
  4402. return 0;
  4403. }
  4404. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  4405. /**
  4406. * drm_mode_gamma_set_ioctl - set the gamma table
  4407. * @dev: DRM device
  4408. * @data: ioctl data
  4409. * @file_priv: DRM file info
  4410. *
  4411. * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
  4412. * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
  4413. *
  4414. * Called by the user via ioctl.
  4415. *
  4416. * Returns:
  4417. * Zero on success, negative errno on failure.
  4418. */
  4419. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  4420. void *data, struct drm_file *file_priv)
  4421. {
  4422. struct drm_mode_crtc_lut *crtc_lut = data;
  4423. struct drm_crtc *crtc;
  4424. void *r_base, *g_base, *b_base;
  4425. int size;
  4426. int ret = 0;
  4427. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4428. return -EINVAL;
  4429. drm_modeset_lock_all(dev);
  4430. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4431. if (!crtc) {
  4432. ret = -ENOENT;
  4433. goto out;
  4434. }
  4435. if (crtc->funcs->gamma_set == NULL) {
  4436. ret = -ENOSYS;
  4437. goto out;
  4438. }
  4439. /* memcpy into gamma store */
  4440. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4441. ret = -EINVAL;
  4442. goto out;
  4443. }
  4444. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4445. r_base = crtc->gamma_store;
  4446. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  4447. ret = -EFAULT;
  4448. goto out;
  4449. }
  4450. g_base = r_base + size;
  4451. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  4452. ret = -EFAULT;
  4453. goto out;
  4454. }
  4455. b_base = g_base + size;
  4456. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  4457. ret = -EFAULT;
  4458. goto out;
  4459. }
  4460. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  4461. out:
  4462. drm_modeset_unlock_all(dev);
  4463. return ret;
  4464. }
  4465. /**
  4466. * drm_mode_gamma_get_ioctl - get the gamma table
  4467. * @dev: DRM device
  4468. * @data: ioctl data
  4469. * @file_priv: DRM file info
  4470. *
  4471. * Copy the current gamma table into the storage provided. This also provides
  4472. * the gamma table size the driver expects, which can be used to size the
  4473. * allocated storage.
  4474. *
  4475. * Called by the user via ioctl.
  4476. *
  4477. * Returns:
  4478. * Zero on success, negative errno on failure.
  4479. */
  4480. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  4481. void *data, struct drm_file *file_priv)
  4482. {
  4483. struct drm_mode_crtc_lut *crtc_lut = data;
  4484. struct drm_crtc *crtc;
  4485. void *r_base, *g_base, *b_base;
  4486. int size;
  4487. int ret = 0;
  4488. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4489. return -EINVAL;
  4490. drm_modeset_lock_all(dev);
  4491. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4492. if (!crtc) {
  4493. ret = -ENOENT;
  4494. goto out;
  4495. }
  4496. /* memcpy into gamma store */
  4497. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4498. ret = -EINVAL;
  4499. goto out;
  4500. }
  4501. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4502. r_base = crtc->gamma_store;
  4503. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  4504. ret = -EFAULT;
  4505. goto out;
  4506. }
  4507. g_base = r_base + size;
  4508. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  4509. ret = -EFAULT;
  4510. goto out;
  4511. }
  4512. b_base = g_base + size;
  4513. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  4514. ret = -EFAULT;
  4515. goto out;
  4516. }
  4517. out:
  4518. drm_modeset_unlock_all(dev);
  4519. return ret;
  4520. }
  4521. /**
  4522. * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
  4523. * @dev: DRM device
  4524. * @data: ioctl data
  4525. * @file_priv: DRM file info
  4526. *
  4527. * This schedules an asynchronous update on a given CRTC, called page flip.
  4528. * Optionally a drm event is generated to signal the completion of the event.
  4529. * Generic drivers cannot assume that a pageflip with changed framebuffer
  4530. * properties (including driver specific metadata like tiling layout) will work,
  4531. * but some drivers support e.g. pixel format changes through the pageflip
  4532. * ioctl.
  4533. *
  4534. * Called by the user via ioctl.
  4535. *
  4536. * Returns:
  4537. * Zero on success, negative errno on failure.
  4538. */
  4539. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  4540. void *data, struct drm_file *file_priv)
  4541. {
  4542. struct drm_mode_crtc_page_flip *page_flip = data;
  4543. struct drm_crtc *crtc;
  4544. struct drm_framebuffer *fb = NULL;
  4545. struct drm_pending_vblank_event *e = NULL;
  4546. unsigned long flags;
  4547. int ret = -EINVAL;
  4548. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4549. return -EINVAL;
  4550. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  4551. page_flip->reserved != 0)
  4552. return -EINVAL;
  4553. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  4554. return -EINVAL;
  4555. crtc = drm_crtc_find(dev, page_flip->crtc_id);
  4556. if (!crtc)
  4557. return -ENOENT;
  4558. drm_modeset_lock_crtc(crtc, crtc->primary);
  4559. if (crtc->primary->fb == NULL) {
  4560. /* The framebuffer is currently unbound, presumably
  4561. * due to a hotplug event, that userspace has not
  4562. * yet discovered.
  4563. */
  4564. ret = -EBUSY;
  4565. goto out;
  4566. }
  4567. if (crtc->funcs->page_flip == NULL)
  4568. goto out;
  4569. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  4570. if (!fb) {
  4571. ret = -ENOENT;
  4572. goto out;
  4573. }
  4574. if (crtc->state) {
  4575. const struct drm_plane_state *state = crtc->primary->state;
  4576. ret = check_src_coords(state->src_x, state->src_y,
  4577. state->src_w, state->src_h, fb);
  4578. } else {
  4579. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  4580. }
  4581. if (ret)
  4582. goto out;
  4583. if (crtc->primary->fb->pixel_format != fb->pixel_format) {
  4584. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  4585. ret = -EINVAL;
  4586. goto out;
  4587. }
  4588. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4589. ret = -ENOMEM;
  4590. spin_lock_irqsave(&dev->event_lock, flags);
  4591. if (file_priv->event_space < sizeof(e->event)) {
  4592. spin_unlock_irqrestore(&dev->event_lock, flags);
  4593. goto out;
  4594. }
  4595. file_priv->event_space -= sizeof(e->event);
  4596. spin_unlock_irqrestore(&dev->event_lock, flags);
  4597. e = kzalloc(sizeof(*e), GFP_KERNEL);
  4598. if (e == NULL) {
  4599. spin_lock_irqsave(&dev->event_lock, flags);
  4600. file_priv->event_space += sizeof(e->event);
  4601. spin_unlock_irqrestore(&dev->event_lock, flags);
  4602. goto out;
  4603. }
  4604. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  4605. e->event.base.length = sizeof(e->event);
  4606. e->event.user_data = page_flip->user_data;
  4607. e->base.event = &e->event.base;
  4608. e->base.file_priv = file_priv;
  4609. e->base.destroy =
  4610. (void (*) (struct drm_pending_event *)) kfree;
  4611. }
  4612. crtc->primary->old_fb = crtc->primary->fb;
  4613. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  4614. if (ret) {
  4615. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4616. spin_lock_irqsave(&dev->event_lock, flags);
  4617. file_priv->event_space += sizeof(e->event);
  4618. spin_unlock_irqrestore(&dev->event_lock, flags);
  4619. kfree(e);
  4620. }
  4621. /* Keep the old fb, don't unref it. */
  4622. crtc->primary->old_fb = NULL;
  4623. } else {
  4624. crtc->primary->fb = fb;
  4625. /* Unref only the old framebuffer. */
  4626. fb = NULL;
  4627. }
  4628. out:
  4629. if (fb)
  4630. drm_framebuffer_unreference(fb);
  4631. if (crtc->primary->old_fb)
  4632. drm_framebuffer_unreference(crtc->primary->old_fb);
  4633. crtc->primary->old_fb = NULL;
  4634. drm_modeset_unlock_crtc(crtc);
  4635. return ret;
  4636. }
  4637. /**
  4638. * drm_mode_config_reset - call ->reset callbacks
  4639. * @dev: drm device
  4640. *
  4641. * This functions calls all the crtc's, encoder's and connector's ->reset
  4642. * callback. Drivers can use this in e.g. their driver load or resume code to
  4643. * reset hardware and software state.
  4644. */
  4645. void drm_mode_config_reset(struct drm_device *dev)
  4646. {
  4647. struct drm_crtc *crtc;
  4648. struct drm_plane *plane;
  4649. struct drm_encoder *encoder;
  4650. struct drm_connector *connector;
  4651. drm_for_each_plane(plane, dev)
  4652. if (plane->funcs->reset)
  4653. plane->funcs->reset(plane);
  4654. drm_for_each_crtc(crtc, dev)
  4655. if (crtc->funcs->reset)
  4656. crtc->funcs->reset(crtc);
  4657. drm_for_each_encoder(encoder, dev)
  4658. if (encoder->funcs->reset)
  4659. encoder->funcs->reset(encoder);
  4660. mutex_lock(&dev->mode_config.mutex);
  4661. drm_for_each_connector(connector, dev)
  4662. if (connector->funcs->reset)
  4663. connector->funcs->reset(connector);
  4664. mutex_unlock(&dev->mode_config.mutex);
  4665. }
  4666. EXPORT_SYMBOL(drm_mode_config_reset);
  4667. /**
  4668. * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
  4669. * @dev: DRM device
  4670. * @data: ioctl data
  4671. * @file_priv: DRM file info
  4672. *
  4673. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  4674. * TTM or something else entirely) and returns the resulting buffer handle. This
  4675. * handle can then be wrapped up into a framebuffer modeset object.
  4676. *
  4677. * Note that userspace is not allowed to use such objects for render
  4678. * acceleration - drivers must create their own private ioctls for such a use
  4679. * case.
  4680. *
  4681. * Called by the user via ioctl.
  4682. *
  4683. * Returns:
  4684. * Zero on success, negative errno on failure.
  4685. */
  4686. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  4687. void *data, struct drm_file *file_priv)
  4688. {
  4689. struct drm_mode_create_dumb *args = data;
  4690. u32 cpp, stride, size;
  4691. if (!dev->driver->dumb_create)
  4692. return -ENOSYS;
  4693. if (!args->width || !args->height || !args->bpp)
  4694. return -EINVAL;
  4695. /* overflow checks for 32bit size calculations */
  4696. /* NOTE: DIV_ROUND_UP() can overflow */
  4697. cpp = DIV_ROUND_UP(args->bpp, 8);
  4698. if (!cpp || cpp > 0xffffffffU / args->width)
  4699. return -EINVAL;
  4700. stride = cpp * args->width;
  4701. if (args->height > 0xffffffffU / stride)
  4702. return -EINVAL;
  4703. /* test for wrap-around */
  4704. size = args->height * stride;
  4705. if (PAGE_ALIGN(size) == 0)
  4706. return -EINVAL;
  4707. /*
  4708. * handle, pitch and size are output parameters. Zero them out to
  4709. * prevent drivers from accidentally using uninitialized data. Since
  4710. * not all existing userspace is clearing these fields properly we
  4711. * cannot reject IOCTL with garbage in them.
  4712. */
  4713. args->handle = 0;
  4714. args->pitch = 0;
  4715. args->size = 0;
  4716. return dev->driver->dumb_create(file_priv, dev, args);
  4717. }
  4718. /**
  4719. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  4720. * @dev: DRM device
  4721. * @data: ioctl data
  4722. * @file_priv: DRM file info
  4723. *
  4724. * Allocate an offset in the drm device node's address space to be able to
  4725. * memory map a dumb buffer.
  4726. *
  4727. * Called by the user via ioctl.
  4728. *
  4729. * Returns:
  4730. * Zero on success, negative errno on failure.
  4731. */
  4732. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  4733. void *data, struct drm_file *file_priv)
  4734. {
  4735. struct drm_mode_map_dumb *args = data;
  4736. /* call driver ioctl to get mmap offset */
  4737. if (!dev->driver->dumb_map_offset)
  4738. return -ENOSYS;
  4739. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  4740. }
  4741. /**
  4742. * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
  4743. * @dev: DRM device
  4744. * @data: ioctl data
  4745. * @file_priv: DRM file info
  4746. *
  4747. * This destroys the userspace handle for the given dumb backing storage buffer.
  4748. * Since buffer objects must be reference counted in the kernel a buffer object
  4749. * won't be immediately freed if a framebuffer modeset object still uses it.
  4750. *
  4751. * Called by the user via ioctl.
  4752. *
  4753. * Returns:
  4754. * Zero on success, negative errno on failure.
  4755. */
  4756. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  4757. void *data, struct drm_file *file_priv)
  4758. {
  4759. struct drm_mode_destroy_dumb *args = data;
  4760. if (!dev->driver->dumb_destroy)
  4761. return -ENOSYS;
  4762. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  4763. }
  4764. /**
  4765. * drm_fb_get_bpp_depth - get the bpp/depth values for format
  4766. * @format: pixel format (DRM_FORMAT_*)
  4767. * @depth: storage for the depth value
  4768. * @bpp: storage for the bpp value
  4769. *
  4770. * This only supports RGB formats here for compat with code that doesn't use
  4771. * pixel formats directly yet.
  4772. */
  4773. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  4774. int *bpp)
  4775. {
  4776. switch (format) {
  4777. case DRM_FORMAT_C8:
  4778. case DRM_FORMAT_RGB332:
  4779. case DRM_FORMAT_BGR233:
  4780. *depth = 8;
  4781. *bpp = 8;
  4782. break;
  4783. case DRM_FORMAT_XRGB1555:
  4784. case DRM_FORMAT_XBGR1555:
  4785. case DRM_FORMAT_RGBX5551:
  4786. case DRM_FORMAT_BGRX5551:
  4787. case DRM_FORMAT_ARGB1555:
  4788. case DRM_FORMAT_ABGR1555:
  4789. case DRM_FORMAT_RGBA5551:
  4790. case DRM_FORMAT_BGRA5551:
  4791. *depth = 15;
  4792. *bpp = 16;
  4793. break;
  4794. case DRM_FORMAT_RGB565:
  4795. case DRM_FORMAT_BGR565:
  4796. *depth = 16;
  4797. *bpp = 16;
  4798. break;
  4799. case DRM_FORMAT_RGB888:
  4800. case DRM_FORMAT_BGR888:
  4801. *depth = 24;
  4802. *bpp = 24;
  4803. break;
  4804. case DRM_FORMAT_XRGB8888:
  4805. case DRM_FORMAT_XBGR8888:
  4806. case DRM_FORMAT_RGBX8888:
  4807. case DRM_FORMAT_BGRX8888:
  4808. *depth = 24;
  4809. *bpp = 32;
  4810. break;
  4811. case DRM_FORMAT_XRGB2101010:
  4812. case DRM_FORMAT_XBGR2101010:
  4813. case DRM_FORMAT_RGBX1010102:
  4814. case DRM_FORMAT_BGRX1010102:
  4815. case DRM_FORMAT_ARGB2101010:
  4816. case DRM_FORMAT_ABGR2101010:
  4817. case DRM_FORMAT_RGBA1010102:
  4818. case DRM_FORMAT_BGRA1010102:
  4819. *depth = 30;
  4820. *bpp = 32;
  4821. break;
  4822. case DRM_FORMAT_ARGB8888:
  4823. case DRM_FORMAT_ABGR8888:
  4824. case DRM_FORMAT_RGBA8888:
  4825. case DRM_FORMAT_BGRA8888:
  4826. *depth = 32;
  4827. *bpp = 32;
  4828. break;
  4829. default:
  4830. DRM_DEBUG_KMS("unsupported pixel format %s\n",
  4831. drm_get_format_name(format));
  4832. *depth = 0;
  4833. *bpp = 0;
  4834. break;
  4835. }
  4836. }
  4837. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  4838. /**
  4839. * drm_format_num_planes - get the number of planes for format
  4840. * @format: pixel format (DRM_FORMAT_*)
  4841. *
  4842. * Returns:
  4843. * The number of planes used by the specified pixel format.
  4844. */
  4845. int drm_format_num_planes(uint32_t format)
  4846. {
  4847. switch (format) {
  4848. case DRM_FORMAT_YUV410:
  4849. case DRM_FORMAT_YVU410:
  4850. case DRM_FORMAT_YUV411:
  4851. case DRM_FORMAT_YVU411:
  4852. case DRM_FORMAT_YUV420:
  4853. case DRM_FORMAT_YVU420:
  4854. case DRM_FORMAT_YUV422:
  4855. case DRM_FORMAT_YVU422:
  4856. case DRM_FORMAT_YUV444:
  4857. case DRM_FORMAT_YVU444:
  4858. return 3;
  4859. case DRM_FORMAT_NV12:
  4860. case DRM_FORMAT_NV21:
  4861. case DRM_FORMAT_NV16:
  4862. case DRM_FORMAT_NV61:
  4863. case DRM_FORMAT_NV24:
  4864. case DRM_FORMAT_NV42:
  4865. return 2;
  4866. default:
  4867. return 1;
  4868. }
  4869. }
  4870. EXPORT_SYMBOL(drm_format_num_planes);
  4871. /**
  4872. * drm_format_plane_cpp - determine the bytes per pixel value
  4873. * @format: pixel format (DRM_FORMAT_*)
  4874. * @plane: plane index
  4875. *
  4876. * Returns:
  4877. * The bytes per pixel value for the specified plane.
  4878. */
  4879. int drm_format_plane_cpp(uint32_t format, int plane)
  4880. {
  4881. unsigned int depth;
  4882. int bpp;
  4883. if (plane >= drm_format_num_planes(format))
  4884. return 0;
  4885. switch (format) {
  4886. case DRM_FORMAT_YUYV:
  4887. case DRM_FORMAT_YVYU:
  4888. case DRM_FORMAT_UYVY:
  4889. case DRM_FORMAT_VYUY:
  4890. return 2;
  4891. case DRM_FORMAT_NV12:
  4892. case DRM_FORMAT_NV21:
  4893. case DRM_FORMAT_NV16:
  4894. case DRM_FORMAT_NV61:
  4895. case DRM_FORMAT_NV24:
  4896. case DRM_FORMAT_NV42:
  4897. return plane ? 2 : 1;
  4898. case DRM_FORMAT_YUV410:
  4899. case DRM_FORMAT_YVU410:
  4900. case DRM_FORMAT_YUV411:
  4901. case DRM_FORMAT_YVU411:
  4902. case DRM_FORMAT_YUV420:
  4903. case DRM_FORMAT_YVU420:
  4904. case DRM_FORMAT_YUV422:
  4905. case DRM_FORMAT_YVU422:
  4906. case DRM_FORMAT_YUV444:
  4907. case DRM_FORMAT_YVU444:
  4908. return 1;
  4909. default:
  4910. drm_fb_get_bpp_depth(format, &depth, &bpp);
  4911. return bpp >> 3;
  4912. }
  4913. }
  4914. EXPORT_SYMBOL(drm_format_plane_cpp);
  4915. /**
  4916. * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  4917. * @format: pixel format (DRM_FORMAT_*)
  4918. *
  4919. * Returns:
  4920. * The horizontal chroma subsampling factor for the
  4921. * specified pixel format.
  4922. */
  4923. int drm_format_horz_chroma_subsampling(uint32_t format)
  4924. {
  4925. switch (format) {
  4926. case DRM_FORMAT_YUV411:
  4927. case DRM_FORMAT_YVU411:
  4928. case DRM_FORMAT_YUV410:
  4929. case DRM_FORMAT_YVU410:
  4930. return 4;
  4931. case DRM_FORMAT_YUYV:
  4932. case DRM_FORMAT_YVYU:
  4933. case DRM_FORMAT_UYVY:
  4934. case DRM_FORMAT_VYUY:
  4935. case DRM_FORMAT_NV12:
  4936. case DRM_FORMAT_NV21:
  4937. case DRM_FORMAT_NV16:
  4938. case DRM_FORMAT_NV61:
  4939. case DRM_FORMAT_YUV422:
  4940. case DRM_FORMAT_YVU422:
  4941. case DRM_FORMAT_YUV420:
  4942. case DRM_FORMAT_YVU420:
  4943. return 2;
  4944. default:
  4945. return 1;
  4946. }
  4947. }
  4948. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  4949. /**
  4950. * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  4951. * @format: pixel format (DRM_FORMAT_*)
  4952. *
  4953. * Returns:
  4954. * The vertical chroma subsampling factor for the
  4955. * specified pixel format.
  4956. */
  4957. int drm_format_vert_chroma_subsampling(uint32_t format)
  4958. {
  4959. switch (format) {
  4960. case DRM_FORMAT_YUV410:
  4961. case DRM_FORMAT_YVU410:
  4962. return 4;
  4963. case DRM_FORMAT_YUV420:
  4964. case DRM_FORMAT_YVU420:
  4965. case DRM_FORMAT_NV12:
  4966. case DRM_FORMAT_NV21:
  4967. return 2;
  4968. default:
  4969. return 1;
  4970. }
  4971. }
  4972. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  4973. /**
  4974. * drm_rotation_simplify() - Try to simplify the rotation
  4975. * @rotation: Rotation to be simplified
  4976. * @supported_rotations: Supported rotations
  4977. *
  4978. * Attempt to simplify the rotation to a form that is supported.
  4979. * Eg. if the hardware supports everything except DRM_REFLECT_X
  4980. * one could call this function like this:
  4981. *
  4982. * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
  4983. * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
  4984. * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
  4985. *
  4986. * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
  4987. * transforms the hardware supports, this function may not
  4988. * be able to produce a supported transform, so the caller should
  4989. * check the result afterwards.
  4990. */
  4991. unsigned int drm_rotation_simplify(unsigned int rotation,
  4992. unsigned int supported_rotations)
  4993. {
  4994. if (rotation & ~supported_rotations) {
  4995. rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
  4996. rotation = (rotation & DRM_REFLECT_MASK) |
  4997. BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
  4998. }
  4999. return rotation;
  5000. }
  5001. EXPORT_SYMBOL(drm_rotation_simplify);
  5002. /**
  5003. * drm_mode_config_init - initialize DRM mode_configuration structure
  5004. * @dev: DRM device
  5005. *
  5006. * Initialize @dev's mode_config structure, used for tracking the graphics
  5007. * configuration of @dev.
  5008. *
  5009. * Since this initializes the modeset locks, no locking is possible. Which is no
  5010. * problem, since this should happen single threaded at init time. It is the
  5011. * driver's problem to ensure this guarantee.
  5012. *
  5013. */
  5014. void drm_mode_config_init(struct drm_device *dev)
  5015. {
  5016. mutex_init(&dev->mode_config.mutex);
  5017. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  5018. mutex_init(&dev->mode_config.idr_mutex);
  5019. mutex_init(&dev->mode_config.fb_lock);
  5020. mutex_init(&dev->mode_config.blob_lock);
  5021. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  5022. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  5023. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  5024. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  5025. INIT_LIST_HEAD(&dev->mode_config.property_list);
  5026. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  5027. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  5028. idr_init(&dev->mode_config.crtc_idr);
  5029. idr_init(&dev->mode_config.tile_idr);
  5030. drm_modeset_lock_all(dev);
  5031. drm_mode_create_standard_properties(dev);
  5032. drm_modeset_unlock_all(dev);
  5033. /* Just to be sure */
  5034. dev->mode_config.num_fb = 0;
  5035. dev->mode_config.num_connector = 0;
  5036. dev->mode_config.num_crtc = 0;
  5037. dev->mode_config.num_encoder = 0;
  5038. dev->mode_config.num_overlay_plane = 0;
  5039. dev->mode_config.num_total_plane = 0;
  5040. }
  5041. EXPORT_SYMBOL(drm_mode_config_init);
  5042. /**
  5043. * drm_mode_config_cleanup - free up DRM mode_config info
  5044. * @dev: DRM device
  5045. *
  5046. * Free up all the connectors and CRTCs associated with this DRM device, then
  5047. * free up the framebuffers and associated buffer objects.
  5048. *
  5049. * Note that since this /should/ happen single-threaded at driver/device
  5050. * teardown time, no locking is required. It's the driver's job to ensure that
  5051. * this guarantee actually holds true.
  5052. *
  5053. * FIXME: cleanup any dangling user buffer objects too
  5054. */
  5055. void drm_mode_config_cleanup(struct drm_device *dev)
  5056. {
  5057. struct drm_connector *connector, *ot;
  5058. struct drm_crtc *crtc, *ct;
  5059. struct drm_encoder *encoder, *enct;
  5060. struct drm_framebuffer *fb, *fbt;
  5061. struct drm_property *property, *pt;
  5062. struct drm_property_blob *blob, *bt;
  5063. struct drm_plane *plane, *plt;
  5064. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  5065. head) {
  5066. encoder->funcs->destroy(encoder);
  5067. }
  5068. list_for_each_entry_safe(connector, ot,
  5069. &dev->mode_config.connector_list, head) {
  5070. connector->funcs->destroy(connector);
  5071. }
  5072. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  5073. head) {
  5074. drm_property_destroy(dev, property);
  5075. }
  5076. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  5077. head_global) {
  5078. drm_property_unreference_blob(blob);
  5079. }
  5080. /*
  5081. * Single-threaded teardown context, so it's not required to grab the
  5082. * fb_lock to protect against concurrent fb_list access. Contrary, it
  5083. * would actually deadlock with the drm_framebuffer_cleanup function.
  5084. *
  5085. * Also, if there are any framebuffers left, that's a driver leak now,
  5086. * so politely WARN about this.
  5087. */
  5088. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  5089. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  5090. drm_framebuffer_free(&fb->refcount);
  5091. }
  5092. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  5093. head) {
  5094. plane->funcs->destroy(plane);
  5095. }
  5096. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  5097. crtc->funcs->destroy(crtc);
  5098. }
  5099. idr_destroy(&dev->mode_config.tile_idr);
  5100. idr_destroy(&dev->mode_config.crtc_idr);
  5101. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  5102. }
  5103. EXPORT_SYMBOL(drm_mode_config_cleanup);
  5104. struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
  5105. unsigned int supported_rotations)
  5106. {
  5107. static const struct drm_prop_enum_list props[] = {
  5108. { DRM_ROTATE_0, "rotate-0" },
  5109. { DRM_ROTATE_90, "rotate-90" },
  5110. { DRM_ROTATE_180, "rotate-180" },
  5111. { DRM_ROTATE_270, "rotate-270" },
  5112. { DRM_REFLECT_X, "reflect-x" },
  5113. { DRM_REFLECT_Y, "reflect-y" },
  5114. };
  5115. return drm_property_create_bitmask(dev, 0, "rotation",
  5116. props, ARRAY_SIZE(props),
  5117. supported_rotations);
  5118. }
  5119. EXPORT_SYMBOL(drm_mode_create_rotation_property);
  5120. /**
  5121. * DOC: Tile group
  5122. *
  5123. * Tile groups are used to represent tiled monitors with a unique
  5124. * integer identifier. Tiled monitors using DisplayID v1.3 have
  5125. * a unique 8-byte handle, we store this in a tile group, so we
  5126. * have a common identifier for all tiles in a monitor group.
  5127. */
  5128. static void drm_tile_group_free(struct kref *kref)
  5129. {
  5130. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  5131. struct drm_device *dev = tg->dev;
  5132. mutex_lock(&dev->mode_config.idr_mutex);
  5133. idr_remove(&dev->mode_config.tile_idr, tg->id);
  5134. mutex_unlock(&dev->mode_config.idr_mutex);
  5135. kfree(tg);
  5136. }
  5137. /**
  5138. * drm_mode_put_tile_group - drop a reference to a tile group.
  5139. * @dev: DRM device
  5140. * @tg: tile group to drop reference to.
  5141. *
  5142. * drop reference to tile group and free if 0.
  5143. */
  5144. void drm_mode_put_tile_group(struct drm_device *dev,
  5145. struct drm_tile_group *tg)
  5146. {
  5147. kref_put(&tg->refcount, drm_tile_group_free);
  5148. }
  5149. /**
  5150. * drm_mode_get_tile_group - get a reference to an existing tile group
  5151. * @dev: DRM device
  5152. * @topology: 8-bytes unique per monitor.
  5153. *
  5154. * Use the unique bytes to get a reference to an existing tile group.
  5155. *
  5156. * RETURNS:
  5157. * tile group or NULL if not found.
  5158. */
  5159. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  5160. char topology[8])
  5161. {
  5162. struct drm_tile_group *tg;
  5163. int id;
  5164. mutex_lock(&dev->mode_config.idr_mutex);
  5165. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  5166. if (!memcmp(tg->group_data, topology, 8)) {
  5167. if (!kref_get_unless_zero(&tg->refcount))
  5168. tg = NULL;
  5169. mutex_unlock(&dev->mode_config.idr_mutex);
  5170. return tg;
  5171. }
  5172. }
  5173. mutex_unlock(&dev->mode_config.idr_mutex);
  5174. return NULL;
  5175. }
  5176. EXPORT_SYMBOL(drm_mode_get_tile_group);
  5177. /**
  5178. * drm_mode_create_tile_group - create a tile group from a displayid description
  5179. * @dev: DRM device
  5180. * @topology: 8-bytes unique per monitor.
  5181. *
  5182. * Create a tile group for the unique monitor, and get a unique
  5183. * identifier for the tile group.
  5184. *
  5185. * RETURNS:
  5186. * new tile group or error.
  5187. */
  5188. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  5189. char topology[8])
  5190. {
  5191. struct drm_tile_group *tg;
  5192. int ret;
  5193. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  5194. if (!tg)
  5195. return ERR_PTR(-ENOMEM);
  5196. kref_init(&tg->refcount);
  5197. memcpy(tg->group_data, topology, 8);
  5198. tg->dev = dev;
  5199. mutex_lock(&dev->mode_config.idr_mutex);
  5200. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  5201. if (ret >= 0) {
  5202. tg->id = ret;
  5203. } else {
  5204. kfree(tg);
  5205. tg = ERR_PTR(ret);
  5206. }
  5207. mutex_unlock(&dev->mode_config.idr_mutex);
  5208. return tg;
  5209. }
  5210. EXPORT_SYMBOL(drm_mode_create_tile_group);