drm_crtc.c 158 KB

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