drm_crtc.c 136 KB

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